#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
//#define height 256
//#define width 256
char original_image[25];
char reconstruct_image[25];
int main(int arg,char *argv[])
{
printf("\n******************************************************************");
printf("\n original_image reconstructed_image flag 1:raw->pgm 0:pgm->raw");
printf("\n *****************************************************************");
if(arg==1)
{
printf("\nplease input:original_image reconstructed_image flag 1:raw->pgm 0:pgm->raw\n");
return 1;
}
if(arg==4)
{
int flag;
int height=512 ,width=512,maxValue=255;
unsigned char *idata;
FILE *fp;
flag=atoi(argv[3]);
strcpy(original_image,argv[1]);
strcpy(reconstruct_image,argv[2]);
idata=(unsigned char *)malloc(width*height*(sizeof(unsigned char)));
if(flag==1)
{
if((fp=fopen(original_image,"rb"))==NULL)
{
printf("\nCan not original_image");
exit(1);
}
if(fread(idata,sizeof(unsigned char),width*height,fp)!=width*height)
printf("\nError to read original image!!!");
fclose(fp);
if((fp=fopen(reconstruct_image,"wb"))==NULL)
{
printf("\nCan not open reconstruct_image");
exit(1);
}
fprintf(fp,"P5\n");
fprintf(fp,"%d %d\n",width,height);
fprintf(fp,"%d\n",maxValue);
if(fwrite(idata,sizeof(unsigned char),width*height,fp)!=width*height)
printf("\nError to write image!!!");
fclose(fp);
}
else
{
if((fp=fopen(original_image,"rb"))==NULL)
{
printf("\nCan not original_image");
exit(1);
}
fseek(fp,16L,SEEK_SET);
if(fread(idata,sizeof(unsigned char),width*height,fp)!=width*height)
printf("\nError to read original image!!!");
fclose(fp);
if((fp=fopen(reconstruct_image,"wb"))==NULL)
{
printf("\nCan not open reconstruct_image");
exit(1);
}
if(fwrite(idata,sizeof(unsigned char),width*height,fp)!=width*height)
printf("\nError to write image!!!");
fclose(fp);
}
free(idata);
}
return 0;
}