| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
/* |
| 10 |
|
| 11 |
This file is partially based on source code of ImageMagick by John Cristy. Its |
| 12 |
license is as follows: |
| 13 |
|
| 14 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 15 |
% % |
| 16 |
% % |
| 17 |
% % |
| 18 |
% X X PPPP M M % |
| 19 |
% X X P P MM MM % |
| 20 |
% X PPPP M M M % |
| 21 |
% X X P M M % |
| 22 |
% X X P M M % |
| 23 |
% % |
| 24 |
% % |
| 25 |
% Read/Write ImageMagick Image Format. % |
| 26 |
% % |
| 27 |
% % |
| 28 |
% Software Design % |
| 29 |
% John Cristy % |
| 30 |
% July 1992 % |
| 31 |
% % |
| 32 |
% % |
| 33 |
% Copyright (C) 2001 ImageMagick Studio, a non-profit organization dedicated % |
| 34 |
% to making software imaging solutions freely available. % |
| 35 |
% % |
| 36 |
% Permission is hereby granted, free of charge, to any person obtaining a % |
| 37 |
% copy of this software and associated documentation files ("ImageMagick"), % |
| 38 |
% to deal in ImageMagick without restriction, including without limitation % |
| 39 |
% the rights to use, copy, modify, merge, publish, distribute, sublicense, % |
| 40 |
% and/or sell copies of ImageMagick, and to permit persons to whom the % |
| 41 |
% ImageMagick is furnished to do so, subject to the following conditions: % |
| 42 |
% % |
| 43 |
% The above copyright notice and this permission notice shall be included in % |
| 44 |
% all copies or substantial portions of ImageMagick. % |
| 45 |
% % |
| 46 |
% The software is provided "as is", without warranty of any kind, express or % |
| 47 |
% implied, including but not limited to the warranties of merchantability, % |
| 48 |
% fitness for a particular purpose and noninfringement. In no event shall % |
| 49 |
% ImageMagick Studio be liable for any claim, damages or other liability, % |
| 50 |
% whether in an action of contract, tort or otherwise, arising from, out of % |
| 51 |
% or in connection with ImageMagick or the use or other dealings in % |
| 52 |
% ImageMagick. % |
| 53 |
% % |
| 54 |
% Except as contained in this notice, the name of the ImageMagick Studio % |
| 55 |
% shall not be used in advertising or otherwise to promote the sale, use or % |
| 56 |
% other dealings in ImageMagick without prior written authorization from the % |
| 57 |
% ImageMagick Studio. % |
| 58 |
% % |
| 59 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 60 |
% |
| 61 |
% |
| 62 |
*/ |
| 63 |
|
| 64 |
#include "img-precomp.h" // Precompiled headers |
| 65 |
// |
| 66 |
#include <mrpt/core/exceptions.h> |
| 67 |
#include <mrpt/img/CImage.h> |
| 68 |
|
| 69 |
#include <cstring> |
| 70 |
#include <string> |
| 71 |
#include <unordered_map> |
| 72 |
|
| 73 |
#if MRPT_HAS_OPENCV |
| 74 |
struct XPMColorMapData |
| 75 |
{ |
| 76 |
unsigned char R{0}, G{0}, B{0}; |
| 77 |
}; |
| 78 |
using XPMColorMap = std::unordered_map<std::string, XPMColorMapData>; |
| 79 |
using Long2LongHash = std::unordered_map<int64_t, int64_t>; |
| 80 |
|
| 81 |
static const char* ParseColor(const char* data) |
| 82 |
{ |
| 83 |
static const char* const targets[] = {"c ", "g ", "g4 ", "m ", "b ", "s ", nullptr}; |
| 84 |
|
| 85 |
const char *p, *r; |
| 86 |
const char* q; |
| 87 |
for (int i = 0; targets[i] != nullptr; i++) |
| 88 |
{ |
| 89 |
r = data; |
| 90 |
for (q = targets[i]; *r != '\0'; r++) |
| 91 |
{ |
| 92 |
if (*r != *q) continue; |
| 93 |
if (!isspace(*(r - 1))) continue; |
| 94 |
p = r; |
| 95 |
for (;;) |
| 96 |
{ |
| 97 |
if (*q == '\0') return p; |
| 98 |
if (*p++ != *q++) break; |
| 99 |
} |
| 100 |
q = targets[i]; |
| 101 |
} |
| 102 |
} |
| 103 |
return nullptr; |
| 104 |
} |
| 105 |
|
| 106 |
/*****************************************************************************\ |
| 107 |
* rgbtab.h * |
| 108 |
* * |
| 109 |
* A hard coded rgb.txt. To keep it short I removed all colornames with * |
| 110 |
* trailing numbers, Blue3 etc, except the GrayXX. Sorry Grey-lovers I prefer * |
| 111 |
* Gray ;-). But Grey is recognized on lookups, only on save Gray will be * |
| 112 |
* used, maybe you want to do some substitue there too. * |
| 113 |
* * |
| 114 |
* To save memory the RGBs are coded in one long value, as done by the RGB * |
| 115 |
* macro. * |
| 116 |
* * |
| 117 |
* Developed by HeDu 3/94 ([email protected]) * |
| 118 |
\*****************************************************************************/ |
| 119 |
|
| 120 |
typedef struct |
| 121 |
{ |
| 122 |
const char* name; |
| 123 |
uint32_t rgb; |
| 124 |
} rgbRecord; |
| 125 |
|
| 126 |
#define myRGB(r, g, b) \ |
| 127 |
(static_cast<uint32_t>(r) << 16 | static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b)) |
| 128 |
|
| 129 |
static const rgbRecord theRGBRecords[] = { |
| 130 |
{ "aliceblue", myRGB(240, 248, 255)}, |
| 131 |
{ "antiquewhite", myRGB(250, 235, 215)}, |
| 132 |
{ "aquamarine", myRGB(50, 191, 193)}, |
| 133 |
{ "azure", myRGB(240, 255, 255)}, |
| 134 |
{ "beige", myRGB(245, 245, 220)}, |
| 135 |
{ "bisque", myRGB(255, 228, 196)}, |
| 136 |
{ "black", myRGB(0, 0, 0)}, |
| 137 |
{ "blanchedalmond", myRGB(255, 235, 205)}, |
| 138 |
{ "blue", myRGB(0, 0, 255)}, |
| 139 |
{ "blueviolet", myRGB(138, 43, 226)}, |
| 140 |
{ "brown", myRGB(165, 42, 42)}, |
| 141 |
{ "burlywood", myRGB(222, 184, 135)}, |
| 142 |
{ "cadetblue", myRGB(95, 146, 158)}, |
| 143 |
{ "chartreuse", myRGB(127, 255, 0)}, |
| 144 |
{ "chocolate", myRGB(210, 105, 30)}, |
| 145 |
{ "coral", myRGB(255, 114, 86)}, |
| 146 |
{ "cornflowerblue", myRGB(34, 34, 152)}, |
| 147 |
{ "cornsilk", myRGB(255, 248, 220)}, |
| 148 |
{ "cyan", myRGB(0, 255, 255)}, |
| 149 |
{ "darkgoldenrod", myRGB(184, 134, 11)}, |
| 150 |
{ "darkgreen", myRGB(0, 86, 45)}, |
| 151 |
{ "darkkhaki", myRGB(189, 183, 107)}, |
| 152 |
{ "darkolivegreen", myRGB(85, 86, 47)}, |
| 153 |
{ "darkorange", myRGB(255, 140, 0)}, |
| 154 |
{ "darkorchid", myRGB(139, 32, 139)}, |
| 155 |
{ "darksalmon", myRGB(233, 150, 122)}, |
| 156 |
{ "darkseagreen", myRGB(143, 188, 143)}, |
| 157 |
{ "darkslateblue", myRGB(56, 75, 102)}, |
| 158 |
{ "darkslategray", myRGB(47, 79, 79)}, |
| 159 |
{ "darkturquoise", myRGB(0, 166, 166)}, |
| 160 |
{ "darkviolet", myRGB(148, 0, 211)}, |
| 161 |
{ "deeppink", myRGB(255, 20, 147)}, |
| 162 |
{ "deepskyblue", myRGB(0, 191, 255)}, |
| 163 |
{ "dimgray", myRGB(84, 84, 84)}, |
| 164 |
{ "dodgerblue", myRGB(30, 144, 255)}, |
| 165 |
{ "firebrick", myRGB(142, 35, 35)}, |
| 166 |
{ "floralwhite", myRGB(255, 250, 240)}, |
| 167 |
{ "forestgreen", myRGB(80, 159, 105)}, |
| 168 |
{ "gainsboro", myRGB(220, 220, 220)}, |
| 169 |
{ "ghostwhite", myRGB(248, 248, 255)}, |
| 170 |
{ "gold", myRGB(218, 170, 0)}, |
| 171 |
{ "goldenrod", myRGB(239, 223, 132)}, |
| 172 |
{ "gray", myRGB(126, 126, 126)}, |
| 173 |
{ "gray0", myRGB(0, 0, 0)}, |
| 174 |
{ "gray1", myRGB(3, 3, 3)}, |
| 175 |
{ "gray10", myRGB(26, 26, 26)}, |
| 176 |
{ "gray100", myRGB(255, 255, 255)}, |
| 177 |
{ "gray11", myRGB(28, 28, 28)}, |
| 178 |
{ "gray12", myRGB(31, 31, 31)}, |
| 179 |
{ "gray13", myRGB(33, 33, 33)}, |
| 180 |
{ "gray14", myRGB(36, 36, 36)}, |
| 181 |
{ "gray15", myRGB(38, 38, 38)}, |
| 182 |
{ "gray16", myRGB(41, 41, 41)}, |
| 183 |
{ "gray17", myRGB(43, 43, 43)}, |
| 184 |
{ "gray18", myRGB(46, 46, 46)}, |
| 185 |
{ "gray19", myRGB(48, 48, 48)}, |
| 186 |
{ "gray2", myRGB(5, 5, 5)}, |
| 187 |
{ "gray20", myRGB(51, 51, 51)}, |
| 188 |
{ "gray21", myRGB(54, 54, 54)}, |
| 189 |
{ "gray22", myRGB(56, 56, 56)}, |
| 190 |
{ "gray23", myRGB(59, 59, 59)}, |
| 191 |
{ "gray24", myRGB(61, 61, 61)}, |
| 192 |
{ "gray25", myRGB(64, 64, 64)}, |
| 193 |
{ "gray26", myRGB(66, 66, 66)}, |
| 194 |
{ "gray27", myRGB(69, 69, 69)}, |
| 195 |
{ "gray28", myRGB(71, 71, 71)}, |
| 196 |
{ "gray29", myRGB(74, 74, 74)}, |
| 197 |
{ "gray3", myRGB(8, 8, 8)}, |
| 198 |
{ "gray30", myRGB(77, 77, 77)}, |
| 199 |
{ "gray31", myRGB(79, 79, 79)}, |
| 200 |
{ "gray32", myRGB(82, 82, 82)}, |
| 201 |
{ "gray33", myRGB(84, 84, 84)}, |
| 202 |
{ "gray34", myRGB(87, 87, 87)}, |
| 203 |
{ "gray35", myRGB(89, 89, 89)}, |
| 204 |
{ "gray36", myRGB(92, 92, 92)}, |
| 205 |
{ "gray37", myRGB(94, 94, 94)}, |
| 206 |
{ "gray38", myRGB(97, 97, 97)}, |
| 207 |
{ "gray39", myRGB(99, 99, 99)}, |
| 208 |
{ "gray4", myRGB(10, 10, 10)}, |
| 209 |
{ "gray40", myRGB(102, 102, 102)}, |
| 210 |
{ "gray41", myRGB(105, 105, 105)}, |
| 211 |
{ "gray42", myRGB(107, 107, 107)}, |
| 212 |
{ "gray43", myRGB(110, 110, 110)}, |
| 213 |
{ "gray44", myRGB(112, 112, 112)}, |
| 214 |
{ "gray45", myRGB(115, 115, 115)}, |
| 215 |
{ "gray46", myRGB(117, 117, 117)}, |
| 216 |
{ "gray47", myRGB(120, 120, 120)}, |
| 217 |
{ "gray48", myRGB(122, 122, 122)}, |
| 218 |
{ "gray49", myRGB(125, 125, 125)}, |
| 219 |
{ "gray5", myRGB(13, 13, 13)}, |
| 220 |
{ "gray50", myRGB(127, 127, 127)}, |
| 221 |
{ "gray51", myRGB(130, 130, 130)}, |
| 222 |
{ "gray52", myRGB(133, 133, 133)}, |
| 223 |
{ "gray53", myRGB(135, 135, 135)}, |
| 224 |
{ "gray54", myRGB(138, 138, 138)}, |
| 225 |
{ "gray55", myRGB(140, 140, 140)}, |
| 226 |
{ "gray56", myRGB(143, 143, 143)}, |
| 227 |
{ "gray57", myRGB(145, 145, 145)}, |
| 228 |
{ "gray58", myRGB(148, 148, 148)}, |
| 229 |
{ "gray59", myRGB(150, 150, 150)}, |
| 230 |
{ "gray6", myRGB(15, 15, 15)}, |
| 231 |
{ "gray60", myRGB(153, 153, 153)}, |
| 232 |
{ "gray61", myRGB(156, 156, 156)}, |
| 233 |
{ "gray62", myRGB(158, 158, 158)}, |
| 234 |
{ "gray63", myRGB(161, 161, 161)}, |
| 235 |
{ "gray64", myRGB(163, 163, 163)}, |
| 236 |
{ "gray65", myRGB(166, 166, 166)}, |
| 237 |
{ "gray66", myRGB(168, 168, 168)}, |
| 238 |
{ "gray67", myRGB(171, 171, 171)}, |
| 239 |
{ "gray68", myRGB(173, 173, 173)}, |
| 240 |
{ "gray69", myRGB(176, 176, 176)}, |
| 241 |
{ "gray7", myRGB(18, 18, 18)}, |
| 242 |
{ "gray70", myRGB(179, 179, 179)}, |
| 243 |
{ "gray71", myRGB(181, 181, 181)}, |
| 244 |
{ "gray72", myRGB(184, 184, 184)}, |
| 245 |
{ "gray73", myRGB(186, 186, 186)}, |
| 246 |
{ "gray74", myRGB(189, 189, 189)}, |
| 247 |
{ "gray75", myRGB(191, 191, 191)}, |
| 248 |
{ "gray76", myRGB(194, 194, 194)}, |
| 249 |
{ "gray77", myRGB(196, 196, 196)}, |
| 250 |
{ "gray78", myRGB(199, 199, 199)}, |
| 251 |
{ "gray79", myRGB(201, 201, 201)}, |
| 252 |
{ "gray8", myRGB(20, 20, 20)}, |
| 253 |
{ "gray80", myRGB(204, 204, 204)}, |
| 254 |
{ "gray81", myRGB(207, 207, 207)}, |
| 255 |
{ "gray82", myRGB(209, 209, 209)}, |
| 256 |
{ "gray83", myRGB(212, 212, 212)}, |
| 257 |
{ "gray84", myRGB(214, 214, 214)}, |
| 258 |
{ "gray85", myRGB(217, 217, 217)}, |
| 259 |
{ "gray86", myRGB(219, 219, 219)}, |
| 260 |
{ "gray87", myRGB(222, 222, 222)}, |
| 261 |
{ "gray88", myRGB(224, 224, 224)}, |
| 262 |
{ "gray89", myRGB(227, 227, 227)}, |
| 263 |
{ "gray9", myRGB(23, 23, 23)}, |
| 264 |
{ "gray90", myRGB(229, 229, 229)}, |
| 265 |
{ "gray91", myRGB(232, 232, 232)}, |
| 266 |
{ "gray92", myRGB(235, 235, 235)}, |
| 267 |
{ "gray93", myRGB(237, 237, 237)}, |
| 268 |
{ "gray94", myRGB(240, 240, 240)}, |
| 269 |
{ "gray95", myRGB(242, 242, 242)}, |
| 270 |
{ "gray96", myRGB(245, 245, 245)}, |
| 271 |
{ "gray97", myRGB(247, 247, 247)}, |
| 272 |
{ "gray98", myRGB(250, 250, 250)}, |
| 273 |
{ "gray99", myRGB(252, 252, 252)}, |
| 274 |
{ "green", myRGB(0, 255, 0)}, |
| 275 |
{ "greenyellow", myRGB(173, 255, 47)}, |
| 276 |
{ "honeydew", myRGB(240, 255, 240)}, |
| 277 |
{ "hotpink", myRGB(255, 105, 180)}, |
| 278 |
{ "indianred", myRGB(107, 57, 57)}, |
| 279 |
{ "ivory", myRGB(255, 255, 240)}, |
| 280 |
{ "khaki", myRGB(179, 179, 126)}, |
| 281 |
{ "lavender", myRGB(230, 230, 250)}, |
| 282 |
{ "lavenderblush", myRGB(255, 240, 245)}, |
| 283 |
{ "lawngreen", myRGB(124, 252, 0)}, |
| 284 |
{ "lemonchiffon", myRGB(255, 250, 205)}, |
| 285 |
{ "lightblue", myRGB(176, 226, 255)}, |
| 286 |
{ "lightcoral", myRGB(240, 128, 128)}, |
| 287 |
{ "lightcyan", myRGB(224, 255, 255)}, |
| 288 |
{ "lightgoldenrod", myRGB(238, 221, 130)}, |
| 289 |
{"lightgoldenrodyellow", myRGB(250, 250, 210)}, |
| 290 |
{ "lightgray", myRGB(168, 168, 168)}, |
| 291 |
{ "lightpink", myRGB(255, 182, 193)}, |
| 292 |
{ "lightsalmon", myRGB(255, 160, 122)}, |
| 293 |
{ "lightseagreen", myRGB(32, 178, 170)}, |
| 294 |
{ "lightskyblue", myRGB(135, 206, 250)}, |
| 295 |
{ "lightslateblue", myRGB(132, 112, 255)}, |
| 296 |
{ "lightslategray", myRGB(119, 136, 153)}, |
| 297 |
{ "lightsteelblue", myRGB(124, 152, 211)}, |
| 298 |
{ "lightyellow", myRGB(255, 255, 224)}, |
| 299 |
{ "limegreen", myRGB(0, 175, 20)}, |
| 300 |
{ "linen", myRGB(250, 240, 230)}, |
| 301 |
{ "magenta", myRGB(255, 0, 255)}, |
| 302 |
{ "maroon", myRGB(143, 0, 82)}, |
| 303 |
{ "mediumaquamarine", myRGB(0, 147, 143)}, |
| 304 |
{ "mediumblue", myRGB(50, 50, 204)}, |
| 305 |
{ "mediumforestgreen", myRGB(50, 129, 75)}, |
| 306 |
{ "mediumgoldenrod", myRGB(209, 193, 102)}, |
| 307 |
{ "mediumorchid", myRGB(189, 82, 189)}, |
| 308 |
{ "mediumpurple", myRGB(147, 112, 219)}, |
| 309 |
{ "mediumseagreen", myRGB(52, 119, 102)}, |
| 310 |
{ "mediumslateblue", myRGB(106, 106, 141)}, |
| 311 |
{ "mediumspringgreen", myRGB(35, 142, 35)}, |
| 312 |
{ "mediumturquoise", myRGB(0, 210, 210)}, |
| 313 |
{ "mediumvioletred", myRGB(213, 32, 121)}, |
| 314 |
{ "midnightblue", myRGB(47, 47, 100)}, |
| 315 |
{ "mintcream", myRGB(245, 255, 250)}, |
| 316 |
{ "mistyrose", myRGB(255, 228, 225)}, |
| 317 |
{ "moccasin", myRGB(255, 228, 181)}, |
| 318 |
{ "navajowhite", myRGB(255, 222, 173)}, |
| 319 |
{ "navy", myRGB(35, 35, 117)}, |
| 320 |
{ "navyblue", myRGB(35, 35, 117)}, |
| 321 |
{ "oldlace", myRGB(253, 245, 230)}, |
| 322 |
{ "olivedrab", myRGB(107, 142, 35)}, |
| 323 |
{ "orange", myRGB(255, 135, 0)}, |
| 324 |
{ "orangered", myRGB(255, 69, 0)}, |
| 325 |
{ "orchid", myRGB(239, 132, 239)}, |
| 326 |
{ "palegoldenrod", myRGB(238, 232, 170)}, |
| 327 |
{ "palegreen", myRGB(115, 222, 120)}, |
| 328 |
{ "paleturquoise", myRGB(175, 238, 238)}, |
| 329 |
{ "palevioletred", myRGB(219, 112, 147)}, |
| 330 |
{ "papayawhip", myRGB(255, 239, 213)}, |
| 331 |
{ "peachpuff", myRGB(255, 218, 185)}, |
| 332 |
{ "peru", myRGB(205, 133, 63)}, |
| 333 |
{ "pink", myRGB(255, 181, 197)}, |
| 334 |
{ "plum", myRGB(197, 72, 155)}, |
| 335 |
{ "powderblue", myRGB(176, 224, 230)}, |
| 336 |
{ "purple", myRGB(160, 32, 240)}, |
| 337 |
{ "red", myRGB(255, 0, 0)}, |
| 338 |
{ "rosybrown", myRGB(188, 143, 143)}, |
| 339 |
{ "royalblue", myRGB(65, 105, 225)}, |
| 340 |
{ "saddlebrown", myRGB(139, 69, 19)}, |
| 341 |
{ "salmon", myRGB(233, 150, 122)}, |
| 342 |
{ "sandybrown", myRGB(244, 164, 96)}, |
| 343 |
{ "seagreen", myRGB(82, 149, 132)}, |
| 344 |
{ "seashell", myRGB(255, 245, 238)}, |
| 345 |
{ "sienna", myRGB(150, 82, 45)}, |
| 346 |
{ "silver", myRGB(192, 192, 192)}, |
| 347 |
{ "skyblue", myRGB(114, 159, 255)}, |
| 348 |
{ "slateblue", myRGB(126, 136, 171)}, |
| 349 |
{ "slategray", myRGB(112, 128, 144)}, |
| 350 |
{ "snow", myRGB(255, 250, 250)}, |
| 351 |
{ "springgreen", myRGB(65, 172, 65)}, |
| 352 |
{ "steelblue", myRGB(84, 112, 170)}, |
| 353 |
{ "tan", myRGB(222, 184, 135)}, |
| 354 |
{ "thistle", myRGB(216, 191, 216)}, |
| 355 |
{ "tomato", myRGB(255, 99, 71)}, |
| 356 |
{ "transparent", myRGB(0, 0, 1)}, |
| 357 |
{ "turquoise", myRGB(25, 204, 223)}, |
| 358 |
{ "violet", myRGB(156, 62, 206)}, |
| 359 |
{ "violetred", myRGB(243, 62, 150)}, |
| 360 |
{ "wheat", myRGB(245, 222, 179)}, |
| 361 |
{ "white", myRGB(255, 255, 255)}, |
| 362 |
{ "whitesmoke", myRGB(245, 245, 245)}, |
| 363 |
{ "yellow", myRGB(255, 255, 0)}, |
| 364 |
{ "yellowgreen", myRGB(50, 216, 56)}, |
| 365 |
{ nullptr, myRGB(0, 0, 0)} |
| 366 |
}; |
| 367 |
static const int numTheRGBRecords = 235; |
| 368 |
|
| 369 |
namespace |
| 370 |
{ |
| 371 |
unsigned char ParseHexadecimal(char digit1, char digit2) |
| 372 |
{ |
| 373 |
unsigned char i1, i2; |
| 374 |
|
| 375 |
if (digit1 >= 'a') |
| 376 |
i1 = (unsigned char)(digit1 - 'a' + 0x0A); |
| 377 |
else if (digit1 >= 'A') |
| 378 |
i1 = (unsigned char)(digit1 - 'A' + 0x0A); |
| 379 |
else |
| 380 |
i1 = (unsigned char)(digit1 - '0'); |
| 381 |
if (digit2 >= 'a') |
| 382 |
i2 = (unsigned char)(digit2 - 'a' + 0x0A); |
| 383 |
else if (digit2 >= 'A') |
| 384 |
i2 = (unsigned char)(digit2 - 'A' + 0x0A); |
| 385 |
else |
| 386 |
i2 = (unsigned char)(digit2 - '0'); |
| 387 |
return (unsigned char)(0x10 * i1 + i2); |
| 388 |
} |
| 389 |
|
| 390 |
bool GetRGBFromName( |
| 391 |
const char* inname, bool* isNone, unsigned char* r, unsigned char* g, unsigned char* b) |
| 392 |
{ |
| 393 |
int left, right, middle; |
| 394 |
int cmp; |
| 395 |
uint32_t rgbVal; |
| 396 |
char* name; |
| 397 |
char *grey, *p; |
| 398 |
|
| 399 |
// Neither #rrggbb nor #rrrrggggbbbb are in database, we parse them directly |
| 400 |
size_t inname_len = strlen(inname); |
| 401 |
if (*inname == '#' && (inname_len == 7 || inname_len == 13)) |
| 402 |
{ |
| 403 |
size_t ofs = (inname_len == 7) ? 2 : 4; |
| 404 |
*r = ParseHexadecimal(inname[1], inname[2]); |
| 405 |
*g = ParseHexadecimal(inname[1 * ofs + 1], inname[1 * ofs + 2]); |
| 406 |
*b = ParseHexadecimal(inname[2 * ofs + 1], inname[2 * ofs + 2]); |
| 407 |
*isNone = false; |
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
name = ::strdup(inname); |
| 412 |
|
| 413 |
// theRGBRecords[] has no names with spaces, and no grey, but a |
| 414 |
// lot of gray... |
| 415 |
|
| 416 |
// so first extract ' ' |
| 417 |
while ((p = strchr(name, ' ')) != nullptr) |
| 418 |
{ |
| 419 |
while (*(p)) // till eof of string |
| 420 |
{ |
| 421 |
*p = *(p + 1); // copy to the left |
| 422 |
p++; |
| 423 |
} |
| 424 |
} |
| 425 |
// fold to lower case |
| 426 |
p = name; |
| 427 |
while (*p) |
| 428 |
{ |
| 429 |
*p = (char)tolower(*p); |
| 430 |
p++; |
| 431 |
} |
| 432 |
|
| 433 |
// substitute Grey with Gray, else rgbtab.h would have more than 100 |
| 434 |
// 'duplicate' entries |
| 435 |
if ((grey = strstr(name, "grey")) != nullptr) grey[2] = 'a'; |
| 436 |
|
| 437 |
// check for special 'none' colour: |
| 438 |
bool found; |
| 439 |
if (strcmp(name, "none") == 0) |
| 440 |
{ |
| 441 |
*isNone = true; |
| 442 |
found = true; |
| 443 |
} |
| 444 |
else // not "None" |
| 445 |
{ |
| 446 |
found = false; |
| 447 |
|
| 448 |
// binary search: |
| 449 |
left = 0; |
| 450 |
right = numTheRGBRecords - 1; |
| 451 |
do |
| 452 |
{ |
| 453 |
middle = (left + right) / 2; |
| 454 |
cmp = strcmp(name, theRGBRecords[middle].name); |
| 455 |
if (cmp == 0) |
| 456 |
{ |
| 457 |
rgbVal = theRGBRecords[middle].rgb; |
| 458 |
*r = (unsigned char)((rgbVal >> 16) & 0xFF); |
| 459 |
*g = (unsigned char)((rgbVal >> 8) & 0xFF); |
| 460 |
*b = (unsigned char)((rgbVal)&0xFF); |
| 461 |
*isNone = false; |
| 462 |
found = true; |
| 463 |
break; |
| 464 |
} |
| 465 |
else if (cmp < 0) |
| 466 |
{ |
| 467 |
right = middle - 1; |
| 468 |
} |
| 469 |
else // cmp > 0 |
| 470 |
{ |
| 471 |
left = middle + 1; |
| 472 |
} |
| 473 |
} while (left <= right); |
| 474 |
} |
| 475 |
|
| 476 |
free(name); |
| 477 |
|
| 478 |
return found; |
| 479 |
} |
| 480 |
} // namespace |
| 481 |
#endif |
| 482 |
|
| 483 |
/** Loads the image from an XPM array, as included from a ".xpm" file. |
| 484 |
* \sa loadFromFile |
| 485 |
* \return false on any error */ |
| 486 |
bool mrpt::img::CImage::loadFromXPM( |
| 487 |
[[maybe_unused]] const char* const* xpm_data, [[maybe_unused]] bool swap_rb) |
| 488 |
{ |
| 489 |
#if MRPT_HAS_OPENCV |
| 490 |
try |
| 491 |
{ |
| 492 |
ASSERTMSG_(xpm_data, "XPM data is nullptr"); |
| 493 |
|
| 494 |
char key[64]; |
| 495 |
XPMColorMap clr_tbl; |
| 496 |
std::string maskKey, keyString; |
| 497 |
|
| 498 |
/* |
| 499 |
* Read hints and initialize structures: |
| 500 |
*/ |
| 501 |
unsigned width, height, colors_cnt, chars_per_pixel; |
| 502 |
const int count = |
| 503 |
::sscanf(xpm_data[0], "%u %u %u %u", &width, &height, &colors_cnt, &chars_per_pixel); |
| 504 |
if (count != 4 || width * height * colors_cnt == 0) |
| 505 |
{ |
| 506 |
THROW_EXCEPTION("XPM: incorrect header format!"); |
| 507 |
} |
| 508 |
|
| 509 |
// VS: XPM color map this large would be insane, since XPMs are encoded |
| 510 |
// with 92 possible values on each position, 92^64 is *way* larger space |
| 511 |
// than 8bit RGB... |
| 512 |
ASSERTMSG_(chars_per_pixel < 64, "XPM colormaps this large not supported."); |
| 513 |
|
| 514 |
this->resize(width, height, CH_RGB); |
| 515 |
key[chars_per_pixel] = '\0'; |
| 516 |
|
| 517 |
/* |
| 518 |
* Create colour map: |
| 519 |
*/ |
| 520 |
XPMColorMapData clr_data; |
| 521 |
for (size_t i = 0; i < colors_cnt; i++) |
| 522 |
{ |
| 523 |
const char* xmpColLine = xpm_data[1 + i]; |
| 524 |
|
| 525 |
// we must have at least " x y" after the colour index, hence +5 |
| 526 |
if (!xmpColLine || strlen(xmpColLine) < chars_per_pixel + 5) |
| 527 |
{ |
| 528 |
THROW_EXCEPTION_FMT("XPM: incorrect colour description in line %d", (int)(1 + i)); |
| 529 |
} |
| 530 |
|
| 531 |
for (size_t i_key = 0; i_key < chars_per_pixel; i_key++) key[i_key] = xmpColLine[i_key]; |
| 532 |
const char* clr_def = ParseColor(xmpColLine + chars_per_pixel); |
| 533 |
|
| 534 |
if (clr_def == nullptr) |
| 535 |
{ |
| 536 |
THROW_EXCEPTION_FMT( |
| 537 |
"XPM: malformed colour definition '%s' at line %d!", xmpColLine, (int)(1 + i)); |
| 538 |
} |
| 539 |
|
| 540 |
bool isNone = false; |
| 541 |
if (!GetRGBFromName(clr_def, &isNone, &clr_data.R, &clr_data.G, &clr_data.B)) |
| 542 |
{ |
| 543 |
THROW_EXCEPTION_FMT( |
| 544 |
"XPM: malformed colour definition '%s' at line %d!", xmpColLine, (int)(1 + i)); |
| 545 |
} |
| 546 |
|
| 547 |
keyString = key; |
| 548 |
if (isNone) maskKey = keyString; |
| 549 |
|
| 550 |
clr_tbl[keyString] = clr_data; |
| 551 |
} |
| 552 |
|
| 553 |
// deal with the mask: we must replace pseudo-colour "None" with the |
| 554 |
// mask colour (which can be any colour not otherwise used in the image) |
| 555 |
if (!maskKey.empty()) |
| 556 |
{ |
| 557 |
Long2LongHash rgb_table; |
| 558 |
long rgb; |
| 559 |
const size_t n = clr_tbl.size(); |
| 560 |
auto iter = clr_tbl.begin(); |
| 561 |
for (size_t i = 0; i < n; ++i, ++iter) |
| 562 |
{ |
| 563 |
const XPMColorMapData& data = iter->second; |
| 564 |
rgb = (data.R << 16) + (data.G << 8) + data.B; |
| 565 |
rgb_table[rgb]; |
| 566 |
} |
| 567 |
for (rgb = 0; rgb <= 0xffffff && rgb_table.count(rgb); ++rgb) |
| 568 |
; |
| 569 |
if (rgb > 0xffffff) |
| 570 |
{ |
| 571 |
THROW_EXCEPTION("XPM: no colors left to use for mask!"); |
| 572 |
} |
| 573 |
|
| 574 |
XPMColorMapData& maskData = clr_tbl[maskKey]; |
| 575 |
maskData.R = uint8_t(rgb >> 16); |
| 576 |
maskData.G = uint8_t(rgb >> 8); |
| 577 |
maskData.B = uint8_t(rgb); |
| 578 |
|
| 579 |
// MRPT CImage does not implement masks (!!) |
| 580 |
// img.SetMaskColour(maskData.R, maskData.G, maskData.B); |
| 581 |
} |
| 582 |
|
| 583 |
/* |
| 584 |
* Parse image data: |
| 585 |
*/ |
| 586 |
unsigned char* img_data = (*this)(0, 0); |
| 587 |
auto end = clr_tbl.end(); |
| 588 |
|
| 589 |
for (size_t j = 0; j < height; j++) |
| 590 |
{ |
| 591 |
for (size_t i = 0; i < width; i++, img_data += 3) |
| 592 |
{ |
| 593 |
const char* xpmImgLine = xpm_data[1 + colors_cnt + j]; |
| 594 |
if (!xpmImgLine || strlen(xpmImgLine) < width * chars_per_pixel) |
| 595 |
{ |
| 596 |
THROW_EXCEPTION_FMT("XPM: truncated image data at line %d!", (int)(1 + colors_cnt + j)); |
| 597 |
return false; |
| 598 |
} |
| 599 |
|
| 600 |
for (size_t i_key = 0; i_key < chars_per_pixel; i_key++) |
| 601 |
{ |
| 602 |
key[i_key] = xpmImgLine[chars_per_pixel * i + i_key]; |
| 603 |
} |
| 604 |
|
| 605 |
keyString = key; |
| 606 |
auto entry = clr_tbl.find(keyString); |
| 607 |
if (entry == end) |
| 608 |
{ |
| 609 |
THROW_EXCEPTION("XPM: Malformed pixel data!"); |
| 610 |
|
| 611 |
// better return right now as otherwise we risk to flood the |
| 612 |
// user with error messages as something seems to be |
| 613 |
// seriously wrong with the file and so we could give this |
| 614 |
// message for each remaining pixel if we don't bail out |
| 615 |
return false; |
| 616 |
} |
| 617 |
|
| 618 |
img_data[0] = entry->second.R; |
| 619 |
img_data[1] = entry->second.G; |
| 620 |
img_data[2] = entry->second.B; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
if (swap_rb) this->swapRB(); |
| 625 |
|
| 626 |
return true; |
| 627 |
} |
| 628 |
catch (const std::exception& e) |
| 629 |
{ |
| 630 |
std::cerr << "[CImage::loadFromXPM] " << e.what() << std::endl; |
| 631 |
return false; |
| 632 |
} |
| 633 |
#else |
| 634 |
return false; |
| 635 |
#endif |
| 636 |
} |