-1

I want to read a PGM file (without # comments) and export just the image data(matrix) to a new text file.So far I did managed to write the code,but not very well I think. My issue is that when I fprintf the matrix from memory to a text file it give me some lines of zeros.Same when i want to printf the matrix from memory.

There is the code:

#include<stdio.h>
#include<stdlib.h>

int main(){
int i,j;
FILE *fd;
FILE *fdd;

//Extract the file info from header 
char type;
int row=0,col=0,depht=0,a=0;
fd=fopen("file.pgm","rb");
fscanf(fd,"%s %d %d %d",&type,&row,&col,&depht);

//Malloc
int** sudo=(int **)malloc(row*sizeof(int *));
if ( sudo != NULL){
   for (k=0; k<row ;k++){
       sudo[k] =(int*) calloc (col,sizeof (int));
         }
       }

//Read the image data from the rest of the file
for(i=0;i<row;i++){
   for(j=0;j<col;j++){
        fscanf(fd,"%c ", &sudo[i][j]);
         }
      }
fclose(fd);

//
printf("Header:\nfile type: %s\nrow: %d\ncol: %d\ndepht: %d\n",&type,row,col,depht);

//Copy the matrix from memory to a new file
fdd=fopen("matrice.txt","wb");

for(i=0;i<row;i++){
   for(j=0;j<col;j++){
      fprintf(fdd,"%d ",sudo[i][j]);
      }
    fprintf(fdd,"\n");
 }

 free(sudo);
 fclose(fdd);
 return 0;
}

Any ideas?

Jongware
  • 22,200
  • 8
  • 54
  • 100
B. Parker
  • 41
  • 5
  • One major problem is that you are opening the PGM file in *binary* mode, even though it's a text file. That will most likely cause trouble with line endings depending on your platform and the platform where the PGM file was created. Same when you're writing to the new file, since you open it in binary mode, the newlines you print may be wrong for the platform you're on. – Some programmer dude Aug 28 '15 at 11:18
  • By the way, and unrelated to your problem, but you have a memory leak at the end of the program. You free `sudo` but not the contents of `sudo` (i.e. `sudo[0]`, `sudo[1]` etc). – Some programmer dude Aug 28 '15 at 11:37
  • I opened the file in normal mode too .It acts even worst than the binary mode (more than 3/4 of the file are zeros).*Fixed the memory leak – B. Parker Aug 28 '15 at 11:52
  • Why are you using `%c` for input, and `%d` for output? – William Pursell Aug 28 '15 at 13:21
  • please indent the code consistently for readability/understandabillity by us humans. Suggest indent after every opening brace '{' and un-indent before every closing brace '}'. 4 space indent would work well as 4 spaces will show in proportionally space fonts fonts and not eat up the available horizontal line – user3629249 Aug 28 '15 at 13:27
  • a .pgm file is all ascii, so should not be opened with the 'b' mode character. – user3629249 Aug 28 '15 at 13:45
  • there is no 'depth' value, however, there is a 'max pixel value' That max pixel value triggers when a pixel is 1 byte or two bytes. The posted code fails to take that into account. – user3629249 Aug 28 '15 at 13:52
  • In C, when calling malloc() ( and family of functions ) do not cast the returned value as it is a 'void *' so can be assigned to any other pointer. Always check (!=NULL) the returned value to assure the operation was successful – user3629249 Aug 28 '15 at 13:54
  • a .pgm file does not have any white space between the pixels. a .pgm file does not have any white space between rows. – user3629249 Aug 28 '15 at 13:58
  • 1
    It seems like the commenters may be talking past each other because nobody's mentioned the fact that there are 2 pgm formats: original (all text) and rawbits (header is text, pixels aren't). They are distinguished by the type, P2 or P5. And speaking of type, you have a `type` variable that's a single `char` and you're reading into it with `%s`; that's definitely going to overflow. –  Aug 28 '15 at 14:09
  • the method of reading the pixels is incorrect for a number of reasons. 1) it does not allow for the width of each pixel being all 1 byte or all 2 bytes. 2) the space in the format string means that certain values of pixel byte(s) can/will cause data to be skipped. – user3629249 Aug 28 '15 at 14:13
  • I got it.What method for reading would you recomend?PS: file is P5: so,header is ascii and image data is binary. – B. Parker Aug 28 '15 at 14:24
  • @WumpusQ.Wumbley, Your correct, the posted code is failing to confirm the actual type of the input file and inputting the data accordingly. However, the reference I linked to indicates the pixels are binary values, not ascii values with a type of P2 – user3629249 Aug 28 '15 at 14:24
  • You could check the 'maxval' field, if it is >255, then read 2 bytes per pixel otherwise read 1 byte per pixel. You might also use an appropriate editor and display the file in hex, so you can see what the actual format is – user3629249 Aug 28 '15 at 14:26
  • It's a uint_8 format. – B. Parker Aug 28 '15 at 14:30
  • At some point the maintainer of netpbm decided that the raw formats (P4, P5, P6) where the only "true" pnm formats, that the formats *originally* called pbm, pgm, and ppm (with types P1, P2, and P3 respectively; collectively called "pnm") should not be called pbm, pgm, ppm, or pnm any more, but only by the retronym "plain pnm". The official documentation is now written in a way that isn't consistent with history. (We have always been at war with plain pnm...) –  Aug 28 '15 at 14:37

1 Answers1

0

this is the format of a .pgm file, as indicted here:

<http://netpbm.sourceforge.net/doc/pgm.html>:

A PGM file consists of a sequence of one or more PGM images. 
There are no data, delimiters, or padding before, after, or between images.

Each PGM image consists of the following:

    A "magic number" for identifying the file type. 
        A pgm image's magic number is the two characters "P5".
    Whitespace (blanks, TABs, CRs, LFs).
    A width, formatted as ASCII characters in decimal.
    Whitespace.
    A height, again in ASCII decimal.
    Whitespace.
    The maximum gray value (Maxval), again in ASCII decimal. 
        Must be less than 65536, and more than zero.
    A single whitespace character (usually a newline).
    A raster of Height rows, in order from top to bottom. 
       Each row consists of Width gray values, in order from left to right. 
    Each gray value is a number from 0 through Maxval, with 0 being black and Maxval being white. 
    Each gray value is represented in pure binary by either 1 or 2 bytes. 
    If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes. 
    The most significant byte is first.

    A row of an image is horizontal. 
    A column is vertical. 
    The pixels in the image are square and contiguous.

    Each gray value is a number proportional to the intensity of the pixel, 
    adjusted by the ITU-R Recommendation BT.709 gamma transfer function.  
    (That transfer function specifies a gamma number of 2.2 and has a linear section for small intensities). 
    A value of zero is therefore black. 
    A value of Maxval represents CIE D65 white 
       and the most intense value in the image 
       and any other image to which the image might be compared.

    Note that a common variation on the PGM format 
       is to have the gray value be "linear," 
       i.e. as specified above except without the gamma adjustment.
       `pnmgamma` takes such a PGM variant as input and produces a true PGM as output.

    In the transparency mask variation on PGM, the value represents opaqueness. It is proportional to the fraction of intensity of a pixel that would show in place of an underlying pixel. So what normally means white represents total opaqueness and what normally means black represents total transparency. In between, you would compute the intensity of a composite pixel of an "under" and "over" pixel as under * (1-(alpha/alpha_maxval)) + over * (alpha/alpha_maxval). 
    Note that there is no gamma transfer function in the transparency mask. 

Strings starting with "#" may be comments, the same as with PBM.

Note that you can use `pamdepth` to convert between a the format with 1 byte per gray value and the one with 2 bytes per gray value.

All characters referred to herein are encoded in ASCII. 
"newline" refers to the character known in ASCII as Line Feed or LF. 
A "white space" character is space, CR, LF, TAB, VT, or FF 
    (I.e. what the ANSI standard C isspace() function calls white space). 
user3629249
  • 16,402
  • 1
  • 16
  • 17