# C bitmap loader



## rrizbaf (Nov 9, 2007)

Hi all

Please can some one test this code and tell me what the reason for it returning the wrong values. It is the textureLoader.h for my opengl program and it just wont load the bitmaps correctly. I have been through the code again and again and cant find any reason for the clearly wrong output. Maybe one of you guys can:

_
#include <stdio.h>

#include <stdlib.h>



int imageIdx = 0;



typedef struct {



unsigned short int type;	//magic identifier 

unsigned int size; //size of the file

unsigned short int reserved1;	//

unsigned short int reserved2;	//

unsigned int offset; //offset to image data 



} BITMAPFILEHEADER;





//------Information header of the bitmap file------//



typedef struct {



unsigned int biSizeImage; //specifies the number of bytes required by the struct

int width , height; //species height and width in pixels of the image

unsigned short int planes; //the number of color planes

unsigned short int bits; //specifies the number of bits per pixel

unsigned int compression; //spcifies the type of compression

unsigned int imagesize; //size of image in bytes

unsigned int xresolution, yresolution;	//pixels in x and y axis

unsigned int nColours; //number of colors used by the bitmap

unsigned int importantColours; //number of colors that are important in the image



} BITMAPINFOHEADER;





unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader){



FILE *filePtr;

BITMAPFILEHEADER bitmapFileHeader;



unsigned char *bitmapImage;









unsigned char tempRGB;



filePtr = fopen (filename, "rb");

if (filePtr ==NULL){



printf("File not found: %s\n", filename);

return 0;

}

else{



printf("Texture loaded successfully: %s,\n and file pointer %s\n", filename, filePtr);



}







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

printf("\nSize of the BITMAPFILEHEADER is: %d\n", sizeof(BITMAPFILEHEADER));



if (bitmapFileHeader.type != 0x4D42){

printf("\nERROR: Wrong type of bitmap or image file used\n");

fclose(filePtr);

return NULL;



}



fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

printf("\nSize of the BITMAPINFOHEADER is: %d\n", sizeof(BITMAPINFOHEADER));



int x =	bitmapFileHeader.offset;

fseek(filePtr, x , SEEK_SET);



printf("offset: %d", &x);



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



if (!bitmapImage){



free(bitmapImage);

fclose(filePtr);

return NULL;

}



fread(&bitmapImage, bitmapInfoHeader->biSizeImage,1, filePtr);



if (bitmapImage == NULL){



fclose(filePtr);

return NULL;



}

printf("Texture loaded successfully: %s,\n and file pointer %s\n", filename, filePtr);



printf("\nsize: %d", bitmapFileHeader.size);



printf("\n\n size: %d", bitmapInfoHeader->biSizeImage);_

If you could tell me where im going wrong that would be awesome and you would also be a life saver :grin:


----------

