-
loading a bitmap
i a loading a 24-bit bitmap with this code but i don't know whats wrong withit?
can any one help me?
Code:
typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef struct {
int Signature;
DWORD Size;
DWORD Reserved;
DWORD BitsOffset;
} BITMAP_FILEHEADER;
typedef struct {
DWORD HeaderSize;
DWORD Width;
DWORD Height;
WORD Planes;
WORD BitCount;
DWORD Compression;
DWORD SizeImage;
DWORD PelsPerMeterX;
DWORD PelsPerMeterY;
DWORD ClrUsed;
DWORD ClrImportant;
} BITMAP_HEADER;
#include <stdio.h>
main()
{
FILE * fp;
BITMAP_FILEHEADER bmpheader;
BITMAP_HEADER bmpinfheader;
int raw_data_size;
char *pixel;
//openning file
fp=fopen("c:\\pic.bmp","rb");
/*if(fp==NULL){
printf("error in opennig file");
exit(1);
}*/
fread(&bmpheader,sizeof(bmpheader),1,fp);//reads the header
fread(&bmpinfheader,sizeof(bmpinfheader),1,fp);//reads the info header
raw_data_size=bmpheader.SizeImage;//stores the size of raw bitmaps
pixel=new char [raw_data_size];//create an array for holding the pixels
fread(pixel,sizeof(char),raw_data_size,fp);//reads them from memory to aray
printf("%x",bmpheader.Signature);//prints 424D
fclose(fp);
return 0;
}
-
I think that struct is going to be padded, I tried pasting it in here and did a sizeof of both structs. This returned 112 bytes, but a bmp have a 56 byte header.
Edit: no, that's not it. A long is 64 bits here that's why.
Anyway, this is not C:
Code:
pixel=new char [raw_data_size];//create an array for holding the pixels
You need to use malloc or calloc instead:
Code:
pixel = malloc(raw_data_size);
-
Another thing worth mentioning is that a pixel is three bytes in a 24bit bmp, but watch out for the padding on each row.
-
can you explain it more clearly?
-
What part? You can not use "new" in C, you need to use malloc or similar, like in the example above.
The last post was just a heads up, if you intend to manipulate the img data. You need to move ahead 3bytes to reach the next pixel. Then each row might be padded to an even multiple of 4 i believe. This mean that you could end up with let's say two empty bytes in the end of each row, which you will need to skip.