readpng.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_LIBPNG_VER_STRING, NULL, NULL, NULL);
  71. if (!png_ptr)
  72. return 4; /* out of memory */
  73. info_ptr = png_create_info_struct(png_ptr);
  74. if (!info_ptr) {
  75. png_destroy_read_struct(&png_ptr, NULL, NULL);
  76. return 4; /* out of memory */
  77. }
  78. /* we could create a second info struct here (end_info), but it's only
  79. * useful if we want to keep pre- and post-IDAT chunk info separated
  80. * (mainly for PNG-aware image editors and converters) */
  81. /* setjmp() must be called in every function that calls a PNG-reading
  82. * libpng function */
  83. if (setjmp(png_jmpbuf(png_ptr))) {
  84. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  85. return 2;
  86. }
  87. png_init_io(png_ptr, infile);
  88. png_set_sig_bytes(png_ptr, 8); /* we already read the 8 signature bytes */
  89. png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */
  90. /* alternatively, could make separate calls to png_get_image_width(),
  91. * etc., but want bit_depth and color_type for later [don't care about
  92. * compression_type and filter_type => NULLs] */
  93. png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
  94. NULL, NULL, NULL);
  95. *pWidth = width;
  96. *pHeight = height;
  97. /* OK, that's all we need for now; return happy */
  98. return 0;
  99. }
  100. /* returns 0 if succeeds, 1 if fails due to no bKGD chunk, 2 if libpng error;
  101. * scales values to 8-bit if necessary */
  102. int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
  103. {
  104. png_color_16p pBackground;
  105. /* setjmp() must be called in every function that calls a PNG-reading
  106. * libpng function */
  107. if (setjmp(png_jmpbuf(png_ptr))) {
  108. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  109. return 2;
  110. }
  111. if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD))
  112. return 1;
  113. /* it is not obvious from the libpng documentation, but this function
  114. * takes a pointer to a pointer, and it always returns valid red, green
  115. * and blue values, regardless of color_type: */
  116. png_get_bKGD(png_ptr, info_ptr, &pBackground);
  117. /* however, it always returns the raw bKGD data, regardless of any
  118. * bit-depth transformations, so check depth and adjust if necessary */
  119. if (bit_depth == 16) {
  120. *red = pBackground->red >> 8;
  121. *green = pBackground->green >> 8;
  122. *blue = pBackground->blue >> 8;
  123. } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
  124. if (bit_depth == 1)
  125. *red = *green = *blue = pBackground->gray? 255 : 0;
  126. else if (bit_depth == 2)
  127. *red = *green = *blue = (255/3) * pBackground->gray;
  128. else /* bit_depth == 4 */
  129. *red = *green = *blue = (255/15) * pBackground->gray;
  130. } else {
  131. *red = (uch)pBackground->red;
  132. *green = (uch)pBackground->green;
  133. *blue = (uch)pBackground->blue;
  134. }
  135. return 0;
  136. }
  137. /* display_exponent == LUT_exponent * CRT_exponent */
  138. uch *readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
  139. {
  140. double gamma;
  141. png_uint_32 i, rowbytes;
  142. png_bytepp row_pointers = NULL;
  143. /* setjmp() must be called in every function that calls a PNG-reading
  144. * libpng function */
  145. if (setjmp(png_jmpbuf(png_ptr))) {
  146. free(image_data);
  147. image_data = NULL;
  148. free(row_pointers);
  149. row_pointers = NULL;
  150. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  151. return NULL;
  152. }
  153. /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
  154. * transparency chunks to full alpha channel; strip 16-bit-per-sample
  155. * images to 8 bits per sample; and convert grayscale to RGB[A] */
  156. if (color_type == PNG_COLOR_TYPE_PALETTE)
  157. png_set_expand(png_ptr);
  158. if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
  159. png_set_expand(png_ptr);
  160. if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
  161. png_set_expand(png_ptr);
  162. #ifdef PNG_READ_16_TO_8_SUPPORTED
  163. if (bit_depth == 16)
  164. # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  165. png_set_scale_16(png_ptr);
  166. # else
  167. png_set_strip_16(png_ptr);
  168. # endif
  169. #endif
  170. if (color_type == PNG_COLOR_TYPE_GRAY ||
  171. color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  172. png_set_gray_to_rgb(png_ptr);
  173. /* unlike the example in the libpng documentation, we have *no* idea where
  174. * this file may have come from--so if it doesn't have a file gamma, don't
  175. * do any correction ("do no harm") */
  176. if (png_get_gAMA(png_ptr, info_ptr, &gamma))
  177. png_set_gamma(png_ptr, display_exponent, gamma);
  178. /* all transformations have been registered; now update info_ptr data,
  179. * get rowbytes and channels, and allocate image memory */
  180. png_read_update_info(png_ptr, info_ptr);
  181. *pRowbytes = rowbytes = png_get_rowbytes(png_ptr, info_ptr);
  182. *pChannels = (int)png_get_channels(png_ptr, info_ptr);
  183. if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
  184. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  185. return NULL;
  186. }
  187. if ((row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep))) == NULL) {
  188. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  189. free(image_data);
  190. image_data = NULL;
  191. return NULL;
  192. }
  193. Trace((stderr, "readpng_get_image: channels = %d, rowbytes = %ld, height = %ld\n",
  194. *pChannels, rowbytes, height));
  195. /* set the individual row_pointers to point at the correct offsets */
  196. for (i = 0; i < height; ++i)
  197. row_pointers[i] = image_data + i*rowbytes;
  198. /* now we can go ahead and just read the whole image */
  199. png_read_image(png_ptr, row_pointers);
  200. /* and we're done! (png_read_end() can be omitted if no processing of
  201. * post-IDAT text/time/etc. is desired) */
  202. free(row_pointers);
  203. row_pointers = NULL;
  204. png_read_end(png_ptr, NULL);
  205. return image_data;
  206. }
  207. void readpng_cleanup(int free_image_data)
  208. {
  209. if (free_image_data && image_data) {
  210. free(image_data);
  211. image_data = NULL;
  212. }
  213. if (png_ptr && info_ptr) {
  214. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  215. png_ptr = NULL;
  216. info_ptr = NULL;
  217. }
  218. }