example.c 11.5 KB
Newer Older
G
Guy Schalnat 已提交
1 2 3 4 5 6 7 8
/* example.c - an example of using libpng */

/* this is an example of how to use libpng to read and write
   png files.  The file libpng.txt is much more verbose then
   this.  If you have not read it, do so first.  This was
   designed to be a starting point of an implementation.
   This is not officially part of libpng, and therefore
   does not require a copyright notice.
G
Guy Schalnat 已提交
9 10 11 12

   This file does not currently compile, because it is missing
   certain parts, like allocating memory to hold an image.
   You will have to supply these parts to get it to compile.
G
Guy Schalnat 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
   */

#include <png.h>

/* check to see if a file is a png file using png_check_sig() */
int check_png(char *file_name)
{
   FILE *fp;
   char buf[8];
   int ret;

   fp = fopen(file_name, "rb");
   if (!fp)
      return 0;
   ret = fread(buf, 1, 8, fp);
   fclose(fp);

   if (ret != 8)
      return 0;

   ret = png_check_sig(buf, 8);

   return (ret);
}

/* read a png file.  You may want to return an error code if the read
   fails (depending upon the failure). */
void read_png(char *file_name)
{
   FILE *fp;
   png_struct *png_ptr;
   png_info *info_ptr;

   /* open the file */
   fp = fopen(file_name, "rb");
   if (!fp)
      return;

   /* allocate the necessary structures */
   png_ptr = malloc(sizeof (png_struct));
   if (!png_ptr)
   {
      fclose(fp);
      return;
   }

   info_ptr = malloc(sizeof (png_info));
   if (!info_ptr)
   {
      fclose(fp);
      free(png_ptr);
      return;
   }

   /* set error handling */
   if (setjmp(png_ptr->jmpbuf))
   {
      png_read_destroy(png_ptr, info_ptr, (png_info *)0);
      fclose(fp);
      free(png_ptr);
      free(info_ptr);
      /* If we get here, we had a problem reading the file */
      return;
   }

   /* initialize the structures, info first for error handling */
   png_info_init(info_ptr);
   png_read_init(png_ptr);

G
Guy Schalnat 已提交
82 83 84
   /* set up the input control for the default input and message functions.
    * If we were to replace both the input and message functions we don't
    * need to call png_init_io first. */
G
Guy Schalnat 已提交
85 86
   png_init_io(png_ptr, fp);

G
Guy Schalnat 已提交
87 88 89 90 91 92 93 94
   /* if you are using replacement read functions, here you would call */
   io_ptr = (user_io_struct *)malloc(sizeof(user_io_struct));
   png_set_read_fn(png_ptr, (void *)io_ptr, user_read_fn);

   /* if you are using replacement message functions, here you would call */
   msg_ptr = (user_msg_struct *)malloc(sizeof(user_msg_struct));
   png_set_read_fn(png_ptr, (void *)msg_ptr, user_error_fn, user_warning_fn);

G
Guy Schalnat 已提交
95 96 97 98 99 100 101
   /* read the file information */
   png_read_info(png_ptr, info_ptr);

   /* set up the transformations you want.  Note that these are
      all optional.  Only call them if you want them */

   /* expand paletted colors into true rgb */
G
Guy Schalnat 已提交
102
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      png_set_expand(png_ptr);

   /* expand grayscale images to the full 8 bits */
   if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
      info_ptr->bit_depth < 8)
      png_set_expand(png_ptr);

   /* expand images with transparency to full alpha channels */
   if (info_ptr->valid & PNG_INFO_tRNS)
      png_set_expand(png_ptr);

   /* Set the background color to draw transparent and alpha
      images over */
   png_color_16 my_background;

   if (info_ptr->valid & PNG_INFO_bKGD)
      png_set_background(png_ptr, &(info_ptr->background),
         PNG_GAMMA_FILE, 1, 1.0);
   else
      png_set_background(png_ptr, &my_background,
         PNG_GAMMA_SCREEN, 0, 1.0);

   /* tell libpng to handle the gamma conversion for you */
   if (info_ptr->valid & PNG_INFO_gAMA)
      png_set_gamma(png_ptr, screen_gamma, info_ptr->gamma);
   else
      png_set_gamma(png_ptr, screen_gamma, 0.45);

   /* tell libpng to strip 16 bit depth files down to 8 bits */
   if (info_ptr->bit_depth == 16)
      png_set_strip_16(png_ptr);

   /* dither rgb files down to 8 bit palettes & reduce palettes
      to the number of colors available on your screen */
   if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
   {
      if (info_ptr->valid & PNG_INFO_PLTE)
         png_set_dither(png_ptr, info_ptr->palette,
            info_ptr->num_palette, max_screen_colors,
               info_ptr->histogram);
      else
      {
         png_color std_color_cube[MAX_SCREEN_COLORS] =
            {/* ... colors ... */};

         png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
            MAX_SCREEN_COLORS, NULL);
      }
   }

   /* invert monocrome files */
   if (info_ptr->bit_depth == 1 &&
      info_ptr->color_type == PNG_COLOR_GRAY)
      png_set_invert(png_ptr);

   /* shift the pixels down to their true bit depth */
G
Guy Schalnat 已提交
159
   if (info_ptr->valid & PNG_INFO_sBIT)
G
Guy Schalnat 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      png_set_shift(png_ptr, &(info_ptr->sig_bit));

   /* pack pixels into bytes */
   if (info_ptr->bit_depth < 8)
      png_set_packing(png_ptr);

   /* flip the rgb pixels to bgr */
   if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
      info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
      png_set_bgr(png_ptr);

   /* swap bytes of 16 bit files to least significant bit first */
   if (info_ptr->bit_depth == 16)
      png_set_swap(png_ptr);

G
Guy Schalnat 已提交
175
   /* add a filler byte to rgb files */
G
Guy Schalnat 已提交
176 177
   if (info_ptr->bit_depth == 8 &&
      info_ptr->color_type == PNG_COLOR_TYPE_RGB)
G
Guy Schalnat 已提交
178 179 180 181 182 183 184
      png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);

   /* turn on interlace handling if you are not using png_read_image() */
   if (info_ptr->interlace_type)
      number_passes = png_set_interlace_handling(png_ptr);
   else
      number_passes = 1;
G
Guy Schalnat 已提交
185 186 187 188

   /* optional call to update palette with transformations */
   png_start_read_image(png_ptr);

G
Guy Schalnat 已提交
189 190 191 192 193 194
   /* optional call to update the info structure */
   png_read_update_info(png_ptr, info_ptr);

   /* allocate the memory to hold the image using the fields
      of png_info. */

G
Guy Schalnat 已提交
195
   /* the easiest way to read the image */
G
Guy Schalnat 已提交
196
   png_bytef *row_pointers[height];
G
Guy Schalnat 已提交
197 198 199 200 201 202 203 204 205 206 207 208
   png_read_image(png_ptr, row_pointers);

   /* the other way to read images - deal with interlacing */

   for (pass = 0; pass < number_passes; pass++)
   {
      /* Read the image using the "sparkle" effect. */
      png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);

      /* If you are only reading on row at a time, this works */
      for (y = 0; y < height; y++)
      {
G
Guy Schalnat 已提交
209
         png_bytef *row_pointers = row[y];
G
Guy Schalnat 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223
         png_read_rows(png_ptr, &row_pointers, NULL, 1);
      }

      /* to get the rectangle effect, use the third parameter */
      png_read_rows(png_ptr, NULL, row_pointers, number_of_rows);

      /* if you want to display the image after every pass, do
         so here */
   }

   /* read the rest of the file, getting any additional chunks
      in info_ptr */
   png_read_end(png_ptr, info_ptr);

G
Guy Schalnat 已提交
224 225 226 227 228
   /* if you had allocated any memory structures for custom input or
      messaging routines you need to free them before png_read_destroy */
   free(png_get_io_ptr(png_ptr));
   free(png_get_msg_ptr(png_ptr));

G
Guy Schalnat 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
   /* clean up after the read, and free any memory allocated */
   png_read_destroy(png_ptr, info_ptr, (png_info *)0);

   /* free the structures */
   free(png_ptr);
   free(info_ptr);

   /* close the file */
   fclose(fp);

   /* that's it */
   return;
}

/* write a png file */
void write_png(char *file_name, ... other image information ...)
{
   FILE *fp;
   png_struct *png_ptr;
   png_info *info_ptr;

   /* open the file */
   fp = fopen(file_name, "wb");
   if (!fp)
      return;

   /* allocate the necessary structures */
   png_ptr = malloc(sizeof (png_struct));
   if (!png_ptr)
   {
      fclose(fp);
      return;
   }

   info_ptr = malloc(sizeof (png_info));
   if (!info_ptr)
   {
      fclose(fp);
      free(png_ptr);
      return;
   }

   /* set error handling */
   if (setjmp(png_ptr->jmpbuf))
   {
      png_write_destroy(png_ptr);
      fclose(fp);
      free(png_ptr);
      free(info_ptr);
      /* If we get here, we had a problem reading the file */
      return;
   }

   /* initialize the structures */
   png_info_init(info_ptr);
   png_write_init(png_ptr);

G
Guy Schalnat 已提交
286 287 288
   /* set up the output control for the default output and message functions.
    * If we were to replace both the output and message functions we don't
    * need to call png_init_io first. */
G
Guy Schalnat 已提交
289 290
   png_init_io(png_ptr, fp);

G
Guy Schalnat 已提交
291 292 293 294 295 296 297 298
   /* if you are using replacement write functions, here you would call */
   io_ptr = (user_io_struct *)malloc(sizeof(user_io_struct));
   png_set_write_fn(png_ptr, (void *)io_ptr, user_write_fn, user_flush_fn);

   /* if you are using replacement message functions, here you would call */
   msg_ptr = (user_msg_struct *)malloc(sizeof(user_msg_struct));
   png_set_read_fn(png_ptr, (void *)msg_ptr, user_error_fn, user_warning_fn);

G
Guy Schalnat 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311
   /* set the file information here */
   info_ptr->width = ;
   info_ptr->height = ;
   etc.

   /* set the palette if there is one */
   info_ptr->valid |= PNG_INFO_PLTE;
   info_ptr->palette = malloc(256 * sizeof (png_color));
   info_ptr->num_palette = 256;
   ... set palette colors ...

   /* optional significant bit chunk */
   info_ptr->valid |= PNG_INFO_sBIT;
G
Guy Schalnat 已提交
312 313 314 315 316 317 318 319 320 321
   /* if we are dealing with a grayscale image then */
   info_ptr->sig_bit.gray = true_bit_depth;
   /* otherwise, if we are dealing with a color image then */
   info_ptr->sig_bit.red = true_red_bit_depth;
   info_ptr->sig_bit.green = true_green_bit_depth;
   info_ptr->sig_bit.blue = true_blue_bit_depth;
   /* if the image has an alpha channel then */
   info_ptr->sig_bit.alpha = true_alpha_bit_depth;

   /* optional gamma chunk is a good idea if you can write one */
G
Guy Schalnat 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
   info_ptr->valid |= PNG_INFO_gAMA;
   info_ptr->gamma = gamma;

   /* other optional chunks */

   /* write the file information */
   png_write_info(png_ptr, info_ptr);

   /* set up the transformations you want.  Note that these are
      all optional.  Only call them if you want them */

   /* invert monocrome pixels */
   png_set_invert(png_ptr);

   /* shift the pixels up to a legal bit depth and fill in
      as appropriate to correctly scale the image */
   png_set_shift(png_ptr, &(info_ptr->sig_bit));

   /* pack pixels into bytes */
   png_set_packing(png_ptr);

   /* flip bgr pixels to rgb */
   png_set_bgr(png_ptr);

   /* swap bytes of 16 bit files to most significant bit first */
   png_set_swap(png_ptr);

G
Guy Schalnat 已提交
349 350 351
   /* get rid of filler bytes, pack rgb into 3 bytes.  The
      filler number is not used. */
   png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
G
Guy Schalnat 已提交
352

G
Guy Schalnat 已提交
353
   /* turn on interlace handling if you are not using png_write_image() */
G
Guy Schalnat 已提交
354 355 356 357 358
   if (interlacing)
      number_passes = png_set_interlace_handling(png_ptr);
   else
      number_passes = 1;

G
Guy Schalnat 已提交
359 360 361 362 363 364
   /* the easiest way to write the image */
   png_bytef *row_pointers[height];
   png_write_image(png_ptr, row_pointers);

   /* the other way to write the image - deal with interlacing */

G
Guy Schalnat 已提交
365 366 367 368 369 370 371 372
   for (pass = 0; pass < number_passes; pass++)
   {
      /* Write a few rows at a time. */
      png_write_rows(png_ptr, row_pointers, number_of_rows);

      /* If you are only writing one row at a time, this works */
      for (y = 0; y < height; y++)
      {
G
Guy Schalnat 已提交
373
         png_bytef *row_pointers = row[y];
G
Guy Schalnat 已提交
374 375 376 377 378 379 380
         png_write_rows(png_ptr, &row_pointers, 1);
      }
   }

   /* write the rest of the file */
   png_write_end(png_ptr, info_ptr);

G
Guy Schalnat 已提交
381 382 383 384 385
   /* if you had allocated any memory structures for custom output or
      messaging routines you need to free them before png_write_destroy */
   free(png_get_io_ptr(png_ptr));
   free(png_get_msg_ptr(png_ptr));

G
Guy Schalnat 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
   /* clean up after the write, and free any memory allocated */
   png_write_destroy(png_ptr);

   /* if you malloced the palette, free it here */
   if (info_ptr->palette)
      free(info_ptr->palette);

   /* free the structures */
   free(png_ptr);
   free(info_ptr);

   /* close the file */
   fclose(fp);

   /* that's it */
   return;
}