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