png.c 4.9 KB

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