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.

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