png.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /****************************************************************************
  2. png.c - read and write png images using libpng routines.
  3. Distributed with Xplanet.
  4. Copyright (C) 2002 Hari Nair <hari@alumni.caltech.edu>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. ****************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <png.h>
  20. #include "const.h"
  21. int
  22. read_png(const char *filename, int *width, int *height, unsigned char **rgb,
  23. unsigned char **alpha)
  24. {
  25. int ret = 0;
  26. png_structp png_ptr;
  27. png_infop info_ptr;
  28. png_bytepp row_pointers;
  29. unsigned char *ptr = NULL;
  30. png_uint_32 w, h;
  31. int bit_depth, color_type, interlace_type;
  32. int i;
  33. FILE *infile = fopen(filename, "rb");
  34. if (infile == NULL) {
  35. fprintf(stderr, "Can not fopen file: %s\n",filename);
  36. return ret;
  37. }
  38. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  39. (png_voidp) NULL,
  40. (png_error_ptr) NULL,
  41. (png_error_ptr) NULL);
  42. if (!png_ptr)
  43. goto file_close;
  44. info_ptr = png_create_info_struct(png_ptr);
  45. if (!info_ptr) {
  46. png_destroy_read_struct(&png_ptr, (png_infopp) NULL,
  47. (png_infopp) NULL);
  48. }
  49. #if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
  50. if (setjmp(png_jmpbuf((png_ptr))))
  51. #else
  52. if (setjmp(png_ptr->jmpbuf))
  53. #endif
  54. goto png_destroy;
  55. png_init_io(png_ptr, infile);
  56. png_read_info(png_ptr, info_ptr);
  57. png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type,
  58. &interlace_type, (int *) NULL, (int *) NULL);
  59. /* Prevent against integer overflow */
  60. if(w >= MAX_DIMENSION || h >= MAX_DIMENSION) {
  61. fprintf(stderr, "Unreasonable dimension found in file: %s\n",filename);
  62. goto png_destroy;
  63. }
  64. *width = (int) w;
  65. *height = (int) h;
  66. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA
  67. || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  68. alpha[0] = malloc(*width * *height);
  69. if (alpha[0] == NULL)
  70. {
  71. fprintf(stderr, "Can't allocate memory for alpha channel in PNG file.\n");
  72. goto png_destroy;
  73. }
  74. }
  75. /* Change a paletted/grayscale image to RGB */
  76. if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
  77. png_set_expand(png_ptr);
  78. /* Change a grayscale image to RGB */
  79. if (color_type == PNG_COLOR_TYPE_GRAY
  80. || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  81. png_set_gray_to_rgb(png_ptr);
  82. /* If the PNG file has 16 bits per channel, strip them down to 8 */
  83. if (bit_depth == 16)
  84. png_set_strip_16(png_ptr);
  85. /* use 1 byte per pixel */
  86. png_set_packing(png_ptr);
  87. row_pointers = malloc(*height * sizeof(png_bytep));
  88. if (row_pointers == NULL) {
  89. fprintf(stderr, "Can't allocate memory for PNG file.\n");
  90. goto png_destroy;
  91. }
  92. for (i = 0; i < *height; i++) {
  93. row_pointers[i] = malloc(4 * *width);
  94. if (row_pointers == NULL) {
  95. fprintf(stderr, "Can't allocate memory for PNG line.\n");
  96. goto rows_free;
  97. }
  98. }
  99. png_read_image(png_ptr, row_pointers);
  100. rgb[0] = malloc(3 * *width * *height);
  101. if (rgb[0] == NULL) {
  102. fprintf(stderr, "Can't allocate memory for PNG file.\n");
  103. goto rows_free;
  104. }
  105. if (alpha[0] == NULL)
  106. {
  107. ptr = rgb[0];
  108. for (i = 0; i < *height; i++) {
  109. memcpy(ptr, row_pointers[i], 3 * *width);
  110. ptr += 3 * *width;
  111. }
  112. } else {
  113. int j;
  114. ptr = rgb[0];
  115. for (i = 0; i < *height; i++) {
  116. int ipos = 0;
  117. for (j = 0; j < *width; j++) {
  118. *ptr++ = row_pointers[i][ipos++];
  119. *ptr++ = row_pointers[i][ipos++];
  120. *ptr++ = row_pointers[i][ipos++];
  121. alpha[0][i * *width + j] = row_pointers[i][ipos++];
  122. }
  123. }
  124. }
  125. ret = 1; /* data reading is OK */
  126. rows_free:
  127. for (i = 0; i < *height; i++) {
  128. if (row_pointers[i] != NULL ) {
  129. free(row_pointers[i]);
  130. }
  131. }
  132. free(row_pointers);
  133. png_destroy:
  134. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
  135. file_close:
  136. fclose(infile);
  137. return(ret);
  138. }