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, int compressed) {
37 unsigned char run, value;
40 while (i<bytes_per_scanline) {
47 while (i<bytes_per_scanline && run--)
51 memcpy(dst, src, bytes_per_scanline);
52 src += bytes_per_scanline;
58 static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
61 for (i=0; i<pallen; i++)
62 *dst++ = bytestream_get_be24(src);
64 memset(dst, 0, (256 - pallen) * sizeof(*dst));
67 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
69 const uint8_t *buf = avpkt->data;
70 int buf_size = avpkt->size;
71 AVFrame * const p = data;
72 int compressed, xmin, ymin, xmax, ymax;
73 unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
76 uint8_t const *bufstart = buf;
80 if (buf[0] != 0x0a || buf[1] > 5) {
81 av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
82 return AVERROR_INVALIDDATA;
86 xmin = AV_RL16(buf+ 4);
87 ymin = AV_RL16(buf+ 6);
88 xmax = AV_RL16(buf+ 8);
89 ymax = AV_RL16(buf+10);
91 if (xmax < xmin || ymax < ymin) {
92 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
93 return AVERROR_INVALIDDATA;
99 bits_per_pixel = buf[3];
100 bytes_per_line = AV_RL16(buf+66);
102 bytes_per_scanline = nplanes * bytes_per_line;
104 if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
105 av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
106 return AVERROR_INVALIDDATA;
109 switch ((nplanes<<8) + bits_per_pixel) {
111 avctx->pix_fmt = AV_PIX_FMT_RGB24;
120 avctx->pix_fmt = AV_PIX_FMT_PAL8;
123 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
124 return AVERROR_INVALIDDATA;
129 if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
131 if (w != avctx->width || h != avctx->height)
132 avcodec_set_dimensions(avctx, w, h);
133 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
134 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
138 p->pict_type = AV_PICTURE_TYPE_I;
141 stride = p->linesize[0];
143 scanline = av_malloc(bytes_per_scanline);
145 return AVERROR(ENOMEM);
147 if (nplanes == 3 && bits_per_pixel == 8) {
148 for (y=0; y<h; y++) {
149 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
151 for (x=0; x<w; x++) {
152 ptr[3*x ] = scanline[x ];
153 ptr[3*x+1] = scanline[x+ bytes_per_line ];
154 ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
160 } else if (nplanes == 1 && bits_per_pixel == 8) {
161 const uint8_t *palstart = bufstart + buf_size - 769;
163 for (y=0; y<h; y++, ptr+=stride) {
164 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
165 memcpy(ptr, scanline, w);
168 if (buf != palstart) {
169 av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
173 av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
177 } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
180 for (y=0; y<h; y++) {
181 init_get_bits(&s, scanline, bytes_per_scanline<<3);
183 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
186 ptr[x] = get_bits(&s, bits_per_pixel);
190 } else { /* planar, 4, 8 or 16 colors */
193 for (y=0; y<h; y++) {
194 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
196 for (x=0; x<w; x++) {
197 int m = 0x80 >> (x&7), v = 0;
198 for (i=nplanes - 1; i>=0; i--) {
200 v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
208 if (nplanes == 1 && bits_per_pixel == 8) {
209 pcx_palette(&buf, (uint32_t *) p->data[1], 256);
210 } else if (bits_per_pixel < 8) {
211 const uint8_t *palette = bufstart+16;
212 pcx_palette(&palette, (uint32_t *) p->data[1], 16);
217 ret = buf - bufstart;
223 AVCodec ff_pcx_decoder = {
225 .type = AVMEDIA_TYPE_VIDEO,
226 .id = AV_CODEC_ID_PCX,
227 .decode = pcx_decode_frame,
228 .capabilities = CODEC_CAP_DR1,
229 .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),