Thread: reading bmps

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    reading bmps

    hey, could someone tell me how to load bmps and look at them? I would like to be able to load a bmp, then scan through it for the values in it.....thanks in advance

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    First, search the board this has been asked a lot but not too recently. Second, bmps are stored in binary so your not going to be able to scan through it for values unless a) you have a hex editer and b) you can read hex
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    well, can't you just use fread and all that? I know there's something about bmp headers that you need......but I can't remember what they are

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    I already told you where to find you answer so this is all im going to say: http://cboard.cprogramming.com/search.php?s=
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  5. #5
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    I guess you could always use GDI, or GDI+ API to load the bitmap into a DeviceContext(DC) then use GetPixel to read pixel information.

    I hope that helps.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Originally posted by xds4lx
    I already told you where to find you answer so this is all im going to say: http://cboard.cprogramming.com/search.php?s=
    ah, sorry bout that.....

  7. #7
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    This might help...

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <dos.h>
    #include <mem.h>
    
    
    #define SCREEN_WIDTH  320
    #define SCREEN_HEIGHT 200
    #define NUM_COLORS    256
    
    unsigned char *VGA=(unsigned char *)0xA0000000L;
    
    void setmode(unsigned char mode)
    {
      union REGS i, o;
    
      i.h.ah = 0x00;
      i.h.al = mode;
      int86(16, &i, &o);
    }
    
    void setrgbpalette(int n, unsigned char r, unsigned char g, unsigned char b)
       {
       union REGS i, o;
       i.h.ah=0x10;
       i.h.al=0x10;
       i.x.bx=n;
       i.h.dh=r;
       i.h.ch=g;
       i.h.cl=b;
       int86(16, &i, &o);
       }
    
    void putpixel(int x, int y, char c)
       {
       *(VGA+((y<<8)+(y<<6)+x)) = c;
       }
    
    struct Fileheader
       {
       unsigned short Type;
       unsigned long Size;
       unsigned short Reserved1;
       unsigned short Reserved2;
       unsigned long OffBits;
       unsigned long StructSize;
       unsigned long Width;
       unsigned long Height;
       unsigned short Planes;
       unsigned short BitCount;
       unsigned long Compression;
       unsigned long SizeImage;
       long XPelsPerMeter;
       long YPelsPerMeter;
       unsigned long ClrUsed;
       unsigned long ClrImportant;
       Fileheader()
    	 {
    	 Size=Width=Height=Planes=BitCount=Compression=SizeImage=XPelsPerMeter=
    	 YPelsPerMeter=ClrUsed=ClrImportant=Type=StructSize=Reserved1=Reserved2=
    	 OffBits=0;
    	 }
       };
    
    struct RGBQuad
       {
       unsigned char rgbBlue;
       unsigned char rgbGreen;
       unsigned char rgbRed;
       unsigned char rgbReserved;
       RGBQuad()
    	 {
    	 rgbBlue = rgbGreen = rgbRed = 0;
    	 rgbReserved = 0;
    	 }
       };
    
    class Bitmap
       {
       private :
    			Fileheader f;
    			unsigned char* Bmp;
    			RGBQuad Palette[256];
       public  :
    			void ReadBitmap(char*);
    			void DisplayBitmap(void);
       };
    
    
    
    void Bitmap::ReadBitmap(char *file)
    {
      FILE *fp;
      unsigned short num_colors;
    
      if ((fp = fopen(file,"rb")) == NULL)
      {
    	printf("Error opening file %s.\n",file);
    	exit(1);
      }
    
      fread(&f, sizeof(Fileheader), 1, fp);
    
      if (f.ClrUsed==0) num_colors=NUM_COLORS;
      else num_colors=f.ClrUsed;
    
      if ((Bmp = (unsigned char * ) malloc(sizeof(unsigned char)*(f.Width*f.Height))) == NULL)
      {
    	fclose(fp);
    	printf("Error allocating memory for file %s.\n",file);
    	exit(1);
      }
    
      for(int i=0; i<num_colors; i++)
    	{
    	fread(&Palette[i], sizeof(RGBQuad), 1, fp);
    	setrgbpalette(i, (Palette[i].rgbRed>>2), (Palette[i].rgbGreen>>2), (Palette[i].rgbBlue>>2));
    	}
    
      for(long j=(f.Height-1)*f.Width; j>=0; j-=f.Width)
    	{
    	for(int i=0;i<f.Width;i++)
    	  *(Bmp+j+i)=fgetc(fp);
    	 if(f.Width%4!=0)
    	   {
    	   for(int x=f.Width%4; x<4; x++)
    		   fgetc(fp);
    	   }
    	}
    
      fclose(fp);
    }
    
    void Bitmap::DisplayBitmap(void)
    {
    	for(long i=0; i<f.Width; i++)
    	 for(int j=0; j<f.Height; j++)
    	   putpixel(i, j, *(Bmp+i+j*f.Width));
    }
    
    void main()
    {
      Bitmap B1;
      char fpath[80];
    
      clrscr();
      printf("Enter the path: ");
      scanf(" %s", fpath);
      clrscr();
    
      setmode(0x13);
      B1.ReadBitmap(fpath);
    
      B1.DisplayBitmap();
      getch();
      setmode(0x03);
    }
    He who sez it cant be done is done??
    Happy Hunting...
    Last edited by kiss_psycho; 02-21-2003 at 03:01 PM.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  8. #8
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    O, I forgot, you have to use a 320/200 pixel picture at 256 colors resolution. The file has to be a .bmp. Greater number of colors results in loss of colors in display, and a resolution greater than 320/200 results in a partly displayed picture and an out of memory message when the program exits. I forgot the memory check and it uses a TC++(3.0) compiler and is compatible with Borland 5.0
    Last edited by kiss_psycho; 02-21-2003 at 03:46 PM.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    What application are we talking about exactly?
    Also use code tags.

  10. #10
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    Applications to load a bmp. Didnt u see the first message???
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by kiss_psycho
    Applications to load a bmp. Didnt u see the first message???
    Heh, I ment type of application? Win32? Win32 Console? DOS?

  12. #12
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    DOS
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  13. #13
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Originally posted by kiss_psycho
    DOS
    code tags!

  14. #14
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    but the thing is, I don't need to display the bmp, I'm using OpenGL, so it handles that for me, what I want to do is use the bmp for a heightmap, so I need to be able to read the value of EVERY pixel (preferably in a gray-scale pic)

  15. #15
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    1- Search the web for the .bmp "file format". There are sites that have the specifications for how various files are formatted. The bitmap format is relatively simple because there is no compression or other funny stuff. There is a header (the first part of the file) that tells the matrix size, (X x Y) and the color depth... maybe some other stuff... then a series of numbers for the colors (or grey level) for each pixel.

    2- Like xds4lx said... Get a hex editor. It will allow you to look at the binary data in the .bmp file (in hex format to make it readable). The hex editor will probably have the option of showing the ASCII character for each hex value. The ASCII view should be helpful in examining the header.

    You could do this without the hex editor, but debugging will be much easier it you can see the pixel values.

    [EDIT]
    A hex editor will allow you to change pixel values too. This way you could change a pixel to a known value (i.e. FF, FF, FF) with the hex editor, and then read it back with your program.

    [EDIT AGAIN]
    I re-read your question, and maybe you aren't writing a program to do this. Maybe ALL you need is a hex editor... But, it might get tricky figuring-out which which pixel you are looking at after the first few... Hmmm... You might have to do some calculations... 'shouldn't be too bad, 'cause the hex editor should will show the byte position for each hex value.
    Last edited by DougDbug; 02-27-2003 at 07:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  4. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM