Home > Software engineering >  The data of binary image
The data of binary image

Time:09-20

Excuse me a BMP format of binary image pixel is expressed in a byte or expressed in a bit? If there is a said a pixel, that how to access the pixels?

CodePudding user response:

Refer to
/* parameter description:
* strSrc - source files (8 bits BMP grayscale)
* strDst - the target file (one BMP binary chart)//a byte 8 point
* threshold - a threshold
*/
BOOL GraytoBinary (const char * strSrc, const char * strDst, const int threshold)
{
BITMAPFILEHEADER fileheader.//file header
BITMAPINFOHEADER bmpinfo;//information head
The FILE * fSrc;//the source file (grayscale)
fSrc=https://bbs.csdn.net/topics/fopen (strSrc, "rb");
if( ! FSrc)
{
return false;
}
Fread (& amp; Fileheader, sizeof (BITMAPFILEHEADER), 1, fSrc);//read the source file header
Fread (& amp; Bmpinfo, sizeof (BITMAPINFOHEADER), 1, fSrc);//read the source file header
//const int SecBPP=8/bmpinfo biBitCount;//the source file for each byte sizes
Const int SrcBitCount=bmpinfo. BiBitCount;//the source file of a deep
Const int SrcRowLen=(((bmpinfo biWidth * bmpinfo biBitCount + 31)/8)/4) * 4;//520 source bytes per
Const int SrcClrNum=bmpinfo. BiClrUsed? Bmpinfo. BiClrUsed: 256;//the original image color table number (8 256 colors)
//read the SRC colors!
//RGBQUAD SrcClr [256].
//fread (SrcClr, sizeof (RGBQUAD), SrcClrNum, fSrc);
//or seek to data
Fseek (fSrc, sizeof (RGBQUAD) * SrcClrNum, SEEK_CUR);
//modify the file information is: the target file information
Bmpinfo. BiBitCount=1;//binary chart of bit depth
Bmpinfo. BiClrImportant=0;//all colors are very important
Bmpinfo. BiClrUsed=2;//there are two color chart
Bmpinfo. BiCompression=0;//no compression
Const int DstBPP=8; //binary map each byte sizes
Const int DstRowLen=(((bmpinfo biWidth * bmpinfo biBitCount + 31)/8)/4) * 4;//68 binary figure bytes per
Fileheader. BfSize=sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) +
2 * sizeof (RGBQUAD) + bmpinfo. BiHeight * DstRowLen;//binary map file size
Fileheader. BfOffBits=sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) +
2 * sizeof (RGBQUAD);//binary chart of the bitmap data offset
Bmpinfo. BiSizeImage=bmpinfo. BiHeight * DstRowLen;//binary figure image data size
//create the target file
The FILE * fDst;
FDst=fopen (strDst, "wb");
if( ! FDst)
{
return false;
}
Fwrite (& amp; Fileheader, sizeof (BITMAPFILEHEADER), 1, fDst);//write binary map file header
Fwrite (& amp; Bmpinfo, sizeof (BITMAPINFOHEADER), 1, fDst);//write binary map information head

The RGBQUAD clrDst [2];//temporary variables: the original image (8) color table (1) and the target image color chart
//clrDst [2] the color of the array to store specific information, binary image data storage is an array subscript
ClrDst [0]. RgbBlue=0;
ClrDst [0]. RgbGreen=0;
ClrDst [0]. RgbRed=0;
ClrDst [0]. RgbReserved=0;//0 means black
ClrDst [1]. RgbBlue=255;
ClrDst [1]. RgbGreen=255;
ClrDst [1]. RgbRed=255;
ClrDst [1]. RgbReserved=0;//1 said white
For (int ii=0; Ii & lt; 2; Ii++)
{
Fwrite (& amp; ClrDst [ii], sizeof (RGBQUAD), 1, fDst);//to write color table into the object file
}

BYTE * bufSrc=https://bbs.csdn.net/topics/new BYTE [SrcRowLen];//the source file (grayscale) to store a line pixel buffers
BYTE * bufDst=new BYTE [DstRowLen];//the target file (binary chart) to store a line pixel buffer

for(int i=0; i {
Fread (bufSrc SrcRowLen, 1, fSrc);//read the source file (8 bits of a bitmap) the rows of data I
Memset (bufDst, 0, DstRowLen);//the target file (binary figure) the rows of data I reset
For (int j=0; J & lt; Bmpinfo. BiWidth; J++)
{
Int index=j/DstBPP; //every 8 bytes read from the source file, the target file by one byte
Int nShift=8 - (DstBPP j % + 1) * bmpinfo biBitCount;//version bit
//nShift=7 June 5 4 3 2 1
If (bufSrc [j] BufDst [index] & amp;=~ (1 & lt; The else
BufDst [index] |=(1 & lt; }
Fwrite (bufDst DstRowLen, 1, fDst);//write this row
}
//
The fclose (fSrc);
The fclose (fDst);
//
The delete [] bufSrc;
The delete [] bufDst;
return true;
}

//GraytoBinary (" Girl8. BMP ", "Girl2. BMP, 28).//
  • Related