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