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