cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % X X PPPP M M % |
| 7 | % X X P P MM MM % |
| 8 | % X PPPP M M M % |
| 9 | % X X P M M % |
| 10 | % X X P M M % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write X Windows system Pixmap Format % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
Cristy | f6ff9ea | 2016-12-05 09:53:35 -0500 | [diff] [blame] | 20 | % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
Cristy | f19d414 | 2017-04-24 11:34:30 -0400 | [diff] [blame^] | 26 | % https://www.imagemagick.org/script/license.php % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
| 43 | #include "MagickCore/attribute.h" |
| 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/color.h" |
| 48 | #include "MagickCore/color-private.h" |
| 49 | #include "MagickCore/colormap.h" |
| 50 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 51 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 52 | #include "MagickCore/exception.h" |
| 53 | #include "MagickCore/exception-private.h" |
| 54 | #include "MagickCore/geometry.h" |
| 55 | #include "MagickCore/image.h" |
| 56 | #include "MagickCore/image-private.h" |
| 57 | #include "MagickCore/list.h" |
| 58 | #include "MagickCore/magick.h" |
| 59 | #include "MagickCore/memory_.h" |
| 60 | #include "MagickCore/monitor.h" |
| 61 | #include "MagickCore/monitor-private.h" |
| 62 | #include "MagickCore/pixel-accessor.h" |
| 63 | #include "MagickCore/quantize.h" |
| 64 | #include "MagickCore/quantum-private.h" |
| 65 | #include "MagickCore/resize.h" |
| 66 | #include "MagickCore/resource_.h" |
| 67 | #include "MagickCore/splay-tree.h" |
| 68 | #include "MagickCore/static.h" |
| 69 | #include "MagickCore/string_.h" |
| 70 | #include "MagickCore/module.h" |
| 71 | #include "MagickCore/threshold.h" |
| 72 | #include "MagickCore/utility.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | Forward declarations. |
| 76 | */ |
| 77 | static MagickBooleanType |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 78 | WritePICONImage(const ImageInfo *,Image *,ExceptionInfo *), |
| 79 | WriteXPMImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 83 | % % |
| 84 | % % |
| 85 | % % |
| 86 | % I s X P M % |
| 87 | % % |
| 88 | % % |
| 89 | % % |
| 90 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 91 | % |
| 92 | % IsXPM() returns MagickTrue if the image format type, identified by the |
| 93 | % magick string, is XPM. |
| 94 | % |
| 95 | % The format of the IsXPM method is: |
| 96 | % |
| 97 | % MagickBooleanType IsXPM(const unsigned char *magick,const size_t length) |
| 98 | % |
| 99 | % A description of each parameter follows: |
| 100 | % |
| 101 | % o magick: compare image format pattern against these bytes. or |
| 102 | % blob. |
| 103 | % |
| 104 | % o length: Specifies the length of the magick string. |
| 105 | % |
| 106 | */ |
| 107 | static MagickBooleanType IsXPM(const unsigned char *magick,const size_t length) |
| 108 | { |
| 109 | if (length < 9) |
| 110 | return(MagickFalse); |
| 111 | if (LocaleNCompare((char *) magick+1,"* XPM *",7) == 0) |
| 112 | return(MagickTrue); |
| 113 | return(MagickFalse); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 118 | % % |
| 119 | % % |
| 120 | % % |
| 121 | % R e a d X P M I m a g e % |
| 122 | % % |
| 123 | % % |
| 124 | % % |
| 125 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 126 | % |
| 127 | % ReadXPMImage() reads an X11 pixmap image file and returns it. It |
| 128 | % allocates the memory necessary for the new Image structure and returns a |
| 129 | % pointer to the new image. |
| 130 | % |
| 131 | % The format of the ReadXPMImage method is: |
| 132 | % |
| 133 | % Image *ReadXPMImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 134 | % |
| 135 | % A description of each parameter follows: |
| 136 | % |
| 137 | % o image_info: the image info. |
| 138 | % |
| 139 | % o exception: return any errors or warnings in this structure. |
| 140 | % |
| 141 | */ |
| 142 | |
| 143 | static int CompareXPMColor(const void *target,const void *source) |
cristy | 2f4741f | 2010-11-01 23:29:42 +0000 | [diff] [blame] | 144 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 145 | const char |
| 146 | *p, |
| 147 | *q; |
cristy | 2f4741f | 2010-11-01 23:29:42 +0000 | [diff] [blame] | 148 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 149 | p=(const char *) target; |
| 150 | q=(const char *) source; |
| 151 | return(strcmp(p,q)); |
| 152 | } |
| 153 | |
cristy | 5463e2e | 2014-12-31 12:06:30 +0000 | [diff] [blame] | 154 | static ssize_t CopyXPMColor(char *destination,const char *source,size_t length) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 155 | { |
cristy | bcbe6c8 | 2014-12-01 11:54:39 +0000 | [diff] [blame] | 156 | register const char |
cristy | dd12db5 | 2014-11-30 21:54:11 +0000 | [diff] [blame] | 157 | *p; |
| 158 | |
| 159 | p=source; |
cristy | 26f6458 | 2014-12-27 20:04:16 +0000 | [diff] [blame] | 160 | while (length-- && (*p != '\0')) |
cristy | dd12db5 | 2014-11-30 21:54:11 +0000 | [diff] [blame] | 161 | *destination++=(*p++); |
cristy | 6f1d419 | 2014-12-28 13:59:39 +0000 | [diff] [blame] | 162 | if (length != 0) |
| 163 | *destination='\0'; |
cristy | 5463e2e | 2014-12-31 12:06:30 +0000 | [diff] [blame] | 164 | return((ssize_t) (p-source)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static char *NextXPMLine(char *p) |
| 168 | { |
cristy | f432c63 | 2014-12-07 15:11:28 +0000 | [diff] [blame] | 169 | assert(p != (char *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 170 | p=strchr(p,'\n'); |
| 171 | if (p != (char *) NULL) |
| 172 | p++; |
| 173 | return(p); |
| 174 | } |
| 175 | |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 176 | static char *ParseXPMColor(char *color,MagickBooleanType search_start) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | { |
| 178 | #define NumberTargets 6 |
| 179 | |
| 180 | register char |
| 181 | *p, |
| 182 | *r; |
| 183 | |
| 184 | register const char |
| 185 | *q; |
| 186 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 187 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 188 | i; |
| 189 | |
| 190 | static const char |
Cristy | 629f805 | 2015-09-14 07:48:08 -0400 | [diff] [blame] | 191 | *const targets[NumberTargets] = { "c ", "g ", "g4 ", "m ", "b ", "s " }; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 192 | |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 193 | if (search_start != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 194 | { |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 195 | for (i=0; i < NumberTargets; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 196 | { |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 197 | p=color; |
| 198 | for (q=targets[i]; *p != '\0'; p++) |
| 199 | { |
| 200 | if (*p == '\n') |
| 201 | break; |
| 202 | if (*p != *q) |
| 203 | continue; |
| 204 | if (isspace((int) ((unsigned char) (*(p-1)))) == 0) |
| 205 | continue; |
| 206 | r=p; |
| 207 | for ( ; ; ) |
| 208 | { |
| 209 | if (*q == '\0') |
| 210 | return(p); |
| 211 | if (*r++ != *q++) |
| 212 | break; |
| 213 | } |
| 214 | q=targets[i]; |
| 215 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 216 | } |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 217 | return((char *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 218 | } |
cristy | 76d045a | 2014-12-02 02:12:04 +0000 | [diff] [blame] | 219 | for (p=color+1; *p != '\0'; p++) |
| 220 | { |
| 221 | if (*p == '\n') |
| 222 | break; |
| 223 | if (isspace((int) ((unsigned char) (*(p-1)))) == 0) |
| 224 | continue; |
| 225 | if (isspace((int) ((unsigned char) (*p))) != 0) |
| 226 | continue; |
| 227 | for (i=0; i < NumberTargets; i++) |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 228 | { |
cristy | 76d045a | 2014-12-02 02:12:04 +0000 | [diff] [blame] | 229 | if ((*p == *targets[i]) && (*(p+1) == *(targets[i]+1))) |
| 230 | return(p); |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 231 | } |
cristy | 76d045a | 2014-12-02 02:12:04 +0000 | [diff] [blame] | 232 | } |
| 233 | return(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static Image *ReadXPMImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 237 | { |
| 238 | char |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 239 | *grey, |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 240 | key[MagickPathExtent], |
| 241 | target[MagickPathExtent], |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 242 | *xpm_buffer; |
| 243 | |
| 244 | Image |
| 245 | *image; |
| 246 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 247 | MagickBooleanType |
| 248 | active, |
| 249 | status; |
| 250 | |
| 251 | register char |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 252 | *next, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 253 | *p, |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 254 | *q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 255 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 256 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 257 | x; |
| 258 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 259 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 260 | *r; |
| 261 | |
| 262 | size_t |
| 263 | length; |
| 264 | |
| 265 | SplayTreeInfo |
| 266 | *xpm_colors; |
| 267 | |
| 268 | ssize_t |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 269 | count, |
| 270 | j, |
| 271 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 272 | |
cristy | d8af19d | 2010-05-29 16:59:36 +0000 | [diff] [blame] | 273 | unsigned long |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 274 | colors, |
| 275 | columns, |
| 276 | rows, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 277 | width; |
| 278 | |
| 279 | /* |
| 280 | Open image file. |
| 281 | */ |
| 282 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 283 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 284 | if (image_info->debug != MagickFalse) |
| 285 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 286 | image_info->filename); |
| 287 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 288 | assert(exception->signature == MagickCoreSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 289 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 290 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 291 | if (status == MagickFalse) |
| 292 | { |
| 293 | image=DestroyImageList(image); |
| 294 | return((Image *) NULL); |
| 295 | } |
| 296 | /* |
| 297 | Read XPM file. |
| 298 | */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 299 | length=MagickPathExtent; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 300 | xpm_buffer=(char *) AcquireQuantumMemory((size_t) length,sizeof(*xpm_buffer)); |
cristy | dd12db5 | 2014-11-30 21:54:11 +0000 | [diff] [blame] | 301 | if (xpm_buffer == (char *) NULL) |
| 302 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 303 | *xpm_buffer='\0'; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 304 | p=xpm_buffer; |
cristy | dd12db5 | 2014-11-30 21:54:11 +0000 | [diff] [blame] | 305 | while (ReadBlobString(image,p) != (char *) NULL) |
| 306 | { |
| 307 | if ((*p == '#') && ((p == xpm_buffer) || (*(p-1) == '\n'))) |
| 308 | continue; |
| 309 | if ((*p == '}') && (*(p+1) == ';')) |
| 310 | break; |
| 311 | p+=strlen(p); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 312 | if ((size_t) (p-xpm_buffer+MagickPathExtent) < length) |
cristy | dd12db5 | 2014-11-30 21:54:11 +0000 | [diff] [blame] | 313 | continue; |
| 314 | length<<=1; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 315 | xpm_buffer=(char *) ResizeQuantumMemory(xpm_buffer,length+MagickPathExtent, |
cristy | dd12db5 | 2014-11-30 21:54:11 +0000 | [diff] [blame] | 316 | sizeof(*xpm_buffer)); |
| 317 | if (xpm_buffer == (char *) NULL) |
| 318 | break; |
| 319 | p=xpm_buffer+strlen(xpm_buffer); |
| 320 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 321 | if (xpm_buffer == (char *) NULL) |
| 322 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 323 | /* |
| 324 | Remove comments. |
| 325 | */ |
| 326 | count=0; |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 327 | width=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 328 | for (p=xpm_buffer; *p != '\0'; p++) |
| 329 | { |
| 330 | if (*p != '"') |
| 331 | continue; |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 332 | count=(ssize_t) sscanf(p+1,"%lu %lu %lu %lu",&columns,&rows,&colors,&width); |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 333 | image->columns=columns; |
| 334 | image->rows=rows; |
| 335 | image->colors=colors; |
cristy | e6b6ece | 2010-05-29 13:25:01 +0000 | [diff] [blame] | 336 | if (count == 4) |
| 337 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 338 | } |
Cristy | 5b34848 | 2016-10-30 20:30:48 -0400 | [diff] [blame] | 339 | if ((count != 4) || (width == 0) || (width > 3) || |
Cristy | 76edbba | 2016-05-06 19:11:52 -0400 | [diff] [blame] | 340 | (image->columns == 0) || (image->rows == 0) || |
| 341 | (image->colors == 0) || (image->colors > MaxColormapSize)) |
| 342 | { |
| 343 | xpm_buffer=DestroyString(xpm_buffer); |
| 344 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 345 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 346 | /* |
| 347 | Remove unquoted characters. |
| 348 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 349 | active=MagickFalse; |
Cristy | e231b1b | 2016-04-18 08:22:26 -0400 | [diff] [blame] | 350 | for (q=xpm_buffer; *p != '\0'; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 351 | { |
| 352 | if (*p++ == '"') |
| 353 | { |
| 354 | if (active != MagickFalse) |
| 355 | *q++='\n'; |
| 356 | active=active != MagickFalse ? MagickFalse : MagickTrue; |
| 357 | } |
| 358 | if (active != MagickFalse) |
| 359 | *q++=(*p); |
| 360 | } |
| 361 | *q='\0'; |
| 362 | /* |
| 363 | Initialize image structure. |
| 364 | */ |
| 365 | xpm_colors=NewSplayTree(CompareXPMColor,RelinquishMagickMemory, |
| 366 | (void *(*)(void *)) NULL); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 367 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
cristy | 12a5ddd | 2015-03-05 23:48:38 +0000 | [diff] [blame] | 368 | { |
| 369 | xpm_buffer=DestroyString(xpm_buffer); |
| 370 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 371 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 372 | /* |
| 373 | Read image colormap. |
| 374 | */ |
cristy | 1d92f79 | 2013-09-09 13:04:57 +0000 | [diff] [blame] | 375 | image->depth=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 376 | next=NextXPMLine(xpm_buffer); |
cristy | 76d045a | 2014-12-02 02:12:04 +0000 | [diff] [blame] | 377 | for (j=0; (j < (ssize_t) image->colors) && (next != (char *) NULL); j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 378 | { |
| 379 | p=next; |
| 380 | next=NextXPMLine(p); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 381 | (void) CopyXPMColor(key,p,MagickMin((size_t) width,MagickPathExtent-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 382 | status=AddValueToSplayTree(xpm_colors,ConstantString(key),(void *) j); |
| 383 | /* |
| 384 | Parse color. |
| 385 | */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 386 | (void) CopyMagickString(target,"gray",MagickPathExtent); |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 387 | q=ParseXPMColor(p+width,MagickTrue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 388 | if (q != (char *) NULL) |
| 389 | { |
| 390 | while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0')) |
| 391 | q++; |
cristy | f432c63 | 2014-12-07 15:11:28 +0000 | [diff] [blame] | 392 | if ((next-q) < 0) |
| 393 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 394 | if (next != (char *) NULL) |
| 395 | (void) CopyXPMColor(target,q,MagickMin((size_t) (next-q), |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 396 | MagickPathExtent-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 397 | else |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 398 | (void) CopyMagickString(target,q,MagickPathExtent); |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 399 | q=ParseXPMColor(target,MagickFalse); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 400 | if (q != (char *) NULL) |
| 401 | *q='\0'; |
| 402 | } |
| 403 | StripString(target); |
dirk | 785a723 | 2014-10-20 19:50:07 +0000 | [diff] [blame] | 404 | grey=strstr(target,"grey"); |
| 405 | if (grey != (char *) NULL) |
dirk | 8695d95 | 2014-10-22 05:01:06 +0000 | [diff] [blame] | 406 | grey[2]='a'; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 407 | if (LocaleCompare(target,"none") == 0) |
| 408 | { |
| 409 | image->storage_class=DirectClass; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 410 | image->alpha_trait=BlendPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 411 | } |
cristy | fc502a1 | 2013-04-09 16:52:51 +0000 | [diff] [blame] | 412 | status=QueryColorCompliance(target,XPMCompliance,&image->colormap[j], |
cristy | a60c020 | 2010-07-31 21:36:45 +0000 | [diff] [blame] | 413 | exception); |
| 414 | if (status == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 415 | break; |
cristy | 1d92f79 | 2013-09-09 13:04:57 +0000 | [diff] [blame] | 416 | if (image->depth < image->colormap[j].depth) |
| 417 | image->depth=image->colormap[j].depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 418 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 419 | if (j < (ssize_t) image->colors) |
cristy | 12a5ddd | 2015-03-05 23:48:38 +0000 | [diff] [blame] | 420 | { |
| 421 | xpm_colors=DestroySplayTree(xpm_colors); |
| 422 | xpm_buffer=DestroyString(xpm_buffer); |
| 423 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 424 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 425 | j=0; |
| 426 | if (image_info->ping == MagickFalse) |
| 427 | { |
| 428 | /* |
| 429 | Read image pixels. |
| 430 | */ |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 431 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 432 | if (status == MagickFalse) |
| 433 | return(DestroyImageList(image)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 434 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 435 | { |
| 436 | p=NextXPMLine(p); |
| 437 | if (p == (char *) NULL) |
| 438 | break; |
| 439 | r=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 440 | if (r == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 441 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 442 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 443 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 444 | ssize_t count=CopyXPMColor(key,p,MagickMin(width,MagickPathExtent-1)); |
cristy | 5463e2e | 2014-12-31 12:06:30 +0000 | [diff] [blame] | 445 | if (count != (ssize_t) width) |
| 446 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 447 | j=(ssize_t) GetValueFromSplayTree(xpm_colors,key); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 448 | if (image->storage_class == PseudoClass) |
cristy | dd12db5 | 2014-11-30 21:54:11 +0000 | [diff] [blame] | 449 | SetPixelIndex(image,(Quantum) j,r); |
cristy | 11a06d3 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 450 | SetPixelViaPixelInfo(image,image->colormap+j,r); |
cristy | 5463e2e | 2014-12-31 12:06:30 +0000 | [diff] [blame] | 451 | p+=count; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 452 | r+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 453 | } |
cristy | 5463e2e | 2014-12-31 12:06:30 +0000 | [diff] [blame] | 454 | if (x < (ssize_t) image->columns) |
| 455 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 456 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 457 | break; |
| 458 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 459 | if (y < (ssize_t) image->rows) |
cristy | 12a5ddd | 2015-03-05 23:48:38 +0000 | [diff] [blame] | 460 | { |
| 461 | xpm_colors=DestroySplayTree(xpm_colors); |
| 462 | xpm_buffer=DestroyString(xpm_buffer); |
| 463 | ThrowReaderException(CorruptImageError,"NotEnoughPixelData"); |
| 464 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 465 | } |
| 466 | /* |
| 467 | Relinquish resources. |
| 468 | */ |
cristy | 12a5ddd | 2015-03-05 23:48:38 +0000 | [diff] [blame] | 469 | xpm_buffer=DestroyString(xpm_buffer); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 470 | xpm_colors=DestroySplayTree(xpm_colors); |
| 471 | (void) CloseBlob(image); |
| 472 | return(GetFirstImageInList(image)); |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 477 | % % |
| 478 | % % |
| 479 | % % |
| 480 | % R e g i s t e r X P M I m a g e % |
| 481 | % % |
| 482 | % % |
| 483 | % % |
| 484 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 485 | % |
| 486 | % RegisterXPMImage() adds attributes for the XPM image format to |
| 487 | % the list of supported formats. The attributes include the image format |
| 488 | % tag, a method to read and/or write the format, whether the format |
| 489 | % supports the saving of more than one frame to the same file or blob, |
| 490 | % whether the format supports native in-memory I/O, and a brief |
| 491 | % description of the format. |
| 492 | % |
| 493 | % The format of the RegisterXPMImage method is: |
| 494 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 495 | % size_t RegisterXPMImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 496 | % |
| 497 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 498 | ModuleExport size_t RegisterXPMImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | { |
| 500 | MagickInfo |
| 501 | *entry; |
| 502 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 503 | entry=AcquireMagickInfo("XPM","PICON","Personal Icon"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 504 | entry->decoder=(DecodeImageHandler *) ReadXPMImage; |
| 505 | entry->encoder=(EncodeImageHandler *) WritePICONImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 506 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 507 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 508 | entry=AcquireMagickInfo("XPM","PM","X Windows system pixmap (color)"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 509 | entry->decoder=(DecodeImageHandler *) ReadXPMImage; |
| 510 | entry->encoder=(EncodeImageHandler *) WriteXPMImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 511 | entry->flags^=CoderAdjoinFlag; |
| 512 | entry->flags|=CoderStealthFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 513 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 514 | entry=AcquireMagickInfo("XPM","XPM","X Windows system pixmap (color)"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 515 | entry->decoder=(DecodeImageHandler *) ReadXPMImage; |
| 516 | entry->encoder=(EncodeImageHandler *) WriteXPMImage; |
| 517 | entry->magick=(IsImageFormatHandler *) IsXPM; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 518 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 519 | (void) RegisterMagickInfo(entry); |
| 520 | return(MagickImageCoderSignature); |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 525 | % % |
| 526 | % % |
| 527 | % % |
| 528 | % U n r e g i s t e r X P M I m a g e % |
| 529 | % % |
| 530 | % % |
| 531 | % % |
| 532 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 533 | % |
| 534 | % UnregisterXPMImage() removes format registrations made by the |
| 535 | % XPM module from the list of supported formats. |
| 536 | % |
| 537 | % The format of the UnregisterXPMImage method is: |
| 538 | % |
| 539 | % UnregisterXPMImage(void) |
| 540 | % |
| 541 | */ |
| 542 | ModuleExport void UnregisterXPMImage(void) |
| 543 | { |
| 544 | (void) UnregisterMagickInfo("PICON"); |
| 545 | (void) UnregisterMagickInfo("PM"); |
| 546 | (void) UnregisterMagickInfo("XPM"); |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 551 | % % |
| 552 | % % |
| 553 | % % |
| 554 | % W r i t e P I C O N I m a g e % |
| 555 | % % |
| 556 | % % |
| 557 | % % |
| 558 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 559 | % |
cristy | a60c020 | 2010-07-31 21:36:45 +0000 | [diff] [blame] | 560 | % WritePICONImage() writes an image to a file in the Personal Icon format. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 561 | % |
| 562 | % The format of the WritePICONImage method is: |
| 563 | % |
| 564 | % MagickBooleanType WritePICONImage(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 565 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 566 | % |
| 567 | % A description of each parameter follows. |
| 568 | % |
| 569 | % o image_info: the image info. |
| 570 | % |
| 571 | % o image: The image. |
| 572 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 573 | % o exception: return any errors or warnings in this structure. |
| 574 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | */ |
| 576 | static MagickBooleanType WritePICONImage(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 577 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 578 | { |
| 579 | #define ColormapExtent 155 |
| 580 | #define GraymapExtent 95 |
| 581 | #define PiconGeometry "48x48>" |
| 582 | |
| 583 | static unsigned char |
| 584 | Colormap[]= |
| 585 | { |
| 586 | 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x06, 0x00, 0x05, 0x00, 0xf4, 0x05, |
| 587 | 0x00, 0x00, 0x00, 0x00, 0x2f, 0x4f, 0x4f, 0x70, 0x80, 0x90, 0x7e, 0x7e, |
| 588 | 0x7e, 0xdc, 0xdc, 0xdc, 0xff, 0xff, 0xff, 0x00, 0x00, 0x80, 0x00, 0x00, |
| 589 | 0xff, 0x1e, 0x90, 0xff, 0x87, 0xce, 0xeb, 0xe6, 0xe6, 0xfa, 0x00, 0xff, |
| 590 | 0xff, 0x80, 0x00, 0x80, 0xb2, 0x22, 0x22, 0x2e, 0x8b, 0x57, 0x32, 0xcd, |
| 591 | 0x32, 0x00, 0xff, 0x00, 0x98, 0xfb, 0x98, 0xff, 0x00, 0xff, 0xff, 0x00, |
| 592 | 0x00, 0xff, 0x63, 0x47, 0xff, 0xa5, 0x00, 0xff, 0xd7, 0x00, 0xff, 0xff, |
| 593 | 0x00, 0xee, 0x82, 0xee, 0xa0, 0x52, 0x2d, 0xcd, 0x85, 0x3f, 0xd2, 0xb4, |
| 594 | 0x8c, 0xf5, 0xde, 0xb3, 0xff, 0xfa, 0xcd, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 595 | 0x00, 0x21, 0xf9, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, |
| 596 | 0x00, 0x00, 0x06, 0x00, 0x05, 0x00, 0x00, 0x05, 0x18, 0x20, 0x10, 0x08, |
| 597 | 0x03, 0x51, 0x18, 0x07, 0x92, 0x28, 0x0b, 0xd3, 0x38, 0x0f, 0x14, 0x49, |
| 598 | 0x13, 0x55, 0x59, 0x17, 0x96, 0x69, 0x1b, 0xd7, 0x85, 0x00, 0x3b, |
| 599 | }, |
| 600 | Graymap[]= |
| 601 | { |
| 602 | 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x04, 0x00, 0x04, 0x00, 0xf3, 0x0f, |
| 603 | 0x00, 0x00, 0x00, 0x00, 0x12, 0x12, 0x12, 0x21, 0x21, 0x21, 0x33, 0x33, |
| 604 | 0x33, 0x45, 0x45, 0x45, 0x54, 0x54, 0x54, 0x66, 0x66, 0x66, 0x78, 0x78, |
| 605 | 0x78, 0x87, 0x87, 0x87, 0x99, 0x99, 0x99, 0xab, 0xab, 0xab, 0xba, 0xba, |
| 606 | 0xba, 0xcc, 0xcc, 0xcc, 0xde, 0xde, 0xde, 0xed, 0xed, 0xed, 0xff, 0xff, |
| 607 | 0xff, 0x21, 0xf9, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, |
| 608 | 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x04, 0x0c, 0x10, 0x04, 0x31, |
| 609 | 0x48, 0x31, 0x07, 0x25, 0xb5, 0x58, 0x73, 0x4f, 0x04, 0x00, 0x3b, |
| 610 | }; |
| 611 | |
| 612 | #define MaxCixels 92 |
| 613 | |
| 614 | static const char |
| 615 | Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk" |
| 616 | "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|"; |
| 617 | |
| 618 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 619 | buffer[MagickPathExtent], |
| 620 | basename[MagickPathExtent], |
| 621 | name[MagickPathExtent], |
| 622 | symbol[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 623 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 624 | Image |
| 625 | *affinity_image, |
| 626 | *picon; |
| 627 | |
| 628 | ImageInfo |
| 629 | *blob_info; |
| 630 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 631 | MagickBooleanType |
| 632 | status, |
| 633 | transparent; |
| 634 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 635 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 636 | pixel; |
| 637 | |
| 638 | QuantizeInfo |
| 639 | *quantize_info; |
| 640 | |
| 641 | RectangleInfo |
| 642 | geometry; |
| 643 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 644 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 645 | *p; |
| 646 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 647 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 648 | i, |
| 649 | x; |
| 650 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 651 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 652 | *q; |
| 653 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 654 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 655 | characters_per_pixel, |
| 656 | colors; |
| 657 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 658 | ssize_t |
| 659 | j, |
| 660 | k, |
| 661 | y; |
| 662 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 663 | /* |
| 664 | Open output image file. |
| 665 | */ |
| 666 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 667 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 668 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 669 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 670 | if (image->debug != MagickFalse) |
| 671 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 672 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 673 | assert(exception->signature == MagickCoreSignature); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 674 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 675 | if (status == MagickFalse) |
| 676 | return(status); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 677 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 678 | SetGeometry(image,&geometry); |
| 679 | (void) ParseMetaGeometry(PiconGeometry,&geometry.x,&geometry.y, |
| 680 | &geometry.width,&geometry.height); |
cristy | aa2c16c | 2012-03-25 22:21:35 +0000 | [diff] [blame] | 681 | picon=ResizeImage(image,geometry.width,geometry.height,TriangleFilter, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 682 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 683 | blob_info=CloneImageInfo(image_info); |
| 684 | (void) AcquireUniqueFilename(blob_info->filename); |
| 685 | if ((image_info->type != TrueColorType) && |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 686 | (SetImageGray(image,exception) != MagickFalse)) |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 687 | affinity_image=BlobToImage(blob_info,Graymap,GraymapExtent,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 688 | else |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 689 | affinity_image=BlobToImage(blob_info,Colormap,ColormapExtent,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 690 | (void) RelinquishUniqueFileResource(blob_info->filename); |
| 691 | blob_info=DestroyImageInfo(blob_info); |
| 692 | if ((picon == (Image *) NULL) || (affinity_image == (Image *) NULL)) |
| 693 | return(MagickFalse); |
| 694 | quantize_info=AcquireQuantizeInfo(image_info); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 695 | status=RemapImage(quantize_info,picon,affinity_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 696 | quantize_info=DestroyQuantizeInfo(quantize_info); |
| 697 | affinity_image=DestroyImage(affinity_image); |
| 698 | transparent=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 699 | if (picon->storage_class == PseudoClass) |
| 700 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 701 | (void) CompressImageColormap(picon,exception); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 702 | if (picon->alpha_trait != UndefinedPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 703 | transparent=MagickTrue; |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | /* |
| 708 | Convert DirectClass to PseudoClass picon. |
| 709 | */ |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 710 | if (picon->alpha_trait != UndefinedPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 711 | { |
| 712 | /* |
| 713 | Map all the transparent pixels. |
| 714 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 715 | for (y=0; y < (ssize_t) picon->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 716 | { |
| 717 | q=GetAuthenticPixels(picon,0,y,picon->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 718 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 719 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 720 | for (x=0; x < (ssize_t) picon->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 721 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 722 | if (GetPixelAlpha(image,q) == (Quantum) TransparentAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | transparent=MagickTrue; |
| 724 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 725 | SetPixelAlpha(picon,OpaqueAlpha,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 726 | q+=GetPixelChannels(picon); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 727 | } |
| 728 | if (SyncAuthenticPixels(picon,exception) == MagickFalse) |
| 729 | break; |
| 730 | } |
| 731 | } |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 732 | (void) SetImageType(picon,PaletteType,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 733 | } |
| 734 | colors=picon->colors; |
| 735 | if (transparent != MagickFalse) |
| 736 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 737 | colors++; |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 738 | picon->colormap=(PixelInfo *) ResizeQuantumMemory((void **) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 739 | picon->colormap,(size_t) colors,sizeof(*picon->colormap)); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 740 | if (picon->colormap == (PixelInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 741 | ThrowWriterException(ResourceLimitError,"MemoryAllocationError"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 742 | for (y=0; y < (ssize_t) picon->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 743 | { |
| 744 | q=GetAuthenticPixels(picon,0,y,picon->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 745 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 746 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 747 | for (x=0; x < (ssize_t) picon->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 748 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 749 | if (GetPixelAlpha(image,q) == (Quantum) TransparentAlpha) |
cristy | dd12db5 | 2014-11-30 21:54:11 +0000 | [diff] [blame] | 750 | SetPixelIndex(picon,(Quantum) picon->colors,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 751 | q+=GetPixelChannels(picon); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 752 | } |
| 753 | if (SyncAuthenticPixels(picon,exception) == MagickFalse) |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | /* |
| 758 | Compute the character per pixel. |
| 759 | */ |
| 760 | characters_per_pixel=1; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 761 | for (k=MaxCixels; (ssize_t) colors > k; k*=MaxCixels) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 762 | characters_per_pixel++; |
| 763 | /* |
| 764 | XPM header. |
| 765 | */ |
| 766 | (void) WriteBlobString(image,"/* XPM */\n"); |
| 767 | GetPathComponent(picon->filename,BasePath,basename); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 768 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 769 | "static char *%s[] = {\n",basename); |
| 770 | (void) WriteBlobString(image,buffer); |
| 771 | (void) WriteBlobString(image,"/* columns rows colors chars-per-pixel */\n"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 772 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 773 | "\"%.20g %.20g %.20g %.20g\",\n",(double) picon->columns,(double) |
| 774 | picon->rows,(double) colors,(double) characters_per_pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 775 | (void) WriteBlobString(image,buffer); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 776 | GetPixelInfo(image,&pixel); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 777 | for (i=0; i < (ssize_t) colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 778 | { |
| 779 | /* |
| 780 | Define XPM color. |
| 781 | */ |
cristy | 9d8c8ce | 2011-10-25 16:13:52 +0000 | [diff] [blame] | 782 | pixel=picon->colormap[i]; |
cristy | 7020ae6 | 2012-04-18 12:58:34 +0000 | [diff] [blame] | 783 | pixel.colorspace=sRGBColorspace; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 784 | pixel.depth=8; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 785 | pixel.alpha=(double) OpaqueAlpha; |
cristy | 269c941 | 2011-10-13 23:41:15 +0000 | [diff] [blame] | 786 | (void) QueryColorname(image,&pixel,XPMCompliance,name,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 787 | if (transparent != MagickFalse) |
| 788 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 789 | if (i == (ssize_t) (colors-1)) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 790 | (void) CopyMagickString(name,"grey75",MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 791 | } |
| 792 | /* |
| 793 | Write XPM color. |
| 794 | */ |
| 795 | k=i % MaxCixels; |
| 796 | symbol[0]=Cixel[k]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 797 | for (j=1; j < (ssize_t) characters_per_pixel; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 798 | { |
| 799 | k=((i-k)/MaxCixels) % MaxCixels; |
| 800 | symbol[j]=Cixel[k]; |
| 801 | } |
| 802 | symbol[j]='\0'; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 803 | (void) FormatLocaleString(buffer,MagickPathExtent,"\"%s c %s\",\n", |
cristy | e6b6ece | 2010-05-29 13:25:01 +0000 | [diff] [blame] | 804 | symbol,name); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 805 | (void) WriteBlobString(image,buffer); |
| 806 | } |
| 807 | /* |
| 808 | Define XPM pixels. |
| 809 | */ |
| 810 | (void) WriteBlobString(image,"/* pixels */\n"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 811 | for (y=0; y < (ssize_t) picon->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 812 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 813 | p=GetVirtualPixels(picon,0,y,picon->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 814 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 815 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 816 | (void) WriteBlobString(image,"\""); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 817 | for (x=0; x < (ssize_t) picon->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 818 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 819 | k=((ssize_t) GetPixelIndex(picon,p) % MaxCixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 820 | symbol[0]=Cixel[k]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 821 | for (j=1; j < (ssize_t) characters_per_pixel; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 822 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 823 | k=(((int) GetPixelIndex(picon,p)-k)/MaxCixels) % MaxCixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 824 | symbol[j]=Cixel[k]; |
| 825 | } |
| 826 | symbol[j]='\0'; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 827 | (void) CopyMagickString(buffer,symbol,MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 828 | (void) WriteBlobString(image,buffer); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 829 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 830 | } |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 831 | (void) FormatLocaleString(buffer,MagickPathExtent,"\"%s\n", |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 832 | y == (ssize_t) (picon->rows-1) ? "" : ","); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 833 | (void) WriteBlobString(image,buffer); |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 834 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 835 | picon->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 836 | if (status == MagickFalse) |
| 837 | break; |
| 838 | } |
| 839 | picon=DestroyImage(picon); |
| 840 | (void) WriteBlobString(image,"};\n"); |
| 841 | (void) CloseBlob(image); |
| 842 | return(MagickTrue); |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 847 | % % |
| 848 | % % |
| 849 | % % |
| 850 | % W r i t e X P M I m a g e % |
| 851 | % % |
| 852 | % % |
| 853 | % % |
| 854 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 855 | % |
cristy | a60c020 | 2010-07-31 21:36:45 +0000 | [diff] [blame] | 856 | % WriteXPMImage() writes an image to a file in the X pixmap format. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 857 | % |
| 858 | % The format of the WriteXPMImage method is: |
| 859 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 860 | % MagickBooleanType WriteXPMImage(const ImageInfo *image_info, |
| 861 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 862 | % |
| 863 | % A description of each parameter follows. |
| 864 | % |
| 865 | % o image_info: the image info. |
| 866 | % |
| 867 | % o image: The image. |
| 868 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 869 | % o exception: return any errors or warnings in this structure. |
| 870 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 871 | */ |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 872 | static MagickBooleanType WriteXPMImage(const ImageInfo *image_info,Image *image, |
| 873 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 874 | { |
| 875 | #define MaxCixels 92 |
| 876 | |
| 877 | static const char |
| 878 | Cixel[MaxCixels+1] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk" |
| 879 | "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|"; |
| 880 | |
| 881 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 882 | buffer[MagickPathExtent], |
| 883 | basename[MagickPathExtent], |
| 884 | name[MagickPathExtent], |
| 885 | symbol[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 886 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 887 | MagickBooleanType |
| 888 | status; |
| 889 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 890 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 891 | pixel; |
| 892 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 893 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 894 | *p; |
| 895 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 896 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 897 | i, |
| 898 | x; |
| 899 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 900 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 901 | characters_per_pixel; |
| 902 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 903 | ssize_t |
| 904 | j, |
| 905 | k, |
| 906 | opacity, |
| 907 | y; |
| 908 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 909 | /* |
| 910 | Open output image file. |
| 911 | */ |
| 912 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 913 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 914 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 915 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 916 | if (image->debug != MagickFalse) |
| 917 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 918 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 919 | assert(exception->signature == MagickCoreSignature); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 920 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 921 | if (status == MagickFalse) |
| 922 | return(status); |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 923 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | 8d95109 | 2012-02-08 18:54:56 +0000 | [diff] [blame] | 924 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 925 | opacity=(-1); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 926 | if (image->alpha_trait == UndefinedPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 927 | { |
| 928 | if ((image->storage_class == DirectClass) || (image->colors > 256)) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 929 | (void) SetImageType(image,PaletteType,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 930 | } |
| 931 | else |
| 932 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 933 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 934 | alpha, |
| 935 | beta; |
| 936 | |
| 937 | /* |
| 938 | Identify transparent colormap index. |
| 939 | */ |
| 940 | if ((image->storage_class == DirectClass) || (image->colors > 256)) |
cristy | def23e5 | 2015-01-22 11:52:01 +0000 | [diff] [blame] | 941 | (void) SetImageType(image,PaletteBilevelAlphaType,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 942 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 943 | if (image->colormap[i].alpha != OpaqueAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 944 | { |
| 945 | if (opacity < 0) |
| 946 | { |
| 947 | opacity=i; |
| 948 | continue; |
| 949 | } |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 950 | alpha=(double) TransparentAlpha-(double) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 951 | image->colormap[i].alpha; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 952 | beta=(double) TransparentAlpha-(double) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 953 | image->colormap[opacity].alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 954 | if (alpha < beta) |
| 955 | opacity=i; |
| 956 | } |
| 957 | if (opacity == -1) |
| 958 | { |
cristy | def23e5 | 2015-01-22 11:52:01 +0000 | [diff] [blame] | 959 | (void) SetImageType(image,PaletteBilevelAlphaType,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 960 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 961 | if (image->colormap[i].alpha != OpaqueAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 962 | { |
| 963 | if (opacity < 0) |
| 964 | { |
| 965 | opacity=i; |
| 966 | continue; |
| 967 | } |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 968 | alpha=(Quantum) TransparentAlpha-(double) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 969 | image->colormap[i].alpha; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 970 | beta=(Quantum) TransparentAlpha-(double) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 971 | image->colormap[opacity].alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 972 | if (alpha < beta) |
| 973 | opacity=i; |
| 974 | } |
| 975 | } |
| 976 | if (opacity >= 0) |
| 977 | { |
| 978 | image->colormap[opacity].red=image->transparent_color.red; |
| 979 | image->colormap[opacity].green=image->transparent_color.green; |
| 980 | image->colormap[opacity].blue=image->transparent_color.blue; |
| 981 | } |
| 982 | } |
| 983 | /* |
| 984 | Compute the character per pixel. |
| 985 | */ |
| 986 | characters_per_pixel=1; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 987 | for (k=MaxCixels; (ssize_t) image->colors > k; k*=MaxCixels) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 988 | characters_per_pixel++; |
| 989 | /* |
| 990 | XPM header. |
| 991 | */ |
| 992 | (void) WriteBlobString(image,"/* XPM */\n"); |
| 993 | GetPathComponent(image->filename,BasePath,basename); |
| 994 | if (isalnum((int) ((unsigned char) *basename)) == 0) |
| 995 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 996 | (void) FormatLocaleString(buffer,MagickPathExtent,"xpm_%s",basename); |
| 997 | (void) CopyMagickString(basename,buffer,MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 998 | } |
cristy | 2f4741f | 2010-11-01 23:29:42 +0000 | [diff] [blame] | 999 | if (isalpha((int) ((unsigned char) basename[0])) == 0) |
| 1000 | basename[0]='_'; |
| 1001 | for (i=1; basename[i] != '\0'; i++) |
| 1002 | if (isalnum((int) ((unsigned char) basename[i])) == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1003 | basename[i]='_'; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1004 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1005 | "static char *%s[] = {\n",basename); |
| 1006 | (void) WriteBlobString(image,buffer); |
| 1007 | (void) WriteBlobString(image,"/* columns rows colors chars-per-pixel */\n"); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1008 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1009 | "\"%.20g %.20g %.20g %.20g \",\n",(double) image->columns,(double) |
| 1010 | image->rows,(double) image->colors,(double) characters_per_pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1011 | (void) WriteBlobString(image,buffer); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1012 | GetPixelInfo(image,&pixel); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1013 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1014 | { |
| 1015 | /* |
| 1016 | Define XPM color. |
| 1017 | */ |
cristy | 9d8c8ce | 2011-10-25 16:13:52 +0000 | [diff] [blame] | 1018 | pixel=image->colormap[i]; |
cristy | 7020ae6 | 2012-04-18 12:58:34 +0000 | [diff] [blame] | 1019 | pixel.colorspace=sRGBColorspace; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1020 | pixel.depth=8; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1021 | pixel.alpha=(double) OpaqueAlpha; |
cristy | 269c941 | 2011-10-13 23:41:15 +0000 | [diff] [blame] | 1022 | (void) QueryColorname(image,&pixel,XPMCompliance,name,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1023 | if (i == opacity) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1024 | (void) CopyMagickString(name,"None",MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1025 | /* |
| 1026 | Write XPM color. |
| 1027 | */ |
| 1028 | k=i % MaxCixels; |
| 1029 | symbol[0]=Cixel[k]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1030 | for (j=1; j < (ssize_t) characters_per_pixel; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1031 | { |
| 1032 | k=((i-k)/MaxCixels) % MaxCixels; |
| 1033 | symbol[j]=Cixel[k]; |
| 1034 | } |
| 1035 | symbol[j]='\0'; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1036 | (void) FormatLocaleString(buffer,MagickPathExtent,"\"%s c %s\",\n",symbol, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1037 | name); |
| 1038 | (void) WriteBlobString(image,buffer); |
| 1039 | } |
| 1040 | /* |
| 1041 | Define XPM pixels. |
| 1042 | */ |
| 1043 | (void) WriteBlobString(image,"/* pixels */\n"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1044 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1045 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 1046 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1047 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1048 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1049 | (void) WriteBlobString(image,"\""); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1050 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1051 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1052 | k=((ssize_t) GetPixelIndex(image,p) % MaxCixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1053 | symbol[0]=Cixel[k]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1054 | for (j=1; j < (ssize_t) characters_per_pixel; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1055 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1056 | k=(((int) GetPixelIndex(image,p)-k)/MaxCixels) % MaxCixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1057 | symbol[j]=Cixel[k]; |
| 1058 | } |
| 1059 | symbol[j]='\0'; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1060 | (void) CopyMagickString(buffer,symbol,MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1061 | (void) WriteBlobString(image,buffer); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1062 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1063 | } |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1064 | (void) FormatLocaleString(buffer,MagickPathExtent,"\"%s\n", |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1065 | (y == (ssize_t) (image->rows-1) ? "" : ",")); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1066 | (void) WriteBlobString(image,buffer); |
cristy | 8b27a6d | 2010-02-14 03:31:15 +0000 | [diff] [blame] | 1067 | if (image->previous == (Image *) NULL) |
| 1068 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 1069 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 1070 | image->rows); |
cristy | 8b27a6d | 2010-02-14 03:31:15 +0000 | [diff] [blame] | 1071 | if (status == MagickFalse) |
| 1072 | break; |
| 1073 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1074 | } |
| 1075 | (void) WriteBlobString(image,"};\n"); |
| 1076 | (void) CloseBlob(image); |
| 1077 | return(MagickTrue); |
| 1078 | } |