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.1KB

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