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