Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // create a BMP file
- // give a color to every pixels of the BMP file
- // use function to simpligy the main function interface
- #include <iostream>
- using namespace std;
- typedef struct{
- unsigned short int signature;
- unsigned int size;
- unsigned short int reserved[2];
- unsigned int offset;
- }FileHeader;
- void WriteFileHeader(FileHeader fh, FILE *fp) {
- fwrite(&fh.signature,sizeof(unsigned short int),1,fp);
- fwrite(&fh.size,sizeof(unsigned int),1,fp);
- fwrite(&fh.reserved,sizeof(unsigned short int),2,fp);
- fwrite(&fh.offset,sizeof(unsigned int),1,fp);
- }
- typedef struct{
- unsigned int headerSize;
- int width;
- int height;
- unsigned short int plane;
- unsigned short int bits;
- unsigned int compression;
- unsigned int imageSize;
- int xPixel;
- int yPixel;
- unsigned int colorTable;
- unsigned int colorCount;
- }InfoHeader;
- void WriteInfoHeader(InfoHeader info, FILE *fp){
- fwrite(&info.headerSize,sizeof(unsigned int),1,fp); // headerSize
- fwrite(&info.width,sizeof(int),1,fp); // width
- fwrite(&info.height,sizeof(int),1,fp); // height
- fwrite(&info.plane,sizeof(unsigned short int),1,fp); // plane
- fwrite(&info.bits,sizeof(unsigned short int),1,fp); // bits
- fwrite(&info.compression,sizeof(unsigned int),1,fp); // compression
- fwrite(&info.imageSize,sizeof(unsigned int),1,fp); // imageSize
- fwrite(&info.xPixel,sizeof(int),1,fp); // xPixel
- fwrite(&info.yPixel,sizeof(int),1,fp); // yPixel
- fwrite(&info.colorTable,sizeof(unsigned int),1,fp); // colorTable
- fwrite(&info.colorCount,sizeof(unsigned int),1,fp); // colorCount
- }
- int main(){
- // create a bmp-format file
- int width = 1024, height = 1024;
- FileHeader fileHeader = {
- 0x4d42, 54 + width * height * 4, {0, 0}, 54
- };
- InfoHeader infoHeader = {
- 40, width, height, 1, 32, 0, 64, 4, 4, 4, 4
- };
- FILE *fp;
- fp = fopen("myBMP06.bmp","wb");
- // write fileHeader into the file
- WriteFileHeader(fileHeader,fp);
- // write infoHeader into the file
- WriteInfoHeader(infoHeader, fp);
- // put color in the file
- // BGRA
- unsigned char color[4] = {222,125,125,50};
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- fwrite(color,sizeof(unsigned char),4,fp);
- }
- }
- fclose(fp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement