www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/08/20/02:51:21

Xref: news2.mv.net comp.os.msdos.djgpp:7653
From: Tom Wheeley <tomw AT tsys DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: strange problem reading binary files
Date: Tue, 20 Aug 96 02:54:40 GMT
Organization: City Zen FM
Lines: 126
Message-ID: <840509680snz@tsys.demon.co.uk>
Reply-To: tomw AT tsys DOT demon DOT co DOT uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I have this extremely irrational looking bug which I cannot solve, but I
am sure will be pointed out in big, embarrassing letters...

Ok.  Whenever I ran this program, it always reported the bmih.biSize as
being 0.  Clearly in a hex dump, it was 0x28:  (dashes signify gaps between
bits in structures below)

42 4d-3e 04 00 00-00 00-00 00-3e 00 00 00-28 00 00 00

Now, when I set it up to read just up to and including biSize, it worked
fine, but whenever Iread the rest it became zero.  At first, thinking it was
some strange alignment thing or whatever, I rewrote it to read each element of
the structures separately. (hence the READ2 and READ4 macros.)  I don't
think it is because I missed the size of a struct element, as all the other
parts are fine.  The following code should demonstrate it precisely.  Defining
READALL when compiling (-DREADALL) will show up the problem.

I've cut out all the irrelevant bits:

{bmpinfo.c}

/* BMPINFO, bitmap info proggy */

#include <stdio.h>
#include <stdlib.h>
#include "fbmp.h"

/* can be redefined for big-endian machines */
#define READ2(x,f)  (x)=getc(f);(x)|=getc(f)<<8
#define READ4(x,f)  (x)=getc(f);(x)|=getc(f)<<8;(x)|=getc(f)<<16;(x)|=getc(f)<<24

int main (int argc, char *argv[])
{
  FILE *bmp;
  BITMAPFILEHEADER bmfh;
  BITMAPINFOHEADER bmih;
  RGBQUAD bmic[1];

  if(!(bmp=fopen(*++argv,"rb"))) {
    printf("Unable to open bmp file `%s'\n",*argv);
    exit(EXIT_FAILURE);
  }

  READ2(bmfh.bfType,bmp);
  READ4(bmfh.bfSize,bmp);
  READ2(bmfh.bfReserved1,bmp);
  READ2(bmfh.bfReserved2,bmp);
  READ4(bmfh.bfOffBits,bmp);


  READ4(bmih.biSize,bmp);

  printf("  biSize (before): %08lx\n",bmih.biSize);
  #ifdef READALL

  READ4(bmih.biWidth,bmp);
  READ4(bmih.biHeight,bmp);
  READ2(bmih.biPlanes,bmp);
  READ2(bmih.biBitCount,bmp);
  READ4(bmih.biCompression,bmp);
  READ4(bmih.biSImage,bmp);
  READ4(bmih.biXPelsPerMeter,bmp);
  READ4(bmih.biYPelsPerMeter,bmp);
  READ4(bmih.biClrUsed,bmp);
  READ4(bmih.biClrImportant,bmp);

  bmic[0].rgbBlue=getc(bmp);bmic[0].rgbGreen=getc(bmp);
  bmic[0].rgbRed=getc(bmp);bmic[0].rgbReserved=getc(bmp);
  bmic[1].rgbBlue=getc(bmp);bmic[1].rgbGreen=getc(bmp);
  bmic[1].rgbRed=getc(bmp);bmic[1].rgbReserved=getc(bmp);

  #endif /* READALL */
  printf("  biSize  (after): %08lx\n",bmih.biSize);

  fclose(bmp);

[...]

{fbmp.h}

typedef unsigned short  UINT;       /* 16 bit unsigned */
typedef unsigned long   DWORD;      /* 32 bit unsigned */
typedef long            LONG;       /* 32 bit */
typedef unsigned short  WORD;       /* 16 bit unsigned */
typedef unsigned char   BYTE;       /* 8 bit unsigned */

typedef struct tagBITMAPFILEHEADER {    /* bmfh */
    UINT    bfType;
    DWORD   bfSize;
    UINT    bfReserved1;
    UINT    bfReserved2;
    DWORD   bfOffBits;
} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER {    /* bmih */
    DWORD   biSize;
    LONG    biWidth;
    LONG    biHeight;
    WORD    biPlanes;
    WORD    biBitCount;
    DWORD   biCompression;
    DWORD   biSImage;
    LONG    biXPelsPerMeter;
    LONG    biYPelsPerMeter;
    DWORD   biClrUsed;
    DWORD   biClrImportant;
} BITMAPINFOHEADER;

typedef struct tagRGBQUAD {     /* rgbq */
    BYTE    rgbBlue;
    BYTE    rgbGreen;
    BYTE    rgbRed;
    BYTE    rgbReserved;
} RGBQUAD;




..splitbung                                        http://www.tsys.demon.co.uk
-- 
#include <stdio.h>                  /* The .splitbung super .sig system! */
#include <string.h>
main(){FILE *f;int c;char s[99];puts("fav .sig: ");fgets(s,99,stdin);strtok
(s,"\n");f=fopen(s,"rb");while((c=getc(f))!=EOF)putchar(c);if(f)fclose(f);}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019