]> git.sesse.net Git - ffmpeg/blob - libavcodec/xbmdec.c
imgconvert: cosmetics: Reshuffle defines to reduce ifdeffery
[ffmpeg] / libavcodec / xbmdec.c
1 /*
2  * XBM image format
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/avstring.h"
22
23 #include "avcodec.h"
24 #include "internal.h"
25 #include "mathops.h"
26
27 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
28                             int *got_frame, AVPacket *avpkt)
29 {
30     AVFrame *p = data;
31     int ret, linesize, i;
32     int width  = 0;
33     int height = 0;
34     const uint8_t *ptr = avpkt->data;
35     uint8_t *dst;
36
37     avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
38     while (!width || !height) {
39         ptr += strcspn(ptr, "#");
40         if (ptr >= avpkt->data + avpkt->size) {
41             av_log(avctx, AV_LOG_ERROR, "End of file reached.\n");
42             return AVERROR_INVALIDDATA;
43         }
44         if (strncmp(ptr, "#define", 7) != 0) {
45             av_log(avctx, AV_LOG_ERROR,
46                    "Unexpected preprocessor directive.\n");
47             return AVERROR_INVALIDDATA;
48         }
49         // skip the name
50         ptr += strcspn(ptr, "_") + 1;
51         // get width or height
52         if (strncmp(ptr, "width", 5) == 0) {
53             ptr += strcspn(ptr, " ");
54             width = strtol(ptr, NULL, 10);
55         } else if (strncmp(ptr, "height", 6) == 0) {
56             ptr += strcspn(ptr, " ");
57             height = strtol(ptr, NULL, 10);
58         } else {
59             // skip offset and unknown variables
60             av_log(avctx, AV_LOG_VERBOSE,
61                    "Ignoring preprocessor directive.\n");
62         }
63     }
64
65     if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
66         return ret;
67
68     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
69         return ret;
70
71     // go to start of image data
72     ptr += strcspn(ptr, "{");
73
74     linesize = (avctx->width + 7) / 8;
75     for (i = 0; i < avctx->height; i++) {
76         int eol = 0, e = 0;
77         dst = p->data[0] + i * p->linesize[0];
78         if (ptr >= avpkt->data + avpkt->size) {
79             av_log(avctx, AV_LOG_ERROR, "End of file reached.\n");
80             return AVERROR_INVALIDDATA;
81         }
82         do {
83             int val;
84             uint8_t *endptr;
85
86             ptr += strcspn(ptr, "x") - 1; // -1 to get 0x
87             val = strtol(ptr, (char **)&endptr, 16);
88
89             if (endptr - ptr == 4) {
90                 // XBM X11 format
91                 *dst++ = ff_reverse[val];
92                 eol = linesize;
93             } else if (endptr - ptr == 6) {
94                 // XBM X10 format
95                 *dst++ = ff_reverse[val >> 8];
96                 *dst++ = ff_reverse[val & 0xFF];
97                 eol = linesize / 2; // 2 bytes read
98             } else {
99                 av_log(avctx, AV_LOG_ERROR,
100                        "Unexpected data at %.8s.\n", ptr);
101                 return AVERROR_INVALIDDATA;
102             }
103             ptr = endptr;
104         } while (++e < eol);
105     }
106
107     p->key_frame = 1;
108     p->pict_type = AV_PICTURE_TYPE_I;
109
110     *got_frame = 1;
111
112     return avpkt->size;
113 }
114
115 AVCodec ff_xbm_decoder = {
116     .name         = "xbm",
117     .long_name    = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
118     .type         = AVMEDIA_TYPE_VIDEO,
119     .id           = AV_CODEC_ID_XBM,
120     .decode       = xbm_decode_frame,
121     .capabilities = CODEC_CAP_DR1,
122 };