Home | History | Annotate | Download | only in libtests
      1 /* Fake a PNG - just write it out directly.
      2  *
      3  * COPYRIGHT: Written by John Cunningham Bowler, 2014.
      4  * To the extent possible under law, the author has waived all copyright and
      5  * related or neighboring rights to this work.  This work is published from:
      6  * United States.
      7  *
      8  */
      9 
     10 #include <stdio.h>
     11 #include <zlib.h> /* for crc32 */
     12 
     13 void
     14 put_uLong(uLong val)
     15 {
     16    putchar(val >> 24);
     17    putchar(val >> 16);
     18    putchar(val >>  8);
     19    putchar(val >>  0);
     20 }
     21 
     22 void
     23 put_chunk(const unsigned char *chunk, uInt length)
     24 {
     25    uLong crc;
     26 
     27    put_uLong(length-4); /* Exclude the tag */
     28 
     29    fwrite(chunk, length, 1, stdout);
     30 
     31    crc = crc32(0, Z_NULL, 0);
     32    put_uLong(crc32(crc, chunk, length));
     33 }
     34 
     35 const unsigned char signature[] =
     36 {
     37    137, 80, 78, 71, 13, 10, 26, 10
     38 };
     39 
     40 const unsigned char IHDR[] =
     41 {
     42    73, 72, 68, 82, /* IHDR */
     43    0, 0, 0, 1, /* width */
     44    0, 0, 0, 1, /* height */
     45    1, /* bit depth */
     46    0, /* color type: greyscale */
     47    0, /* compression method */
     48    0, /* filter method */
     49    0  /* interlace method: none */
     50 };
     51 
     52 const unsigned char unknown[] =
     53 {
     54    'u', 'n', 'K', 'n' /* "unKn" - private safe to copy */
     55 };
     56 
     57 int
     58 main(void)
     59 {
     60    fwrite(signature, sizeof signature, 1, stdout);
     61    put_chunk(IHDR, sizeof IHDR);
     62 
     63    for (;;)
     64       put_chunk(unknown, sizeof unknown);
     65 }
     66