pcx.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "bitstream.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) {
  41. unsigned int i = 0;
  42. unsigned char run, value;
  43. while (i<bytes_per_scanline) {
  44. run = 1;
  45. value = *src++;
  46. if (value >= 0xc0) {
  47. run = value & 0x3f;
  48. value = *src++;
  49. }
  50. while (i<bytes_per_scanline && run--)
  51. dst[i++] = value;
  52. }
  53. return src;
  54. }
  55. static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
  56. unsigned int i;
  57. for (i=0; i<pallen; i++)
  58. *dst++ = bytestream_get_be24(src);
  59. memset(dst, 0, (256 - pallen) * sizeof(*dst));
  60. }
  61. static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  62. const uint8_t *buf, int buf_size) {
  63. PCXContext * const s = avctx->priv_data;
  64. AVFrame *picture = data;
  65. AVFrame * const p = &s->picture;
  66. int xmin, ymin, xmax, ymax;
  67. unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
  68. bytes_per_scanline;
  69. uint8_t *ptr;
  70. uint8_t const *bufstart = buf;
  71. if (buf[0] != 0x0a || buf[1] > 5 || buf[1] == 1 || buf[2] != 1) {
  72. av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
  73. return -1;
  74. }
  75. xmin = AV_RL16(buf+ 4);
  76. ymin = AV_RL16(buf+ 6);
  77. xmax = AV_RL16(buf+ 8);
  78. ymax = AV_RL16(buf+10);
  79. if (xmax < xmin || ymax < ymin) {
  80. av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
  81. return -1;
  82. }
  83. w = xmax - xmin + 1;
  84. h = ymax - ymin + 1;
  85. bits_per_pixel = buf[3];
  86. bytes_per_line = AV_RL16(buf+66);
  87. nplanes = buf[65];
  88. bytes_per_scanline = nplanes * bytes_per_line;
  89. if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
  90. av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
  91. return -1;
  92. }
  93. switch ((nplanes<<8) + bits_per_pixel) {
  94. case 0x0308:
  95. avctx->pix_fmt = PIX_FMT_RGB24;
  96. break;
  97. case 0x0108:
  98. case 0x0104:
  99. case 0x0102:
  100. case 0x0101:
  101. case 0x0401:
  102. case 0x0301:
  103. case 0x0201:
  104. avctx->pix_fmt = PIX_FMT_PAL8;
  105. break;
  106. default:
  107. av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
  108. return -1;
  109. }
  110. buf += 128;
  111. if (p->data[0])
  112. avctx->release_buffer(avctx, p);
  113. if (avcodec_check_dimensions(avctx, w, h))
  114. return -1;
  115. if (w != avctx->width || h != avctx->height)
  116. avcodec_set_dimensions(avctx, w, h);
  117. if (avctx->get_buffer(avctx, p) < 0) {
  118. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  119. return -1;
  120. }
  121. p->pict_type = FF_I_TYPE;
  122. ptr = p->data[0];
  123. stride = p->linesize[0];
  124. if (nplanes == 3 && bits_per_pixel == 8) {
  125. uint8_t scanline[bytes_per_scanline];
  126. for (y=0; y<h; y++) {
  127. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  128. for (x=0; x<w; x++) {
  129. ptr[3*x ] = scanline[x ];
  130. ptr[3*x+1] = scanline[x+ bytes_per_line ];
  131. ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
  132. }
  133. ptr += stride;
  134. }
  135. } else if (nplanes == 1 && bits_per_pixel == 8) {
  136. uint8_t scanline[bytes_per_scanline];
  137. const uint8_t *palstart = bufstart + buf_size - 769;
  138. for (y=0; y<h; y++, ptr+=stride) {
  139. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  140. memcpy(ptr, scanline, w);
  141. }
  142. if (buf != palstart) {
  143. av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
  144. buf = palstart;
  145. }
  146. if (*buf++ != 12) {
  147. av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
  148. return -1;
  149. }
  150. } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
  151. uint8_t scanline[bytes_per_scanline];
  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);
  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. uint8_t scanline[bytes_per_scanline];
  162. int i;
  163. for (y=0; y<h; y++) {
  164. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  165. for (x=0; x<w; x++) {
  166. int m = 0x80 >> (x&7), v = 0;
  167. for (i=nplanes - 1; i>=0; i--) {
  168. v <<= 1;
  169. v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
  170. }
  171. ptr[x] = v;
  172. }
  173. ptr += stride;
  174. }
  175. }
  176. if (nplanes == 1 && bits_per_pixel == 8) {
  177. pcx_palette(&buf, (uint32_t *) p->data[1], 256);
  178. } else if (bits_per_pixel < 8) {
  179. const uint8_t *palette = bufstart+16;
  180. pcx_palette(&palette, (uint32_t *) p->data[1], 16);
  181. }
  182. *picture = s->picture;
  183. *data_size = sizeof(AVFrame);
  184. return buf - bufstart;
  185. }
  186. static av_cold int pcx_end(AVCodecContext *avctx) {
  187. PCXContext *s = avctx->priv_data;
  188. if(s->picture.data[0])
  189. avctx->release_buffer(avctx, &s->picture);
  190. return 0;
  191. }
  192. AVCodec pcx_decoder = {
  193. "pcx",
  194. CODEC_TYPE_VIDEO,
  195. CODEC_ID_PCX,
  196. sizeof(PCXContext),
  197. pcx_init,
  198. NULL,
  199. pcx_end,
  200. pcx_decode_frame,
  201. 0,
  202. NULL,
  203. .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
  204. };