-
Zooming Algorithm Bug
Hi Guys this is Jagadish I have written a zooming Algorithm in C which is not working for some for the values..Unable to find out the Bug..Kindly help me...The person who fix the bug will be appreciated..
thanx
jagadish
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);
}
-
I have no idea what values you are thinking of, and what values you think you should be getting. This doesn't seem like a zoom, so much as a resize; zooming out 0% does nothing, while typing in "0" to this program annihilates your picture. So I guess the question is, what do you want and what do you get that isn't what you want?
-
It's not working for some values? It sounds like you expect us to try random values ourselves and you're hoping that we'll hit one of those values where it doesn't work. Can you not just tell us values for which it does not work? Does it depend on the image size?
As it happens I'm away on holiday and this laptop doesn't have a compiler, but if you had given the values used and described the result, I may have had an answer for you already.
These are things you'll learn to provide up front. You'll enable us to help you much quicker that way.
What I can tell by looking at it is that it would be very slow, in other words, there is a ton of room for optimisations there. If you're concerned about speed and what simple changes you can do to make it waaay faster, then also let us know.