title: Trying to extract pixel values from a given PNG image
body: <p><strong>Trying to understand PNG format.</strong> </p>
  
  <p>Consider this PNG Image:</p>
  
  <p><img src="https://i.stack.imgur.com/DhV9Z.png" alt="enter image description here"></p>
  
  <p>The Image is taken from <a href="http://en.wikipedia.org/wiki/File:PNG-Gradient.png" rel="noreferrer">here</a> </p>
  
  <p>In Hex Editor , it looks like this: </p>
  
  <pre><code>89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 00 00 00 80 00 00 00 44 08 02 00 00 00 
  C6 25 AA 3E 00 00 00 C2 49 44 41 54 78 5E ED D4 81 06 C3 30 14 40 D1 B7 34 DD FF FF 6F 
  B3 74 56 EA 89 12 6C 28 73 E2 AA 34 49 03 87 D6 FE D8 7B 89 BB 52 8D 3B 87 FE 01 00 80 
  00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 
  00 00 08 00 00 01 00 20 00 00 00 D4 5E 6A 64 4B 94 F5 98 7C D1 F4 92 5C 5C 3E CF 9C 3F 
  73 71 58 5F AF 8B 79 5B EE 96 B6 47 EB F1 EA D1 CE B6 E3 75 3B E6 B9 95 8D C7 CE 03 39 
  C9 AF C6 33 93 7B 66 37 CF AB BF F9 C9 2F 08 80 00 00 10 00 00 02 00 40 00 00 08 00 00 
  01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 8C 37 DB 
  68 03 20 FB ED 96 65 00 00 00 00 49 45 4E 44 AE 42 60 82
  </code></pre>
  
  <p>Equivalent characters:</p>
  
  <pre><code>‰PNG........IHDR...€...D.....Æ%ª&gt;...ÂIDATx^íÔ..Ã0.@Ñ·4Ýÿÿo³tVê‰.l(sâª4I.‡ÖþØ{‰
  »R.;‡þ..€.......@....... ....€.......@....... ...Ô^jdK”õ˜|Ñô’\\&gt;Ïœ?sqX_¯
  ‹y[î–¶GëñêÑÎ¶ãu;æ¹•.ÇÎ.9É¯Æ3“{f7Ï«¿ùÉ/.€.......@....... ....€.......@....... ..Œ7Ûh. 
  ûí–e....IEND®B`‚
  </code></pre>
  
  <p>The same is shown in following Screenshot of the HEX Editor:</p>
  
  <p><img src="https://i.stack.imgur.com/tsXHw.png" alt="enter image description here"> </p>
  
  <p>I am trying to reverse engineer this image to extract the header part and the RGB pixel values.  I read about the <a href="http://en.wikipedia.org/wiki/Portable_Network_Graphics" rel="noreferrer">PNG</a> and also <a href="http://www.w3.org/TR/PNG-Chunks.html" rel="noreferrer">here</a> , and so far I have noted the following about this Image:</p>
  
  <p>The IHDR chunk must appear FIRST. It contains:</p>
  
  <pre><code>Width:              4 bytes
  Height:             4 bytes
  Bit depth:          1 byte
  Color type:         1 byte
  Compression method: 1 byte
  Filter method:      1 byte
  Interlace method:   1 byte
  </code></pre>
  
  <p><strong>Below I am starting reading the HEX Data in sequence:</strong> </p>
  
  <p><strong>1-  First 8-bytes: This is the 8-Byte signature</strong> </p>
  
  <pre><code> 89 50 4E 47 0D 0A 1A 0A
  </code></pre>
  
  <p>Equivalently this is :  %PNG as can be seen in HEX Editor </p>
  
  <p>A valid PNG image must contain an IHDR chunk, one or more IDAT chunks, and an IEND chunk.</p>
  
  <p><strong>2-  Chunk:  Length</strong></p>
  
  <pre><code> 00 00 00 0D
  </code></pre>
  
  <p><strong>3-Chunk: Chunk Type</strong></p>
  
  <pre><code> 49 48 44 52
  </code></pre>
  
  <p>Which is IHDR.</p>
  
  <p><a href="http://www.w3.org/TR/PNG-Chunks.html" rel="noreferrer">http://www.w3.org/TR/PNG-Chunks.html</a></p>
  
  <p><strong>4-  Chunk: Width of the Image (in Decimal 128)</strong></p>
  
  <pre><code>00 00 00 80
  </code></pre>
  
  <p><strong>5- Chunk: Height of the image (in Decimal 68)</strong></p>
  
  <pre><code>00 00 00 44
  </code></pre>
  
  <p><strong>6- Chunk: BIT DEPTH (1 byte )</strong></p>
  
  <pre><code>08
  </code></pre>
  
  <p><strong>7- Chunk: Color Type</strong></p>
  
  <pre><code>02
  </code></pre>
  
  <p><strong>8- Compression method</strong></p>
  
  <pre><code>00
  </code></pre>
  
  <p><strong>9- Filter method:</strong></p>
  
  <pre><code>00
  </code></pre>
  
  <p><strong>10- Interlace method:</strong></p>
  
  <pre><code>00
  </code></pre>
  
  <p><strong><em>11-  What is the following data?</em></strong></p>
  
  <p>C6 25 AA 3E 00 00 00 C2</p>
  
  <p><strong>12-- IDAT</strong></p>
  
  <p>49 44 41 54</p>
  
  <p><strong><em>13- What is this data (after IDAT):</em></strong></p>
  
  <p>78 5E ED D4 81 06 C3 30 14 40 D1 B7 34 DD FF FF 6F B3 74 56 EA 89 12 6C 28 73 E2 AA 34 49 03 87 D6 FE D8 7B 89 BB 52 8D 3B 87 FE 01 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 00 D4 5E 6A 64 4B 94 F5 98 7C D1 F4 92 5C 5C 3E CF 9C 3F 73 71 58 5F AF 8B 79 5B EE 96 B6 47 EB F1 EA D1 CE B6 E3 75 3B E6 B9 95 8D C7 CE 03 39 C9 AF C6 33 93 7B 66 37 CF AB BF F9 C9 2F 08 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 8C 37 DB 68 03 20 FB ED 96 65 00 00 00 00</p>
  
  <p><strong>14-  IEND:</strong></p>
  
  <p>49 45 4E 44</p>
  
  <p><strong>15- Last 4 bytes</strong></p>
  
  <pre><code> AE 42 60 82
  </code></pre>
  
  <p><strong><em>What are these ?</em></strong></p>
  
  <p>Can some help me understand, points 11, 13 and  15 above?    And where are the Pixel values? The Image is having (128 x 68 pixels)  </p>
  
  <p><strong>Purpose of knowing these details:</strong></p>
  
  <p>Once I know these details, I will generate my own 16 bit PNG image. I already have pixel values, so my job would be to introduce headers etc.<br>
  I dont know if there is software that can perform this job. </p>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>I understand now because of compression, I would not be able to locate the pixel values.   </p>
  
  <p>I got the idea that I can write a file in OpenCV and save it as png. Well now my direct question is: I have a <strong>binary</strong> file having gray-scale 16 bit-pixel values.  Can I write this in OpenCV as 16 bit PNG ?     </p>