1. 程式人生 > >二進位制圖片轉換成BMP點陣圖(C++)

二進位制圖片轉換成BMP點陣圖(C++)



void saveBitmap()  

{  

//一個586*586的二進位制圖片

const int height = 586;
const int width = 586;

//新生產bmp圖片資料塊總大小 (bmpWidth *biBitCount/8+3)/4*4*bmpHeight;
const int size = 588 *586 ;


// Part.1 Create Bitmap File Header  
BITMAPFILEHEADER fileHeader;  


fileHeader.bfType = 0x4D42;  
fileHeader.bfReserved1 = 0;  
fileHeader.bfReserved2 = 0;  


fileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + size + 256 * sizeof(RGBQUAD);  
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);


// Part.2 Create Bitmap Info Header  
BITMAPINFOHEADER bitmapHeader = { 0 };  


bitmapHeader.biSize = sizeof(BITMAPINFOHEADER);  
bitmapHeader.biHeight = height;  
bitmapHeader.biWidth = width;  
bitmapHeader.biPlanes = 1;  
bitmapHeader.biBitCount =8;
bitmapHeader.biSizeImage = 0;  
bitmapHeader.biXPelsPerMeter = 0;
bitmapHeader.biYPelsPerMeter = 0;
bitmapHeader.biClrUsed = 256;
bitmapHeader.biClrImportant = 0;
bitmapHeader.biCompression = 0;


int pad;
int biWidth = 0;
int padWidth = 0 ;


if(bitmapHeader.biBitCount==24)
pad = 4 - ((bitmapHeader.biWidth * 3) % 4);
else
pad = 4 - ((bitmapHeader.biWidth) % 4);
if (pad == 4)      
pad = 0;           
padWidth = bitmapHeader.biWidth + pad;


RGBQUAD* pRgbQuards=new RGBQUAD[256]; 
for(int i=0;i<256;i++) 

pRgbQuards[i].rgbBlue=i; 
pRgbQuards[i].rgbRed=i; 
pRgbQuards[i].rgbGreen=i; 



int filesize = files.size();
for (int i= 0;i <filesize; i++)
{
FILE *imgp;
string filename = files[i];
fopen_s(&imgp,filename.data(),"rb");


if (imgp == NULL)
{
return ;
}
BYTE *bits = (BYTE *)malloc(586*586);
BYTE *bits0 = (BYTE *)malloc(1); 
BYTE *bits1 = (BYTE *)malloc(1); 
BYTE *bits2 = (BYTE *)malloc(1); 
BYTE *bits3 = (BYTE *)malloc(586*586);


for (int j = 0; j<586*586;j++)
{
memset(bits1, 0xFF, 1); 
memset(bits2, 0xFF, 1); 
fread(bits1,sizeof(char),1,imgp);
fread(bits2,sizeof(char),1,imgp);
bits3[j] = (((*bits1)<<6)| ((*bits2 )>>2));

fclose(imgp);


string Bmpfilename = filename.substr(0,filename.length()- 3) + "bmp";


FILE *output = fopen(Bmpfilename.data(), "wb");  
if (output == NULL)  
{  
printf("Cannot open file!\n");  
}  
else  
{  
fwrite(&fileHeader,1,sizeof(BITMAPFILEHEADER),output);  
fwrite(&bitmapHeader,1,sizeof(BITMAPINFOHEADER),output);  
fwrite(pRgbQuards,sizeof(RGBQUAD),256,output); 
//fwrite(outbfr,sizeof(BYTE),586*586,output);
char *by = "0x00"; 
for (int n= 586; n >0 ; n--)
{
for (int m = 0;m < 586; m++)
{
bits0[0] =bits3[(n-1)*586 +m];
fwrite(bits0,sizeof(BYTE),1,output);
}
for(int i = 1; i <= pad; i++)
{
fwrite(by,sizeof(BYTE),1,output);
}
}
}
fclose(output);
free(bits); 
free(bits0); 
free(bits1); 
free(bits2); 
free(bits3); 
bits = NULL;
bits0= NULL;
bits1 = NULL;
bits2 = NULL;
bits3 = NULL;


string Pngfilename = filename.substr(0,filename.length()- 3) + "png";

LPCWSTR  Bmpname = stringToLPCWSTR(Bmpfilename);
LPCWSTR  Pngname = stringToLPCWSTR(Pngfilename);

//將BMP圖片轉換成PNG圖片  詳見下一篇博文
BMptoPNG(Bmpname,Pngname);  
DeleteFile(Bmpname);
}
}