Cooker
February 8th, 2005, 05:21 AM
Hi,
I want to read a graphic(Width:266,Height:341).
After reading this graphic, I will do something on it and store it.
I write the following codes:
#include <stdio.h>
#include <windows.h>
int main()
{
BITMAPFILEHEADER bmheader;
BITMAPINFOHEADER bm;
FILE *fp;
unsigned char *data;
fp = fopen("origin.bmp","rb");
if ( fp == NULL ) {
printf("Cannot open file!\n");
return -1;
}
fread(&bmheader,1,sizeof(BITMAPFILEHEADER),fp);
fread(&bm,1,sizeof(BITMAPINFOHEADER),fp);
data = (unsigned char *)malloc(bm.biHeight*bm.biWidth*3*sizeof(unsigned char));
fread(data,1,bm.biHeight * bm.biWidth * 3,fp);
fclose(fp);
// do something...
fp = fopen("result.bmp","wb");
fwrite(&bmheader,1,sizeof(BITMAPFILEHEADER),fp);
fwrite(&bm,1,sizeof(BITMAPINFOHEADER),fp);
fwrite(data,1,bm.biHeight * bm.biWidth * 3, fp);
fclose(fp);
free(data);
return 0;
}
I just write the read and store parts.
But I find that the stored graphic can't display correctly.
I think the size of graphic file is 272172 bytes.
( sizeof(BITMAPFILEHEADE) + sizeof(BITMAPINFOHEADER) + raster data( 3 * Width * Height) )
But I find the size of graphic file is 272854. The difference is 682 ( 272854-272172).
What' s the purpose of the 682 bytes?
And what shall I do to store correctly?
Thank you!
I want to read a graphic(Width:266,Height:341).
After reading this graphic, I will do something on it and store it.
I write the following codes:
#include <stdio.h>
#include <windows.h>
int main()
{
BITMAPFILEHEADER bmheader;
BITMAPINFOHEADER bm;
FILE *fp;
unsigned char *data;
fp = fopen("origin.bmp","rb");
if ( fp == NULL ) {
printf("Cannot open file!\n");
return -1;
}
fread(&bmheader,1,sizeof(BITMAPFILEHEADER),fp);
fread(&bm,1,sizeof(BITMAPINFOHEADER),fp);
data = (unsigned char *)malloc(bm.biHeight*bm.biWidth*3*sizeof(unsigned char));
fread(data,1,bm.biHeight * bm.biWidth * 3,fp);
fclose(fp);
// do something...
fp = fopen("result.bmp","wb");
fwrite(&bmheader,1,sizeof(BITMAPFILEHEADER),fp);
fwrite(&bm,1,sizeof(BITMAPINFOHEADER),fp);
fwrite(data,1,bm.biHeight * bm.biWidth * 3, fp);
fclose(fp);
free(data);
return 0;
}
I just write the read and store parts.
But I find that the stored graphic can't display correctly.
I think the size of graphic file is 272172 bytes.
( sizeof(BITMAPFILEHEADE) + sizeof(BITMAPINFOHEADER) + raster data( 3 * Width * Height) )
But I find the size of graphic file is 272854. The difference is 682 ( 272854-272172).
What' s the purpose of the 682 bytes?
And what shall I do to store correctly?
Thank you!