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.

231 lines
6.7KB

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