Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 23rd, 2005, 3:20 PM   #1
Daniel_kd
Newbie
 
Daniel_kd's Avatar
 
Join Date: Apr 2005
Posts: 22
Rep Power: 0 Daniel_kd is on a distinguished road
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.
Daniel_kd is offline   Reply With Quote
Old Apr 26th, 2005, 4:17 PM   #2
amin1982
Newbie
 
Join Date: Oct 2004
Posts: 15
Rep Power: 0 amin1982 is on a distinguished road
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........
amin1982 is offline   Reply With Quote
Old Apr 26th, 2005, 4:22 PM   #3
amin1982
Newbie
 
Join Date: Oct 2004
Posts: 15
Rep Power: 0 amin1982 is on a distinguished road
Post no prob

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.........
amin1982 is offline   Reply With Quote
Old Apr 28th, 2005, 7:43 AM   #4
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 328
Rep Power: 4 mackenga is on a distinguished road
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).
mackenga is offline   Reply With Quote
Old May 3rd, 2005, 7:20 AM   #5
Daniel_kd
Newbie
 
Daniel_kd's Avatar
 
Join Date: Apr 2005
Posts: 22
Rep Power: 0 Daniel_kd is on a distinguished road
thanx a lot I'll try this instead
Daniel_kd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC