Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/3rdparty/libpng/example.c

    r2 r561  
    33
    44/* example.c - an example of using libpng
    5  * Last changed in libpng 1.2.1 December 7, 2001.
     5 * Last changed in libpng 1.2.37 [June 4, 2009]
    66 * This file has been placed in the public domain by the authors.
    7  * Maintained 1998-2007 Glenn Randers-Pehrson
     7 * Maintained 1998-2009 Glenn Randers-Pehrson
    88 * Maintained 1996, 1997 Andreas Dilger)
    99 * Written 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
     
    9292   if ((fp = fopen(file_name, "rb")) == NULL)
    9393      return (ERROR);
     94
    9495#else no_open_file /* prototype 2 */
    95 void read_png(FILE *fp, unsigned int sig_read)  /* file is already open */
     96void read_png(FILE *fp, unsigned int sig_read)  /* File is already open */
    9697{
    9798   png_structp png_ptr;
     
    99100   png_uint_32 width, height;
    100101   int bit_depth, color_type, interlace_type;
    101 #endif no_open_file /* only use one prototype! */
     102#endif no_open_file /* Only use one prototype! */
    102103
    103104   /* Create and initialize the png_struct with the desired error handler
     
    165166    */
    166167   png_read_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL);
     168
    167169#else
    168170   /* OK, you're doing it the hard way, with the lower-level functions */
     
    176178       &interlace_type, int_p_NULL, int_p_NULL);
    177179
    178 /* Set up the data transformations you want.  Note that these are all
    179  * optional.  Only call them if you want/need them.  Many of the
    180  * transformations only work on specific types of images, and many
    181  * are mutually exclusive.
    182  */
    183 
    184    /* tell libpng to strip 16 bit/color files down to 8 bits/color */
     180   /* Set up the data transformations you want.  Note that these are all
     181    * optional.  Only call them if you want/need them.  Many of the
     182    * transformations only work on specific types of images, and many
     183    * are mutually exclusive.
     184    */
     185
     186   /* Tell libpng to strip 16 bit/color files down to 8 bits/color */
    185187   png_set_strip_16(png_ptr);
    186188
     
    205207   /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
    206208   if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
    207       png_set_gray_1_2_4_to_8(png_ptr);
     209      png_set_expand_gray_1_2_4_to_8(png_ptr);
    208210
    209211   /* Expand paletted or RGB images with transparency to full alpha channels
     
    229231                         PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
    230232
    231    /* Some suggestions as to how to get a screen gamma value */
    232 
    233    /* Note that screen gamma is the display_exponent, which includes
    234     * the CRT_exponent and any correction for viewing conditions */
     233   /* Some suggestions as to how to get a screen gamma value
     234    *
     235    * Note that screen gamma is the display_exponent, which includes
     236    * the CRT_exponent and any correction for viewing conditions
     237    */
    235238   if (/* We have a user-defined screen gamma value */)
    236239   {
     
    245248   else
    246249   {
    247       screen_gamma = 2.2;  /* A good guess for a PC monitors in a dimly
     250      screen_gamma = 2.2;  /* A good guess for a PC monitor in a dimly
    248251                              lit room */
    249252      screen_gamma = 1.7 or 1.0;  /* A good guess for Mac systems */
     
    278281
    279282      /* This reduces the image to the application supplied palette */
    280       if (/* we have our own palette */)
     283      if (/* We have our own palette */)
    281284      {
    282285         /* An array of colors to which the image should be dithered */
     
    298301   }
    299302
    300    /* invert monochrome files to have 0 as white and 1 as black */
     303   /* Invert monochrome files to have 0 as white and 1 as black */
    301304   png_set_invert_mono(png_ptr);
    302305
     
    307310   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
    308311   {
    309       png_color_8p sig_bit;
    310 
    311       png_get_sBIT(png_ptr, info_ptr, &sig_bit);
    312       png_set_shift(png_ptr, sig_bit);
    313    }
    314 
    315    /* flip the RGB pixels to BGR (or RGBA to BGRA) */
     312      png_color_8p sig_bit_p;
     313
     314      png_get_sBIT(png_ptr, info_ptr, &sig_bit_p);
     315      png_set_shift(png_ptr, sig_bit_p);
     316   }
     317
     318   /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
    316319   if (color_type & PNG_COLOR_MASK_COLOR)
    317320      png_set_bgr(png_ptr);
    318321
    319    /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
     322   /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
    320323   png_set_swap_alpha(png_ptr);
    321324
    322    /* swap bytes of 16 bit files to least significant byte first */
     325   /* Swap bytes of 16 bit files to least significant byte first */
    323326   png_set_swap(png_ptr);
    324327
     
    343346   png_bytep row_pointers[height];
    344347
     348   /* Clear the pointer array */
    345349   for (row = 0; row < height; row++)
    346    {
     350      row_pointers[row] = NULL;
     351
     352   for (row = 0; row < height; row++)
    347353      row_pointers[row] = png_malloc(png_ptr, png_get_rowbytes(png_ptr,
    348354         info_ptr));
    349    }
    350355
    351356   /* Now it's time to read the image.  One of these methods is REQUIRED */
     
    373378         png_read_rows(png_ptr, png_bytepp_NULL, &row_pointers[y],
    374379            number_of_rows);
    375 #endif no_sparkle /* use only one of these two methods */
     380#endif no_sparkle /* Use only one of these two methods */
    376381      }
    377382
    378       /* if you want to display the image after every pass, do
    379          so here */
    380 #endif no_single /* use only one of these two methods */
    381    }
    382 #endif no_entire /* use only one of these two methods */
    383 
    384    /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
     383      /* If you want to display the image after every pass, do so here */
     384#endif no_single /* Use only one of these two methods */
     385   }
     386#endif no_entire /* Use only one of these two methods */
     387
     388   /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
    385389   png_read_end(png_ptr, info_ptr);
    386390#endif hilevel
     
    388392   /* At this point you have read the entire image */
    389393
    390    /* clean up after the read, and free any memory allocated - REQUIRED */
     394   /* Clean up after the read, and free any memory allocated - REQUIRED */
    391395   png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
    392396
    393    /* close the file */
     397   /* Close the file */
    394398   fclose(fp);
    395399
    396    /* that's it */
     400   /* That's it */
    397401   return (OK);
    398402}
    399403
    400 /* progressively read a file */
     404/* Progressively read a file */
    401405
    402406int
     
    463467   /* This one's new also.  Simply give it chunks of data as
    464468    * they arrive from the data stream (in order, of course).
    465     * On Segmented machines, don't give it any more than 64K.
     469    * On segmented machines, don't give it any more than 64K.
    466470    * The library seems to run fine with sizes of 4K, although
    467471    * you can give it much less if necessary (I assume you can
     
    477481info_callback(png_structp png_ptr, png_infop info)
    478482{
    479 /* do any setup here, including setting any of the transformations
    480  * mentioned in the Reading PNG files section.  For now, you _must_
    481  * call either png_start_read_image() or png_read_update_info()
    482  * after all the transformations are set (even if you don't set
    483  * any).  You may start getting rows before png_process_data()
    484  * returns, so this is your last chance to prepare for that.
    485  */
     483   /* Do any setup here, including setting any of the transformations
     484    * mentioned in the Reading PNG files section.  For now, you _must_
     485    * call either png_start_read_image() or png_read_update_info()
     486    * after all the transformations are set (even if you don't set
     487    * any).  You may start getting rows before png_process_data()
     488    * returns, so this is your last chance to prepare for that.
     489    */
    486490}
    487491
     
    489493   png_uint_32 row_num, int pass)
    490494{
    491 /*
    492  * This function is called for every row in the image.  If the
    493  * image is interlaced, and you turned on the interlace handler,
    494  * this function will be called for every row in every pass.
    495  *
    496  * In this function you will receive a pointer to new row data from
    497  * libpng called new_row that is to replace a corresponding row (of
    498  * the same data format) in a buffer allocated by your application.
    499  *
    500  * The new row data pointer new_row may be NULL, indicating there is
    501  * no new data to be replaced (in cases of interlace loading).
    502  *
    503  * If new_row is not NULL then you need to call
    504  * png_progressive_combine_row() to replace the corresponding row as
    505  * shown below:
    506  */
     495   /*
     496    * This function is called for every row in the image.  If the
     497    * image is interlaced, and you turned on the interlace handler,
     498    * this function will be called for every row in every pass.
     499    *
     500    * In this function you will receive a pointer to new row data from
     501    * libpng called new_row that is to replace a corresponding row (of
     502    * the same data format) in a buffer allocated by your application.
     503    *
     504    * The new row data pointer "new_row" may be NULL, indicating there is
     505    * no new data to be replaced (in cases of interlace loading).
     506    *
     507    * If new_row is not NULL then you need to call
     508    * png_progressive_combine_row() to replace the corresponding row as
     509    * shown below:
     510    */
     511
    507512   /* Check if row_num is in bounds. */
    508    if((row_num >= 0) && (row_num < height))
     513   if ((row_num >= 0) && (row_num < height))
    509514   {
    510515     /* Get pointer to corresponding row in our
     
    516521      * data to the corresponding row data.
    517522      */
    518      if((old_row != NULL) && (new_row != NULL))
     523     if ((old_row != NULL) && (new_row != NULL))
    519524     png_progressive_combine_row(png_ptr, old_row, new_row);
    520525   }
    521 /*
    522  * The rows and passes are called in order, so you don't really
    523  * need the row_num and pass, but I'm supplying them because it
    524  * may make your life easier.
    525  *
    526  * For the non-NULL rows of interlaced images, you must call
    527  * png_progressive_combine_row() passing in the new row and the
    528  * old row, as demonstrated above.  You can call this function for
    529  * NULL rows (it will just return) and for non-interlaced images
    530  * (it just does the png_memcpy for you) if it will make the code
    531  * easier.  Thus, you can just do this for all cases:
    532  */
     526   /*
     527    * The rows and passes are called in order, so you don't really
     528    * need the row_num and pass, but I'm supplying them because it
     529    * may make your life easier.
     530    *
     531    * For the non-NULL rows of interlaced images, you must call
     532    * png_progressive_combine_row() passing in the new row and the
     533    * old row, as demonstrated above.  You can call this function for
     534    * NULL rows (it will just return) and for non-interlaced images
     535    * (it just does the png_memcpy for you) if it will make the code
     536    * easier.  Thus, you can just do this for all cases:
     537    */
    533538
    534539   png_progressive_combine_row(png_ptr, old_row, new_row);
    535540
    536 /* where old_row is what was displayed for previous rows.  Note
    537  * that the first pass (pass == 0 really) will completely cover
    538  * the old row, so the rows do not have to be initialized.  After
    539  * the first pass (and only for interlaced images), you will have
    540  * to pass the current row as new_row, and the function will combine
    541  * the old row and the new row.
    542  */
     541   /* where old_row is what was displayed for previous rows.  Note
     542    * that the first pass (pass == 0 really) will completely cover
     543    * the old row, so the rows do not have to be initialized.  After
     544    * the first pass (and only for interlaced images), you will have
     545    * to pass the current row as new_row, and the function will combine
     546    * the old row and the new row.
     547    */
    543548}
    544549
    545550end_callback(png_structp png_ptr, png_infop info)
    546551{
    547 /* this function is called when the whole image has been read,
    548  * including any chunks after the image (up to and including
    549  * the IEND).  You will usually have the same info chunk as you
    550  * had in the header, although some data may have been added
    551  * to the comments and time fields.
    552  *
    553  * Most people won't do much here, perhaps setting a flag that
    554  * marks the image as finished.
    555  */
     552   /* This function is called when the whole image has been read,
     553    * including any chunks after the image (up to and including
     554    * the IEND).  You will usually have the same info chunk as you
     555    * had in the header, although some data may have been added
     556    * to the comments and time fields.
     557    *
     558    * Most people won't do much here, perhaps setting a flag that
     559    * marks the image as finished.
     560    */
    556561}
    557562
    558 /* write a png file */
     563/* Write a png file */
    559564void write_png(char *file_name /* , ... other image information ... */)
    560565{
     
    564569   png_colorp palette;
    565570
    566    /* open the file */
     571   /* Open the file */
    567572   fp = fopen(file_name, "wb");
    568573   if (fp == NULL)
     
    598603   if (setjmp(png_jmpbuf(png_ptr)))
    599604   {
    600       /* If we get here, we had a problem reading the file */
     605      /* If we get here, we had a problem writing the file */
    601606      fclose(fp);
    602607      png_destroy_write_struct(&png_ptr, &info_ptr);
     
    605610
    606611   /* One of the following I/O initialization functions is REQUIRED */
     612
    607613#ifdef streams /* I/O initialization method 1 */
    608    /* set up the output control if you are using standard C streams */
     614   /* Set up the output control if you are using standard C streams */
    609615   png_init_io(png_ptr, fp);
     616
    610617#else no_streams /* I/O initialization method 2 */
    611    /* If you are using replacement read functions, instead of calling
    612     * png_init_io() here you would call */
     618   /* If you are using replacement write functions, instead of calling
     619    * png_init_io() here you would call
     620    */
    613621   png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn,
    614622      user_IO_flush_function);
    615623   /* where user_io_ptr is a structure you want available to the callbacks */
    616 #endif no_streams /* only use one initialization method */
     624#endif no_streams /* Only use one initialization method */
    617625
    618626#ifdef hilevel
    619627   /* This is the easy way.  Use it if you already have all the
    620     * image info living info in the structure.  You could "|" many
     628    * image info living in the structure.  You could "|" many
    621629    * PNG_TRANSFORM flags into the png_transforms integer here.
    622630    */
    623631   png_write_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL);
     632
    624633#else
    625634   /* This is the hard way */
     
    636645      PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
    637646
    638    /* set the palette if there is one.  REQUIRED for indexed-color images */
     647   /* Set the palette if there is one.  REQUIRED for indexed-color images */
    639648   palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH
    640              * png_sizeof (png_color));
    641    /* ... set palette colors ... */
     649             * png_sizeof(png_color));
     650   /* ... Set palette colors ... */
    642651   png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
    643652   /* You must not free palette here, because png_set_PLTE only makes a link to
    644       the palette that you malloced.  Wait until you are about to destroy
    645       the png structure. */
    646 
    647    /* optional significant bit chunk */
    648    /* if we are dealing with a grayscale image then */
     653    * the palette that you malloced.  Wait until you are about to destroy
     654    * the png structure.
     655    */
     656
     657   /* Optional significant bit (sBIT) chunk */
     658   png_color_8 sig_bit;
     659   /* If we are dealing with a grayscale image then */
    649660   sig_bit.gray = true_bit_depth;
    650    /* otherwise, if we are dealing with a color image then */
     661   /* Otherwise, if we are dealing with a color image then */
    651662   sig_bit.red = true_red_bit_depth;
    652663   sig_bit.green = true_green_bit_depth;
    653664   sig_bit.blue = true_blue_bit_depth;
    654    /* if the image has an alpha channel then */
     665   /* If the image has an alpha channel then */
    655666   sig_bit.alpha = true_alpha_bit_depth;
    656    png_set_sBIT(png_ptr, info_ptr, sig_bit);
     667   png_set_sBIT(png_ptr, info_ptr, &sig_bit);
    657668
    658669
     
    679690   png_set_text(png_ptr, info_ptr, text_ptr, 3);
    680691
    681    /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */
    682    /* note that if sRGB is present the gAMA and cHRM chunks must be ignored
    683     * on read and must be written in accordance with the sRGB profile */
     692   /* Other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs */
     693
     694   /* Note that if sRGB is present the gAMA and cHRM chunks must be ignored
     695    * on read and, if your application chooses to write them, they must
     696    * be written in accordance with the sRGB profile
     697    */
    684698
    685699   /* Write the file header information.  REQUIRED */
     
    693707    *   png_write_info(png_ptr, info_ptr);
    694708    *
    695     * However, given the level of known- and unknown-chunk support in 1.1.0
     709    * However, given the level of known- and unknown-chunk support in 1.2.0
    696710    * and up, this should no longer be necessary.
    697711    */
     
    703717    */
    704718
    705    /* set up the transformations you want.  Note that these are
     719   /* Set up the transformations you want.  Note that these are
    706720    * all optional.  Only call them if you want them.
    707721    */
    708722
    709    /* invert monochrome pixels */
     723   /* Invert monochrome pixels */
    710724   png_set_invert_mono(png_ptr);
    711725
     
    715729   png_set_shift(png_ptr, &sig_bit);
    716730
    717    /* pack pixels into bytes */
     731   /* Pack pixels into bytes */
    718732   png_set_packing(png_ptr);
    719733
    720    /* swap location of alpha bytes from ARGB to RGBA */
     734   /* Swap location of alpha bytes from ARGB to RGBA */
    721735   png_set_swap_alpha(png_ptr);
    722736
     
    726740   png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
    727741
    728    /* flip BGR pixels to RGB */
     742   /* Flip BGR pixels to RGB */
    729743   png_set_bgr(png_ptr);
    730744
    731    /* swap bytes of 16-bit files to most significant byte first */
     745   /* Swap bytes of 16-bit files to most significant byte first */
    732746   png_set_swap(png_ptr);
    733747
    734    /* swap bits of 1, 2, 4 bit packed pixel formats */
     748   /* Swap bits of 1, 2, 4 bit packed pixel formats */
    735749   png_set_packswap(png_ptr);
    736750
    737    /* turn on interlace handling if you are not using png_write_image() */
     751   /* Turn on interlace handling if you are not using png_write_image() */
    738752   if (interlacing)
    739753      number_passes = png_set_interlace_handling(png_ptr);
     
    756770
    757771   /* One of the following output methods is REQUIRED */
    758 #ifdef entire /* write out the entire image data in one call */
     772
     773#ifdef entire /* Write out the entire image data in one call */
    759774   png_write_image(png_ptr, row_pointers);
    760775
    761    /* the other way to write the image - deal with interlacing */
    762 
    763 #else no_entire /* write out the image data by one or more scanlines */
     776   /* The other way to write the image - deal with interlacing */
     777
     778#else no_entire /* Write out the image data by one or more scanlines */
     779
    764780   /* The number of passes is either 1 for non-interlaced images,
    765781    * or 7 for interlaced images.
     
    772788      /* If you are only writing one row at a time, this works */
    773789      for (y = 0; y < height; y++)
    774       {
    775790         png_write_rows(png_ptr, &row_pointers[y], 1);
    776       }
    777    }
    778 #endif no_entire /* use only one output method */
     791   }
     792#endif no_entire /* Use only one output method */
    779793
    780794   /* You can write optional chunks like tEXt, zTXt, and tIME at the end
    781     * as well.  Shouldn't be necessary in 1.1.0 and up as all the public
     795    * as well.  Shouldn't be necessary in 1.2.0 and up as all the public
    782796    * chunks are supported and you can use png_set_unknown_chunks() to
    783797    * register unknown chunks into the info structure to be written out.
     
    789803
    790804   /* If you png_malloced a palette, free it here (don't free info_ptr->palette,
    791       as recommended in versions 1.0.5m and earlier of this example; if
    792       libpng mallocs info_ptr->palette, libpng will free it).  If you
    793       allocated it with malloc() instead of png_malloc(), use free() instead
    794       of png_free(). */
     805    * as recommended in versions 1.0.5m and earlier of this example; if
     806    * libpng mallocs info_ptr->palette, libpng will free it).  If you
     807    * allocated it with malloc() instead of png_malloc(), use free() instead
     808    * of png_free().
     809    */
    795810   png_free(png_ptr, palette);
    796    palette=NULL;
     811   palette = NULL;
    797812
    798813   /* Similarly, if you png_malloced any data that you passed in with
    799       png_set_something(), such as a hist or trans array, free it here,
    800       when you can be sure that libpng is through with it. */
     814    * png_set_something(), such as a hist or trans array, free it here,
     815    * when you can be sure that libpng is through with it.
     816    */
    801817   png_free(png_ptr, trans);
    802    trans=NULL;
    803 
    804    /* clean up after the write, and free any memory allocated */
     818   trans = NULL;
     819   /* Whenever you use png_free() it is a good idea to set the pointer to
     820    * NULL in case your application inadvertently tries to png_free() it
     821    * again.  When png_free() sees a NULL it returns without action, thus
     822    * avoiding the double-free security problem.
     823    */
     824
     825   /* Clean up after the write, and free any memory allocated */
    805826   png_destroy_write_struct(&png_ptr, &info_ptr);
    806827
    807    /* close the file */
     828   /* Close the file */
    808829   fclose(fp);
    809830
    810    /* that's it */
     831   /* That's it */
    811832   return (OK);
    812833}
Note: See TracChangeset for help on using the changeset viewer.