png.c 4.4 KB

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