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"
28 #include "bitstream.h"
29 #include "bytestream.h"
32 #define PCX_HEADER_SIZE 128
35 * @return advanced src pointer
37 static void pcx_rle_decode(GetByteContext *gb,
39 unsigned int bytes_per_scanline,
43 unsigned char run, value;
46 while (i < bytes_per_scanline && bytestream2_get_bytes_left(gb)) {
48 value = bytestream2_get_byte(gb);
49 if (value >= 0xc0 && bytestream2_get_bytes_left(gb)) {
51 value = bytestream2_get_byte(gb);
53 while (i < bytes_per_scanline && run--)
57 bytestream2_get_buffer(gb, dst, bytes_per_scanline);
61 static void pcx_palette(GetByteContext *gb, uint32_t *dst,
66 for (i = 0; i < pallen; i++)
67 *dst++ = bytestream2_get_be24(gb);
69 memset(dst, 0, (256 - pallen) * sizeof(*dst));
72 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
75 const uint8_t *buf = avpkt->data;
76 int buf_size = avpkt->size;
77 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,
86 if (buf_size < PCX_HEADER_SIZE) {
87 av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
88 return AVERROR_INVALIDDATA;
91 if (buf[0] != 0x0a || buf[1] > 5) {
92 av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
93 return AVERROR_INVALIDDATA;
97 xmin = AV_RL16(buf + 4);
98 ymin = AV_RL16(buf + 6);
99 xmax = AV_RL16(buf + 8);
100 ymax = AV_RL16(buf + 10);
102 if (xmax < xmin || ymax < ymin) {
103 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
104 return AVERROR_INVALIDDATA;
110 bits_per_pixel = buf[3];
111 bytes_per_line = AV_RL16(buf + 66);
113 bytes_per_scanline = nplanes * bytes_per_line;
115 if (bytes_per_scanline < (w * bits_per_pixel * nplanes + 7) / 8 ||
116 (!compressed && bytes_per_scanline > buf_size / h)) {
117 av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
118 return AVERROR_INVALIDDATA;
121 switch ((nplanes << 8) + bits_per_pixel) {
123 avctx->pix_fmt = AV_PIX_FMT_RGB24;
132 avctx->pix_fmt = AV_PIX_FMT_PAL8;
135 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
136 return AVERROR_INVALIDDATA;
139 bytestream2_init(&gb, buf + PCX_HEADER_SIZE, buf_size - PCX_HEADER_SIZE);
141 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
144 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
145 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
149 p->pict_type = AV_PICTURE_TYPE_I;
152 stride = p->linesize[0];
154 scanline = av_malloc(bytes_per_scanline + AV_INPUT_BUFFER_PADDING_SIZE);
156 return AVERROR(ENOMEM);
158 if (nplanes == 3 && bits_per_pixel == 8) {
159 for (y = 0; y < h; y++) {
160 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
162 for (x = 0; x < w; x++) {
163 ptr[3 * x] = scanline[x];
164 ptr[3 * x + 1] = scanline[x + bytes_per_line];
165 ptr[3 * x + 2] = scanline[x + (bytes_per_line << 1)];
170 } else if (nplanes == 1 && bits_per_pixel == 8) {
171 for (y = 0; y < h; y++, ptr += stride) {
172 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
173 memcpy(ptr, scanline, w);
176 if (bytestream2_get_byte(&gb) != 12) {
177 av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
178 ret = avctx->err_recognition & AV_EF_EXPLODE ?
179 AVERROR_INVALIDDATA : buf_size;
182 } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
185 for (y = 0; y < h; y++) {
186 bitstream_init(&s, scanline, bytes_per_scanline << 3);
188 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
190 for (x = 0; x < w; x++)
191 ptr[x] = bitstream_read(&s, bits_per_pixel);
194 } else { /* planar, 4, 8 or 16 colors */
197 for (y = 0; y < h; y++) {
198 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
200 for (x = 0; x < w; x++) {
201 int m = 0x80 >> (x & 7), v = 0;
202 for (i = nplanes - 1; i >= 0; i--) {
204 v += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
212 if (nplanes == 1 && bits_per_pixel == 8) {
213 if (bytestream2_get_bytes_left(&gb) < 768) {
214 av_log(avctx, AV_LOG_ERROR, "Palette truncated\n");
215 ret = AVERROR_INVALIDDATA;
219 pcx_palette(&gb, (uint32_t *)p->data[1], 256);
220 } else if (bits_per_pixel < 8) {
222 bytestream2_init(&gb1, avpkt->data + 16, 48);
223 pcx_palette(&gb1, (uint32_t *)p->data[1], 16);
228 ret = bytestream2_tell(&gb);
234 AVCodec ff_pcx_decoder = {
236 .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
237 .type = AVMEDIA_TYPE_VIDEO,
238 .id = AV_CODEC_ID_PCX,
239 .decode = pcx_decode_frame,
240 .capabilities = AV_CODEC_CAP_DR1,