IMG_pcx.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. SDL_image: An example image loading library for use with SDL
  3. Copyright (C) 1999, 2000, 2001 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. */
  18. /* $Id: IMG_pcx.c,v 1.1 2004/07/21 16:24:11 paigoddess Exp $ */
  19. /*
  20. * PCX file reader:
  21. * Supports:
  22. * 1..4 bits/pixel in multiplanar format (1 bit/plane/pixel)
  23. * 8 bits/pixel in single-planar format (8 bits/plane/pixel)
  24. * 24 bits/pixel in 3-plane format (8 bits/plane/pixel)
  25. *
  26. * (The <8bpp formats are expanded to 8bpp surfaces)
  27. *
  28. * Doesn't support:
  29. * single-planar packed-pixel formats other than 8bpp
  30. * 4-plane 32bpp format with a fourth "intensity" plane
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "SDL_endian.h"
  35. #include "SDL_image.h"
  36. struct PCXheader {
  37. Uint8 Manufacturer;
  38. Uint8 Version;
  39. Uint8 Encoding;
  40. Uint8 BitsPerPixel;
  41. Sint16 Xmin, Ymin, Xmax, Ymax;
  42. Sint16 HDpi, VDpi;
  43. Uint8 Colormap[48];
  44. Uint8 Reserved;
  45. Uint8 NPlanes;
  46. Sint16 BytesPerLine;
  47. Sint16 PaletteInfo;
  48. Sint16 HscreenSize;
  49. Sint16 VscreenSize;
  50. Uint8 Filler[54];
  51. };
  52. /* See if an image is contained in a data source */
  53. int IMG_isPCX(SDL_RWops *src)
  54. {
  55. int is_PCX;
  56. const int ZSoft_Manufacturer = 10;
  57. const int PC_Paintbrush_Version = 5;
  58. const int PCX_RunLength_Encoding = 1;
  59. struct PCXheader pcxh;
  60. is_PCX = 0;
  61. if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) {
  62. if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
  63. (pcxh.Version == PC_Paintbrush_Version) &&
  64. (pcxh.Encoding == PCX_RunLength_Encoding) ) {
  65. is_PCX = 1;
  66. }
  67. }
  68. return(is_PCX);
  69. }
  70. /* Load a PCX type image from an SDL datasource */
  71. SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
  72. {
  73. struct PCXheader pcxh;
  74. Uint32 Rmask;
  75. Uint32 Gmask;
  76. Uint32 Bmask;
  77. Uint32 Amask;
  78. SDL_Surface *surface = NULL;
  79. int width, height;
  80. int y, bpl;
  81. Uint8 *row, *buf = NULL;
  82. char *error = NULL;
  83. int bits, src_bits;
  84. if ( ! src ) {
  85. goto done;
  86. }
  87. if ( ! SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
  88. error = "file truncated";
  89. goto done;
  90. }
  91. pcxh.Xmin = SDL_SwapLE16(pcxh.Xmin);
  92. pcxh.Ymin = SDL_SwapLE16(pcxh.Ymin);
  93. pcxh.Xmax = SDL_SwapLE16(pcxh.Xmax);
  94. pcxh.Ymax = SDL_SwapLE16(pcxh.Ymax);
  95. pcxh.BytesPerLine = SDL_SwapLE16(pcxh.BytesPerLine);
  96. /* Create the surface of the appropriate type */
  97. width = (pcxh.Xmax - pcxh.Xmin) + 1;
  98. height = (pcxh.Ymax - pcxh.Ymin) + 1;
  99. Rmask = Gmask = Bmask = Amask = 0;
  100. src_bits = pcxh.BitsPerPixel * pcxh.NPlanes;
  101. if((pcxh.BitsPerPixel == 1 && pcxh.NPlanes >= 1 && pcxh.NPlanes <= 4)
  102. || (pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 1)) {
  103. bits = 8;
  104. } else if(pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 3) {
  105. bits = 24;
  106. if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
  107. Rmask = 0x000000FF;
  108. Gmask = 0x0000FF00;
  109. Bmask = 0x00FF0000;
  110. } else {
  111. Rmask = 0xFF0000;
  112. Gmask = 0x00FF00;
  113. Bmask = 0x0000FF;
  114. }
  115. } else {
  116. error = "unsupported PCX format";
  117. goto done;
  118. }
  119. surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
  120. bits, Rmask, Gmask, Bmask, Amask);
  121. if ( surface == NULL )
  122. goto done;
  123. bpl = pcxh.NPlanes * pcxh.BytesPerLine;
  124. buf = (Uint8*)malloc(bpl);
  125. row = (Uint8*)surface->pixels;
  126. for ( y=0; y<surface->h; ++y ) {
  127. /* decode a scan line to a temporary buffer first */
  128. int i, count = 0;
  129. Uint8 ch;
  130. Uint8 *dst = (src_bits == 8) ? row : buf;
  131. for(i = 0; i < bpl; i++) {
  132. if(!count) {
  133. if(!SDL_RWread(src, &ch, 1, 1)) {
  134. error = "file truncated";
  135. goto done;
  136. }
  137. if( (ch & 0xc0) == 0xc0) {
  138. count = ch & 0x3f;
  139. if(!SDL_RWread(src, &ch, 1, 1)) {
  140. error = "file truncated";
  141. goto done;
  142. }
  143. } else
  144. count = 1;
  145. }
  146. dst[i] = ch;
  147. count--;
  148. }
  149. if(src_bits <= 4) {
  150. /* expand planes to 1 byte/pixel */
  151. Uint8 *src = buf;
  152. int plane;
  153. for(plane = 0; plane < pcxh.NPlanes; plane++) {
  154. int i, j, x = 0;
  155. for(i = 0; i < pcxh.BytesPerLine; i++) {
  156. Uint8 byte = *src++;
  157. for(j = 7; j >= 0; j--) {
  158. unsigned bit = (byte >> j) & 1;
  159. row[x++] |= bit << plane;
  160. }
  161. }
  162. }
  163. } else if(src_bits == 24) {
  164. /* de-interlace planes */
  165. Uint8 *src = buf;
  166. int plane;
  167. for(plane = 0; plane < pcxh.NPlanes; plane++) {
  168. int x;
  169. dst = row + plane;
  170. for(x = 0; x < width; x++) {
  171. *dst = *src++;
  172. dst += pcxh.NPlanes;
  173. }
  174. }
  175. }
  176. row += surface->pitch;
  177. }
  178. if(bits == 8) {
  179. SDL_Color *colors = surface->format->palette->colors;
  180. int nc = 1 << src_bits;
  181. int i;
  182. surface->format->palette->ncolors = nc;
  183. if(src_bits == 8) {
  184. Uint8 ch;
  185. /* look for a 256-colour palette */
  186. do {
  187. if ( !SDL_RWread(src, &ch, 1, 1)) {
  188. error = "file truncated";
  189. goto done;
  190. }
  191. } while ( ch != 12 );
  192. for(i = 0; i < 256; i++) {
  193. SDL_RWread(src, &colors[i].r, 1, 1);
  194. SDL_RWread(src, &colors[i].g, 1, 1);
  195. SDL_RWread(src, &colors[i].b, 1, 1);
  196. }
  197. } else {
  198. for(i = 0; i < nc; i++) {
  199. colors[i].r = pcxh.Colormap[i * 3];
  200. colors[i].g = pcxh.Colormap[i * 3 + 1];
  201. colors[i].b = pcxh.Colormap[i * 3 + 2];
  202. }
  203. }
  204. }
  205. done:
  206. free(buf);
  207. if ( error ) {
  208. SDL_FreeSurface(surface);
  209. IMG_SetError(error);
  210. surface = NULL;
  211. }
  212. return(surface);
  213. }