png.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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,
  47. (png_infopp)NULL,
  48. (png_infopp)NULL);
  49. }
  50. #if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
  51. if (setjmp(png_jmpbuf((png_ptr))))
  52. #else
  53. if (setjmp(png_ptr->jmpbuf))
  54. #endif
  55. goto png_destroy;
  56. png_init_io(png_ptr, infile);
  57. png_read_info(png_ptr, info_ptr);
  58. png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type,
  59. &interlace_type, (int *) NULL, (int *) NULL);
  60. /* Prevent against integer overflow */
  61. if (w >= MAX_DIMENSION || h >= MAX_DIMENSION) {
  62. fprintf(stderr,
  63. "Unreasonable dimension found in file: %s\n", filename);
  64. goto png_destroy;
  65. }
  66. *width = (int) w;
  67. *height = (int) h;
  68. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA
  69. || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  70. alpha[0] = malloc(*width * *height);
  71. if (alpha[0] == NULL) {
  72. fprintf(stderr,
  73. "Can't allocate memory for alpha channel in PNG file.\n");
  74. goto png_destroy;
  75. }
  76. }
  77. /* Change a paletted/grayscale image to RGB */
  78. if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
  79. png_set_expand(png_ptr);
  80. /* Change a grayscale image to RGB */
  81. if (color_type == PNG_COLOR_TYPE_GRAY ||
  82. color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  83. png_set_gray_to_rgb(png_ptr);
  84. /* If the PNG file has 16 bits per channel, strip them down to 8 */
  85. if (bit_depth == 16)
  86. png_set_strip_16(png_ptr);
  87. /* use 1 byte per pixel */
  88. png_set_packing(png_ptr);
  89. row_pointers = malloc(*height * sizeof(png_bytep));
  90. if (row_pointers == NULL) {
  91. fprintf(stderr, "Can't allocate memory for PNG file.\n");
  92. goto png_destroy;
  93. }
  94. for (i = 0; i < *height; i++) {
  95. row_pointers[i] = malloc(4 * *width);
  96. if (row_pointers == NULL) {
  97. fprintf(stderr,
  98. "Can't allocate memory for PNG line.\n");
  99. goto rows_free;
  100. }
  101. }
  102. png_read_image(png_ptr, row_pointers);
  103. rgb[0] = malloc(3 * *width * *height);
  104. if (rgb[0] == NULL) {
  105. fprintf(stderr, "Can't allocate memory for PNG file.\n");
  106. goto rows_free;
  107. }
  108. if (alpha[0] == NULL) {
  109. ptr = rgb[0];
  110. for (i = 0; i < *height; i++) {
  111. memcpy(ptr, row_pointers[i], 3 * *width);
  112. ptr += 3 * *width;
  113. }
  114. } else {
  115. int j;
  116. ptr = rgb[0];
  117. for (i = 0; i < *height; i++) {
  118. int ipos = 0;
  119. for (j = 0; j < *width; j++) {
  120. *ptr++ = row_pointers[i][ipos++];
  121. *ptr++ = row_pointers[i][ipos++];
  122. *ptr++ = row_pointers[i][ipos++];
  123. alpha[0][i * *width + j]
  124. = row_pointers[i][ipos++];
  125. }
  126. }
  127. }
  128. ret = 1; /* data reading is OK */
  129. rows_free:
  130. for (i = 0; i < *height; i++) {
  131. if (row_pointers[i] != NULL)
  132. free(row_pointers[i]);
  133. }
  134. free(row_pointers);
  135. png_destroy:
  136. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
  137. file_close:
  138. fclose(infile);
  139. return ret;
  140. }