1 /*
2 * XPM image format
3 *
4 * Copyright (c) 2012 Paul B Mahol
5 * Copyright (c) 2017 Paras Chadha
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
28
29 typedef struct XPMContext {
35
37 const char *
name;
///< a string representing the name of the color
40
42 {
44 }
45
47 { "AliceBlue", 0xFFF0F8FF },
48 { "AntiqueWhite", 0xFFFAEBD7 },
49 { "Aqua", 0xFF00FFFF },
50 { "Aquamarine", 0xFF7FFFD4 },
51 { "Azure", 0xFFF0FFFF },
52 { "Beige", 0xFFF5F5DC },
53 { "Bisque", 0xFFFFE4C4 },
54 { "Black", 0xFF000000 },
55 { "BlanchedAlmond", 0xFFFFEBCD },
56 { "Blue", 0xFF0000FF },
57 { "BlueViolet", 0xFF8A2BE2 },
58 { "Brown", 0xFFA52A2A },
59 { "BurlyWood", 0xFFDEB887 },
60 { "CadetBlue", 0xFF5F9EA0 },
61 { "Chartreuse", 0xFF7FFF00 },
62 { "Chocolate", 0xFFD2691E },
63 { "Coral", 0xFFFF7F50 },
64 { "CornflowerBlue", 0xFF6495ED },
65 { "Cornsilk", 0xFFFFF8DC },
66 { "Crimson", 0xFFDC143C },
67 { "Cyan", 0xFF00FFFF },
68 { "DarkBlue", 0xFF00008B },
69 { "DarkCyan", 0xFF008B8B },
70 { "DarkGoldenRod", 0xFFB8860B },
71 { "DarkGray", 0xFFA9A9A9 },
72 { "DarkGreen", 0xFF006400 },
73 { "DarkKhaki", 0xFFBDB76B },
74 { "DarkMagenta", 0xFF8B008B },
75 { "DarkOliveGreen", 0xFF556B2F },
76 { "Darkorange", 0xFFFF8C00 },
77 { "DarkOrchid", 0xFF9932CC },
78 { "DarkRed", 0xFF8B0000 },
79 { "DarkSalmon", 0xFFE9967A },
80 { "DarkSeaGreen", 0xFF8FBC8F },
81 { "DarkSlateBlue", 0xFF483D8B },
82 { "DarkSlateGray", 0xFF2F4F4F },
83 { "DarkTurquoise", 0xFF00CED1 },
84 { "DarkViolet", 0xFF9400D3 },
85 { "DeepPink", 0xFFFF1493 },
86 { "DeepSkyBlue", 0xFF00BFFF },
87 { "DimGray", 0xFF696969 },
88 { "DodgerBlue", 0xFF1E90FF },
89 { "FireBrick", 0xFFB22222 },
90 { "FloralWhite", 0xFFFFFAF0 },
91 { "ForestGreen", 0xFF228B22 },
92 { "Fuchsia", 0xFFFF00FF },
93 { "Gainsboro", 0xFFDCDCDC },
94 { "GhostWhite", 0xFFF8F8FF },
95 { "Gold", 0xFFFFD700 },
96 { "GoldenRod", 0xFFDAA520 },
97 { "Gray", 0xFFBEBEBE },
98 { "Green", 0xFF00FF00 },
99 { "GreenYellow", 0xFFADFF2F },
100 { "HoneyDew", 0xFFF0FFF0 },
101 { "HotPink", 0xFFFF69B4 },
102 { "IndianRed", 0xFFCD5C5C },
103 { "Indigo", 0xFF4B0082 },
104 { "Ivory", 0xFFFFFFF0 },
105 { "Khaki", 0xFFF0E68C },
106 { "Lavender", 0xFFE6E6FA },
107 { "LavenderBlush", 0xFFFFF0F5 },
108 { "LawnGreen", 0xFF7CFC00 },
109 { "LemonChiffon", 0xFFFFFACD },
110 { "LightBlue", 0xFFADD8E6 },
111 { "LightCoral", 0xFFF08080 },
112 { "LightCyan", 0xFFE0FFFF },
113 { "LightGoldenRodYellow", 0xFFFAFAD2 },
114 { "LightGreen", 0xFF90EE90 },
115 { "LightGrey", 0xFFD3D3D3 },
116 { "LightPink", 0xFFFFB6C1 },
117 { "LightSalmon", 0xFFFFA07A },
118 { "LightSeaGreen", 0xFF20B2AA },
119 { "LightSkyBlue", 0xFF87CEFA },
120 { "LightSlateGray", 0xFF778899 },
121 { "LightSteelBlue", 0xFFB0C4DE },
122 { "LightYellow", 0xFFFFFFE0 },
123 { "Lime", 0xFF00FF00 },
124 { "LimeGreen", 0xFF32CD32 },
125 { "Linen", 0xFFFAF0E6 },
126 { "Magenta", 0xFFFF00FF },
127 { "Maroon", 0xFFB03060 },
128 { "MediumAquaMarine", 0xFF66CDAA },
129 { "MediumBlue", 0xFF0000CD },
130 { "MediumOrchid", 0xFFBA55D3 },
131 { "MediumPurple", 0xFF9370D8 },
132 { "MediumSeaGreen", 0xFF3CB371 },
133 { "MediumSlateBlue", 0xFF7B68EE },
134 { "MediumSpringGreen", 0xFF00FA9A },
135 { "MediumTurquoise", 0xFF48D1CC },
136 { "MediumVioletRed", 0xFFC71585 },
137 { "MidnightBlue", 0xFF191970 },
138 { "MintCream", 0xFFF5FFFA },
139 { "MistyRose", 0xFFFFE4E1 },
140 { "Moccasin", 0xFFFFE4B5 },
141 { "NavajoWhite", 0xFFFFDEAD },
142 { "Navy", 0xFF000080 },
143 { "None", 0x00000000 },
144 { "OldLace", 0xFFFDF5E6 },
145 { "Olive", 0xFF808000 },
146 { "OliveDrab", 0xFF6B8E23 },
147 { "Orange", 0xFFFFA500 },
148 { "OrangeRed", 0xFFFF4500 },
149 { "Orchid", 0xFFDA70D6 },
150 { "PaleGoldenRod", 0xFFEEE8AA },
151 { "PaleGreen", 0xFF98FB98 },
152 { "PaleTurquoise", 0xFFAFEEEE },
153 { "PaleVioletRed", 0xFFD87093 },
154 { "PapayaWhip", 0xFFFFEFD5 },
155 { "PeachPuff", 0xFFFFDAB9 },
156 { "Peru", 0xFFCD853F },
157 { "Pink", 0xFFFFC0CB },
158 { "Plum", 0xFFDDA0DD },
159 { "PowderBlue", 0xFFB0E0E6 },
160 { "Purple", 0xFFA020F0 },
161 { "Red", 0xFFFF0000 },
162 { "RosyBrown", 0xFFBC8F8F },
163 { "RoyalBlue", 0xFF4169E1 },
164 { "SaddleBrown", 0xFF8B4513 },
165 { "Salmon", 0xFFFA8072 },
166 { "SandyBrown", 0xFFF4A460 },
167 { "SeaGreen", 0xFF2E8B57 },
168 { "SeaShell", 0xFFFFF5EE },
169 { "Sienna", 0xFFA0522D },
170 { "Silver", 0xFFC0C0C0 },
171 { "SkyBlue", 0xFF87CEEB },
172 { "SlateBlue", 0xFF6A5ACD },
173 { "SlateGray", 0xFF708090 },
174 { "Snow", 0xFFFFFAFA },
175 { "SpringGreen", 0xFF00FF7F },
176 { "SteelBlue", 0xFF4682B4 },
177 { "Tan", 0xFFD2B48C },
178 { "Teal", 0xFF008080 },
179 { "Thistle", 0xFFD8BFD8 },
180 { "Tomato", 0xFFFF6347 },
181 { "Turquoise", 0xFF40E0D0 },
182 { "Violet", 0xFFEE82EE },
183 { "Wheat", 0xFFF5DEB3 },
184 { "White", 0xFFFFFFFF },
185 { "WhiteSmoke", 0xFFF5F5F5 },
186 { "Yellow", 0xFFFFFF00 },
187 { "YellowGreen", 0xFF9ACD32 }
188 };
189
191 {
192 if (x >= 'a' && x <= 'f')
193 x -= 'a' - 10;
194 else if (x >= 'A' && x <= 'F')
195 x -= 'A' - 10;
196 else if (x >= '0' && x <= '9')
197 x -= '0';
198 else
199 x = 0;
200 return x;
201 }
202
203 /*
204 * Function same as strcspn but ignores characters if they are inside a C style comments
205 */
207 {
208 int i, j;
209
210 for (i = 0; string && string[i]; i++) {
211 if (string[i] == '/' && string[i+1] == '*') {
212 i += 2;
213 while ( string && string[i] && (string[i] != '*' || string[i+1] != '/') )
214 i++;
215 i++;
216 } else if (string[i] == '/' && string[i+1] == '/') {
217 i += 2;
218 while ( string && string[i] && string[i] != '\n' )
219 i++;
220 } else {
221 for (j = 0; reject && reject[j]; j++) {
222 if (string[i] == reject[j])
223 break;
224 }
225 if (reject && reject[j])
226 break;
227 }
228 }
229 return i;
230 }
231
233 {
234 uint32_t ret = 0xFF000000;
236 char color_name[100];
237
238 len =
FFMIN(
FFMAX(len, 0),
sizeof(color_name) - 1);
239
240 if (*p == '#') {
241 p++;
242 len--;
243 if (len == 3) {
247 } else if (len == 4) {
252 } else if (len == 6) {
259 } else if (len == 8) {
268 }
269 } else {
270 strncpy(color_name, p, len);
271 color_name[
len] =
'0円';
272
273 entry = bsearch(color_name,
274 color_table,
278
279 if (!entry)
280 return ret;
281
283 }
284 return ret;
285 }
286
288 {
291
292 for (i = 0; i < cpp; i++) {
293 if (*p < ' ' || *p > '~')
295 n += (*p++ - ' ') * m;
296 m *= 95;
297 }
299 }
300
303 {
307 int ncolors, cpp, ret, i, j;
309 uint32_t *dst;
310
312
318
321 while (end - ptr > 9 && memcmp(ptr, "/* XPM */", 9))
322 ptr++;
323
324 if (end - ptr <= 9) {
327 }
328
330 if (sscanf(ptr, "\"%u %u %u %u\",",
331 &avctx->
width, &avctx->
height, &ncolors, &cpp) != 4) {
334 }
335
337 return ret;
338
340 return ret;
341
342 if (cpp <= 0 || cpp >= 5) {
343 av_log(avctx,
AV_LOG_ERROR,
"unsupported/invalid number of chars per pixel: %d\n", cpp);
345 }
346
347 size = 1;
348 for (i = 0; i < cpp; i++)
349 size *= 95;
350
351 if (ncolors <= 0 || ncolors > size) {
354 }
355
356 size *= 4;
357
361
363 if (end - ptr < 1)
365
366 for (i = 0; i < ncolors; i++) {
369
371 if (end - ptr < cpp)
373 index = ptr;
374 ptr += cpp;
375
376 ptr = strstr(ptr, "c ");
377 if (ptr) {
378 ptr += 2;
379 } else {
381 }
382
383 len = strcspn(ptr, "\" ");
384
386 return ret;
387
390 if (end - ptr < 1)
392 }
393
394 for (i = 0; i < avctx->
height; i++) {
396 if (end - ptr < 1)
399 if (end - ptr < 1)
401
402 for (j = 0; j < avctx->
width; j++) {
403 if (end - ptr < cpp)
405
407 return ret;
408
410 ptr += cpp;
411 }
413 }
414
417
418 *got_frame = 1;
419
421 }
422
424 {
427
430
431 return 0;
432 }
433
443 };
static int xpm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static int color_table_compare(const void *lhs, const void *rhs)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
const char * name
a string representing the name of the color
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
static uint32_t color_string_to_rgba(const char *p, int len)
static av_cold int end(AVCodecContext *avctx)
uint32_t rgb_color
RGB values for the color.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_cold int xpm_decode_close(AVCodecContext *avctx)
static const ColorEntry color_table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
const char * name
Name of the codec implementation.
enum AVPictureType pict_type
Picture type of the frame.
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
int width
picture width / height.
#define FF_ARRAY_ELEMS(a)
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static size_t mod_strcspn(const char *string, const char *reject)
common internal api header.
int key_frame
1 -> keyframe, 0-> not
static int ascii2index(const uint8_t *cpixel, int cpp)
This structure stores compressed data.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static unsigned hex_char_to_number(uint8_t x)