码迷,mamicode.com
首页 > 其他好文 > 详细

读写bmp图片

时间:2015-12-01 22:47:34      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

  1 //读写bmp图像,好久的前写的,放这做记录吧
  2 
  3 
  4 //h
  5 #pragma once
  6 #include <string>
  7 using std::string;
  8 
  9 class BmpRW
 10 {
 11 public:
 12     BmpRW(void);
 13     ~BmpRW(void);
 14 
 15 public:
 16     char* R8Bitmap(char *imName, int &imWidth, int &imHeight);
 17     bool W8BitBmp(char *imBuffer, int imWidth, int imHeight, char *imName); 
 18 
 19     char* R8BitmapC(const string &sName, int &iWidth, int &iHeight);
 20     bool W8BitBmpC(unsigned char *cBuffer, int iWidth, int iHeight, char *cName);
 21 
 22     char* R24BitmapC(const string &sName, int &iWidth, int &iHeight);
 23     bool W24BitBmpC(unsigned char *cBuffer, int iWidth, int iHeight, char *cName);
 24 
 25  
 26     bool W8BitBmpFILEHEAD(int iWidth, int iHeight);
 27     bool W8BitBmpINFOHEAD(int iWidth, int iHeight);
 28     bool W8BitBmpQUAD();
 29     bool W8BitBmpBUFFER(char *cBuffer, int iWidth, int iHeight);
 30 
 31     bool W8BitBmpFILEHEADC(int iWidth, int iHeight);
 32     bool W8BitBmpINFOHEADC(int iWidth, int iHeight);
 33     bool W8BitBmpQUADC();
 34     bool W8BitBmpBUFFERC(char *cBuffer, int iWidth, int iHeight);
 35 };
 36 
 37 
 38 //cpp
 39 #include "BmpRW.h"
 40 #include <assert.h>
 41 #include <stdio.h>
 42 #include <Windows.h>
 43 
 44 #include <fstream>
 45 using std::ifstream;
 46 using std::ofstream;
 47 
 48 BmpRW::BmpRW(void)
 49 {
 50 }
 51 
 52 
 53 BmpRW::~BmpRW(void)
 54 {
 55 }
 56 
 57 char* BmpRW::R24BitmapC(const string &sName, int &iWidth, int &iHeight)
 58 {
 59     assert(sName != "");
 60 
 61     ifstream ifs(sName, ifstream::binary);
 62 
 63     if(ifs)
 64     {
 65         ifs.seekg(sizeof(BITMAPFILEHEADER), ifs.beg);    
 66 
 67         BITMAPINFOHEADER infoHead;
 68         ifs.read((char*)&infoHead, sizeof(BITMAPINFOHEADER));
 69 
 70         if(infoHead.biBitCount != 24)
 71         {
 72             return NULL;    
 73         }
 74         iWidth = infoHead.biWidth;
 75         iHeight = infoHead.biHeight;
 76 
 77         int iLineByte = ((iWidth) * (infoHead.biBitCount ) / 8 + 3) / 4 * 4;  
 78 
 79         //RGBQUAD *pColorTable = new RGBQUAD[256];  
 80         //ifs.read((char*)pColorTable, sizeof(RGBQUAD)*256);
 81 
 82         //delete pColorTable;  
 83 
 84         //读格式数据  
 85         char *pBmpBuf = new char[iLineByte * (iHeight)];  
 86         ifs.read(pBmpBuf, iLineByte*iHeight);
 87 
 88         ifs.close();
 89 
 90         return pBmpBuf;
 91     }
 92 
 93     return NULL;
 94 }
 95 
 96 bool BmpRW::W24BitBmpC(unsigned char *cBuffer, int iWidth, int iHeight, char *cName)
 97 {
 98     assert((cBuffer != NULL) && (iWidth > 0) && (iHeight > 0) && (cName != NULL));  
 99     
100     ofstream ofs(cName, ofstream::trunc | ofstream::binary);
101     if(!ofs)
102     {
103         return false;    
104     }
105 
106     int iBitCount = 24;   
107     int iColorSize= 0;  
108     int iLineByte = (iWidth * iBitCount / 8 + 3) / 4 * 4;  
109 
110     BITMAPFILEHEADER fileHead;  
111     fileHead.bfType = 0x4D42;   
112     //bfSize是图像文件4个组成部分之和  
113     fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + iColorSize + iLineByte*iHeight;  
114     fileHead.bfReserved1 = 0;  
115     fileHead.bfReserved2 = 0;  
116     //bfOffBits是图像文件前3个部分所需空间之和  
117     fileHead.bfOffBits = 54 + iColorSize;  
118 
119     ofs.write((char*)&fileHead, sizeof(BITMAPFILEHEADER));
120     ofs.flush();
121 
122     BITMAPINFOHEADER infoHead;  
123     infoHead.biSize = 40;    //本结构的长度  
124     infoHead.biWidth = iWidth; //位图的宽度,以像素为单位  
125     infoHead.biHeight  = iHeight; //位图的宽度,以像素为单位  
126     infoHead.biPlanes  = 1; //目标设备的级别,必须是1  
127     infoHead.biBitCount = iBitCount; //每个像素所占的位数(bit),其值必须为1(黑白图像),4(16色图),8(256色),24(真彩色图)  
128     infoHead.biCompression = BI_RGB; //位图压缩类型,有效的值为BI_RGB(未经压缩)、BI_RLE4,BI_RLE8,BI_BITFIEDS(均为Windows定义常量)。  
129     infoHead.biSizeImage  = iLineByte*iHeight;  //实际的位图数据占用的字节数  
130     infoHead.biXPelsPerMeter = 0; //指定目标数据的水平分辨率,单位是像素/米。  
131     infoHead.biYPelsPerMeter = 0; //指定目标数据的垂直分辨率,单位是像素/米。  
132     infoHead.biClrUsed = 0; //位图实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次幂  
133     infoHead.biClrImportant  = 0; //位图显示过程中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。  
134 
135     ofs.write((char*)&infoHead, sizeof(BITMAPINFOHEADER));
136     ofs.flush();
137 
138     /*
139     RGBQUAD *pColorTable = new RGBQUAD[256];  
140     for (int i=0; i<256; i++)  
141     {  
142         pColorTable[i].rgbRed = i;  
143         pColorTable[i].rgbGreen = i;  
144         pColorTable[i].rgbBlue = i;  
145         pColorTable[i].rgbReserved = 0;  
146     }  
147     ofs.write((const char*)pColorTable, sizeof(RGBQUAD)*256);
148     ofs.flush();
149     delete pColorTable;  
150     */
151 
152     ofs.write((char*)cBuffer, iLineByte*iHeight);
153     ofs.flush();
154     ofs.close();
155 }
156 
157 char* BmpRW::R8BitmapC(const string &sName, int &iWidth, int &iHeight)
158 {
159     assert(sName != "");
160 
161     ifstream ifs(sName, ifstream::binary);
162 
163     if(ifs)
164     {
165         ifs.seekg(sizeof(BITMAPFILEHEADER), ifs.beg);    
166 
167         BITMAPINFOHEADER infoHead;
168         ifs.read((char*)&infoHead, sizeof(BITMAPINFOHEADER));
169 
170         if(infoHead.biBitCount != 8)
171         {
172             return NULL;    
173         }
174         iWidth = infoHead.biWidth;
175         iHeight = infoHead.biHeight;
176 
177         int iLineByte = ((iWidth) * (infoHead.biBitCount ) / 8 + 3) / 4 * 4;  
178 
179         RGBQUAD *pColorTable = new RGBQUAD[256];  
180         ifs.read((char*)pColorTable, sizeof(RGBQUAD)*256);
181 
182         delete pColorTable;  
183 
184         //读格式数据  
185         char *pBmpBuf = new char[iLineByte * (iHeight)];  
186         ifs.read(pBmpBuf, iLineByte*iHeight);
187 
188         ifs.close();
189 
190         return pBmpBuf;
191     }
192 
193     return NULL;
194 }
195 
196 char* BmpRW::R8Bitmap(char *imName, int &imWidth, int &imHeight)  
197 {  
198     //对参数的有效性进行检查  
199     assert(imName != 0);  
200   
201     //二进制读方式打开图像文件  
202     FILE *fp = fopen(imName, "rb");  
203     if(fp == NULL)  
204     {  
205         return NULL;  
206     }  
207   
208     //跳过位图文件头  
209     fseek(fp, sizeof(BITMAPFILEHEADER), 0);  
210   
211     //读取位图信息头  
212     BITMAPINFOHEADER head;  
213     fread(&head, sizeof(BITMAPINFOHEADER), 1, fp);   
214   
215      //判断是否为8位灰度图像  
216     if (head.biBitCount != 8)  
217     {  
218         return NULL;  
219     }  
220   
221     //获取图像宽、高信息  
222     imWidth = head.biWidth ;  
223     imHeight = head.biHeight ;   
224   
225     //求图像格式宽度(必须是4的倍数)  
226     int lineByte = ((imWidth) * (head.biBitCount ) / 8 + 3) / 4 * 4;  
227   
228     //读取颜色表  
229     RGBQUAD *pColorTable = new RGBQUAD[256];  
230     fread(pColorTable, sizeof(RGBQUAD), 256, fp);     
231     delete pColorTable;  
232   
233     //读格式数据  
234     char *pBmpBuf = new char[lineByte * (imHeight)];  
235     fread(pBmpBuf, 1, lineByte * (imHeight), fp);  
236       
237     fclose(fp);  
238   
239     return pBmpBuf;  
240 }  
241 
242 bool BmpRW::W8BitBmpC(unsigned char *cBuffer, int iWidth, int iHeight, char *cName)
243 {
244     assert((cBuffer != NULL) && (iWidth > 0) && (iHeight > 0) && (cName != NULL));  
245     
246     ofstream ofs(cName, ofstream::trunc | ofstream::binary);
247     if(!ofs)
248     {
249         return false;    
250     }
251 
252     int iBitCount = 8;   
253     int iColorSize= 1024;  
254     int iLineByte = (iWidth * iBitCount / 8 + 3) / 4 * 4;  
255 
256     BITMAPFILEHEADER fileHead;  
257     fileHead.bfType = 0x4D42;   
258     //bfSize是图像文件4个组成部分之和  
259     fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + iColorSize + iLineByte*iHeight;  
260     fileHead.bfReserved1 = 0;  
261     fileHead.bfReserved2 = 0;  
262     //bfOffBits是图像文件前3个部分所需空间之和  
263     fileHead.bfOffBits = 54 + iColorSize;  
264 
265     ofs.write((char*)&fileHead, sizeof(BITMAPFILEHEADER));
266     ofs.flush();
267 
268     BITMAPINFOHEADER infoHead;  
269     infoHead.biSize = 40;    //本结构的长度  
270     infoHead.biWidth = iWidth; //位图的宽度,以像素为单位  
271     infoHead.biHeight  = iHeight; //位图的宽度,以像素为单位  
272     infoHead.biPlanes  = 1; //目标设备的级别,必须是1  
273     infoHead.biBitCount = 8; //每个像素所占的位数(bit),其值必须为1(黑白图像),4(16色图),8(256色),24(真彩色图)  
274     infoHead.biCompression = BI_RGB; //位图压缩类型,有效的值为BI_RGB(未经压缩)、BI_RLE4,BI_RLE8,BI_BITFIEDS(均为Windows定义常量)。  
275     infoHead.biSizeImage  = iLineByte*iHeight;  //实际的位图数据占用的字节数  
276     infoHead.biXPelsPerMeter = 0; //指定目标数据的水平分辨率,单位是像素/米。  
277     infoHead.biYPelsPerMeter = 0; //指定目标数据的垂直分辨率,单位是像素/米。  
278     infoHead.biClrUsed = 0; //位图实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次幂  
279     infoHead.biClrImportant  = 0; //位图显示过程中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。  
280 
281     ofs.write((char*)&infoHead, sizeof(BITMAPINFOHEADER));
282     ofs.flush();
283 
284     RGBQUAD *pColorTable = new RGBQUAD[256];  
285     for (int i=0; i<256; i++)  
286     {  
287         pColorTable[i].rgbRed = i;  
288         pColorTable[i].rgbGreen = i;  
289         pColorTable[i].rgbBlue = i;  
290         pColorTable[i].rgbReserved = 0;  
291     }  
292     ofs.write((const char*)pColorTable, sizeof(RGBQUAD)*256);
293     ofs.flush();
294     delete pColorTable;  
295 
296     ofs.write((char*)cBuffer, iLineByte*iHeight);
297     ofs.flush();
298     ofs.close();
299 }
300 bool BmpRW::W8BitBmp(char *imBuffer, int imWidth, int imHeight, char *imName)   
301 {  
302     //对参数的有效性进行检查  
303     assert((imBuffer != NULL) && (imWidth > 0) && (imHeight > 0) && (imName != NULL));  
304   
305     //每个像素所占的位数(bit)  
306     int biBitCount = 8;   
307   
308     //颜色表大小,以字节为单位,灰度图像颜色表为1024字节  
309     int colorTablesize = 1024;  
310   
311     //待存储图像数据每行字节数为4的倍数  
312     int lineByte = (imWidth * biBitCount / 8 + 3) / 4 * 4;  
313   
314     //以二进制写的方式打开文件  
315     FILE *fp = fopen(imName, "wb");  
316     if(fp == NULL)  
317     {  
318         return false;  
319     }  
320   
321     //申请位图文件头结构变量,填写文件头信息  
322     BITMAPFILEHEADER fileHead;  
323     fileHead.bfType = 0x4D42;   //bmp类型  
324   
325     //bfSize是图像文件4个组成部分之和  
326     fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte*imHeight;  
327     fileHead.bfReserved1 = 0;  
328     fileHead.bfReserved2 = 0;  
329   
330     //bfOffBits是图像文件前3个部分所需空间之和  
331     fileHead.bfOffBits = 54 + colorTablesize;  
332   
333     //写文件头进文件  
334     fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp);  
335   
336     //申请位图信息头结构变量,填写信息头信息  
337     BITMAPINFOHEADER head;  
338     head.biSize = 40;    //本结构的长度  
339     head.biWidth = imWidth; //位图的宽度,以像素为单位  
340     head.biHeight  = imHeight; //位图的宽度,以像素为单位  
341     head.biPlanes  = 1; //目标设备的级别,必须是1  
342     head.biBitCount = 8; //每个像素所占的位数(bit),其值必须为1(黑白图像),4(16色图),8(256色),24(真彩色图)  
343     head.biCompression = BI_RGB; //位图压缩类型,有效的值为BI_RGB(未经压缩)、BI_RLE4,BI_RLE8,BI_BITFIEDS(均为Windows定义常量)。  
344     head.biSizeImage  = lineByte*imHeight;  //实际的位图数据占用的字节数  
345     head.biXPelsPerMeter = 0; //指定目标数据的水平分辨率,单位是像素/米。  
346     head.biYPelsPerMeter = 0; //指定目标数据的垂直分辨率,单位是像素/米。  
347     head.biClrUsed = 0; //位图实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次幂  
348     head.biClrImportant  = 0; //位图显示过程中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。  
349   
350     //写位图信息头进内存  
351     fwrite(&head, sizeof(BITMAPINFOHEADER), 1, fp);  
352   
353     //申请颜色表所需要的空间,写颜色表进文件  
354     RGBQUAD *pColorTable = new RGBQUAD[256];  
355     for (int i=0; i<256; i++)  
356     {  
357         pColorTable[i].rgbRed = i;  
358         pColorTable[i].rgbGreen = i;  
359         pColorTable[i].rgbBlue = i;  
360         pColorTable[i].rgbReserved = 0;  
361     }  
362     fwrite(pColorTable, sizeof(RGBQUAD), 256, fp);  
363     delete pColorTable;  
364   
365     //写格式图像数据写进文件  
366     fwrite(imBuffer, imHeight*lineByte, 1, fp);  
367   
368     //关闭文件  
369     fclose(fp);   
370   
371     return true;  
372 }
373 
374 bool BmpRW::W8BitBmpFILEHEAD(int iWidth, int iHeight)
375 {
376     int biBitCount = 8;   
377     int colorTablesize = 1024;  
378     int lineByte = (iWidth * biBitCount / 8 + 3) / 4 * 4;  
379   
380     //以二进制写的方式打开文件  
381     FILE *fp = fopen("fileheadc", "wb");  
382     if(fp == NULL)  
383     {  
384         return false;  
385     }  
386   
387     //申请位图文件头结构变量,填写文件头信息  
388     BITMAPFILEHEADER fileHead;  
389     fileHead.bfType = 0x4D42;   //bmp类型  
390   
391     //bfSize是图像文件4个组成部分之和  
392     fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte*iHeight;  
393     fileHead.bfReserved1 = 0;  
394     fileHead.bfReserved2 = 0;  
395   
396     //bfOffBits是图像文件前3个部分所需空间之和  
397     fileHead.bfOffBits = 54 + colorTablesize;  
398   
399     //写文件头进文件  
400     fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp); 
401     fclose(fp);   
402 }
403 
404 bool BmpRW::W8BitBmpINFOHEAD(int iWidth, int iHeight)
405 {
406     FILE *fp = fopen("infoheadc", "wb");  
407     if(fp == NULL)  
408     {  
409         return false;  
410     }  
411 
412     int biBitCount = 8;   
413     int lineByte = (iWidth * biBitCount / 8 + 3) / 4 * 4;  
414 
415     BITMAPINFOHEADER head;  
416     head.biSize = 40;    //本结构的长度  
417     head.biWidth = iWidth; //位图的宽度,以像素为单位  
418     head.biHeight  = iHeight; //位图的宽度,以像素为单位  
419     head.biPlanes  = 1; //目标设备的级别,必须是1  
420     head.biBitCount = 8; //每个像素所占的位数(bit),其值必须为1(黑白图像),4(16色图),8(256色),24(真彩色图)  
421     head.biCompression = BI_RGB; //位图压缩类型,有效的值为BI_RGB(未经压缩)、BI_RLE4,BI_RLE8,BI_BITFIEDS(均为Windows定义常量)。  
422     head.biSizeImage  = lineByte*iHeight;  //实际的位图数据占用的字节数  
423     head.biXPelsPerMeter = 0; //指定目标数据的水平分辨率,单位是像素/米。  
424     head.biYPelsPerMeter = 0; //指定目标数据的垂直分辨率,单位是像素/米。  
425     head.biClrUsed = 0; //位图实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次幂  
426     head.biClrImportant  = 0; //位图显示过程中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。  
427   
428     //写位图信息头进内存  
429     fwrite(&head, sizeof(BITMAPINFOHEADER), 1, fp);  
430     fclose(fp);   
431 
432 }
433 
434 bool BmpRW::W8BitBmpBUFFER(char *cBuffer, int iWidth, int iHeight)
435 {
436     FILE *fp = fopen("bufferc", "wb");  
437     if(fp == NULL)  
438     {  
439         return false;  
440     }  
441     int biBitCount = 8;   
442     int lineByte = (iWidth * biBitCount / 8 + 3) / 4 * 4;  
443 
444     fwrite(cBuffer, iHeight*lineByte, 1, fp);  
445   
446     fclose(fp);   
447 
448 }
449 
450 bool BmpRW::W8BitBmpFILEHEADC(int iWidth, int iHeight)
451 {
452     ofstream ofs("filehead", ofstream::trunc | ofstream::binary);
453     if(!ofs)
454     {
455         return false;    
456     }
457 
458     int iBitCount = 8;   
459     int iColorSize= 1024;  
460     int iLineByte = (iWidth * iBitCount / 8 + 3) / 4 * 4;  
461 
462     BITMAPFILEHEADER fileHead;  
463     fileHead.bfType = 0x4D42;   
464     //bfSize是图像文件4个组成部分之和  
465     fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + iColorSize + iLineByte*iHeight;  
466     fileHead.bfReserved1 = 0;  
467     fileHead.bfReserved2 = 0;  
468     //bfOffBits是图像文件前3个部分所需空间之和  
469     fileHead.bfOffBits = 54 + iColorSize;  
470 
471     ofs.write((char*)&fileHead, sizeof(BITMAPFILEHEADER));
472     ofs.flush();
473     ofs.close();
474 }
475 
476 bool BmpRW::W8BitBmpINFOHEADC(int iWidth, int iHeight)
477 {
478     ofstream ofs("infohead", ofstream::trunc | ofstream::binary);
479     if(!ofs)
480     {
481         return false;    
482     }
483     int iBitCount = 8;   
484     int iLineByte = (iWidth * iBitCount / 8 + 3) / 4 * 4;  
485 
486     BITMAPINFOHEADER infoHead;  
487     infoHead.biSize = 40;    //本结构的长度  
488     infoHead.biWidth = iWidth; //位图的宽度,以像素为单位  
489     infoHead.biHeight  = iHeight; //位图的宽度,以像素为单位  
490     infoHead.biPlanes  = 1; //目标设备的级别,必须是1  
491     infoHead.biBitCount = 8; //每个像素所占的位数(bit),其值必须为1(黑白图像),4(16色图),8(256色),24(真彩色图)  
492     infoHead.biCompression = BI_RGB; //位图压缩类型,有效的值为BI_RGB(未经压缩)、BI_RLE4,BI_RLE8,BI_BITFIEDS(均为Windows定义常量)。  
493     infoHead.biSizeImage  = iLineByte*iHeight;  //实际的位图数据占用的字节数  
494     infoHead.biXPelsPerMeter = 0; //指定目标数据的水平分辨率,单位是像素/米。  
495     infoHead.biYPelsPerMeter = 0; //指定目标数据的垂直分辨率,单位是像素/米。  
496     infoHead.biClrUsed = 0; //位图实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次幂  
497     infoHead.biClrImportant  = 0; //位图显示过程中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。  
498 
499     ofs.write((char*)&infoHead, sizeof(BITMAPINFOHEADER));
500     ofs.flush();
501     ofs.close();
502 }
503 bool BmpRW::W8BitBmpQUAD()
504 {
505     FILE *fp = fopen("rgbguadc", "wb");  
506     if(fp == NULL)  
507     {  
508         return false;  
509     }  
510 
511     RGBQUAD *pColorTable = new RGBQUAD[256];  
512     for (int i=0; i<256; i++)  
513     {  
514         pColorTable[i].rgbRed = i;  
515         pColorTable[i].rgbGreen = i;  
516         pColorTable[i].rgbBlue = i;  
517         pColorTable[i].rgbReserved = 0;  
518     }  
519     fwrite(pColorTable, sizeof(RGBQUAD), 256, fp);  
520     delete pColorTable;  
521 
522     fclose(fp);   
523 }
524 bool BmpRW::W8BitBmpQUADC()
525 {
526     ofstream ofs("rgbguad", ofstream::trunc | ofstream::binary);
527     if(!ofs)
528     {
529         return false;    
530     }
531 
532     RGBQUAD *pColorTable = new RGBQUAD[256];  
533     for (int i=0; i<256; i++)  
534     {  
535         pColorTable[i].rgbRed = i;  
536         pColorTable[i].rgbGreen = i;  
537         pColorTable[i].rgbBlue = i;  
538         pColorTable[i].rgbReserved = 0;  
539     }  
540     ofs.write((char*)pColorTable, sizeof(RGBQUAD)*256);
541     delete pColorTable;  
542 
543 }
544 bool BmpRW::W8BitBmpBUFFERC(char *cBuffer, int iWidth, int iHeight)
545 {
546     ofstream ofs("buffer", ofstream::trunc | ofstream::binary);
547     if(!ofs)
548     {
549         return false;    
550     }
551 
552     int iBitCount = 8;   
553     int iLineByte = (iWidth * iBitCount / 8 + 3) / 4 * 4;  
554 
555     ofs.write(cBuffer, iLineByte*iHeight);
556     ofs.flush();
557     ofs.close();
558 
559 }
560 
561 
562 //main
563 #include <iostream>
564 using namespace std;
565 #include "BmpRW.h"
566 
567 int main()
568 {
569     int iWidth = 0;
570     int iHeight = 0;
571     BmpRW bmp;
572 
573     //读写24位图像
574     char *buffer24 = bmp.R24BitmapC("R24.bmp", iWidth, iHeight); 
575     bmp.W24BitBmpC((unsigned char*)buffer24, iWidth, iHeight, "W24.bmp");
576 
577 
578     //char *buffer = bmp.R8Bitmap("B2.bmp", iWidth, iHeight); 
579     char *buffer = bmp.R8BitmapC("B2.bmp", iWidth, iHeight); 
580     bmp.W8BitBmpC((unsigned char*)buffer, iWidth, iHeight, "aaa.bmp");
581     bmp.W8BitBmp(buffer, iWidth, iHeight, "bbb.bmp");
582 
583     bmp.W8BitBmpFILEHEADC(iWidth, iHeight);
584     bmp.W8BitBmpINFOHEADC(iWidth, iHeight);
585     bmp.W8BitBmpQUADC();
586     bmp.W8BitBmpBUFFERC(buffer, iWidth, iHeight);
587 
588     bmp.W8BitBmpFILEHEAD(iWidth, iHeight);
589     bmp.W8BitBmpINFOHEAD(iWidth, iHeight);
590     bmp.W8BitBmpQUAD();
591     bmp.W8BitBmpBUFFER(buffer, iWidth, iHeight);
592 
593     cout<<"hello world"<<endl;
594     system("pause");
595 }

 

读写bmp图片

标签:

原文地址:http://www.cnblogs.com/optao/p/5011545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!