Is this right?

This is a discussion on Is this right? within the C Programming forums, part of the General Programming Boards category; NOTE: I had a previous thread about this, but since that post, I've attempted to come up with some solutions. ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    3

    Is this right?

    NOTE: I had a previous thread about this, but since that post, I've attempted to come up with some solutions.

    Just to show you, this is the place in my program where I got the variables for width and height from the input image file:

    Code:
    void read_header(FILE *new)
    {
       int max_color;
       char P[10];
    
    
       fgets(P, 10, new);
       fscanf(new, "%d %d", &width, &height);
       fscanf(new, "%d", &max_color);
    }

    PART 1:
    What I need to do is print out an error if the file size exceeds the 500 by 500 measurements (defined at the top as max_width and height).

    PART 2: The other part is that I have to read the pixel information from the input file and store it into a 2d array. Each pixel has 3 values for red, green, and blue, but I'm not sure if this matters.

    My attempt at both solutions:

    Part 1:
    Code:
    void check_file_size /*Don't know what to put because width and height are global variables*/
    {
       if (width > 500 && height > 500)
       {
          perror("Error: File size too big.\n");
       }
    }
    Part 2:

    Code:
    void store_into_array(FILE *input)
    {
       int array[max_width][max_height];
    
    
       for (x = 0; x < width; x++)
       {
          for (y = height; y >=0; y--)
          {
             fscanf(input, "%d", &array[x][y]);
          }
       }
    }
    Last edited by klee87; 06-03-2013 at 09:42 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Why are width and height global variables?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    3
    Because I had to use it in two separate helper functions, the one where I check the file size and the one where I stored the values into the 2d array.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by klee87
    Because I had to use it in two separate helper functions, the one where I check the file size and the one where I stored the values into the 2d array.
    Poor excuse. You should make them local variables and pass them to the helper functions instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    235
    Given the info provided by this and the other post, along with the info provided by this link, here is a possible approach, regarding PART1 of your question...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAGIC		"P3"	/* http://netpbm.sourceforge.net/doc/ppm.html */
    #define MAX_H		500
    #define MAX_W		500
    
    typedef struct {
    	char magic[3];
    	int width, height;
    	int maxColor;
    	int pix[MAX_H][MAX_W];
    }Ppm;
    
    /* --------------------------------------------------- */
    int ppm_has_valid_header( Ppm *image, int maxWidth, int maxHeight )
    {
    	if ( !image || maxWidth < 1 || maxHeight < 1 )
    		return 0;	/* false */
    
    	return	0 == strcmp(MAGIC, image->magic)
    		&& image->width  > 0 && image->width  < 1 + maxWidth
    		&& image->height > 0 && image->height < 1 + maxHeight
    		;
    }
    /* --------------------------------------------------- */
    int main( void )
    {
    	Ppm img;
    	int temp;
    
    	FILE *fp = fopen( "_ppm.txt", "r" );
    	if ( !fp )
    		goto failed_exit;
    
    	/* start fresh */
    	memset( &img, 0, sizeof(Ppm) );
    
    	/* PART1 : read header data */
    	temp = fscanf( fp, "%2s %d %d %d", img.magic, &img.width, &img.height, &img.maxColor );
    	if ( 4 != temp
    	|| !ppm_has_valid_header(&img, MAX_W, MAX_H)
    	){
    		fputs( "*** invalid or too big image, aborting...\n", stderr );
    		goto failed_exit;
    	}
    
    	printf( "%s\t(magic)\n%d %d\t(width height)\n%d\t(max color value)\n",
    		img.magic, img.width, img.height, img.maxColor
    		);
    
    	/* PART2: read pixel data into img.pix
    	...
    	*/
    
    	fclose( fp );
    
    	return 0;
    
    failed_exit:
    	if (fp)
    		fclose(fp);
    	return 1;
    
    }
    PART2 is more tricky. I would most probably read all three pixel samples using just one scanf(), because the file is expected to be well-formatted.

    Something like the following...

    Code:
    	hasmore = 1; /* true */
    	for (i=0; hasmore && i < img.height; i++)
    	{
    		for (j=0; j < 3 * img.width; j+=3) 
    		{
    			temp = fscanf(	fp,
    					"%d %d %d",
    					&img.pix[i][j],		/* red */
    					&img.pix[i][j+1],	/* green */
    					&img.pix[i][j+2]	/* blue */
    					);
    			...
    		}
    	}
    with temp and hasmore used for further validating the process, informing the user and breaking out of the nested loop as needed.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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