Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

binery file read error

Status
Not open for further replies.

sagar474

Full Member level 5
Joined
Oct 18, 2009
Messages
285
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,318
Location
India,kakinada
Activity points
3,122
bindery file read error

I wrote the following code to read bmp file and write it to new bmp file
that is, to create a duplicate the bmp file.

but the created bmp file is not in correct format

when opened shows an error "this is not valid bmp file"
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct BITMAPFILEHEADER{
   unsigned short type;
   unsigned long size;
   unsigned short reserved1;
   unsigned short reserved2;
   unsigned long offsetbits;
};



struct BITMAPINFOHEADER{
   unsigned long size;
   unsigned long width;
   unsigned long height;
   unsigned short planes;
   unsigned short bitcount;
   unsigned long compression;
   unsigned long sizeimage;
   long xpelspermeter;
   long ypelspermeter;
   unsigned long colorsused;
   unsigned long colorsimportant;
};



struct SINGLE_PIXEL{
   unsigned char blue;
   unsigned char green;
   unsigned char red;
};

int i=0;
int S=0;
struct BITMAPFILEHEADER source_head;
struct BITMAPINFOHEADER source_info;
struct SINGLE_PIXEL source_pix;

struct head dist_head;
struct BITMAPINFOHEADER dist_info;
struct SINGLE_PIXEL dist_pix;


FILE *fp;
FILE *Dfp;

fp=fopen("rajni.bmp","rb");
Dfp=fopen("rajni2.bmp","wb");

fread(&source_head,sizeof(struct head),1,fp);
fread(&source_info,sizeof(struct BITMAPINFOHEADER),1,fp);

fwrite(&source_head,sizeof(struct head),1,Dfp);
fwrite(&source_info,sizeof(struct BITMAPINFOHEADER),1,Dfp);

S=source_info.width*source_info.height;

for(i=1;i<=S;i++)
{
fread(&source_pix,sizeof(struct SINGLE_PIXEL),1,fp);
fwrite(&source_pix,sizeof(struct SINGLE_PIXEL),1,Dfp);
}
fclose(fp);
fclose(Dfp);
return 0;
}
 

Your header file format seems to out of place. There is no better place: Wiki.
That having said I do believe the bmp was 14 byte initial header which doesn't match with yours. Then there is a DIB header.. which is variant.
 

I figured out the problem
this is only a data type misuse.
I used int for declaring i and S which. S stores the number of pixels to read or write. and i increments till it reaches to s.
the size of s depends on height and width of the image. my image is 400x300 hence the space that is allocated previously is insufficient.
and i changed it to unsigned long int. my stupidity is I used a signed integer where it is no question about sign while representing number of pixels.

thank you
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top