]> 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 (~(1UL*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 (~(1UL*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 (~(1UL*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   MagickRealType
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=(MagickRealType) QuantumRange*alpha;
2104           status|=FxEvaluateChannelExpression(fx_info,GreenPixelChannel,0,0,
2105             &alpha,exception);
2106           pixel.green=(MagickRealType) QuantumRange*alpha;
2107           status|=FxEvaluateChannelExpression(fx_info,BluePixelChannel,0,0,
2108             &alpha,exception);
2109           pixel.blue=(MagickRealType) QuantumRange*alpha;
2110           if (image->colorspace == CMYKColorspace)
2111             {
2112               status|=FxEvaluateChannelExpression(fx_info,BlackPixelChannel,0,0,
2113                 &alpha,exception);
2114               pixel.black=(MagickRealType) QuantumRange*alpha;
2115             }
2116           status|=FxEvaluateChannelExpression(fx_info,AlphaPixelChannel,0,0,
2117             &alpha,exception);
2118           pixel.alpha=(MagickRealType) 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",
2249         (double) (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       /* FUTURE: ensure this does not generate the formatted comment! */
2260       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2261         GetNumberColors(image,(FILE *) NULL,exception));
2262       break;
2263     }
2264     case 'l': /* Image label property - empty string by default */
2265     {
2266       string=GetImageProperty(image,"label",exception);
2267       if ( string == (const char *)NULL)
2268         string="";
2269       break;
2270     }
2271     case 'm': /* Image format (file magick) */
2272     {
2273       string=image->magick;
2274       break;
2275     }
2276     case 'n': /* Number of images in the list.  */
2277     {
2278       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2279         GetImageListLength(image));
2280       break;
2281     }
2282     case 'o': /* Output Filename - for delegate use only */
2283     {
2284       string=image_info->filename;
2285       break;
2286     }
2287     case 'p': /* Image index in current image list -- As 'n' OBSOLETE */
2288     {
2289       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2290         GetImageIndexInList(image));
2291       break;
2292     }
2293     case 'q': /* Quantum depth of image in memory */
2294     {
2295       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2296         MAGICKCORE_QUANTUM_DEPTH);
2297       break;
2298     }
2299     case 'r': /* Image storage class and colorspace.  */
2300     {
2301       ColorspaceType
2302         colorspace;
2303
2304       colorspace=image->colorspace;
2305       if (IfMagickTrue(IsImageGray(image,exception)))
2306         colorspace=GRAYColorspace;
2307       (void) FormatLocaleString(value,MaxTextExtent,"%s %s %s",
2308         CommandOptionToMnemonic(MagickClassOptions,(ssize_t) image->storage_class),
2309         CommandOptionToMnemonic(MagickColorspaceOptions,(ssize_t) colorspace),
2310         IfMagickTrue(image->matte)?"Matte":"");
2311       break;
2312     }
2313     case 's': /* Image scene number */
2314     {
2315       if (image_info->number_scenes != 0)
2316         (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2317           image_info->scene);
2318       else
2319         (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2320           image->scene);
2321       break;
2322     }
2323     case 't': /* Base filename without directory or extention */
2324     {
2325       GetPathComponent(image->magick_filename,BasePath,value);
2326       break;
2327     }
2328     case 'u': /* Unique filename */
2329     {
2330       string=image_info->unique;
2331       break;
2332     }
2333     case 'w': /* Image width (current) */
2334     {
2335       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2336         (image->columns != 0 ? image->columns : image->magick_columns));
2337       break;
2338     }
2339     case 'x': /* Image horizontal resolution (with units) */
2340     {
2341       (void) FormatLocaleString(value,MaxTextExtent,"%g %s",
2342         image->resolution.x,CommandOptionToMnemonic(
2343         MagickResolutionOptions,(ssize_t)image->units));
2344       break;
2345     }
2346     case 'y': /* Image vertical resolution (with units) */
2347     {
2348       (void) FormatLocaleString(value,MaxTextExtent,"%g %s",
2349         image->resolution.y,CommandOptionToMnemonic(MagickResolutionOptions,
2350         (ssize_t) image->units));
2351       break;
2352     }
2353     case 'z': /* Image depth as read in */
2354     {
2355       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2356         image->depth);
2357       break;
2358     }
2359     case 'A': /* Image alpha channel  */
2360     {
2361       (void) FormatLocaleString(value,MaxTextExtent,"%s",
2362         CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
2363       break;
2364     }
2365     case 'C': /* Image compression method.  */
2366     {
2367       (void) FormatLocaleString(value,MaxTextExtent,"%s",
2368         CommandOptionToMnemonic(MagickCompressOptions,(ssize_t)
2369           image->compression));
2370       break;
2371     }
2372     case 'D': /* Image dispose method.  */
2373     {
2374       (void) FormatLocaleString(value,MaxTextExtent,"%s",
2375         CommandOptionToMnemonic(MagickDisposeOptions,(ssize_t) image->dispose));
2376       break;
2377     }
2378     case 'G': /* Image size as geometry = "%wx%h" */
2379     {
2380       (void) FormatLocaleString(value,MaxTextExtent,"%.20gx%.20g",
2381         (double)image->magick_columns,(double) image->magick_rows);
2382       break;
2383     }
2384     case 'H': /* layer canvas height */
2385     {
2386       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2387         image->page.height);
2388       break;
2389     }
2390     case 'M': /* Magick filename - filename given incl. coder & read mods */
2391     {
2392       string=image->magick_filename;
2393       break;
2394     }
2395     case 'O': /* layer canvas offset with sign = "+%X+%Y" */
2396     {
2397       (void) FormatLocaleString(value,MaxTextExtent,"%+ld%+ld",(long)
2398         image->page.x,(long) image->page.y);
2399       break;
2400     }
2401     case 'P': /* layer canvas page size = "%Wx%H" */
2402     {
2403       (void) FormatLocaleString(value,MaxTextExtent,"%.20gx%.20g",
2404         (double) image->page.width,(double) image->page.height);
2405       break;
2406     }
2407     case 'Q': /* image compression quality */
2408     {
2409       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2410         image->quality);
2411       break;
2412     }
2413     case 'S': /* Image scenes  ???? */
2414     {
2415       if (image_info->number_scenes == 0)
2416         string="2147483647";
2417       else
2418         (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2419           image_info->scene+image_info->number_scenes);
2420       break;
2421     }
2422     case 'T': /* image time delay for animations */
2423     {
2424       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2425         image->delay);
2426       break;
2427     }
2428     case 'W': /* layer canvas width */
2429     {
2430       (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2431         image->page.width);
2432       break;
2433     }
2434     case 'X': /* layer canvas X offset */
2435     {
2436       (void) FormatLocaleString(value,MaxTextExtent,"%+.20g",(double)
2437         image->page.x);
2438       break;
2439     }
2440     case 'Y': /* layer canvas Y offset */
2441     {
2442       (void) FormatLocaleString(value,MaxTextExtent,"%+.20g",(double)
2443         image->page.y);
2444       break;
2445     }
2446     case 'Z': /* Zero filename ??? */
2447     {
2448       string=image_info->zero;
2449       break;
2450     }
2451     case '@': /* Trim bounding box, without Trimming! */
2452     {
2453       RectangleInfo
2454         page;
2455
2456       page=GetImageBoundingBox(image,exception);
2457       (void) FormatLocaleString(value,MaxTextExtent,"%.20gx%.20g%+.20g%+.20g",
2458         (double) page.width,(double) page.height,(double) page.x,(double)
2459         page.y);
2460       break;
2461     }
2462     case '#': /* Image signature */
2463     {
2464       (void) SignatureImage(image,exception);
2465       string=GetImageProperty(image,"signature",exception);
2466       break;
2467     }
2468     case '%': /* percent escaped */
2469     {
2470       string="%";
2471       break;
2472     }
2473   }
2474   if (*value != '\0')
2475     string=value;
2476   if (string != (char *) NULL)
2477     {
2478       (void) SetImageArtifact(image, "get-property", string);
2479       return(GetImageArtifact(image, "get-property"));
2480     }
2481   return((char *)NULL);
2482 }
2483
2484 MagickExport const char *GetMagickProperty(const ImageInfo *image_info,
2485   Image *image,const char *property,ExceptionInfo *exception)
2486 {
2487   char
2488     value[MaxTextExtent];
2489
2490   const char
2491     *string;
2492
2493   assert(property[0] != '\0');
2494   if (property[1] == '\0')  /* single letter property request */
2495     return(GetMagickPropertyLetter(image_info,image,*property,exception));
2496   if ((image != (Image *) NULL) && IfMagickTrue(image->debug))
2497     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2498   *value='\0';  /* formated string */
2499   string=(char *) NULL;  /* constant string reference */
2500   switch (*property)
2501   {
2502     case 'b':
2503     {
2504       if ((LocaleCompare("base",property) == 0) ||
2505           (LocaleCompare("basename",property) == 0) )
2506         {
2507           GetPathComponent(image->magick_filename,BasePath,value);
2508           break;
2509         }
2510       break;
2511     }
2512     case 'c':
2513     {
2514       if (LocaleCompare("channels",property) == 0)
2515         {
2516           /* FUTURE: return actual image channels */
2517           (void) FormatLocaleString(value,MaxTextExtent,"%s",
2518             CommandOptionToMnemonic(MagickColorspaceOptions,(ssize_t)
2519             image->colorspace));
2520           LocaleLower(value);
2521           if( IfMagickTrue(image->matte) )
2522             (void) ConcatenateMagickString(value,"a",MaxTextExtent);
2523           break;
2524         }
2525       if (LocaleCompare("colorspace",property) == 0)
2526         {
2527           ColorspaceType
2528             colorspace;
2529
2530           /* FUTURE: return actual colorspace - no 'gray' stuff */
2531           colorspace=image->colorspace;
2532           if( IfMagickTrue(IsImageGray(image,exception)) )
2533             colorspace=GRAYColorspace;
2534           string=CommandOptionToMnemonic(MagickColorspaceOptions,(ssize_t)
2535             colorspace);
2536           break;
2537         }
2538       if (LocaleCompare("copyright",property) == 0)
2539         {
2540           (void) CopyMagickString(value,GetMagickCopyright(),MaxTextExtent);
2541           break;
2542         }
2543       break;
2544     }
2545     case 'd':
2546     {
2547       if (LocaleCompare("depth",property) == 0)
2548         {
2549           (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2550             image->depth);
2551           break;
2552         }
2553       if (LocaleCompare("directory",property) == 0)
2554         {
2555           GetPathComponent(image->magick_filename,HeadPath,value);
2556           break;
2557         }
2558       break;
2559     }
2560     case 'e':
2561     {
2562       if (LocaleCompare("extension",property) == 0)
2563         {
2564           GetPathComponent(image->magick_filename,ExtensionPath,value);
2565           break;
2566         }
2567       break;
2568     }
2569     case 'g':
2570     {
2571       if (LocaleCompare("gamma",property) == 0)
2572         {
2573           (void) FormatLocaleString(value,MaxTextExtent,"%.*g",
2574             GetMagickPrecision(),image->gamma);
2575           break;
2576         }
2577       if ( (image_info != (ImageInfo *) NULL) &&
2578            (LocaleCompare("group",property) == 0) )
2579         {
2580           (void) FormatLocaleString(value,MaxTextExtent,"0x%lx",(unsigned long)
2581             image_info->group);
2582           break;
2583         }
2584       break;
2585     }
2586     case 'h':
2587     {
2588       if (LocaleCompare("height",property) == 0)
2589         {
2590           (void) FormatLocaleString(value,MaxTextExtent,"%.20g",
2591             image->magick_rows != 0 ? (double) image->magick_rows : 256.0);
2592           break;
2593         }
2594       break;
2595     }
2596     case 'i':
2597     {
2598       if (LocaleCompare("input",property) == 0)
2599         {
2600           string=image->filename;
2601           break;
2602         }
2603       break;
2604     }
2605     case 'k':
2606     {
2607       if (LocaleCompare("kurtosis",property) == 0)
2608         {
2609           double
2610             kurtosis,
2611             skewness;
2612
2613           (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
2614           (void) FormatLocaleString(value,MaxTextExtent,"%.*g",
2615             GetMagickPrecision(),kurtosis);
2616           break;
2617         }
2618       break;
2619     }
2620     case 'm':
2621     {
2622       if (LocaleCompare("magick",property) == 0)
2623         {
2624           string=image->magick;
2625           break;
2626         }
2627       if (LocaleCompare("max",property) == 0)
2628         {
2629           double
2630             maximum,
2631             minimum;
2632
2633           (void) GetImageRange(image,&minimum,&maximum,exception);
2634           (void) FormatLocaleString(value,MaxTextExtent,"%.*g",
2635             GetMagickPrecision(),maximum);
2636           break;
2637         }
2638       if (LocaleCompare("mean",property) == 0)
2639         {
2640           double
2641             mean,
2642             standard_deviation;
2643
2644           (void) GetImageMean(image,&mean,&standard_deviation,exception);
2645           (void) FormatLocaleString(value,MaxTextExtent,"%.*g",
2646             GetMagickPrecision(),mean);
2647           break;
2648         }
2649       if (LocaleCompare("min",property) == 0)
2650         {
2651           double
2652             maximum,
2653             minimum;
2654
2655           (void) GetImageRange(image,&minimum,&maximum,exception);
2656           (void) FormatLocaleString(value,MaxTextExtent,"%.*g",
2657             GetMagickPrecision(),minimum);
2658           break;
2659         }
2660       break;
2661     }
2662     case 'o':
2663     {
2664       if (LocaleCompare("opaque",property) == 0)
2665         {
2666           MagickBooleanType
2667             opaque;
2668
2669           opaque=IsImageOpaque(image,exception);
2670           (void) CopyMagickString(value,IfMagickTrue(opaque)?"true":"false",
2671                MaxTextExtent);
2672           break;
2673         }
2674       if (LocaleCompare("orientation",property) == 0)
2675         {
2676           string=CommandOptionToMnemonic(MagickOrientationOptions,(ssize_t)
2677             image->orientation);
2678           break;
2679         }
2680       if ((image_info != (ImageInfo *) NULL) &&
2681           (LocaleCompare("output",property) == 0))
2682         {
2683           (void) CopyMagickString(value,image_info->filename,MaxTextExtent);
2684           break;
2685         }
2686      break;
2687     }
2688     case 'p':
2689     {
2690       if (LocaleCompare("page",property) == 0)
2691         {
2692           (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2693             GetImageIndexInList(image)+1);
2694           break;
2695         }
2696       break;
2697     }
2698     case 'r':
2699     {
2700       /* This matches %[fx:resolution.x] */
2701       if (LocaleCompare("resolution.x",property) == 0)
2702         {
2703           (void) FormatLocaleString(value,MaxTextExtent,"%g",
2704             image->resolution.x);
2705           break;
2706         }
2707       /* This matches %[fx:resolution.y] */
2708       if (LocaleCompare("resolution.y",property) == 0)
2709         {
2710           (void) FormatLocaleString(value,MaxTextExtent,"%g",
2711             image->resolution.y);
2712           break;
2713         }
2714       break;
2715     }
2716     case 's':
2717     {
2718       if (LocaleCompare("scene",property) == 0)
2719         {
2720           if ((image_info != (ImageInfo *) NULL) &&
2721               (image_info->number_scenes != 0))
2722             (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2723               image_info->scene);
2724           else
2725             (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2726               image->scene);
2727           break;
2728         }
2729       if (LocaleCompare("scenes",property) == 0)
2730         {
2731           (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2732             GetImageListLength(image));
2733           break;
2734         }
2735       if (LocaleCompare("size",property) == 0)
2736         {
2737           char
2738             format[MaxTextExtent];
2739
2740           (void) FormatMagickSize(GetBlobSize(image),MagickFalse,format);
2741           (void) FormatLocaleString(value,MaxTextExtent,"%sB",format);
2742           break;
2743         }
2744       if (LocaleCompare("skewness",property) == 0)
2745         {
2746           double
2747             kurtosis,
2748             skewness;
2749
2750           (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
2751           (void) FormatLocaleString(value,MaxTextExtent,"%.*g",
2752             GetMagickPrecision(),skewness);
2753           break;
2754         }
2755       if (LocaleCompare("standard-deviation",property) == 0)
2756         {
2757           double
2758             mean,
2759             standard_deviation;
2760
2761           (void) GetImageMean(image,&mean,&standard_deviation,
2762             exception);
2763           (void) FormatLocaleString(value,MaxTextExtent,"%.*g",
2764             GetMagickPrecision(),standard_deviation);
2765           break;
2766         }
2767        break;
2768     }
2769     case 't':
2770     {
2771       if (LocaleCompare("type",property) == 0)
2772         {
2773           string=CommandOptionToMnemonic(MagickTypeOptions,(ssize_t)
2774             GetImageType(image,exception));
2775           break;
2776         }
2777        break;
2778     }
2779     case 'u':
2780     {
2781       if ((image_info != (ImageInfo *) NULL) &&
2782           (LocaleCompare("unique",property) == 0))
2783         {
2784           string=image_info->unique;
2785           break;
2786         }
2787       break;
2788     }
2789     case 'v':
2790     {
2791       if (LocaleCompare("version",property) == 0)
2792         {
2793           string=GetMagickVersion((size_t *) NULL);
2794           break;
2795         }
2796       break;
2797     }
2798     case 'w':
2799     {
2800       if (LocaleCompare("width",property) == 0)
2801         {
2802           (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
2803             (image->magick_columns != 0 ? image->magick_columns : 256));
2804           break;
2805         }
2806       break;
2807     }
2808     case 'x': /* FUTURE: Obsolete X resolution */
2809     {
2810       if ((LocaleCompare("xresolution",property) == 0) ||
2811           (LocaleCompare("x-resolution",property) == 0) )
2812         {
2813           (void) FormatLocaleString(value,MaxTextExtent,"%.20g",
2814             image->resolution.x);
2815           break;
2816         }
2817       break;
2818     }
2819     case 'y': /* FUTURE: Obsolete Y resolution */
2820     {
2821       if ((LocaleCompare("yresolution",property) == 0) ||
2822           (LocaleCompare("y-resolution",property) == 0) )
2823         {
2824           (void) FormatLocaleString(value,MaxTextExtent,"%.20g",
2825             image->resolution.y);
2826           break;
2827         }
2828       break;
2829     }
2830     case 'z':
2831     {
2832       if ((image_info != (ImageInfo *) NULL) &&
2833           (LocaleCompare("zero",property) == 0))
2834         {
2835           string=image_info->zero;
2836           break;
2837         }
2838       break;
2839     }
2840   }
2841   if (*value != '\0')
2842     string=value;
2843   if (string != (char *)NULL)
2844     {
2845       (void) SetImageArtifact(image,"get-property", string);
2846       return(GetImageArtifact(image,"get-property"));
2847     }
2848   return((char *)NULL);
2849 }
2850 \f
2851 /*
2852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2853 %                                                                             %
2854 %                                                                             %
2855 %                                                                             %
2856 %   G e t N e x t I m a g e P r o p e r t y                                   %
2857 %                                                                             %
2858 %                                                                             %
2859 %                                                                             %
2860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2861 %
2862 %  GetNextImageProperty() gets the next free-form string property name.
2863 %
2864 %  The format of the GetNextImageProperty method is:
2865 %
2866 %      char *GetNextImageProperty(const Image *image)
2867 %
2868 %  A description of each parameter follows:
2869 %
2870 %    o image: the image.
2871 %
2872 */
2873 MagickExport char *GetNextImageProperty(const Image *image)
2874 {
2875   assert(image != (Image *) NULL);
2876   assert(image->signature == MagickSignature);
2877   if( IfMagickTrue(image->debug) )
2878     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2879       image->filename);
2880   if (image->properties == (void *) NULL)
2881     return((char *) NULL);
2882   return((char *) GetNextKeyInSplayTree((SplayTreeInfo *) image->properties));
2883 }
2884 \f
2885 /*
2886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2887 %                                                                             %
2888 %                                                                             %
2889 %                                                                             %
2890 %   I n t e r p r e t I m a g e P r o p e r t i e s                           %
2891 %                                                                             %
2892 %                                                                             %
2893 %                                                                             %
2894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2895 %
2896 %  InterpretImageProperties() replaces any embedded formatting characters with
2897 %  the appropriate image property and returns the interpreted text.
2898 %
2899 %  This searches for and replaces
2900 %     \n \r \%          replaced by newline, return, and percent resp.
2901 %     &lt; &gt; &amp;   replaced by '<', '>', '&' resp.
2902 %     %%                replaced by percent
2903 %
2904 %     %x            where 'x' is a single letter, case sensitive).
2905 %     %[type:name]  where 'type' is specifically known prefix.
2906 %     %[name]       where 'name' is a specifically known attribute, calculated
2907 %                   value, or a per-image property string name, or a per-image
2908 %                   'artifact' (as generated from a global option)
2909 %
2910 %  Single letter % substitutions will only happen if the preceeding character
2911 %  is NOT a number. But braced substitutions will always be performed. This
2912 %  prevents typical usage of percent in 'geometry arguments' from being
2913 %  substituted unexpectedly.
2914 %
2915 %  If 'glob-expresions' ('*' or '?' characters) is used for 'name' it may be
2916 %  used as a search pattern to print multiple lines of "name=value\n" pairs of
2917 %  the associacted set of properities.
2918 %
2919 %  The returned string must be freed using DestoryString().
2920 %
2921 %  The format of the InterpretImageProperties method is:
2922 %
2923 %      char *InterpretImageProperties(const ImageInfo *image_info,
2924 %        Image *image,const char *embed_text,ExceptionInfo *exception)
2925 %
2926 %  A description of each parameter follows:
2927 %
2928 %    o image_info: the image info.
2929 %
2930 %    o image: the image.
2931 %
2932 %    o embed_text: the address of a character string containing the embedded
2933 %      formatting characters.
2934 %
2935 %    o exception: return any errors or warnings in this structure.
2936 %
2937 */
2938 MagickExport char *InterpretImageProperties(const ImageInfo *image_info,
2939   Image *image,const char *embed_text,ExceptionInfo *exception)
2940 {
2941   char
2942     *interpret_text;
2943
2944   register char
2945     *q;
2946
2947   register const char
2948     *p;
2949
2950   size_t
2951     extent,
2952     length;
2953
2954   MagickBooleanType
2955     number;
2956
2957   assert(image != (Image *) NULL);
2958   assert(image->signature == MagickSignature);
2959   if( IfMagickTrue(image->debug) )
2960     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2961
2962   if ((embed_text == (const char *) NULL))
2963     return((char *) NULL);
2964   p=embed_text;
2965
2966   if (*p == '\0')
2967     return(ConstantString(""));
2968
2969   /* handle a '@' replace string from file */
2970   if (*p == '@') {
2971      p++;
2972      if (*p != '-' && IfMagickFalse(IsPathAccessible(p)) ) {
2973        (void) ThrowMagickException(exception,GetMagickModule(),
2974            OptionError,"UnableToAccessPath","%s",p);
2975        return((char *) NULL);
2976      }
2977      return(FileToString(p,~0,exception));
2978   }
2979
2980   /*
2981     Translate any embedded format characters.
2982   */
2983   interpret_text=AcquireString(embed_text); /* new string with extra space */
2984   extent=MaxTextExtent;                     /* how many extra space */
2985   number=MagickFalse;                       /* is last char a number? */
2986   for (q=interpret_text; *p!='\0'; number=IsMagickTrue(isdigit(*p)),p++)
2987   {
2988     *q='\0';
2989     if ((size_t) (q-interpret_text+MaxTextExtent) >= extent)
2990       {
2991         extent+=MaxTextExtent;
2992         interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+
2993           MaxTextExtent+1,sizeof(*interpret_text));
2994         if (interpret_text == (char *) NULL)
2995           return((char *) NULL);
2996         q=interpret_text+strlen(interpret_text);
2997       }
2998     /*
2999       Look for percent escapes, (and handle other specials)
3000     */
3001     switch (*p) {
3002       case '\\':
3003         switch (*(p+1)) {
3004           case '\0':
3005             continue;
3006           case 'r':       /* convert to RETURN */
3007             *q++='\r';
3008             p++;
3009             continue;
3010           case 'n':       /* convert to NEWLINE */
3011             *q++='\n';
3012             p++;
3013             continue;
3014           case '\n':      /* EOL removal UNIX,MacOSX */
3015             p++;
3016             continue;
3017           case '\r':      /* EOL removal DOS,Windows */
3018             p++;
3019             if (*p == '\n') /* return-newline EOL */
3020               p++;
3021             continue;
3022           default:
3023             p++;
3024             *q++=(*p);
3025             continue;
3026         }
3027         continue; /* never reached! */
3028       case '&':
3029         if (LocaleNCompare("&lt;",p,4) == 0)
3030           *q++='<', p+=3;
3031         else if (LocaleNCompare("&gt;",p,4) == 0)
3032           *q++='>', p+=3;
3033         else if (LocaleNCompare("&amp;",p,5) == 0)
3034           *q++='&', p+=4;
3035         else
3036           *q++=(*p);
3037         continue;
3038       case '%':
3039         break;      /* continue to next set of handlers */
3040       default:
3041         *q++=(*p);  /* any thing else is 'as normal' */
3042         continue;
3043     }
3044     p++; /* advance beyond the percent */
3045
3046     /*
3047       Doubled Percent - or percent at end of string
3048     */
3049     if ( *p == '\0' )
3050        p--;
3051     if ( *p == '%' ) {
3052         *q++='%';
3053         continue;
3054       }
3055
3056     /*
3057       Single letter escapes
3058     */
3059     if ( *p != '[' ) {
3060       const char
3061         *value;
3062
3063       /* But only if not preceeded by a number! */
3064       if ( IfMagickTrue(number) ) {
3065         *q++='%'; /* do NOT substitute the percent */
3066         p--;      /* back up one */
3067         continue;
3068       }
3069       value=GetMagickPropertyLetter(image_info,image,*p, exception);
3070       if (value != (char *) NULL) {
3071         length=strlen(value);
3072         if ((size_t) (q-interpret_text+length+1) >= extent)
3073           {
3074             extent+=length;
3075             interpret_text=(char *) ResizeQuantumMemory(interpret_text,
3076               extent+MaxTextExtent,sizeof(*interpret_text));
3077             if (interpret_text == (char *) NULL)
3078               return((char *)NULL);
3079             q=interpret_text+strlen(interpret_text);
3080           }
3081         (void) CopyMagickString(q,value,extent);
3082         q+=length;
3083         continue;
3084       }
3085       (void) ThrowMagickException(exception,GetMagickModule(),
3086           OptionWarning,"UnknownImageProperty","\"%%%c\"",*p);
3087       continue;
3088     }
3089
3090     /*
3091       Braced Percent Escape
3092     */
3093     {
3094       char
3095         pattern[MaxTextExtent];
3096
3097       const char
3098         *key,
3099         *value;
3100
3101       register ssize_t
3102         len;
3103
3104       ssize_t
3105         depth;
3106
3107       /* get the string framed by the %[...] */
3108       p++;  /* advance p to just inside the opening brace */
3109       depth=1;
3110       if ( *p == ']' ) {
3111         (void) ThrowMagickException(exception,GetMagickModule(),
3112             OptionWarning,"UnknownImageProperty","\"%%[]\"");
3113         break;
3114       }
3115       for (len=0; len<(MaxTextExtent-1L) && (*p != '\0');)
3116       {
3117         /* skip escaped braces within braced pattern */
3118         if ( (*p == '\\') && (*(p+1) != '\0') ) {
3119           pattern[len++]=(*p++);
3120           pattern[len++]=(*p++);
3121           continue;
3122         }
3123         if (*p == '[')
3124           depth++;
3125         if (*p == ']')
3126           depth--;
3127         if (depth <= 0)
3128           break;
3129         pattern[len++]=(*p++);
3130       }
3131       pattern[len]='\0';
3132       /* Check for unmatched final ']' for "%[...]" */
3133       if ( depth != 0 ) {
3134         if (len >= 64) {  /* truncate string for error message */
3135           pattern[61] = '.';
3136           pattern[62] = '.';
3137           pattern[63] = '.';
3138           pattern[64] = '\0';
3139         }
3140         (void) ThrowMagickException(exception,GetMagickModule(),
3141             OptionError,"UnbalancedBraces","\"%%[%s\"",pattern);
3142         interpret_text=DestroyString(interpret_text);
3143         return((char *)NULL);
3144       }
3145
3146       /*
3147         Special Property Prefixes
3148         such as: %[exif:...] %[fx:...] %[pixel:...]
3149         Otherwise a free-form property string
3150       */
3151       value=GetImageProperty(image,pattern,exception);
3152       if (value != (const char *) NULL)
3153         {
3154           length=strlen(value);
3155           if ((size_t) (q-interpret_text+length+1) >= extent)
3156             {
3157               extent+=length;
3158               interpret_text=(char *) ResizeQuantumMemory(interpret_text,
3159                 extent+MaxTextExtent,sizeof(*interpret_text));
3160               if (interpret_text == (char *) NULL)
3161                 return((char *)NULL);
3162               q=interpret_text+strlen(interpret_text);
3163             }
3164           (void) CopyMagickString(q,value,extent);
3165           q+=length;
3166           continue;
3167         }
3168       /*
3169         Handle property 'glob' patterns
3170         Such as:  %[*]   %[user:array_??]  %[filename:e*]
3171       */
3172       if( IfMagickTrue(IsGlob(pattern)) )
3173         {
3174           ResetImagePropertyIterator(image);
3175           key=GetNextImageProperty(image);
3176           while (key != (const char *) NULL)
3177           {
3178             if( IfMagickTrue(GlobExpression(key,pattern,MagickTrue)) )
3179               {
3180                 value=GetImageProperty(image,key,exception);
3181                 if (value != (const char *) NULL)
3182                   {
3183                     length=strlen(key)+strlen(value)+2;
3184                     if ((size_t) (q-interpret_text+length+1) >= extent)
3185                       {
3186                         extent+=length;
3187                         interpret_text=(char *) ResizeQuantumMemory(
3188                           interpret_text,extent+MaxTextExtent,
3189                           sizeof(*interpret_text));
3190                         if (interpret_text == (char *) NULL)
3191                           return((char *)NULL);
3192                         q=interpret_text+strlen(interpret_text);
3193                       }
3194                     q+=FormatLocaleString(q,extent,"%s=%s\n",key,value);
3195                   }
3196               }
3197             key=GetNextImageProperty(image);
3198           }
3199           continue;
3200         }
3201       /*
3202         Look for a known property or image attribute
3203         Such as  %[basename]  %[denisty]  %[delay]
3204         Also does wrapped single letters:  %[b] %[G] %[g]
3205       */
3206       value=GetMagickProperty(image_info,image,pattern,exception);
3207       if (value != (const char *) NULL)
3208         {
3209           length=strlen(value);
3210           if ((size_t) (q-interpret_text+length+1) >= extent)
3211             {
3212               extent+=length;
3213               interpret_text=(char *) ResizeQuantumMemory(interpret_text,
3214                 extent+MaxTextExtent,sizeof(*interpret_text));
3215               if (interpret_text == (char *) NULL)
3216                 return((char *)NULL);
3217               q=interpret_text+strlen(interpret_text);
3218             }
3219           (void) CopyMagickString(q,value,extent);
3220           q+=length;
3221           continue;
3222         }
3223       /*
3224         Look for a per-image Artifact (user option, post-interpreted)
3225       */
3226       value=GetImageArtifact(image,pattern);
3227       if (value != (char *) NULL)
3228         {
3229           length=strlen(value);
3230           if ((size_t) (q-interpret_text+length+1) >= extent)
3231             {
3232               extent+=length;
3233               interpret_text=(char *) ResizeQuantumMemory(interpret_text,
3234                 extent+MaxTextExtent,sizeof(*interpret_text));
3235               if (interpret_text == (char *) NULL)
3236                 return((char *)NULL);
3237               q=interpret_text+strlen(interpret_text);
3238             }
3239           (void) CopyMagickString(q,value,extent);
3240           q+=length;
3241           continue;
3242         }
3243       /*
3244         Look for user option of this name (should never match in CLI usage)
3245       */
3246       if (image_info != (ImageInfo *) NULL) {
3247         value=GetImageOption(image_info,pattern);
3248         if (value != (char *) NULL)
3249           {
3250             length=strlen(value);
3251             if ((size_t) (q-interpret_text+length+1) >= extent)
3252               {
3253                 extent+=length;
3254                 interpret_text=(char *) ResizeQuantumMemory(interpret_text,
3255                   extent+MaxTextExtent,sizeof(*interpret_text));
3256                 if (interpret_text == (char *) NULL)
3257                   return((char *)NULL);
3258                 q=interpret_text+strlen(interpret_text);
3259               }
3260             (void) CopyMagickString(q,value,extent);
3261             q+=length;
3262             continue;
3263           }
3264         }
3265       /*
3266         Failed to find any match anywhere!
3267       */
3268       if (len >= 64) {  /* truncate string for error message */
3269         pattern[61] = '.';
3270         pattern[62] = '.';
3271         pattern[63] = '.';
3272         pattern[64] = '\0';
3273       }
3274       (void) ThrowMagickException(exception,GetMagickModule(),
3275           OptionWarning,"UnknownImageProperty","\"%%[%s]\"",pattern);
3276       /* continue */
3277     } /* Braced Percent Escape */
3278
3279   } /* for each char in 'embed_text' */
3280   *q='\0';
3281   return(interpret_text);
3282 }
3283 \f
3284 /*
3285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3286 %                                                                             %
3287 %                                                                             %
3288 %                                                                             %
3289 %   R e m o v e I m a g e P r o p e r t y                                     %
3290 %                                                                             %
3291 %                                                                             %
3292 %                                                                             %
3293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3294 %
3295 %  RemoveImageProperty() removes a property from the image and returns its
3296 %  value.
3297 %
3298 %  The format of the RemoveImageProperty method is:
3299 %
3300 %      char *RemoveImageProperty(Image *image,const char *property)
3301 %
3302 %  A description of each parameter follows:
3303 %
3304 %    o image: the image.
3305 %
3306 %    o property: the image property.
3307 %
3308 */
3309 MagickExport char *RemoveImageProperty(Image *image,
3310   const char *property)
3311 {
3312   char
3313     *value;
3314
3315   assert(image != (Image *) NULL);
3316   assert(image->signature == MagickSignature);
3317   if( IfMagickTrue(image->debug) )
3318     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3319       image->filename);
3320   if (image->properties == (void *) NULL)
3321     return((char *) NULL);
3322   value=(char *) RemoveNodeFromSplayTree((SplayTreeInfo *) image->properties,
3323     property);
3324   return(value);
3325 }
3326 \f
3327 /*
3328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3329 %                                                                             %
3330 %                                                                             %
3331 %                                                                             %
3332 %   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                       %
3333 %                                                                             %
3334 %                                                                             %
3335 %                                                                             %
3336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3337 %
3338 %  ResetImagePropertyIterator() resets the image properties iterator.  Use it
3339 %  in conjunction with GetNextImageProperty() to iterate over all the values
3340 %  associated with an image property.
3341 %
3342 %  The format of the ResetImagePropertyIterator method is:
3343 %
3344 %      ResetImagePropertyIterator(Image *image)
3345 %
3346 %  A description of each parameter follows:
3347 %
3348 %    o image: the image.
3349 %
3350 */
3351 MagickExport void ResetImagePropertyIterator(const Image *image)
3352 {
3353   assert(image != (Image *) NULL);
3354   assert(image->signature == MagickSignature);
3355   if( IfMagickTrue(image->debug) )
3356     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3357       image->filename);
3358   if (image->properties == (void *) NULL)
3359     return;
3360   ResetSplayTreeIterator((SplayTreeInfo *) image->properties);
3361 }
3362 \f
3363 /*
3364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3365 %                                                                             %
3366 %                                                                             %
3367 %                                                                             %
3368 %   S e t I m a g e P r o p e r t y                                           %
3369 %                                                                             %
3370 %                                                                             %
3371 %                                                                             %
3372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3373 %
3374 %  SetImageProperty() saves the given string value either to specific known
3375 %  attribute or to a freeform property string.
3376 %
3377 %  Attempting to set a property that is normally calculated will produce
3378 %  an exception.
3379 %
3380 %  The format of the SetImageProperty method is:
3381 %
3382 %      MagickBooleanType SetImageProperty(Image *image,const char *property,
3383 %        const char *value,ExceptionInfo *exception)
3384 %
3385 %  A description of each parameter follows:
3386 %
3387 %    o image: the image.
3388 %
3389 %    o property: the image property.
3390 %
3391 %    o values: the image property values.
3392 %
3393 %    o exception: return any errors or warnings in this structure.
3394 %
3395 */
3396 MagickExport MagickBooleanType SetImageProperty(Image *image,
3397   const char *property,const char *value,ExceptionInfo *exception)
3398 {
3399   MagickBooleanType
3400     status;
3401
3402   MagickStatusType
3403     flags;
3404
3405   assert(image != (Image *) NULL);
3406   assert(image->signature == MagickSignature);
3407   if( IfMagickTrue(image->debug) )
3408     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3409
3410   /* Create splay-tree */
3411   if (image->properties == (void *) NULL)
3412     image->properties=NewSplayTree(CompareSplayTreeString,
3413       RelinquishMagickMemory,RelinquishMagickMemory);
3414
3415   /* Delete property if NULL --  empty string values are valid! */
3416   if ((value == (const char *) NULL))
3417     return(DeleteImageProperty(image,property));
3418   status=MagickTrue;
3419
3420   /* Do not 'set' single letter properties - read only shorthand */
3421   if (strlen(property) <= 1)
3422     {
3423       (void) ThrowMagickException(exception,GetMagickModule(),
3424           OptionError,"SetReadOnlyProperty","'%s'",property);
3425       return(MagickFalse);
3426     }
3427
3428   /* FUTURE: These should produce 'illegal settings'
3429      + binary chars in p[roperty key
3430      + single letter property keys (read only)
3431      + known special prefix (read only, they don't get saved!)
3432   */
3433
3434   switch (*property)
3435   {
3436     case 'B':
3437     case 'b':
3438     {
3439       if (LocaleCompare("background",property) == 0)
3440         {
3441           (void) QueryColorCompliance(value,AllCompliance,
3442             &image->background_color,exception);
3443           break;
3444         }
3445       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3446         ConstantString(property),ConstantString(value));
3447       /* FUTURE: error if status is bad? */
3448       break;
3449     }
3450     case 'C':
3451     case 'c':
3452     {
3453       if (LocaleCompare("channels",property) == 0)
3454         {
3455           (void) ThrowMagickException(exception,GetMagickModule(),
3456                OptionError,"SetReadOnlyProperty","'%s'",property);
3457           status=MagickFalse;
3458           break;
3459         }
3460       if (LocaleCompare("colorspace",property) == 0)
3461         {
3462           ssize_t
3463             colorspace;
3464
3465           colorspace=ParseCommandOption(MagickColorspaceOptions,MagickFalse,
3466             value);
3467           if (colorspace < 0)
3468             break;
3469           image->colorspace=(ColorspaceType) colorspace;
3470           image->rendering_intent=UndefinedIntent;
3471           image->gamma=1.000f;
3472           ResetMagickMemory(&image->chromaticity,0,sizeof(image->chromaticity));
3473           if (IssRGBColorspace(image->colorspace) != MagickFalse)
3474             {
3475               image->rendering_intent=PerceptualIntent;
3476               image->gamma=1.000f/2.200f;
3477               image->chromaticity.red_primary.x=0.6400f;
3478               image->chromaticity.red_primary.y=0.3300f;
3479               image->chromaticity.red_primary.z=0.0300f;
3480               image->chromaticity.green_primary.x=0.3000f;
3481               image->chromaticity.green_primary.y=0.6000f;
3482               image->chromaticity.green_primary.z=0.1000f;
3483               image->chromaticity.blue_primary.x=0.1500f;
3484               image->chromaticity.blue_primary.y=0.0600f;
3485               image->chromaticity.blue_primary.z=0.7900f;
3486               image->chromaticity.white_point.x=0.3127f;
3487               image->chromaticity.white_point.y=0.3290f;
3488               image->chromaticity.white_point.z=0.3583f;
3489             }
3490           break;
3491         }
3492       if (LocaleCompare("compose",property) == 0)
3493         {
3494           ssize_t
3495             compose;
3496
3497           compose=ParseCommandOption(MagickComposeOptions,MagickFalse,value);
3498           if (compose < 0)
3499             break;
3500           image->compose=(CompositeOperator) compose;
3501           break;
3502         }
3503       if (LocaleCompare("compress",property) == 0)
3504         {
3505           ssize_t
3506             compression;
3507
3508           compression=ParseCommandOption(MagickCompressOptions,MagickFalse,
3509             value);
3510           if (compression < 0)
3511             break;
3512           image->compression=(CompressionType) compression;
3513           break;
3514         }
3515       if (LocaleCompare("copyright",property) == 0)
3516         {
3517           (void) ThrowMagickException(exception,GetMagickModule(),
3518                OptionError,"SetReadOnlyProperty","'%s'",property);
3519           status=MagickFalse;
3520           break;
3521         }
3522       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3523         ConstantString(property),ConstantString(value));
3524       break;
3525     }
3526     case 'D':
3527     case 'd':
3528     {
3529       if (LocaleCompare("delay",property) == 0)
3530         {
3531           GeometryInfo
3532             geometry_info;
3533
3534           flags=ParseGeometry(value,&geometry_info);
3535           if ((flags & GreaterValue) != 0)
3536             {
3537               if (image->delay > (size_t) floor(geometry_info.rho+0.5))
3538                 image->delay=(size_t) floor(geometry_info.rho+0.5);
3539             }
3540           else
3541             if ((flags & LessValue) != 0)
3542               {
3543                 if (image->delay < (size_t) floor(geometry_info.rho+0.5))
3544                   image->ticks_per_second=(ssize_t)
3545                     floor(geometry_info.sigma+0.5);
3546               }
3547             else
3548               image->delay=(size_t) floor(geometry_info.rho+0.5);
3549           if ((flags & SigmaValue) != 0)
3550             image->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
3551           break;
3552         }
3553       if (LocaleCompare("density",property) == 0)
3554         {
3555           GeometryInfo
3556             geometry_info;
3557
3558           flags=ParseGeometry(value,&geometry_info);
3559           image->resolution.x=geometry_info.rho;
3560           image->resolution.y=geometry_info.sigma;
3561           if ((flags & SigmaValue) == 0)
3562             image->resolution.y=image->resolution.x;
3563         }
3564       if (LocaleCompare("depth",property) == 0)
3565         {
3566           image->depth=StringToUnsignedLong(value);
3567           break;
3568         }
3569       if (LocaleCompare("dispose",property) == 0)
3570         {
3571           ssize_t
3572             dispose;
3573
3574           dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,value);
3575           if (dispose < 0)
3576             break;
3577           image->dispose=(DisposeType) dispose;
3578           break;
3579         }
3580       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3581         ConstantString(property),ConstantString(value));
3582       break;
3583     }
3584     case 'G':
3585     case 'g':
3586     {
3587       if (LocaleCompare("gamma",property) == 0)
3588         {
3589           image->gamma=StringToDouble(value,(char **) NULL);
3590           break;
3591         }
3592       if (LocaleCompare("gravity",property) == 0)
3593         {
3594           ssize_t
3595             gravity;
3596
3597           gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,value);
3598           if (gravity < 0)
3599             break;
3600           image->gravity=(GravityType) gravity;
3601           break;
3602         }
3603       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3604         ConstantString(property),ConstantString(value));
3605       break;
3606     }
3607     case 'H':
3608     case 'h':
3609       if (LocaleCompare("height",property) == 0)
3610         {
3611           (void) ThrowMagickException(exception,GetMagickModule(),
3612                OptionError,"SetReadOnlyProperty","'%s'",property);
3613           status=MagickFalse;
3614           break;
3615         }
3616     case 'I':
3617     case 'i':
3618     {
3619       if (LocaleCompare("intent",property) == 0)
3620         {
3621           ssize_t
3622             rendering_intent;
3623
3624           rendering_intent=ParseCommandOption(MagickIntentOptions,MagickFalse,
3625             value);
3626           if (rendering_intent < 0)
3627             break;
3628           image->rendering_intent=(RenderingIntent) rendering_intent;
3629           break;
3630         }
3631       if (LocaleCompare("interpolate",property) == 0)
3632         {
3633           ssize_t
3634             interpolate;
3635
3636           interpolate=ParseCommandOption(MagickInterpolateOptions,MagickFalse,
3637             value);
3638           if (interpolate < 0)
3639             break;
3640           image->interpolate=(PixelInterpolateMethod) interpolate;
3641           break;
3642         }
3643       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3644         ConstantString(property),ConstantString(value));
3645       break;
3646     }
3647     case 'K':
3648     case 'k':
3649       if (LocaleCompare("kurtosis",property) == 0)
3650         {
3651           (void) ThrowMagickException(exception,GetMagickModule(),
3652                OptionError,"SetReadOnlyProperty","'%s'",property);
3653           status=MagickFalse;
3654           break;
3655         }
3656     case 'L':
3657     case 'l':
3658     {
3659       if (LocaleCompare("loop",property) == 0)
3660         {
3661           image->iterations=StringToUnsignedLong(value);
3662           break;
3663         }
3664       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3665         ConstantString(property),ConstantString(value));
3666       break;
3667     }
3668     case 'M':
3669     case 'm':
3670       if ( (LocaleCompare("magick",property) == 0) ||
3671            (LocaleCompare("max",property) == 0) ||
3672            (LocaleCompare("mean",property) == 0) ||
3673            (LocaleCompare("min",property) == 0) ||
3674            (LocaleCompare("min",property) == 0) )
3675         {
3676           (void) ThrowMagickException(exception,GetMagickModule(),
3677                OptionError,"SetReadOnlyProperty","'%s'",property);
3678           status=MagickFalse;
3679           break;
3680         }
3681     case 'O':
3682     case 'o':
3683       if (LocaleCompare("opaque",property) == 0)
3684         {
3685           (void) ThrowMagickException(exception,GetMagickModule(),
3686                OptionError,"SetReadOnlyProperty","'%s'",property);
3687           status=MagickFalse;
3688           break;
3689         }
3690     case 'P':
3691     case 'p':
3692     {
3693       if (LocaleCompare("page",property) == 0)
3694         {
3695           char
3696             *geometry;
3697
3698           geometry=GetPageGeometry(value);
3699           flags=ParseAbsoluteGeometry(geometry,&image->page);
3700           geometry=DestroyString(geometry);
3701           break;
3702         }
3703       if (LocaleCompare("profile",property) == 0)
3704         {
3705           ImageInfo
3706             *image_info;
3707
3708           StringInfo
3709             *profile;
3710
3711           image_info=AcquireImageInfo();
3712           (void) CopyMagickString(image_info->filename,value,MaxTextExtent);
3713           (void) SetImageInfo(image_info,1,exception);
3714           profile=FileToStringInfo(image_info->filename,~0UL,exception);
3715           if (profile != (StringInfo *) NULL)
3716             status=SetImageProfile(image,image_info->magick,profile,exception);
3717           image_info=DestroyImageInfo(image_info);
3718           break;
3719         }
3720       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3721         ConstantString(property),ConstantString(value));
3722       break;
3723     }
3724     case 'R':
3725     case 'r':
3726     {
3727       if (LocaleCompare("rendering-intent",property) == 0)
3728         {
3729           ssize_t
3730             rendering_intent;
3731
3732           rendering_intent=ParseCommandOption(MagickIntentOptions,MagickFalse,
3733             value);
3734           if (rendering_intent < 0)
3735             status=MagickFalse;
3736           else
3737             image->rendering_intent=(RenderingIntent) rendering_intent;
3738           break;
3739         }
3740       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3741         ConstantString(property),ConstantString(value));
3742       break;
3743     }
3744     case 'S':
3745     case 's':
3746       if ( (LocaleCompare("size",property) == 0) ||
3747            (LocaleCompare("skewness",property) == 0) ||
3748            (LocaleCompare("scenes",property) == 0) ||
3749            (LocaleCompare("standard-deviation",property) == 0) )
3750         {
3751           (void) ThrowMagickException(exception,GetMagickModule(),
3752                OptionError,"SetReadOnlyProperty","'%s'",property);
3753           status=MagickFalse;
3754           break;
3755         }
3756     case 'T':
3757     case 't':
3758     {
3759       if (LocaleCompare("tile-offset",property) == 0)
3760         {
3761           char
3762             *geometry;
3763
3764           geometry=GetPageGeometry(value);
3765           flags=ParseAbsoluteGeometry(geometry,&image->tile_offset);
3766           geometry=DestroyString(geometry);
3767           break;
3768         }
3769       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3770         ConstantString(property),ConstantString(value));
3771       break;
3772     }
3773     case 'U':
3774     case 'u':
3775     {
3776       if (LocaleCompare("units",property) == 0)
3777         {
3778           ssize_t
3779             units;
3780
3781           units=ParseCommandOption(MagickResolutionOptions,MagickFalse,value);
3782           if (units < 0)
3783             break;
3784           image->units=(ResolutionType) units;
3785           break;
3786         }
3787       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3788         ConstantString(property),ConstantString(value));
3789       break;
3790     }
3791     case 'V':
3792     case 'v':
3793       if (LocaleCompare("version",property) == 0)
3794         {
3795           (void) ThrowMagickException(exception,GetMagickModule(),
3796                OptionError,"SetReadOnlyProperty","'%s'",property);
3797           status=MagickFalse;
3798           break;
3799         }
3800     case 'W':
3801     case 'w':
3802       if (LocaleCompare("width",property) == 0)
3803         {
3804           (void) ThrowMagickException(exception,GetMagickModule(),
3805                OptionError,"SetReadOnlyProperty","'%s'",property);
3806           status=MagickFalse;
3807           break;
3808         }
3809     default:
3810     {
3811       status=AddValueToSplayTree((SplayTreeInfo *) image->properties,
3812         ConstantString(property),ConstantString(value));
3813       break;
3814     }
3815   }
3816   return(status);
3817 }