C++读取bmp图片的例子
1 #include2 #include 3 #include 4 #include <malloc.h> 5 6 #define WIDTHBYTES(x) ((x+31)/32*4) 7 #define my(x) ( ( (x + 31) & ~31) / 8) 8 9 BITMAPFILEHEADER bf; //BMP文件头结构体 10 BITMAPINFOHEADER bi; //BMP信息头结构体 11 12 int i,j; 13 RGBQUAD *ipRGB; //颜色表 14 DWORD NumColors; //颜色表数目 15 unsigned char ** Imgdata; 16 17 void read(char*); 18 void display(); 19 void org(char*); 20 void gray(char*); 21 22 void test() 23 { 24 DWORD LineByte; 25 DWORD ImgSize; 26 LineByte=(DWORD)my(bi.biWidth*bi.biBitCount); //计算位图的实际宽度并确保它为32的倍数 27 ImgSize=(DWORD)LineByte*bi.biHeight; 28 printf("LineByte is %d. \n", LineByte); 29 printf("ImgSize is %d. \n", ImgSize); 30 } 31 32 int main() 33 { 34 char *filename; 35 filename="U616.bmp"; 36 read(filename); 37 display(); 38 test(); 39 org("123.bmp"); 40 free(Imgdata); 41 // printf("<2>"); 42 // display(); 43 free(ipRGB); 44 system("pause"); 45 return 0; 46 } 47 48 void read(char* filename) 49 { 50 FILE* fp; 51 fp=fopen(filename,"rb"); 52 if(fp == NULL) 53 { 54 printf("Open file error!"); 55 exit(0); 56 } 57 58 //读取信息头、文件头 59 fread(&bf,sizeof(BITMAPFILEHEADER),1,fp); 60 fread(&bi,sizeof(BITMAPINFOHEADER),1,fp); 61 62 if (bi.biClrUsed != 0 ) 63 NumColors=(DWORD)bi.biClrUsed; 64 else 65 switch (bi.biBitCount) 66 { 67 case 1: NumColors=2; break; 68 case 4: NumColors=16; break; 69 case 8: NumColors=256; break; 70 case 24: NumColors=0; break; 71 } 72 73 //分配调色板内存 74 ipRGB=(RGBQUAD *)malloc(NumColors*sizeof(RGBQUAD)); 75 fread(ipRGB,sizeof(RGBQUAD),NumColors,fp); 76 77 Imgdata=new unsigned char*[bi.biHeight]; //声明一个指针数组 78 if(bi.biBitCount==24) 79 { 80 for ( i=(bi.biHeight)-1;i>=0;i--) 81 Imgdata[i]=new unsigned char[bi.biWidth*3]; //每个数组元素也是一个指针数组 82 83 for ( i=(bi.biHeight)-1;i>=0;i--) 84 for(j=0;j 3;j++) 85 fread(&Imgdata[i][j],1,1,fp);//每次只读取一个1字节,存入数组 86 } 87 else 88 { 89 for ( i=(bi.biHeight)-1;i>=0;i--) 90 Imgdata[i]=new unsigned char[bi.biWidth]; 91 92 for ( i=(bi.biHeight)-1;i>=0;i--) 93 for(j=0;j ) 94 fread(&Imgdata[i][j],1,1,fp); 95 } 96 97 fclose(fp); 98 } 99 100 void org(char *filename) 101 { 102 FILE* fp; 103 fp=fopen(filename,"wb"); 104 fwrite(&bf,sizeof(BITMAPFILEHEADER),1,fp); //写文件头 105 fwrite(&bi,sizeof(BITMAPINFOHEADER),1,fp); //写信息头 106 fwrite(ipRGB,sizeof(RGBQUAD),NumColors,fp);//写颜色头 107 108 if(bi.biBitCount==24) 109 for (i=(bi.biHeight)-1 ;i>=0;i--) 110 for (j=0 ;j 3);j++) 111 fwrite(&Imgdata[i][j],1,1,fp); 112 else 113 for (i=(bi.biHeight)-1 ;i>=0;i--) 114 for (j=0 ;j ) 115 fwrite(&Imgdata[i][j],1,1,fp); 116 fclose(fp); 117 } 118 119 void gray(char *filename) 120 { 121 FILE* fp; 122 fp=fopen(filename,"wb"); 123 /* 124 bf.bfOffBits=(DWORD)1078; 125 bi.biSizeImage=(DWORD)(bi.biWidth*bi.biHeight); 126 bf.bfSize=bi.biSizeImage+bf.bfOffBits; 127 bi.biBitCount=(WORD)8; 128 bi.biClrUsed=(DWORD)256; 129 bi.biClrImportant=(DWORD)256; 130 bi.biXPelsPerMeter=(LONG)0; 131 bi.biYPelsPerMeter=(LONG)0; 132 NumColors=(DWORD)bi.biClrUsed; 133 ipRGB=(RGBQUAD *)malloc(NumColors*sizeof(RGBQUAD)); 134 */ 135 fwrite(&bf,sizeof(BITMAPFILEHEADER),1,fp); //写文件头 136 fwrite(&bi,sizeof(BITMAPINFOHEADER),1,fp); //写信息头 137 fwrite(ipRGB,sizeof(RGBQUAD),NumColors,fp);//写颜色头 138 139 char temp; 140 for (i=(bi.biHeight)-1 ;i>=0;i--) 141 for (j=0 ;j 3;j++) 142 if(j%3==0) 143 { 144 temp=(Imgdata[i][j]*76+Imgdata[i][j+1]*151+Imgdata[i][j+2]*28)>>8; 145 fwrite(&temp,1,1,fp); 146 } 147 else 148 { 149 fwrite(&temp,1,1,fp); 150 } 151 fclose(fp); 152 } 153 154 void display() 155 { 156 printf("tagBITMAPFILEHEADER info is: %d\n",sizeof(BITMAPFILEHEADER)); 157 printf("bfType is %d.\n", bf.bfType); 158 printf("bfSize is %d.\n", bf.bfSize); 159 printf("bfReserved1 is %d.\n", bf.bfReserved1); 160 printf("bfReserved2 is %d.\n", bf.bfReserved2); 161 printf("bfOffBits is %d.\n", bf.bfOffBits); 162 163 printf("\ntagBITMAPINFOHEADER info is: %d\n",sizeof(BITMAPINFOHEADER)); 164 printf("biSize is %d. \n", bi.biSize); 165 printf("biWidth is %ld.\n", bi.biWidth); 166 printf("biHeight is %ld.\n", bi.biHeight); 167 printf("biPlanes is %d. \n", bi.biPlanes); 168 printf("biBitCount is %d. \n", bi.biBitCount); //每像素所需位数 169 printf("biCompression is %d. \n", bi.biCompression); 170 printf("biSizeImage is %d. \n", bi.biSizeImage); 171 printf("biXPelsPerMerer is %ld.\n", bi.biXPelsPerMeter); //每米像素数 172 printf("biYPelsPerMerer is %ld.\n", bi.biYPelsPerMeter); 173 printf("biClrUsed is %d. \n", bi.biClrUsed); 174 printf("biClrImportant is %d. \n", bi.biClrImportant); 175 printf("\nNum of colors is %d. \n", NumColors); 176 }