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 FFmpeg.
10 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
30 typedef struct PCXContext {
34 static av_cold int pcx_init(AVCodecContext *avctx)
36 PCXContext *s = avctx->priv_data;
38 avcodec_get_frame_defaults(&s->picture);
39 avctx->coded_frame= &s->picture;
44 static void pcx_rle_decode(GetByteContext *gb, uint8_t *dst,
45 unsigned int bytes_per_scanline, int compressed)
48 unsigned char run, value;
51 while (i<bytes_per_scanline) {
53 value = bytestream2_get_byte(gb);
56 value = bytestream2_get_byte(gb);
58 while (i<bytes_per_scanline && run--)
62 bytestream2_get_buffer(gb, dst, bytes_per_scanline);
66 static void pcx_palette(GetByteContext *gb, uint32_t *dst, int pallen)
70 pallen = FFMIN(pallen, bytestream2_get_bytes_left(gb) / 3);
71 for (i=0; i<pallen; i++)
72 *dst++ = 0xFF000000 | bytestream2_get_be24u(gb);
74 memset(dst, 0, (256 - pallen) * sizeof(*dst));
77 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
80 PCXContext * const s = avctx->priv_data;
81 AVFrame *picture = data;
82 AVFrame * const p = &s->picture;
84 int compressed, xmin, ymin, xmax, ymax, ret;
85 unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
87 uint8_t *ptr, *scanline;
89 if (avpkt->size < 128)
90 return AVERROR_INVALIDDATA;
92 bytestream2_init(&gb, avpkt->data, avpkt->size);
94 if (bytestream2_get_byteu(&gb) != 0x0a || bytestream2_get_byteu(&gb) > 5) {
95 av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
96 return AVERROR_INVALIDDATA;
99 compressed = bytestream2_get_byteu(&gb);
100 bits_per_pixel = bytestream2_get_byteu(&gb);
101 xmin = bytestream2_get_le16u(&gb);
102 ymin = bytestream2_get_le16u(&gb);
103 xmax = bytestream2_get_le16u(&gb);
104 ymax = bytestream2_get_le16u(&gb);
106 if (xmax < xmin || ymax < ymin) {
107 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
108 return AVERROR_INVALIDDATA;
114 bytestream2_skipu(&gb, 53);
115 nplanes = bytestream2_get_byteu(&gb);
116 bytes_per_line = bytestream2_get_le16u(&gb);
117 bytes_per_scanline = nplanes * bytes_per_line;
119 if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
120 av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
121 return AVERROR_INVALIDDATA;
124 switch ((nplanes<<8) + bits_per_pixel) {
126 avctx->pix_fmt = AV_PIX_FMT_RGB24;
135 avctx->pix_fmt = AV_PIX_FMT_PAL8;
138 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
139 return AVERROR_INVALIDDATA;
142 bytestream2_skipu(&gb, 60);
145 avctx->release_buffer(avctx, p);
147 if (av_image_check_size(w, h, 0, avctx))
148 return AVERROR_INVALIDDATA;
149 if (w != avctx->width || h != avctx->height)
150 avcodec_set_dimensions(avctx, w, h);
151 if ((ret = avctx->get_buffer(avctx, p)) < 0) {
152 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
156 p->pict_type = AV_PICTURE_TYPE_I;
159 stride = p->linesize[0];
161 scanline = av_malloc(bytes_per_scanline);
163 return AVERROR(ENOMEM);
165 if (nplanes == 3 && bits_per_pixel == 8) {
166 for (y=0; y<h; y++) {
167 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
169 for (x=0; x<w; x++) {
170 ptr[3*x ] = scanline[x ];
171 ptr[3*x+1] = scanline[x+ bytes_per_line ];
172 ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
178 } else if (nplanes == 1 && bits_per_pixel == 8) {
179 int palstart = avpkt->size - 769;
181 for (y=0; y<h; y++, ptr+=stride) {
182 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
183 memcpy(ptr, scanline, w);
186 if (bytestream2_tell(&gb) != palstart) {
187 av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
188 bytestream2_seek(&gb, palstart, SEEK_SET);
190 if (bytestream2_get_byte(&gb) != 12) {
191 av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
192 ret = AVERROR_INVALIDDATA;
196 } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
199 for (y=0; y<h; y++) {
200 init_get_bits(&s, scanline, bytes_per_scanline<<3);
202 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
205 ptr[x] = get_bits(&s, bits_per_pixel);
209 } else { /* planar, 4, 8 or 16 colors */
212 for (y=0; y<h; y++) {
213 pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
215 for (x=0; x<w; x++) {
216 int m = 0x80 >> (x&7), v = 0;
217 for (i=nplanes - 1; i>=0; i--) {
219 v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
227 ret = bytestream2_tell(&gb);
228 if (nplanes == 1 && bits_per_pixel == 8) {
229 pcx_palette(&gb, (uint32_t *) p->data[1], 256);
231 } else if (bits_per_pixel * nplanes == 1) {
232 AV_WN32A(p->data[1] , 0xFF000000);
233 AV_WN32A(p->data[1]+4, 0xFFFFFFFF);
234 } else if (bits_per_pixel < 8) {
235 bytestream2_seek(&gb, 16, SEEK_SET);
236 pcx_palette(&gb, (uint32_t *) p->data[1], 16);
239 *picture = s->picture;
240 *data_size = sizeof(AVFrame);
247 static av_cold int pcx_end(AVCodecContext *avctx)
249 PCXContext *s = avctx->priv_data;
251 if(s->picture.data[0])
252 avctx->release_buffer(avctx, &s->picture);
257 AVCodec ff_pcx_decoder = {
259 .type = AVMEDIA_TYPE_VIDEO,
260 .id = AV_CODEC_ID_PCX,
261 .priv_data_size = sizeof(PCXContext),
264 .decode = pcx_decode_frame,
265 .capabilities = CODEC_CAP_DR1,
266 .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),