Code:
#include <Stdlib.h>
#include <Stdio.h>
#define INPUT_PATH "C:\\4.bmp"
struct CUSTOMBITMAPHEADER
{
unsigned short Type;
unsigned long Size;
unsigned short Reserved1;
unsigned short Reserved2;
unsigned long OffBits;
unsigned long RemainingSize;
unsigned long Width;
unsigned long Height;
unsigned short Planes;
unsigned short BitCount;
unsigned long Compression;
unsigned long SizeImage;
unsigned long XPelsPerMeter;
unsigned long YPelsPerMeter;
unsigned long ClrUsed;
unsigned long ClrImportant;
};
void CompleteHeader(CUSTOMBITMAPHEADER *BitmapHeader, FILE *BinaryIFile);
int main(void)
{
CUSTOMBITMAPHEADER CustomBitmapHeader;
unsigned char *Pixels = NULL;
FILE *InputFile = NULL;
unsigned long FileSize = 0;
printf("Opening File: ");
InputFile = fopen(INPUT_PATH, "rb");
if(InputFile == NULL)
{
printf("Failed\n");
}
else
{
printf("Succeeded\n");
fseek(InputFile, 0, SEEK_END);
FileSize = ftell(InputFile);
rewind(InputFile);
printf("File Status: ");
if(FileSize < sizeof(CUSTOMBITMAPHEADER))
{
printf("Invalid\n");
}
else
{
printf("Valid\n");
CompleteHeader(&CustomBitmapHeader, InputFile);
printf("Creating Pixels: ");
Pixels = (unsigned char *)malloc(CustomBitmapHeader.SizeImage);
if(Pixels == NULL)
{
printf("Failed\n");
}
else
{
printf("Succeeded\n");
fread(&(Pixels[0]), sizeof(Pixels[0]), 1, InputFile);
for(int I = 0; feof(InputFile) == false; I++)
{
if(I % (CustomBitmapHeader.Width * 3) == 0)
printf("\n");
if(I % 3 == 0)
printf("| ");
printf("%d ", Pixels[I]);
fread(&(Pixels[I+1]), sizeof(Pixels[I+1]), 1, InputFile);
}
printf("\n");
}
}
}
if(Pixels != NULL)
{
free(Pixels);
Pixels = NULL;
}
if(InputFile != NULL)
{
fclose(InputFile);
InputFile = NULL;
}
return 0;
}
void CompleteHeader(CUSTOMBITMAPHEADER *BitmapHeader, FILE *BinaryIFile)
{
fread(&(BitmapHeader->Type), sizeof(BitmapHeader->Type), 1, BinaryIFile);
fread(&(BitmapHeader->Size), sizeof(BitmapHeader->Size), 1, BinaryIFile);
fread(&(BitmapHeader->Reserved1), sizeof(BitmapHeader->Reserved1), 1, BinaryIFile);
fread(&(BitmapHeader->Reserved2), sizeof(BitmapHeader->Reserved2), 1, BinaryIFile);
fread(&(BitmapHeader->OffBits), sizeof(BitmapHeader->OffBits), 1, BinaryIFile);
fread(&(BitmapHeader->RemainingSize), sizeof(BitmapHeader->Size), 1, BinaryIFile);
fread(&(BitmapHeader->Width), sizeof(BitmapHeader->Width), 1, BinaryIFile);
fread(&(BitmapHeader->Height), sizeof(BitmapHeader->Height), 1, BinaryIFile);
fread(&(BitmapHeader->Planes), sizeof(BitmapHeader->Planes), 1, BinaryIFile);
fread(&(BitmapHeader->BitCount), sizeof(BitmapHeader->BitCount), 1, BinaryIFile);
fread(&(BitmapHeader->Compression), sizeof(BitmapHeader->Compression), 1, BinaryIFile);
fread(&(BitmapHeader->SizeImage), sizeof(BitmapHeader->SizeImage), 1, BinaryIFile);
fread(&(BitmapHeader->XPelsPerMeter), sizeof(BitmapHeader->XPelsPerMeter), 1, BinaryIFile);
fread(&(BitmapHeader->YPelsPerMeter), sizeof(BitmapHeader->YPelsPerMeter), 1, BinaryIFile);
fread(&(BitmapHeader->ClrUsed), sizeof(BitmapHeader->ClrUsed), 1, BinaryIFile);
fread(&(BitmapHeader->ClrImportant), sizeof(BitmapHeader->ClrImportant), 1, BinaryIFile);
}
Which I believe is working correctly now, and I will move to implementing it in my Terrain Program and hopefully get some nice Terrain's in there.