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 
30 typedef struct PCXContext {
32 } PCXContext;
33 
34 static av_cold int pcx_init(AVCodecContext *avctx) {
35  PCXContext *s = avctx->priv_data;
36 
38  avctx->coded_frame= &s->picture;
39 
40  return 0;
41 }
42 
46 static const uint8_t *pcx_rle_decode(const uint8_t *src,
47  const uint8_t *end,
48  uint8_t *dst,
49  unsigned int bytes_per_scanline,
50  int compressed) {
51  unsigned int i = 0;
52  unsigned char run, value;
53 
54  if (compressed) {
55  while (i < bytes_per_scanline && src < end) {
56  run = 1;
57  value = *src++;
58  if (value >= 0xc0 && src < end) {
59  run = value & 0x3f;
60  value = *src++;
61  }
62  while (i<bytes_per_scanline && run--)
63  dst[i++] = value;
64  }
65  } else {
66  memcpy(dst, src, bytes_per_scanline);
67  src += bytes_per_scanline;
68  }
69 
70  return src;
71 }
72 
73 static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
74  unsigned int i;
75 
76  for (i=0; i<pallen; i++)
77  *dst++ = bytestream_get_be24(src);
78  if (pallen < 256)
79  memset(dst, 0, (256 - pallen) * sizeof(*dst));
80 }
81 
82 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
83  AVPacket *avpkt) {
84  const uint8_t *buf = avpkt->data;
85  int buf_size = avpkt->size;
86  PCXContext * const s = avctx->priv_data;
87  AVFrame *picture = data;
88  AVFrame * const p = &s->picture;
89  int compressed, xmin, ymin, xmax, ymax;
90  unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
91  bytes_per_scanline;
92  uint8_t *ptr;
93  const uint8_t *buf_end = buf + buf_size;
94  uint8_t const *bufstart = buf;
95  uint8_t *scanline;
96  int ret = -1;
97 
98  if (buf[0] != 0x0a || buf[1] > 5) {
99  av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
100  return -1;
101  }
102 
103  compressed = buf[2];
104  xmin = AV_RL16(buf+ 4);
105  ymin = AV_RL16(buf+ 6);
106  xmax = AV_RL16(buf+ 8);
107  ymax = AV_RL16(buf+10);
108 
109  if (xmax < xmin || ymax < ymin) {
110  av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
111  return -1;
112  }
113 
114  w = xmax - xmin + 1;
115  h = ymax - ymin + 1;
116 
117  bits_per_pixel = buf[3];
118  bytes_per_line = AV_RL16(buf+66);
119  nplanes = buf[65];
120  bytes_per_scanline = nplanes * bytes_per_line;
121 
122  if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8 ||
123  (!compressed && bytes_per_scanline > buf_size / h)) {
124  av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
125  return -1;
126  }
127 
128  switch ((nplanes<<8) + bits_per_pixel) {
129  case 0x0308:
130  avctx->pix_fmt = PIX_FMT_RGB24;
131  break;
132  case 0x0108:
133  case 0x0104:
134  case 0x0102:
135  case 0x0101:
136  case 0x0401:
137  case 0x0301:
138  case 0x0201:
139  avctx->pix_fmt = PIX_FMT_PAL8;
140  break;
141  default:
142  av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
143  return -1;
144  }
145 
146  buf += 128;
147 
148  if (p->data[0])
149  avctx->release_buffer(avctx, p);
150 
151  if (av_image_check_size(w, h, 0, avctx))
152  return -1;
153  if (w != avctx->width || h != avctx->height)
154  avcodec_set_dimensions(avctx, w, h);
155  if (avctx->get_buffer(avctx, p) < 0) {
156  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
157  return -1;
158  }
159 
161 
162  ptr = p->data[0];
163  stride = p->linesize[0];
164 
165  scanline = av_malloc(bytes_per_scanline);
166  if (!scanline)
167  return AVERROR(ENOMEM);
168 
169  if (nplanes == 3 && bits_per_pixel == 8) {
170  for (y=0; y<h; y++) {
171  buf = pcx_rle_decode(buf, buf_end,
172  scanline, bytes_per_scanline, compressed);
173 
174  for (x=0; x<w; x++) {
175  ptr[3*x ] = scanline[x ];
176  ptr[3*x+1] = scanline[x+ bytes_per_line ];
177  ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
178  }
179 
180  ptr += stride;
181  }
182 
183  } else if (nplanes == 1 && bits_per_pixel == 8) {
184  const uint8_t *palstart = bufstart + buf_size - 769;
185 
186  if (buf_size < 769) {
187  av_log(avctx, AV_LOG_ERROR, "File is too short\n");
188  ret = buf_size;
189  goto end;
190  }
191 
192  for (y = 0; y < h; y++, ptr += stride) {
193  buf = pcx_rle_decode(buf, buf_end,
194  scanline, bytes_per_scanline, compressed);
195  memcpy(ptr, scanline, w);
196  }
197 
198  if (buf != palstart) {
199  av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
200  buf = palstart;
201  }
202  if (*buf++ != 12) {
203  av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
204  ret = buf_size;
205  goto end;
206  }
207 
208  } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
209  GetBitContext s;
210 
211  for (y=0; y<h; y++) {
212  init_get_bits(&s, scanline, bytes_per_scanline<<3);
213 
214  buf = pcx_rle_decode(buf, buf_end,
215  scanline, bytes_per_scanline, compressed);
216 
217  for (x=0; x<w; x++)
218  ptr[x] = get_bits(&s, bits_per_pixel);
219  ptr += stride;
220  }
221 
222  } else { /* planar, 4, 8 or 16 colors */
223  int i;
224 
225  for (y=0; y<h; y++) {
226  buf = pcx_rle_decode(buf, buf_end,
227  scanline, bytes_per_scanline, compressed);
228 
229  for (x=0; x<w; x++) {
230  int m = 0x80 >> (x&7), v = 0;
231  for (i=nplanes - 1; i>=0; i--) {
232  v <<= 1;
233  v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
234  }
235  ptr[x] = v;
236  }
237  ptr += stride;
238  }
239  }
240 
241  if (nplanes == 1 && bits_per_pixel == 8) {
242  pcx_palette(&buf, (uint32_t *) p->data[1], 256);
243  } else if (bits_per_pixel < 8) {
244  const uint8_t *palette = bufstart+16;
245  pcx_palette(&palette, (uint32_t *) p->data[1], 16);
246  }
247 
248  *picture = s->picture;
249  *data_size = sizeof(AVFrame);
250 
251  ret = buf - bufstart;
252 end:
253  av_free(scanline);
254  return ret;
255 }
256 
257 static av_cold int pcx_end(AVCodecContext *avctx) {
258  PCXContext *s = avctx->priv_data;
259 
260  if(s->picture.data[0])
261  avctx->release_buffer(avctx, &s->picture);
262 
263  return 0;
264 }
265 
267  .name = "pcx",
268  .type = AVMEDIA_TYPE_VIDEO,
269  .id = CODEC_ID_PCX,
270  .priv_data_size = sizeof(PCXContext),
271  .init = pcx_init,
272  .close = pcx_end,
274  .capabilities = CODEC_CAP_DR1,
275  .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
276 };