Sign in to follow this  

problem with read a bitmap in "C"

This topic is 2855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hallo! I made an application that reads the header of a bitmap file. Reading the header goes well, but getting de bitmap data not. So my question is, can anybody please help me with fixing my problem. Here is my "C" code("main.c").
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "main.h"

int main(){
	MyBitmap bitmaptest;
	memset(&bitmaptest,0,sizeof(bitmaptest));
	printf("My Bitmap Header Reading Test\n");
	if(ReadBitmap("dummy.bmp",bitmaptest) == 1){
		printf("error on ReadBitmap()\n");
		system("pause");
	}
	return 0;
}

int ReadBitmap(char *filename,MyBitmap bmp){
	FILE *fp;
	fp = fopen(filename,"rb");
	if(fp != NULL){		//
		fread(&bmp.id[0],sizeof(char),1,fp);
		fread(&bmp.id[1],sizeof(char),1,fp);
		fread(&bmp.filesize,sizeof(bmp.filesize),1,fp);
		fread(&bmp.dump1,sizeof(bmp.dump1),1,fp);
		fread(&bmp.offset,sizeof(bmp.offset),1,fp);
		fread(&bmp.headsize,sizeof(bmp.headsize),1,fp);
		fread(&bmp.width,sizeof(bmp.width),1,fp);
		fread(&bmp.height,sizeof(bmp.height),1,fp);
		fread(&bmp.planes,sizeof(bmp.planes),1,fp);
		fread(&bmp.bit,sizeof(bmp.bit),1,fp);
		fread(&bmp.compression,sizeof(bmp.compression),1,fp);
		fread(&bmp.datasize,sizeof(bmp.datasize),1,fp);
		fread(&bmp.hres,sizeof(bmp.hres),1,fp);
		fread(&bmp.vres,sizeof(bmp.vres),1,fp);
		fread(&bmp.colors,sizeof(bmp.colors),1,fp);
		fread(&bmp.imp_colors,sizeof(bmp.imp_colors),1,fp);
		bmp.data = (BYTE*)malloc(bmp.filesize-bmp.offset);
		if(bmp.data == NULL){
			printf("bitmap data is empty...\n");
			system("pause");
			return 1;
		}
		memset(bmp.data,0,bmp.filesize-bmp.offset);
		fseek(fp,bmp.offset,SEEK_SET);
		fread(&bmp.data,sizeof(BYTE),bmp.filesize-bmp.offset ,fp);
		fclose(fp);
		printf("Filesize: %u\n",bmp.filesize);
		printf("offset adress: %i\n",bmp.offset);
		printf("Header Size: %u\n",bmp.headsize);
		printf("width: %u\n",bmp.width);
		printf("Height: %u\n",bmp.height);
		printf("planes: %u\n",bmp.planes);
		printf("Bit rate: %u\n",bmp.bit);
		printf("Compression: %u\n",bmp.compression);
		printf("Bitmap Datasize: %u\n",bmp.datasize);
		printf("hResolution: %u\n",bmp.hres);
		printf("vResolution: %u\n",bmp.vres);	
		printf("Colors: %u\n",bmp.colors);
		printf("important colors: %u\n",bmp.imp_colors);
		system("pause");

		fp = fopen("dump.raw","wb");
		fwrite(bmp.data,sizeof(BYTE),(bmp.filesize-bmp.offset),fp);
		fclose(fp);
	}
	else{
		return 1;
	}
	return 0;
}






And here is my header file("main.c")
#ifndef _BITMAPLOADER
#define _BITMAPLOADER
typedef struct tagMyBitmap{
	char	id[1];
	DWORD	filesize;//echte sizeof bitmap data
	DWORD	dump1;
	DWORD	offset;
	DWORD	headsize;
	DWORD	width;
	DWORD	height;
	WORD	planes;
	WORD	bit;
	DWORD	compression;
	DWORD	datasize;
	DWORD	hres;
	DWORD	vres;
	DWORD	colors;
	DWORD	imp_colors;
	byte	*palette;
	BYTE	*data;///is dit wel goed? moet dit niet gewoon een local var zijn

}MyBitmap;
#endif

extern int ReadBitmap(char *filename,MyBitmap bmp);




Thank you. [Edited by - ApochPiQ on February 21, 2010 5:29:08 PM]

Share this post


Link to post
Share on other sites

This topic is 2855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this