]> git.sesse.net Git - ffmpeg/blob - libavcodec/pcx.c
pcx: K&R formatting cosmetics
[ffmpeg] / libavcodec / pcx.c
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 Libav.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "libavutil/imgutils.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "get_bits.h"
29 #include "internal.h"
30
31 /**
32  * @return advanced src pointer
33  */
34 static const uint8_t *pcx_rle_decode(const uint8_t *src, uint8_t *dst,
35                                      unsigned int bytes_per_scanline,
36                                      int compressed)
37 {
38     unsigned int i = 0;
39     unsigned char run, value;
40
41     if (compressed) {
42         while (i < bytes_per_scanline) {
43             run   = 1;
44             value = *src++;
45             if (value >= 0xc0) {
46                 run   = value & 0x3f;
47                 value = *src++;
48             }
49             while (i < bytes_per_scanline && run--)
50                 dst[i++] = value;
51         }
52     } else {
53         memcpy(dst, src, bytes_per_scanline);
54         src += bytes_per_scanline;
55     }
56
57     return src;
58 }
59
60 static void pcx_palette(const uint8_t **src, uint32_t *dst,
61                         unsigned int pallen)
62 {
63     unsigned int i;
64
65     for (i = 0; i < pallen; i++)
66         *dst++ = bytestream_get_be24(src);
67     if (pallen < 256)
68         memset(dst, 0, (256 - pallen) * sizeof(*dst));
69 }
70
71 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
72                             AVPacket *avpkt)
73 {
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,
79                  bytes_per_scanline;
80     uint8_t *ptr;
81     uint8_t const *bufstart = buf;
82     uint8_t *scanline;
83     int ret = -1;
84
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;
88     }
89
90     compressed = buf[2];
91     xmin       = AV_RL16(buf + 4);
92     ymin       = AV_RL16(buf + 6);
93     xmax       = AV_RL16(buf + 8);
94     ymax       = AV_RL16(buf + 10);
95
96     if (xmax < xmin || ymax < ymin) {
97         av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
98         return AVERROR_INVALIDDATA;
99     }
100
101     w = xmax - xmin + 1;
102     h = ymax - ymin + 1;
103
104     bits_per_pixel     = buf[3];
105     bytes_per_line     = AV_RL16(buf + 66);
106     nplanes            = buf[65];
107     bytes_per_scanline = nplanes * bytes_per_line;
108
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;
112     }
113
114     switch ((nplanes << 8) + bits_per_pixel) {
115     case 0x0308:
116         avctx->pix_fmt = AV_PIX_FMT_RGB24;
117         break;
118     case 0x0108:
119     case 0x0104:
120     case 0x0102:
121     case 0x0101:
122     case 0x0401:
123     case 0x0301:
124     case 0x0201:
125         avctx->pix_fmt = AV_PIX_FMT_PAL8;
126         break;
127     default:
128         av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
129         return AVERROR_INVALIDDATA;
130     }
131
132     buf += 128;
133
134     if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
135         return ret;
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");
140         return ret;
141     }
142
143     p->pict_type = AV_PICTURE_TYPE_I;
144
145     ptr    = p->data[0];
146     stride = p->linesize[0];
147
148     scanline = av_malloc(bytes_per_scanline);
149     if (!scanline)
150         return AVERROR(ENOMEM);
151
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);
155
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)];
160             }
161
162             ptr += stride;
163         }
164     } else if (nplanes == 1 && bits_per_pixel == 8) {
165         const uint8_t *palstart = bufstart + buf_size - 769;
166
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);
170         }
171
172         if (buf != palstart) {
173             av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
174             buf = palstart;
175         }
176         if (*buf++ != 12) {
177             av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
178             goto end;
179         }
180     } else if (nplanes == 1) {   /* all packed formats, max. 16 colors */
181         GetBitContext s;
182
183         for (y = 0; y < h; y++) {
184             init_get_bits(&s, scanline, bytes_per_scanline << 3);
185
186             buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
187
188             for (x = 0; x < w; x++)
189                 ptr[x] = get_bits(&s, bits_per_pixel);
190             ptr += stride;
191         }
192     } else {    /* planar, 4, 8 or 16 colors */
193         int i;
194
195         for (y = 0; y < h; y++) {
196             buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
197
198             for (x = 0; x < w; x++) {
199                 int m = 0x80 >> (x & 7), v = 0;
200                 for (i = nplanes - 1; i >= 0; i--) {
201                     v <<= 1;
202                     v  += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
203                 }
204                 ptr[x] = v;
205             }
206             ptr += stride;
207         }
208     }
209
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);
215     }
216
217     *got_frame = 1;
218
219     ret = buf - bufstart;
220 end:
221     av_free(scanline);
222     return ret;
223 }
224
225 AVCodec ff_pcx_decoder = {
226     .name         = "pcx",
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"),
232 };