source: svn/newcon3bcm2_21bu/dst/dlib/src/PNG/example.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

  • Property svn:executable set to *
File size: 25.5 KB
Line 
1/*
2 * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/PNG/lpng102/example.c#1 $
3 * $Revision: #1 $
4 * $DateTime: 2006/02/24 17:51:46 $
5 * $Change: 42566 $
6 * $Author: pryush.sharma $
7 */
8
9
10/* example.c - an example of using libpng */
11
12/* This is an example of how to use libpng to read and write PNG files.
13 * The file libpng.txt is much more verbose then this.  If you have not
14 * read it, do so first.  This was designed to be a starting point of an
15 * implementation.  This is not officially part of libpng, and therefore
16 * does not require a copyright notice.
17 *
18 * This file does not currently compile, because it is missing certain
19 * parts, like allocating memory to hold an image.  You will have to
20 * supply these parts to get it to compile.  For an example of a minimal
21 * working PNG reader/writer, see pngtest.c, included in this distribution.
22 */
23
24#include "png.h"
25
26/* Check to see if a file is a PNG file using png_sig_cmp().  Returns
27 * non-zero if the image is a PNG, and 0 if it isn't a PNG.
28 *
29 * If this call is successful, and you are going to keep the file open,
30 * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once
31 * you have created the png_ptr, so that libpng knows your application
32 * has read that many bytes from the start of the file.  Make sure you
33 * don't call png_set_sig_bytes() with more than 8 bytes read or give it
34 * an incorrect number of bytes read, or you will either have read too
35 * many bytes (your fault), or you are telling libpng to read the wrong
36 * number of magic bytes (also your fault).
37 *
38 * Many applications already read the first 2 or 4 bytes from the start
39 * of the image to determine the file type, so it would be easiest just
40 * to pass the bytes to png_sig_cmp() or even skip that if you know
41 * you have a PNG file, and call png_set_sig_bytes().
42 */
43#define PNG_BYTES_TO_CHECK 4
44int check_if_png(char *file_name, FILE **fp)
45{
46   char buf[PNG_BYTES_TO_CHECK];
47
48   /* Open the prospective PNG file. */
49   if ((*fp = fopen(file_name, "rb")) != NULL)
50      return 0;
51
52   /* Read in the signature bytes */
53   if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK)
54      return 0;
55
56   /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */
57   return(png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
58}
59
60/* Read a PNG file.  You may want to return an error code if the read
61 * fails (depending upon the failure).  There are two "prototypes" given
62 * here - one where we are given the filename, and we need to open the
63 * file, and the other where we are given an open file (possibly with
64 * some or all of the magic bytes read - see comments above).
65 */
66#ifdef open_file /* prototype 1 */
67void read_png(char *file_name)  /* We need to open the file */
68{
69   png_structp png_ptr;
70   png_infop info_ptr;
71   unsigned int sig_read = 0;
72   png_uint_32 width, height;
73   int bit_depth, color_type, interlace_type;
74   FILE *fp;
75
76   if ((fp = fopen(file_name, "rb")) == NULL)
77      return;
78#else /* no_open_file prototype 2 */
79void read_png(FILE *fp, unsigned int sig_read)  /* file is already open */
80{
81   png_structp png_ptr;
82   png_infop info_ptr;
83   png_uint_32 width, height;
84   int bit_depth, color_type, interlace_type;
85#endif /* no_open_file. only use one prototype! */
86
87  png_color_16 my_background, *image_background;
88
89   /* Create and initialize the png_struct with the desired error handler
90    * functions.  If you want to use the default stderr and longjump method,
91    * you can supply NULL for the last three parameters.  We also supply the
92    * the compiler header file version, so that we know if the application
93    * was compiled with a compatible version of the library.  REQUIRED
94    */
95   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
96      (png_voidp) user_error_ptr, user_error_fn, user_warning_fn);
97
98   if (png_ptr == NULL)
99   {
100      fclose(fp);
101      return;
102   }
103
104   /* Allocate/initialize the memory for image information.  REQUIRED. */
105   info_ptr = png_create_info_struct(png_ptr);
106   if (info_ptr == NULL)
107   {
108      fclose(fp);
109      png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
110      return;
111   }
112
113   /* Set error handling if you are using the setjmp/longjmp method (this is
114    * the normal method of doing things with libpng).  REQUIRED unless you
115    * set up your own error handlers in the png_create_read_struct() earlier.
116    */
117   if (setjmp(png_ptr->jmpbuf))
118   {
119      /* Free all of the memory associated with the png_ptr and info_ptr */
120      png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
121      fclose(fp);
122      /* If we get here, we had a problem reading the file */
123      return;
124   }
125
126   /* One of the following I/O initialization methods is REQUIRED */
127#ifdef streams /* PNG file I/O method 1 */
128   /* Set up the input control if you are using standard C streams */
129   png_init_io(png_ptr, fp);
130
131#else /* no_streams PNG file I/O method 2 */
132   /* If you are using replacement read functions, instead of calling
133    * png_init_io() here you would call:
134    */
135   png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn);
136   /* where user_io_ptr is a structure you want available to the callbacks */
137#endif /* no_streams  Use only one I/O method! */
138
139   /* If we have already read some of the signature */
140   png_set_sig_bytes(png_ptr, sig_read);
141
142   /* The call to png_read_info() gives us all of the information from the
143    * PNG file before the first IDAT (image data chunk).  REQUIRED
144    */
145   png_read_info(png_ptr, info_ptr);
146
147   png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
148       &interlace_type, NULL, NULL);
149
150/**** Set up the data transformations you want.  Note that these are all
151 **** optional.  Only call them if you want/need them.  Many of the
152 **** transformations only work on specific types of images, and many
153 **** are mutually exclusive.
154 ****/
155
156   /* tell libpng to strip 16 bit/color files down to 8 bits/color */
157   png_set_strip_16(png_ptr);
158
159   /* Strip alpha bytes from the input data without combining with th
160    * background (not recommended).
161    */
162   png_set_strip_alpha(png_ptr);
163
164   /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
165    * byte into separate bytes (useful for paletted and grayscale images).
166    */
167   png_set_packing(png_ptr);
168
169   /* Change the order of packed pixels to least significant bit first
170    * (not useful if you are using png_set_packing). */
171   png_set_packswap(png_ptr);
172
173   /* Expand paletted colors into true RGB triplets */
174   if (color_type == PNG_COLOR_TYPE_PALETTE)
175      png_set_expand(png_ptr);
176
177   /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
178   if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
179      png_set_expand(png_ptr);
180
181   /* Expand paletted or RGB images with transparency to full alpha channels
182    * so the data will be available as RGBA quartets.
183    */
184   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
185      png_set_expand(png_ptr);
186
187   /* Set the background color to draw transparent and alpha images over.
188    * It is possible to set the red, green, and blue components directly
189    * for paletted images instead of supplying a palette index.  Note that
190    * even if the PNG file supplies a background, you are not required to
191    * use it - you should use the (solid) application background if it has one.
192    */
193
194   if (png_get_bKGD(png_ptr, info_ptr, &image_background))
195      png_set_background(png_ptr, image_background,
196                         PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
197   else
198      png_set_background(png_ptr, &my_background,
199                         PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
200
201   /* Some suggestions as to how to get a screen gamma value */
202
203   /* Note that screen gamma is (display_gamma/viewing_gamma) */
204   if (/* We have a user-defined screen gamma value */)
205   {
206      screen_gamma = user-defined screen_gamma;
207   }
208   /* This is one way that applications share the same screen gamma value */
209   else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL)
210   {
211      screen_gamma = atof(gamma_str);
212   }
213   /* If we don't have another value */
214   else
215   {
216      screen_gamma = 2.2;  /* A good guess for a PC monitors in a dimly
217                              lit room */
218      screen_gamma = 1.7 or 1.0;  /* A good guess for Mac systems */
219   }
220
221   /* Tell libpng to handle the gamma conversion for you.  The second call
222    * is a good guess for PC generated images, but it should be configurable
223    * by the user at run time by the user.  It is strongly suggested that
224    * your application support gamma correction.
225    */
226
227   int intent;
228
229   if (png_get_sRGB(png_ptr, info_ptr, &intent))
230      png_set_sRGB(png_ptr, intent, 0);
231   else 
232      if (png_get_gAMA(png_ptr, info_ptr, &image_gamma))
233         png_set_gamma(png_ptr, screen_gamma, image_gamma);
234      else
235         png_set_gamma(png_ptr, screen_gamma, 0.50);
236
237   /* Dither RGB files down to 8 bit palette or reduce palettes
238    * to the number of colors available on your screen.
239    */
240   if (color_type & PNG_COLOR_MASK_COLOR)
241   {
242      png_uint_32 num_palette;
243      png_colorp palette;
244
245      /* This reduces the image to the application supplied palette */
246      if (/* we have our own palette */)
247      {
248         /* An array of colors to which the image should be dithered */
249         png_color std_color_cube[MAX_SCREEN_COLORS];
250
251         png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
252            MAX_SCREEN_COLORS, NULL, 0);
253      }
254      /* This reduces the image to the palette supplied in the file */
255      else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette))
256      {
257         png_color16p histogram;
258
259         png_get_hIST(png_ptr, info_ptr, &histogram);
260
261         png_set_dither(png_ptr, palette, num_palette,
262                        max_screen_colors, histogram, 0);
263      }
264   }
265
266   /* invert monocrome files to have 0 as white and 1 as black */
267   png_set_invert_mono(png_ptr);
268
269   /* If you want to shift the pixel values from the range [0,255] or
270    * [0,65535] to the original [0,7] or [0,31], or whatever range the
271    * colors were originally in:
272    */
273   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
274   {
275      png_color8p sig_bit;
276
277      png_get_sBIT(png_ptr, info_ptr, &sig_bit);
278      png_set_shift(png_ptr, sig_bit);
279   }
280
281   /* flip the RGB pixels to BGR (or RGBA to BGRA) */
282   png_set_bgr(png_ptr);
283
284   /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
285   png_set_swap_alpha(png_ptr);
286
287   /* swap bytes of 16 bit files to least significant byte first */
288   png_set_swap(png_ptr);
289
290   /* Add filler (or alpha) byte (before/after each RGB triplet) */
291   png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
292
293   /* Turn on interlace handling.  REQUIRED if you are not using
294    * png_read_image().  To see how to handle interlacing passes,
295    * see the png_read_row() method below:
296    */
297   number_passes = png_set_interlace_handling(png_ptr);
298
299   /* Optional call to gamma correct and add the background to the palette
300    * and update info structure.  REQUIRED if you are expecting libpng to
301    * update the palette for you (ie you selected such a transform above).
302    */
303   png_read_update_info(png_ptr, info_ptr);
304
305   /* Allocate the memory to hold the image using the fields of info_ptr. */
306
307   /* The easiest way to read the image: */
308   png_bytep row_pointers[height];
309
310   for (row = 0; row < height; row++)
311   {
312      row_pointers[row] = malloc(png_get_rowbytes(png_ptr, info_ptr));
313   }
314
315   /* Now it's time to read the image.  One of these methods is REQUIRED */
316#ifdef entire /* Read the entire image in one go */
317   png_read_image(png_ptr, row_pointers);
318
319#else no_entire /* Read the image one or more scanlines at a time */
320   /* The other way to read images - deal with interlacing: */
321
322   for (pass = 0; pass < number_passes; pass++)
323   {
324#ifdef single /* Read the image a single row at a time */
325      for (y = 0; y < height; y++)
326      {
327         png_read_rows(png_ptr, &row_pointers[y], NULL, 1);
328      }
329
330#else no_single /* Read the image several rows at a time */
331      for (y = 0; y < height; y += number_of_rows)
332      {
333#ifdef sparkle /* Read the image using the "sparkle" effect. */
334         png_read_rows(png_ptr, &row_pointers[y], NULL, number_of_rows);
335       
336         png_read_rows(png_ptr, NULL, row_pointers[y], number_of_rows);
337#else no_sparkle /* Read the image using the "rectangle" effect */
338         png_read_rows(png_ptr, NULL, &row_pointers[y], number_of_rows);
339#endif /* no_sparkle - use only one of these two methods */
340      }
341     
342      /* if you want to display the image after every pass, do
343         so here */
344#endif /* no_single - use only one of these two methods */
345   }
346#endif /* no_entire - use only one of these two methods */
347
348   /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
349   png_read_end(png_ptr, info_ptr);
350
351   /* clean up after the read, and free any memory allocated - REQUIRED */
352   png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
353
354   /* close the file */
355   fclose(fp);
356
357   /* that's it */
358   return;
359}
360
361/* progressively read a file */
362
363int
364initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr)
365{
366   /* Create and initialize the png_struct with the desired error handler
367    * functions.  If you want to use the default stderr and longjump method,
368    * you can supply NULL for the last three parameters.  We also check that
369    * the library version is compatible in case we are using dynamically
370    * linked libraries.
371    */
372   *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
373       png_voidp user_error_ptr, user_error_fn, user_warning_fn);
374
375   if (*png_ptr == NULL)
376   {
377      *info_ptr = NULL;
378      return ERROR;
379   }
380
381   *info_ptr = png_create_info_struct(png_ptr);
382
383   if (*info_ptr == NULL)
384   {
385      png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
386      return ERROR;
387   }
388
389   if (setjmp((*png_ptr)->jmpbuf))
390   {
391      png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
392      return ERROR;
393   }
394
395   /* this one's new.  You will need to provide all three
396    * function callbacks, even if you aren't using them all.
397    * These functions shouldn't be dependent on global or
398    * static variables if you are decoding several images
399    * simultaneously.  You should store stream specific data
400    * in a separate struct, given as the second parameter,
401    * and retrieve the pointer from inside the callbacks using
402    * the function png_get_progressive_ptr(png_ptr).
403    */
404   png_set_progressive_read_fn(*png_ptr, (void *)stream_data,
405      info_callback, row_callback, end_callback);
406
407   return OK;
408}
409
410int
411process_data(png_structp *png_ptr, png_infop *info_ptr,
412   png_bytep buffer, png_uint_32 length)
413{
414   if (setjmp((*png_ptr)->jmpbuf))
415   {
416      /* Free the png_ptr and info_ptr memory on error */
417      png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL);
418      return ERROR;
419   }
420
421   /* This one's new also.  Simply give it chunks of data as
422    * they arrive from the data stream (in order, of course).
423    * On Segmented machines, don't give it any more than 64K.
424    * The library seems to run fine with sizes of 4K, although
425    * you can give it much less if necessary (I assume you can
426    * give it chunks of 1 byte, but I haven't tried with less
427    * than 256 bytes yet).  When this function returns, you may
428    * want to display any rows that were generated in the row
429    * callback, if you aren't already displaying them there.
430    */
431   png_process_data(*png_ptr, *info_ptr, buffer, length);
432   return OK;
433}
434
435info_callback(png_structp png_ptr, png_infop info)
436{
437/* do any setup here, including setting any of the transformations
438 * mentioned in the Reading PNG files section.  For now, you _must_
439 * call either png_start_read_image() or png_read_update_info()
440 * after all the transformations are set (even if you don't set
441 * any).  You may start getting rows before png_process_data()
442 * returns, so this is your last chance to prepare for that.
443 */
444}
445
446row_callback(png_structp png_ptr, png_bytep new_row,
447   png_uint_32 row_num, int pass)
448{
449/* this function is called for every row in the image.  If the
450 * image is interlacing, and you turned on the interlace handler,
451 * this function will be called for every row in every pass.
452 * Some of these rows will not be changed from the previous pass.
453 * When the row is not changed, the new_row variable will be NULL.
454 * The rows and passes are called in order, so you don't really
455 * need the row_num and pass, but I'm supplying them because it
456 * may make your life easier.
457 *
458 * For the non-NULL rows of interlaced images, you must call
459 * png_progressive_combine_row() passing in the row and the
460 * old row.  You can call this function for NULL rows (it will
461 * just return) and for non-interlaced images (it just does the
462 * memcpy for you) if it will make the code easier.  Thus, you
463 * can just do this for all cases:
464 */
465
466   png_progressive_combine_row(png_ptr, old_row, new_row);
467
468/* where old_row is what was displayed for previous rows.  Note
469 * that the first pass (pass == 0 really) will completely cover
470 * the old row, so the rows do not have to be initialized.  After
471 * the first pass (and only for interlaced images), you will have
472 * to pass the current row, and the function will combine the
473 * old row and the new row.
474 */
475}
476
477end_callback(png_structp png_ptr, png_infop info)
478{
479/* this function is called when the whole image has been read,
480 * including any chunks after the image (up to and including
481 * the IEND).  You will usually have the same info chunk as you
482 * had in the header, although some data may have been added
483 * to the comments and time fields.
484 *
485 * Most people won't do much here, perhaps setting a flag that
486 * marks the image as finished.
487 */
488}
489
490/* write a png file */
491void write_png(char *file_name /* , ... other image information ... */)
492{
493   FILE *fp;
494   png_structp png_ptr;
495   png_infop info_ptr;
496
497   /* open the file */
498   fp = fopen(file_name, "wb");
499   if (fp == NULL)
500      return;
501
502   /* Create and initialize the png_struct with the desired error handler
503    * functions.  If you want to use the default stderr and longjump method,
504    * you can supply NULL for the last three parameters.  We also check that
505    * the library version is compatible with the one used at compile time,
506    * in case we are using dynamically linked libraries.  REQUIRED.
507    */
508   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
509      png_voidp user_error_ptr, user_error_fn, user_warning_fn);
510
511   if (png_ptr == NULL)
512   {
513      fclose(fp);
514      return;
515   }
516
517   /* Allocate/initialize the image information data.  REQUIRED */
518   info_ptr = png_create_info_struct(png_ptr);
519   if (info_ptr == NULL)
520   {
521      fclose(fp);
522      png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
523      return;
524   }
525
526   /* Set error handling.  REQUIRED if you aren't supplying your own
527    * error hadnling functions in the png_create_write_struct() call.
528    */
529   if (setjmp(png_ptr->jmpbuf))
530   {
531      /* If we get here, we had a problem reading the file */
532      fclose(fp);
533      png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
534      return;
535   }
536
537   /* One of the following I/O initialization functions is REQUIRED */
538#ifdef streams /* I/O initialization method 1 */
539   /* set up the output control if you are using standard C streams */
540   png_init_io(png_ptr, fp);
541#else no_streams /* I/O initialization method 2 */
542   /* If you are using replacement read functions, instead of calling
543    * png_init_io() here you would call */
544   png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn,
545      user_IO_flush_function);
546   /* where user_io_ptr is a structure you want available to the callbacks */
547#endif /* no_streams - only use one initialization method */
548
549   /* Set the image information here.  Width and height are up to 2^31,
550    * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
551    * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
552    * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
553    * or PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
554    * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
555    * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
556    */
557   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???,
558      PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
559
560   /* set the palette if there is one.  REQUIRED for indexed-color images */
561   palette = (png_colorp)png_malloc(png_ptr, 256 * sizeof (png_color));
562   /* ... set palette colors ... */
563   png_set_PLTE(png_ptr, info_ptr, palette, 256);
564
565   /* optional significant bit chunk */
566   /* if we are dealing with a grayscale image then */
567   sig_bit.gray = true_bit_depth;
568   /* otherwise, if we are dealing with a color image then */
569   sig_bit.red = true_red_bit_depth;
570   sig_bit.green = true_green_bit_depth;
571   sig_bit.blue = true_blue_bit_depth;
572   /* if the image has an alpha channel then */
573   sig_bit.alpha = true_alpha_bit_depth;
574   png_set_sBIT(png_ptr, info_ptr, sig_bit);
575
576 
577   /* Optional gamma chunk is strongly suggested if you have any guess
578    * as to the correct gamma of the image.
579    */
580   png_set_gAMA(png_ptr, info_ptr, gamma);
581
582   /* Optionally write comments into the image */
583   text_ptr[0].key = "Title";
584   text_ptr[0].text = "Mona Lisa";
585   text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
586   text_ptr[1].key = "Author";
587   text_ptr[1].text = "Leonardo DaVinci";
588   text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE;
589   text_ptr[2].key = "Description";
590   text_ptr[2].text = "<long text>";
591   text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt;
592   png_set_text(png_ptr, info_ptr, text_ptr, 3);
593
594   /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */
595   /* note that if sRGB is present the cHRM chunk must be ignored
596    * on read and must be written in accordance with the sRGB profile */
597
598   /* Write the file header information.  REQUIRED */
599   png_write_info(png_ptr, info_ptr);
600
601   /* Once we write out the header, the compression type on the text
602    * chunks gets changed to PNG_TEXT_COMPRESSION_NONE_WR or
603    * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again
604    * at the end.
605    */
606
607   /* set up the transformations you want.  Note that these are
608    * all optional.  Only call them if you want them.
609    */
610
611   /* invert monocrome pixels */
612   png_set_invert_mono(png_ptr);
613
614   /* Shift the pixels up to a legal bit depth and fill in
615    * as appropriate to correctly scale the image.
616    */
617   png_set_shift(png_ptr, &sig_bit);
618
619   /* pack pixels into bytes */
620   png_set_packing(png_ptr);
621
622   /* swap location of alpha bytes from ARGB to RGBA */
623   png_set_swap_alpha(png_ptr);
624
625   /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
626    * RGB (4 channels -> 3 channels). The second parameter is not used.
627    */
628   png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
629
630   /* flip BGR pixels to RGB */
631   png_set_bgr(png_ptr);
632
633   /* swap bytes of 16-bit files to most significant byte first */
634   png_set_swap(png_ptr);
635
636   /* swap bits of 1, 2, 4 bit packed pixel formats */
637   png_set_packswap(png_ptr);
638
639   /* turn on interlace handling if you are not using png_write_image() */
640   if (interlacing)
641      number_passes = png_set_interlace_handling(png_ptr);
642   else
643      number_passes = 1;
644
645   /* The easiest way to write the image (you may have a different memory
646    * layout, however, so choose what fits your needs best).  You need to
647    * use the first method if you aren't handling interlacing yourself.
648    */
649   png_uint_32 k, height, width;
650   png_byte image[height][width];
651   png_bytep row_pointers[height];
652   for (k = 0; k < height; k++)
653     row_pointers[k] = image + k*width;
654
655   /* One of the following output methods is REQUIRED */
656#ifdef entire /* write out the entire image data in one call */
657   png_write_image(png_ptr, row_pointers);
658
659   /* the other way to write the image - deal with interlacing */
660
661#else no_entire /* write out the image data by one or more scanlines */
662   /* The number of passes is either 1 for non-interlaced images,
663    * or 7 for interlaced images.
664    */
665   for (pass = 0; pass < number_passes; pass++)
666   {
667      /* Write a few rows at a time. */
668      png_write_rows(png_ptr, &row_pointers[first_row], number_of_rows);
669
670      /* If you are only writing one row at a time, this works */
671      for (y = 0; y < height; y++)
672      {
673         png_write_rows(png_ptr, &row_pointers[y], 1);
674      }
675   }
676#endif /* no_entire - use only one output method */
677
678   /* You can write optional chunks like tEXt, zTXt, and tIME at the end
679    * as well.
680    */
681
682   /* It is REQUIRED to call this to finish writing the rest of the file */
683   png_write_end(png_ptr, info_ptr);
684
685   /* if you malloced the palette, free it here */
686   free(info_ptr->palette);
687
688   /* if you allocated any text comments, free them here */
689
690   /* clean up after the write, and free any memory allocated */
691   png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
692
693   /* close the file */
694   fclose(fp);
695
696   /* that's it */
697   return;
698}
699
Note: See TracBrowser for help on using the repository browser.