/project/jni/sdl_image/IMG_pcx.c

https://github.com/aichunyu/FFPlayer · C · 276 lines · 210 code · 22 blank · 44 comment · 64 complexity · 3e2c8fadbf524a6b8db12cc11d83e42f MD5 · raw file

  1. /*
  2. SDL_image: An example image loading library for use with SDL
  3. Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
  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. * PCX file reader:
  20. * Supports:
  21. * 1..4 bits/pixel in multiplanar format (1 bit/plane/pixel)
  22. * 8 bits/pixel in single-planar format (8 bits/plane/pixel)
  23. * 24 bits/pixel in 3-plane format (8 bits/plane/pixel)
  24. *
  25. * (The <8bpp formats are expanded to 8bpp surfaces)
  26. *
  27. * Doesn't support:
  28. * single-planar packed-pixel formats other than 8bpp
  29. * 4-plane 32bpp format with a fourth "intensity" plane
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "SDL_endian.h"
  34. #include "SDL_image.h"
  35. #ifdef LOAD_PCX
  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 start;
  56. int is_PCX;
  57. const int ZSoft_Manufacturer = 10;
  58. const int PC_Paintbrush_Version = 5;
  59. const int PCX_Uncompressed_Encoding = 0;
  60. const int PCX_RunLength_Encoding = 1;
  61. struct PCXheader pcxh;
  62. if ( !src )
  63. return 0;
  64. start = SDL_RWtell(src);
  65. is_PCX = 0;
  66. if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) {
  67. if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
  68. (pcxh.Version == PC_Paintbrush_Version) &&
  69. (pcxh.Encoding == PCX_RunLength_Encoding ||
  70. pcxh.Encoding == PCX_Uncompressed_Encoding) ) {
  71. is_PCX = 1;
  72. }
  73. }
  74. SDL_RWseek(src, start, RW_SEEK_SET);
  75. return(is_PCX);
  76. }
  77. /* Load a PCX type image from an SDL datasource */
  78. SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
  79. {
  80. int start;
  81. struct PCXheader pcxh;
  82. Uint32 Rmask;
  83. Uint32 Gmask;
  84. Uint32 Bmask;
  85. Uint32 Amask;
  86. SDL_Surface *surface = NULL;
  87. int width, height;
  88. int y, bpl;
  89. Uint8 *row, *buf = NULL;
  90. char *error = NULL;
  91. int bits, src_bits;
  92. if ( !src ) {
  93. /* The error message has been set in SDL_RWFromFile */
  94. return NULL;
  95. }
  96. start = SDL_RWtell(src);
  97. if ( ! SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
  98. error = "file truncated";
  99. goto done;
  100. }
  101. pcxh.Xmin = SDL_SwapLE16(pcxh.Xmin);
  102. pcxh.Ymin = SDL_SwapLE16(pcxh.Ymin);
  103. pcxh.Xmax = SDL_SwapLE16(pcxh.Xmax);
  104. pcxh.Ymax = SDL_SwapLE16(pcxh.Ymax);
  105. pcxh.BytesPerLine = SDL_SwapLE16(pcxh.BytesPerLine);
  106. /* Create the surface of the appropriate type */
  107. width = (pcxh.Xmax - pcxh.Xmin) + 1;
  108. height = (pcxh.Ymax - pcxh.Ymin) + 1;
  109. Rmask = Gmask = Bmask = Amask = 0;
  110. src_bits = pcxh.BitsPerPixel * pcxh.NPlanes;
  111. if((pcxh.BitsPerPixel == 1 && pcxh.NPlanes >= 1 && pcxh.NPlanes <= 4)
  112. || (pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 1)) {
  113. bits = 8;
  114. } else if(pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 3) {
  115. bits = 24;
  116. if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
  117. Rmask = 0x000000FF;
  118. Gmask = 0x0000FF00;
  119. Bmask = 0x00FF0000;
  120. } else {
  121. Rmask = 0xFF0000;
  122. Gmask = 0x00FF00;
  123. Bmask = 0x0000FF;
  124. }
  125. } else {
  126. error = "unsupported PCX format";
  127. goto done;
  128. }
  129. surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
  130. bits, Rmask, Gmask, Bmask, Amask);
  131. if ( surface == NULL )
  132. goto done;
  133. bpl = pcxh.NPlanes * pcxh.BytesPerLine;
  134. if (bpl > surface->pitch) {
  135. error = "bytes per line is too large (corrupt?)";
  136. }
  137. buf = malloc(bpl);
  138. row = surface->pixels;
  139. for ( y=0; y<surface->h; ++y ) {
  140. /* decode a scan line to a temporary buffer first */
  141. int i, count = 0;
  142. Uint8 ch;
  143. Uint8 *dst = (src_bits == 8) ? row : buf;
  144. if ( pcxh.Encoding == 0 ) {
  145. if(!SDL_RWread(src, dst, bpl, 1)) {
  146. error = "file truncated";
  147. goto done;
  148. }
  149. } else {
  150. for(i = 0; i < bpl; i++) {
  151. if(!count) {
  152. if(!SDL_RWread(src, &ch, 1, 1)) {
  153. error = "file truncated";
  154. goto done;
  155. }
  156. if( (ch & 0xc0) == 0xc0) {
  157. count = ch & 0x3f;
  158. if(!SDL_RWread(src, &ch, 1, 1)) {
  159. error = "file truncated";
  160. goto done;
  161. }
  162. } else
  163. count = 1;
  164. }
  165. dst[i] = ch;
  166. count--;
  167. }
  168. }
  169. if(src_bits <= 4) {
  170. /* expand planes to 1 byte/pixel */
  171. Uint8 *src = buf;
  172. int plane;
  173. for(plane = 0; plane < pcxh.NPlanes; plane++) {
  174. int i, j, x = 0;
  175. for(i = 0; i < pcxh.BytesPerLine; i++) {
  176. Uint8 byte = *src++;
  177. for(j = 7; j >= 0; j--) {
  178. unsigned bit = (byte >> j) & 1;
  179. /* skip padding bits */
  180. if (i * 8 + j >= width)
  181. continue;
  182. row[x++] |= bit << plane;
  183. }
  184. }
  185. }
  186. } else if(src_bits == 24) {
  187. /* de-interlace planes */
  188. Uint8 *src = buf;
  189. int plane;
  190. for(plane = 0; plane < pcxh.NPlanes; plane++) {
  191. int x;
  192. dst = row + plane;
  193. for(x = 0; x < width; x++) {
  194. *dst = *src++;
  195. dst += pcxh.NPlanes;
  196. }
  197. }
  198. }
  199. row += surface->pitch;
  200. }
  201. if(bits == 8) {
  202. SDL_Color *colors = surface->format->palette->colors;
  203. int nc = 1 << src_bits;
  204. int i;
  205. surface->format->palette->ncolors = nc;
  206. if(src_bits == 8) {
  207. Uint8 ch;
  208. /* look for a 256-colour palette */
  209. do {
  210. if ( !SDL_RWread(src, &ch, 1, 1)) {
  211. error = "file truncated";
  212. goto done;
  213. }
  214. } while ( ch != 12 );
  215. for(i = 0; i < 256; i++) {
  216. SDL_RWread(src, &colors[i].r, 1, 1);
  217. SDL_RWread(src, &colors[i].g, 1, 1);
  218. SDL_RWread(src, &colors[i].b, 1, 1);
  219. }
  220. } else {
  221. for(i = 0; i < nc; i++) {
  222. colors[i].r = pcxh.Colormap[i * 3];
  223. colors[i].g = pcxh.Colormap[i * 3 + 1];
  224. colors[i].b = pcxh.Colormap[i * 3 + 2];
  225. }
  226. }
  227. }
  228. done:
  229. free(buf);
  230. if ( error ) {
  231. SDL_RWseek(src, start, RW_SEEK_SET);
  232. if ( surface ) {
  233. SDL_FreeSurface(surface);
  234. surface = NULL;
  235. }
  236. IMG_SetError(error);
  237. }
  238. return(surface);
  239. }
  240. #else
  241. /* See if an image is contained in a data source */
  242. int IMG_isPCX(SDL_RWops *src)
  243. {
  244. return(0);
  245. }
  246. /* Load a PCX type image from an SDL datasource */
  247. SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
  248. {
  249. return(NULL);
  250. }
  251. #endif /* LOAD_PCX */