readpng.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*---------------------------------------------------------------------------
  2. rpng - simple PNG display program readpng.c
  3. ---------------------------------------------------------------------------
  4. Copyright (c) 1998-2007 Greg Roelofs. All rights reserved.
  5. This software is provided "as is," without warranty of any kind,
  6. express or implied. In no event shall the author or contributors
  7. be held liable for any damages arising in any way from the use of
  8. this software.
  9. The contents of this file are DUAL-LICENSED. You may modify and/or
  10. redistribute this software according to the terms of one of the
  11. following two licenses (at your option):
  12. LICENSE 1 ("BSD-like with advertising clause"):
  13. Permission is granted to anyone to use this software for any purpose,
  14. including commercial applications, and to alter it and redistribute
  15. it freely, subject to the following restrictions:
  16. 1. Redistributions of source code must retain the above copyright
  17. notice, disclaimer, and this list of conditions.
  18. 2. Redistributions in binary form must reproduce the above copyright
  19. notice, disclaimer, and this list of conditions in the documenta-
  20. tion and/or other materials provided with the distribution.
  21. 3. All advertising materials mentioning features or use of this
  22. software must display the following acknowledgment:
  23. This product includes software developed by Greg Roelofs
  24. and contributors for the book, "PNG: The Definitive Guide,"
  25. published by O'Reilly and Associates.
  26. LICENSE 2 (GNU GPL v2 or later):
  27. This program is free software; you can redistribute it and/or modify
  28. it under the terms of the GNU General Public License as published by
  29. the Free Software Foundation; either version 2 of the License, or
  30. (at your option) any later version.
  31. This program is distributed in the hope that it will be useful,
  32. but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. GNU General Public License for more details.
  35. You should have received a copy of the GNU General Public License
  36. along with this program; if not, write to the Free Software Foundation,
  37. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  38. ---------------------------------------------------------------------------*/
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <zlib.h>
  42. #include "png.h" /* libpng header */
  43. #include "readpng.h" /* typedefs, common macros, public prototypes */
  44. /* future versions of libpng will provide this macro: */
  45. #ifndef png_jmpbuf
  46. # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
  47. #endif
  48. static png_structp png_ptr = NULL;
  49. static png_infop info_ptr = NULL;
  50. png_uint_32 width, height;
  51. int bit_depth, color_type;
  52. uch *image_data = NULL;
  53. void readpng_version_info(void)
  54. {
  55. fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n",
  56. PNG_LIBPNG_VER_STRING, png_libpng_ver);
  57. fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n",
  58. ZLIB_VERSION, zlib_version);
  59. }
  60. /* return value = 0 for success, 1 for bad sig, 2 for bad IHDR, 4 for no mem */
  61. int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
  62. {
  63. uch sig[8];
  64. /* first do a quick check that the file really is a PNG image; could
  65. * have used slightly more general png_sig_cmp() function instead */
  66. fread(sig, 1, 8, infile);
  67. if (png_sig_cmp(sig, 0, 8))
  68. return 1; /* bad signature */
  69. /* could pass pointers to user-defined error handlers instead of NULLs: */
  70. png_ptr = png_create_read_struct(png_get_libpng_ver(NULL), NULL, NULL,
  71. NULL);
  72. if (!png_ptr)
  73. return 4; /* out of memory */
  74. info_ptr = png_create_info_struct(png_ptr);
  75. if (!info_ptr) {
  76. png_destroy_read_struct(&png_ptr, NULL, NULL);
  77. return 4; /* out of memory */
  78. }
  79. /* we could create a second info struct here (end_info), but it's only
  80. * useful if we want to keep pre- and post-IDAT chunk info separated
  81. * (mainly for PNG-aware image editors and converters) */
  82. /* setjmp() must be called in every function that calls a PNG-reading
  83. * libpng function */
  84. if (setjmp(png_jmpbuf(png_ptr))) {
  85. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  86. return 2;
  87. }
  88. png_init_io(png_ptr, infile);
  89. png_set_sig_bytes(png_ptr, 8); /* we already read the 8 signature bytes */
  90. png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */
  91. /* alternatively, could make separate calls to png_get_image_width(),
  92. * etc., but want bit_depth and color_type for later [don't care about
  93. * compression_type and filter_type => NULLs] */
  94. png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
  95. NULL, NULL, NULL);
  96. *pWidth = width;
  97. *pHeight = height;
  98. /* OK, that's all we need for now; return happy */
  99. return 0;
  100. }
  101. /* returns 0 if succeeds, 1 if fails due to no bKGD chunk, 2 if libpng error;
  102. * scales values to 8-bit if necessary */
  103. int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
  104. {
  105. png_color_16p pBackground;
  106. /* setjmp() must be called in every function that calls a PNG-reading
  107. * libpng function */
  108. if (setjmp(png_jmpbuf(png_ptr))) {
  109. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  110. return 2;
  111. }
  112. if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD))
  113. return 1;
  114. /* it is not obvious from the libpng documentation, but this function
  115. * takes a pointer to a pointer, and it always returns valid red, green
  116. * and blue values, regardless of color_type: */
  117. png_get_bKGD(png_ptr, info_ptr, &pBackground);
  118. /* however, it always returns the raw bKGD data, regardless of any
  119. * bit-depth transformations, so check depth and adjust if necessary */
  120. if (bit_depth == 16) {
  121. *red = pBackground->red >> 8;
  122. *green = pBackground->green >> 8;
  123. *blue = pBackground->blue >> 8;
  124. } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
  125. if (bit_depth == 1)
  126. *red = *green = *blue = pBackground->gray? 255 : 0;
  127. else if (bit_depth == 2)
  128. *red = *green = *blue = (255/3) * pBackground->gray;
  129. else /* bit_depth == 4 */
  130. *red = *green = *blue = (255/15) * pBackground->gray;
  131. } else {
  132. *red = (uch)pBackground->red;
  133. *green = (uch)pBackground->green;
  134. *blue = (uch)pBackground->blue;
  135. }
  136. return 0;
  137. }
  138. /* display_exponent == LUT_exponent * CRT_exponent */
  139. uch *readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
  140. {
  141. double gamma;
  142. png_uint_32 i, rowbytes;
  143. png_bytepp row_pointers = NULL;
  144. /* setjmp() must be called in every function that calls a PNG-reading
  145. * libpng function */
  146. if (setjmp(png_jmpbuf(png_ptr))) {
  147. free(image_data);
  148. image_data = NULL;
  149. free(row_pointers);
  150. row_pointers = NULL;
  151. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  152. return NULL;
  153. }
  154. /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
  155. * transparency chunks to full alpha channel; strip 16-bit-per-sample
  156. * images to 8 bits per sample; and convert grayscale to RGB[A] */
  157. if (color_type == PNG_COLOR_TYPE_PALETTE)
  158. png_set_expand(png_ptr);
  159. if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
  160. png_set_expand(png_ptr);
  161. if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
  162. png_set_expand(png_ptr);
  163. #ifdef PNG_READ_16_TO_8_SUPPORTED
  164. if (bit_depth == 16)
  165. # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  166. png_set_scale_16(png_ptr);
  167. # else
  168. png_set_strip_16(png_ptr);
  169. # endif
  170. #endif
  171. if (color_type == PNG_COLOR_TYPE_GRAY ||
  172. color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  173. png_set_gray_to_rgb(png_ptr);
  174. /* unlike the example in the libpng documentation, we have *no* idea where
  175. * this file may have come from--so if it doesn't have a file gamma, don't
  176. * do any correction ("do no harm") */
  177. if (png_get_gAMA(png_ptr, info_ptr, &gamma))
  178. png_set_gamma(png_ptr, display_exponent, gamma);
  179. /* all transformations have been registered; now update info_ptr data,
  180. * get rowbytes and channels, and allocate image memory */
  181. png_read_update_info(png_ptr, info_ptr);
  182. *pRowbytes = rowbytes = png_get_rowbytes(png_ptr, info_ptr);
  183. *pChannels = (int)png_get_channels(png_ptr, info_ptr);
  184. if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
  185. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  186. return NULL;
  187. }
  188. if ((row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep))) == NULL) {
  189. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  190. free(image_data);
  191. image_data = NULL;
  192. return NULL;
  193. }
  194. Trace((stderr, "readpng_get_image: channels = %d, rowbytes = %ld, height = %ld\n",
  195. *pChannels, rowbytes, height));
  196. /* set the individual row_pointers to point at the correct offsets */
  197. for (i = 0; i < height; ++i)
  198. row_pointers[i] = image_data + i*rowbytes;
  199. /* now we can go ahead and just read the whole image */
  200. png_read_image(png_ptr, row_pointers);
  201. /* and we're done! (png_read_end() can be omitted if no processing of
  202. * post-IDAT text/time/etc. is desired) */
  203. free(row_pointers);
  204. row_pointers = NULL;
  205. png_read_end(png_ptr, NULL);
  206. return image_data;
  207. }
  208. void readpng_cleanup(int free_image_data)
  209. {
  210. if (free_image_data && image_data) {
  211. free(image_data);
  212. image_data = NULL;
  213. }
  214. if (png_ptr && info_ptr) {
  215. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  216. png_ptr = NULL;
  217. info_ptr = NULL;
  218. }
  219. }