Click here to Skip to main content
15,888,984 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionMultimap (STL) equal_range function Pin
AkashAg3-May-10 1:26
AkashAg3-May-10 1:26 
AnswerRe: Multimap (STL) equal_range function Pin
CPallini3-May-10 2:08
mveCPallini3-May-10 2:08 
AnswerRe: Multimap (STL) equal_range function Pin
Stephen Hewitt3-May-10 5:00
Stephen Hewitt3-May-10 5:00 
Questioncrasing in CAsyncSocket Accept Pin
Member 36537513-May-10 1:06
Member 36537513-May-10 1:06 
QuestionRe: crasing in CAsyncSocket Accept Pin
David Crow3-May-10 3:00
David Crow3-May-10 3:00 
AnswerRe: crasing in CAsyncSocket Accept Pin
Member 36537513-May-10 3:55
Member 36537513-May-10 3:55 
GeneralRe: crasing in CAsyncSocket Accept Pin
Rajesh R Subramanian3-May-10 4:26
professionalRajesh R Subramanian3-May-10 4:26 
QuestionProblems reading Bitmaps Pin
Manfr3d3-May-10 1:02
Manfr3d3-May-10 1:02 
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"

#define DBG(x)	(cout << #x << "\t:\t" << (x) << endl)

using namespace std;

int main(void)
{
	BITMAP myBmp;
	myBmp = readbmp("..\\bmp24.bmp");

	DBG(myBmp.bmpFileHeader.MagicNumber);
	DBG(myBmp.bmpFileHeader.FileSize);
	DBG(myBmp.bmpFileHeader.Reserved1);
	DBG(myBmp.bmpFileHeader.Reserved2);
	DBG(myBmp.bmpFileHeader.Offset);
	DBG(myBmp.bmpInfoHeader.HeaderSize);
	DBG(myBmp.bmpInfoHeader.ImageWidth);
	DBG(myBmp.bmpInfoHeader.ImageHeight);
	DBG(myBmp.bmpInfoHeader.NumberOfColorPanels);
	DBG(myBmp.bmpInfoHeader.ColorDepth);
	DBG(myBmp.bmpInfoHeader.Compression);
	DBG(myBmp.bmpInfoHeader.ImageSize);
	DBG(myBmp.bmpInfoHeader.HorizontalResolution);
	DBG(myBmp.bmpInfoHeader.VerticalResolution);
	DBG(myBmp.bmpInfoHeader.NumberOfColors);
	DBG(myBmp.bmpInfoHeader.NumberOfImportantColors);
	cin.get();
	return 0;
}


This code just writes out the header informations as following:

myBmp.bmpFileHeader.MagicNumber : 19778
myBmp.bmpFileHeader.FileSize : 0
myBmp.bmpFileHeader.Reserved1 : 0
myBmp.bmpFileHeader.Reserved2 : 54
myBmp.bmpFileHeader.Offset : 2621440
myBmp.bmpInfoHeader.HeaderSize : 131072
myBmp.bmpInfoHeader.ImageWidth : 131072
myBmp.bmpInfoHeader.ImageHeight : 65536
myBmp.bmpInfoHeader.NumberOfColorPanels : 24
myBmp.bmpInfoHeader.ColorDepth : 0
myBmp.bmpInfoHeader.Compression : 0
myBmp.bmpInfoHeader.ImageSize : 247660544
myBmp.bmpInfoHeader.HorizontalResolution : 247660544
myBmp.bmpInfoHeader.VerticalResolution : 0
myBmp.bmpInfoHeader.NumberOfColors : 0
myBmp.bmpInfoHeader.NumberOfImportantColors : 0

What am I doing wrong? It looks as if the parameters are in a wrong order, even though I used the one described on wikipedia.
Thanks for your help.
AnswerRe: Problems reading Bitmaps Pin
Cedric Moonen3-May-10 1:08
Cedric Moonen3-May-10 1:08 
GeneralRe: Problems reading Bitmaps Pin
Manfr3d3-May-10 1:27
Manfr3d3-May-10 1:27 
GeneralRe: Problems reading Bitmaps Pin
Jonathan Davies3-May-10 2:16
Jonathan Davies3-May-10 2:16 
GeneralRe: Problems reading Bitmaps Pin
Manfr3d3-May-10 5:17
Manfr3d3-May-10 5:17 
QuestionHow to Convert from console application with .C files to MFC based application Pin
manoharbalu2-May-10 20:26
manoharbalu2-May-10 20:26 
AnswerRe: How to Convert from console application with .C files to MFC based application Pin
KingsGambit2-May-10 20:51
KingsGambit2-May-10 20:51 
AnswerRe: How to Convert from console application with .C files to MFC based application Pin
CPallini2-May-10 21:15
mveCPallini2-May-10 21:15 
AnswerRe: How to Convert from console application with .C files to MFC based application Pin
Cedric Moonen2-May-10 21:22
Cedric Moonen2-May-10 21:22 
AnswerRe: How to Convert from console application with .C files to MFC based application Pin
Rajesh R Subramanian2-May-10 22:57
professionalRajesh R Subramanian2-May-10 22:57 
GeneralRe: How to Convert from console application with .C files to MFC based application Pin
CPallini3-May-10 21:19
mveCPallini3-May-10 21:19 
GeneralRe: How to Convert from console application with .C files to MFC based application Pin
Rajesh R Subramanian3-May-10 22:31
professionalRajesh R Subramanian3-May-10 22:31 
QuestionWin32 / C++ Pin
Fareed Rizkalla2-May-10 10:33
Fareed Rizkalla2-May-10 10:33 
AnswerRe: Win32 / C++ Pin
Code-o-mat2-May-10 10:48
Code-o-mat2-May-10 10:48 
AnswerRe: Win32 / C++ Pin
Michel Godfroid2-May-10 10:51
Michel Godfroid2-May-10 10:51 
AnswerRe: Win32 / C++ Pin
ThatsAlok2-May-10 20:04
ThatsAlok2-May-10 20:04 
QuestionMultithreading in general Pin
csrss2-May-10 9:22
csrss2-May-10 9:22 
AnswerRe: Multithreading in general Pin
CPallini2-May-10 9:39
mveCPallini2-May-10 9:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.