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