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
29
30 #define MIN_ELEMENT ' '
31 #define MAX_ELEMENT 0xfe
32 #define NB_ELEMENTS (MAX_ELEMENT - MIN_ELEMENT + 1)
33
39 } XPMDecContext;
40
42 const char *
name;
///< a string representing the name of the color
45
47 {
49 }
50
52 { "AliceBlue", 0xFFF0F8FF },
53 { "AntiqueWhite", 0xFFFAEBD7 },
54 { "Aqua", 0xFF00FFFF },
55 { "Aquamarine", 0xFF7FFFD4 },
56 { "Azure", 0xFFF0FFFF },
57 { "Beige", 0xFFF5F5DC },
58 { "Bisque", 0xFFFFE4C4 },
59 { "Black", 0xFF000000 },
60 { "BlanchedAlmond", 0xFFFFEBCD },
61 { "Blue", 0xFF0000FF },
62 { "BlueViolet", 0xFF8A2BE2 },
63 { "Brown", 0xFFA52A2A },
64 { "BurlyWood", 0xFFDEB887 },
65 { "CadetBlue", 0xFF5F9EA0 },
66 { "Chartreuse", 0xFF7FFF00 },
67 { "Chocolate", 0xFFD2691E },
68 { "Coral", 0xFFFF7F50 },
69 { "CornflowerBlue", 0xFF6495ED },
70 { "Cornsilk", 0xFFFFF8DC },
71 { "Crimson", 0xFFDC143C },
72 { "Cyan", 0xFF00FFFF },
73 { "DarkBlue", 0xFF00008B },
74 { "DarkCyan", 0xFF008B8B },
75 { "DarkGoldenRod", 0xFFB8860B },
76 { "DarkGray", 0xFFA9A9A9 },
77 { "DarkGreen", 0xFF006400 },
78 { "DarkKhaki", 0xFFBDB76B },
79 { "DarkMagenta", 0xFF8B008B },
80 { "DarkOliveGreen", 0xFF556B2F },
81 { "Darkorange", 0xFFFF8C00 },
82 { "DarkOrchid", 0xFF9932CC },
83 { "DarkRed", 0xFF8B0000 },
84 { "DarkSalmon", 0xFFE9967A },
85 { "DarkSeaGreen", 0xFF8FBC8F },
86 { "DarkSlateBlue", 0xFF483D8B },
87 { "DarkSlateGray", 0xFF2F4F4F },
88 { "DarkTurquoise", 0xFF00CED1 },
89 { "DarkViolet", 0xFF9400D3 },
90 { "DeepPink", 0xFFFF1493 },
91 { "DeepSkyBlue", 0xFF00BFFF },
92 { "DimGray", 0xFF696969 },
93 { "DodgerBlue", 0xFF1E90FF },
94 { "FireBrick", 0xFFB22222 },
95 { "FloralWhite", 0xFFFFFAF0 },
96 { "ForestGreen", 0xFF228B22 },
97 { "Fuchsia", 0xFFFF00FF },
98 { "Gainsboro", 0xFFDCDCDC },
99 { "GhostWhite", 0xFFF8F8FF },
100 { "Gold", 0xFFFFD700 },
101 { "GoldenRod", 0xFFDAA520 },
102 { "Gray", 0xFFBEBEBE },
103 { "Green", 0xFF00FF00 },
104 { "GreenYellow", 0xFFADFF2F },
105 { "HoneyDew", 0xFFF0FFF0 },
106 { "HotPink", 0xFFFF69B4 },
107 { "IndianRed", 0xFFCD5C5C },
108 { "Indigo", 0xFF4B0082 },
109 { "Ivory", 0xFFFFFFF0 },
110 { "Khaki", 0xFFF0E68C },
111 { "Lavender", 0xFFE6E6FA },
112 { "LavenderBlush", 0xFFFFF0F5 },
113 { "LawnGreen", 0xFF7CFC00 },
114 { "LemonChiffon", 0xFFFFFACD },
115 { "LightBlue", 0xFFADD8E6 },
116 { "LightCoral", 0xFFF08080 },
117 { "LightCyan", 0xFFE0FFFF },
118 { "LightGoldenRodYellow", 0xFFFAFAD2 },
119 { "LightGreen", 0xFF90EE90 },
120 { "LightGrey", 0xFFD3D3D3 },
121 { "LightPink", 0xFFFFB6C1 },
122 { "LightSalmon", 0xFFFFA07A },
123 { "LightSeaGreen", 0xFF20B2AA },
124 { "LightSkyBlue", 0xFF87CEFA },
125 { "LightSlateGray", 0xFF778899 },
126 { "LightSteelBlue", 0xFFB0C4DE },
127 { "LightYellow", 0xFFFFFFE0 },
128 { "Lime", 0xFF00FF00 },
129 { "LimeGreen", 0xFF32CD32 },
130 { "Linen", 0xFFFAF0E6 },
131 { "Magenta", 0xFFFF00FF },
132 { "Maroon", 0xFFB03060 },
133 { "MediumAquaMarine", 0xFF66CDAA },
134 { "MediumBlue", 0xFF0000CD },
135 { "MediumOrchid", 0xFFBA55D3 },
136 { "MediumPurple", 0xFF9370D8 },
137 { "MediumSeaGreen", 0xFF3CB371 },
138 { "MediumSlateBlue", 0xFF7B68EE },
139 { "MediumSpringGreen", 0xFF00FA9A },
140 { "MediumTurquoise", 0xFF48D1CC },
141 { "MediumVioletRed", 0xFFC71585 },
142 { "MidnightBlue", 0xFF191970 },
143 { "MintCream", 0xFFF5FFFA },
144 { "MistyRose", 0xFFFFE4E1 },
145 { "Moccasin", 0xFFFFE4B5 },
146 { "NavajoWhite", 0xFFFFDEAD },
147 { "Navy", 0xFF000080 },
148 { "None", 0x00000000 },
149 { "OldLace", 0xFFFDF5E6 },
150 { "Olive", 0xFF808000 },
151 { "OliveDrab", 0xFF6B8E23 },
152 { "Orange", 0xFFFFA500 },
153 { "OrangeRed", 0xFFFF4500 },
154 { "Orchid", 0xFFDA70D6 },
155 { "PaleGoldenRod", 0xFFEEE8AA },
156 { "PaleGreen", 0xFF98FB98 },
157 { "PaleTurquoise", 0xFFAFEEEE },
158 { "PaleVioletRed", 0xFFD87093 },
159 { "PapayaWhip", 0xFFFFEFD5 },
160 { "PeachPuff", 0xFFFFDAB9 },
161 { "Peru", 0xFFCD853F },
162 { "Pink", 0xFFFFC0CB },
163 { "Plum", 0xFFDDA0DD },
164 { "PowderBlue", 0xFFB0E0E6 },
165 { "Purple", 0xFFA020F0 },
166 { "Red", 0xFFFF0000 },
167 { "RosyBrown", 0xFFBC8F8F },
168 { "RoyalBlue", 0xFF4169E1 },
169 { "SaddleBrown", 0xFF8B4513 },
170 { "Salmon", 0xFFFA8072 },
171 { "SandyBrown", 0xFFF4A460 },
172 { "SeaGreen", 0xFF2E8B57 },
173 { "SeaShell", 0xFFFFF5EE },
174 { "Sienna", 0xFFA0522D },
175 { "Silver", 0xFFC0C0C0 },
176 { "SkyBlue", 0xFF87CEEB },
177 { "SlateBlue", 0xFF6A5ACD },
178 { "SlateGray", 0xFF708090 },
179 { "Snow", 0xFFFFFAFA },
180 { "SpringGreen", 0xFF00FF7F },
181 { "SteelBlue", 0xFF4682B4 },
182 { "Tan", 0xFFD2B48C },
183 { "Teal", 0xFF008080 },
184 { "Thistle", 0xFFD8BFD8 },
185 { "Tomato", 0xFFFF6347 },
186 { "Turquoise", 0xFF40E0D0 },
187 { "Violet", 0xFFEE82EE },
188 { "Wheat", 0xFFF5DEB3 },
189 { "White", 0xFFFFFFFF },
190 { "WhiteSmoke", 0xFFF5F5F5 },
191 { "Yellow", 0xFFFFFF00 },
192 { "YellowGreen", 0xFF9ACD32 }
193 };
194
196 {
197 #define TIMES256(idx) \
198 TIMES64(4 * (idx)) TIMES64(4 * (idx) + 1) TIMES64(4 * (idx) + 2) TIMES64(4 * (idx) + 3)
199 #define TIMES64(idx) \
200 TIMES16(4 * (idx)) TIMES16(4 * (idx) + 1) TIMES16(4 * (idx) + 2) TIMES16(4 * (idx) + 3)
201 #define TIMES16(idx) \
202 TIMES4(4 * (idx)) TIMES4(4 * (idx) + 1) TIMES4(4 * (idx) + 2) TIMES4(4 * (idx) + 3)
203 #define TIMES4(idx) \
204 ENTRY(4 * (idx)) ENTRY(4 * (idx) + 1) ENTRY(4 * (idx) + 2) ENTRY(4 * (idx) + 3)
205 #define ENTRY(x) [x] = ((x) >= 'a' && (x) <= 'f') ? (x) - ('a' - 10) : \
206 ((x) >= 'A' && (x) <= 'F') ? (x) - ('A' - 10) : \
207 ((x) >= '0' && (x) <= '9') ? (x) - '0' : 0,
208
209 static const uint8_t lut[] = {
211 };
212 return lut[x];
213 }
214
215 /*
216 * Function same as strcspn but ignores characters if they are inside a C style comments
217 */
219 {
221
222 for (
i = 0;
string &&
string[
i];
i++) {
223 if (
string[
i] ==
'/' &&
string[
i+1] ==
'*') {
225 while (
string &&
string[
i] && (
string[
i] !=
'*' ||
string[
i+1] !=
'/') )
228 }
else if (
string[
i] ==
'/' &&
string[
i+1] ==
'/') {
230 while (
string &&
string[
i] &&
string[
i] !=
'\n' )
232 } else {
233 for (j = 0; reject && reject[j]; j++) {
234 if (
string[
i] == reject[j])
235 break;
236 }
237 if (reject && reject[j])
238 break;
239 }
240 }
242 }
243
245 {
246 uint32_t
ret = 0xFF000000;
248 char color_name[100];
249
257 }
else if (
len == 4) {
262 }
else if (
len == 6) {
269 }
else if (
len == 8) {
278 }
279 } else {
281 strncpy(color_name,
p,
len);
282 color_name[
len] =
'0円';
283
284 entry = bsearch(color_name,
289
292
294 }
296 }
297
299 {
302
303 for (
i = 0;
i < cpp;
i++) {
308 }
309 return n;
310 }
311
314 {
316 const uint8_t *end, *ptr;
317 int ncolors, cpp,
ret,
i, j;
321
323
325 if (!x->buf)
327 memcpy(x->buf, avpkt->
data, avpkt->
size);
328 x->buf[avpkt->
size] = 0;
329
330 ptr = x->buf;
331 end = x->buf + avpkt->
size;
332 while (end - ptr > 9 && memcmp(ptr, "/* XPM */", 9))
333 ptr++;
334
335 if (end - ptr <= 9) {
338 }
339
341 if (sscanf(ptr, "\"%u %u %u %u\",",
345 }
346
349
350 if (cpp <= 0 || cpp >= 5) {
351 av_log(avctx,
AV_LOG_ERROR,
"unsupported/invalid number of chars per pixel: %d\n", cpp);
353 }
354
356 for (
i = 0;
i < cpp;
i++)
358
359 if (ncolors <= 0 || ncolors >
size) {
362 }
363
364 if (
size > SIZE_MAX / 4)
366
368
370 if (end - ptr < 1)
372
375
378
380 if (!x->pixels)
382
383 for (
i = 0;
i < ncolors;
i++) {
384 const uint8_t *
index;
386
388 if (end - ptr < cpp)
391 ptr += cpp;
392
393 ptr = strstr(ptr, "c ");
394 if (ptr) {
395 ptr += 2;
396 } else {
398 }
399
400 len = strcspn(ptr,
"\" ");
401
404
407 if (end - ptr < 1)
409 }
410
412 dst = (uint32_t *)(
p->data[0] +
i *
p->linesize[0]);
413 if (end - ptr < 1)
416 if (end - ptr < 1)
418
419 for (j = 0; j < avctx->
width; j++) {
420 if (end - ptr < cpp)
422
425
427 ptr += cpp;
428 }
430 }
431
432 *got_frame = 1;
433
435 }
436
438 {
441
443 x->buf_size = 0;
444
445 return 0;
446 }
447
454 .priv_data_size = sizeof(XPMDecContext),
458 };