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, uint8_t *dst,
35 unsigned int bytes_per_scanline,
39 unsigned char run, value;
42 while (i < bytes_per_scanline) {
49 while (i < bytes_per_scanline && run--)
53 memcpy(dst, src, bytes_per_scanline);
54 src += bytes_per_scanline;
60 static void pcx_palette(const uint8_t **src, uint32_t *dst,
65 for (i = 0; i < pallen; i++)
66 *dst++ = bytestream_get_be24(src);
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;
77 int compressed, xmin, ymin, xmax, ymax;
78 unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
81 uint8_t const *bufstart = buf;
85 if (buf[0] != 0x0a || buf[1] > 5) {
86 av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
87 return AVERROR_INVALIDDATA;
91 xmin = AV_RL16(buf + 4);
92 ymin = AV_RL16(buf + 6);
93 xmax = AV_RL16(buf + 8);
94 ymax = AV_RL16(buf + 10);
96 if (xmax < xmin || ymax < ymin) {
97 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
98 return AVERROR_INVALIDDATA;
104 bits_per_pixel = buf[3];
105 bytes_per_line = AV_RL16(buf + 66);
107 bytes_per_scanline = nplanes * bytes_per_line;
109 if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
110 av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
111 return AVERROR_INVALIDDATA;
114 switch ((nplanes << 8) + bits_per_pixel) {
116 avctx->pix_fmt = AV_PIX_FMT_RGB24;
125 avctx->pix_fmt = AV_PIX_FMT_PAL8;
128 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
129 return AVERROR_INVALIDDATA;
134 if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
136 if (w != avctx->width || h != avctx->height)
137 avcodec_set_dimensions(avctx, w, h);
138 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
139 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
143 p->pict_type = AV_PICTURE_TYPE_I;
146 stride = p->linesize[0];
148 scanline = av_malloc(bytes_per_scanline);
150 return AVERROR(ENOMEM);
152 if (nplanes == 3 && bits_per_pixel == 8) {
153 for (y = 0; y < h; y++) {
154 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
156 for (x = 0; x < w; x++) {
157 ptr[3 * x] = scanline[x];
158 ptr[3 * x + 1] = scanline[x + bytes_per_line];
159 ptr[3 * x + 2] = scanline[x + (bytes_per_line << 1)];
164 } else if (nplanes == 1 && bits_per_pixel == 8) {
165 const uint8_t *palstart = bufstart + buf_size - 769;
167 for (y = 0; y < h; y++, ptr += stride) {
168 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
169 memcpy(ptr, scanline, w);
172 if (buf != palstart) {
173 av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
177 av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
180 } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
183 for (y = 0; y < h; y++) {
184 init_get_bits(&s, scanline, bytes_per_scanline << 3);
186 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
188 for (x = 0; x < w; x++)
189 ptr[x] = get_bits(&s, bits_per_pixel);
192 } else { /* planar, 4, 8 or 16 colors */
195 for (y = 0; y < h; y++) {
196 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
198 for (x = 0; x < w; x++) {
199 int m = 0x80 >> (x & 7), v = 0;
200 for (i = nplanes - 1; i >= 0; i--) {
202 v += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
210 if (nplanes == 1 && bits_per_pixel == 8) {
211 pcx_palette(&buf, (uint32_t *)p->data[1], 256);
212 } else if (bits_per_pixel < 8) {
213 const uint8_t *palette = bufstart + 16;
214 pcx_palette(&palette, (uint32_t *)p->data[1], 16);
219 ret = buf - bufstart;
225 AVCodec ff_pcx_decoder = {
227 .type = AVMEDIA_TYPE_VIDEO,
228 .id = AV_CODEC_ID_PCX,
229 .decode = pcx_decode_frame,
230 .capabilities = CODEC_CAP_DR1,
231 .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),