Note that there are some explanatory texts on larger screens.

plurals
  1. POMy function is copying a PGM image file to PPM in a different way
    primarykey
    data
    text
    <p>I have a very simple function that saves a PPM image:</p> <pre><code>void WriteCImage(CImage *cimg, char *filename) { FILE *fp; int i,n; fp = fopen(filename,"w"); fprintf(fp,"P6\n"); fprintf(fp,"%d %d\n",cimg-&gt;C[0]-&gt;ncols,cimg-&gt;C[0]-&gt;nrows); fprintf(fp,"255\n"); n = cimg-&gt;C[0]-&gt;ncols*cimg-&gt;C[0]-&gt;nrows; for (i=0; i &lt; n; i++) { fputc(cimg-&gt;C[0]-&gt;val[i],fp); fputc(cimg-&gt;C[1]-&gt;val[i],fp); fputc(cimg-&gt;C[2]-&gt;val[i],fp); } fclose(fp); } </code></pre> <p>as you can see, this function receives a matrix (in CImage format) and writes the image data in the ASCII file. It seems correct, but every time I copy a grayscale image to a PPM image I have problems. Look at the code:</p> <pre><code>//that's a PGM grayscale image gt = ReadImage(argv[1]); //creating an RGB image with same dimensions of the PGM image nwcimg = CreateCImage(gt-&gt;nrows,gt-&gt;ncols); n=gt-&gt;nrows*gt-&gt;ncols; //iterate through the PGM image for(index=0;index&lt;n;index++) { // just a copy of the grayscale image value to all 3 layeres //of the PPM (RGB) image nwcimg-&gt;C[0]-&gt;val[index]=gt-&gt;val[index]; nwcimg-&gt;C[1]-&gt;val[index]=gt-&gt;val[index]; nwcimg-&gt;C[2]-&gt;val[index]=gt-&gt;val[index]; } WriteCImage(nwcimg,"gt-copied.ppm"); DestroyCImage(&amp;nwcimg); DestroyImage(&amp;gt); </code></pre> <p>what problem i have? well, the code seems correct and simple. But when the cimage matrix/vector is written as a file, i can see that the two pictures are not the same. Seems like the pixels of the PGM image are 'shifted' or 'mirrored' in the copied image.</p> <p>You can see the <a href="http://www.ic.unicamp.br/~anselmoferreira/images/gt.pgm" rel="nofollow">Image File</a> and <a href="http://www.ic.unicamp.br/~anselmoferreira/images/gt-copy.ppm" rel="nofollow">The RGB copy</a> </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload