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