readppm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*---------------------------------------------------------------------------
  2. rpng - simple PNG display program readppm.c
  3. ---------------------------------------------------------------------------
  4. This is a special-purpose replacement for readpng.c that allows binary
  5. PPM files to be used in place of PNG images.
  6. ---------------------------------------------------------------------------
  7. Copyright (c) 1998-2007 Greg Roelofs. All rights reserved.
  8. This software is provided "as is," without warranty of any kind,
  9. express or implied. In no event shall the author or contributors
  10. be held liable for any damages arising in any way from the use of
  11. this software.
  12. The contents of this file are DUAL-LICENSED. You may modify and/or
  13. redistribute this software according to the terms of one of the
  14. following two licenses (at your option):
  15. LICENSE 1 ("BSD-like with advertising clause"):
  16. Permission is granted to anyone to use this software for any purpose,
  17. including commercial applications, and to alter it and redistribute
  18. it freely, subject to the following restrictions:
  19. 1. Redistributions of source code must retain the above copyright
  20. notice, disclaimer, and this list of conditions.
  21. 2. Redistributions in binary form must reproduce the above copyright
  22. notice, disclaimer, and this list of conditions in the documenta-
  23. tion and/or other materials provided with the distribution.
  24. 3. All advertising materials mentioning features or use of this
  25. software must display the following acknowledgment:
  26. This product includes software developed by Greg Roelofs
  27. and contributors for the book, "PNG: The Definitive Guide,"
  28. published by O'Reilly and Associates.
  29. LICENSE 2 (GNU GPL v2 or later):
  30. This program is free software; you can redistribute it and/or modify
  31. it under the terms of the GNU General Public License as published by
  32. the Free Software Foundation; either version 2 of the License, or
  33. (at your option) any later version.
  34. This program is distributed in the hope that it will be useful,
  35. but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. GNU General Public License for more details.
  38. You should have received a copy of the GNU General Public License
  39. along with this program; if not, write to the Free Software Foundation,
  40. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  41. ---------------------------------------------------------------------------*/
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include "readpng.h" /* typedefs, common macros, public prototypes */
  45. ulg width, height;
  46. int bit_depth, color_type, channels;
  47. uch *image_data = NULL;
  48. FILE *saved_infile;
  49. void readpng_version_info()
  50. {
  51. fprintf(stderr, " Compiled without libpng, zlib or PBMPLUS/NetPBM.\n");
  52. }
  53. /* return value = 0 for success, 1 for bad sig, 2 for bad IHDR, 4 for no mem */
  54. int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
  55. {
  56. static uch ppmline[256];
  57. int maxval;
  58. saved_infile = infile;
  59. fgets(ppmline, 256, infile);
  60. if (ppmline[0] != 'P' || ppmline[1] != '6') {
  61. fprintf(stderr, "ERROR: not a PPM file\n");
  62. return 1;
  63. }
  64. /* possible color types: P5 = grayscale (0), P6 = RGB (2), P8 = RGBA (6) */
  65. if (ppmline[1] == '6') {
  66. color_type = 2;
  67. channels = 3;
  68. } else if (ppmline[1] == '8') {
  69. color_type = 6;
  70. channels = 4;
  71. } else /* if (ppmline[1] == '5') */ {
  72. color_type = 0;
  73. channels = 1;
  74. }
  75. do {
  76. fgets(ppmline, 256, infile);
  77. } while (ppmline[0] == '#');
  78. sscanf(ppmline, "%lu %lu", &width, &height);
  79. do {
  80. fgets(ppmline, 256, infile);
  81. } while (ppmline[0] == '#');
  82. sscanf(ppmline, "%d", &maxval);
  83. if (maxval != 255) {
  84. fprintf(stderr, "ERROR: maxval = %d\n", maxval);
  85. return 2;
  86. }
  87. bit_depth = 8;
  88. *pWidth = width;
  89. *pHeight = height;
  90. return 0;
  91. }
  92. /* returns 0 if succeeds, 1 if fails due to no bKGD chunk, 2 if libpng error;
  93. * scales values to 8-bit if necessary */
  94. int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
  95. {
  96. return 1;
  97. }
  98. /* display_exponent == LUT_exponent * CRT_exponent */
  99. uch *readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
  100. {
  101. ulg rowbytes;
  102. /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
  103. * transparency chunks to full alpha channel; strip 16-bit-per-sample
  104. * images to 8 bits per sample; and convert grayscale to RGB[A] */
  105. /* GRR WARNING: grayscale needs to be expanded and channels reset! */
  106. *pRowbytes = rowbytes = channels*width;
  107. *pChannels = channels;
  108. if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
  109. return NULL;
  110. }
  111. Trace((stderr, "readpng_get_image: rowbytes = %ld, height = %ld\n", rowbytes, height));
  112. /* now we can go ahead and just read the whole image */
  113. if (fread(image_data, 1L, rowbytes*height, saved_infile) <
  114. rowbytes*height) {
  115. free (image_data);
  116. image_data = NULL;
  117. return NULL;
  118. }
  119. return image_data;
  120. }
  121. void readpng_cleanup(int free_image_data)
  122. {
  123. if (free_image_data && image_data) {
  124. free(image_data);
  125. image_data = NULL;
  126. }
  127. }