Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
zoom();
int round(float v)
{
// return v;
int iv = v;
if( int(v-.5) == iv)
return iv+1;
return iv;
}
void main()
{
zoom();
}
zoom()
{
int width=0,height=0,Maxval=0,s;
float i,j,p,k;
char readChars[30];
int count=0;
FILE *ipfile;
FILE *opfile;
ipfile = fopen("C:\\Documents and Settings\\Jagadish\\Desktop\\PPM to PGM\\pepper1.pgm","rb");
if (!ipfile)
{
printf("Error: Unable to open file %s.\n\n",ipfile);
exit(1);
}
printf ("Reading PPM file: %s...\n",ipfile);
fscanf (ipfile, "%s", readChars);
if (strcmp(readChars, "P5")==0)
{
printf("valid file type\n");
}
else
{
printf("Invalid file type\n");
exit(1);
}
char ch1=getc(ipfile);
fscanf(ipfile,"%d",&width);
printf("width=%d\n",width);
char ch2=getc(ipfile);
fscanf(ipfile,"%d",&height);
printf("height=%d\n",height);
char ch3=getc(ipfile);
fscanf(ipfile,"%d",&Maxval);
printf("Maxval=%d\n",Maxval);
unsigned char ch4=getc(ipfile);
unsigned char *ip,*op;
ip=(unsigned char *)malloc (width * height);
printf("Enter the percentage to Zoom Out\n");
scanf("%f",&p);
//These many value which has to be moved from input file.
k=p/100;
printf("k=%f\n",k);
op=(unsigned char *)malloc( width*k * height*k*1.1);
s=fread(ip,1,(width*height),ipfile);
printf ("Loaded PPM. Size: %dx%d, Maxval: %d \n",width, height,Maxval);
printf("width is %d\n",(int)((width)*k));
printf("height is %d\n",(int)((height)*k));
opfile=fopen("C:\\Documents and Settings\\Jagadish\\Desktop\\PPM to PGM\\newpeppers.pgm","wb");
fprintf( opfile, "P5\t" );
fprintf( opfile, "%d\t",(int)((width)*k));
fprintf( opfile, "%d\t",(int)((height)*k));
fprintf( opfile, "%d\t",Maxval);
unsigned char *iip, *oop = op;
float z=(3/k);
printf("z=%f\n",z);
int ij, pj;
for(i=0;i<height;i+=(1/k))
{
iip = ip + round(i)*width;
j = 0;
pj = 0;
while(1)
{
*oop = iip[0];
oop++;
pj = round(j);
j+=(1/k);
ij = round(j);
if(ij>=width)
break;
if( ij != pj )
iip += (ij-pj); //these many values which has to be moved from input file.
}
}
fwrite(op, 1,(height)*k*(width)*k, opfile);
fclose(ipfile);
fclose(opfile);
}