64,637
社区成员
发帖
与我相关
我的任务
分享
/*
* Bitmap.h
*
* Created on: 2011-8-26
* Author: ray-ni
*/
#ifndef BITMAP_H_
#define BITMAP_H_
//#pragma pack(2)
typedef unsigned int DWORD;
typedef int LONG;
typedef unsigned short WORD;
/**
* 鏂囦欢淇℃伅澶? */
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
/**
* 浣嶅浘淇℃伅澶? */
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
/**
* 璋冭壊鏉夸俊鎭? */
typedef struct tagRGBQUAD {
char rgbBlue;
char rgbGreen;
char rgbRed;
char rgbReserved;
} RGBQUAD, *PRGBQUAD;
#define BI_RGB 0L
#define BI_RLE8 1L
#define BI_RLE4 2L
#define BI_BITFIELDS 3L
#define BI_JPEG 4L
#define BI_PNG 5L
//#pragma pack()
class BitmapUtils{
public:
BitmapUtils(int width, int height);
void setColorTable(unsigned char** colors, int size);
void addData(char* data, int length);
void save();
private:
BITMAPFILEHEADER mHead;
BITMAPINFOHEADER mInfo;
PRGBQUAD mColorTable;
int mColorTableSize;
char* mData;
int mDataSize;
};
#endif /* BITMAP_H_ */
/*
* BitmapUtils.cpp
*
* Created on: 2011-8-29
* Author: ray-ni
*/
#include "../head/Bitmap.h"
#include <cstdio>
#include <cstdlib>
BitmapUtils::BitmapUtils(int width, int height) {
char* tmp = new char[10]; //这里出错。。。
//璁惧畾鏂囦欢澶? this->mHead.bfType = 0x4D42;
this->mHead.bfSize = 54;
this->mHead.bfReserved1 = 0;
this->mHead.bfReserved2 = 0;
this->mHead.bfOffBits = 0;
//璁惧畾浣嶅浘淇℃伅
this->mInfo.biSize = 40;
this->mInfo.biWidth = width;
this->mInfo.biHeight = height;
this->mInfo.biPlanes = 1;
this->mInfo.biBitCount = 8;
this->mInfo.biCompression = BI_RGB;
this->mInfo.biSizeImage = 0;
this->mInfo.biXPelsPerMeter = 0;
this->mInfo.biYPelsPerMeter = 0;
this->mInfo.biClrUsed = 0;
this->mInfo.biClrImportant = 0;
}
/**
* 璁惧畾璋冭壊鏉? */
void BitmapUtils::setColorTable(unsigned char** colors, int size) {
this->mColorTableSize = size;
this->mColorTable = new RGBQUAD[size];
for (int i = 0; i < size; i++) {
this->mColorTable[i].rgbBlue = colors[i][0];
this->mColorTable[i].rgbGreen = colors[i][1];
this->mColorTable[i].rgbRed = colors[i][2];
this->mColorTable[i].rgbReserved = 0;
}
this->mHead.bfSize += size * 4;
this->mHead.bfOffBits = this->mHead.bfSize;
}
/**
* 娣诲姞棰滆壊鏁版嵁
*/
void BitmapUtils::addData(char* data, int length) {
this->mData = data;
this->mDataSize = length;
this->mHead.bfSize += this->mInfo.biHeight *((this->mInfo.biWidth +3)/4*4);
}
/**
* 淇濆瓨鏂囦欢
*/
void BitmapUtils::save() {
FILE* file = fopen("./test.bmp", "w+");
fwrite(&this->mHead, 1, sizeof(this->mHead), file);
fwrite(&this->mInfo, 1, sizeof(this->mInfo), file);
fwrite(&this->mColorTable, 4, this->mColorTableSize, file);
char zero = 0 , j = 0;
for (int i = this->mInfo.biHeight - 1; i >= 0; i--) {
j= 0;
while (j < this->mInfo.biWidth) {
fwrite(&this->mData + i * this->mInfo.biWidth, 1, 1, file);
j++;
}
while ((j % 4) != 0) {
fwrite(&zero, 1, 1, file);
j++;
}
}
fclose(file);
delete[] this->mColorTable;
}