comparison pcx.c @ 6073:60d30d88cf71 libavcodec

PC Paintbrush PCX image decoder
author ivo
date Wed, 26 Dec 2007 22:17:46 +0000
parents
children 4d43f2e3aaa5
comparison
equal deleted inserted replaced
6072:fe2733d26e9b 6073:60d30d88cf71
1 /*
2 * PC Paintbrush PCX (.pcx) image decoder
3 * Copyright (c) 2007, 2008 Ivo van Poorten
4 *
5 * This decoder does not support CGA palettes. I am unable to find samples
6 * and Netpbm cannot generate them.
7 *
8 * This file is part of FFmpeg.
9 *
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.
14 *
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.
19 *
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
23 */
24
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "bitstream.h"
28
29 typedef struct PCXContext {
30 AVFrame picture;
31 } PCXContext;
32
33 static int pcx_init(AVCodecContext *avctx) {
34 PCXContext *s = avctx->priv_data;
35
36 avcodec_get_frame_defaults(&s->picture);
37 avctx->coded_frame= &s->picture;
38
39 return 0;
40 }
41
42 /**
43 * @return advanced src pointer
44 */
45 static char *pcx_rle_decode(uint8_t *src, uint8_t *dst,
46 unsigned int bytes_per_scanline) {
47 unsigned int i = 0;
48 unsigned char run, value;
49
50 while (i<bytes_per_scanline) {
51 run = 1;
52 value = *src++;
53 if (value >= 0xc0) {
54 run = value & 0x3f;
55 value = *src++;
56 }
57 while (i<bytes_per_scanline && run--)
58 dst[i++] = value;
59 }
60
61 return src;
62 }
63
64 static void pcx_palette(uint8_t **src, uint32_t *dst, unsigned int pallen) {
65 unsigned int i;
66
67 for (i=0; i<pallen; i++)
68 *dst++ = bytestream_get_be24(src);
69 memset(dst, 0, (256 - pallen) * sizeof(*dst));
70 }
71
72 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
73 uint8_t *buf, int buf_size) {
74 PCXContext * const s = avctx->priv_data;
75 AVFrame *picture = data;
76 AVFrame * const p = (AVFrame *)&s->picture;
77 int xmin, ymin, xmax, ymax;
78 unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
79 bytes_per_scanline;
80 uint8_t *ptr, *bufstart = buf;
81
82 if (buf[0] != 0x0a || buf[1] > 5 || buf[1] == 1 || buf[2] != 1) {
83 av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
84 return -1;
85 }
86
87 xmin = AV_RL16(buf+ 4);
88 ymin = AV_RL16(buf+ 6);
89 xmax = AV_RL16(buf+ 8);
90 ymax = AV_RL16(buf+10);
91
92 if (xmax < xmin || ymax < ymin) {
93 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
94 return -1;
95 }
96
97 w = xmax - xmin + 1;
98 h = ymax - ymin + 1;
99
100 bits_per_pixel = buf[3];
101 bytes_per_line = AV_RL16(buf+66);
102 nplanes = buf[65];
103 bytes_per_scanline = nplanes * bytes_per_line;
104
105 if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
106 av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
107 return -1;
108 }
109
110 switch ((nplanes<<8) + bits_per_pixel) {
111 case 0x0308:
112 avctx->pix_fmt = PIX_FMT_RGB24;
113 break;
114 case 0x0108:
115 case 0x0104:
116 case 0x0102:
117 case 0x0101:
118 case 0x0401:
119 case 0x0301:
120 case 0x0201:
121 avctx->pix_fmt = PIX_FMT_PAL8;
122 break;
123 default:
124 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
125 return -1;
126 }
127
128 buf += 128;
129
130 if (p->data[0])
131 avctx->release_buffer(avctx, p);
132
133 if (avcodec_check_dimensions(avctx, w, h))
134 return -1;
135 if (w != avctx->width || h != avctx->height)
136 avcodec_set_dimensions(avctx, w, h);
137 if (avctx->get_buffer(avctx, p) < 0) {
138 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
139 return -1;
140 }
141
142 p->pict_type = FF_I_TYPE;
143
144 ptr = p->data[0];
145 stride = p->linesize[0];
146
147 if (nplanes == 3 && bits_per_pixel == 8) {
148 uint8_t scanline[bytes_per_scanline];
149
150 for (y=0; y<h; y++) {
151 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
152
153 for (x=0; x<w; x++) {
154 ptr[3*x ] = scanline[x ];
155 ptr[3*x+1] = scanline[x+ bytes_per_line ];
156 ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
157 }
158
159 ptr += stride;
160 }
161
162 } else if (nplanes == 1 && bits_per_pixel == 8) {
163 uint8_t scanline[bytes_per_scanline];
164 uint8_t *palstart = bufstart + buf_size - 769;
165
166 for (y=0; y<h; y++, ptr+=stride) {
167 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
168 memcpy(ptr, scanline, w);
169 }
170
171 if (buf != palstart) {
172 av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
173 buf = palstart;
174 }
175 if (*buf++ != 12) {
176 av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
177 return -1;
178 }
179
180 } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
181 uint8_t scanline[bytes_per_scanline];
182 GetBitContext s;
183
184 for (y=0; y<h; y++) {
185 init_get_bits(&s, scanline, bytes_per_scanline<<3);
186
187 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
188
189 for (x=0; x<w; x++)
190 ptr[x] = get_bits(&s, bits_per_pixel);
191 ptr += stride;
192 }
193
194 } else { /* planar, 4, 8 or 16 colors */
195 uint8_t scanline[bytes_per_scanline];
196 int i;
197
198 for (y=0; y<h; y++) {
199 buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
200
201 for (x=0; x<w; x++) {
202 int m = 0x80 >> (x&7), v = 0;
203 for (i=nplanes - 1; i>=0; i--) {
204 v <<= 1;
205 v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
206 }
207 ptr[x] = v;
208 }
209 ptr += stride;
210 }
211 }
212
213 if (nplanes == 1 && bits_per_pixel == 8) {
214 pcx_palette(&buf, (uint32_t *) p->data[1], 256);
215 } else if (bits_per_pixel < 8) {
216 uint8_t *palette = bufstart+16;
217 pcx_palette(&palette, (uint32_t *) p->data[1], 16);
218 }
219
220 *picture = s->picture;
221 *data_size = sizeof(AVFrame);
222
223 return buf - bufstart;
224 }
225
226 static int pcx_end(AVCodecContext *avctx) {
227 PCXContext *s = avctx->priv_data;
228
229 if(s->picture.data[0])
230 avctx->release_buffer(avctx, &s->picture);
231
232 return 0;
233 }
234
235 AVCodec pcx_decoder = {
236 "pcx",
237 CODEC_TYPE_VIDEO,
238 CODEC_ID_PCX,
239 sizeof(PCXContext),
240 pcx_init,
241 NULL,
242 pcx_end,
243 pcx_decode_frame,
244 0,
245 NULL
246 };