IMG_png.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. SDL_image: An example image loading library for use with SDL
  3. Copyright (C) 1999, 2000, 2001 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. */
  18. /* $Id: IMG_png.c,v 1.1 2004/07/21 16:24:11 paigoddess Exp $ */
  19. /* This is a PNG image file loading framework */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include "SDL_image.h"
  23. /*=============================================================================
  24. File: SDL_png.c
  25. Purpose: A PNG loader and saver for the SDL library
  26. Revision:
  27. Created by: Philippe Lavoie (2 November 1998)
  28. lavoie@zeus.genie.uottawa.ca
  29. Modified by:
  30. Copyright notice:
  31. Copyright (C) 1998 Philippe Lavoie
  32. This library is free software; you can redistribute it and/or
  33. modify it under the terms of the GNU Library General Public
  34. License as published by the Free Software Foundation; either
  35. version 2 of the License, or (at your option) any later version.
  36. This library is distributed in the hope that it will be useful,
  37. but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  39. Library General Public License for more details.
  40. You should have received a copy of the GNU Library General Public
  41. License along with this library; if not, write to the Free
  42. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  43. Comments: The load and save routine are basically the ones you can find
  44. in the example.c file from the libpng distribution.
  45. Changes:
  46. 5/17/99 - Modified to use the new SDL data sources - Sam Lantinga
  47. =============================================================================*/
  48. #include "SDL_endian.h"
  49. #ifdef macintosh
  50. #define MACOS
  51. #endif
  52. #include <png.h>
  53. #define PNG_BYTES_TO_CHECK 4
  54. /* See if an image is contained in a data source */
  55. int IMG_isPNG(SDL_RWops *src)
  56. {
  57. unsigned char buf[PNG_BYTES_TO_CHECK];
  58. /* Read in the signature bytes */
  59. if (SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) != PNG_BYTES_TO_CHECK)
  60. return 0;
  61. /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */
  62. return( !png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
  63. }
  64. /* Load a PNG type image from an SDL datasource */
  65. static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
  66. {
  67. SDL_RWops *src;
  68. src = (SDL_RWops *)png_get_io_ptr(ctx);
  69. SDL_RWread(src, area, size, 1);
  70. }
  71. SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
  72. {
  73. SDL_Surface *volatile surface;
  74. png_structp png_ptr;
  75. png_infop info_ptr;
  76. png_uint_32 width, height;
  77. int bit_depth, color_type, interlace_type;
  78. Uint32 Rmask;
  79. Uint32 Gmask;
  80. Uint32 Bmask;
  81. Uint32 Amask;
  82. SDL_Palette *palette;
  83. png_bytep *volatile row_pointers;
  84. int row, i;
  85. volatile int ckey = -1;
  86. png_color_16 *transv;
  87. /* Initialize the data we will clean up when we're done */
  88. png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
  89. /* Check to make sure we have something to do */
  90. if ( ! src ) {
  91. goto done;
  92. }
  93. /* Create the PNG loading context structure */
  94. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  95. NULL,NULL,NULL);
  96. if (png_ptr == NULL){
  97. IMG_SetError("Couldn't allocate memory for PNG file");
  98. goto done;
  99. }
  100. /* Allocate/initialize the memory for image information. REQUIRED. */
  101. info_ptr = png_create_info_struct(png_ptr);
  102. if (info_ptr == NULL) {
  103. IMG_SetError("Couldn't create image information for PNG file");
  104. goto done;
  105. }
  106. /* Set error handling if you are using setjmp/longjmp method (this is
  107. * the normal method of doing things with libpng). REQUIRED unless you
  108. * set up your own error handlers in png_create_read_struct() earlier.
  109. */
  110. if ( setjmp(png_ptr->jmpbuf) ) {
  111. IMG_SetError("Error reading the PNG file.");
  112. goto done;
  113. }
  114. /* Set up the input control */
  115. png_set_read_fn(png_ptr, src, png_read_data);
  116. /* Read PNG header info */
  117. png_read_info(png_ptr, info_ptr);
  118. png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
  119. &color_type, &interlace_type, NULL, NULL);
  120. /* tell libpng to strip 16 bit/color files down to 8 bits/color */
  121. png_set_strip_16(png_ptr) ;
  122. /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
  123. * byte into separate bytes (useful for paletted and grayscale images).
  124. */
  125. png_set_packing(png_ptr);
  126. /* scale greyscale values to the range 0..255 */
  127. if(color_type == PNG_COLOR_TYPE_GRAY)
  128. png_set_expand(png_ptr);
  129. /* For images with a single "transparent colour", set colour key;
  130. if more than one index has transparency, or if partially transparent
  131. entries exist, use full alpha channel */
  132. if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
  133. int num_trans;
  134. Uint8 *trans;
  135. png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
  136. &transv);
  137. if(color_type == PNG_COLOR_TYPE_PALETTE) {
  138. /* Check if all tRNS entries are opaque except one */
  139. int i, t = -1;
  140. for(i = 0; i < num_trans; i++)
  141. if(trans[i] == 0) {
  142. if(t >= 0)
  143. break;
  144. t = i;
  145. } else if(trans[i] != 255)
  146. break;
  147. if(i == num_trans) {
  148. /* exactly one transparent index */
  149. ckey = t;
  150. } else {
  151. /* more than one transparent index, or translucency */
  152. png_set_expand(png_ptr);
  153. }
  154. } else
  155. ckey = 0; /* actual value will be set later */
  156. }
  157. if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
  158. png_set_gray_to_rgb(png_ptr);
  159. png_read_update_info(png_ptr, info_ptr);
  160. png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
  161. &color_type, &interlace_type, NULL, NULL);
  162. /* Allocate the SDL surface to hold the image */
  163. Rmask = Gmask = Bmask = Amask = 0 ;
  164. if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
  165. if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
  166. Rmask = 0x000000FF;
  167. Gmask = 0x0000FF00;
  168. Bmask = 0x00FF0000;
  169. Amask = (info_ptr->channels == 4) ? 0xFF000000 : 0;
  170. } else {
  171. int s = (info_ptr->channels == 4) ? 0 : 8;
  172. Rmask = 0xFF000000 >> s;
  173. Gmask = 0x00FF0000 >> s;
  174. Bmask = 0x0000FF00 >> s;
  175. Amask = 0x000000FF >> s;
  176. }
  177. }
  178. surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
  179. bit_depth*info_ptr->channels, Rmask,Gmask,Bmask,Amask);
  180. if ( surface == NULL ) {
  181. IMG_SetError("Out of memory");
  182. goto done;
  183. }
  184. if(ckey != -1) {
  185. if(color_type != PNG_COLOR_TYPE_PALETTE)
  186. /* FIXME: Should these be truncated or shifted down? */
  187. ckey = SDL_MapRGB(surface->format,
  188. (Uint8)transv->red,
  189. (Uint8)transv->green,
  190. (Uint8)transv->blue);
  191. SDL_SetColorKey(surface, SDL_SRCCOLORKEY, ckey);
  192. }
  193. /* Create the array of pointers to image data */
  194. row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*height);
  195. if ( (row_pointers == NULL) ) {
  196. IMG_SetError("Out of memory");
  197. SDL_FreeSurface(surface);
  198. surface = NULL;
  199. goto done;
  200. }
  201. for (row = 0; row < (int)height; row++) {
  202. row_pointers[row] = (png_bytep)
  203. (Uint8 *)surface->pixels + row*surface->pitch;
  204. }
  205. /* Read the entire image in one go */
  206. png_read_image(png_ptr, row_pointers);
  207. /* read rest of file, get additional chunks in info_ptr - REQUIRED */
  208. png_read_end(png_ptr, info_ptr);
  209. /* Load the palette, if any */
  210. palette = surface->format->palette;
  211. if ( palette ) {
  212. if(color_type == PNG_COLOR_TYPE_GRAY) {
  213. palette->ncolors = 256;
  214. for(i = 0; i < 256; i++) {
  215. palette->colors[i].r = i;
  216. palette->colors[i].g = i;
  217. palette->colors[i].b = i;
  218. }
  219. } else if (info_ptr->num_palette > 0 ) {
  220. palette->ncolors = info_ptr->num_palette;
  221. for( i=0; i<info_ptr->num_palette; ++i ) {
  222. palette->colors[i].b = info_ptr->palette[i].blue;
  223. palette->colors[i].g = info_ptr->palette[i].green;
  224. palette->colors[i].r = info_ptr->palette[i].red;
  225. }
  226. }
  227. }
  228. done: /* Clean up and return */
  229. png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : (png_infopp)0,
  230. (png_infopp)0);
  231. if ( row_pointers ) {
  232. free(row_pointers);
  233. }
  234. return(surface);
  235. }