Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This works for me.</p> <pre><code>//gcc c2.c -std=c99 -g -Wall -Wextra #include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #include &lt;assert.h&gt; typedef struct pgm_type pgm_type; struct pgm_type{ char filetype[2]; int width,height,maxgray; unsigned char*p; }; int readpgm (pgm_type * header, char*fn) { FILE *instream; int size, read; instream = fopen (fn,"rb"); assert(instream); fscanf (instream, "%2s%d%d%d", header-&gt;filetype, &amp;header-&gt;width, &amp;header-&gt;height, &amp;header-&gt;maxgray); printf("%2s %d %d %d\n",header-&gt;filetype,header-&gt;width, header-&gt;height,header-&gt;maxgray); if (!header-&gt;filetype[0] == 'P' || !header-&gt;filetype[1] == '5') { printf ("Incorrect Type"); } size = header-&gt;width * header-&gt;height; header-&gt;p = malloc (size * sizeof (char)); read = fread (header-&gt;p, 1, size, instream); if (read != size) { printf ("Incorrect Size"); } return size; } void printFile (int x1, int x2, int y1, int y2,pgm_type * header, char*fn) { FILE *outstream; int wide=x2-x1+1,high=y2-y1+1; printf("cropping to %dx%d\n",wide,high); outstream = fopen (fn, "wb"); assert (outstream); fprintf (outstream, "%2s\n%i %i\n%i\n", header-&gt;filetype, wide, high, header-&gt;maxgray); unsigned char image[header-&gt;height][header-&gt;width]; unsigned char *pix = malloc ((wide * high) * sizeof (char)); int a = 0; for (int b = 0; b &lt; header-&gt;height; ++b) { for (int c = 0; c &lt; header-&gt;width; ++c) { image[b][c] = header-&gt;p[a]; ++a; } } int k = 0; for (int j = y1; j &lt;= y2; ++j) { for (int i = x1; i &lt;= x2; ++i) { pix[k] = image[j][i]; ++k; } } fwrite (pix, wide,high, outstream); free (pix); fclose (outstream); } int main() { pgm_type h; readpgm(&amp;h,"lena.pgm"); printFile(64,129,32,230,&amp;h,"o2.pgm"); return 0; } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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