Thread: How to write image data to binary PGM file format(P5)?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    How to write image data to binary PGM file format(P5)?

    Hi all,
    I fail to write the image data to binary PGM format (P5).
    Let's say i have to write a image data into PGM P5 format.
    I have the information for width, height, Y_min, Y_max, XL, XR and the TEMPLATE1 (data matrix) ==>A region of interest from an image.
    Y_min & Y_max = minimum and maximum of the height value.
    XL & XR = min and max of the width value for the image.

    First, i call the subroutine,
    Code:
    write_data_grey_binary(width1,height1,XL1,XR1,Y_min1,Y_max1,filename99[total],TEMPLATE1); 
    
    void write_data_grey_binary(width,height,XL,XR,Y_min,Y_max,filename,output)
    int width,height,XL,XR,Y_min,Y_max;
    char *filename;
    unsigned char** output; 
    {
        int i,j,nread;
        FILE *fp;
        unsigned char* temp;
    
        if((fp = fopen(filename,"w")) == NULL){
            printf("File 333 output data  can not open\n");
            exit(1);
        }
    	
    	fprintf(fp,"P5\n%d  %d\n%d\n",width,height,(GRYSCL-1));
    
    	temp = (unsigned char*)calloc(height*width, sizeof(unsigned char));
    
    	for(i=Y_min;i<=Y_max;i++){
       		for(j=XL;j<=XR;j++){  
    	            temp[(i*width)+j]= (unsigned char)output[i][j];
    		}
    	}
    	nread = fwrite((void*)temp, sizeof(unsigned char), (height*width), fp);
    	free(temp);
                    fclose(fp);
    	return(0);
    }
    However , i have an error at the line " temp[(i*width)+j]= (unsigned char)output[i][j]; ".
    However, this coding can be ran when i want to write the full image like:
    write_data_grey_binary(width,height,0,width-1,0,height-1,filename99[total],inpgrey);

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Why not just copy temp then increment it in your loop? It would be faster and easier to read. IE:
    Code:
    unsigned char *ptr = temp;
    for(i=Y_min;i<=Y_max;i++)
    {
         for(j=XL;j<=XR;j++, ptr++)
        {  
             *ptr = (unsigned char)output[i][j];
        }
    }
    [edit] What error messages do you get? [/edit]
    Last edited by mike_g; 10-08-2007 at 06:33 AM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this part of an existing bit of old code? The old-style function prototype tells me that it is.

    I don't see the point of the cast here:
    Code:
    temp[(i*width)+j]= (unsigned char)output[i][j];
    Both sides are already given as "unsigned char", so the cast doesn't change anything.

    Also, there's no need to cast the return value of calloc:
    Code:
    temp = (unsigned char*)calloc(height*width, sizeof(unsigned char));
    This just leads to problems if you don't include the correct header files.

    You also seem to mix text (fprintf()) with binary output to the file. That's generally not a good plan.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Thanks for the advices guys. I have solve the problem.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Why are you using temporary space? There's no need for it...

    Code:
    for(i=Y_min;i<=Y_max;i++){
            fwrite(output[i] + XL, 1, XL - XR + 1, fp);
    }
    Also, your loops are based on XL..XR and Y_min..Y_max, and yet the values for width and height you output to the file come from somewhere else. This means that if somebody passes a width that is not exactly XL - XR + 1, or a height which is not exactly Y_max - Y_min + 1, things blow up. Why pass the width and height at all, since they can be computed from the rectangle?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    You also seem to mix text (fprintf()) with binary output to the file. That's generally not a good plan.
    No problem mixing fprintf() and fwrite(). Writing to files would be a true pain if it were otherwise.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Thanks a lot for these advices. I have learned a lot from here though the problem has been solved.
    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. write in a binary file
    By singersinger in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2007, 03:24 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM