Blame contrib/examples/pngpixel.c

Packit a6cca5
/*- pngpixel
Packit a6cca5
 *
Packit a6cca5
 * COPYRIGHT: Written by John Cunningham Bowler, 2011.
Packit a6cca5
 * To the extent possible under law, the author has waived all copyright and
Packit a6cca5
 * related or neighboring rights to this work.  This work is published from:
Packit a6cca5
 * United States.
Packit a6cca5
 *
Packit a6cca5
 * Read a single pixel value from a PNG file.
Packit a6cca5
 *
Packit a6cca5
 * This code illustrates basic 'by-row' reading of a PNG file using libpng.
Packit a6cca5
 * Rows are read until a particular pixel is found; the value of this pixel is
Packit a6cca5
 * then printed on stdout.
Packit a6cca5
 *
Packit a6cca5
 * The code illustrates how to do this on interlaced as well as non-interlaced
Packit a6cca5
 * images.  Normally you would call png_set_interlace_handling() to have libpng
Packit a6cca5
 * deal with the interlace for you, but that obliges you to buffer half of the
Packit a6cca5
 * image to assemble the interlaced rows.  In this code
Packit a6cca5
 * png_set_interlace_handling() is not called and, instead, the code handles the
Packit a6cca5
 * interlace passes directly looking for the required pixel.
Packit a6cca5
 */
Packit a6cca5
#include <stdlib.h>
Packit a6cca5
#include <stdio.h>
Packit a6cca5
#include <setjmp.h> /* required for error handling */
Packit a6cca5
Packit a6cca5
/* Normally use <png.h> here to get the installed libpng, but this is done to
Packit a6cca5
 * ensure the code picks up the local libpng implementation:
Packit a6cca5
 */
Packit a6cca5
#include "../../png.h"
Packit a6cca5
Packit a6cca5
#if defined(PNG_READ_SUPPORTED) && defined(PNG_SEQUENTIAL_READ_SUPPORTED)
Packit a6cca5
Packit a6cca5
/* Return component 'c' of pixel 'x' from the given row. */
Packit a6cca5
static unsigned int
Packit a6cca5
component(png_const_bytep row, png_uint_32 x, unsigned int c,
Packit a6cca5
   unsigned int bit_depth, unsigned int channels)
Packit a6cca5
{
Packit a6cca5
   /* PNG images can be up to 2^31 pixels wide, but this means they can be up to
Packit a6cca5
    * 2^37 bits wide (for a 64-bit pixel - the largest possible) and hence 2^34
Packit a6cca5
    * bytes wide.  Since the row fitted into memory, however, the following must
Packit a6cca5
    * work:
Packit a6cca5
    */
Packit a6cca5
   png_uint_32 bit_offset_hi = bit_depth * ((x >> 6) * channels);
Packit a6cca5
   png_uint_32 bit_offset_lo = bit_depth * ((x & 0x3f) * channels + c);
Packit a6cca5
Packit a6cca5
   row = (png_const_bytep)(((PNG_CONST png_byte (*)[8])row) + bit_offset_hi);
Packit a6cca5
   row += bit_offset_lo >> 3;
Packit a6cca5
   bit_offset_lo &= 0x07;
Packit a6cca5
Packit a6cca5
   /* PNG pixels are packed into bytes to put the first pixel in the highest
Packit a6cca5
    * bits of the byte and into two bytes for 16-bit values with the high 8 bits
Packit a6cca5
    * first, so:
Packit a6cca5
    */
Packit a6cca5
   switch (bit_depth)
Packit a6cca5
   {
Packit a6cca5
      case 1: return (row[0] >> (7-bit_offset_lo)) & 0x01;
Packit a6cca5
      case 2: return (row[0] >> (6-bit_offset_lo)) & 0x03;
Packit a6cca5
      case 4: return (row[0] >> (4-bit_offset_lo)) & 0x0f;
Packit a6cca5
      case 8: return row[0];
Packit a6cca5
      case 16: return (row[0] << 8) + row[1];
Packit a6cca5
      default:
Packit a6cca5
         /* This should never happen; it indicates a bug in this program or in
Packit a6cca5
          * libpng itself:
Packit a6cca5
          */
Packit a6cca5
         fprintf(stderr, "pngpixel: invalid bit depth %u\n", bit_depth);
Packit a6cca5
         exit(1);
Packit a6cca5
   }
Packit a6cca5
}
Packit a6cca5
Packit a6cca5
/* Print a pixel from a row returned by libpng; determine the row format, find
Packit a6cca5
 * the pixel, and print the relevant information to stdout.
Packit a6cca5
 */
Packit a6cca5
static void
Packit a6cca5
print_pixel(png_structp png_ptr, png_infop info_ptr, png_const_bytep row,
Packit a6cca5
   png_uint_32 x)
Packit a6cca5
{
Packit a6cca5
   PNG_CONST unsigned int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
Packit a6cca5
Packit a6cca5
   switch (png_get_color_type(png_ptr, info_ptr))
Packit a6cca5
   {
Packit a6cca5
      case PNG_COLOR_TYPE_GRAY:
Packit a6cca5
         printf("GRAY %u\n", component(row, x, 0, bit_depth, 1));
Packit a6cca5
         return;
Packit a6cca5
Packit a6cca5
      /* The palette case is slightly more difficult - the palette and, if
Packit a6cca5
       * present, the tRNS ('transparency', though the values are really
Packit a6cca5
       * opacity) data must be read to give the full picture:
Packit a6cca5
       */
Packit a6cca5
      case PNG_COLOR_TYPE_PALETTE:
Packit a6cca5
         {
Packit a6cca5
            PNG_CONST int index = component(row, x, 0, bit_depth, 1);
Packit a6cca5
            png_colorp palette = NULL;
Packit a6cca5
            int num_palette = 0;
Packit a6cca5
Packit a6cca5
            if ((png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette) &
Packit a6cca5
               PNG_INFO_PLTE) && num_palette > 0 && palette != NULL)
Packit a6cca5
            {
Packit a6cca5
               png_bytep trans_alpha = NULL;
Packit a6cca5
               int num_trans = 0;
Packit a6cca5
               if ((png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans,
Packit a6cca5
                  NULL) & PNG_INFO_tRNS) && num_trans > 0 &&
Packit a6cca5
                  trans_alpha != NULL)
Packit a6cca5
                  printf("INDEXED %u = %d %d %d %d\n", index,
Packit a6cca5
                     palette[index].red, palette[index].green,
Packit a6cca5
                     palette[index].blue,
Packit a6cca5
                     index < num_trans ? trans_alpha[index] : 255);
Packit a6cca5
Packit a6cca5
               else /* no transparency */
Packit a6cca5
                  printf("INDEXED %u = %d %d %d\n", index,
Packit a6cca5
                     palette[index].red, palette[index].green,
Packit a6cca5
                     palette[index].blue);
Packit a6cca5
            }
Packit a6cca5
Packit a6cca5
            else
Packit a6cca5
               printf("INDEXED %u = invalid index\n", index);
Packit a6cca5
         }
Packit a6cca5
         return;
Packit a6cca5
Packit a6cca5
      case PNG_COLOR_TYPE_RGB:
Packit a6cca5
         printf("RGB %u %u %u\n", component(row, x, 0, bit_depth, 3),
Packit a6cca5
            component(row, x, 1, bit_depth, 3),
Packit a6cca5
            component(row, x, 2, bit_depth, 3));
Packit a6cca5
         return;
Packit a6cca5
Packit a6cca5
      case PNG_COLOR_TYPE_GRAY_ALPHA:
Packit a6cca5
         printf("GRAY+ALPHA %u %u\n", component(row, x, 0, bit_depth, 2),
Packit a6cca5
            component(row, x, 1, bit_depth, 2));
Packit a6cca5
         return;
Packit a6cca5
Packit a6cca5
      case PNG_COLOR_TYPE_RGB_ALPHA:
Packit a6cca5
         printf("RGBA %u %u %u %u\n", component(row, x, 0, bit_depth, 4),
Packit a6cca5
            component(row, x, 1, bit_depth, 4),
Packit a6cca5
            component(row, x, 2, bit_depth, 4),
Packit a6cca5
            component(row, x, 3, bit_depth, 4));
Packit a6cca5
         return;
Packit a6cca5
Packit a6cca5
      default:
Packit a6cca5
         png_error(png_ptr, "pngpixel: invalid color type");
Packit a6cca5
   }
Packit a6cca5
}
Packit a6cca5
Packit a6cca5
int main(int argc, const char **argv)
Packit a6cca5
{
Packit a6cca5
   /* This program uses the default, <setjmp.h> based, libpng error handling
Packit a6cca5
    * mechanism, therefore any local variable that exists before the call to
Packit a6cca5
    * setjmp and is changed after the call to setjmp returns successfully must
Packit a6cca5
    * be declared with 'volatile' to ensure that their values don't get
Packit a6cca5
    * destroyed by longjmp:
Packit a6cca5
    */
Packit a6cca5
   volatile int result = 1/*fail*/;
Packit a6cca5
Packit a6cca5
   if (argc == 4)
Packit a6cca5
   {
Packit a6cca5
      long x = atol(argv[1]);
Packit a6cca5
      long y = atol(argv[2]);
Packit a6cca5
      FILE *f = fopen(argv[3], "rb");
Packit a6cca5
      volatile png_bytep row = NULL;
Packit a6cca5
Packit a6cca5
      if (f != NULL)
Packit a6cca5
      {
Packit a6cca5
         /* libpng requires a callback function for handling errors; this
Packit a6cca5
          * callback must not return.  The default callback function uses a
Packit a6cca5
          * stored <setjmp.h> style jmp_buf which is held in a png_struct and
Packit a6cca5
          * writes error messages to stderr.  Creating the png_struct is a
Packit a6cca5
          * little tricky; just copy the following code.
Packit a6cca5
          */
Packit a6cca5
         png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
Packit a6cca5
            NULL, NULL, NULL);
Packit a6cca5
Packit a6cca5
         if (png_ptr != NULL)
Packit a6cca5
         {
Packit a6cca5
            png_infop info_ptr = png_create_info_struct(png_ptr);
Packit a6cca5
Packit a6cca5
            if (info_ptr != NULL)
Packit a6cca5
            {
Packit a6cca5
               /* Declare stack variables to hold pointers to locally allocated
Packit a6cca5
                * data.
Packit a6cca5
                */
Packit a6cca5
Packit a6cca5
               /* Initialize the error control buffer: */
Packit a6cca5
               if (setjmp(png_jmpbuf(png_ptr)) == 0)
Packit a6cca5
               {
Packit a6cca5
                  png_uint_32 width, height;
Packit a6cca5
                  int bit_depth, color_type, interlace_method,
Packit a6cca5
                     compression_method, filter_method;
Packit a6cca5
                  png_bytep row_tmp;
Packit a6cca5
Packit a6cca5
                  /* Now associate the recently opened (FILE*) with the default
Packit a6cca5
                   * libpng initialization functions.  Sometimes libpng is
Packit a6cca5
                   * compiled without stdio support (it can be difficult to do
Packit a6cca5
                   * in some environments); in that case you will have to write
Packit a6cca5
                   * your own read callback to read data from the (FILE*).
Packit a6cca5
                   */
Packit a6cca5
                  png_init_io(png_ptr, f);
Packit a6cca5
Packit a6cca5
                  /* And read the first part of the PNG file - the header and
Packit a6cca5
                   * all the information up to the first pixel.
Packit a6cca5
                   */
Packit a6cca5
                  png_read_info(png_ptr, info_ptr);
Packit a6cca5
Packit a6cca5
                  /* This fills in enough information to tell us the width of
Packit a6cca5
                   * each row in bytes, allocate the appropriate amount of
Packit a6cca5
                   * space.  In this case png_malloc is used - it will not
Packit a6cca5
                   * return if memory isn't available.
Packit a6cca5
                   */
Packit a6cca5
                  row = png_malloc(png_ptr, png_get_rowbytes(png_ptr,
Packit a6cca5
                     info_ptr));
Packit a6cca5
Packit a6cca5
                  /* To avoid the overhead of using a volatile auto copy row_tmp
Packit a6cca5
                   * to a local here - just use row for the png_free below.
Packit a6cca5
                   */
Packit a6cca5
                  row_tmp = row;
Packit a6cca5
Packit a6cca5
                  /* All the information we need is in the header is returned by
Packit a6cca5
                   * png_get_IHDR, if this fails we can now use 'png_error' to
Packit a6cca5
                   * signal the error and return control to the setjmp above.
Packit a6cca5
                   */
Packit a6cca5
                  if (png_get_IHDR(png_ptr, info_ptr, &width, &height,
Packit a6cca5
                     &bit_depth, &color_type, &interlace_method,
Packit a6cca5
                     &compression_method, &filter_method))
Packit a6cca5
                  {
Packit a6cca5
                     int passes, pass;
Packit a6cca5
Packit a6cca5
                     /* png_set_interlace_handling returns the number of
Packit a6cca5
                      * passes required as well as turning on libpng's
Packit a6cca5
                      * handling, but since we do it ourselves this is
Packit a6cca5
                      * necessary:
Packit a6cca5
                      */
Packit a6cca5
                     switch (interlace_method)
Packit a6cca5
                     {
Packit a6cca5
                        case PNG_INTERLACE_NONE:
Packit a6cca5
                           passes = 1;
Packit a6cca5
                           break;
Packit a6cca5
Packit a6cca5
                        case PNG_INTERLACE_ADAM7:
Packit a6cca5
                           passes = PNG_INTERLACE_ADAM7_PASSES;
Packit a6cca5
                           break;
Packit a6cca5
Packit a6cca5
                        default:
Packit a6cca5
                           png_error(png_ptr, "pngpixel: unknown interlace");
Packit a6cca5
                     }
Packit a6cca5
Packit a6cca5
                     /* Now read the pixels, pass-by-pass, row-by-row: */
Packit a6cca5
                     png_start_read_image(png_ptr);
Packit a6cca5
Packit a6cca5
                     for (pass=0; pass
Packit a6cca5
                     {
Packit a6cca5
                        png_uint_32 ystart, xstart, ystep, xstep;
Packit a6cca5
                        png_uint_32 py;
Packit a6cca5
Packit a6cca5
                        if (interlace_method == PNG_INTERLACE_ADAM7)
Packit a6cca5
                        {
Packit a6cca5
                           /* Sometimes the whole pass is empty because the
Packit a6cca5
                            * image is too narrow or too short.  libpng
Packit a6cca5
                            * expects to be called for each row that is
Packit a6cca5
                            * present in the pass, so it may be necessary to
Packit a6cca5
                            * skip the loop below (over py) if the image is
Packit a6cca5
                            * too narrow.
Packit a6cca5
                            */
Packit a6cca5
                           if (PNG_PASS_COLS(width, pass) == 0)
Packit a6cca5
                              continue;
Packit a6cca5
Packit a6cca5
                           /* We need the starting pixel and the offset
Packit a6cca5
                            * between each pixel in this pass; use the macros
Packit a6cca5
                            * in png.h:
Packit a6cca5
                            */
Packit a6cca5
                           xstart = PNG_PASS_START_COL(pass);
Packit a6cca5
                           ystart = PNG_PASS_START_ROW(pass);
Packit a6cca5
                           xstep = PNG_PASS_COL_OFFSET(pass);
Packit a6cca5
                           ystep = PNG_PASS_ROW_OFFSET(pass);
Packit a6cca5
                        }
Packit a6cca5
Packit a6cca5
                        else
Packit a6cca5
                        {
Packit a6cca5
                           ystart = xstart = 0;
Packit a6cca5
                           ystep = xstep = 1;
Packit a6cca5
                        }
Packit a6cca5
Packit a6cca5
                        /* To find the pixel, loop over 'py' for each pass
Packit a6cca5
                         * reading a row and then checking to see if it
Packit a6cca5
                         * contains the pixel.
Packit a6cca5
                         */
Packit a6cca5
                        for (py = ystart; py < height; py += ystep)
Packit a6cca5
                        {
Packit a6cca5
                           png_uint_32 px, ppx;
Packit a6cca5
Packit a6cca5
                           /* png_read_row takes two pointers.  When libpng
Packit a6cca5
                            * handles the interlace the first is filled in
Packit a6cca5
                            * pixel-by-pixel, and the second receives the same
Packit a6cca5
                            * pixels but they are replicated across the
Packit a6cca5
                            * unwritten pixels so far for each pass.  When we
Packit a6cca5
                            * do the interlace, however, they just contain
Packit a6cca5
                            * the pixels from the interlace pass - giving
Packit a6cca5
                            * both is wasteful and pointless, so we pass a
Packit a6cca5
                            * NULL pointer.
Packit a6cca5
                            */
Packit a6cca5
                           png_read_row(png_ptr, row_tmp, NULL);
Packit a6cca5
Packit a6cca5
                           /* Now find the pixel if it is in this row; there
Packit a6cca5
                            * are, of course, much better ways of doing this
Packit a6cca5
                            * than using a for loop:
Packit a6cca5
                            */
Packit a6cca5
                           if (y == py) for (px = xstart, ppx = 0;
Packit a6cca5
                              px < width; px += xstep, ++ppx) if (x == px)
Packit a6cca5
                           {
Packit a6cca5
                              /* 'ppx' is the index of the pixel in the row
Packit a6cca5
                               * buffer.
Packit a6cca5
                               */
Packit a6cca5
                              print_pixel(png_ptr, info_ptr, row_tmp, ppx);
Packit a6cca5
Packit a6cca5
                              /* Now terminate the loops early - we have
Packit a6cca5
                               * found and handled the required data.
Packit a6cca5
                               */
Packit a6cca5
                              goto pass_loop_end;
Packit a6cca5
                           } /* x loop */
Packit a6cca5
                        } /* y loop */
Packit a6cca5
                     } /* pass loop */
Packit a6cca5
Packit a6cca5
                     /* Finally free the temporary buffer: */
Packit a6cca5
                  pass_loop_end:
Packit a6cca5
                     row = NULL;
Packit a6cca5
                     png_free(png_ptr, row_tmp);
Packit a6cca5
                  }
Packit a6cca5
Packit a6cca5
                  else
Packit a6cca5
                     png_error(png_ptr, "pngpixel: png_get_IHDR failed");
Packit a6cca5
Packit a6cca5
               }
Packit a6cca5
Packit a6cca5
               else
Packit a6cca5
               {
Packit a6cca5
                  /* Else libpng has raised an error.  An error message has
Packit a6cca5
                   * already been output, so it is only necessary to clean up
Packit a6cca5
                   * locally allocated data:
Packit a6cca5
                   */
Packit a6cca5
                  if (row != NULL)
Packit a6cca5
                  {
Packit a6cca5
                     /* The default implementation of png_free never errors out
Packit a6cca5
                      * (it just crashes if something goes wrong), but the safe
Packit a6cca5
                      * way of using it is still to clear 'row' before calling
Packit a6cca5
                      * png_free:
Packit a6cca5
                      */
Packit a6cca5
                     png_bytep row_tmp = row;
Packit a6cca5
                     row = NULL;
Packit a6cca5
                     png_free(png_ptr, row_tmp);
Packit a6cca5
                  }
Packit a6cca5
               }
Packit a6cca5
Packit a6cca5
               png_destroy_info_struct(png_ptr, &info_ptr);
Packit a6cca5
            }
Packit a6cca5
Packit a6cca5
            else
Packit a6cca5
               fprintf(stderr, "pngpixel: out of memory allocating png_info\n");
Packit a6cca5
Packit a6cca5
            png_destroy_read_struct(&png_ptr, NULL, NULL);
Packit a6cca5
         }
Packit a6cca5
Packit a6cca5
         else
Packit a6cca5
            fprintf(stderr, "pngpixel: out of memory allocating png_struct\n");
Packit a6cca5
      }
Packit a6cca5
Packit a6cca5
      else
Packit a6cca5
         fprintf(stderr, "pngpixel: %s: could not open file\n", argv[3]);
Packit a6cca5
   }
Packit a6cca5
Packit a6cca5
   else
Packit a6cca5
      /* Wrong number of arguments */
Packit a6cca5
      fprintf(stderr, "pngpixel: usage: pngpixel x y png-file\n");
Packit a6cca5
Packit a6cca5
   return result;
Packit a6cca5
}
Packit a6cca5
#endif /* READ && SEQUENTIAL_READ */