]> granicus.if.org Git - imagemagick/blob - MagickCore/property.c
...
[imagemagick] / MagickCore / property.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %            PPPP    RRRR    OOO   PPPP   EEEEE  RRRR   TTTTT  Y   Y          %
7 %            P   P   R   R  O   O  P   P  E      R   R    T     Y Y           %
8 %            PPPP    RRRR   O   O  PPPP   EEE    RRRR     T      Y            %
9 %            P       R R    O   O  P      E      R R      T      Y            %
10 %            P       R  R    OOO   P      EEEEE  R  R     T      Y            %
11 %                                                                             %
12 %                                                                             %
13 %                         MagickCore Property Methods                         %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 March 2000                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization      %
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 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
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 /*
41   Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/cache-private.h"
48 #include "MagickCore/color.h"
49 #include "MagickCore/color-private.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/compare.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/effect.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/fx.h"
58 #include "MagickCore/fx-private.h"
59 #include "MagickCore/gem.h"
60 #include "MagickCore/geometry.h"
61 #include "MagickCore/histogram.h"
62 #include "MagickCore/image.h"
63 #include "MagickCore/layer.h"
64 #include "MagickCore/locale-private.h"
65 #include "MagickCore/list.h"
66 #include "MagickCore/magick.h"
67 #include "MagickCore/memory_.h"
68 #include "MagickCore/monitor.h"
69 #include "MagickCore/montage.h"
70 #include "MagickCore/option.h"
71 #include "MagickCore/policy.h"
72 #include "MagickCore/profile.h"
73 #include "MagickCore/property.h"
74 #include "MagickCore/quantum.h"
75 #include "MagickCore/resource_.h"
76 #include "MagickCore/splay-tree.h"
77 #include "MagickCore/signature.h"
78 #include "MagickCore/statistic.h"
79 #include "MagickCore/string_.h"
80 #include "MagickCore/string-private.h"
81 #include "MagickCore/token.h"
82 #include "MagickCore/token-private.h"
83 #include "MagickCore/utility.h"
84 #include "MagickCore/utility-private.h"
85 #include "MagickCore/version.h"
86 #include "MagickCore/xml-tree.h"
87 #include "MagickCore/xml-tree-private.h"
88 #if defined(MAGICKCORE_LCMS_DELEGATE)
89 #if defined(MAGICKCORE_HAVE_LCMS2_LCMS2_H)
90 #include <lcms2/lcms2.h>
91 #elif defined(MAGICKCORE_HAVE_LCMS2_H)
92 #include "lcms2.h"
93 #elif defined(MAGICKCORE_HAVE_LCMS_LCMS_H)
94 #include <lcms/lcms.h>
95 #else
96 #include "lcms.h"
97 #endif
98 #endif
99 \f
100 /*
101   Define declarations.
102 */
103 #if defined(MAGICKCORE_LCMS_DELEGATE)
104 #if defined(LCMS_VERSION) && (LCMS_VERSION < 2000)
105 #define cmsUInt32Number  DWORD
106 #endif
107 #endif
108 \f
109 /*
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 %                                                                             %
112 %                                                                             %
113 %                                                                             %
114 %   C l o n e I m a g e P r o p e r t i e s                                   %
115 %                                                                             %
116 %                                                                             %
117 %                                                                             %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 %
120 %  CloneImageProperties() clones all the image properties to another image.
121 %
122 %  The format of the CloneImageProperties method is:
123 %
124 %      MagickBooleanType CloneImageProperties(Image *image,
125 %        const Image *clone_image)
126 %
127 %  A description of each parameter follows:
128 %
129 %    o image: the image.
130 %
131 %    o clone_image: the clone image.
132 %
133 */
134 MagickExport MagickBooleanType CloneImageProperties(Image *image,
135   const Image *clone_image)
136 {
137   assert(image != (Image *) NULL);
138   assert(image->signature == MagickCoreSignature);
139   if (image->debug != MagickFalse)
140     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
141   assert(clone_image != (const Image *) NULL);
142   assert(clone_image->signature == MagickCoreSignature);
143   if (clone_image->debug != MagickFalse)
144     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
145       clone_image->filename);
146   (void) CopyMagickString(image->filename,clone_image->filename,MagickPathExtent);
147   (void) CopyMagickString(image->magick_filename,clone_image->magick_filename,
148     MagickPathExtent);
149   image->compression=clone_image->compression;
150   image->quality=clone_image->quality;
151   image->depth=clone_image->depth;
152   image->alpha_color=clone_image->alpha_color;
153   image->background_color=clone_image->background_color;
154   image->border_color=clone_image->border_color;
155   image->transparent_color=clone_image->transparent_color;
156   image->gamma=clone_image->gamma;
157   image->chromaticity=clone_image->chromaticity;
158   image->rendering_intent=clone_image->rendering_intent;
159   image->black_point_compensation=clone_image->black_point_compensation;
160   image->units=clone_image->units;
161   image->montage=(char *) NULL;
162   image->directory=(char *) NULL;
163   (void) CloneString(&image->geometry,clone_image->geometry);
164   image->offset=clone_image->offset;
165   image->resolution.x=clone_image->resolution.x;
166   image->resolution.y=clone_image->resolution.y;
167   image->page=clone_image->page;
168   image->tile_offset=clone_image->tile_offset;
169   image->extract_info=clone_image->extract_info;
170   image->filter=clone_image->filter;
171   image->fuzz=clone_image->fuzz;
172   image->intensity=clone_image->intensity;
173   image->interlace=clone_image->interlace;
174   image->interpolate=clone_image->interpolate;
175   image->endian=clone_image->endian;
176   image->gravity=clone_image->gravity;
177   image->compose=clone_image->compose;
178   image->orientation=clone_image->orientation;
179   image->scene=clone_image->scene;
180   image->dispose=clone_image->dispose;
181   image->delay=clone_image->delay;
182   image->ticks_per_second=clone_image->ticks_per_second;
183   image->iterations=clone_image->iterations;
184   image->total_colors=clone_image->total_colors;
185   image->taint=clone_image->taint;
186   image->progress_monitor=clone_image->progress_monitor;
187   image->client_data=clone_image->client_data;
188   image->start_loop=clone_image->start_loop;
189   image->error=clone_image->error;
190   image->signature=clone_image->signature;
191   if (clone_image->properties != (void *) NULL)
192     {
193       if (image->properties != (void *) NULL)
194         DestroyImageProperties(image);
195       image->properties=CloneSplayTree((SplayTreeInfo *)
196         clone_image->properties,(void *(*)(void *)) ConstantString,
197         (void *(*)(void *)) ConstantString);
198     }
199   return(MagickTrue);
200 }
201 \f
202 /*
203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 %                                                                             %
205 %                                                                             %
206 %                                                                             %
207 %   D e f i n e I m a g e P r o p e r t y                                     %
208 %                                                                             %
209 %                                                                             %
210 %                                                                             %
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 %
213 %  DefineImageProperty() associates an assignment string of the form
214 %  "key=value" with an artifact or options. It is equivelent to
215 %  SetImageProperty()
216 %
217 %  The format of the DefineImageProperty method is:
218 %
219 %      MagickBooleanType DefineImageProperty(Image *image,const char *property,
220 %        ExceptionInfo *exception)
221 %
222 %  A description of each parameter follows:
223 %
224 %    o image: the image.
225 %
226 %    o property: the image property.
227 %
228 %    o exception: return any errors or warnings in this structure.
229 %
230 */
231 MagickExport MagickBooleanType DefineImageProperty(Image *image,
232   const char *property,ExceptionInfo *exception)
233 {
234   char
235     key[MagickPathExtent],
236     value[MagickPathExtent];
237
238   register char
239     *p;
240
241   assert(image != (Image *) NULL);
242   assert(property != (const char *) NULL);
243   (void) CopyMagickString(key,property,MagickPathExtent-1);
244   for (p=key; *p != '\0'; p++)
245     if (*p == '=')
246       break;
247   *value='\0';
248   if (*p == '=')
249     (void) CopyMagickString(value,p+1,MagickPathExtent);
250   *p='\0';
251   return(SetImageProperty(image,key,value,exception));
252 }
253 \f
254 /*
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 %                                                                             %
257 %                                                                             %
258 %                                                                             %
259 %   D e l e t e I m a g e P r o p e r t y                                     %
260 %                                                                             %
261 %                                                                             %
262 %                                                                             %
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 %
265 %  DeleteImageProperty() deletes an image property.
266 %
267 %  The format of the DeleteImageProperty method is:
268 %
269 %      MagickBooleanType DeleteImageProperty(Image *image,const char *property)
270 %
271 %  A description of each parameter follows:
272 %
273 %    o image: the image.
274 %
275 %    o property: the image property.
276 %
277 */
278 MagickExport MagickBooleanType DeleteImageProperty(Image *image,
279   const char *property)
280 {
281   assert(image != (Image *) NULL);
282   assert(image->signature == MagickCoreSignature);
283   if (image->debug != MagickFalse)
284     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
285       image->filename);
286   if (image->properties == (void *) NULL)
287     return(MagickFalse);
288   return(DeleteNodeFromSplayTree((SplayTreeInfo *) image->properties,property));
289 }
290 \f
291 /*
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 %                                                                             %
294 %                                                                             %
295 %                                                                             %
296 %   D e s t r o y I m a g e P r o p e r t i e s                               %
297 %                                                                             %
298 %                                                                             %
299 %                                                                             %
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 %
302 %  DestroyImageProperties() destroys all properties and associated memory
303 %  attached to the given image.
304 %
305 %  The format of the DestroyDefines method is:
306 %
307 %      void DestroyImageProperties(Image *image)
308 %
309 %  A description of each parameter follows:
310 %
311 %    o image: the image.
312 %
313 */
314 MagickExport void DestroyImageProperties(Image *image)
315 {
316   assert(image != (Image *) NULL);
317   assert(image->signature == MagickCoreSignature);
318   if (image->debug != MagickFalse)
319     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
320       image->filename);
321   if (image->properties != (void *) NULL)
322     image->properties=(void *) DestroySplayTree((SplayTreeInfo *)
323       image->properties);
324 }
325 \f
326 /*
327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 %                                                                             %
329 %                                                                             %
330 %                                                                             %
331 %  F o r m a t I m a g e P r o p e r t y                                      %
332 %                                                                             %
333 %                                                                             %
334 %                                                                             %
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 %
337 %  FormatImageProperty() permits formatted property/value pairs to be saved as
338 %  an image property.
339 %
340 %  The format of the FormatImageProperty method is:
341 %
342 %      MagickBooleanType FormatImageProperty(Image *image,const char *property,
343 %        const char *format,...)
344 %
345 %  A description of each parameter follows.
346 %
347 %   o  image:  The image.
348 %
349 %   o  property:  The attribute property.
350 %
351 %   o  format:  A string describing the format to use to write the remaining
352 %      arguments.
353 %
354 */
355 MagickExport MagickBooleanType FormatImageProperty(Image *image,
356   const char *property,const char *format,...)
357 {
358   char
359     value[MagickPathExtent];
360
361   ExceptionInfo
362     *exception;
363
364   MagickBooleanType
365     status;
366
367   ssize_t
368     n;
369
370   va_list
371     operands;
372
373   va_start(operands,format);
374   n=FormatLocaleStringList(value,MagickPathExtent,format,operands);
375   (void) n;
376   va_end(operands);
377   exception=AcquireExceptionInfo();
378   status=SetImageProperty(image,property,value,exception);
379   exception=DestroyExceptionInfo(exception);
380   return(status);
381 }
382 \f
383 /*
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
385 %                                                                             %
386 %                                                                             %
387 %                                                                             %
388 %   G e t I m a g e P r o p e r t y                                           %
389 %                                                                             %
390 %                                                                             %
391 %                                                                             %
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 %
394 %  GetImageProperty() gets a value associated with an image property.
395 %
396 %  This includes,  profile prefixes, such as "exif:", "iptc:" and "8bim:"
397 %  It does not handle non-prifile prefixes, such as "fx:", "option:", or
398 %  "artifact:".
399 %
400 %  The returned string is stored as a properity of the same name for faster
401 %  lookup later. It should NOT be freed by the caller.
402 %
403 %  The format of the GetImageProperty method is:
404 %
405 %      const char *GetImageProperty(const Image *image,const char *key,
406 %        ExceptionInfo *exception)
407 %
408 %  A description of each parameter follows:
409 %
410 %    o image: the image.
411 %
412 %    o key: the key.
413 %
414 %    o exception: return any errors or warnings in this structure.
415 %
416 */
417
418 static char
419   *TracePSClippath(const unsigned char *,size_t),
420   *TraceSVGClippath(const unsigned char *,size_t,const size_t,
421     const size_t);
422
423 static MagickBooleanType GetIPTCProperty(const Image *image,const char *key,
424   ExceptionInfo *exception)
425 {
426   char
427     *attribute,
428     *message;
429
430   const StringInfo
431     *profile;
432
433   long
434     count,
435     dataset,
436     record;
437
438   register ssize_t
439     i;
440
441   size_t
442     length;
443
444   profile=GetImageProfile(image,"iptc");
445   if (profile == (StringInfo *) NULL)
446     profile=GetImageProfile(image,"8bim");
447   if (profile == (StringInfo *) NULL)
448     return(MagickFalse);
449   count=sscanf(key,"IPTC:%ld:%ld",&dataset,&record);
450   if (count != 2)
451     return(MagickFalse);
452   attribute=(char *) NULL;
453   for (i=0; i < (ssize_t) GetStringInfoLength(profile); i+=(ssize_t) length)
454   {
455     length=1;
456     if ((ssize_t) GetStringInfoDatum(profile)[i] != 0x1c)
457       continue;
458     length=(size_t) (GetStringInfoDatum(profile)[i+3] << 8);
459     length|=GetStringInfoDatum(profile)[i+4];
460     if (((long) GetStringInfoDatum(profile)[i+1] == dataset) &&
461         ((long) GetStringInfoDatum(profile)[i+2] == record))
462       {
463         message=(char *) NULL;
464         if (~length >= 1)
465           message=(char *) AcquireQuantumMemory(length+1UL,sizeof(*message));
466         if (message != (char *) NULL)
467           {
468             (void) CopyMagickString(message,(char *) GetStringInfoDatum(
469               profile)+i+5,length+1);
470             (void) ConcatenateString(&attribute,message);
471             (void) ConcatenateString(&attribute,";");
472             message=DestroyString(message);
473           }
474       }
475     i+=5;
476   }
477   if ((attribute == (char *) NULL) || (*attribute == ';'))
478     {
479       if (attribute != (char *) NULL)
480         attribute=DestroyString(attribute);
481       return(MagickFalse);
482     }
483   attribute[strlen(attribute)-1]='\0';
484   (void) SetImageProperty((Image *) image,key,(const char *) attribute,
485     exception);
486   attribute=DestroyString(attribute);
487   return(MagickTrue);
488 }
489
490 static inline int ReadPropertyByte(const unsigned char **p,size_t *length)
491 {
492   int
493     c;
494
495   if (*length < 1)
496     return(EOF);
497   c=(int) (*(*p)++);
498   (*length)--;
499   return(c);
500 }
501
502 static inline signed int ReadPropertyMSBLong(const unsigned char **p,
503   size_t *length)
504 {
505   union
506   {
507     unsigned int
508       unsigned_value;
509
510     signed int
511       signed_value;
512   } quantum;
513
514   int
515     c;
516
517   register ssize_t
518     i;
519
520   unsigned char
521     buffer[4];
522
523   unsigned int
524     value;
525
526   if (*length < 4)
527     return(-1);
528   for (i=0; i < 4; i++)
529   {
530     c=(int) (*(*p)++);
531     (*length)--;
532     buffer[i]=(unsigned char) c;
533   }
534   value=(unsigned int) buffer[0] << 24;
535   value|=(unsigned int) buffer[1] << 16;
536   value|=(unsigned int) buffer[2] << 8;
537   value|=(unsigned int) buffer[3];
538   quantum.unsigned_value=value & 0xffffffff;
539   return(quantum.signed_value);
540 }
541
542 static inline signed short ReadPropertyMSBShort(const unsigned char **p,
543   size_t *length)
544 {
545   union
546   {
547     unsigned short
548       unsigned_value;
549
550     signed short
551       signed_value;
552   } quantum;
553
554   int
555     c;
556
557   register ssize_t
558     i;
559
560   unsigned char
561     buffer[2];
562
563   unsigned short
564     value;
565
566   if (*length < 2)
567     return((unsigned short) ~0);
568   for (i=0; i < 2; i++)
569   {
570     c=(int) (*(*p)++);
571     (*length)--;
572     buffer[i]=(unsigned char) c;
573   }
574   value=(unsigned short) buffer[0] << 8;
575   value|=(unsigned short) buffer[1];
576   quantum.unsigned_value=value & 0xffff;
577   return(quantum.signed_value);
578 }
579
580 static MagickBooleanType Get8BIMProperty(const Image *image,const char *key,
581   ExceptionInfo *exception)
582 {
583   char
584     *attribute,
585     format[MagickPathExtent],
586     name[MagickPathExtent],
587     *resource;
588
589   const StringInfo
590     *profile;
591
592   const unsigned char
593     *info;
594
595   long
596     start,
597     stop;
598
599   MagickBooleanType
600     status;
601
602   register ssize_t
603     i;
604
605   ssize_t
606     count,
607     id,
608     sub_number;
609
610   size_t
611     length;
612
613   /*
614     There are no newlines in path names, so it's safe as terminator.
615   */
616   profile=GetImageProfile(image,"8bim");
617   if (profile == (StringInfo *) NULL)
618     return(MagickFalse);
619   count=(ssize_t) sscanf(key,"8BIM:%ld,%ld:%1024[^\n]\n%1024[^\n]",&start,&stop,
620     name,format);
621   if ((count != 2) && (count != 3) && (count != 4))
622     return(MagickFalse);
623   if (count < 4)
624     (void) CopyMagickString(format,"SVG",MagickPathExtent);
625   if (count < 3)
626     *name='\0';
627   sub_number=1;
628   if (*name == '#')
629     sub_number=(ssize_t) StringToLong(&name[1]);
630   sub_number=MagickMax(sub_number,1L);
631   resource=(char *) NULL;
632   status=MagickFalse;
633   length=GetStringInfoLength(profile);
634   info=GetStringInfoDatum(profile);
635   while ((length > 0) && (status == MagickFalse))
636   {
637     if (ReadPropertyByte(&info,&length) != (unsigned char) '8')
638       continue;
639     if (ReadPropertyByte(&info,&length) != (unsigned char) 'B')
640       continue;
641     if (ReadPropertyByte(&info,&length) != (unsigned char) 'I')
642       continue;
643     if (ReadPropertyByte(&info,&length) != (unsigned char) 'M')
644       continue;
645     id=(ssize_t) ReadPropertyMSBShort(&info,&length);
646     if (id < (ssize_t) start)
647       continue;
648     if (id > (ssize_t) stop)
649       continue;
650     if (resource != (char *) NULL)
651       resource=DestroyString(resource);
652     count=(ssize_t) ReadPropertyByte(&info,&length);
653     if ((count != 0) && ((size_t) count <= length))
654       {
655         resource=(char *) NULL;
656         if (~((size_t) count) >= (MagickPathExtent-1))
657           resource=(char *) AcquireQuantumMemory((size_t) count+
658             MagickPathExtent,sizeof(*resource));
659         if (resource != (char *) NULL)
660           {
661             for (i=0; i < (ssize_t) count; i++)
662               resource[i]=(char) ReadPropertyByte(&info,&length);
663             resource[count]='\0';
664           }
665       }
666     if ((count & 0x01) == 0)
667       (void) ReadPropertyByte(&info,&length);
668     count=(ssize_t) ReadPropertyMSBLong(&info,&length);
669     if ((*name != '\0') && (*name != '#'))
670       if ((resource == (char *) NULL) || (LocaleCompare(name,resource) != 0))
671         {
672           /*
673             No name match, scroll forward and try next.
674           */
675           info+=count;
676           length-=MagickMin(count,(ssize_t) length);
677           continue;
678         }
679     if ((*name == '#') && (sub_number != 1))
680       {
681         /*
682           No numbered match, scroll forward and try next.
683         */
684         sub_number--;
685         info+=count;
686         length-=MagickMin(count,(ssize_t) length);
687         continue;
688       }
689     /*
690       We have the resource of interest.
691     */
692     attribute=(char *) NULL;
693     if (~((size_t) count) >= (MagickPathExtent-1))
694       attribute=(char *) AcquireQuantumMemory((size_t) count+MagickPathExtent,
695         sizeof(*attribute));
696     if (attribute != (char *) NULL)
697       {
698         (void) CopyMagickMemory(attribute,(char *) info,(size_t) count);
699         attribute[count]='\0';
700         info+=count;
701         length-=MagickMin(count,(ssize_t) length);
702         if ((id <= 1999) || (id >= 2999))
703           (void) SetImageProperty((Image *) image,key,(const char *)
704             attribute,exception);
705         else
706           {
707             char
708               *path;
709
710             if (LocaleCompare(format,"svg") == 0)
711               path=TraceSVGClippath((unsigned char *) attribute,(size_t) count,
712                 image->columns,image->rows);
713             else
714               path=TracePSClippath((unsigned char *) attribute,(size_t) count);
715             (void) SetImageProperty((Image *) image,key,(const char *) path,
716               exception);
717             path=DestroyString(path);
718           }
719         attribute=DestroyString(attribute);
720         status=MagickTrue;
721       }
722   }
723   if (resource != (char *) NULL)
724     resource=DestroyString(resource);
725   return(status);
726 }
727
728 static inline signed int ReadPropertySignedLong(const EndianType endian,
729   const unsigned char *buffer)
730 {
731   union
732   {
733     unsigned int
734       unsigned_value;
735
736     signed int
737       signed_value;
738   } quantum;
739
740   unsigned int
741     value;
742
743   if (endian == LSBEndian)
744     {
745       value=(unsigned int) buffer[3] << 24;
746       value|=(unsigned int) buffer[2] << 16;
747       value|=(unsigned int) buffer[1] << 8;
748       value|=(unsigned int) buffer[0];
749       quantum.unsigned_value=value & 0xffffffff;
750       return(quantum.signed_value);
751     }
752   value=(unsigned int) buffer[0] << 24;
753   value|=(unsigned int) buffer[1] << 16;
754   value|=(unsigned int) buffer[2] << 8;
755   value|=(unsigned int) buffer[3];
756   quantum.unsigned_value=value & 0xffffffff;
757   return(quantum.signed_value);
758 }
759
760 static inline unsigned int ReadPropertyUnsignedLong(const EndianType endian,
761   const unsigned char *buffer)
762 {
763   unsigned int
764     value;
765
766   if (endian == LSBEndian)
767     {
768       value=(unsigned int) buffer[3] << 24;
769       value|=(unsigned int) buffer[2] << 16;
770       value|=(unsigned int) buffer[1] << 8;
771       value|=(unsigned int) buffer[0];
772       return(value & 0xffffffff);
773     }
774   value=(unsigned int) buffer[0] << 24;
775   value|=(unsigned int) buffer[1] << 16;
776   value|=(unsigned int) buffer[2] << 8;
777   value|=(unsigned int) buffer[3];
778   return(value & 0xffffffff);
779 }   
780
781 static inline signed short ReadPropertySignedShort(const EndianType endian,
782   const unsigned char *buffer)
783 {
784   union
785   {
786     unsigned short
787       unsigned_value;
788
789     signed short
790       signed_value;
791   } quantum;
792
793   unsigned short
794     value;
795
796   if (endian == LSBEndian)
797     {
798       value=(unsigned short) buffer[1] << 8;
799       value|=(unsigned short) buffer[0];
800       quantum.unsigned_value=value & 0xffff;
801       return(quantum.signed_value);
802     }
803   value=(unsigned short) buffer[0] << 8;
804   value|=(unsigned short) buffer[1];
805   quantum.unsigned_value=value & 0xffff;
806   return(quantum.signed_value);
807 }
808
809 static inline unsigned short ReadPropertyUnsignedShort(const EndianType endian,
810   const unsigned char *buffer)
811 {
812   unsigned short
813     value;
814
815   if (endian == LSBEndian)
816     {
817       value=(unsigned short) buffer[1] << 8;
818       value|=(unsigned short) buffer[0];
819       return(value & 0xffff);
820     }
821   value=(unsigned short) buffer[0] << 8;
822   value|=(unsigned short) buffer[1];
823   return(value & 0xffff);
824 }
825
826 static MagickBooleanType GetEXIFProperty(const Image *image,
827   const char *property,ExceptionInfo *exception)
828 {
829 #define MaxDirectoryStack  16
830 #define EXIF_DELIMITER  "\n"
831 #define EXIF_NUM_FORMATS  12
832 #define EXIF_FMT_BYTE  1
833 #define EXIF_FMT_STRING  2
834 #define EXIF_FMT_USHORT  3
835 #define EXIF_FMT_ULONG  4
836 #define EXIF_FMT_URATIONAL  5
837 #define EXIF_FMT_SBYTE  6
838 #define EXIF_FMT_UNDEFINED  7
839 #define EXIF_FMT_SSHORT  8
840 #define EXIF_FMT_SLONG  9
841 #define EXIF_FMT_SRATIONAL  10
842 #define EXIF_FMT_SINGLE  11
843 #define EXIF_FMT_DOUBLE  12
844 #define TAG_EXIF_OFFSET  0x8769
845 #define TAG_GPS_OFFSET  0x8825
846 #define TAG_INTEROP_OFFSET  0xa005
847
848 #define EXIFMultipleValues(size,format,arg) \
849 { \
850    ssize_t \
851      component; \
852  \
853    size_t \
854      length; \
855  \
856    unsigned char \
857      *p1; \
858  \
859    length=0; \
860    p1=p; \
861    for (component=0; component < components; component++) \
862    { \
863      length+=FormatLocaleString(buffer+length,MagickPathExtent-length, \
864        format", ",arg); \
865      if (length >= (MagickPathExtent-1)) \
866        length=MagickPathExtent-1; \
867      p1+=size; \
868    } \
869    if (length > 1) \
870      buffer[length-2]='\0'; \
871    value=AcquireString(buffer); \
872 }
873
874 #define EXIFMultipleFractions(size,format,arg1,arg2) \
875 { \
876    ssize_t \
877      component; \
878  \
879    size_t \
880      length; \
881  \
882    unsigned char \
883      *p1; \
884  \
885    length=0; \
886    p1=p; \
887    for (component=0; component < components; component++) \
888    { \
889      length+=FormatLocaleString(buffer+length,MagickPathExtent-length, \
890        format", ",(arg1),(arg2)); \
891      if (length >= (MagickPathExtent-1)) \
892        length=MagickPathExtent-1; \
893      p1+=size; \
894    } \
895    if (length > 1) \
896      buffer[length-2]='\0'; \
897    value=AcquireString(buffer); \
898 }
899
900   typedef struct _DirectoryInfo
901   {
902     const unsigned char
903       *directory;
904
905     size_t
906       entry;
907
908     ssize_t
909       offset;
910   } DirectoryInfo;
911
912   typedef struct _TagInfo
913   {
914     size_t
915       tag;
916
917     const char
918       *description;
919   } TagInfo;
920
921   static TagInfo
922     EXIFTag[] =
923     {
924       {  0x001, "exif:InteroperabilityIndex" },
925       {  0x002, "exif:InteroperabilityVersion" },
926       {  0x100, "exif:ImageWidth" },
927       {  0x101, "exif:ImageLength" },
928       {  0x102, "exif:BitsPerSample" },
929       {  0x103, "exif:Compression" },
930       {  0x106, "exif:PhotometricInterpretation" },
931       {  0x10a, "exif:FillOrder" },
932       {  0x10d, "exif:DocumentName" },
933       {  0x10e, "exif:ImageDescription" },
934       {  0x10f, "exif:Make" },
935       {  0x110, "exif:Model" },
936       {  0x111, "exif:StripOffsets" },
937       {  0x112, "exif:Orientation" },
938       {  0x115, "exif:SamplesPerPixel" },
939       {  0x116, "exif:RowsPerStrip" },
940       {  0x117, "exif:StripByteCounts" },
941       {  0x11a, "exif:XResolution" },
942       {  0x11b, "exif:YResolution" },
943       {  0x11c, "exif:PlanarConfiguration" },
944       {  0x11d, "exif:PageName" },
945       {  0x11e, "exif:XPosition" },
946       {  0x11f, "exif:YPosition" },
947       {  0x118, "exif:MinSampleValue" },
948       {  0x119, "exif:MaxSampleValue" },
949       {  0x120, "exif:FreeOffsets" },
950       {  0x121, "exif:FreeByteCounts" },
951       {  0x122, "exif:GrayResponseUnit" },
952       {  0x123, "exif:GrayResponseCurve" },
953       {  0x124, "exif:T4Options" },
954       {  0x125, "exif:T6Options" },
955       {  0x128, "exif:ResolutionUnit" },
956       {  0x12d, "exif:TransferFunction" },
957       {  0x131, "exif:Software" },
958       {  0x132, "exif:DateTime" },
959       {  0x13b, "exif:Artist" },
960       {  0x13e, "exif:WhitePoint" },
961       {  0x13f, "exif:PrimaryChromaticities" },
962       {  0x140, "exif:ColorMap" },
963       {  0x141, "exif:HalfToneHints" },
964       {  0x142, "exif:TileWidth" },
965       {  0x143, "exif:TileLength" },
966       {  0x144, "exif:TileOffsets" },
967       {  0x145, "exif:TileByteCounts" },
968       {  0x14a, "exif:SubIFD" },
969       {  0x14c, "exif:InkSet" },
970       {  0x14d, "exif:InkNames" },
971       {  0x14e, "exif:NumberOfInks" },
972       {  0x150, "exif:DotRange" },
973       {  0x151, "exif:TargetPrinter" },
974       {  0x152, "exif:ExtraSample" },
975       {  0x153, "exif:SampleFormat" },
976       {  0x154, "exif:SMinSampleValue" },
977       {  0x155, "exif:SMaxSampleValue" },
978       {  0x156, "exif:TransferRange" },
979       {  0x157, "exif:ClipPath" },
980       {  0x158, "exif:XClipPathUnits" },
981       {  0x159, "exif:YClipPathUnits" },
982       {  0x15a, "exif:Indexed" },
983       {  0x15b, "exif:JPEGTables" },
984       {  0x15f, "exif:OPIProxy" },
985       {  0x200, "exif:JPEGProc" },
986       {  0x201, "exif:JPEGInterchangeFormat" },
987       {  0x202, "exif:JPEGInterchangeFormatLength" },
988       {  0x203, "exif:JPEGRestartInterval" },
989       {  0x205, "exif:JPEGLosslessPredictors" },
990       {  0x206, "exif:JPEGPointTransforms" },
991       {  0x207, "exif:JPEGQTables" },
992       {  0x208, "exif:JPEGDCTables" },
993       {  0x209, "exif:JPEGACTables" },
994       {  0x211, "exif:YCbCrCoefficients" },
995       {  0x212, "exif:YCbCrSubSampling" },
996       {  0x213, "exif:YCbCrPositioning" },
997       {  0x214, "exif:ReferenceBlackWhite" },
998       {  0x2bc, "exif:ExtensibleMetadataPlatform" },
999       {  0x301, "exif:Gamma" },
1000       {  0x302, "exif:ICCProfileDescriptor" },
1001       {  0x303, "exif:SRGBRenderingIntent" },
1002       {  0x320, "exif:ImageTitle" },
1003       {  0x5001, "exif:ResolutionXUnit" },
1004       {  0x5002, "exif:ResolutionYUnit" },
1005       {  0x5003, "exif:ResolutionXLengthUnit" },
1006       {  0x5004, "exif:ResolutionYLengthUnit" },
1007       {  0x5005, "exif:PrintFlags" },
1008       {  0x5006, "exif:PrintFlagsVersion" },
1009       {  0x5007, "exif:PrintFlagsCrop" },
1010       {  0x5008, "exif:PrintFlagsBleedWidth" },
1011       {  0x5009, "exif:PrintFlagsBleedWidthScale" },
1012       {  0x500A, "exif:HalftoneLPI" },
1013       {  0x500B, "exif:HalftoneLPIUnit" },
1014       {  0x500C, "exif:HalftoneDegree" },
1015       {  0x500D, "exif:HalftoneShape" },
1016       {  0x500E, "exif:HalftoneMisc" },
1017       {  0x500F, "exif:HalftoneScreen" },
1018       {  0x5010, "exif:JPEGQuality" },
1019       {  0x5011, "exif:GridSize" },
1020       {  0x5012, "exif:ThumbnailFormat" },
1021       {  0x5013, "exif:ThumbnailWidth" },
1022       {  0x5014, "exif:ThumbnailHeight" },
1023       {  0x5015, "exif:ThumbnailColorDepth" },
1024       {  0x5016, "exif:ThumbnailPlanes" },
1025       {  0x5017, "exif:ThumbnailRawBytes" },
1026       {  0x5018, "exif:ThumbnailSize" },
1027       {  0x5019, "exif:ThumbnailCompressedSize" },
1028       {  0x501a, "exif:ColorTransferFunction" },
1029       {  0x501b, "exif:ThumbnailData" },
1030       {  0x5020, "exif:ThumbnailImageWidth" },
1031       {  0x5021, "exif:ThumbnailImageHeight" },
1032       {  0x5022, "exif:ThumbnailBitsPerSample" },
1033       {  0x5023, "exif:ThumbnailCompression" },
1034       {  0x5024, "exif:ThumbnailPhotometricInterp" },
1035       {  0x5025, "exif:ThumbnailImageDescription" },
1036       {  0x5026, "exif:ThumbnailEquipMake" },
1037       {  0x5027, "exif:ThumbnailEquipModel" },
1038       {  0x5028, "exif:ThumbnailStripOffsets" },
1039       {  0x5029, "exif:ThumbnailOrientation" },
1040       {  0x502a, "exif:ThumbnailSamplesPerPixel" },
1041       {  0x502b, "exif:ThumbnailRowsPerStrip" },
1042       {  0x502c, "exif:ThumbnailStripBytesCount" },
1043       {  0x502d, "exif:ThumbnailResolutionX" },
1044       {  0x502e, "exif:ThumbnailResolutionY" },
1045       {  0x502f, "exif:ThumbnailPlanarConfig" },
1046       {  0x5030, "exif:ThumbnailResolutionUnit" },
1047       {  0x5031, "exif:ThumbnailTransferFunction" },
1048       {  0x5032, "exif:ThumbnailSoftwareUsed" },
1049       {  0x5033, "exif:ThumbnailDateTime" },
1050       {  0x5034, "exif:ThumbnailArtist" },
1051       {  0x5035, "exif:ThumbnailWhitePoint" },
1052       {  0x5036, "exif:ThumbnailPrimaryChromaticities" },
1053       {  0x5037, "exif:ThumbnailYCbCrCoefficients" },
1054       {  0x5038, "exif:ThumbnailYCbCrSubsampling" },
1055       {  0x5039, "exif:ThumbnailYCbCrPositioning" },
1056       {  0x503A, "exif:ThumbnailRefBlackWhite" },
1057       {  0x503B, "exif:ThumbnailCopyRight" },
1058       {  0x5090, "exif:LuminanceTable" },
1059       {  0x5091, "exif:ChrominanceTable" },
1060       {  0x5100, "exif:FrameDelay" },
1061       {  0x5101, "exif:LoopCount" },
1062       {  0x5110, "exif:PixelUnit" },
1063       {  0x5111, "exif:PixelPerUnitX" },
1064       {  0x5112, "exif:PixelPerUnitY" },
1065       {  0x5113, "exif:PaletteHistogram" },
1066       {  0x1000, "exif:RelatedImageFileFormat" },
1067       {  0x1001, "exif:RelatedImageLength" },
1068       {  0x1002, "exif:RelatedImageWidth" },
1069       {  0x800d, "exif:ImageID" },
1070       {  0x80e3, "exif:Matteing" },
1071       {  0x80e4, "exif:DataType" },
1072       {  0x80e5, "exif:ImageDepth" },
1073       {  0x80e6, "exif:TileDepth" },
1074       {  0x828d, "exif:CFARepeatPatternDim" },
1075       {  0x828e, "exif:CFAPattern2" },
1076       {  0x828f, "exif:BatteryLevel" },
1077       {  0x8298, "exif:Copyright" },
1078       {  0x829a, "exif:ExposureTime" },
1079       {  0x829d, "exif:FNumber" },
1080       {  0x83bb, "exif:IPTC/NAA" },
1081       {  0x84e3, "exif:IT8RasterPadding" },
1082       {  0x84e5, "exif:IT8ColorTable" },
1083       {  0x8649, "exif:ImageResourceInformation" },
1084       {  0x8769, "exif:ExifOffset" },
1085       {  0x8773, "exif:InterColorProfile" },
1086       {  0x8822, "exif:ExposureProgram" },
1087       {  0x8824, "exif:SpectralSensitivity" },
1088       {  0x8825, "exif:GPSInfo" },
1089       {  0x8827, "exif:ISOSpeedRatings" },
1090       {  0x8828, "exif:OECF" },
1091       {  0x8829, "exif:Interlace" },
1092       {  0x882a, "exif:TimeZoneOffset" },
1093       {  0x882b, "exif:SelfTimerMode" },
1094       {  0x9000, "exif:ExifVersion" },
1095       {  0x9003, "exif:DateTimeOriginal" },
1096       {  0x9004, "exif:DateTimeDigitized" },
1097       {  0x9101, "exif:ComponentsConfiguration" },
1098       {  0x9102, "exif:CompressedBitsPerPixel" },
1099       {  0x9201, "exif:ShutterSpeedValue" },
1100       {  0x9202, "exif:ApertureValue" },
1101       {  0x9203, "exif:BrightnessValue" },
1102       {  0x9204, "exif:ExposureBiasValue" },
1103       {  0x9205, "exif:MaxApertureValue" },
1104       {  0x9206, "exif:SubjectDistance" },
1105       {  0x9207, "exif:MeteringMode" },
1106       {  0x9208, "exif:LightSource" },
1107       {  0x9209, "exif:Flash" },
1108       {  0x920a, "exif:FocalLength" },
1109       {  0x920b, "exif:FlashEnergy" },
1110       {  0x920c, "exif:SpatialFrequencyResponse" },
1111       {  0x920d, "exif:Noise" },
1112       {  0x9211, "exif:ImageNumber" },
1113       {  0x9212, "exif:SecurityClassification" },
1114       {  0x9213, "exif:ImageHistory" },
1115       {  0x9214, "exif:SubjectArea" },
1116       {  0x9215, "exif:ExposureIndex" },
1117       {  0x9216, "exif:TIFF-EPStandardID" },
1118       {  0x927c, "exif:MakerNote" },
1119       {  0x9C9b, "exif:WinXP-Title" },
1120       {  0x9C9c, "exif:WinXP-Comments" },
1121       {  0x9C9d, "exif:WinXP-Author" },
1122       {  0x9C9e, "exif:WinXP-Keywords" },
1123       {  0x9C9f, "exif:WinXP-Subject" },
1124       {  0x9286, "exif:UserComment" },
1125       {  0x9290, "exif:SubSecTime" },
1126       {  0x9291, "exif:SubSecTimeOriginal" },
1127       {  0x9292, "exif:SubSecTimeDigitized" },
1128       {  0xa000, "exif:FlashPixVersion" },
1129       {  0xa001, "exif:ColorSpace" },
1130       {  0xa002, "exif:ExifImageWidth" },
1131       {  0xa003, "exif:ExifImageLength" },
1132       {  0xa004, "exif:RelatedSoundFile" },
1133       {  0xa005, "exif:InteroperabilityOffset" },
1134       {  0xa20b, "exif:FlashEnergy" },
1135       {  0xa20c, "exif:SpatialFrequencyResponse" },
1136       {  0xa20d, "exif:Noise" },
1137       {  0xa20e, "exif:FocalPlaneXResolution" },
1138       {  0xa20f, "exif:FocalPlaneYResolution" },
1139       {  0xa210, "exif:FocalPlaneResolutionUnit" },
1140       {  0xa214, "exif:SubjectLocation" },
1141       {  0xa215, "exif:ExposureIndex" },
1142       {  0xa216, "exif:TIFF/EPStandardID" },
1143       {  0xa217, "exif:SensingMethod" },
1144       {  0xa300, "exif:FileSource" },
1145       {  0xa301, "exif:SceneType" },
1146       {  0xa302, "exif:CFAPattern" },
1147       {  0xa401, "exif:CustomRendered" },
1148       {  0xa402, "exif:ExposureMode" },
1149       {  0xa403, "exif:WhiteBalance" },
1150       {  0xa404, "exif:DigitalZoomRatio" },
1151       {  0xa405, "exif:FocalLengthIn35mmFilm" },
1152       {  0xa406, "exif:SceneCaptureType" },
1153       {  0xa407, "exif:GainControl" },
1154       {  0xa408, "exif:Contrast" },
1155       {  0xa409, "exif:Saturation" },
1156       {  0xa40a, "exif:Sharpness" },
1157       {  0xa40b, "exif:DeviceSettingDescription" },
1158       {  0xa40c, "exif:SubjectDistanceRange" },
1159       {  0xa420, "exif:ImageUniqueID" },
1160       {  0xc4a5, "exif:PrintImageMatching" },
1161       {  0xa500, "exif:Gamma" },
1162       {  0xc640, "exif:CR2Slice" },
1163       { 0x10000, "exif:GPSVersionID" },
1164       { 0x10001, "exif:GPSLatitudeRef" },
1165       { 0x10002, "exif:GPSLatitude" },
1166       { 0x10003, "exif:GPSLongitudeRef" },
1167       { 0x10004, "exif:GPSLongitude" },
1168       { 0x10005, "exif:GPSAltitudeRef" },
1169       { 0x10006, "exif:GPSAltitude" },
1170       { 0x10007, "exif:GPSTimeStamp" },
1171       { 0x10008, "exif:GPSSatellites" },
1172       { 0x10009, "exif:GPSStatus" },
1173       { 0x1000a, "exif:GPSMeasureMode" },
1174       { 0x1000b, "exif:GPSDop" },
1175       { 0x1000c, "exif:GPSSpeedRef" },
1176       { 0x1000d, "exif:GPSSpeed" },
1177       { 0x1000e, "exif:GPSTrackRef" },
1178       { 0x1000f, "exif:GPSTrack" },
1179       { 0x10010, "exif:GPSImgDirectionRef" },
1180       { 0x10011, "exif:GPSImgDirection" },
1181       { 0x10012, "exif:GPSMapDatum" },
1182       { 0x10013, "exif:GPSDestLatitudeRef" },
1183       { 0x10014, "exif:GPSDestLatitude" },
1184       { 0x10015, "exif:GPSDestLongitudeRef" },
1185       { 0x10016, "exif:GPSDestLongitude" },
1186       { 0x10017, "exif:GPSDestBearingRef" },
1187       { 0x10018, "exif:GPSDestBearing" },
1188       { 0x10019, "exif:GPSDestDistanceRef" },
1189       { 0x1001a, "exif:GPSDestDistance" },
1190       { 0x1001b, "exif:GPSProcessingMethod" },
1191       { 0x1001c, "exif:GPSAreaInformation" },
1192       { 0x1001d, "exif:GPSDateStamp" },
1193       { 0x1001e, "exif:GPSDifferential" },
1194       { 0x00000, (const char *) NULL }
1195     };
1196
1197   const StringInfo
1198     *profile;
1199
1200   const unsigned char
1201     *directory,
1202     *exif;
1203
1204   DirectoryInfo
1205     directory_stack[MaxDirectoryStack];
1206
1207   EndianType
1208     endian;
1209
1210   MagickBooleanType
1211     status;
1212
1213   register ssize_t
1214     i;
1215
1216   size_t
1217     entry,
1218     length,
1219     number_entries,
1220     tag,
1221     tag_value;
1222
1223   SplayTreeInfo
1224     *exif_resources;
1225
1226   ssize_t
1227     all,
1228     id,
1229     level,
1230     offset,
1231     tag_offset;
1232
1233   static int
1234     tag_bytes[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8};
1235
1236   /*
1237     If EXIF data exists, then try to parse the request for a tag.
1238   */
1239   profile=GetImageProfile(image,"exif");
1240   if (profile == (const StringInfo *) NULL)
1241     return(MagickFalse);
1242   if ((property == (const char *) NULL) || (*property == '\0'))
1243     return(MagickFalse);
1244   while (isspace((int) ((unsigned char) *property)) != 0)
1245     property++;
1246   if (strlen(property) <= 5)
1247     return(MagickFalse);
1248   all=0;
1249   tag=(~0UL);
1250   switch (*(property+5))
1251   {
1252     case '*':
1253     {
1254       /*
1255         Caller has asked for all the tags in the EXIF data.
1256       */
1257       tag=0;
1258       all=1; /* return the data in description=value format */
1259       break;
1260     }
1261     case '!':
1262     {
1263       tag=0;
1264       all=2; /* return the data in tagid=value format */
1265       break;
1266     }
1267     case '#':
1268     case '@':
1269     {
1270       int
1271         c;
1272
1273       size_t
1274         n;
1275
1276       /*
1277         Check for a hex based tag specification first.
1278       */
1279       tag=(*(property+5) == '@') ? 1UL : 0UL;
1280       property+=6;
1281       n=strlen(property);
1282       if (n != 4)
1283         return(MagickFalse);
1284       /*
1285         Parse tag specification as a hex number.
1286       */
1287       n/=4;
1288       do
1289       {
1290         for (i=(ssize_t) n-1L; i >= 0; i--)
1291         {
1292           c=(*property++);
1293           tag<<=4;
1294           if ((c >= '0') && (c <= '9'))
1295             tag|=(c-'0');
1296           else
1297             if ((c >= 'A') && (c <= 'F'))
1298               tag|=(c-('A'-10));
1299             else
1300               if ((c >= 'a') && (c <= 'f'))
1301                 tag|=(c-('a'-10));
1302               else
1303                 return(MagickFalse);
1304         }
1305       } while (*property != '\0');
1306       break;
1307     }
1308     default:
1309     {
1310       /*
1311         Try to match the text with a tag name instead.
1312       */
1313       for (i=0; ; i++)
1314       {
1315         if (EXIFTag[i].tag == 0)
1316           break;
1317         if (LocaleCompare(EXIFTag[i].description,property) == 0)
1318           {
1319             tag=(size_t) EXIFTag[i].tag;
1320             break;
1321           }
1322       }
1323       break;
1324     }
1325   }
1326   if (tag == (~0UL))
1327     return(MagickFalse);
1328   length=GetStringInfoLength(profile);
1329   exif=GetStringInfoDatum(profile);
1330   while (length != 0)
1331   {
1332     if (ReadPropertyByte(&exif,&length) != 0x45)
1333       continue;
1334     if (ReadPropertyByte(&exif,&length) != 0x78)
1335       continue;
1336     if (ReadPropertyByte(&exif,&length) != 0x69)
1337       continue;
1338     if (ReadPropertyByte(&exif,&length) != 0x66)
1339       continue;
1340     if (ReadPropertyByte(&exif,&length) != 0x00)
1341       continue;
1342     if (ReadPropertyByte(&exif,&length) != 0x00)
1343       continue;
1344     break;
1345   }
1346   if (length < 16)
1347     return(MagickFalse);
1348   id=(ssize_t) ReadPropertySignedShort(LSBEndian,exif);
1349   endian=LSBEndian;
1350   if (id == 0x4949)
1351     endian=LSBEndian;
1352   else
1353     if (id == 0x4D4D)
1354       endian=MSBEndian;
1355     else
1356       return(MagickFalse);
1357   if (ReadPropertyUnsignedShort(endian,exif+2) != 0x002a)
1358     return(MagickFalse);
1359   /*
1360     This the offset to the first IFD.
1361   */
1362   offset=(ssize_t) ReadPropertySignedLong(endian,exif+4);
1363   if ((offset < 0) || (size_t) offset >= length)
1364     return(MagickFalse);
1365   /*
1366     Set the pointer to the first IFD and follow it were it leads.
1367   */
1368   status=MagickFalse;
1369   directory=exif+offset;
1370   level=0;
1371   entry=0;
1372   tag_offset=0;
1373   exif_resources=NewSplayTree((int (*)(const void *,const void *)) NULL,
1374     (void *(*)(void *)) NULL,(void *(*)(void *)) NULL);
1375   do
1376   {
1377     /*
1378       If there is anything on the stack then pop it off.
1379     */
1380     if (level > 0)
1381       {
1382         level--;
1383         directory=directory_stack[level].directory;
1384         entry=directory_stack[level].entry;
1385         tag_offset=directory_stack[level].offset;
1386       }
1387     if ((directory < exif) || (directory > (exif+length-2)))
1388       break;
1389     /*
1390       Determine how many entries there are in the current IFD.
1391     */
1392     number_entries=(size_t) ReadPropertyUnsignedShort(endian,directory);
1393     for ( ; entry < number_entries; entry++)
1394     {
1395       register unsigned char
1396         *p,
1397         *q;
1398
1399       size_t
1400         format;
1401
1402       ssize_t
1403         number_bytes,
1404         components;
1405
1406       q=(unsigned char *) (directory+(12*entry)+2);
1407       if (q > (exif+length-12))
1408         break;  /* corrupt EXIF */
1409       if (GetValueFromSplayTree(exif_resources,q) == q)
1410         break;
1411       (void) AddValueToSplayTree(exif_resources,q,q);
1412       tag_value=(size_t) ReadPropertyUnsignedShort(endian,q)+tag_offset;
1413       format=(size_t) ReadPropertyUnsignedShort(endian,q+2);
1414       if (format >= (sizeof(tag_bytes)/sizeof(*tag_bytes)))
1415         break;
1416       components=(ssize_t) ReadPropertySignedLong(endian,q+4);
1417       if (components < 0)
1418         break;  /* corrupt EXIF */
1419       number_bytes=(size_t) components*tag_bytes[format];
1420       if (number_bytes < components)
1421         break;  /* prevent overflow */
1422       if (number_bytes <= 4)
1423         p=q+8;
1424       else
1425         {
1426           ssize_t
1427             offset;
1428
1429           /*
1430             The directory entry contains an offset.
1431           */
1432           offset=(ssize_t) ReadPropertySignedLong(endian,q+8);
1433           if ((offset < 0) || (size_t) offset >= length)
1434             continue;
1435           if ((ssize_t) (offset+number_bytes) < offset)
1436             continue;  /* prevent overflow */
1437           if ((size_t) (offset+number_bytes) > length)
1438             continue;
1439           p=(unsigned char *) (exif+offset);
1440         }
1441       if ((all != 0) || (tag == (size_t) tag_value))
1442         {
1443           char
1444             buffer[MagickPathExtent],
1445             *value;
1446
1447           value=(char *) NULL;
1448           *buffer='\0';
1449           switch (format)
1450           {
1451             case EXIF_FMT_BYTE:
1452             case EXIF_FMT_UNDEFINED:
1453             {
1454               EXIFMultipleValues(1,"%.20g",(double) (*(unsigned char *) p1));
1455               break;
1456             }
1457             case EXIF_FMT_SBYTE:
1458             {
1459               EXIFMultipleValues(1,"%.20g",(double) (*(signed char *) p1));
1460               break;
1461             }
1462             case EXIF_FMT_SSHORT:
1463             {
1464               EXIFMultipleValues(2,"%hd",ReadPropertySignedShort(endian,p1));
1465               break;
1466             }
1467             case EXIF_FMT_USHORT:
1468             {
1469               EXIFMultipleValues(2,"%hu",ReadPropertyUnsignedShort(endian,p1));
1470               break;
1471             }
1472             case EXIF_FMT_ULONG:
1473             {
1474               EXIFMultipleValues(4,"%.20g",(double)
1475                 ReadPropertyUnsignedLong(endian,p1));
1476               break;
1477             }
1478             case EXIF_FMT_SLONG:
1479             {
1480               EXIFMultipleValues(4,"%.20g",(double)
1481                 ReadPropertySignedLong(endian,p1));
1482               break;
1483             }
1484             case EXIF_FMT_URATIONAL:
1485             {
1486               EXIFMultipleFractions(8,"%.20g/%.20g",(double)
1487                 ReadPropertyUnsignedLong(endian,p1),(double)
1488                 ReadPropertyUnsignedLong(endian,p1+4));
1489               break;
1490             }
1491             case EXIF_FMT_SRATIONAL:
1492             {
1493               EXIFMultipleFractions(8,"%.20g/%.20g",(double)
1494                 ReadPropertySignedLong(endian,p1),(double)
1495                 ReadPropertySignedLong(endian,p1+4));
1496               break;
1497             }
1498             case EXIF_FMT_SINGLE:
1499             {
1500               EXIFMultipleValues(4,"%f",(double) *(float *) p1);
1501               break;
1502             }
1503             case EXIF_FMT_DOUBLE:
1504             {
1505               EXIFMultipleValues(8,"%f",*(double *) p1);
1506               break;
1507             }
1508             default:
1509             case EXIF_FMT_STRING:
1510             {
1511               value=(char *) NULL;
1512               if (~((size_t) number_bytes) >= 1)
1513                 value=(char *) AcquireQuantumMemory((size_t) number_bytes+1UL,
1514                   sizeof(*value));
1515               if (value != (char *) NULL)
1516                 {
1517                   register ssize_t
1518                     i;
1519
1520                   for (i=0; i < (ssize_t) number_bytes; i++)
1521                   {
1522                     value[i]='.';
1523                     if ((isprint((int) p[i]) != 0) || (p[i] == '\0'))
1524                       value[i]=(char) p[i];
1525                   }
1526                   value[i]='\0';
1527                 }
1528               break;
1529             }
1530           }
1531           if (value != (char *) NULL)
1532             {
1533               char
1534                 *key;
1535
1536               register const char
1537                 *p;
1538
1539               key=AcquireString(property);
1540               switch (all)
1541               {
1542                 case 1:
1543                 {
1544                   const char
1545                     *description;
1546
1547                   register ssize_t
1548                     i;
1549
1550                   description="unknown";
1551                   for (i=0; ; i++)
1552                   {
1553                     if (EXIFTag[i].tag == 0)
1554                       break;
1555                     if (EXIFTag[i].tag == tag_value)
1556                       {
1557                         description=EXIFTag[i].description;
1558                         break;
1559                       }
1560                   }
1561                   (void) FormatLocaleString(key,MagickPathExtent,"%s",
1562                     description);
1563                   if (level == 2)
1564                     (void) SubstituteString(&key,"exif:","exif:thumbnail:");
1565                   break;
1566                 }
1567                 case 2:
1568                 {
1569                   if (tag_value < 0x10000)
1570                     (void) FormatLocaleString(key,MagickPathExtent,"#%04lx",
1571                       (unsigned long) tag_value);
1572                   else
1573                     if (tag_value < 0x20000)
1574                       (void) FormatLocaleString(key,MagickPathExtent,"@%04lx",
1575                         (unsigned long) (tag_value & 0xffff));
1576                     else
1577                       (void) FormatLocaleString(key,MagickPathExtent,"unknown");
1578                   break;
1579                 }
1580                 default:
1581                 {
1582                   if (level == 2)
1583                     (void) SubstituteString(&key,"exif:","exif:thumbnail:");
1584                 }
1585               }
1586               p=(const char *) NULL;
1587               if (image->properties != (void *) NULL)
1588                 p=(const char *) GetValueFromSplayTree((SplayTreeInfo *)
1589                   image->properties,key);
1590               if (p == (const char *) NULL)
1591                 (void) SetImageProperty((Image *) image,key,value,exception);
1592               value=DestroyString(value);
1593               key=DestroyString(key);
1594               status=MagickTrue;
1595             }
1596         }
1597         if ((tag_value == TAG_EXIF_OFFSET) ||
1598             (tag_value == TAG_INTEROP_OFFSET) || (tag_value == TAG_GPS_OFFSET))
1599           {
1600             ssize_t
1601               offset;
1602
1603             offset=(ssize_t) ReadPropertySignedLong(endian,p);
1604             if (((size_t) offset < length) && (level < (MaxDirectoryStack-2)))
1605               {
1606                 ssize_t
1607                   tag_offset1;
1608
1609                 tag_offset1=(ssize_t) ((tag_value == TAG_GPS_OFFSET) ? 0x10000 :
1610                   0);
1611                 directory_stack[level].directory=directory;
1612                 entry++;
1613                 directory_stack[level].entry=entry;
1614                 directory_stack[level].offset=tag_offset;
1615                 level++;
1616                 directory_stack[level].directory=exif+offset;
1617                 directory_stack[level].offset=tag_offset1;
1618                 directory_stack[level].entry=0;
1619                 level++;
1620                 if ((directory+2+(12*number_entries)) > (exif+length))
1621                   break;
1622                 offset=(ssize_t) ReadPropertySignedLong(endian,directory+2+(12*
1623                   number_entries));
1624                 if ((offset != 0) && ((size_t) offset < length) &&
1625                     (level < (MaxDirectoryStack-2)))
1626                   {
1627                     directory_stack[level].directory=exif+offset;
1628                     directory_stack[level].entry=0;
1629                     directory_stack[level].offset=tag_offset1;
1630                     level++;
1631                   }
1632               }
1633             break;
1634           }
1635     }
1636   } while (level > 0);
1637   exif_resources=DestroySplayTree(exif_resources);
1638   return(status);
1639 }
1640
1641 static MagickBooleanType GetICCProperty(const Image *image,const char *property,
1642   ExceptionInfo *exception)
1643 {
1644   const StringInfo
1645     *profile;
1646
1647   magick_unreferenced(property);
1648
1649   profile=GetImageProfile(image,"icc");
1650   if (profile == (StringInfo *) NULL)
1651     profile=GetImageProfile(image,"icm");
1652   if (profile == (StringInfo *) NULL)
1653     return(MagickFalse);
1654   if (GetStringInfoLength(profile) < 128)
1655     return(MagickFalse);  /* minimum ICC profile length */
1656 #if defined(MAGICKCORE_LCMS_DELEGATE)
1657   {
1658     cmsHPROFILE
1659       icc_profile;
1660
1661     icc_profile=cmsOpenProfileFromMem(GetStringInfoDatum(profile),
1662       (cmsUInt32Number) GetStringInfoLength(profile));
1663     if (icc_profile != (cmsHPROFILE *) NULL)
1664       {
1665 #if defined(LCMS_VERSION) && (LCMS_VERSION < 2000)
1666         const char
1667           *name;
1668
1669         name=cmsTakeProductName(icc_profile);
1670         if (name != (const char *) NULL)
1671           (void) SetImageProperty((Image *) image,"icc:name",name,exception);
1672 #else
1673         char
1674           info[MagickPathExtent];
1675
1676         (void) cmsGetProfileInfoASCII(icc_profile,cmsInfoDescription,
1677           "en","US",info,MagickPathExtent);
1678         (void) SetImageProperty((Image *) image,"icc:description",info,
1679           exception);
1680         (void) cmsGetProfileInfoASCII(icc_profile,cmsInfoManufacturer,
1681           "en","US",info,MagickPathExtent);
1682         (void) SetImageProperty((Image *) image,"icc:manufacturer",info,
1683           exception);
1684         (void) cmsGetProfileInfoASCII(icc_profile,cmsInfoModel,"en",
1685           "US",info,MagickPathExtent);
1686         (void) SetImageProperty((Image *) image,"icc:model",info,exception);
1687         (void) cmsGetProfileInfoASCII(icc_profile,cmsInfoCopyright,
1688           "en","US",info,MagickPathExtent);
1689         (void) SetImageProperty((Image *) image,"icc:copyright",info,exception);
1690 #endif
1691         (void) cmsCloseProfile(icc_profile);
1692       }
1693   }
1694 #endif
1695   return(MagickTrue);
1696 }
1697
1698 static MagickBooleanType SkipXMPValue(const char *value)
1699 {
1700   if (value == (const char*) NULL)
1701     return(MagickTrue);
1702   while (*value != '\0')
1703   {
1704     if (isspace((int) ((unsigned char) *value)) == 0)
1705       return(MagickFalse);
1706     value++;
1707   }
1708   return(MagickTrue);
1709 }
1710
1711 static MagickBooleanType GetXMPProperty(const Image *image,const char *property)
1712 {
1713   char
1714     *xmp_profile;
1715
1716   const char
1717     *content;
1718
1719   const StringInfo
1720     *profile;
1721
1722   ExceptionInfo
1723     *exception;
1724
1725   MagickBooleanType
1726     status;
1727
1728   register const char
1729     *p;
1730
1731   XMLTreeInfo
1732     *child,
1733     *description,
1734     *node,
1735     *rdf,
1736     *xmp;
1737
1738   profile=GetImageProfile(image,"xmp");
1739   if (profile == (StringInfo *) NULL)
1740     return(MagickFalse);
1741   if ((property == (const char *) NULL) || (*property == '\0'))
1742     return(MagickFalse);
1743   xmp_profile=StringInfoToString(profile);
1744   if (xmp_profile == (char *) NULL)
1745     return(MagickFalse);
1746   for (p=xmp_profile; *p != '\0'; p++)
1747     if ((*p == '<') && (*(p+1) == 'x'))
1748       break;
1749   exception=AcquireExceptionInfo();
1750   xmp=NewXMLTree((char *) p,exception);
1751   xmp_profile=DestroyString(xmp_profile);
1752   exception=DestroyExceptionInfo(exception);
1753   if (xmp == (XMLTreeInfo *) NULL)
1754     return(MagickFalse);
1755   status=MagickFalse;
1756   rdf=GetXMLTreeChild(xmp,"rdf:RDF");
1757   if (rdf != (XMLTreeInfo *) NULL)
1758     {
1759       if (image->properties == (void *) NULL)
1760         ((Image *) image)->properties=NewSplayTree(CompareSplayTreeString,
1761           RelinquishMagickMemory,RelinquishMagickMemory);
1762       description=GetXMLTreeChild(rdf,"rdf:Description");
1763       while (description != (XMLTreeInfo *) NULL)
1764       {
1765         node=GetXMLTreeChild(description,(const char *) NULL);
1766         while (node != (XMLTreeInfo *) NULL)
1767         {
1768           child=GetXMLTreeChild(node,(const char *) NULL);
1769           content=GetXMLTreeContent(node);
1770           if ((child == (XMLTreeInfo *) NULL) &&
1771               (SkipXMPValue(content) == MagickFalse))
1772             (void) AddValueToSplayTree((SplayTreeInfo *) image->properties,
1773               ConstantString(GetXMLTreeTag(node)),ConstantString(content));
1774           while (child != (XMLTreeInfo *) NULL)
1775           {
1776             content=GetXMLTreeContent(child);
1777             if (SkipXMPValue(content) == MagickFalse)
1778               (void) AddValueToSplayTree((SplayTreeInfo *) image->properties,
1779                 ConstantString(GetXMLTreeTag(child)),ConstantString(content));
1780             child=GetXMLTreeSibling(child);
1781           }
1782           node=GetXMLTreeSibling(node);
1783         }
1784         description=GetNextXMLTreeTag(description);
1785       }
1786     }
1787   xmp=DestroyXMLTree(xmp);
1788   return(status);
1789 }
1790
1791 static char *TracePSClippath(const unsigned char *blob,size_t length)
1792 {
1793   char
1794     *path,
1795     *message;
1796
1797   MagickBooleanType
1798     in_subpath;
1799
1800   PointInfo
1801     first[3],
1802     last[3],
1803     point[3];
1804
1805   register ssize_t
1806     i,
1807     x;
1808
1809   ssize_t
1810     knot_count,
1811     selector,
1812     y;
1813
1814   path=AcquireString((char *) NULL);
1815   if (path == (char *) NULL)
1816     return((char *) NULL);
1817   message=AcquireString((char *) NULL);
1818   (void) FormatLocaleString(message,MagickPathExtent,"/ClipImage\n");
1819   (void) ConcatenateString(&path,message);
1820   (void) FormatLocaleString(message,MagickPathExtent,"{\n");
1821   (void) ConcatenateString(&path,message);
1822   (void) FormatLocaleString(message,MagickPathExtent,
1823     "  /c {curveto} bind def\n");
1824   (void) ConcatenateString(&path,message);
1825   (void) FormatLocaleString(message,MagickPathExtent,
1826     "  /l {lineto} bind def\n");
1827   (void) ConcatenateString(&path,message);
1828   (void) FormatLocaleString(message,MagickPathExtent,
1829     "  /m {moveto} bind def\n");
1830   (void) ConcatenateString(&path,message);
1831   (void) FormatLocaleString(message,MagickPathExtent,
1832     "  /v {currentpoint 6 2 roll curveto} bind def\n");
1833   (void) ConcatenateString(&path,message);
1834   (void) FormatLocaleString(message,MagickPathExtent,
1835     "  /y {2 copy curveto} bind def\n");
1836   (void) ConcatenateString(&path,message);
1837   (void) FormatLocaleString(message,MagickPathExtent,
1838     "  /z {closepath} bind def\n");
1839   (void) ConcatenateString(&path,message);
1840   (void) FormatLocaleString(message,MagickPathExtent,"  newpath\n");
1841   (void) ConcatenateString(&path,message);
1842   /*
1843     The clipping path format is defined in "Adobe Photoshop File Formats
1844     Specification" version 6.0 downloadable from adobe.com.
1845   */
1846   (void) ResetMagickMemory(point,0,sizeof(point));
1847   (void) ResetMagickMemory(first,0,sizeof(first));
1848   (void) ResetMagickMemory(last,0,sizeof(last));
1849   knot_count=0;
1850   in_subpath=MagickFalse;
1851   while (length > 0)
1852   {
1853     selector=(ssize_t) ReadPropertyMSBShort(&blob,&length);
1854     switch (selector)
1855     {
1856       case 0:
1857       case 3:
1858       {
1859         if (knot_count != 0)
1860           {
1861             blob+=24;
1862             length-=MagickMin(24,(ssize_t) length);
1863             break;
1864           }
1865         /*
1866           Expected subpath length record.
1867         */
1868         knot_count=(ssize_t) ReadPropertyMSBShort(&blob,&length);
1869         blob+=22;
1870         length-=MagickMin(22,(ssize_t) length);
1871         break;
1872       }
1873       case 1:
1874       case 2:
1875       case 4:
1876       case 5:
1877       {
1878         if (knot_count == 0)
1879           {
1880             /*
1881               Unexpected subpath knot
1882             */
1883             blob+=24;
1884             length-=MagickMin(24,(ssize_t) length);
1885             break;
1886           }
1887         /*
1888           Add sub-path knot
1889         */
1890         for (i=0; i < 3; i++)
1891         {
1892           size_t
1893             xx,
1894             yy;
1895
1896           yy=(size_t) ReadPropertyMSBLong(&blob,&length);
1897           xx=(size_t) ReadPropertyMSBLong(&blob,&length);
1898           x=(ssize_t) xx;
1899           if (xx > 2147483647)
1900             x=(ssize_t) xx-4294967295U-1;
1901           y=(ssize_t) yy;
1902           if (yy > 2147483647)
1903             y=(ssize_t) yy-4294967295U-1;
1904           point[i].x=(double) x/4096/4096;
1905           point[i].y=1.0-(double) y/4096/4096;
1906         }
1907         if (in_subpath == MagickFalse)
1908           {
1909             (void) FormatLocaleString(message,MagickPathExtent,"  %g %g m\n",
1910               point[1].x,point[1].y);
1911             for (i=0; i < 3; i++)
1912             {
1913               first[i]=point[i];
1914               last[i]=point[i];
1915             }
1916           }
1917         else
1918           {
1919             /*
1920               Handle special cases when Bezier curves are used to describe
1921               corners and straight lines.
1922             */
1923             if ((last[1].x == last[2].x) && (last[1].y == last[2].y) &&
1924                 (point[0].x == point[1].x) && (point[0].y == point[1].y))
1925               (void) FormatLocaleString(message,MagickPathExtent,
1926                 "  %g %g l\n",point[1].x,point[1].y);
1927             else
1928               if ((last[1].x == last[2].x) && (last[1].y == last[2].y))
1929                 (void) FormatLocaleString(message,MagickPathExtent,
1930                   "  %g %g %g %g v\n",point[0].x,point[0].y,
1931                   point[1].x,point[1].y);
1932               else
1933                 if ((point[0].x == point[1].x) && (point[0].y == point[1].y))
1934                   (void) FormatLocaleString(message,MagickPathExtent,
1935                     "  %g %g %g %g y\n",last[2].x,last[2].y,
1936                     point[1].x,point[1].y);
1937                 else
1938                   (void) FormatLocaleString(message,MagickPathExtent,
1939                     "  %g %g %g %g %g %g c\n",last[2].x,
1940                     last[2].y,point[0].x,point[0].y,point[1].x,point[1].y);
1941             for (i=0; i < 3; i++)
1942               last[i]=point[i];
1943           }
1944         (void) ConcatenateString(&path,message);
1945         in_subpath=MagickTrue;
1946         knot_count--;
1947         /*
1948           Close the subpath if there are no more knots.
1949         */
1950         if (knot_count == 0)
1951           {
1952             /*
1953               Same special handling as above except we compare to the
1954               first point in the path and close the path.
1955             */
1956             if ((last[1].x == last[2].x) && (last[1].y == last[2].y) &&
1957                 (first[0].x == first[1].x) && (first[0].y == first[1].y))
1958               (void) FormatLocaleString(message,MagickPathExtent,
1959                 "  %g %g l z\n",first[1].x,first[1].y);
1960             else
1961               if ((last[1].x == last[2].x) && (last[1].y == last[2].y))
1962                 (void) FormatLocaleString(message,MagickPathExtent,
1963                   "  %g %g %g %g v z\n",first[0].x,first[0].y,
1964                   first[1].x,first[1].y);
1965               else
1966                 if ((first[0].x == first[1].x) && (first[0].y == first[1].y))
1967                   (void) FormatLocaleString(message,MagickPathExtent,
1968                     "  %g %g %g %g y z\n",last[2].x,last[2].y,
1969                     first[1].x,first[1].y);
1970                 else
1971                   (void) FormatLocaleString(message,MagickPathExtent,
1972                     "  %g %g %g %g %g %g c z\n",last[2].x,
1973                     last[2].y,first[0].x,first[0].y,first[1].x,first[1].y);
1974             (void) ConcatenateString(&path,message);
1975             in_subpath=MagickFalse;
1976           }
1977         break;
1978       }
1979       case 6:
1980       case 7:
1981       case 8:
1982       default:
1983       {
1984         blob+=24;
1985         length-=MagickMin(24,(ssize_t) length);
1986         break;
1987       }
1988     }
1989   }
1990   /*
1991     Returns an empty PS path if the path has no knots.
1992   */
1993   (void) FormatLocaleString(message,MagickPathExtent,"  eoclip\n");
1994   (void) ConcatenateString(&path,message);
1995   (void) FormatLocaleString(message,MagickPathExtent,"} bind def");
1996   (void) ConcatenateString(&path,message);
1997   message=DestroyString(message);
1998   return(path);
1999 }
2000
2001 static char *TraceSVGClippath(const unsigned char *blob,size_t length,
2002   const size_t columns,const size_t rows)
2003 {
2004   char
2005     *path,
2006     *message;
2007
2008   MagickBooleanType
2009     in_subpath;
2010
2011   PointInfo
2012     first[3],
2013     last[3],
2014     point[3];
2015
2016   register ssize_t
2017     i;
2018
2019   ssize_t
2020     knot_count,
2021     selector,
2022     x,
2023     y;
2024
2025   path=AcquireString((char *) NULL);
2026   if (path == (char *) NULL)
2027     return((char *) NULL);
2028   message=AcquireString((char *) NULL);
2029   (void) FormatLocaleString(message,MagickPathExtent,(
2030     "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
2031     "<svg xmlns=\"http://www.w3.org/2000/svg\""
2032     " width=\"%.20g\" height=\"%.20g\">\n"
2033     "<g>\n"
2034     "<path fill-rule=\"evenodd\" style=\"fill:#00000000;stroke:#00000000;"
2035     "stroke-width:0;stroke-antialiasing:false\" d=\"\n"),(double) columns,
2036     (double) rows);
2037   (void) ConcatenateString(&path,message);
2038   (void) ResetMagickMemory(point,0,sizeof(point));
2039   (void) ResetMagickMemory(first,0,sizeof(first));
2040   (void) ResetMagickMemory(last,0,sizeof(last));
2041   knot_count=0;
2042   in_subpath=MagickFalse;
2043   while (length != 0)
2044   {
2045     selector=(ssize_t) ReadPropertyMSBShort(&blob,&length);
2046     switch (selector)
2047     {
2048       case 0:
2049       case 3:
2050       {
2051         if (knot_count != 0)
2052           {
2053             blob+=24;
2054             length-=MagickMin(24,(ssize_t) length);
2055             break;
2056           }
2057         /*
2058           Expected subpath length record.
2059         */
2060         knot_count=(ssize_t) ReadPropertyMSBShort(&blob,&length);
2061         blob+=22;
2062         length-=MagickMin(22,(ssize_t) length);
2063         break;
2064       }
2065       case 1:
2066       case 2:
2067       case 4:
2068       case 5:
2069       {
2070         if (knot_count == 0)
2071           {
2072             /*
2073               Unexpected subpath knot.
2074             */
2075             blob+=24;
2076             length-=MagickMin(24,(ssize_t) length);
2077             break;
2078           }
2079         /*
2080           Add sub-path knot
2081         */
2082         for (i=0; i < 3; i++)
2083         {
2084           unsigned int
2085             xx,
2086             yy;
2087
2088           yy=(unsigned int) ReadPropertyMSBLong(&blob,&length);
2089           xx=(unsigned int) ReadPropertyMSBLong(&blob,&length);
2090           x=(ssize_t) xx;
2091           if (xx > 2147483647)
2092             x=(ssize_t) xx-4294967295U-1;
2093           y=(ssize_t) yy;
2094           if (yy > 2147483647)
2095             y=(ssize_t) yy-4294967295U-1;
2096           point[i].x=(double) x*columns/4096/4096;
2097           point[i].y=(double) y*rows/4096/4096;
2098         }
2099         if (in_subpath == MagickFalse)
2100           {
2101             (void) FormatLocaleString(message,MagickPathExtent,"M %g %g\n",
2102               point[1].x,point[1].y);
2103             for (i=0; i < 3; i++)
2104             {
2105               first[i]=point[i];
2106               last[i]=point[i];
2107             }
2108           }
2109         else
2110           {
2111             /*
2112               Handle special cases when Bezier curves are used to describe
2113               corners and straight lines.
2114             */
2115             if ((last[1].x == last[2].x) && (last[1].y == last[2].y) &&
2116                 (point[0].x == point[1].x) && (point[0].y == point[1].y))
2117               (void) FormatLocaleString(message,MagickPathExtent,
2118                 "L %g %g\n",point[1].x,point[1].y);
2119             else
2120               (void) FormatLocaleString(message,MagickPathExtent,
2121                 "C %g %g %g %g %g %g\n",last[2].x,
2122                 last[2].y,point[0].x,point[0].y,point[1].x,point[1].y);
2123             for (i=0; i < 3; i++)
2124               last[i]=point[i];
2125           }
2126         (void) ConcatenateString(&path,message);
2127         in_subpath=MagickTrue;
2128         knot_count--;
2129         /*
2130           Close the subpath if there are no more knots.
2131         */
2132         if (knot_count == 0)
2133           {
2134            /*
2135               Same special handling as above except we compare to the
2136               first point in the path and close the path.
2137             */
2138             if ((last[1].x == last[2].x) && (last[1].y == last[2].y) &&
2139                 (first[0].x == first[1].x) && (first[0].y == first[1].y))
2140               (void) FormatLocaleString(message,MagickPathExtent,
2141                 "L %g %g Z\n",first[1].x,first[1].y);
2142             else
2143               (void) FormatLocaleString(message,MagickPathExtent,
2144                 "C %g %g %g %g %g %g Z\n",last[2].x,
2145                 last[2].y,first[0].x,first[0].y,first[1].x,first[1].y);
2146             (void) ConcatenateString(&path,message);
2147             in_subpath=MagickFalse;
2148           }
2149         break;
2150       }
2151       case 6:
2152       case 7:
2153       case 8:
2154       default:
2155       {
2156         blob+=24;
2157         length-=MagickMin(24,(ssize_t) length);
2158         break;
2159       }
2160     }
2161   }
2162   /*
2163     Return an empty SVG image if the path does not have knots.
2164   */
2165   (void) ConcatenateString(&path,"\"/>\n</g>\n</svg>\n");
2166   message=DestroyString(message);
2167   return(path);
2168 }
2169
2170 MagickExport const char *GetImageProperty(const Image *image,
2171   const char *property,ExceptionInfo *exception)
2172 {
2173   register const char
2174     *p;
2175
2176   assert(image != (Image *) NULL);
2177   assert(image->signature == MagickCoreSignature);
2178   if (image->debug != MagickFalse)
2179     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2180   p=(const char *) NULL;
2181   if (image->properties != (void *) NULL)
2182     {
2183       if (property == (const char *) NULL)
2184         {
2185           ResetSplayTreeIterator((SplayTreeInfo *) image->properties);
2186           p=(const char *) GetNextValueInSplayTree((SplayTreeInfo *)
2187             image->properties);
2188           return(p);
2189         }
2190         p=(const char *) GetValueFromSplayTree((SplayTreeInfo *)
2191           image->properties,property);
2192         if (p != (const char *) NULL)
2193           return(p);
2194     }
2195   if ((property == (const char *) NULL) ||
2196       (strchr(property,':') == (char *) NULL))
2197     return(p);
2198   switch (*property)
2199   {
2200     case '8':
2201     {
2202       if (LocaleNCompare("8bim:",property,5) == 0)
2203         {
2204           (void) Get8BIMProperty(image,property,exception);
2205           break;
2206         }
2207       break;
2208     }
2209     case 'E':
2210     case 'e':
2211     {
2212       if (LocaleNCompare("exif:",property,5) == 0)
2213         {
2214           (void) GetEXIFProperty(image,property,exception);
2215           break;
2216         }
2217       break;
2218     }
2219     case 'I':
2220     case 'i':
2221     {
2222       if ((LocaleNCompare("icc:",property,4) == 0) ||
2223           (LocaleNCompare("icm:",property,4) == 0))
2224         {
2225           (void) GetICCProperty(image,property,exception);
2226           break;
2227         }
2228       if (LocaleNCompare("iptc:",property,5) == 0)
2229         {
2230           (void) GetIPTCProperty(image,property,exception);
2231           break;
2232         }
2233       break;
2234     }
2235     case 'X':
2236     case 'x':
2237     {
2238       if (LocaleNCompare("xmp:",property,4) == 0)
2239         {
2240           (void) GetXMPProperty(image,property);
2241           break;
2242         }
2243       break;
2244     }
2245     default:
2246       break;
2247   }
2248   if (image->properties != (void *) NULL)
2249     {
2250       p=(const char *) GetValueFromSplayTree((SplayTreeInfo *)
2251         image->properties,property);
2252       return(p);
2253     }
2254   return((const char *) NULL);
2255 }
2256 \f
2257 /*
2258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2259 %                                                                             %
2260 %                                                                             %
2261 %                                                                             %
2262 +   G e t M a g i c k P r o p e r t y                                         %
2263 %                                                                             %
2264 %                                                                             %
2265 %                                                                             %
2266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2267 %
2268 %  GetMagickProperty() gets attributes or calculated values that is associated
2269 %  with a fixed known property name, or single letter property. It may be
2270 %  called if no image is defined (IMv7), in which case only global image_info
2271 %  values are available:
2272 %
2273 %    \n   newline
2274 %    \r   carriage return
2275 %    <    less-than character.
2276 %    >    greater-than character.
2277 %    &    ampersand character.
2278 %    %%   a percent sign
2279 %    %b   file size of image read in
2280 %    %c   comment meta-data property
2281 %    %d   directory component of path
2282 %    %e   filename extension or suffix
2283 %    %f   filename (including suffix)
2284 %    %g   layer canvas page geometry   (equivalent to "%Wx%H%X%Y")
2285 %    %h   current image height in pixels
2286 %    %i   image filename (note: becomes output filename for "info:")
2287 %    %k   CALCULATED: number of unique colors
2288 %    %l   label meta-data property
2289 %    %m   image file format (file magic)
2290 %    %n   number of images in current image sequence
2291 %    %o   output filename  (used for delegates)
2292 %    %p   index of image in current image list
2293 %    %q   quantum depth (compile-time constant)
2294 %    %r   image class and colorspace
2295 %    %s   scene number (from input unless re-assigned)
2296 %    %t   filename without directory or extension (suffix)
2297 %    %u   unique temporary filename (used for delegates)
2298 %    %w   current width in pixels
2299 %    %x   x resolution (density)
2300 %    %y   y resolution (density)
2301 %    %z   image depth (as read in unless modified, image save depth)
2302 %    %A   image transparency channel enabled (true/false)
2303 %    %C   image compression type
2304 %    %D   image GIF dispose method
2305 %    %G   original image size (%wx%h; before any resizes)
2306 %    %H   page (canvas) height
2307 %    %M   Magick filename (original file exactly as given,  including read mods)
2308 %    %O   page (canvas) offset ( = %X%Y )
2309 %    %P   page (canvas) size ( = %Wx%H )
2310 %    %Q   image compression quality ( 0 = default )
2311 %    %S   ?? scenes ??
2312 %    %T   image time delay (in centi-seconds)
2313 %    %U   image resolution units
2314 %    %W   page (canvas) width
2315 %    %X   page (canvas) x offset (including sign)
2316 %    %Y   page (canvas) y offset (including sign)
2317 %    %Z   unique filename (used for delegates)
2318 %    %@   CALCULATED: trim bounding box (without actually trimming)
2319 %    %#   CALCULATED: 'signature' hash of image values
2320 %
2321 %  This routine only handles specifically known properties.  It does not
2322 %  handle special prefixed properties, profiles, or expressions. Nor does
2323 %  it return any free-form property strings.
2324 %
2325 %  The returned string is stored in a structure somewhere, and should not be
2326 %  directly freed.  If the string was generated (common) the string will be
2327 %  stored as as either as artifact or option 'get-property'.  These may be
2328 %  deleted (cleaned up) when no longer required, but neither artifact or
2329 %  option is guranteed to exist.
2330 %
2331 %  The format of the GetMagickProperty method is:
2332 %
2333 %      const char *GetMagickProperty(ImageInfo *image_info,Image *image,
2334 %        const char *property,ExceptionInfo *exception)
2335 %
2336 %  A description of each parameter follows:
2337 %
2338 %    o image_info: the image info (optional)
2339 %
2340 %    o image: the image (optional)
2341 %
2342 %    o key: the key.
2343 %
2344 %    o exception: return any errors or warnings in this structure.
2345 %
2346 */
2347 static const char *GetMagickPropertyLetter(ImageInfo *image_info,
2348   Image *image,const char letter,ExceptionInfo *exception)
2349 {
2350 #define WarnNoImageReturn(format,arg) \
2351   if (image == (Image *) NULL ) { \
2352     (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning, \
2353       "NoImageForProperty",format,arg); \
2354     return((const char *) NULL); \
2355   }
2356 #define WarnNoImageInfoReturn(format,arg) \
2357   if (image_info == (ImageInfo *) NULL ) { \
2358     (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning, \
2359       "NoImageInfoForProperty",format,arg); \
2360     return((const char *) NULL); \
2361   }
2362
2363   char
2364     value[MagickPathExtent];  /* formated string to store as a returned artifact */
2365
2366   const char
2367     *string;     /* return a string already stored somewher */
2368
2369   if ((image != (Image *) NULL) && (image->debug != MagickFalse))
2370     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2371   else
2372     if ((image_info != (ImageInfo *) NULL) &&
2373         (image_info->debug != MagickFalse))
2374     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s","no-images");
2375   *value='\0';           /* formatted string */
2376   string=(char *) NULL;  /* constant string reference */
2377   /*
2378     Get properities that are directly defined by images.
2379   */
2380   switch (letter)
2381   {
2382     case 'b':  /* image size read in - in bytes */
2383     {
2384       WarnNoImageReturn("\"%%%c\"",letter);
2385       (void) FormatMagickSize(image->extent,MagickFalse,"B",MagickPathExtent,
2386         value);
2387       if (image->extent == 0)
2388         (void) FormatMagickSize(GetBlobSize(image),MagickFalse,"B",
2389           MagickPathExtent,value);
2390       break;
2391     }
2392     case 'c':  /* image comment property - empty string by default */
2393     {
2394       WarnNoImageReturn("\"%%%c\"",letter);
2395       string=GetImageProperty(image,"comment",exception);
2396       if ( string == (const char *) NULL )
2397         string="";
2398       break;
2399     }
2400     case 'd':  /* Directory component of filename */
2401     {
2402       WarnNoImageReturn("\"%%%c\"",letter);
2403       GetPathComponent(image->magick_filename,HeadPath,value);
2404       if (*value == '\0') string="";
2405       break;
2406     }
2407     case 'e': /* Filename extension (suffix) of image file */
2408     {
2409       WarnNoImageReturn("\"%%%c\"",letter);
2410       GetPathComponent(image->magick_filename,ExtensionPath,value);
2411       if (*value == '\0') string="";
2412       break;
2413     }
2414     case 'f': /* Filename without directory component */
2415     {
2416       WarnNoImageReturn("\"%%%c\"",letter);
2417       GetPathComponent(image->magick_filename,TailPath,value);
2418       if (*value == '\0') string="";
2419       break;
2420     }
2421     case 'g': /* Image geometry, canvas and offset  %Wx%H+%X+%Y */
2422     {
2423       WarnNoImageReturn("\"%%%c\"",letter);
2424       (void) FormatLocaleString(value,MagickPathExtent,
2425         "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
2426         image->page.height,(double) image->page.x,(double) image->page.y);
2427       break;
2428     }
2429     case 'h': /* Image height (current) */
2430     {
2431       WarnNoImageReturn("\"%%%c\"",letter);
2432       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2433         (image->rows != 0 ? image->rows : image->magick_rows));
2434       break;
2435     }
2436     case 'i': /* Filename last used for an image (read or write) */
2437     {
2438       WarnNoImageReturn("\"%%%c\"",letter);
2439       string=image->filename;
2440       break;
2441     }
2442     case 'k': /* Number of unique colors  */
2443     {
2444       /*
2445         FUTURE: ensure this does not generate the formatted comment!
2446       */
2447       WarnNoImageReturn("\"%%%c\"",letter);
2448       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2449         GetNumberColors(image,(FILE *) NULL,exception));
2450       break;
2451     }
2452     case 'l': /* Image label property - empty string by default */
2453     {
2454       WarnNoImageReturn("\"%%%c\"",letter);
2455       string=GetImageProperty(image,"label",exception);
2456       if ( string == (const char *) NULL)
2457         string="";
2458       break;
2459     }
2460     case 'm': /* Image format (file magick) */
2461     {
2462       WarnNoImageReturn("\"%%%c\"",letter);
2463       string=image->magick;
2464       break;
2465     }
2466     case 'n': /* Number of images in the list.  */
2467     {
2468       if ( image != (Image *) NULL )
2469         (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2470           GetImageListLength(image));
2471       else
2472         string="0";    /* no images or scenes */
2473       break;
2474     }
2475     case 'o': /* Output Filename - for delegate use only */
2476       WarnNoImageInfoReturn("\"%%%c\"",letter);
2477       string=image_info->filename;
2478       break;
2479     case 'p': /* Image index in current image list */
2480     {
2481       WarnNoImageReturn("\"%%%c\"",letter);
2482       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2483         GetImageIndexInList(image));
2484       break;
2485     }
2486     case 'q': /* Quantum depth of image in memory */
2487     {
2488       WarnNoImageReturn("\"%%%c\"",letter);
2489       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2490         MAGICKCORE_QUANTUM_DEPTH);
2491       break;
2492     }
2493     case 'r': /* Image storage class, colorspace, and alpha enabled.  */
2494     {
2495       ColorspaceType
2496         colorspace;
2497
2498       WarnNoImageReturn("\"%%%c\"",letter);
2499       colorspace=image->colorspace;
2500       if (SetImageGray(image,exception) != MagickFalse)
2501         colorspace=GRAYColorspace;   /* FUTURE: this is IMv6 not IMv7 */
2502       (void) FormatLocaleString(value,MagickPathExtent,"%s %s %s",
2503         CommandOptionToMnemonic(MagickClassOptions,(ssize_t)
2504         image->storage_class),CommandOptionToMnemonic(MagickColorspaceOptions,
2505         (ssize_t) colorspace),image->alpha_trait != UndefinedPixelTrait ?
2506         "Alpha" : "");
2507       break;
2508     }
2509     case 's': /* Image scene number */
2510     {
2511 #if 0  /* this seems non-sensical -- simplifing */
2512       if (image_info->number_scenes != 0)
2513         (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2514           image_info->scene);
2515       else if (image != (Image *) NULL)
2516         (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2517           image->scene);
2518       else
2519           string="0";
2520 #else
2521       WarnNoImageReturn("\"%%%c\"",letter);
2522       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2523          image->scene);
2524 #endif
2525       break;
2526     }
2527     case 't': /* Base filename without directory or extention */
2528     {
2529       WarnNoImageReturn("\"%%%c\"",letter);
2530       GetPathComponent(image->magick_filename,BasePath,value);
2531       if (*value == '\0') string="";
2532       break;
2533     }
2534     case 'u': /* Unique filename */
2535     {
2536       WarnNoImageInfoReturn("\"%%%c\"",letter);
2537       string=image_info->unique;
2538       break;
2539     }
2540     case 'w': /* Image width (current) */
2541     {
2542       WarnNoImageReturn("\"%%%c\"",letter);
2543       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2544         (image->columns != 0 ? image->columns : image->magick_columns));
2545       break;
2546     }
2547     case 'x': /* Image horizontal resolution (with units) */
2548     {
2549       WarnNoImageReturn("\"%%%c\"",letter);
2550       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
2551         fabs(image->resolution.x) > MagickEpsilon ? image->resolution.x : 72.0);
2552       break;
2553     }
2554     case 'y': /* Image vertical resolution (with units) */
2555     {
2556       WarnNoImageReturn("\"%%%c\"",letter);
2557       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
2558         fabs(image->resolution.y) > MagickEpsilon ? image->resolution.y : 72.0);
2559       break;
2560     }
2561     case 'z': /* Image depth as read in */
2562     {
2563       WarnNoImageReturn("\"%%%c\"",letter);
2564       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
2565         (double) image->depth);
2566       break;
2567     }
2568     case 'A': /* Image alpha channel  */
2569     {
2570       WarnNoImageReturn("\"%%%c\"",letter);
2571       string=CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
2572         image->alpha_trait);
2573       break;
2574     }
2575     case 'C': /* Image compression method.  */
2576     {
2577       WarnNoImageReturn("\"%%%c\"",letter);
2578       string=CommandOptionToMnemonic(MagickCompressOptions,
2579         (ssize_t) image->compression);
2580       break;
2581     }
2582     case 'D': /* Image dispose method.  */
2583     {
2584       WarnNoImageReturn("\"%%%c\"",letter);
2585       string=CommandOptionToMnemonic(MagickDisposeOptions,
2586         (ssize_t) image->dispose);
2587       break;
2588     }
2589     case 'G': /* Image size as geometry = "%wx%h" */
2590     {
2591       WarnNoImageReturn("\"%%%c\"",letter);
2592       (void) FormatLocaleString(value,MagickPathExtent,"%.20gx%.20g",
2593         (double)image->magick_columns,(double) image->magick_rows);
2594       break;
2595     }
2596     case 'H': /* layer canvas height */
2597     {
2598       WarnNoImageReturn("\"%%%c\"",letter);
2599       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
2600         (double) image->page.height);
2601       break;
2602     }
2603     case 'M': /* Magick filename - filename given incl. coder & read mods */
2604     {
2605       WarnNoImageReturn("\"%%%c\"",letter);
2606       string=image->magick_filename;
2607       break;
2608     }
2609     case 'O': /* layer canvas offset with sign = "+%X+%Y" */
2610     {
2611       WarnNoImageReturn("\"%%%c\"",letter);
2612       (void) FormatLocaleString(value,MagickPathExtent,"%+ld%+ld",(long)
2613         image->page.x,(long) image->page.y);
2614       break;
2615     }
2616     case 'P': /* layer canvas page size = "%Wx%H" */
2617     {
2618       WarnNoImageReturn("\"%%%c\"",letter);
2619       (void) FormatLocaleString(value,MagickPathExtent,"%.20gx%.20g",
2620         (double) image->page.width,(double) image->page.height);
2621       break;
2622     }
2623     case 'Q': /* image compression quality */
2624     {
2625       WarnNoImageReturn("\"%%%c\"",letter);
2626       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2627         (image->quality == 0 ? 92 : image->quality));
2628       break;
2629     }
2630     case 'S': /* Number of scenes in image list.  */
2631     {
2632       WarnNoImageInfoReturn("\"%%%c\"",letter);
2633 #if 0 /* What is this number? -- it makes no sense - simplifing */
2634       if (image_info->number_scenes == 0)
2635          string="2147483647";
2636       else if ( image != (Image *) NULL )
2637         (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2638                 image_info->scene+image_info->number_scenes);
2639       else
2640         string="0";
2641 #else
2642       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2643         (image_info->number_scenes == 0 ? 2147483647 :
2644          image_info->number_scenes));
2645 #endif
2646       break;
2647     }
2648     case 'T': /* image time delay for animations */
2649     {
2650       WarnNoImageReturn("\"%%%c\"",letter);
2651       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2652         image->delay);
2653       break;
2654     }
2655     case 'U': /* Image resolution units. */
2656     {
2657       WarnNoImageReturn("\"%%%c\"",letter);
2658       string=CommandOptionToMnemonic(MagickResolutionOptions,
2659         (ssize_t) image->units);
2660       break;
2661     }
2662     case 'W': /* layer canvas width */
2663     {
2664       WarnNoImageReturn("\"%%%c\"",letter);
2665       (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2666         image->page.width);
2667       break;
2668     }
2669     case 'X': /* layer canvas X offset */
2670     {
2671       WarnNoImageReturn("\"%%%c\"",letter);
2672       (void) FormatLocaleString(value,MagickPathExtent,"%+.20g",(double)
2673         image->page.x);
2674       break;
2675     }
2676     case 'Y': /* layer canvas Y offset */
2677     {
2678       WarnNoImageReturn("\"%%%c\"",letter);
2679       (void) FormatLocaleString(value,MagickPathExtent,"%+.20g",(double)
2680         image->page.y);
2681       break;
2682     }
2683     case '%': /* percent escaped */
2684     {
2685       string="%";
2686       break;
2687     }
2688     case '@': /* Trim bounding box, without actually Trimming! */
2689     {
2690       RectangleInfo
2691         page;
2692
2693       WarnNoImageReturn("\"%%%c\"",letter);
2694       page=GetImageBoundingBox(image,exception);
2695       (void) FormatLocaleString(value,MagickPathExtent,
2696         "%.20gx%.20g%+.20g%+.20g",(double) page.width,(double) page.height,
2697         (double) page.x,(double)page.y);
2698       break;
2699     }
2700     case '#':
2701     {
2702       /*
2703         Image signature.
2704       */
2705       WarnNoImageReturn("\"%%%c\"",letter);
2706       (void) SignatureImage(image,exception);
2707       string=GetImageProperty(image,"signature",exception);
2708       break;
2709     }
2710   }
2711   if (string != (char *) NULL)
2712     return(string);
2713   if (*value != '\0')
2714     {
2715       /*
2716         Create a cloned copy of result.
2717       */
2718       if (image != (Image *) NULL)
2719         {
2720           (void) SetImageArtifact(image,"get-property",value);
2721           return(GetImageArtifact(image,"get-property"));
2722         }
2723       else
2724         {
2725           (void) SetImageOption(image_info,"get-property",value);
2726           return(GetImageOption(image_info,"get-property"));
2727         }
2728     }
2729   return((char *) NULL);
2730 }
2731
2732 MagickExport const char *GetMagickProperty(ImageInfo *image_info,
2733   Image *image,const char *property,ExceptionInfo *exception)
2734 {
2735   char
2736     value[MagickPathExtent];
2737
2738   const char
2739     *string;
2740
2741   assert(property[0] != '\0');
2742   assert(image != (Image *) NULL || image_info != (ImageInfo *) NULL );
2743
2744   if (property[1] == '\0')  /* single letter property request */
2745     return(GetMagickPropertyLetter(image_info,image,*property,exception));
2746
2747   if (image != (Image *) NULL && image->debug != MagickFalse)
2748     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2749   else if( image_info != (ImageInfo *) NULL && image_info->debug != MagickFalse)
2750     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s","no-images");
2751
2752   *value='\0';           /* formated string */
2753   string=(char *) NULL;  /* constant string reference */
2754   switch (*property)
2755   {
2756     case 'b':
2757     {
2758       if (LocaleCompare("basename",property) == 0)
2759         {
2760           WarnNoImageReturn("\"%%[%s]\"",property);
2761           GetPathComponent(image->magick_filename,BasePath,value);
2762           if (*value == '\0') string="";
2763           break;
2764         }
2765       if (LocaleCompare("bit-depth",property) == 0)
2766         {
2767           (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2768             GetImageDepth(image, exception));
2769           break;
2770         }
2771       break;
2772     }
2773     case 'c':
2774     {
2775       if (LocaleCompare("channels",property) == 0)
2776         {
2777           WarnNoImageReturn("\"%%[%s]\"",property);
2778           /* FUTURE: return actual image channels */
2779           (void) FormatLocaleString(value,MagickPathExtent,"%s",
2780             CommandOptionToMnemonic(MagickColorspaceOptions,(ssize_t)
2781             image->colorspace));
2782           LocaleLower(value);
2783           if( image->alpha_trait != UndefinedPixelTrait )
2784             (void) ConcatenateMagickString(value,"a",MagickPathExtent);
2785           break;
2786         }
2787       if (LocaleCompare("colorspace",property) == 0)
2788         {
2789           WarnNoImageReturn("\"%%[%s]\"",property);
2790           /* FUTURE: return actual colorspace - no 'gray' stuff */
2791           string=CommandOptionToMnemonic(MagickColorspaceOptions,(ssize_t)
2792             image->colorspace);
2793           break;
2794         }
2795       if (LocaleCompare("copyright",property) == 0)
2796         {
2797           (void) CopyMagickString(value,GetMagickCopyright(),MagickPathExtent);
2798           break;
2799         }
2800       break;
2801     }
2802     case 'd':
2803     {
2804       if (LocaleCompare("depth",property) == 0)
2805         {
2806           WarnNoImageReturn("\"%%[%s]\"",property);
2807           (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
2808             image->depth);
2809           break;
2810         }
2811       if (LocaleCompare("directory",property) == 0)
2812         {
2813           WarnNoImageReturn("\"%%[%s]\"",property);
2814           GetPathComponent(image->magick_filename,HeadPath,value);
2815           if (*value == '\0') string="";
2816           break;
2817         }
2818       break;
2819     }
2820     case 'e':
2821     {
2822       if (LocaleCompare("entropy",property) == 0)
2823         {
2824           double
2825             entropy;
2826
2827           WarnNoImageReturn("\"%%[%s]\"",property);
2828           (void) GetImageEntropy(image,&entropy,exception);
2829           (void) FormatLocaleString(value,MagickPathExtent,"%.*g",
2830             GetMagickPrecision(),entropy);
2831           break;
2832         }
2833       if (LocaleCompare("extension",property) == 0)
2834         {
2835           WarnNoImageReturn("\"%%[%s]\"",property);
2836           GetPathComponent(image->magick_filename,ExtensionPath,value);
2837           if (*value == '\0') string="";
2838           break;
2839         }
2840       break;
2841     }
2842     case 'g':
2843     {
2844       if (LocaleCompare("gamma",property) == 0)
2845         {
2846           WarnNoImageReturn("\"%%[%s]\"",property);
2847           (void) FormatLocaleString(value,MagickPathExtent,"%.*g",
2848             GetMagickPrecision(),image->gamma);
2849           break;
2850         }
2851       break;
2852     }
2853     case 'h':
2854     {
2855       if (LocaleCompare("height",property) == 0)
2856         {
2857           WarnNoImageReturn("\"%%[%s]\"",property);
2858           (void) FormatLocaleString(value,MagickPathExtent,"%.20g",
2859             image->magick_rows != 0 ? (double) image->magick_rows : 256.0);
2860           break;
2861         }
2862       break;
2863     }
2864     case 'i':
2865     {
2866       if (LocaleCompare("input",property) == 0)
2867         {
2868           WarnNoImageReturn("\"%%[%s]\"",property);
2869           string=image->filename;
2870           break;
2871         }
2872       break;
2873     }
2874     case 'k':
2875     {
2876       if (LocaleCompare("kurtosis",property) == 0)
2877         {
2878           double
2879             kurtosis,
2880             skewness;
2881
2882           WarnNoImageReturn("\"%%[%s]\"",property);
2883           (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
2884           (void) FormatLocaleString(value,MagickPathExtent,"%.*g",
2885             GetMagickPrecision(),kurtosis);
2886           break;
2887         }
2888       break;
2889     }
2890     case 'm':
2891     {
2892       if (LocaleCompare("magick",property) == 0)
2893         {
2894           WarnNoImageReturn("\"%%[%s]\"",property);
2895           string=image->magick;
2896           break;
2897         }
2898       if ((LocaleCompare("maxima",property) == 0) ||
2899           (LocaleCompare("max",property) == 0))
2900         {
2901           double
2902             maximum,
2903             minimum;
2904
2905           WarnNoImageReturn("\"%%[%s]\"",property);
2906           (void) GetImageRange(image,&minimum,&maximum,exception);
2907           (void) FormatLocaleString(value,MagickPathExtent,"%.*g",
2908             GetMagickPrecision(),maximum);
2909           break;
2910         }
2911       if (LocaleCompare("mean",property) == 0)
2912         {
2913           double
2914             mean,
2915             standard_deviation;
2916
2917           WarnNoImageReturn("\"%%[%s]\"",property);
2918           (void) GetImageMean(image,&mean,&standard_deviation,exception);
2919           (void) FormatLocaleString(value,MagickPathExtent,"%.*g",
2920             GetMagickPrecision(),mean);
2921           break;
2922         }
2923       if ((LocaleCompare("minima",property) == 0) ||
2924           (LocaleCompare("min",property) == 0))
2925         {
2926           double
2927             maximum,
2928             minimum;
2929
2930           WarnNoImageReturn("\"%%[%s]\"",property);
2931           (void) GetImageRange(image,&minimum,&maximum,exception);
2932           (void) FormatLocaleString(value,MagickPathExtent,"%.*g",
2933             GetMagickPrecision(),minimum);
2934           break;
2935         }
2936       break;
2937     }
2938     case 'o':
2939     {
2940       if (LocaleCompare("opaque",property) == 0)
2941         {
2942           WarnNoImageReturn("\"%%[%s]\"",property);
2943           string=CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t)
2944             IsImageOpaque(image,exception));
2945           break;
2946         }
2947       if (LocaleCompare("orientation",property) == 0)
2948         {
2949           WarnNoImageReturn("\"%%[%s]\"",property);
2950           string=CommandOptionToMnemonic(MagickOrientationOptions,(ssize_t)
2951             image->orientation);
2952           break;
2953         }
2954       if (LocaleCompare("output",property) == 0)
2955         {
2956           WarnNoImageInfoReturn("\"%%[%s]\"",property);
2957           (void) CopyMagickString(value,image_info->filename,MagickPathExtent);
2958           break;
2959         }
2960      break;
2961     }
2962     case 'p':
2963     {
2964 #if defined(MAGICKCORE_LCMS_DELEGATE)
2965       if (LocaleCompare("profile:icc",property) == 0 ||
2966           LocaleCompare("profile:icm",property) == 0)
2967         {
2968 #if !defined(LCMS_VERSION) || (LCMS_VERSION < 2000)
2969 #define cmsUInt32Number  DWORD
2970 #endif
2971
2972           const StringInfo
2973             *profile;
2974
2975           cmsHPROFILE
2976             icc_profile;
2977
2978           profile=GetImageProfile(image,property+8);
2979           if (profile == (StringInfo *) NULL)
2980             break;
2981           icc_profile=cmsOpenProfileFromMem(GetStringInfoDatum(profile),
2982             (cmsUInt32Number) GetStringInfoLength(profile));
2983           if (icc_profile != (cmsHPROFILE *) NULL)
2984             {
2985 #if defined(LCMS_VERSION) && (LCMS_VERSION < 2000)
2986               string=cmsTakeProductName(icc_profile);
2987 #else
2988               (void) cmsGetProfileInfoASCII(icc_profile,cmsInfoDescription,
2989                 "en","US",value,MagickPathExtent);
2990 #endif
2991               (void) cmsCloseProfile(icc_profile);
2992             }
2993       }
2994 #endif
2995       if (LocaleCompare("profiles",property) == 0)
2996         {
2997           const char
2998             *name;
2999
3000           ResetImageProfileIterator(image);
3001           name=GetNextImageProfile(image);
3002           if (name != (char *) NULL)
3003             {
3004               (void) CopyMagickString(value,name,MagickPathExtent);
3005               name=GetNextImageProfile(image);
3006               while (name != (char *) NULL)
3007               {
3008                 ConcatenateMagickString(value,",",MagickPathExtent);
3009                 ConcatenateMagickString(value,name,MagickPathExtent);
3010                 name=GetNextImageProfile(image);
3011               }
3012             }
3013           break;
3014         }
3015       break;
3016     }
3017     case 'r':
3018     {
3019       if (LocaleCompare("resolution.x",property) == 0)
3020         {
3021           WarnNoImageReturn("\"%%[%s]\"",property);
3022           (void) FormatLocaleString(value,MagickPathExtent,"%g",
3023             image->resolution.x);
3024           break;
3025         }
3026       if (LocaleCompare("resolution.y",property) == 0)
3027         {
3028           WarnNoImageReturn("\"%%[%s]\"",property);
3029           (void) FormatLocaleString(value,MagickPathExtent,"%g",
3030             image->resolution.y);
3031           break;
3032         }
3033       break;
3034     }
3035     case 's':
3036     {
3037       if (LocaleCompare("scene",property) == 0)
3038         {
3039           WarnNoImageInfoReturn("\"%%[%s]\"",property);
3040           if (image_info->number_scenes != 0)
3041             (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
3042               image_info->scene);
3043           else {
3044             WarnNoImageReturn("\"%%[%s]\"",property);
3045             (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
3046               image->scene);
3047           }
3048           break;
3049         }
3050       if (LocaleCompare("scenes",property) == 0)
3051         {
3052           /* FUTURE: equivelent to %n? */
3053           WarnNoImageReturn("\"%%[%s]\"",property);
3054           (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
3055             GetImageListLength(image));
3056           break;
3057         }
3058       if (LocaleCompare("size",property) == 0)
3059         {
3060           WarnNoImageReturn("\"%%[%s]\"",property);
3061           (void) FormatMagickSize(GetBlobSize(image),MagickFalse,"B",
3062             MagickPathExtent,value);
3063           break;
3064         }
3065       if (LocaleCompare("skewness",property) == 0)
3066         {
3067           double
3068             kurtosis,
3069             skewness;
3070
3071           WarnNoImageReturn("\"%%[%s]\"",property);
3072           (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
3073           (void) FormatLocaleString(value,MagickPathExtent,"%.*g",
3074             GetMagickPrecision(),skewness);
3075           break;
3076         }
3077       if (LocaleCompare("standard-deviation",property) == 0)
3078         {
3079           double
3080             mean,
3081             standard_deviation;
3082
3083           WarnNoImageReturn("\"%%[%s]\"",property);
3084           (void) GetImageMean(image,&mean,&standard_deviation,exception);
3085           (void) FormatLocaleString(value,MagickPathExtent,"%.*g",
3086             GetMagickPrecision(),standard_deviation);
3087           break;
3088         }
3089        break;
3090     }
3091     case 't':
3092     {
3093       if (LocaleCompare("type",property) == 0)
3094         {
3095           WarnNoImageReturn("\"%%[%s]\"",property);
3096           string=CommandOptionToMnemonic(MagickTypeOptions,(ssize_t)
3097             IdentifyImageType(image,exception));
3098           break;
3099         }
3100        break;
3101     }
3102     case 'u':
3103     {
3104       if (LocaleCompare("unique",property) == 0)
3105         {
3106           WarnNoImageInfoReturn("\"%%[%s]\"",property);
3107           string=image_info->unique;
3108           break;
3109         }
3110       if (LocaleCompare("units",property) == 0)
3111         {
3112           WarnNoImageReturn("\"%%[%s]\"",property);
3113           string=CommandOptionToMnemonic(MagickResolutionOptions,(ssize_t)
3114             image->units);
3115           break;
3116         }
3117       if (LocaleCompare("copyright",property) == 0)
3118       break;
3119     }
3120     case 'v':
3121     {
3122       if (LocaleCompare("version",property) == 0)
3123         {
3124           string=GetMagickVersion((size_t *) NULL);
3125           break;
3126         }
3127       break;
3128     }
3129     case 'w':
3130     {
3131       if (LocaleCompare("width",property) == 0)
3132         {
3133           WarnNoImageReturn("\"%%[%s]\"",property);
3134           (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
3135             (image->magick_columns != 0 ? image->magick_columns : 256));
3136           break;
3137         }
3138       break;
3139     }
3140   }
3141   if (string != (char *) NULL)
3142     return(string);
3143   if (*value != '\0')
3144   {
3145     /* create a cloned copy of result, that will get cleaned up, eventually */
3146     if (image != (Image *) NULL)
3147       {
3148         (void) SetImageArtifact(image,"get-property",value);
3149         return(GetImageArtifact(image,"get-property"));
3150       }
3151     else
3152       {
3153         (void) SetImageOption(image_info,"get-property",value);
3154         return(GetImageOption(image_info,"get-property"));
3155       }
3156   }
3157   return((char *) NULL);
3158 }
3159 #undef WarnNoImageReturn
3160 \f
3161 /*
3162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3163 %                                                                             %
3164 %                                                                             %
3165 %                                                                             %
3166 %   G e t N e x t I m a g e P r o p e r t y                                   %
3167 %                                                                             %
3168 %                                                                             %
3169 %                                                                             %
3170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3171 %
3172 %  GetNextImageProperty() gets the next free-form string property name.
3173 %
3174 %  The format of the GetNextImageProperty method is:
3175 %
3176 %      char *GetNextImageProperty(const Image *image)
3177 %
3178 %  A description of each parameter follows:
3179 %
3180 %    o image: the image.
3181 %
3182 */
3183 MagickExport const char *GetNextImageProperty(const Image *image)
3184 {
3185   assert(image != (Image *) NULL);
3186   assert(image->signature == MagickCoreSignature);
3187   if (image->debug != MagickFalse)
3188     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3189       image->filename);
3190   if (image->properties == (void *) NULL)
3191     return((const char *) NULL);
3192   return((const char *) GetNextKeyInSplayTree(
3193     (SplayTreeInfo *) image->properties));
3194 }
3195 \f
3196 /*
3197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3198 %                                                                             %
3199 %                                                                             %
3200 %                                                                             %
3201 %   I n t e r p r e t I m a g e P r o p e r t i e s                           %
3202 %                                                                             %
3203 %                                                                             %
3204 %                                                                             %
3205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3206 %
3207 %  InterpretImageProperties() replaces any embedded formatting characters with
3208 %  the appropriate image property and returns the interpreted text.
3209 %
3210 %  This searches for and replaces
3211 %     \n \r \%          replaced by newline, return, and percent resp.
3212 %     &lt; &gt; &amp;   replaced by '<', '>', '&' resp.
3213 %     %%                replaced by percent
3214 %
3215 %     %x %[x]       where 'x' is a single letter properity, case sensitive).
3216 %     %[type:name]  where 'type' a is special and known prefix.
3217 %     %[name]       where 'name' is a specifically known attribute, calculated
3218 %                   value, or a per-image property string name, or a per-image
3219 %                   'artifact' (as generated from a global option).
3220 %                   It may contain ':' as long as the prefix is not special.
3221 %
3222 %  Single letter % substitutions will only happen if the character before the
3223 %  percent is NOT a number. But braced substitutions will always be performed.
3224 %  This prevents the typical usage of percent in a interpreted geometry
3225 %  argument from being substituted when the percent is a geometry flag.
3226 %
3227 %  If 'glob-expresions' ('*' or '?' characters) is used for 'name' it may be
3228 %  used as a search pattern to print multiple lines of "name=value\n" pairs of
3229 %  the associacted set of properties.
3230 %
3231 %  The returned string must be freed using DestoryString() by the caller.
3232 %
3233 %  The format of the InterpretImageProperties method is:
3234 %
3235 %      char *InterpretImageProperties(ImageInfo *image_info,
3236 %        Image *image,const char *embed_text,ExceptionInfo *exception)
3237 %
3238 %  A description of each parameter follows:
3239 %
3240 %    o image_info: the image info. (required)
3241 %
3242 %    o image: the image. (optional)
3243 %
3244 %    o embed_text: the address of a character string containing the embedded
3245 %      formatting characters.
3246 %
3247 %    o exception: return any errors or warnings in this structure.
3248 %
3249 */
3250 MagickExport char *InterpretImageProperties(ImageInfo *image_info,
3251   Image *image,const char *embed_text,ExceptionInfo *exception)
3252 {
3253 #define ExtendInterpretText(string_length) \
3254 DisableMSCWarning(4127) \
3255 { \
3256   size_t length=(string_length); \
3257   if ((size_t) (q-interpret_text+length+1) >= extent) \
3258     { \
3259       extent+=length; \
3260       interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
3261         MaxTextExtent,sizeof(*interpret_text)); \
3262       if (interpret_text == (char *) NULL) \
3263         return((char *) NULL); \
3264       q=interpret_text+strlen(interpret_text); \
3265    } \
3266 } \
3267 RestoreMSCWarning
3268
3269 #define AppendKeyValue2Text(key,value)\
3270 DisableMSCWarning(4127) \
3271 { \
3272   size_t length=strlen(key)+strlen(value)+2; \
3273   if ((size_t) (q-interpret_text+length+1) >= extent) \
3274     { \
3275       extent+=length; \
3276       interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
3277         MaxTextExtent,sizeof(*interpret_text)); \
3278       if (interpret_text == (char *) NULL) \
3279         return((char *) NULL); \
3280       q=interpret_text+strlen(interpret_text); \
3281      } \
3282    q+=FormatLocaleString(q,extent,"%s=%s\n",(key),(value)); \
3283 } \
3284 RestoreMSCWarning
3285
3286 #define AppendString2Text(string) \
3287 DisableMSCWarning(4127) \
3288 { \
3289   size_t length=strlen((string)); \
3290   if ((size_t) (q-interpret_text+length+1) >= extent) \
3291     { \
3292       extent+=length; \
3293       interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
3294         MaxTextExtent,sizeof(*interpret_text)); \
3295       if (interpret_text == (char *) NULL) \
3296         return((char *) NULL); \
3297       q=interpret_text+strlen(interpret_text); \
3298     } \
3299   (void) CopyMagickString(q,(string),extent); \
3300   q+=length; \
3301 } \
3302 RestoreMSCWarning
3303
3304   char
3305     *interpret_text;
3306
3307   register char
3308     *q;  /* current position in interpret_text */
3309
3310   register const char
3311     *p;  /* position in embed_text string being expanded */
3312
3313   size_t
3314     extent;  /* allocated length of interpret_text */
3315
3316   MagickBooleanType
3317     number;
3318
3319   assert(image == NULL || image->signature == MagickCoreSignature);
3320   assert(image_info == NULL || image_info->signature == MagickCoreSignature);
3321   if ((image != (Image *) NULL) && (image->debug != MagickFalse))
3322     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3323   else
3324    if ((image_info != (ImageInfo *) NULL) && (image_info->debug != MagickFalse))
3325      (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s","no-image");
3326   if (embed_text == (const char *) NULL)
3327     return(ConstantString(""));
3328   p=embed_text;
3329   while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
3330     p++;
3331   if (*p == '\0')
3332     return(ConstantString(""));
3333   if ((*p == '@') && (IsPathAccessible(p+1) != MagickFalse))
3334     {
3335       /*
3336         Handle a '@' replace string from file.
3337       */
3338       if (IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,p) == MagickFalse)
3339         {
3340           errno=EPERM;
3341           (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3342             "NotAuthorized","`%s'",p);
3343           return(ConstantString(""));
3344         }
3345       interpret_text=FileToString(p+1,~0UL,exception);
3346       if (interpret_text != (char *) NULL)
3347         return(interpret_text);
3348     }
3349   /*
3350     Translate any embedded format characters.
3351   */
3352   interpret_text=AcquireString(embed_text); /* new string with extra space */
3353   extent=MagickPathExtent;                     /* allocated space in string */
3354   number=MagickFalse;                       /* is last char a number? */
3355   for (q=interpret_text; *p!='\0';
3356     number=isdigit(*p) ? MagickTrue : MagickFalse,p++)
3357   {
3358     /*
3359       Look for the various escapes, (and handle other specials)
3360     */
3361     *q='\0';
3362     ExtendInterpretText(MagickPathExtent);
3363     switch (*p)
3364     {
3365       case '\\':
3366       {
3367         switch (*(p+1))
3368         {
3369           case '\0':
3370             continue;
3371           case 'r':  /* convert to RETURN */
3372           {
3373             *q++='\r';
3374             p++;
3375             continue;
3376           }
3377           case 'n':  /* convert to NEWLINE */
3378           {
3379             *q++='\n';
3380             p++;
3381             continue;
3382           }
3383           case '\n':  /* EOL removal UNIX,MacOSX */
3384           {
3385             p++;
3386             continue;
3387           }
3388           case '\r':  /* EOL removal DOS,Windows */
3389           {
3390             p++;
3391             if (*p == '\n') /* return-newline EOL */
3392               p++;
3393             continue;
3394           }
3395           default:
3396           {
3397             p++;
3398             *q++=(*p);
3399           }
3400         }
3401         continue;
3402       }
3403       case '&':
3404       {
3405         if (LocaleNCompare("&lt;",p,4) == 0)
3406           {
3407             *q++='<';
3408             p+=3;
3409           }
3410         else
3411           if (LocaleNCompare("&gt;",p,4) == 0)
3412             {
3413               *q++='>';
3414               p+=3;
3415             }
3416           else
3417             if (LocaleNCompare("&amp;",p,5) == 0)
3418               {
3419                 *q++='&';
3420                 p+=4;
3421               }
3422             else
3423               *q++=(*p);
3424         continue;
3425       }
3426       case '%':
3427         break;  /* continue to next set of handlers */
3428       default:
3429       {
3430         *q++=(*p);  /* any thing else is 'as normal' */
3431         continue;
3432       }
3433     }
3434     p++; /* advance beyond the percent */
3435     /*
3436       Doubled Percent - or percent at end of string.
3437     */
3438     if ((*p == '\0') || (*p == '\'') || (*p == '"'))
3439       p--;
3440     if (*p == '%')
3441       {
3442         *q++='%';
3443         continue;
3444       }
3445     /*
3446       Single letter escapes %c.
3447     */
3448     if (*p != '[')
3449       {
3450         const char
3451           *string;
3452
3453         if (number != MagickFalse)
3454           {
3455             /*
3456               But only if not preceeded by a number!
3457             */
3458             *q++='%'; /* do NOT substitute the percent */
3459             p--;      /* back up one */
3460             continue;
3461           }
3462         string=GetMagickPropertyLetter(image_info,image,*p, exception);
3463         if (string != (char *) NULL)
3464           {
3465             AppendString2Text(string);
3466             if (image != (Image *) NULL)
3467               (void) DeleteImageArtifact(image,"get-property");
3468             if (image_info != (ImageInfo *) NULL)
3469               (void) DeleteImageOption(image_info,"get-property");
3470             continue;
3471           }
3472         (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
3473           "UnknownImageProperty","\"%%%c\"",*p);
3474         continue;
3475       }
3476     {
3477       char
3478         pattern[2*MagickPathExtent];
3479
3480       const char
3481         *key,
3482         *string;
3483
3484       register ssize_t
3485         len;
3486
3487       ssize_t
3488         depth;
3489
3490       /*
3491         Braced Percent Escape %[...].
3492       */
3493       p++;  /* advance p to just inside the opening brace */
3494       depth=1;
3495       if (*p == ']')
3496         {
3497           (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
3498             "UnknownImageProperty","\"%%[]\"");
3499           break;
3500         }
3501       for (len=0; len<(MagickPathExtent-1L) && (*p != '\0');)
3502       {
3503         if ((*p == '\\') && (*(p+1) != '\0'))
3504           {
3505             /*
3506               Skip escaped braces within braced pattern.
3507             */
3508             pattern[len++]=(*p++);
3509             pattern[len++]=(*p++);
3510             continue;
3511           }
3512         if (*p == '[')
3513           depth++;
3514         if (*p == ']')
3515           depth--;
3516         if (depth <= 0)
3517           break;
3518         pattern[len++]=(*p++);
3519       }
3520       pattern[len]='\0';
3521       if (depth != 0)
3522         {
3523           /*
3524             Check for unmatched final ']' for "%[...]".
3525           */
3526           if (len >= 64)
3527             {
3528               pattern[61] = '.';  /* truncate string for error message */
3529               pattern[62] = '.';
3530               pattern[63] = '.';
3531               pattern[64] = '\0';
3532             }
3533           (void) ThrowMagickException(exception,GetMagickModule(),
3534             OptionError,"UnbalancedBraces","\"%%[%s\"",pattern);
3535           interpret_text=DestroyString(interpret_text);
3536           return((char *) NULL);
3537         }
3538       /*
3539         Special Lookup Prefixes %[prefix:...].
3540       */
3541       if (LocaleNCompare("fx:",pattern,3) == 0)
3542         {
3543           FxInfo
3544             *fx_info;
3545
3546           double
3547             value;
3548
3549           MagickBooleanType
3550             status;
3551
3552           /*
3553             FX - value calculator.
3554           */
3555           if (image == (Image *) NULL )
3556             {
3557               (void) ThrowMagickException(exception,GetMagickModule(),
3558                 OptionWarning,"NoImageForProperty","\"%%[%s]\"",pattern);
3559               continue; /* else no image to retrieve artifact */
3560             }
3561           fx_info=AcquireFxInfo(image,pattern+3,exception);
3562           status=FxEvaluateChannelExpression(fx_info,IntensityPixelChannel,0,0,
3563             &value,exception);
3564           fx_info=DestroyFxInfo(fx_info);
3565           if (status != MagickFalse)
3566             {
3567               char
3568                 result[MagickPathExtent];
3569
3570               (void) FormatLocaleString(result,MagickPathExtent,"%.*g",
3571                 GetMagickPrecision(),(double) value);
3572               AppendString2Text(result);
3573             }
3574           continue;
3575         }
3576       if (LocaleNCompare("pixel:",pattern,6) == 0)
3577         {
3578           FxInfo
3579             *fx_info;
3580
3581           double
3582             value;
3583
3584           MagickStatusType
3585             status;
3586
3587           PixelInfo
3588             pixel;
3589
3590           /*
3591             Pixel - color value calculator.
3592           */
3593           if (image == (Image *) NULL)
3594             {
3595               (void) ThrowMagickException(exception,GetMagickModule(),
3596                 OptionWarning,"NoImageForProperty","\"%%[%s]\"",pattern);
3597               continue; /* else no image to retrieve artifact */
3598             }
3599           GetPixelInfo(image,&pixel);
3600           fx_info=AcquireFxInfo(image,pattern+6,exception);
3601           status=FxEvaluateChannelExpression(fx_info,RedPixelChannel,0,0,
3602             &value,exception);
3603           pixel.red=(double) QuantumRange*value;
3604           status&=FxEvaluateChannelExpression(fx_info,GreenPixelChannel,0,0,
3605             &value,exception);
3606           pixel.green=(double) QuantumRange*value;
3607           status&=FxEvaluateChannelExpression(fx_info,BluePixelChannel,0,0,
3608             &value,exception);
3609           pixel.blue=(double) QuantumRange*value;
3610           if (image->colorspace == CMYKColorspace)
3611             {
3612               status&=FxEvaluateChannelExpression(fx_info,BlackPixelChannel,0,0,
3613                 &value,exception);
3614               pixel.black=(double) QuantumRange*value;
3615             }
3616           status&=FxEvaluateChannelExpression(fx_info,AlphaPixelChannel,0,0,
3617             &value,exception);
3618           pixel.alpha=(double) QuantumRange*value;
3619           fx_info=DestroyFxInfo(fx_info);
3620           if (status != MagickFalse)
3621             {
3622               char
3623                 name[MagickPathExtent];
3624
3625               (void) QueryColorname(image,&pixel,SVGCompliance,name,
3626                 exception);
3627               AppendString2Text(name);
3628             }
3629           continue;
3630         }
3631       if (LocaleNCompare("option:",pattern,7) == 0)
3632         {
3633           /*
3634             Option - direct global option lookup (with globbing).
3635           */
3636           if (image_info == (ImageInfo *) NULL )
3637             {
3638               (void) ThrowMagickException(exception,GetMagickModule(),
3639                 OptionWarning,"NoImageForProperty","\"%%[%s]\"",pattern);
3640               continue; /* else no image to retrieve artifact */
3641             }
3642           if (IsGlob(pattern+7) != MagickFalse)
3643             {
3644               ResetImageOptionIterator(image_info);
3645               while ((key=GetNextImageOption(image_info)) != (const char *) NULL)
3646                 if (GlobExpression(key,pattern+7,MagickTrue) != MagickFalse)
3647                   {
3648                     string=GetImageOption(image_info,key);
3649                     if (string != (const char *) NULL)
3650                       AppendKeyValue2Text(key,string);
3651                     /* else - assertion failure? key found but no string value! */
3652                   }
3653               continue;
3654             }
3655           string=GetImageOption(image_info,pattern+7);
3656           if (string == (char *) NULL)
3657             goto PropertyLookupFailure; /* no artifact of this specifc name */
3658           AppendString2Text(string);
3659           continue;
3660         }
3661       if (LocaleNCompare("artifact:",pattern,9) == 0)
3662         {
3663           /*
3664             Artifact - direct image artifact lookup (with glob).
3665           */
3666           if (image == (Image *) NULL)
3667             {
3668               (void) ThrowMagickException(exception,GetMagickModule(),
3669                 OptionWarning,"NoImageForProperty","\"%%[%s]\"",pattern);
3670               continue; /* else no image to retrieve artifact */
3671             }
3672           if (IsGlob(pattern+9) != MagickFalse)
3673             {
3674               ResetImageArtifactIterator(image);
3675               while ((key=GetNextImageArtifact(image)) != (const char *) NULL)
3676               if (GlobExpression(key,pattern+9,MagickTrue) != MagickFalse)
3677                 {
3678                   string=GetImageArtifact(image,key);
3679                   if (string != (const char *) NULL)
3680                     AppendKeyValue2Text(key,string);
3681                   /* else - assertion failure? key found but no string value! */
3682                 }
3683               continue;
3684             }
3685           string=GetImageArtifact(image,pattern+9);
3686           if (string == (char *) NULL)
3687             goto PropertyLookupFailure; /* no artifact of this specifc name */
3688           AppendString2Text(string);
3689           continue;
3690         }
3691       if (LocaleNCompare("property:",pattern,9) == 0)
3692         {
3693           /*
3694             Property - direct image property lookup (with glob).
3695           */
3696           if (image == (Image *) NULL)
3697             {
3698               (void) ThrowMagickException(exception,GetMagickModule(),
3699                 OptionWarning,"NoImageForProperty","\"%%[%s]\"",pattern);
3700               continue; /* else no image to retrieve artifact */
3701             }
3702           if (IsGlob(pattern+9) != MagickFalse)
3703             {
3704               ResetImagePropertyIterator(image);
3705               while ((key=GetNextImageProperty(image)) != (const char *) NULL)
3706                 if (GlobExpression(key,pattern,MagickTrue) != MagickFalse)
3707                   {
3708                     string=GetImageProperty(image,key,exception);
3709                     if (string != (const char *) NULL)
3710                       AppendKeyValue2Text(key,string);
3711                     /* else - assertion failure? */
3712                   }
3713               continue;
3714             }
3715           string=GetImageProperty(image,pattern+9,exception);
3716           if (string == (char *) NULL)
3717             goto PropertyLookupFailure; /* no artifact of this specifc name */
3718           AppendString2Text(string);
3719           continue;
3720         }
3721       if (image != (Image *) NULL)
3722         {
3723           /*
3724             Properties without special prefix.  This handles attributes,
3725             properties, and profiles such as %[exif:...].  Note the profile
3726             properties may also include a glob expansion pattern.
3727           */
3728           string=GetImageProperty(image,pattern,exception);
3729           if (string != (const char *) NULL)
3730             {
3731               AppendString2Text(string);
3732               if (image != (Image *) NULL)
3733                 (void)DeleteImageArtifact(image,"get-property");
3734               if (image_info != (ImageInfo *) NULL)
3735                 (void)DeleteImageOption(image_info,"get-property");
3736               continue;
3737             }
3738         }
3739       if (IsGlob(pattern) != MagickFalse)
3740         {
3741           /*
3742             Handle property 'glob' patterns such as:
3743             %[*] %[user:array_??] %[filename:e*]>
3744           */
3745           if (image == (Image *) NULL)
3746             continue; /* else no image to retrieve proprty - no list */
3747           ResetImagePropertyIterator(image);
3748           while ((key=GetNextImageProperty(image)) != (const char *) NULL)
3749             if (GlobExpression(key,pattern,MagickTrue) != MagickFalse)
3750               {
3751                 string=GetImageProperty(image,key,exception);
3752                 if (string != (const char *) NULL)
3753                   AppendKeyValue2Text(key,string);
3754                 /* else - assertion failure? */
3755               }
3756           continue;
3757         }
3758       /*
3759         Look for a known property or image attribute such as
3760         %[basename] %[denisty] %[delay].  Also handles a braced single
3761         letter: %[b] %[G] %[g].
3762       */
3763       string=GetMagickProperty(image_info,image,pattern,exception);
3764       if (string != (const char *) NULL)
3765         {
3766           AppendString2Text(string);
3767           continue;
3768         }
3769       /*
3770         Look for a per-image artifact. This includes option lookup
3771         (FUTURE: interpreted according to image).
3772       */
3773       if (image != (Image *) NULL)
3774         {
3775           string=GetImageArtifact(image,pattern);
3776           if (string != (char *) NULL)
3777             {
3778               AppendString2Text(string);
3779               continue;
3780             }
3781         }
3782       else
3783         if (image_info != (ImageInfo *) NULL)
3784           {
3785             /*
3786               No image, so direct 'option' lookup (no delayed percent escapes).
3787             */
3788             string=GetImageOption(image_info,pattern);
3789             if (string != (char *) NULL)
3790               {
3791                 AppendString2Text(string);
3792                 continue;
3793               }
3794           }
3795 PropertyLookupFailure:
3796       /*
3797         Failed to find any match anywhere!
3798       */
3799       if (len >= 64)
3800         {
3801           pattern[61] = '.';  /* truncate string for error message */
3802           pattern[62] = '.';
3803           pattern[63] = '.';
3804           pattern[64] = '\0';
3805         }
3806       (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
3807         "UnknownImageProperty","\"%%[%s]\"",pattern);
3808     }
3809   }
3810   *q='\0';
3811   return(interpret_text);
3812 }
3813 \f
3814 /*
3815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3816 %                                                                             %
3817 %                                                                             %
3818 %                                                                             %
3819 %   R e m o v e I m a g e P r o p e r t y                                     %
3820 %                                                                             %
3821 %                                                                             %
3822 %                                                                             %
3823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3824 %
3825 %  RemoveImageProperty() removes a property from the image and returns its
3826 %  value.
3827 %
3828 %  In this case the ConstantString() value returned should be freed by the
3829 %  caller when finished.
3830 %
3831 %  The format of the RemoveImageProperty method is:
3832 %
3833 %      char *RemoveImageProperty(Image *image,const char *property)
3834 %
3835 %  A description of each parameter follows:
3836 %
3837 %    o image: the image.
3838 %
3839 %    o property: the image property.
3840 %
3841 */
3842 MagickExport char *RemoveImageProperty(Image *image,
3843   const char *property)
3844 {
3845   char
3846     *value;
3847
3848   assert(image != (Image *) NULL);
3849   assert(image->signature == MagickCoreSignature);
3850   if (image->debug != MagickFalse)
3851     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3852       image->filename);
3853   if (image->properties == (void *) NULL)
3854     return((char *) NULL);
3855   value=(char *) RemoveNodeFromSplayTree((SplayTreeInfo *) image->properties,
3856     property);
3857   return(value);
3858 }
3859 \f
3860 /*
3861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3862 %                                                                             %
3863 %                                                                             %
3864 %                                                                             %
3865 %   R e s e t I m a g e P r o p e r t y I t e r a t o r                       %
3866 %                                                                             %
3867 %                                                                             %
3868 %                                                                             %
3869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3870 %
3871 %  ResetImagePropertyIterator() resets the image properties iterator.  Use it
3872 %  in conjunction with GetNextImageProperty() to iterate over all the values
3873 %  associated with an image property.
3874 %
3875 %  The format of the ResetImagePropertyIterator method is:
3876 %
3877 %      ResetImagePropertyIterator(Image *image)
3878 %
3879 %  A description of each parameter follows:
3880 %
3881 %    o image: the image.
3882 %
3883 */
3884 MagickExport void ResetImagePropertyIterator(const Image *image)
3885 {
3886   assert(image != (Image *) NULL);
3887   assert(image->signature == MagickCoreSignature);
3888   if (image->debug != MagickFalse)
3889     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3890       image->filename);
3891   if (image->properties == (void *) NULL)
3892     return;
3893   ResetSplayTreeIterator((SplayTreeInfo *) image->properties);
3894 }
3895 \f
3896 /*
3897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3898 %                                                                             %
3899 %                                                                             %
3900 %                                                                             %
3901 %   S e t I m a g e P r o p e r t y                                           %
3902 %                                                                             %
3903 %                                                                             %
3904 %                                                                             %
3905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3906 %
3907 %  SetImageProperty() saves the given string value either to specific known
3908 %  attribute or to a freeform property string.
3909 %
3910 %  Attempting to set a property that is normally calculated will produce
3911 %  an exception.
3912 %
3913 %  The format of the SetImageProperty method is:
3914 %
3915 %      MagickBooleanType SetImageProperty(Image *image,const char *property,
3916 %        const char *value,ExceptionInfo *exception)
3917 %
3918 %  A description of each parameter follows:
3919 %
3920 %    o image: the image.
3921 %
3922 %    o property: the image property.
3923 %
3924 %    o values: the image property values.
3925 %
3926 %    o exception: return any errors or warnings in this structure.
3927 %
3928 */
3929 MagickExport MagickBooleanType SetImageProperty(Image *image,
3930   const char *property,const char *value,ExceptionInfo *exception)
3931 {
3932   MagickBooleanType
3933     status;
3934
3935   MagickStatusType
3936     flags;
3937
3938   assert(image != (Image *) NULL);
3939   assert(image->signature == MagickCoreSignature);
3940   if (image->debug != MagickFalse)
3941     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3942   if (image->properties == (void *) NULL)
3943     image->properties=NewSplayTree(CompareSplayTreeString,
3944       RelinquishMagickMemory,RelinquishMagickMemory);  /* create splay-tree */
3945   if (value == (const char *) NULL)
3946     return(DeleteImageProperty(image,property));  /* delete if NULL */
3947   status=MagickTrue;
3948   if (strlen(property) <= 1)
3949     {
3950       /*
3951         Do not 'set' single letter properties - read only shorthand.
3952        */
3953       (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
3954         "SetReadOnlyProperty","`%s'",property);
3955       return(MagickFalse);
3956     }
3957
3958   /* FUTURE: binary chars or quotes in key should produce a error */
3959   /* Set attributes with known names or special prefixes
3960      return result is found, or break to set a free form properity
3961   */
3962   switch (*property)
3963   {
3964 #if 0  /* Percent escape's sets values with this prefix: for later use
3965           Throwing an exception causes this setting to fail */
3966     case '8':
3967     {
3968       if (LocaleNCompare("8bim:",property,5) == 0)
3969         {
3970           (void) ThrowMagickException(exception,GetMagickModule(),
3971                OptionError,"SetReadOnlyProperty","`%s'",property);
3972           return(MagickFalse);
3973         }
3974       break;
3975     }
3976 #endif
3977     case 'B':
3978     case 'b':
3979     {
3980       if (LocaleCompare("background",property) == 0)
3981         {
3982           (void) QueryColorCompliance(value,AllCompliance,
3983                &image->background_color,exception);
3984           /* check for FUTURE: value exception?? */
3985           /* also add user input to splay tree */
3986         }
3987       break; /* not an attribute, add as a property */
3988     }
3989     case 'C':
3990     case 'c':
3991     {
3992       if (LocaleCompare("channels",property) == 0)
3993         {
3994           (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
3995             "SetReadOnlyProperty","`%s'",property);
3996           return(MagickFalse);
3997         }
3998       if (LocaleCompare("colorspace",property) == 0)
3999         {
4000           ssize_t
4001             colorspace;
4002
4003           colorspace=ParseCommandOption(MagickColorspaceOptions,MagickFalse,
4004             value);
4005           if (colorspace < 0)
4006             return(MagickFalse); /* FUTURE: value exception?? */
4007           return(SetImageColorspace(image,(ColorspaceType) colorspace,exception));
4008         }
4009       if (LocaleCompare("compose",property) == 0)
4010         {
4011           ssize_t
4012             compose;
4013
4014           compose=ParseCommandOption(MagickComposeOptions,MagickFalse,value);
4015           if (compose < 0)
4016             return(MagickFalse); /* FUTURE: value exception?? */
4017           image->compose=(CompositeOperator) compose;
4018           return(MagickTrue);
4019         }
4020       if (LocaleCompare("compress",property) == 0)
4021         {
4022           ssize_t
4023             compression;
4024
4025           compression=ParseCommandOption(MagickCompressOptions,MagickFalse,
4026             value);
4027           if (compression < 0)
4028             return(MagickFalse); /* FUTURE: value exception?? */
4029           image->compression=(CompressionType) compression;
4030           return(MagickTrue);
4031         }
4032       break; /* not an attribute, add as a property */
4033     }
4034     case 'D':
4035     case 'd':
4036     {
4037       if (LocaleCompare("delay",property) == 0)
4038         {
4039           GeometryInfo
4040             geometry_info;
4041
4042           flags=ParseGeometry(value,&geometry_info);
4043           if ((flags & GreaterValue) != 0)
4044             {
4045               if (image->delay > (size_t) floor(geometry_info.rho+0.5))
4046                 image->delay=(size_t) floor(geometry_info.rho+0.5);
4047             }
4048           else
4049             if ((flags & LessValue) != 0)
4050               {
4051                 if (image->delay < (size_t) floor(geometry_info.rho+0.5))
4052                   image->delay=(ssize_t)
4053                     floor(geometry_info.sigma+0.5);
4054               }
4055             else
4056               image->delay=(size_t) floor(geometry_info.rho+0.5);
4057           if ((flags & SigmaValue) != 0)
4058             image->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
4059           return(MagickTrue);
4060         }
4061       if (LocaleCompare("delay_units",property) == 0)
4062         {
4063           (void) ThrowMagickException(exception,GetMagickModule(),
4064                OptionError,"SetReadOnlyProperty","`%s'",property);
4065           return(MagickFalse);
4066         }
4067       if (LocaleCompare("density",property) == 0)
4068         {
4069           GeometryInfo
4070             geometry_info;
4071
4072           flags=ParseGeometry(value,&geometry_info);
4073           image->resolution.x=geometry_info.rho;
4074           image->resolution.y=geometry_info.sigma;
4075           if ((flags & SigmaValue) == 0)
4076             image->resolution.y=image->resolution.x;
4077           return(MagickTrue);
4078         }
4079       if (LocaleCompare("depth",property) == 0)
4080         {
4081           image->depth=StringToUnsignedLong(value);
4082           return(MagickTrue);
4083         }
4084       if (LocaleCompare("dispose",property) == 0)
4085         {
4086           ssize_t
4087             dispose;
4088
4089           dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,value);
4090           if (dispose < 0)
4091             return(MagickFalse); /* FUTURE: value exception?? */
4092           image->dispose=(DisposeType) dispose;
4093           return(MagickTrue);
4094         }
4095       break; /* not an attribute, add as a property */
4096     }
4097 #if 0  /* Percent escape's sets values with this prefix: for later use
4098           Throwing an exception causes this setting to fail */
4099     case 'E':
4100     case 'e':
4101     {
4102       if (LocaleNCompare("exif:",property,5) == 0)
4103         {
4104           (void) ThrowMagickException(exception,GetMagickModule(),
4105                OptionError,"SetReadOnlyProperty","`%s'",property);
4106           return(MagickFalse);
4107         }
4108       break; /* not an attribute, add as a property */
4109     }
4110     case 'F':
4111     case 'f':
4112     {
4113       if (LocaleNCompare("fx:",property,3) == 0)
4114         {
4115           (void) ThrowMagickException(exception,GetMagickModule(),
4116                OptionError,"SetReadOnlyProperty","`%s'",property);
4117           return(MagickFalse);
4118         }
4119       break; /* not an attribute, add as a property */
4120     }
4121 #endif
4122     case 'G':
4123     case 'g':
4124     {
4125       if (LocaleCompare("gamma",property) == 0)
4126         {
4127           image->gamma=StringToDouble(value,(char **) NULL);
4128           return(MagickTrue);
4129         }
4130       if (LocaleCompare("gravity",property) == 0)
4131         {
4132           ssize_t
4133             gravity;
4134
4135           gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,value);
4136           if (gravity < 0)
4137             return(MagickFalse); /* FUTURE: value exception?? */
4138           image->gravity=(GravityType) gravity;
4139           return(MagickTrue);
4140         }
4141       break; /* not an attribute, add as a property */
4142     }
4143     case 'H':
4144     case 'h':
4145     {
4146       if (LocaleCompare("height",property) == 0)
4147         {
4148           (void) ThrowMagickException(exception,GetMagickModule(),
4149                OptionError,"SetReadOnlyProperty","`%s'",property);
4150           return(MagickFalse);
4151         }
4152       break; /* not an attribute, add as a property */
4153     }
4154     case 'I':
4155     case 'i':
4156     {
4157       if (LocaleCompare("intensity",property) == 0)
4158         {
4159           ssize_t
4160             intensity;
4161
4162           intensity=ParseCommandOption(MagickIntentOptions,MagickFalse,
4163             value);
4164           if (intensity < 0)
4165             return(MagickFalse);
4166           image->intensity=(PixelIntensityMethod) intensity;
4167           return(MagickTrue);
4168         }
4169       if (LocaleCompare("intent",property) == 0)
4170         {
4171           ssize_t
4172             rendering_intent;
4173
4174           rendering_intent=ParseCommandOption(MagickIntentOptions,MagickFalse,
4175             value);
4176           if (rendering_intent < 0)
4177             return(MagickFalse); /* FUTURE: value exception?? */
4178           image->rendering_intent=(RenderingIntent) rendering_intent;
4179           return(MagickTrue);
4180         }
4181       if (LocaleCompare("interpolate",property) == 0)
4182         {
4183           ssize_t
4184             interpolate;
4185
4186           interpolate=ParseCommandOption(MagickInterpolateOptions,MagickFalse,
4187             value);
4188           if (interpolate < 0)
4189             return(MagickFalse); /* FUTURE: value exception?? */
4190           image->interpolate=(PixelInterpolateMethod) interpolate;
4191           return(MagickTrue);
4192         }
4193 #if 0  /* Percent escape's sets values with this prefix: for later use
4194           Throwing an exception causes this setting to fail */
4195       if (LocaleNCompare("iptc:",property,5) == 0)
4196         {
4197           (void) ThrowMagickException(exception,GetMagickModule(),
4198                OptionError,"SetReadOnlyProperty","`%s'",property);
4199           return(MagickFalse);
4200         }
4201 #endif
4202       break; /* not an attribute, add as a property */
4203     }
4204     case 'K':
4205     case 'k':
4206       if (LocaleCompare("kurtosis",property) == 0)
4207         {
4208           (void) ThrowMagickException(exception,GetMagickModule(),
4209                OptionError,"SetReadOnlyProperty","`%s'",property);
4210           return(MagickFalse);
4211         }
4212       break; /* not an attribute, add as a property */
4213     case 'L':
4214     case 'l':
4215     {
4216       if (LocaleCompare("loop",property) == 0)
4217         {
4218           image->iterations=StringToUnsignedLong(value);
4219           return(MagickTrue);
4220         }
4221       break; /* not an attribute, add as a property */
4222     }
4223     case 'M':
4224     case 'm':
4225       if ( (LocaleCompare("magick",property) == 0) ||
4226            (LocaleCompare("max",property) == 0) ||
4227            (LocaleCompare("mean",property) == 0) ||
4228            (LocaleCompare("min",property) == 0) ||
4229            (LocaleCompare("min",property) == 0) )
4230         {
4231           (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
4232              "SetReadOnlyProperty","`%s'",property);
4233           return(MagickFalse);
4234         }
4235       break; /* not an attribute, add as a property */
4236     case 'O':
4237     case 'o':
4238       if (LocaleCompare("opaque",property) == 0)
4239         {
4240           (void) ThrowMagickException(exception,GetMagickModule(),
4241                OptionError,"SetReadOnlyProperty","`%s'",property);
4242           return(MagickFalse);
4243         }
4244       break; /* not an attribute, add as a property */
4245     case 'P':
4246     case 'p':
4247     {
4248       if (LocaleCompare("page",property) == 0)
4249         {
4250           char
4251             *geometry;
4252
4253           geometry=GetPageGeometry(value);
4254           flags=ParseAbsoluteGeometry(geometry,&image->page);
4255           geometry=DestroyString(geometry);
4256           return(MagickTrue);
4257         }
4258 #if 0  /* Percent escape's sets values with this prefix: for later use
4259           Throwing an exception causes this setting to fail */
4260       if (LocaleNCompare("pixel:",property,6) == 0)
4261         {
4262           (void) ThrowMagickException(exception,GetMagickModule(),
4263                OptionError,"SetReadOnlyProperty","`%s'",property);
4264           return(MagickFalse);
4265         }
4266 #endif
4267       if (LocaleCompare("profile",property) == 0)
4268         {
4269           ImageInfo
4270             *image_info;
4271
4272           StringInfo
4273             *profile;
4274
4275           image_info=AcquireImageInfo();
4276           (void) CopyMagickString(image_info->filename,value,MagickPathExtent);
4277           (void) SetImageInfo(image_info,1,exception);
4278           profile=FileToStringInfo(image_info->filename,~0UL,exception);
4279           if (profile != (StringInfo *) NULL)
4280             status=SetImageProfile(image,image_info->magick,profile,exception);
4281           image_info=DestroyImageInfo(image_info);
4282           return(MagickTrue);
4283         }
4284       break; /* not an attribute, add as a property */
4285     }
4286     case 'R':
4287     case 'r':
4288     {
4289       if (LocaleCompare("rendering-intent",property) == 0)
4290         {
4291           ssize_t
4292             rendering_intent;
4293
4294           rendering_intent=ParseCommandOption(MagickIntentOptions,MagickFalse,
4295             value);
4296           if (rendering_intent < 0)
4297             return(MagickFalse); /* FUTURE: value exception?? */
4298           image->rendering_intent=(RenderingIntent) rendering_intent;
4299           return(MagickTrue);
4300         }
4301       break; /* not an attribute, add as a property */
4302     }
4303     case 'S':
4304     case 's':
4305       if ( (LocaleCompare("size",property) == 0) ||
4306            (LocaleCompare("skewness",property) == 0) ||
4307            (LocaleCompare("scenes",property) == 0) ||
4308            (LocaleCompare("standard-deviation",property) == 0) )
4309         {
4310           (void) ThrowMagickException(exception,GetMagickModule(),
4311                OptionError,"SetReadOnlyProperty","`%s'",property);
4312           return(MagickFalse);
4313         }
4314       break; /* not an attribute, add as a property */
4315     case 'T':
4316     case 't':
4317     {
4318       if (LocaleCompare("tile-offset",property) == 0)
4319         {
4320           char
4321             *geometry;
4322
4323           geometry=GetPageGeometry(value);
4324           flags=ParseAbsoluteGeometry(geometry,&image->tile_offset);
4325           geometry=DestroyString(geometry);
4326           return(MagickTrue);
4327         }
4328       break; /* not an attribute, add as a property */
4329     }
4330     case 'U':
4331     case 'u':
4332     {
4333       if (LocaleCompare("units",property) == 0)
4334         {
4335           ssize_t
4336             units;
4337
4338           units=ParseCommandOption(MagickResolutionOptions,MagickFalse,value);
4339           if (units < 0)
4340             return(MagickFalse); /* FUTURE: value exception?? */
4341           image->units=(ResolutionType) units;
4342           return(MagickTrue);
4343         }
4344       break; /* not an attribute, add as a property */
4345     }
4346     case 'V':
4347     case 'v':
4348     {
4349       if (LocaleCompare("version",property) == 0)
4350         {
4351           (void) ThrowMagickException(exception,GetMagickModule(),
4352                OptionError,"SetReadOnlyProperty","`%s'",property);
4353           return(MagickFalse);
4354         }
4355       break; /* not an attribute, add as a property */
4356     }
4357     case 'W':
4358     case 'w':
4359     {
4360       if (LocaleCompare("width",property) == 0)
4361         {
4362           (void) ThrowMagickException(exception,GetMagickModule(),
4363                OptionError,"SetReadOnlyProperty","`%s'",property);
4364           return(MagickFalse);
4365         }
4366       break; /* not an attribute, add as a property */
4367     }
4368 #if 0  /* Percent escape's sets values with this prefix: for later use
4369           Throwing an exception causes this setting to fail */
4370     case 'X':
4371     case 'x':
4372     {
4373       if (LocaleNCompare("xmp:",property,4) == 0)
4374         {
4375           (void) ThrowMagickException(exception,GetMagickModule(),
4376                OptionError,"SetReadOnlyProperty","`%s'",property);
4377           return(MagickFalse);
4378         }
4379       break; /* not an attribute, add as a property */
4380     }
4381 #endif
4382   }
4383   /* Default: not an attribute, add as a property */
4384   status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
4385     ConstantString(property),ConstantString(value));
4386   /* FUTURE: error if status is bad? */
4387   return(status);
4388 }