Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualMcGrane

Posted 02 February 2013 - 07:19 PM

Im trying to understand how bitmaps are stored.

Im using this code to load bitmaps...

unsigned char* Load( char *fileName, BITMAPINFOHEADER *bitmapInfoHeader ) {

      FILE                      *filePtr;          // The File Pointer
      BITMAPFILEHEADER           bitmapFileHeader; // Bitmap File Header
      unsigned char             *bitmapImage;      // Bitmap Image Data
      int                        imageIdx = 0;     // Image Index Counter
      unsigned char              tempRGB;          // Swap Variable BGR -> RGB
     
      filePtr  =  fopen( fileName, "rb" );
      if( filePtr == NULL )
           return NULL;
	  
	  fread( &bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr );

      if(bitmapFileHeader.bfType != 0x4D42 ) {
           fclose(filePtr);
           return NULL;
      }
      
	  fread( bitmapInfoHeader, sizeof( BITMAPINFOHEADER ), 1, filePtr );
	  fseek( filePtr, bitmapFileHeader.bfOffBits, SEEK_SET );
      
	  bitmapImage = ( unsigned char* ) malloc( bitmapInfoHeader->biSizeImage );
      
	  if( !bitmapImage ) {
           free(bitmapImage);
           fclose(filePtr);
           return NULL;
      }
	  fread( bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr );
      
            
      for( imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3 ) {
           tempRGB = bitmapImage[imageIdx];
           bitmapImage[ imageIdx ] = bitmapImage[ imageIdx + 2 ];
           bitmapImage[imageIdx+2]=tempRGB;
      }
     fclose(filePtr);     
     
	 return bitmapImage;
}

 

And this to read them...

BITMAPINFOHEADER    bitmapInfoHeader;
    unsigned char       *bitmapImage;
    
    bitmapImage = Load( "test.bmp", &bitmapInfoHeader );
    
    int index = 0;
    
    for( index = 0; index < 16; index+=3 ) {
           cout<< (int)bitmapImage[index+0]<<", ";
           cout<< (int)bitmapImage[index+1]<<", ";
           cout<< (int)bitmapImage[index+2]<<endl;
    }
    

 

What i dont understand is that when i pass a 2x2 block, say:

All red, i get:

255, 0, 0

255, 0, 0

0,0,0

0,255,0

0,255,0

 

Green:

0, 255, 0

0, 255, 0,

0,0,0

0,0,255,

0,0,255

 

and Blue:

0,0,255

0,0,255

255,0,0

255,0,0

0,0,0


#2McGrane

Posted 02 February 2013 - 07:12 PM

<p>Hey, im having trouble understanding how bitmaps are stored, can someone lend their insight ?</p>
<p>Here is the code im using to load a bitmap..</p>
<pre class="_prettyXprint">
unsigned char* Load( char *fileName, BITMAPINFOHEADER *bitmapInfoHeader ) {

FILE *filePtr; // The File Pointer
BITMAPFILEHEADER bitmapFileHeader; // Bitmap File Header
unsigned char *bitmapImage; // Bitmap Image Data
int imageIdx = 0; // Image Index Counter
unsigned char tempRGB; // Swap Variable BGR -&gt; RGB

filePtr = fopen( fileName, &quot;rb&quot; );
if( filePtr == NULL )
return NULL;

fread( &amp;bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr );

if(bitmapFileHeader.bfType != 0x4D42 ) {
fclose(filePtr);
return NULL;
}

fread( bitmapInfoHeader, sizeof( BITMAPINFOHEADER ), 1, filePtr );
fseek( filePtr, bitmapFileHeader.bfOffBits, SEEK_SET );

bitmapImage = ( unsigned char* ) malloc( bitmapInfoHeader-&gt;biSizeImage );

if( !bitmapImage ) {
free(bitmapImage);
fclose(filePtr);
return NULL;
}
fread( bitmapImage, 1, bitmapInfoHeader-&gt;biSizeImage, filePtr );

for( imageIdx = 0; imageIdx &lt; bitmapInfoHeader-&gt;biSizeImage; imageIdx += 3 ) {
tempRGB = bitmapImage[imageIdx];
bitmapImage[ imageIdx ] = bitmapImage[ imageIdx + 2 ];
bitmapImage[imageIdx+2]=tempRGB;
}
fclose(filePtr);

return bitmapImage;
}

</pre>
<p>&nbsp;</p>
<p>And load using&nbsp;</p>
<pre class="_prettyXprint _lang-">
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char *bitmapImage;

bitmapImage = Load( &quot;test.bmp&quot;, &amp;bitmapInfoHeader );

int index = 0;

for( index = 0; index &lt; 16; index+=3 ) {
cout&lt;&lt; (int)bitmapImage[index+0]&lt;&lt;&quot;, &quot;;
cout&lt;&lt; (int)bitmapImage[index+1]&lt;&lt;&quot;, &quot;;
cout&lt;&lt; (int)bitmapImage[index+2]&lt;&lt;endl;
}

</pre>
<p>&nbsp;</p>
<p>When i pass a 2x2 blocks, i get...</p>
<p>Red:</p>
<p>255, 0, 0</p>
<p>255, 0, 0</p>
<p>&nbsp; &nbsp; 0, 0, 0</p>
<p>0, 255, 0</p>
<p>0, 255, 0</p>
<p>&nbsp;</p>
<p>Green:&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

#1McGrane

Posted 02 February 2013 - 07:09 PM

Hey, im having trouble understanding how bitmaps are stored, can someone lend their insight ?

Here is the code im using to load a bitmap..

unsigned char* Load( char *fileName, BITMAPINFOHEADER *bitmapInfoHeader ) {

      FILE                      *filePtr;          // The File Pointer
      BITMAPFILEHEADER           bitmapFileHeader; // Bitmap File Header
      unsigned char             *bitmapImage;      // Bitmap Image Data
      int                        imageIdx = 0;     // Image Index Counter
      unsigned char              tempRGB;          // Swap Variable BGR -> RGB
     
      filePtr  =  fopen( fileName, "rb" );
      if( filePtr == NULL )
           return NULL;
	  
	  fread( &bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr );

      if(bitmapFileHeader.bfType != 0x4D42 ) {
           fclose(filePtr);
           return NULL;
      }
      
	  fread( bitmapInfoHeader, sizeof( BITMAPINFOHEADER ), 1, filePtr );
	  fseek( filePtr, bitmapFileHeader.bfOffBits, SEEK_SET );
      
	  bitmapImage = ( unsigned char* ) malloc( bitmapInfoHeader->biSizeImage );
      
	  if( !bitmapImage ) {
           free(bitmapImage);
           fclose(filePtr);
           return NULL;
      }
fread( bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr );
                  
      for( imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3 ) {
           tempRGB = bitmapImage[imageIdx];
           bitmapImage[ imageIdx ] = bitmapImage[ imageIdx + 2 ];
           bitmapImage[imageIdx+2]=tempRGB;
      }
     fclose(filePtr);     
     
	 return bitmapImage;
}

 

When i 


PARTNERS