Create a gist now

Instantly share code, notes, and snippets.

Generate bmp image with C.
typedef struct BitMapFileHeader{
const unsigned short Reserve;
const unsigned short FileType;// = 0x424D; //BM
unsigned int FileSize;
const unsigned short Reserve1;// = 0x00;
const unsigned short Reserve2;// = 0x00;
const unsigned int DateOffSet;// = 0x3E;
} BMPFileHeader;
typedef struct BitMapInfoHeader{
const unsigned int ThisSize;// = 0x28;
int ImgWidth;
int ImgHeight;
const unsigned short PlainNum;// = 0x01;
const unsigned short BitPerPx;// = 0x01;
const unsigned int CompressType;// = 0x00;
unsigned int ImgDateSize;
const unsigned int HorizontalDpi;// = 0x00;
const unsigned int VerticalDpi;// = 0x00;
const unsigned int ColorIndex;// = 0x02;
const unsigned int PrincipalIndex;// = 0x00;
} BMPInfoHeader;
typedef struct ColorPalette{
const unsigned char Red;
const unsigned char Green;
const unsigned char Blue;
const unsigned char Reserve;
} ColorPalette;
BMPFileHeader InitializeFileHeader(){
BMPFileHeader bmpFH = {
0x00,
0x4D42, //FileType
0x00, //FileSize
0x00, //Reserve1
0x00, //Reserve2
0x3E //DateOffSet
};
return bmpFH;
}
BMPInfoHeader InitializeInfoHeader(){
BMPInfoHeader bmpIH = {
0x28, //ThisSize
0x00, //ImgWidth
0x00, //ImgHeight
0x01, //PlainNum
0x01, //BitPerPx
0x00, //CompressType
0x00, //ImgDateSize
0x00, //HorizontalDpi
0x00, //VerticalDpi
0x02, //ColorIndex
0x00 //PrincipalIndex
};
return bmpIH;
}
//中京と書いてあるbmp画像を生成するプログラムです。
#include<stdio.h>
#include"bmpsetting.h"
int main(void){
FILE *BMPfp;
errno_t error;
if (error = fopen_s(&BMPfp, "test.bmp", "wb") != 0)
return error;
int sfsdaf[10][18] =
{ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1 },
{ 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 } };
BMPFileHeader bmpFH = InitializeFileHeader();
BMPInfoHeader bmpIH = InitializeInfoHeader();
ColorPalette colP[2] = {
{ 0x00, 0x00, 0x00, 0x00 }, //Black
{ 0xFF, 0xFF, 0xFF, 0x00 } //White
};
bmpIH.ImgHeight = 0x0A;
bmpIH.ImgWidth = 0x12;
unsigned int ByteperImgLine = ((0x12 / 8) + 1) + 1;
bmpIH.ImgDateSize = 0x0A * ByteperImgLine;
bmpFH.FileSize = bmpFH.DateOffSet + bmpIH.ImgDateSize;
fwrite((char*)&bmpFH + 2, sizeof(BMPFileHeader)-2, 1, BMPfp);
fwrite(&bmpIH, sizeof(BMPInfoHeader), 1, BMPfp);
fwrite(colP, sizeof(ColorPalette), 2, BMPfp);
unsigned int num;
char zero = 0;
for (int row = 0; row < 10; row++){
num = 0;
for (int column = 0; column < 18; column++){
num = num * 2 + sfsdaf[9 - row][column];
}
fwrite(&num, sizeof(int), 1, BMPfp);
}
fclose(BMPfp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment