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.

249 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. /**
  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. const uint8_t *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 + 7) / 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 = ff_set_dimensions(avctx, w, h)) < 0)
  123. return ret;
  124. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  125. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  126. return ret;
  127. }
  128. p->pict_type = AV_PICTURE_TYPE_I;
  129. ptr = p->data[0];
  130. stride = p->linesize[0];
  131. scanline = av_malloc(bytes_per_scanline);
  132. if (!scanline)
  133. return AVERROR(ENOMEM);
  134. if (nplanes == 3 && bits_per_pixel == 8) {
  135. for (y = 0; y < h; y++) {
  136. buf = pcx_rle_decode(buf, buf_end,
  137. scanline, bytes_per_scanline, compressed);
  138. for (x = 0; x < w; x++) {
  139. ptr[3 * x] = scanline[x];
  140. ptr[3 * x + 1] = scanline[x + bytes_per_line];
  141. ptr[3 * x + 2] = scanline[x + (bytes_per_line << 1)];
  142. }
  143. ptr += stride;
  144. }
  145. } else if (nplanes == 1 && bits_per_pixel == 8) {
  146. const uint8_t *palstart = bufstart + buf_size - 769;
  147. if (buf_size < 769) {
  148. av_log(avctx, AV_LOG_ERROR, "File is too short\n");
  149. ret = avctx->err_recognition & AV_EF_EXPLODE ?
  150. AVERROR_INVALIDDATA : buf_size;
  151. goto end;
  152. }
  153. for (y = 0; y < h; y++, ptr += stride) {
  154. buf = pcx_rle_decode(buf, buf_end,
  155. scanline, bytes_per_scanline, compressed);
  156. memcpy(ptr, scanline, w);
  157. }
  158. if (buf != palstart) {
  159. av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
  160. buf = palstart;
  161. }
  162. if (*buf++ != 12) {
  163. av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
  164. ret = avctx->err_recognition & AV_EF_EXPLODE ?
  165. AVERROR_INVALIDDATA : buf_size;
  166. goto end;
  167. }
  168. } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
  169. GetBitContext s;
  170. for (y = 0; y < h; y++) {
  171. init_get_bits(&s, scanline, bytes_per_scanline << 3);
  172. buf = pcx_rle_decode(buf, buf_end,
  173. scanline, bytes_per_scanline, compressed);
  174. for (x = 0; x < w; x++)
  175. ptr[x] = get_bits(&s, bits_per_pixel);
  176. ptr += stride;
  177. }
  178. } else { /* planar, 4, 8 or 16 colors */
  179. int i;
  180. for (y = 0; y < h; y++) {
  181. buf = pcx_rle_decode(buf, buf_end,
  182. scanline, bytes_per_scanline, compressed);
  183. for (x = 0; x < w; x++) {
  184. int m = 0x80 >> (x & 7), v = 0;
  185. for (i = nplanes - 1; i >= 0; i--) {
  186. v <<= 1;
  187. v += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
  188. }
  189. ptr[x] = v;
  190. }
  191. ptr += stride;
  192. }
  193. }
  194. if (nplanes == 1 && bits_per_pixel == 8) {
  195. pcx_palette(&buf, (uint32_t *)p->data[1], 256);
  196. } else if (bits_per_pixel < 8) {
  197. const uint8_t *palette = bufstart + 16;
  198. pcx_palette(&palette, (uint32_t *)p->data[1], 16);
  199. }
  200. *got_frame = 1;
  201. ret = buf - bufstart;
  202. end:
  203. av_free(scanline);
  204. return ret;
  205. }
  206. AVCodec ff_pcx_decoder = {
  207. .name = "pcx",
  208. .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
  209. .type = AVMEDIA_TYPE_VIDEO,
  210. .id = AV_CODEC_ID_PCX,
  211. .decode = pcx_decode_frame,
  212. .capabilities = AV_CODEC_CAP_DR1,
  213. };