#include<stdlib.h>
#include<string.h>
#include<conio.h>
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef int LONG;
typedef struct _BMPFH
{
BYTE bftype1;
BYTE bftype2;
DWORD bfsize;
WORD bfreserved1;
WORD bfreserved2;
DWORD bfOffbits;
} __attribute__((__packed__))BMPFH;
typedef struct _BMPIH
{
DWORD bisize;
LONG biw;
LONG bih;
WORD biplane;
WORD bibitcount;
DWORD biComp;
DWORD bisizeimage;
LONG bix;
LONG biy;
DWORD biclused;
DWORD biclimp;
} __attribute__((__packed__))BMPIH;
typedef struct _PALET
{
BYTE rgbblue;
BYTE rgbgreen;
BYTE rgbred;
BYTE rgbreserved;
} __attribute__((__packed__))PALET;
typedef struct _IMAGE
{
BMPFH bmpfh;
BMPIH bmpih;
PALET *palet;
BYTE *data;
} __attribute__((__packed__)) IMAGE;
IMAGE *ImageOku (IMAGE *image,char *filename)
{
BMPFH bmpfh;
BMPIH bmpih;
FILE *fp;
DWORD r,satirsize,size;
fp=fopen(filename,"rb");
if(fp==NULL)
{
printf("Dosya bulunamadi.");
exit(1);
}
fread(&bmpfh,sizeof(BMPFH),1,fp);
fread(&bmpih,sizeof(BMPIH),1,fp);
image=(IMAGE *) malloc(bmpfh.bfsize);
if(image==NULL)
{
printf("Bellek acilmadi.");
exit(1);
}
image->bmpfh=bmpfh;
image->bmpih=bmpih;
r=0;
if(bmpih.bibitcount==1) r=2;
if(bmpih.bibitcount==4) r=16;
if(bmpih.bibitcount==8) r=256;
if(r!=0)
{
image->palet=(PALET*) malloc(4*r);
fread(image->palet,4*r,1,fp);
}
satirsize=(image->bmpih.biw*image->bmpih.bibitcount+31)/32*4;
size=satirsize*image->bmpih.bih;
image->data=(BYTE *)malloc (size);
fread(image->data,size,1,fp);
fclose(fp);
return image;
}
void ImageYaz(IMAGE *image,char *filename)
{
FILE *fp;
int r,satirsize,size;
fp=fopen(filename,"wb");
if(fp==NULL) {printf("Dosya hatası..");exit(1);}
fwrite(&image->bmpfh,sizeof(BMPFH),1,fp);
fwrite(&image->bmpih,sizeof(BMPIH),1,fp);
r=0;
if(image->bmpih.bibitcount==1) r=2;
if(image->bmpih.bibitcount==4) r=16;
if(image->bmpih.bibitcount==8) r=256;
if(r!=0) fwrite(image->palet,4*r,1,fp);
satirsize=(image->bmpih.biw*image->bmpih.bibitcount+31)/32*4;
size=satirsize*image->bmpih.bih;
fwrite(image->data,size,1,fp);
fclose(fp);
}
main()
{
IMAGE *image;
printf("\n%d %d",sizeof(BMPIH),sizeof(PALET));
image=ImageOku(image,"Charles.bmp");
ImageYaz(image,"Charles1.bmp");
free(image);
system("pause");
return 0;
}