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"
31 #define PCX_HEADER_SIZE 128
34 * @return advanced src pointer
36 static void pcx_rle_decode(GetByteContext *gb,
38 unsigned int bytes_per_scanline,
42 unsigned char run, value;
45 while (i < bytes_per_scanline && bytestream2_get_bytes_left(gb)) {
47 value = bytestream2_get_byte(gb);
48 if (value >= 0xc0 && bytestream2_get_bytes_left(gb)) {
50 value = bytestream2_get_byte(gb);
52 while (i < bytes_per_scanline && run--)
56 bytestream2_get_buffer(gb, dst, bytes_per_scanline);
60 static void pcx_palette(GetByteContext *gb, uint32_t *dst,
65 for (i = 0; i < pallen; i++)
66 *dst++ = bytestream2_get_be24(gb);
68 memset(dst, 0, (256 - pallen) * sizeof(*dst));
71 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
74 const uint8_t *buf = avpkt->data;
75 int buf_size = avpkt->size;
76 AVFrame *const p = data;
78 int compressed, xmin, ymin, xmax, ymax;
79 unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
85 if (buf_size < PCX_HEADER_SIZE) {
86 av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
87 return AVERROR_INVALIDDATA;
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;
96 xmin = AV_RL16(buf + 4);
97 ymin = AV_RL16(buf + 6);
98 xmax = AV_RL16(buf + 8);
99 ymax = AV_RL16(buf + 10);
101 if (xmax < xmin || ymax < ymin) {
102 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
103 return AVERROR_INVALIDDATA;
109 bits_per_pixel = buf[3];
110 bytes_per_line = AV_RL16(buf + 66);
112 bytes_per_scanline = nplanes * bytes_per_line;
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;
120 switch ((nplanes << 8) + bits_per_pixel) {
122 avctx->pix_fmt = AV_PIX_FMT_RGB24;
131 avctx->pix_fmt = AV_PIX_FMT_PAL8;
134 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
135 return AVERROR_INVALIDDATA;
138 bytestream2_init(&gb, buf + PCX_HEADER_SIZE, buf_size - PCX_HEADER_SIZE);
140 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
143 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
144 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
148 p->pict_type = AV_PICTURE_TYPE_I;
151 stride = p->linesize[0];
153 scanline = av_malloc(bytes_per_scanline + AV_INPUT_BUFFER_PADDING_SIZE);
155 return AVERROR(ENOMEM);
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);
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)];
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);
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;
181 } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
184 for (y = 0; y < h; y++) {
185 init_get_bits(&s, scanline, bytes_per_scanline << 3);
187 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
189 for (x = 0; x < w; x++)
190 ptr[x] = get_bits(&s, bits_per_pixel);
193 } else { /* planar, 4, 8 or 16 colors */
196 for (y = 0; y < h; y++) {
197 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
199 for (x = 0; x < w; x++) {
200 int m = 0x80 >> (x & 7), v = 0;
201 for (i = nplanes - 1; i >= 0; i--) {
203 v += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
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;
218 pcx_palette(&gb, (uint32_t *)p->data[1], 256);
219 } else if (bits_per_pixel < 8) {
221 bytestream2_init(&gb1, avpkt->data + 16, 48);
222 pcx_palette(&gb1, (uint32_t *)p->data[1], 16);
227 ret = bytestream2_tell(&gb);
233 AVCodec ff_pcx_decoder = {
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 = AV_CODEC_CAP_DR1,