This is a follow up question from here, Link
I read and copy pasted the code from the link to Code::Blocks but with a little change,
Now I want to create a 20x20 black image
So I edited the code but I am unable to open the image on Win7, it says 'Windows Photo Viewer can't open this Image'.
Can anyone please advice me what is wrong with the code ??
My code:-
#include<stdio.h>
unsigned char bitmap[1000];
void BMPmake()
{
int i;
// -- FILE HEADER -- //
// bitmap signature
bitmap[0] = 'B';
bitmap[1] = 'M';
// file size
bitmap[2] = 0xc6; // 40 + 14 + 400
bitmap[3] = 0x01;
bitmap[4] = 0;
bitmap[5] = 0;
// reserved field (in hex. 00 00 00 00)
for( i = 6; i < 10; i++) bitmap[i] = 0;
// offset of pixel data inside the image
//thats is 54 or d8 the difference between the starting and the position where data actually starts.
bitmap[10]=0xd8;
for( i = 11; i < 14; i++) bitmap[i] = 0;
// -- BITMAP HEADER -- //
// header size
bitmap[14] = 40;
for( i = 15; i < 18; i++) bitmap[i] = 0;
// width of the image
bitmap[18] = 20;
for( i = 19; i < 22; i++) bitmap[i] = 0;
// height of the image
bitmap[22] = 20;
for( i = 23; i < 26; i++) bitmap[i] = 0;
// no of color planes, must be 1
bitmap[26] = 1;
bitmap[27] = 0;
// number of bits per pixel
bitmap[28] = 8; // 1 byte
bitmap[29] = 0;
// compression method (no compression here)
for( i = 30; i < 34; i++) bitmap[i] = 0;
// size of pixel data
bitmap[34] = 0x90; // 400 bytes => 400 pixels ,,,, 20x20x1
bitmap[35] = 0x01;
bitmap[36] = 0;
bitmap[37] = 0;
// horizontal resolution of the image - pixels per meter (2835)
bitmap[38] = 0;
bitmap[39] = 0;
bitmap[40] = 0;
bitmap[41] = 0;
// vertical resolution of the image - pixels per meter (2835)
bitmap[42] = 0;
bitmap[43] = 0;
bitmap[44] = 0;
bitmap[45] = 0;
// color palette information here 256
bitmap[46]=0xff;
bitmap[47]=1;
for( i = 48; i < 50; i++) bitmap[i] = 0;
// number of important colors
for( i = 50; i < 54; i++) bitmap[i] = 0;
// -- PIXEL DATA -- //
for( i = 54; i < 454; i++) bitmap[i] = 0xff;
}
void BMPwrite()
{
FILE *file;
int i;
file = fopen("b.bmp", "wb+");
for( i = 0; i < 454; i++)
{
fputc(bitmap[i], file);
}
fclose(file);
}
void main()
{
BMPmake();
BMPwrite();
printf("Done!!");
}