2 * PC Paintbrush PCX (.pcx) image decoder
3 * Copyright (c) 2007, 2008 Ivo van Poorten
5 * This decoder does not support CGA palettes. I am unable to find samples
6 * and Netpbm cannot generate them.
8 * This file is part of Libav.
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.
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.
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
25 #include "libavutil/imgutils.h"
27 #include "bytestream.h"
32 * @return advanced src pointer
34 static const uint8_t *pcx_rle_decode(const uint8_t *src,
37 unsigned int bytes_per_scanline,
41 unsigned char run, value;
44 while (i < bytes_per_scanline && src < end) {
47 if (value >= 0xc0 && src < end) {
51 while (i < bytes_per_scanline && run--)
55 memcpy(dst, src, bytes_per_scanline);
56 src += bytes_per_scanline;
62 static void pcx_palette(const uint8_t **src, uint32_t *dst,
67 for (i = 0; i < pallen; i++)
68 *dst++ = bytestream_get_be24(src);
70 memset(dst, 0, (256 - pallen) * sizeof(*dst));
73 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
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,
83 const uint8_t *buf_end = buf + buf_size;
84 uint8_t const *bufstart = buf;
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;
94 xmin = AV_RL16(buf + 4);
95 ymin = AV_RL16(buf + 6);
96 xmax = AV_RL16(buf + 8);
97 ymax = AV_RL16(buf + 10);
99 if (xmax < xmin || ymax < ymin) {
100 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
101 return AVERROR_INVALIDDATA;
107 bits_per_pixel = buf[3];
108 bytes_per_line = AV_RL16(buf + 66);
110 bytes_per_scanline = nplanes * bytes_per_line;
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;
118 switch ((nplanes << 8) + bits_per_pixel) {
120 avctx->pix_fmt = AV_PIX_FMT_RGB24;
129 avctx->pix_fmt = AV_PIX_FMT_PAL8;
132 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
133 return AVERROR_INVALIDDATA;
138 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
141 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
142 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
146 p->pict_type = AV_PICTURE_TYPE_I;
149 stride = p->linesize[0];
151 scanline = av_malloc(bytes_per_scanline);
153 return AVERROR(ENOMEM);
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);
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)];
168 } else if (nplanes == 1 && bits_per_pixel == 8) {
169 const uint8_t *palstart = bufstart + buf_size - 769;
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;
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);
184 if (buf != palstart) {
185 av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
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;
194 } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
197 for (y = 0; y < h; y++) {
198 init_get_bits(&s, scanline, bytes_per_scanline << 3);
200 buf = pcx_rle_decode(buf, buf_end,
201 scanline, bytes_per_scanline, compressed);
203 for (x = 0; x < w; x++)
204 ptr[x] = get_bits(&s, bits_per_pixel);
207 } else { /* planar, 4, 8 or 16 colors */
210 for (y = 0; y < h; y++) {
211 buf = pcx_rle_decode(buf, buf_end,
212 scanline, bytes_per_scanline, compressed);
214 for (x = 0; x < w; x++) {
215 int m = 0x80 >> (x & 7), v = 0;
216 for (i = nplanes - 1; i >= 0; i--) {
218 v += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
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);
235 ret = buf - bufstart;
241 AVCodec ff_pcx_decoder = {
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 = AV_CODEC_CAP_DR1,