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 
31 #define PCX_HEADER_SIZE 128
32 
37  uint8_t *dst,
38  unsigned int bytes_per_scanline,
39  int compressed)
40 {
41  unsigned int i = 0;
42  unsigned char run, value;
43 
44  if (compressed) {
45  while (i < bytes_per_scanline && bytestream2_get_bytes_left(gb)) {
46  run = 1;
47  value = bytestream2_get_byte(gb);
48  if (value >= 0xc0 && bytestream2_get_bytes_left(gb)) {
49  run = value & 0x3f;
50  value = bytestream2_get_byte(gb);
51  }
52  while (i < bytes_per_scanline && run--)
53  dst[i++] = value;
54  }
55  } else {
56  bytestream2_get_buffer(gb, dst, bytes_per_scanline);
57  }
58 }
59 
60 static void pcx_palette(GetByteContext *gb, uint32_t *dst,
61  unsigned int pallen)
62 {
63  unsigned int i;
64 
65  for (i = 0; i < pallen; i++)
66  *dst++ = bytestream2_get_be24(gb);
67  if (pallen < 256)
68  memset(dst, 0, (256 - pallen) * sizeof(*dst));
69 }
70 
71 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
72  AVPacket *avpkt)
73 {
74  const uint8_t *buf = avpkt->data;
75  int buf_size = avpkt->size;
76  AVFrame *const p = data;
77  GetByteContext gb;
78  int compressed, xmin, ymin, xmax, ymax;
79  unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
80  bytes_per_scanline;
81  uint8_t *ptr;
82  uint8_t *scanline;
83  int ret = -1;
84 
85  if (buf_size < PCX_HEADER_SIZE) {
86  av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
87  return AVERROR_INVALIDDATA;
88  }
89 
90  if (buf[0] != 0x0a || buf[1] > 5) {
91  av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
92  return AVERROR_INVALIDDATA;
93  }
94 
95  compressed = buf[2];
96  xmin = AV_RL16(buf + 4);
97  ymin = AV_RL16(buf + 6);
98  xmax = AV_RL16(buf + 8);
99  ymax = AV_RL16(buf + 10);
100 
101  if (xmax < xmin || ymax < ymin) {
102  av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
103  return AVERROR_INVALIDDATA;
104  }
105 
106  w = xmax - xmin + 1;
107  h = ymax - ymin + 1;
108 
109  bits_per_pixel = buf[3];
110  bytes_per_line = AV_RL16(buf + 66);
111  nplanes = buf[65];
112  bytes_per_scanline = nplanes * bytes_per_line;
113 
114  if (bytes_per_scanline < (w * bits_per_pixel * nplanes + 7) / 8 ||
115  (!compressed && bytes_per_scanline > buf_size / h)) {
116  av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
117  return AVERROR_INVALIDDATA;
118  }
119 
120  switch ((nplanes << 8) + bits_per_pixel) {
121  case 0x0308:
122  avctx->pix_fmt = AV_PIX_FMT_RGB24;
123  break;
124  case 0x0108:
125  case 0x0104:
126  case 0x0102:
127  case 0x0101:
128  case 0x0401:
129  case 0x0301:
130  case 0x0201:
131  avctx->pix_fmt = AV_PIX_FMT_PAL8;
132  break;
133  default:
134  av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
135  return AVERROR_INVALIDDATA;
136  }
137 
138  bytestream2_init(&gb, buf + PCX_HEADER_SIZE, buf_size - PCX_HEADER_SIZE);
139 
140  if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
141  return ret;
142 
143  if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
144  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
145  return ret;
146  }
147 
149 
150  ptr = p->data[0];
151  stride = p->linesize[0];
152 
153  scanline = av_malloc(bytes_per_scanline + FF_INPUT_BUFFER_PADDING_SIZE);
154  if (!scanline)
155  return AVERROR(ENOMEM);
156 
157  if (nplanes == 3 && bits_per_pixel == 8) {
158  for (y = 0; y < h; y++) {
159  pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
160 
161  for (x = 0; x < w; x++) {
162  ptr[3 * x] = scanline[x];
163  ptr[3 * x + 1] = scanline[x + bytes_per_line];
164  ptr[3 * x + 2] = scanline[x + (bytes_per_line << 1)];
165  }
166 
167  ptr += stride;
168  }
169  } else if (nplanes == 1 && bits_per_pixel == 8) {
170  for (y = 0; y < h; y++, ptr += stride) {
171  pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
172  memcpy(ptr, scanline, w);
173  }
174 
175  if (bytestream2_get_byte(&gb) != 12) {
176  av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
177  ret = avctx->err_recognition & AV_EF_EXPLODE ?
178  AVERROR_INVALIDDATA : buf_size;
179  goto end;
180  }
181  } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
182  GetBitContext s;
183 
184  for (y = 0; y < h; y++) {
185  init_get_bits(&s, scanline, bytes_per_scanline << 3);
186 
187  pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
188 
189  for (x = 0; x < w; x++)
190  ptr[x] = get_bits(&s, bits_per_pixel);
191  ptr += stride;
192  }
193  } else { /* planar, 4, 8 or 16 colors */
194  int i;
195 
196  for (y = 0; y < h; y++) {
197  pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
198 
199  for (x = 0; x < w; x++) {
200  int m = 0x80 >> (x & 7), v = 0;
201  for (i = nplanes - 1; i >= 0; i--) {
202  v <<= 1;
203  v += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
204  }
205  ptr[x] = v;
206  }
207  ptr += stride;
208  }
209  }
210 
211  if (nplanes == 1 && bits_per_pixel == 8) {
212  if (bytestream2_get_bytes_left(&gb) < 768) {
213  av_log(avctx, AV_LOG_ERROR, "Palette truncated\n");
214  ret = AVERROR_INVALIDDATA;
215  goto end;
216  }
217 
218  pcx_palette(&gb, (uint32_t *)p->data[1], 256);
219  } else if (bits_per_pixel < 8) {
220  GetByteContext gb1;
221  bytestream2_init(&gb1, avpkt->data + 16, 48);
222  pcx_palette(&gb1, (uint32_t *)p->data[1], 16);
223  }
224 
225  *got_frame = 1;
226 
227  ret = bytestream2_tell(&gb);
228 end:
229  av_free(scanline);
230  return ret;
231 }
232 
234  .name = "pcx",
235  .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
236  .type = AVMEDIA_TYPE_VIDEO,
237  .id = AV_CODEC_ID_PCX,
238  .decode = pcx_decode_frame,
239  .capabilities = CODEC_CAP_DR1,
240 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:133
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
#define AV_RL16
Definition: intreadwrite.h:42
AVCodec ff_pcx_decoder
Definition: pcx.c:233
uint8_t run
Definition: svq3.c:146
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2812
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define PCX_HEADER_SIZE
Definition: pcx.c:31
static void pcx_palette(GetByteContext *gb, uint32_t *dst, unsigned int pallen)
Definition: pcx.c:60
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
bitstream reader API header.
static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: pcx.c:71
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:260
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:151
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2422
#define AV_EF_EXPLODE
Definition: avcodec.h:2433
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:185
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
static void pcx_rle_decode(GetByteContext *gb, uint8_t *dst, unsigned int bytes_per_scanline, int compressed)
Definition: pcx.c:36
common internal api header.
This structure stores compressed data.
Definition: avcodec.h:950