2
0

IMG_pnm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. SDL_image: An example image loading library for use with SDL
  3. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. * PNM (portable anymap) image loader:
  20. *
  21. * Supports: PBM, PGM and PPM, ASCII and binary formats
  22. * (PBM and PGM are loaded as 8bpp surfaces)
  23. * Does not support: maximum component value > 255
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include "SDL_image.h"
  30. #ifdef LOAD_PNM
  31. /* See if an image is contained in a data source */
  32. int IMG_isPNM(SDL_RWops *src)
  33. {
  34. Sint64 start;
  35. int is_PNM;
  36. char magic[2];
  37. if ( !src )
  38. return 0;
  39. start = SDL_RWtell(src);
  40. is_PNM = 0;
  41. if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
  42. /*
  43. * PNM magic signatures:
  44. * P1 PBM, ascii format
  45. * P2 PGM, ascii format
  46. * P3 PPM, ascii format
  47. * P4 PBM, binary format
  48. * P5 PGM, binary format
  49. * P6 PPM, binary format
  50. * P7 PAM, a general wrapper for PNM data
  51. */
  52. if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
  53. is_PNM = 1;
  54. }
  55. }
  56. SDL_RWseek(src, start, RW_SEEK_SET);
  57. return(is_PNM);
  58. }
  59. /* read a non-negative integer from the source. return -1 upon error */
  60. static int ReadNumber(SDL_RWops *src)
  61. {
  62. int number;
  63. unsigned char ch;
  64. /* Initialize return value */
  65. number = 0;
  66. /* Skip leading whitespace */
  67. do {
  68. if ( ! SDL_RWread(src, &ch, 1, 1) ) {
  69. return(0);
  70. }
  71. /* Eat comments as whitespace */
  72. if ( ch == '#' ) { /* Comment is '#' to end of line */
  73. do {
  74. if ( ! SDL_RWread(src, &ch, 1, 1) ) {
  75. return -1;
  76. }
  77. } while ( (ch != '\r') && (ch != '\n') );
  78. }
  79. } while ( isspace(ch) );
  80. /* Add up the number */
  81. do {
  82. number *= 10;
  83. number += ch-'0';
  84. if ( !SDL_RWread(src, &ch, 1, 1) ) {
  85. return -1;
  86. }
  87. } while ( isdigit(ch) );
  88. return(number);
  89. }
  90. SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
  91. {
  92. Sint64 start;
  93. SDL_Surface *surface = NULL;
  94. int width, height;
  95. int maxval, y, bpl;
  96. Uint8 *row;
  97. Uint8 *buf = NULL;
  98. char *error = NULL;
  99. Uint8 magic[2];
  100. int ascii;
  101. enum { PBM, PGM, PPM, PAM } kind;
  102. #define ERROR(s) do { error = (s); goto done; } while(0)
  103. if ( !src ) {
  104. /* The error message has been set in SDL_RWFromFile */
  105. return NULL;
  106. }
  107. start = SDL_RWtell(src);
  108. SDL_RWread(src, magic, 2, 1);
  109. kind = magic[1] - '1';
  110. ascii = 1;
  111. if(kind >= 3) {
  112. ascii = 0;
  113. kind -= 3;
  114. }
  115. width = ReadNumber(src);
  116. height = ReadNumber(src);
  117. if(width <= 0 || height <= 0)
  118. ERROR("Unable to read image width and height");
  119. if(kind != PBM) {
  120. maxval = ReadNumber(src);
  121. if(maxval <= 0 || maxval > 255)
  122. ERROR("unsupported PNM format");
  123. } else
  124. maxval = 255; /* never scale PBMs */
  125. /* binary PNM allows just a single character of whitespace after
  126. the last parameter, and we've already consumed it */
  127. if(kind == PPM) {
  128. /* 24-bit surface in R,G,B byte order */
  129. surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24,
  130. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  131. 0x000000ff, 0x0000ff00, 0x00ff0000,
  132. #else
  133. 0x00ff0000, 0x0000ff00, 0x000000ff,
  134. #endif
  135. 0);
  136. } else {
  137. /* load PBM/PGM as 8-bit indexed images */
  138. surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8,
  139. 0, 0, 0, 0);
  140. }
  141. if ( surface == NULL )
  142. ERROR("Out of memory");
  143. bpl = width * surface->format->BytesPerPixel;
  144. if(kind == PGM) {
  145. SDL_Color *c = surface->format->palette->colors;
  146. int i;
  147. for(i = 0; i < 256; i++)
  148. c[i].r = c[i].g = c[i].b = i;
  149. surface->format->palette->ncolors = 256;
  150. } else if(kind == PBM) {
  151. /* for some reason PBM has 1=black, 0=white */
  152. SDL_Color *c = surface->format->palette->colors;
  153. c[0].r = c[0].g = c[0].b = 255;
  154. c[1].r = c[1].g = c[1].b = 0;
  155. surface->format->palette->ncolors = 2;
  156. bpl = (width + 7) >> 3;
  157. buf = (Uint8 *)SDL_malloc(bpl);
  158. if(buf == NULL)
  159. ERROR("Out of memory");
  160. }
  161. /* Read the image into the surface */
  162. row = (Uint8 *)surface->pixels;
  163. for(y = 0; y < height; y++) {
  164. if(ascii) {
  165. int i;
  166. if(kind == PBM) {
  167. for(i = 0; i < width; i++) {
  168. Uint8 ch;
  169. do {
  170. if(!SDL_RWread(src, &ch,
  171. 1, 1))
  172. ERROR("file truncated");
  173. ch -= '0';
  174. } while(ch > 1);
  175. row[i] = ch;
  176. }
  177. } else {
  178. for(i = 0; i < bpl; i++) {
  179. int c;
  180. c = ReadNumber(src);
  181. if(c < 0)
  182. ERROR("file truncated");
  183. row[i] = c;
  184. }
  185. }
  186. } else {
  187. Uint8 *dst = (kind == PBM) ? buf : row;
  188. if(!SDL_RWread(src, dst, bpl, 1))
  189. ERROR("file truncated");
  190. if(kind == PBM) {
  191. /* expand bitmap to 8bpp */
  192. int i;
  193. for(i = 0; i < width; i++) {
  194. int bit = 7 - (i & 7);
  195. row[i] = (buf[i >> 3] >> bit) & 1;
  196. }
  197. }
  198. }
  199. if(maxval < 255) {
  200. /* scale up to full dynamic range (slow) */
  201. int i;
  202. for(i = 0; i < bpl; i++)
  203. row[i] = row[i] * 255 / maxval;
  204. }
  205. row += surface->pitch;
  206. }
  207. done:
  208. SDL_free(buf);
  209. if(error) {
  210. SDL_RWseek(src, start, RW_SEEK_SET);
  211. if ( surface ) {
  212. SDL_FreeSurface(surface);
  213. surface = NULL;
  214. }
  215. IMG_SetError(error);
  216. }
  217. return(surface);
  218. }
  219. #else
  220. /* See if an image is contained in a data source */
  221. int IMG_isPNM(SDL_RWops *src)
  222. {
  223. return(0);
  224. }
  225. /* Load a PNM type image from an SDL datasource */
  226. SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
  227. {
  228. return(NULL);
  229. }
  230. #endif /* LOAD_PNM */