png.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. int
  21. read_png(const char *filename, int *width, int *height, unsigned char **rgb,
  22. unsigned char **alpha)
  23. {
  24. FILE *infile = fopen(filename, "rb");
  25. png_structp png_ptr;
  26. png_infop info_ptr;
  27. png_bytepp row_pointers;
  28. unsigned char *ptr = NULL;
  29. png_uint_32 w, h;
  30. int bit_depth, color_type, interlace_type;
  31. int i;
  32. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  33. (png_voidp) NULL,
  34. (png_error_ptr) NULL,
  35. (png_error_ptr) NULL);
  36. if (!png_ptr)
  37. {
  38. fclose(infile);
  39. return(0);
  40. }
  41. info_ptr = png_create_info_struct(png_ptr);
  42. if (!info_ptr)
  43. {
  44. png_destroy_read_struct(&png_ptr, (png_infopp) NULL,
  45. (png_infopp) NULL);
  46. fclose(infile);
  47. return(0);
  48. }
  49. if (setjmp(png_ptr->jmpbuf))
  50. {
  51. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
  52. fclose(infile);
  53. return(0);
  54. }
  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. *width = (int) w;
  60. *height = (int) h;
  61. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA
  62. || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  63. {
  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. return(0);
  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) png_set_strip_16(png_ptr);
  80. /* use 1 byte per pixel */
  81. png_set_packing(png_ptr);
  82. row_pointers = malloc(*height * sizeof(png_bytep));
  83. if (row_pointers == NULL)
  84. {
  85. fprintf(stderr, "Can't allocate memory for PNG file.\n");
  86. return(0);
  87. }
  88. for (i = 0; i < *height; i++)
  89. {
  90. row_pointers[i] = malloc(4 * *width);
  91. if (row_pointers == NULL)
  92. {
  93. fprintf(stderr, "Can't allocate memory for PNG line.\n");
  94. return(0);
  95. }
  96. }
  97. png_read_image(png_ptr, row_pointers);
  98. rgb[0] = malloc(3 * *width * *height);
  99. if (rgb[0] == NULL)
  100. {
  101. fprintf(stderr, "Can't allocate memory for PNG file.\n");
  102. return(0);
  103. }
  104. if (alpha[0] == NULL)
  105. {
  106. ptr = rgb[0];
  107. for (i = 0; i < *height; i++)
  108. {
  109. memcpy(ptr, row_pointers[i], 3 * *width);
  110. ptr += 3 * *width;
  111. }
  112. }
  113. else
  114. {
  115. int j;
  116. ptr = rgb[0];
  117. for (i = 0; i < *height; i++)
  118. {
  119. int ipos = 0;
  120. for (j = 0; j < *width; j++)
  121. {
  122. *ptr++ = row_pointers[i][ipos++];
  123. *ptr++ = row_pointers[i][ipos++];
  124. *ptr++ = row_pointers[i][ipos++];
  125. alpha[0][i * *width + j] = row_pointers[i][ipos++];
  126. }
  127. }
  128. }
  129. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
  130. for (i = 0; i < *height; i++) free(row_pointers[i]);
  131. free(row_pointers);
  132. fclose(infile);
  133. return(1);
  134. }