![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 22
Rep Power: 0
![]() |
bitmap files
I wrote yesterday a code that reads the headers of a bitmap file and print them on screen. Actually I'll write a program extracting an image but I wanted to check if the headers are ok. The problem is that I tried it on a few bitmaps I had and it didn't show the expected results here is the code
#include <stdio.h>
#include <stdlib.h>
typedef struct BITMAPFILEHEADER{
short bfType;
long bfSize;
short bfReserved1;
short bfReserved2;
long bfOffbits;
} bmfh;
typedef struct BITMAPINFOHEADER{
long biSize;
long biWidth;
long biHeight;
short biPlanes;
short biBitCount;
long biCompression;
long biSizeImage;
long biXPelsPerMeter;
long biClrUsed;
long biClrImportant;
} bmih;
FILE *bitmap;
bmfh fileheader;
bmih infoheader;
main()
{
if((bitmap = fopen("Black-Bubble.BMP","rb")) == NULL){
puts("error oppening file");
exit(0);
}
fread(&fileheader,sizeof(bmfh),1, bitmap);
fread(&infoheader,sizeof(bmih),1, bitmap);
printf("File Header:\n");
printf("bfType = %d\n",(fileheader.bfType));
printf("bfSize = %d\n",(fileheader.bfSize));
printf("bfReserved1 = %d\n",(fileheader.bfReserved1));
printf("bfReserved2 = %d\n",(fileheader.bfReserved2));
printf("bfOffbits = %d\n",(fileheader.bfOffbits)/1024);
printf("Info Header:\n");
printf("biSize = %d\n",(infoheader.biSize));
printf("biWidth = %d\n",(infoheader.biWidth));
printf("biHeight = %d\n",(infoheader.biHeight));
printf("biPlanes = %d\n",(infoheader.biPlanes));
printf("bbiBitCount = %d\n",(infoheader.biBitCount));
printf("biCompression = %d\n",(infoheader.biCompression));
printf("biSizeImage = %d\n",(infoheader.biSizeImage));
printf("biXPelsPerMeter = %d\n",(infoheader.biXPelsPerMeter));
printf("biClrUsed = %d\n",(infoheader.biClrUsed));
printf("biClrImportant = %d\n",(infoheader.biClrImportant));
return 0;
}can Anyone tell what's wrong? Last edited by Daniel_kd; Apr 23rd, 2005 at 3:24 PM. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Oct 2004
Posts: 15
Rep Power: 0
![]() |
i need help
hi .........
i am doing an image compressing algorithms so i had the following problem in programming the jpeg algorithm when the data is entered for the DCT equation when i want to return it back as it was by using the inverse DCT (IDCT) it couldnt be as it was what is the problem .......>????? plz if u have any idea about this help me waiting reply thnx in advance amin........ |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2004
Posts: 15
Rep Power: 0
![]() |
for ur case
u can write your data to an (.RAW) file instead of BMP which needs header u need a photoshop to open .raw file when writing to it u dont need to write a header amin......... |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 328
Rep Power: 4
![]() |
The reason the code won't work is that it reads the first n bytes, where n is the size of the struct which you believe represents the bitmap header, into the struct. This is too simplistic a solution. It would be really nice if this worked, but it doesn't, because of padding introduced by the compiler into the struct.
To do this properly, you'll have to read each field of the struct individually. I know, it sucks; I was most frustrated when I discovered you can't just do this. The same applies to just writing structs to the disk; unless you're just going to read them right back as structs, it can be a problem because of the padding being written too. You could do a simple little diagnostic to see if this is actually the problem. Figure out what size the struct should actually be to have room for all of its members, then add a line to the program like printf("%i\n", sizeof(bmih));The value of sizeof() will probably, if I'm right, be at least one byte bigger than you think it should be. It has to do with aligning fields on word boundaries and the like, which makes accessing the structures in memory more efficient. Like I say, the solution is to read the header a little at a time and fill it into the structure yourself, field by field (well, you can actually read it into a buffer big enough to hold all of it then work with that, but you know what I mean). |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2005
Posts: 22
Rep Power: 0
![]() |
thanx a lot I'll try this instead
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|