Problems reading Bitmaps
-
Hello guys, I'm trying to write a funtion to read a simple bitmap, but I've encountered a serious problem. As model I used the description of the bitmap file format from wikipedia http://en.wikipedia.org/wiki/BMP_file_format[^] and as far as I know this is definitely the correct format. I also used the same 2x2 24-bit bitmap[^] to test my function, but except for the first value (the magic number or identifier) all other read values are wrong.
#include <stdint.h>
#include <stdio.h>/*Data types-->*/
typedef uint8_t BYTE;
typedef uint16_t BYTE2;
typedef uint32_t BYTE4;
/*<--Data types*//*Bitmap file header-->*/
typedef struct BMP_FILE_HEADER
{
BYTE2 MagicNumber;
BYTE4 FileSize;
BYTE2 Reserved1;
BYTE2 Reserved2;
BYTE4 Offset;
} BMP_FILE_HEADER;
/*<--Bitmap file header*//*Bitmap information header-->*/
typedef struct BMP_INFO_HEADER
{
BYTE4 HeaderSize;
BYTE4 ImageWidth;
BYTE4 ImageHeight;
BYTE2 NumberOfColorPanels;
BYTE2 ColorDepth;
BYTE4 Compression;
BYTE4 ImageSize;
BYTE4 HorizontalResolution;
BYTE4 VerticalResolution;
BYTE4 NumberOfColors;
BYTE4 NumberOfImportantColors;
} BMP_INFO_HEADER;
/*<--Bitmap information header*//*Virtual bitmap-->*/
typedef struct BITMAP
{
BMP_FILE_HEADER bmpFileHeader;
BMP_INFO_HEADER bmpInfoHeader;
BYTE* bmpData;
} BITMAP;
/*<--Virtual bitmap*//*Compression types and values-->*/
#define BI_RGB 0
#define BI_RLE8 1
#define BI_RLE4 2
#define BI_BITFIELDS 3
#define BI_JPEG 4
#define BI_PNG 5
/*<--Compression types and values*/BITMAP readbmp(const char* _pcFileName)
{
FILE* bmp = fopen(_pcFileName, "rb");BITMAP bitmap; fread(&bitmap.bmpFileHeader, sizeof(BMP\_FILE\_HEADER), 1, bmp); fread(&bitmap.bmpInfoHeader, sizeof(BMP\_INFO\_HEADER), 1, bmp); fseek(bmp, bitmap.bmpFileHeader.Offset, SEEK\_SET); bitmap.bmpData = (BYTE\*)malloc(bitmap.bmpInfoHeader.ImageSize); fread(bitmap.bmpData, bitmap.bmpInfoHeader.ImageSize, 1, bmp); fclose(bmp); return bitmap;
}
To test my function i wrote a short program, which reads the above mentioned bitmap.
#include <iostream>
#include "readbmp.h" -
Hello guys, I'm trying to write a funtion to read a simple bitmap, but I've encountered a serious problem. As model I used the description of the bitmap file format from wikipedia http://en.wikipedia.org/wiki/BMP_file_format[^] and as far as I know this is definitely the correct format. I also used the same 2x2 24-bit bitmap[^] to test my function, but except for the first value (the magic number or identifier) all other read values are wrong.
#include <stdint.h>
#include <stdio.h>/*Data types-->*/
typedef uint8_t BYTE;
typedef uint16_t BYTE2;
typedef uint32_t BYTE4;
/*<--Data types*//*Bitmap file header-->*/
typedef struct BMP_FILE_HEADER
{
BYTE2 MagicNumber;
BYTE4 FileSize;
BYTE2 Reserved1;
BYTE2 Reserved2;
BYTE4 Offset;
} BMP_FILE_HEADER;
/*<--Bitmap file header*//*Bitmap information header-->*/
typedef struct BMP_INFO_HEADER
{
BYTE4 HeaderSize;
BYTE4 ImageWidth;
BYTE4 ImageHeight;
BYTE2 NumberOfColorPanels;
BYTE2 ColorDepth;
BYTE4 Compression;
BYTE4 ImageSize;
BYTE4 HorizontalResolution;
BYTE4 VerticalResolution;
BYTE4 NumberOfColors;
BYTE4 NumberOfImportantColors;
} BMP_INFO_HEADER;
/*<--Bitmap information header*//*Virtual bitmap-->*/
typedef struct BITMAP
{
BMP_FILE_HEADER bmpFileHeader;
BMP_INFO_HEADER bmpInfoHeader;
BYTE* bmpData;
} BITMAP;
/*<--Virtual bitmap*//*Compression types and values-->*/
#define BI_RGB 0
#define BI_RLE8 1
#define BI_RLE4 2
#define BI_BITFIELDS 3
#define BI_JPEG 4
#define BI_PNG 5
/*<--Compression types and values*/BITMAP readbmp(const char* _pcFileName)
{
FILE* bmp = fopen(_pcFileName, "rb");BITMAP bitmap; fread(&bitmap.bmpFileHeader, sizeof(BMP\_FILE\_HEADER), 1, bmp); fread(&bitmap.bmpInfoHeader, sizeof(BMP\_INFO\_HEADER), 1, bmp); fseek(bmp, bitmap.bmpFileHeader.Offset, SEEK\_SET); bitmap.bmpData = (BYTE\*)malloc(bitmap.bmpInfoHeader.ImageSize); fread(bitmap.bmpData, bitmap.bmpInfoHeader.ImageSize, 1, bmp); fclose(bmp); return bitmap;
}
To test my function i wrote a short program, which reads the above mentioned bitmap.
#include <iostream>
#include "readbmp.h"This is because the compiler will add padding-zeros in the structure to align the different members on a specific boundary. You can read more about the subject here[^] (or google to find additional information). To disable that, you have to pack your structure on a 1 byte boundary using
#pragma pack(1)
. See here[^]. On the other hand, why are you writing the code to read a bitmap ? There are already quite some libraries which can do that, so why reinventing the wheel ? (unless for learning purpose, in which case it is understandable).Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
This is because the compiler will add padding-zeros in the structure to align the different members on a specific boundary. You can read more about the subject here[^] (or google to find additional information). To disable that, you have to pack your structure on a 1 byte boundary using
#pragma pack(1)
. See here[^]. On the other hand, why are you writing the code to read a bitmap ? There are already quite some libraries which can do that, so why reinventing the wheel ? (unless for learning purpose, in which case it is understandable).Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Learning purpose ... not really. I know that there are a lot of libraries out there, which can load bitmaps. Actually I was just bored to death and wanted to do something. And I wanted a bitmap structure/class which gives me full control over all parameters and single values. Oh, and thanks, the parameters are now displayed correctly. There is just one thing I do not understand: The value which contains the image size is always 0x00.
-
Learning purpose ... not really. I know that there are a lot of libraries out there, which can load bitmaps. Actually I was just bored to death and wanted to do something. And I wanted a bitmap structure/class which gives me full control over all parameters and single values. Oh, and thanks, the parameters are now displayed correctly. There is just one thing I do not understand: The value which contains the image size is always 0x00.
Manfr3d wrote:
There is just one thing I do not understand: The value which contains the image size is always 0x00
It may be you won't need to use image-size, just width and height, and so don't need to worry too much about it. A quick search of (a limited amount of) code on my laptop that deals with bitmaps did not find it being used. Though Width and Height occur all the time as in a call to, for example, the Win32 function:
BitBlt(hDC,xDest,yDest,bmInfo.biWidth,bmInfo.biHeight,hDCMem,0,0,SRCCOPY);
-
Manfr3d wrote:
There is just one thing I do not understand: The value which contains the image size is always 0x00
It may be you won't need to use image-size, just width and height, and so don't need to worry too much about it. A quick search of (a limited amount of) code on my laptop that deals with bitmaps did not find it being used. Though Width and Height occur all the time as in a call to, for example, the Win32 function:
BitBlt(hDC,xDest,yDest,bmInfo.biWidth,bmInfo.biHeight,hDCMem,0,0,SRCCOPY);