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.

262 lines
7.4KB

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