Zooming Algorithm Bug

This is a discussion on Zooming Algorithm Bug within the C Programming forums, part of the General Programming Boards category; Hi Guys this is Jagadish I have written a zooming Algorithm in C which is not working for some for ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1

    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);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21