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.

264 lines
7.5KB

  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 "libavutil/imgutils.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "get_bits.h"
  28. typedef struct PCXContext {
  29. AVFrame picture;
  30. } PCXContext;
  31. static av_cold int pcx_init(AVCodecContext *avctx) {
  32. PCXContext *s = avctx->priv_data;
  33. avcodec_get_frame_defaults(&s->picture);
  34. avctx->coded_frame= &s->picture;
  35. return 0;
  36. }
  37. /**
  38. * @return advanced src pointer
  39. */
  40. static const uint8_t *pcx_rle_decode(const uint8_t *src, uint8_t *dst,
  41. unsigned int bytes_per_scanline, int compressed) {
  42. unsigned int i = 0;
  43. unsigned char run, value;
  44. if (compressed) {
  45. while (i<bytes_per_scanline) {
  46. run = 1;
  47. value = *src++;
  48. if (value >= 0xc0) {
  49. run = value & 0x3f;
  50. value = *src++;
  51. }
  52. while (i<bytes_per_scanline && run--)
  53. dst[i++] = value;
  54. }
  55. } else {
  56. memcpy(dst, src, bytes_per_scanline);
  57. src += bytes_per_scanline;
  58. }
  59. return src;
  60. }
  61. static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
  62. unsigned int i;
  63. for (i=0; i<pallen; i++)
  64. *dst++ = 0xFF000000 | bytestream_get_be24(src);
  65. if (pallen < 256)
  66. memset(dst, 0, (256 - pallen) * sizeof(*dst));
  67. }
  68. static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  69. AVPacket *avpkt) {
  70. const uint8_t *buf = avpkt->data;
  71. int buf_size = avpkt->size;
  72. PCXContext * const s = avctx->priv_data;
  73. AVFrame *picture = data;
  74. AVFrame * const p = &s->picture;
  75. int compressed, xmin, ymin, xmax, ymax;
  76. unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
  77. bytes_per_scanline;
  78. uint8_t *ptr;
  79. uint8_t const *bufstart = buf;
  80. uint8_t *scanline;
  81. int ret = -1;
  82. if (buf[0] != 0x0a || buf[1] > 5) {
  83. av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
  84. return -1;
  85. }
  86. compressed = buf[2];
  87. xmin = AV_RL16(buf+ 4);
  88. ymin = AV_RL16(buf+ 6);
  89. xmax = AV_RL16(buf+ 8);
  90. ymax = AV_RL16(buf+10);
  91. if (xmax < xmin || ymax < ymin) {
  92. av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
  93. return -1;
  94. }
  95. w = xmax - xmin + 1;
  96. h = ymax - ymin + 1;
  97. bits_per_pixel = buf[3];
  98. bytes_per_line = AV_RL16(buf+66);
  99. nplanes = buf[65];
  100. bytes_per_scanline = nplanes * bytes_per_line;
  101. if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
  102. av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
  103. return -1;
  104. }
  105. switch ((nplanes<<8) + bits_per_pixel) {
  106. case 0x0308:
  107. avctx->pix_fmt = PIX_FMT_RGB24;
  108. break;
  109. case 0x0108:
  110. case 0x0104:
  111. case 0x0102:
  112. case 0x0101:
  113. case 0x0401:
  114. case 0x0301:
  115. case 0x0201:
  116. avctx->pix_fmt = PIX_FMT_PAL8;
  117. break;
  118. default:
  119. av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
  120. return -1;
  121. }
  122. buf += 128;
  123. if (p->data[0])
  124. avctx->release_buffer(avctx, p);
  125. if (av_image_check_size(w, h, 0, avctx))
  126. return -1;
  127. if (w != avctx->width || h != avctx->height)
  128. avcodec_set_dimensions(avctx, w, h);
  129. if (avctx->get_buffer(avctx, p) < 0) {
  130. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  131. return -1;
  132. }
  133. p->pict_type = AV_PICTURE_TYPE_I;
  134. ptr = p->data[0];
  135. stride = p->linesize[0];
  136. scanline = av_malloc(bytes_per_scanline);
  137. if (!scanline)
  138. return AVERROR(ENOMEM);
  139. if (nplanes == 3 && bits_per_pixel == 8) {
  140. for (y=0; y<h; y++) {
  141. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
  142. for (x=0; x<w; x++) {
  143. ptr[3*x ] = scanline[x ];
  144. ptr[3*x+1] = scanline[x+ bytes_per_line ];
  145. ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
  146. }
  147. ptr += stride;
  148. }
  149. } else if (nplanes == 1 && bits_per_pixel == 8) {
  150. const uint8_t *palstart = bufstart + buf_size - 769;
  151. for (y=0; y<h; y++, ptr+=stride) {
  152. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
  153. memcpy(ptr, scanline, w);
  154. }
  155. if (buf != palstart) {
  156. av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
  157. buf = palstart;
  158. }
  159. if (*buf++ != 12) {
  160. av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
  161. goto end;
  162. }
  163. } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
  164. GetBitContext s;
  165. for (y=0; y<h; y++) {
  166. init_get_bits(&s, scanline, bytes_per_scanline<<3);
  167. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
  168. for (x=0; x<w; x++)
  169. ptr[x] = get_bits(&s, bits_per_pixel);
  170. ptr += stride;
  171. }
  172. } else { /* planar, 4, 8 or 16 colors */
  173. int i;
  174. for (y=0; y<h; y++) {
  175. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
  176. for (x=0; x<w; x++) {
  177. int m = 0x80 >> (x&7), v = 0;
  178. for (i=nplanes - 1; i>=0; i--) {
  179. v <<= 1;
  180. v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
  181. }
  182. ptr[x] = v;
  183. }
  184. ptr += stride;
  185. }
  186. }
  187. if (nplanes == 1 && bits_per_pixel == 8) {
  188. pcx_palette(&buf, (uint32_t *) p->data[1], 256);
  189. } else if (bits_per_pixel * nplanes == 1) {
  190. AV_WN32A(p->data[1] , 0xFF000000);
  191. AV_WN32A(p->data[1]+4, 0xFFFFFFFF);
  192. } else if (bits_per_pixel < 8) {
  193. const uint8_t *palette = bufstart+16;
  194. pcx_palette(&palette, (uint32_t *) p->data[1], 16);
  195. }
  196. *picture = s->picture;
  197. *data_size = sizeof(AVFrame);
  198. ret = buf - bufstart;
  199. end:
  200. av_free(scanline);
  201. return ret;
  202. }
  203. static av_cold int pcx_end(AVCodecContext *avctx) {
  204. PCXContext *s = avctx->priv_data;
  205. if(s->picture.data[0])
  206. avctx->release_buffer(avctx, &s->picture);
  207. return 0;
  208. }
  209. AVCodec ff_pcx_decoder = {
  210. .name = "pcx",
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .id = AV_CODEC_ID_PCX,
  213. .priv_data_size = sizeof(PCXContext),
  214. .init = pcx_init,
  215. .close = pcx_end,
  216. .decode = pcx_decode_frame,
  217. .capabilities = CODEC_CAP_DR1,
  218. .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
  219. };