You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
7.2KB

  1. /*
  2. * PC Paintbrush PCX (.pcx) image decoder
  3. * Copyright (c) 2007, 2008 Ivo van Poorten
  4. *
  5. * This decoder does not support CGA palettes. I am unable to find samples
  6. * and Netpbm cannot generate them.
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "get_bits.h"
  27. typedef struct PCXContext {
  28. AVFrame picture;
  29. } PCXContext;
  30. static av_cold int pcx_init(AVCodecContext *avctx) {
  31. PCXContext *s = avctx->priv_data;
  32. avcodec_get_frame_defaults(&s->picture);
  33. avctx->coded_frame= &s->picture;
  34. return 0;
  35. }
  36. /**
  37. * @return advanced src pointer
  38. */
  39. static const uint8_t *pcx_rle_decode(const uint8_t *src, uint8_t *dst,
  40. unsigned int bytes_per_scanline, int compressed) {
  41. unsigned int i = 0;
  42. unsigned char run, value;
  43. if (compressed) {
  44. while (i<bytes_per_scanline) {
  45. run = 1;
  46. value = *src++;
  47. if (value >= 0xc0) {
  48. run = value & 0x3f;
  49. value = *src++;
  50. }
  51. while (i<bytes_per_scanline && run--)
  52. dst[i++] = value;
  53. }
  54. } else {
  55. memcpy(dst, src, bytes_per_scanline);
  56. src += bytes_per_scanline;
  57. }
  58. return src;
  59. }
  60. static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
  61. unsigned int i;
  62. for (i=0; i<pallen; i++)
  63. *dst++ = bytestream_get_be24(src);
  64. if (pallen < 256)
  65. memset(dst, 0, (256 - pallen) * sizeof(*dst));
  66. }
  67. static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  68. AVPacket *avpkt) {
  69. const uint8_t *buf = avpkt->data;
  70. int buf_size = avpkt->size;
  71. PCXContext * const s = avctx->priv_data;
  72. AVFrame *picture = data;
  73. AVFrame * const p = &s->picture;
  74. int compressed, xmin, ymin, xmax, ymax;
  75. unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
  76. bytes_per_scanline;
  77. uint8_t *ptr;
  78. uint8_t const *bufstart = buf;
  79. if (buf[0] != 0x0a || buf[1] > 5) {
  80. av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
  81. return -1;
  82. }
  83. compressed = buf[2];
  84. xmin = AV_RL16(buf+ 4);
  85. ymin = AV_RL16(buf+ 6);
  86. xmax = AV_RL16(buf+ 8);
  87. ymax = AV_RL16(buf+10);
  88. if (xmax < xmin || ymax < ymin) {
  89. av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
  90. return -1;
  91. }
  92. w = xmax - xmin + 1;
  93. h = ymax - ymin + 1;
  94. bits_per_pixel = buf[3];
  95. bytes_per_line = AV_RL16(buf+66);
  96. nplanes = buf[65];
  97. bytes_per_scanline = nplanes * bytes_per_line;
  98. if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
  99. av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
  100. return -1;
  101. }
  102. switch ((nplanes<<8) + bits_per_pixel) {
  103. case 0x0308:
  104. avctx->pix_fmt = PIX_FMT_RGB24;
  105. break;
  106. case 0x0108:
  107. case 0x0104:
  108. case 0x0102:
  109. case 0x0101:
  110. case 0x0401:
  111. case 0x0301:
  112. case 0x0201:
  113. avctx->pix_fmt = PIX_FMT_PAL8;
  114. break;
  115. default:
  116. av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
  117. return -1;
  118. }
  119. buf += 128;
  120. if (p->data[0])
  121. avctx->release_buffer(avctx, p);
  122. if (avcodec_check_dimensions(avctx, w, h))
  123. return -1;
  124. if (w != avctx->width || h != avctx->height)
  125. avcodec_set_dimensions(avctx, w, h);
  126. if (avctx->get_buffer(avctx, p) < 0) {
  127. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  128. return -1;
  129. }
  130. p->pict_type = FF_I_TYPE;
  131. ptr = p->data[0];
  132. stride = p->linesize[0];
  133. if (nplanes == 3 && bits_per_pixel == 8) {
  134. uint8_t scanline[bytes_per_scanline];
  135. for (y=0; y<h; y++) {
  136. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
  137. for (x=0; x<w; x++) {
  138. ptr[3*x ] = scanline[x ];
  139. ptr[3*x+1] = scanline[x+ bytes_per_line ];
  140. ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
  141. }
  142. ptr += stride;
  143. }
  144. } else if (nplanes == 1 && bits_per_pixel == 8) {
  145. uint8_t scanline[bytes_per_scanline];
  146. const uint8_t *palstart = bufstart + buf_size - 769;
  147. for (y=0; y<h; y++, ptr+=stride) {
  148. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
  149. memcpy(ptr, scanline, w);
  150. }
  151. if (buf != palstart) {
  152. av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
  153. buf = palstart;
  154. }
  155. if (*buf++ != 12) {
  156. av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
  157. return -1;
  158. }
  159. } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
  160. uint8_t scanline[bytes_per_scanline];
  161. GetBitContext s;
  162. for (y=0; y<h; y++) {
  163. init_get_bits(&s, scanline, bytes_per_scanline<<3);
  164. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
  165. for (x=0; x<w; x++)
  166. ptr[x] = get_bits(&s, bits_per_pixel);
  167. ptr += stride;
  168. }
  169. } else { /* planar, 4, 8 or 16 colors */
  170. uint8_t scanline[bytes_per_scanline];
  171. int i;
  172. for (y=0; y<h; y++) {
  173. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
  174. for (x=0; x<w; x++) {
  175. int m = 0x80 >> (x&7), v = 0;
  176. for (i=nplanes - 1; i>=0; i--) {
  177. v <<= 1;
  178. v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
  179. }
  180. ptr[x] = v;
  181. }
  182. ptr += stride;
  183. }
  184. }
  185. if (nplanes == 1 && bits_per_pixel == 8) {
  186. pcx_palette(&buf, (uint32_t *) p->data[1], 256);
  187. } else if (bits_per_pixel < 8) {
  188. const uint8_t *palette = bufstart+16;
  189. pcx_palette(&palette, (uint32_t *) p->data[1], 16);
  190. }
  191. *picture = s->picture;
  192. *data_size = sizeof(AVFrame);
  193. return buf - bufstart;
  194. }
  195. static av_cold int pcx_end(AVCodecContext *avctx) {
  196. PCXContext *s = avctx->priv_data;
  197. if(s->picture.data[0])
  198. avctx->release_buffer(avctx, &s->picture);
  199. return 0;
  200. }
  201. AVCodec pcx_decoder = {
  202. "pcx",
  203. CODEC_TYPE_VIDEO,
  204. CODEC_ID_PCX,
  205. sizeof(PCXContext),
  206. pcx_init,
  207. NULL,
  208. pcx_end,
  209. pcx_decode_frame,
  210. CODEC_CAP_DR1,
  211. NULL,
  212. .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
  213. };