]> granicus.if.org Git - imagemagick/blob - MagickCore/image.c
(no commit message)
[imagemagick] / MagickCore / image.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                     IIIII  M   M   AAA    GGGG  EEEEE                       %
7 %                       I    MM MM  A   A  G      E                           %
8 %                       I    M M M  AAAAA  G  GG  EEE                         %
9 %                       I    M   M  A   A  G   G  E                           %
10 %                     IIIII  M   M  A   A   GGGG  EEEEE                       %
11 %                                                                             %
12 %                                                                             %
13 %                           MagickCore Image Methods                          %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 %
38 */
39 \f
40 /*
41   Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/animate.h"
45 #include "MagickCore/artifact.h"
46 #include "MagickCore/attribute.h"
47 #include "MagickCore/blob.h"
48 #include "MagickCore/blob-private.h"
49 #include "MagickCore/cache.h"
50 #include "MagickCore/cache-private.h"
51 #include "MagickCore/cache-view.h"
52 #include "MagickCore/client.h"
53 #include "MagickCore/color.h"
54 #include "MagickCore/color-private.h"
55 #include "MagickCore/colormap.h"
56 #include "MagickCore/colorspace.h"
57 #include "MagickCore/colorspace-private.h"
58 #include "MagickCore/composite.h"
59 #include "MagickCore/composite-private.h"
60 #include "MagickCore/compress.h"
61 #include "MagickCore/constitute.h"
62 #include "MagickCore/display.h"
63 #include "MagickCore/draw.h"
64 #include "MagickCore/enhance.h"
65 #include "MagickCore/exception.h"
66 #include "MagickCore/exception-private.h"
67 #include "MagickCore/gem.h"
68 #include "MagickCore/geometry.h"
69 #include "MagickCore/histogram.h"
70 #include "MagickCore/image-private.h"
71 #include "MagickCore/list.h"
72 #include "MagickCore/magic.h"
73 #include "MagickCore/magick.h"
74 #include "MagickCore/magick-private.h"
75 #include "MagickCore/memory_.h"
76 #include "MagickCore/module.h"
77 #include "MagickCore/monitor.h"
78 #include "MagickCore/monitor-private.h"
79 #include "MagickCore/option.h"
80 #include "MagickCore/paint.h"
81 #include "MagickCore/pixel-accessor.h"
82 #include "MagickCore/profile.h"
83 #include "MagickCore/property.h"
84 #include "MagickCore/quantize.h"
85 #include "MagickCore/random_.h"
86 #include "MagickCore/resource_.h"
87 #include "MagickCore/segment.h"
88 #include "MagickCore/semaphore.h"
89 #include "MagickCore/signature-private.h"
90 #include "MagickCore/statistic.h"
91 #include "MagickCore/string_.h"
92 #include "MagickCore/string-private.h"
93 #include "MagickCore/thread-private.h"
94 #include "MagickCore/threshold.h"
95 #include "MagickCore/timer.h"
96 #include "MagickCore/token.h"
97 #include "MagickCore/utility.h"
98 #include "MagickCore/utility-private.h"
99 #include "MagickCore/version.h"
100 #include "MagickCore/xwindow-private.h"
101 \f
102 /*
103   Constant declaration.
104 */
105 const char
106   BackgroundColor[] = "#ffffff",  /* white */
107   BorderColor[] = "#dfdfdf",  /* gray */
108   DefaultTileFrame[] = "15x15+3+3",
109   DefaultTileGeometry[] = "120x120+4+3>",
110   DefaultTileLabel[] = "%f\n%G\n%b",
111   ForegroundColor[] = "#000",  /* black */
112   LoadImageTag[] = "Load/Image",
113   LoadImagesTag[] = "Load/Images",
114   MatteColor[] = "#bdbdbd",  /* gray */
115   PSDensityGeometry[] = "72.0x72.0",
116   PSPageGeometry[] = "612x792",
117   SaveImageTag[] = "Save/Image",
118   SaveImagesTag[] = "Save/Images",
119   TransparentColor[] = "#00000000";  /* transparent black */
120
121 const double
122   DefaultResolution = 72.0;
123 \f
124 /*
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 %                                                                             %
127 %                                                                             %
128 %                                                                             %
129 %   A c q u i r e I m a g e                                                   %
130 %                                                                             %
131 %                                                                             %
132 %                                                                             %
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 %
135 %  AcquireImage() returns a pointer to an image structure initialized to
136 %  default values.
137 %
138 %  The format of the AcquireImage method is:
139 %
140 %      Image *AcquireImage(const ImageInfo *image_info,ExceptionInfo *exception)
141 %
142 %  A description of each parameter follows:
143 %
144 %    o image_info: Many of the image default values are set from this
145 %      structure.  For example, filename, compression, depth, background color,
146 %      and others.
147 %
148 %    o exception: return any errors or warnings in this structure.
149 %
150 */
151 MagickExport Image *AcquireImage(const ImageInfo *image_info,
152   ExceptionInfo *exception)
153 {
154   const char
155     *option;
156
157   Image
158     *image;
159
160   MagickStatusType
161     flags;
162
163   /*
164     Allocate image structure.
165   */
166   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
167   image=(Image *) AcquireMagickMemory(sizeof(*image));
168   if (image == (Image *) NULL)
169     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
170   (void) ResetMagickMemory(image,0,sizeof(*image));
171   /*
172     Initialize Image structure.
173   */
174   (void) CopyMagickString(image->magick,"MIFF",MaxTextExtent);
175   image->storage_class=DirectClass;
176   image->depth=MAGICKCORE_QUANTUM_DEPTH;
177   image->colorspace=sRGBColorspace;
178   image->rendering_intent=PerceptualIntent;
179   image->gamma=1.000f/2.200f;
180   image->chromaticity.red_primary.x=0.6400f;
181   image->chromaticity.red_primary.y=0.3300f;
182   image->chromaticity.red_primary.z=0.0300f;
183   image->chromaticity.green_primary.x=0.3000f;
184   image->chromaticity.green_primary.y=0.6000f;
185   image->chromaticity.green_primary.z=0.1000f;
186   image->chromaticity.blue_primary.x=0.1500f;
187   image->chromaticity.blue_primary.y=0.0600f;
188   image->chromaticity.blue_primary.z=0.7900f;
189   image->chromaticity.white_point.x=0.3127f;
190   image->chromaticity.white_point.y=0.3290f;
191   image->chromaticity.white_point.z=0.3583f;
192   image->interlace=NoInterlace;
193   image->ticks_per_second=UndefinedTicksPerSecond;
194   image->compose=OverCompositeOp;
195   (void) QueryColorCompliance(BackgroundColor,AllCompliance,
196     &image->background_color,exception);
197   (void) QueryColorCompliance(BorderColor,AllCompliance,&image->border_color,
198     exception);
199   (void) QueryColorCompliance(MatteColor,AllCompliance,&image->matte_color,
200     exception);
201   (void) QueryColorCompliance(TransparentColor,AllCompliance,
202     &image->transparent_color,exception);
203   GetTimerInfo(&image->timer);
204   image->cache=AcquirePixelCache(0);
205   image->channel_mask=DefaultChannels;
206   image->channel_map=AcquirePixelChannelMap();
207   image->blob=CloneBlobInfo((BlobInfo *) NULL);
208   image->debug=IsEventLogging();
209   image->reference_count=1;
210   image->semaphore=AllocateSemaphoreInfo();
211   image->signature=MagickSignature;
212   if (image_info == (ImageInfo *) NULL)
213     return(image);
214   /*
215     Transfer image info.
216   */
217   SetBlobExempt(image,image_info->file != (FILE *) NULL ? MagickTrue :
218     MagickFalse);
219   (void) CopyMagickString(image->filename,image_info->filename,MaxTextExtent);
220   (void) CopyMagickString(image->magick_filename,image_info->filename,
221     MaxTextExtent);
222   (void) CopyMagickString(image->magick,image_info->magick,MaxTextExtent);
223   if (image_info->size != (char *) NULL)
224     {
225       (void) ParseAbsoluteGeometry(image_info->size,&image->extract_info);
226       image->columns=image->extract_info.width;
227       image->rows=image->extract_info.height;
228       image->offset=image->extract_info.x;
229       image->extract_info.x=0;
230       image->extract_info.y=0;
231     }
232   if (image_info->extract != (char *) NULL)
233     {
234       RectangleInfo
235         geometry;
236
237       flags=ParseAbsoluteGeometry(image_info->extract,&geometry);
238       if (((flags & XValue) != 0) || ((flags & YValue) != 0))
239         {
240           image->extract_info=geometry;
241           Swap(image->columns,image->extract_info.width);
242           Swap(image->rows,image->extract_info.height);
243         }
244     }
245   image->compression=image_info->compression;
246   image->quality=image_info->quality;
247   image->endian=image_info->endian;
248   image->interlace=image_info->interlace;
249   image->units=image_info->units;
250   if (image_info->density != (char *) NULL)
251     {
252       GeometryInfo
253         geometry_info;
254
255       flags=ParseGeometry(image_info->density,&geometry_info);
256       image->resolution.x=geometry_info.rho;
257       image->resolution.y=geometry_info.sigma;
258       if ((flags & SigmaValue) == 0)
259         image->resolution.y=image->resolution.x;
260     }
261   if (image_info->page != (char *) NULL)
262     {
263       char
264         *geometry;
265
266       image->page=image->extract_info;
267       geometry=GetPageGeometry(image_info->page);
268       (void) ParseAbsoluteGeometry(geometry,&image->page);
269       geometry=DestroyString(geometry);
270     }
271   if (image_info->depth != 0)
272     image->depth=image_info->depth;
273   image->dither=image_info->dither;
274   image->background_color=image_info->background_color;
275   image->border_color=image_info->border_color;
276   image->matte_color=image_info->matte_color;
277   image->transparent_color=image_info->transparent_color;
278   image->ping=image_info->ping;
279   image->progress_monitor=image_info->progress_monitor;
280   image->client_data=image_info->client_data;
281   if (image_info->cache != (void *) NULL)
282     ClonePixelCacheMethods(image->cache,image_info->cache);
283
284   /* Set all global options that map to per-image settings */
285   (void) SyncImageSettings(image_info,image,exception);
286   /* At this point the image is not yet part of this image_info structure */
287   image->image_info = (ImageInfo *) NULL;
288
289   /* global options that are only set for new images */
290   option=GetImageOption(image_info,"delay");
291   if (option != (const char *) NULL)
292     {
293       GeometryInfo
294         geometry_info;
295
296       flags=ParseGeometry(option,&geometry_info);
297       if ((flags & GreaterValue) != 0)
298         {
299           if (image->delay > (size_t) floor(geometry_info.rho+0.5))
300             image->delay=(size_t) floor(geometry_info.rho+0.5);
301         }
302       else
303         if ((flags & LessValue) != 0)
304           {
305             if (image->delay < (size_t) floor(geometry_info.rho+0.5))
306               image->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
307           }
308         else
309           image->delay=(size_t) floor(geometry_info.rho+0.5);
310       if ((flags & SigmaValue) != 0)
311         image->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
312     }
313   option=GetImageOption(image_info,"dispose");
314   if (option != (const char *) NULL)
315     image->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
316       MagickFalse,option);
317   return(image);
318 }
319 \f
320 /*
321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 %                                                                             %
323 %                                                                             %
324 %                                                                             %
325 %   A c q u i r e I m a g e I n f o                                           %
326 %                                                                             %
327 %                                                                             %
328 %                                                                             %
329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 %
331 %  AcquireImageInfo() allocates the ImageInfo structure.
332 %
333 %  The format of the AcquireImageInfo method is:
334 %
335 %      ImageInfo *AcquireImageInfo(void)
336 %
337 */
338 MagickExport ImageInfo *AcquireImageInfo(void)
339 {
340   ImageInfo
341     *image_info;
342
343   image_info=(ImageInfo *) AcquireMagickMemory(sizeof(*image_info));
344   if (image_info == (ImageInfo *) NULL)
345     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
346   GetImageInfo(image_info);
347   return(image_info);
348 }
349 \f
350 /*
351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 %                                                                             %
353 %                                                                             %
354 %                                                                             %
355 %   A c q u i r e N e x t I m a g e                                           %
356 %                                                                             %
357 %                                                                             %
358 %                                                                             %
359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 %
361 %  AcquireNextImage() initializes the next image in a sequence to
362 %  default values.  The next member of image points to the newly allocated
363 %  image.  If there is a memory shortage, next is assigned NULL.
364 %
365 %  The format of the AcquireNextImage method is:
366 %
367 %      void AcquireNextImage(const ImageInfo *image_info,Image *image,
368 %        ExceptionInfo *exception)
369 %
370 %  A description of each parameter follows:
371 %
372 %    o image_info: Many of the image default values are set from this
373 %      structure.  For example, filename, compression, depth, background color,
374 %      and others.
375 %
376 %    o image: the image.
377 %
378 %    o exception: return any errors or warnings in this structure.
379 %
380 */
381 MagickExport void AcquireNextImage(const ImageInfo *image_info,Image *image,
382   ExceptionInfo *exception)
383 {
384   /*
385     Allocate image structure.
386   */
387   assert(image != (Image *) NULL);
388   assert(image->signature == MagickSignature);
389   if (image->debug != MagickFalse)
390     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
391   image->next=AcquireImage(image_info,exception);
392   if (GetNextImageInList(image) == (Image *) NULL)
393     return;
394   (void) CopyMagickString(GetNextImageInList(image)->filename,image->filename,
395     MaxTextExtent);
396   if (image_info != (ImageInfo *) NULL)
397     (void) CopyMagickString(GetNextImageInList(image)->filename,
398       image_info->filename,MaxTextExtent);
399   DestroyBlob(GetNextImageInList(image));
400   image->next->blob=ReferenceBlob(image->blob);
401   image->next->endian=image->endian;
402   image->next->scene=image->scene+1;
403   image->next->previous=image;
404 }
405 \f
406 /*
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 %                                                                             %
409 %                                                                             %
410 %                                                                             %
411 %     A p p e n d I m a g e s                                                 %
412 %                                                                             %
413 %                                                                             %
414 %                                                                             %
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 %
417 %  AppendImages() takes all images from the current image pointer to the end
418 %  of the image list and appends them to each other top-to-bottom if the
419 %  stack parameter is true, otherwise left-to-right.
420 %
421 %  The current gravity setting effects how the image is justified in the
422 %  final image.
423 %
424 %  The format of the AppendImages method is:
425 %
426 %      Image *AppendImages(const Image *images,const MagickBooleanType stack,
427 %        ExceptionInfo *exception)
428 %
429 %  A description of each parameter follows:
430 %
431 %    o images: the image sequence.
432 %
433 %    o stack: A value other than 0 stacks the images top-to-bottom.
434 %
435 %    o exception: return any errors or warnings in this structure.
436 %
437 */
438 MagickExport Image *AppendImages(const Image *images,
439   const MagickBooleanType stack,ExceptionInfo *exception)
440 {
441 #define AppendImageTag  "Append/Image"
442
443   CacheView
444     *append_view;
445
446   Image
447     *append_image;
448
449   MagickBooleanType
450     proceed,
451     status;
452
453   MagickOffsetType
454     n;
455
456   PixelTrait
457     alpha_trait;
458
459   RectangleInfo
460     geometry;
461
462   register const Image
463     *next;
464
465   size_t
466     height,
467     number_images,
468     width;
469
470   ssize_t
471     x_offset,
472     y,
473     y_offset;
474
475   /*
476     Compute maximum area of appended area.
477   */
478   assert(images != (Image *) NULL);
479   assert(images->signature == MagickSignature);
480   if (images->debug != MagickFalse)
481     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
482   assert(exception != (ExceptionInfo *) NULL);
483   assert(exception->signature == MagickSignature);
484   alpha_trait=images->alpha_trait;
485   number_images=1;
486   width=images->columns;
487   height=images->rows;
488   next=GetNextImageInList(images);
489   for ( ; next != (Image *) NULL; next=GetNextImageInList(next))
490   {
491     if (next->alpha_trait == BlendPixelTrait)
492       alpha_trait=BlendPixelTrait;
493     number_images++;
494     if (stack != MagickFalse)
495       {
496         if (next->columns > width)
497           width=next->columns;
498         height+=next->rows;
499         continue;
500       }
501     width+=next->columns;
502     if (next->rows > height)
503       height=next->rows;
504   }
505   /*
506     Append images.
507   */
508   append_image=CloneImage(images,width,height,MagickTrue,exception);
509   if (append_image == (Image *) NULL)
510     return((Image *) NULL);
511   if (SetImageStorageClass(append_image,DirectClass,exception) == MagickFalse)
512     {
513       append_image=DestroyImage(append_image);
514       return((Image *) NULL);
515     }
516   append_image->alpha_trait=alpha_trait;
517   (void) SetImageBackgroundColor(append_image,exception);
518   status=MagickTrue;
519   x_offset=0;
520   y_offset=0;
521   next=images;
522   append_view=AcquireAuthenticCacheView(append_image,exception);
523   for (n=0; n < (MagickOffsetType) number_images; n++)
524   {
525     CacheView
526       *image_view;
527
528     Image
529       *image;
530
531     image=CloneImage(next,0,0,MagickTrue,exception);
532     if (image == (Image *) NULL)
533       break;
534     (void) TransformImageColorspace(image,append_image->colorspace,exception);
535     SetGeometry(append_image,&geometry);
536     GravityAdjustGeometry(image->columns,image->rows,image->gravity,&geometry);
537     if (stack != MagickFalse)
538       x_offset-=geometry.x;
539     else
540       y_offset-=geometry.y;
541     image_view=AcquireVirtualCacheView(image,exception);
542 #if defined(MAGICKCORE_OPENMP_SUPPORT)
543     #pragma omp parallel for schedule(static,4) shared(status) \
544       magick_threads(image,image,image->rows,1)
545 #endif
546     for (y=0; y < (ssize_t) image->rows; y++)
547     {
548       MagickBooleanType
549         sync;
550
551       PixelInfo
552         pixel;
553
554       register const Quantum
555         *restrict p;
556
557       register Quantum
558         *restrict q;
559
560       register ssize_t
561         x;
562
563       if (status == MagickFalse)
564         continue;
565       p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
566       q=QueueCacheViewAuthenticPixels(append_view,x_offset,y+y_offset,
567         image->columns,1,exception);
568       if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
569         {
570           status=MagickFalse;
571           continue;
572         }
573       GetPixelInfo(image,&pixel);
574       for (x=0; x < (ssize_t) image->columns; x++)
575       {
576         if (GetPixelMask(image,p) != 0)
577           {
578             p+=GetPixelChannels(image);
579             q+=GetPixelChannels(append_image);
580             continue;
581           }
582         GetPixelInfoPixel(image,p,&pixel);
583         SetPixelInfoPixel(append_image,&pixel,q);
584         p+=GetPixelChannels(image);
585         q+=GetPixelChannels(append_image);
586       }
587       sync=SyncCacheViewAuthenticPixels(append_view,exception);
588       if (sync == MagickFalse)
589         status=MagickFalse;
590     }
591     image_view=DestroyCacheView(image_view);
592     if (stack == MagickFalse)
593       {
594         x_offset+=(ssize_t) image->columns;
595         y_offset=0;
596       }
597     else
598       {
599         x_offset=0;
600         y_offset+=(ssize_t) image->rows;
601       }
602     image=DestroyImage(image);
603     proceed=SetImageProgress(append_image,AppendImageTag,n,number_images);
604     if (proceed == MagickFalse)
605       break;
606     next=GetNextImageInList(next);
607   }
608   append_view=DestroyCacheView(append_view);
609   if (status == MagickFalse)
610     append_image=DestroyImage(append_image);
611   return(append_image);
612 }
613 \f
614 /*
615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
616 %                                                                             %
617 %                                                                             %
618 %                                                                             %
619 %   C a t c h I m a g e E x c e p t i o n                                     %
620 %                                                                             %
621 %                                                                             %
622 %                                                                             %
623 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 %
625 %  CatchImageException() returns if no exceptions are found in the image
626 %  sequence, otherwise it determines the most severe exception and reports
627 %  it as a warning or error depending on the severity.
628 %
629 %  The format of the CatchImageException method is:
630 %
631 %      ExceptionType CatchImageException(Image *image)
632 %
633 %  A description of each parameter follows:
634 %
635 %    o image: An image sequence.
636 %
637 */
638 MagickExport ExceptionType CatchImageException(Image *image)
639 {
640   ExceptionInfo
641     *exception;
642
643   ExceptionType
644     severity;
645
646   assert(image != (const Image *) NULL);
647   assert(image->signature == MagickSignature);
648   if (image->debug != MagickFalse)
649     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
650   exception=AcquireExceptionInfo();
651   CatchException(exception);
652   severity=exception->severity;
653   exception=DestroyExceptionInfo(exception);
654   return(severity);
655 }
656 \f
657 /*
658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659 %                                                                             %
660 %                                                                             %
661 %                                                                             %
662 %   C l i p I m a g e P a t h                                                 %
663 %                                                                             %
664 %                                                                             %
665 %                                                                             %
666 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667 %
668 %  ClipImagePath() sets the image clip mask based any clipping path information
669 %  if it exists.
670 %
671 %  The format of the ClipImagePath method is:
672 %
673 %      MagickBooleanType ClipImagePath(Image *image,const char *pathname,
674 %        const MagickBooleanType inside,ExceptionInfo *exception)
675 %
676 %  A description of each parameter follows:
677 %
678 %    o image: the image.
679 %
680 %    o pathname: name of clipping path resource. If name is preceded by #, use
681 %      clipping path numbered by name.
682 %
683 %    o inside: if non-zero, later operations take effect inside clipping path.
684 %      Otherwise later operations take effect outside clipping path.
685 %
686 %    o exception: return any errors or warnings in this structure.
687 %
688 */
689
690 MagickExport MagickBooleanType ClipImage(Image *image,ExceptionInfo *exception)
691 {
692   return(ClipImagePath(image,"#1",MagickTrue,exception));
693 }
694
695 MagickExport MagickBooleanType ClipImagePath(Image *image,const char *pathname,
696   const MagickBooleanType inside,ExceptionInfo *exception)
697 {
698 #define ClipImagePathTag  "ClipPath/Image"
699
700   char
701     *property;
702
703   const char
704     *value;
705
706   Image
707     *clip_mask;
708
709   ImageInfo
710     *image_info;
711
712   assert(image != (const Image *) NULL);
713   assert(image->signature == MagickSignature);
714   if (image->debug != MagickFalse)
715     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
716   assert(pathname != NULL);
717   property=AcquireString(pathname);
718   (void) FormatLocaleString(property,MaxTextExtent,"8BIM:1999,2998:%s",
719     pathname);
720   value=GetImageProperty(image,property,exception);
721   property=DestroyString(property);
722   if (value == (const char *) NULL)
723     {
724       ThrowFileException(exception,OptionError,"NoClipPathDefined",
725         image->filename);
726       return(MagickFalse);
727     }
728   image_info=AcquireImageInfo();
729   (void) CopyMagickString(image_info->filename,image->filename,MaxTextExtent);
730   (void) ConcatenateMagickString(image_info->filename,pathname,MaxTextExtent);
731   clip_mask=BlobToImage(image_info,value,strlen(value),exception);
732   image_info=DestroyImageInfo(image_info);
733   if (clip_mask == (Image *) NULL)
734     return(MagickFalse);
735   if (clip_mask->storage_class == PseudoClass)
736     {
737       (void) SyncImage(clip_mask,exception);
738       if (SetImageStorageClass(clip_mask,DirectClass,exception) == MagickFalse)
739         return(MagickFalse);
740     }
741   if (inside == MagickFalse)
742     (void) NegateImage(clip_mask,MagickFalse,exception);
743   (void) FormatLocaleString(clip_mask->magick_filename,MaxTextExtent,
744     "8BIM:1999,2998:%s\nPS",pathname);
745   (void) SetImageMask(image,clip_mask,exception);
746   clip_mask=DestroyImage(clip_mask);
747   return(MagickTrue);
748 }
749 \f
750 /*
751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752 %                                                                             %
753 %                                                                             %
754 %                                                                             %
755 %   C l o n e I m a g e                                                       %
756 %                                                                             %
757 %                                                                             %
758 %                                                                             %
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 %
761 %  CloneImage() copies an image and returns the copy as a new image object.
762 %
763 %  If the specified columns and rows is 0, an exact copy of the image is
764 %  returned, otherwise the pixel data is undefined and must be initialized
765 %  with the QueueAuthenticPixels() and SyncAuthenticPixels() methods.  On
766 %  failure, a NULL image is returned and exception describes the reason for the
767 %  failure.
768 %
769 %  The format of the CloneImage method is:
770 %
771 %      Image *CloneImage(const Image *image,const size_t columns,
772 %        const size_t rows,const MagickBooleanType orphan,
773 %        ExceptionInfo *exception)
774 %
775 %  A description of each parameter follows:
776 %
777 %    o image: the image.
778 %
779 %    o columns: the number of columns in the cloned image.
780 %
781 %    o rows: the number of rows in the cloned image.
782 %
783 %    o detach:  With a value other than 0, the cloned image is detached from
784 %      its parent I/O stream.
785 %
786 %    o exception: return any errors or warnings in this structure.
787 %
788 */
789 MagickExport Image *CloneImage(const Image *image,const size_t columns,
790   const size_t rows,const MagickBooleanType detach,ExceptionInfo *exception)
791 {
792   Image
793     *clone_image;
794
795   double
796     scale;
797
798   size_t
799     length;
800
801   /*
802     Clone the image.
803   */
804   assert(image != (const Image *) NULL);
805   assert(image->signature == MagickSignature);
806   if (image->debug != MagickFalse)
807     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
808   assert(exception != (ExceptionInfo *) NULL);
809   assert(exception->signature == MagickSignature);
810   clone_image=(Image *) AcquireMagickMemory(sizeof(*clone_image));
811   if (clone_image == (Image *) NULL)
812     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
813   (void) ResetMagickMemory(clone_image,0,sizeof(*clone_image));
814   clone_image->signature=MagickSignature;
815   clone_image->storage_class=image->storage_class;
816   clone_image->number_channels=image->number_channels;
817   clone_image->number_meta_channels=image->number_meta_channels;
818   clone_image->metacontent_extent=image->metacontent_extent;
819   clone_image->colorspace=image->colorspace;
820   clone_image->mask=image->mask;
821   clone_image->alpha_trait=image->alpha_trait;
822   clone_image->columns=image->columns;
823   clone_image->rows=image->rows;
824   clone_image->dither=image->dither;
825   if (image->colormap != (PixelInfo *) NULL)
826     {
827       /*
828         Allocate and copy the image colormap.
829       */
830       clone_image->colors=image->colors;
831       length=(size_t) image->colors;
832       clone_image->colormap=(PixelInfo *) AcquireQuantumMemory(length,
833         sizeof(*clone_image->colormap));
834       if (clone_image->colormap == (PixelInfo *) NULL)
835         ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
836       (void) CopyMagickMemory(clone_image->colormap,image->colormap,length*
837         sizeof(*clone_image->colormap));
838     }
839   (void) CloneImageProfiles(clone_image,image);
840   (void) CloneImageProperties(clone_image,image);
841   (void) CloneImageArtifacts(clone_image,image);
842   GetTimerInfo(&clone_image->timer);
843   if (image->ascii85 != (void *) NULL)
844     Ascii85Initialize(clone_image);
845   clone_image->magick_columns=image->magick_columns;
846   clone_image->magick_rows=image->magick_rows;
847   clone_image->type=image->type;
848   clone_image->channel_mask=image->channel_mask;
849   clone_image->channel_map=ClonePixelChannelMap(image->channel_map);
850   (void) CopyMagickString(clone_image->magick_filename,image->magick_filename,
851     MaxTextExtent);
852   (void) CopyMagickString(clone_image->magick,image->magick,MaxTextExtent);
853   (void) CopyMagickString(clone_image->filename,image->filename,MaxTextExtent);
854   clone_image->progress_monitor=image->progress_monitor;
855   clone_image->client_data=image->client_data;
856   clone_image->reference_count=1;
857   clone_image->next=image->next;
858   clone_image->previous=image->previous;
859   clone_image->list=NewImageList();
860   if (detach == MagickFalse)
861     clone_image->blob=ReferenceBlob(image->blob);
862   else
863     {
864       clone_image->next=NewImageList();
865       clone_image->previous=NewImageList();
866       clone_image->blob=CloneBlobInfo((BlobInfo *) NULL);
867     }
868   clone_image->ping=image->ping;
869   clone_image->debug=IsEventLogging();
870   clone_image->semaphore=AllocateSemaphoreInfo();
871   if ((columns == 0) && (rows == 0))
872     {
873       if (image->montage != (char *) NULL)
874         (void) CloneString(&clone_image->montage,image->montage);
875       if (image->directory != (char *) NULL)
876         (void) CloneString(&clone_image->directory,image->directory);
877       clone_image->cache=ReferencePixelCache(image->cache);
878       return(clone_image);
879     }
880   scale=(double) columns/(double) image->columns;
881   clone_image->page.width=(size_t) floor(scale*image->page.width+0.5);
882   clone_image->page.x=(ssize_t) ceil(scale*image->page.x-0.5);
883   clone_image->tile_offset.x=(ssize_t) ceil(scale*image->tile_offset.x-0.5);
884   scale=(double) rows/(double) image->rows;
885   clone_image->page.height=(size_t) floor(scale*image->page.height+0.5);
886   clone_image->page.y=(ssize_t) ceil(scale*image->page.y-0.5);
887   clone_image->tile_offset.y=(ssize_t) ceil(scale*image->tile_offset.y-0.5);
888   clone_image->columns=columns;
889   clone_image->rows=rows;
890   clone_image->cache=ClonePixelCache(image->cache);
891   return(clone_image);
892 }
893 \f
894 /*
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896 %                                                                             %
897 %                                                                             %
898 %                                                                             %
899 %   C l o n e I m a g e I n f o                                               %
900 %                                                                             %
901 %                                                                             %
902 %                                                                             %
903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 %
905 %  CloneImageInfo() makes a copy of the given image info structure.  If
906 %  NULL is specified, a new image info structure is created initialized to
907 %  default values.
908 %
909 %  The format of the CloneImageInfo method is:
910 %
911 %      ImageInfo *CloneImageInfo(const ImageInfo *image_info)
912 %
913 %  A description of each parameter follows:
914 %
915 %    o image_info: the image info.
916 %
917 */
918 MagickExport ImageInfo *CloneImageInfo(const ImageInfo *image_info)
919 {
920   ImageInfo
921     *clone_info;
922
923   clone_info=AcquireImageInfo();
924   if (image_info == (ImageInfo *) NULL)
925     return(clone_info);
926   clone_info->compression=image_info->compression;
927   clone_info->temporary=image_info->temporary;
928   clone_info->adjoin=image_info->adjoin;
929   clone_info->antialias=image_info->antialias;
930   clone_info->scene=image_info->scene;
931   clone_info->number_scenes=image_info->number_scenes;
932   clone_info->depth=image_info->depth;
933   (void) CloneString(&clone_info->size,image_info->size);
934   (void) CloneString(&clone_info->extract,image_info->extract);
935   (void) CloneString(&clone_info->scenes,image_info->scenes);
936   (void) CloneString(&clone_info->page,image_info->page);
937   clone_info->interlace=image_info->interlace;
938   clone_info->endian=image_info->endian;
939   clone_info->units=image_info->units;
940   clone_info->quality=image_info->quality;
941   (void) CloneString(&clone_info->sampling_factor,image_info->sampling_factor);
942   (void) CloneString(&clone_info->server_name,image_info->server_name);
943   (void) CloneString(&clone_info->font,image_info->font);
944   (void) CloneString(&clone_info->texture,image_info->texture);
945   (void) CloneString(&clone_info->density,image_info->density);
946   clone_info->pointsize=image_info->pointsize;
947   clone_info->fuzz=image_info->fuzz;
948   clone_info->background_color=image_info->background_color;
949   clone_info->border_color=image_info->border_color;
950   clone_info->matte_color=image_info->matte_color;
951   clone_info->transparent_color=image_info->transparent_color;
952   clone_info->dither=image_info->dither;
953   clone_info->monochrome=image_info->monochrome;
954   clone_info->colorspace=image_info->colorspace;
955   clone_info->type=image_info->type;
956   clone_info->orientation=image_info->orientation;
957   clone_info->preview_type=image_info->preview_type;
958   clone_info->group=image_info->group;
959   clone_info->ping=image_info->ping;
960   clone_info->verbose=image_info->verbose;
961   (void) CloneString(&clone_info->view,image_info->view);
962   clone_info->progress_monitor=image_info->progress_monitor;
963   clone_info->client_data=image_info->client_data;
964   clone_info->cache=image_info->cache;
965   if (image_info->cache != (void *) NULL)
966     clone_info->cache=ReferencePixelCache(image_info->cache);
967   if (image_info->profile != (void *) NULL)
968     clone_info->profile=(void *) CloneStringInfo((StringInfo *)
969       image_info->profile);
970   SetImageInfoFile(clone_info,image_info->file);
971   SetImageInfoBlob(clone_info,image_info->blob,image_info->length);
972   clone_info->stream=image_info->stream;
973   (void) CopyMagickString(clone_info->magick,image_info->magick,MaxTextExtent);
974   (void) CopyMagickString(clone_info->unique,image_info->unique,MaxTextExtent);
975   (void) CopyMagickString(clone_info->zero,image_info->zero,MaxTextExtent);
976   (void) CopyMagickString(clone_info->filename,image_info->filename,
977     MaxTextExtent);
978   clone_info->channel=image_info->channel;
979   (void) CloneImageOptions(clone_info,image_info);
980   clone_info->debug=IsEventLogging();
981   clone_info->signature=image_info->signature;
982   return(clone_info);
983 }
984 \f
985 /*
986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 %                                                                             %
988 %                                                                             %
989 %                                                                             %
990 %   D e s t r o y I m a g e                                                   %
991 %                                                                             %
992 %                                                                             %
993 %                                                                             %
994 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 %
996 %  DestroyImage() dereferences an image, deallocating memory associated with
997 %  the image if the reference count becomes zero.
998 %
999 %  The format of the DestroyImage method is:
1000 %
1001 %      Image *DestroyImage(Image *image)
1002 %
1003 %  A description of each parameter follows:
1004 %
1005 %    o image: the image.
1006 %
1007 */
1008 MagickExport Image *DestroyImage(Image *image)
1009 {
1010   MagickBooleanType
1011     destroy;
1012
1013   /*
1014     Dereference image.
1015   */
1016   assert(image != (Image *) NULL);
1017   assert(image->signature == MagickSignature);
1018   if (image->debug != MagickFalse)
1019     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1020   destroy=MagickFalse;
1021   LockSemaphoreInfo(image->semaphore);
1022   image->reference_count--;
1023   if (image->reference_count == 0)
1024     destroy=MagickTrue;
1025   UnlockSemaphoreInfo(image->semaphore);
1026   if (destroy == MagickFalse)
1027     return((Image *) NULL);
1028   /*
1029     Destroy image.
1030   */
1031   DestroyImagePixels(image);
1032   image->channel_map=DestroyPixelChannelMap(image->channel_map);
1033   if (image->montage != (char *) NULL)
1034     image->montage=DestroyString(image->montage);
1035   if (image->directory != (char *) NULL)
1036     image->directory=DestroyString(image->directory);
1037   if (image->colormap != (PixelInfo *) NULL)
1038     image->colormap=(PixelInfo *) RelinquishMagickMemory(image->colormap);
1039   if (image->geometry != (char *) NULL)
1040     image->geometry=DestroyString(image->geometry);
1041   DestroyImageProfiles(image);
1042   DestroyImageProperties(image);
1043   DestroyImageArtifacts(image);
1044   if (image->ascii85 != (Ascii85Info*) NULL)
1045     image->ascii85=(Ascii85Info *) RelinquishMagickMemory(image->ascii85);
1046   DestroyBlob(image);
1047   if (image->semaphore != (SemaphoreInfo *) NULL)
1048     DestroySemaphoreInfo(&image->semaphore);
1049   image->signature=(~MagickSignature);
1050   image=(Image *) RelinquishMagickMemory(image);
1051   return(image);
1052 }
1053 \f
1054 /*
1055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1056 %                                                                             %
1057 %                                                                             %
1058 %                                                                             %
1059 %   D e s t r o y I m a g e I n f o                                           %
1060 %                                                                             %
1061 %                                                                             %
1062 %                                                                             %
1063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1064 %
1065 %  DestroyImageInfo() deallocates memory associated with an ImageInfo
1066 %  structure.
1067 %
1068 %  The format of the DestroyImageInfo method is:
1069 %
1070 %      ImageInfo *DestroyImageInfo(ImageInfo *image_info)
1071 %
1072 %  A description of each parameter follows:
1073 %
1074 %    o image_info: the image info.
1075 %
1076 */
1077 MagickExport ImageInfo *DestroyImageInfo(ImageInfo *image_info)
1078 {
1079   assert(image_info != (ImageInfo *) NULL);
1080   assert(image_info->signature == MagickSignature);
1081   if (image_info->debug != MagickFalse)
1082     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1083       image_info->filename);
1084   if (image_info->size != (char *) NULL)
1085     image_info->size=DestroyString(image_info->size);
1086   if (image_info->extract != (char *) NULL)
1087     image_info->extract=DestroyString(image_info->extract);
1088   if (image_info->scenes != (char *) NULL)
1089     image_info->scenes=DestroyString(image_info->scenes);
1090   if (image_info->page != (char *) NULL)
1091     image_info->page=DestroyString(image_info->page);
1092   if (image_info->sampling_factor != (char *) NULL)
1093     image_info->sampling_factor=DestroyString(
1094       image_info->sampling_factor);
1095   if (image_info->server_name != (char *) NULL)
1096     image_info->server_name=DestroyString(
1097       image_info->server_name);
1098   if (image_info->font != (char *) NULL)
1099     image_info->font=DestroyString(image_info->font);
1100   if (image_info->texture != (char *) NULL)
1101     image_info->texture=DestroyString(image_info->texture);
1102   if (image_info->density != (char *) NULL)
1103     image_info->density=DestroyString(image_info->density);
1104   if (image_info->view != (char *) NULL)
1105     image_info->view=DestroyString(image_info->view);
1106   if (image_info->cache != (void *) NULL)
1107     image_info->cache=DestroyPixelCache(image_info->cache);
1108   if (image_info->profile != (StringInfo *) NULL)
1109     image_info->profile=(void *) DestroyStringInfo((StringInfo *)
1110       image_info->profile);
1111   DestroyImageOptions(image_info);
1112   image_info->signature=(~MagickSignature);
1113   image_info=(ImageInfo *) RelinquishMagickMemory(image_info);
1114   return(image_info);
1115 }
1116 \f
1117 /*
1118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1119 %                                                                             %
1120 %                                                                             %
1121 %                                                                             %
1122 +   D i s a s s o c i a t e I m a g e S t r e a m                             %
1123 %                                                                             %
1124 %                                                                             %
1125 %                                                                             %
1126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1127 %
1128 %  DisassociateImageStream() disassociates the image stream.
1129 %
1130 %  The format of the DisassociateImageStream method is:
1131 %
1132 %      MagickBooleanType DisassociateImageStream(const Image *image)
1133 %
1134 %  A description of each parameter follows:
1135 %
1136 %    o image: the image.
1137 %
1138 */
1139 MagickExport void DisassociateImageStream(Image *image)
1140 {
1141   assert(image != (const Image *) NULL);
1142   assert(image->signature == MagickSignature);
1143   if (image->debug != MagickFalse)
1144     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1145   (void) DetachBlob(image->blob);
1146 }
1147 \f
1148 /*
1149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1150 %                                                                             %
1151 %                                                                             %
1152 %                                                                             %
1153 %   G e t I m a g e A l p h a C h a n n e l                                   %
1154 %                                                                             %
1155 %                                                                             %
1156 %                                                                             %
1157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1158 %
1159 %  GetImageAlphaChannel() returns MagickFalse if the image alpha channel is
1160 %  not activated.  That is, the image is RGB rather than RGBA or CMYK rather
1161 %  than CMYKA.
1162 %
1163 %  The format of the GetImageAlphaChannel method is:
1164 %
1165 %      MagickBooleanType GetImageAlphaChannel(const Image *image)
1166 %
1167 %  A description of each parameter follows:
1168 %
1169 %    o image: the image.
1170 %
1171 */
1172 MagickExport MagickBooleanType GetImageAlphaChannel(const Image *image)
1173 {
1174   assert(image != (const Image *) NULL);
1175   if (image->debug != MagickFalse)
1176     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1177   assert(image->signature == MagickSignature);
1178   return(image->alpha_trait == BlendPixelTrait ? MagickTrue : MagickFalse);
1179 }
1180 \f
1181 /*
1182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183 %                                                                             %
1184 %                                                                             %
1185 %                                                                             %
1186 %   G e t I m a g e I n f o                                                   %
1187 %                                                                             %
1188 %                                                                             %
1189 %                                                                             %
1190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1191 %
1192 %  GetImageInfo() initializes image_info to default values.
1193 %
1194 %  The format of the GetImageInfo method is:
1195 %
1196 %      void GetImageInfo(ImageInfo *image_info)
1197 %
1198 %  A description of each parameter follows:
1199 %
1200 %    o image_info: the image info.
1201 %
1202 */
1203 MagickExport void GetImageInfo(ImageInfo *image_info)
1204 {
1205   char
1206     *synchronize;
1207
1208   ExceptionInfo
1209     *exception;
1210
1211   /*
1212     File and image dimension members.
1213   */
1214   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1215   assert(image_info != (ImageInfo *) NULL);
1216   (void) ResetMagickMemory(image_info,0,sizeof(*image_info));
1217   image_info->adjoin=MagickTrue;
1218   image_info->interlace=NoInterlace;
1219   image_info->channel=DefaultChannels;
1220   image_info->quality=UndefinedCompressionQuality;
1221   image_info->antialias=MagickTrue;
1222   image_info->dither=MagickTrue;
1223   synchronize=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
1224   if (synchronize != (const char *) NULL)
1225     {
1226       image_info->synchronize=IsStringTrue(synchronize);
1227       synchronize=DestroyString(synchronize);
1228     }
1229   exception=AcquireExceptionInfo();
1230   (void) QueryColorCompliance(BackgroundColor,AllCompliance,
1231     &image_info->background_color,exception);
1232   (void) QueryColorCompliance(BorderColor,AllCompliance,
1233     &image_info->border_color,exception);
1234   (void) QueryColorCompliance(MatteColor,AllCompliance,&image_info->matte_color,
1235     exception);
1236   (void) QueryColorCompliance(TransparentColor,AllCompliance,
1237     &image_info->transparent_color,exception);
1238   exception=DestroyExceptionInfo(exception);
1239   image_info->debug=IsEventLogging();
1240   image_info->signature=MagickSignature;
1241 }
1242 \f
1243 /*
1244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245 %                                                                             %
1246 %                                                                             %
1247 %                                                                             %
1248 %   G e t I m a g e I n f o F i l e                                           %
1249 %                                                                             %
1250 %                                                                             %
1251 %                                                                             %
1252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253 %
1254 %  GetImageInfoFile() returns the image info file member.
1255 %
1256 %  The format of the GetImageInfoFile method is:
1257 %
1258 %      FILE *GetImageInfoFile(const ImageInfo *image_info)
1259 %
1260 %  A description of each parameter follows:
1261 %
1262 %    o image_info: the image info.
1263 %
1264 */
1265 MagickExport FILE *GetImageInfoFile(const ImageInfo *image_info)
1266 {
1267   return(image_info->file);
1268 }
1269 \f
1270 /*
1271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1272 %                                                                             %
1273 %                                                                             %
1274 %                                                                             %
1275 %   G e t I m a g e M a s k                                                   %
1276 %                                                                             %
1277 %                                                                             %
1278 %                                                                             %
1279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1280 %
1281 %  GetImageMask() returns the mask associated with the image.
1282 %
1283 %  The format of the GetImageMask method is:
1284 %
1285 %      Image *GetImageMask(const Image *image,ExceptionInfo *exception)
1286 %
1287 %  A description of each parameter follows:
1288 %
1289 %    o image: the image.
1290 %
1291 */
1292 MagickExport Image *GetImageMask(const Image *image,ExceptionInfo *exception)
1293 {
1294   CacheView
1295     *mask_view,
1296     *image_view;
1297
1298   Image
1299     *mask_image;
1300
1301   MagickBooleanType
1302     status;
1303
1304   ssize_t
1305     y;
1306
1307   /*
1308     Get image mask.
1309   */
1310   assert(image != (Image *) NULL);
1311   if (image->debug != MagickFalse)
1312     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1313   assert(image->signature == MagickSignature);
1314   mask_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1315   if (mask_image == (Image *) NULL)
1316     return((Image *) NULL);
1317   status=MagickTrue;
1318   (void) SetImageColorspace(mask_image,GRAYColorspace,exception);
1319   mask_image->mask=MagickFalse;
1320   image_view=AcquireVirtualCacheView(image,exception);
1321   mask_view=AcquireAuthenticCacheView(mask_image,exception);
1322   for (y=0; y < (ssize_t) image->rows; y++)
1323   {
1324     register const Quantum
1325       *restrict p;
1326
1327     register Quantum
1328       *restrict q;
1329
1330     register ssize_t
1331       x;
1332
1333     if (status == MagickFalse)
1334       continue;
1335     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1336     q=GetCacheViewAuthenticPixels(mask_view,0,y,mask_image->columns,1,
1337       exception);
1338     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1339       {
1340         status=MagickFalse;
1341         continue;
1342       }
1343     for (x=0; x < (ssize_t) image->columns; x++)
1344     {
1345       SetPixelGray(mask_image,GetPixelMask(image,p),q);
1346       p+=GetPixelChannels(image);
1347       q+=GetPixelChannels(mask_image);
1348     }
1349     if (SyncCacheViewAuthenticPixels(mask_view,exception) == MagickFalse)
1350       status=MagickFalse;
1351   }
1352   mask_view=DestroyCacheView(mask_view);
1353   image_view=DestroyCacheView(image_view);
1354   if (status == MagickFalse)
1355     mask_image=DestroyImage(mask_image);
1356   return(mask_image);
1357 }
1358 \f
1359 /*
1360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1361 %                                                                             %
1362 %                                                                             %
1363 %                                                                             %
1364 +   G e t I m a g e R e f e r e n c e C o u n t                               %
1365 %                                                                             %
1366 %                                                                             %
1367 %                                                                             %
1368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1369 %
1370 %  GetImageReferenceCount() returns the image reference count.
1371 %
1372 %  The format of the GetReferenceCount method is:
1373 %
1374 %      ssize_t GetImageReferenceCount(Image *image)
1375 %
1376 %  A description of each parameter follows:
1377 %
1378 %    o image: the image.
1379 %
1380 */
1381 MagickExport ssize_t GetImageReferenceCount(Image *image)
1382 {
1383   ssize_t
1384     reference_count;
1385
1386   assert(image != (Image *) NULL);
1387   assert(image->signature == MagickSignature);
1388   if (image->debug != MagickFalse)
1389     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1390   LockSemaphoreInfo(image->semaphore);
1391   reference_count=image->reference_count;
1392   UnlockSemaphoreInfo(image->semaphore);
1393   return(reference_count);
1394 }
1395 \f
1396 /*
1397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1398 %                                                                             %
1399 %                                                                             %
1400 %                                                                             %
1401 %   G e t I m a g e V i r t u a l P i x e l M e t h o d                       %
1402 %                                                                             %
1403 %                                                                             %
1404 %                                                                             %
1405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1406 %
1407 %  GetImageVirtualPixelMethod() gets the "virtual pixels" method for the
1408 %  image.  A virtual pixel is any pixel access that is outside the boundaries
1409 %  of the image cache.
1410 %
1411 %  The format of the GetImageVirtualPixelMethod() method is:
1412 %
1413 %      VirtualPixelMethod GetImageVirtualPixelMethod(const Image *image)
1414 %
1415 %  A description of each parameter follows:
1416 %
1417 %    o image: the image.
1418 %
1419 */
1420 MagickExport VirtualPixelMethod GetImageVirtualPixelMethod(const Image *image)
1421 {
1422   assert(image != (Image *) NULL);
1423   assert(image->signature == MagickSignature);
1424   if (image->debug != MagickFalse)
1425     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1426   return(GetPixelCacheVirtualMethod(image));
1427 }
1428 \f
1429 /*
1430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431 %                                                                             %
1432 %                                                                             %
1433 %                                                                             %
1434 %  I n t e r p r e t I m a g e F i l e n a m e                                %
1435 %                                                                             %
1436 %                                                                             %
1437 %                                                                             %
1438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1439 %
1440 %  InterpretImageFilename() interprets embedded characters in an image filename.
1441 %  The filename length is returned.
1442 %
1443 %  The format of the InterpretImageFilename method is:
1444 %
1445 %      size_t InterpretImageFilename(const ImageInfo *image_info,Image *image,
1446 %        const char *format,int value,char *filename,ExceptionInfo *exception)
1447 %
1448 %  A description of each parameter follows.
1449 %
1450 %    o image_info: the image info..
1451 %
1452 %    o image: the image.
1453 %
1454 %    o format:  A filename describing the format to use to write the numeric
1455 %      argument. Only the first numeric format identifier is replaced.
1456 %
1457 %    o value:  Numeric value to substitute into format filename.
1458 %
1459 %    o filename:  return the formatted filename in this character buffer.
1460 %
1461 %    o exception: return any errors or warnings in this structure.
1462 %
1463 */
1464 MagickExport size_t InterpretImageFilename(const ImageInfo *image_info,
1465   Image *image,const char *format,int value,char *filename,
1466   ExceptionInfo *exception)
1467 {
1468   char
1469     *q;
1470
1471   int
1472     c;
1473
1474   MagickBooleanType
1475     canonical;
1476
1477   register const char
1478     *p;
1479
1480   size_t
1481     length;
1482
1483   canonical=MagickFalse;
1484   length=0;
1485   (void) CopyMagickString(filename,format,MaxTextExtent);
1486   for (p=strchr(format,'%'); p != (char *) NULL; p=strchr(p+1,'%'))
1487   {
1488     q=(char *) p+1;
1489     if (*q == '%')
1490       {
1491         p=q+1;
1492         continue;
1493       }
1494     if (*q == '0')
1495       {
1496         ssize_t
1497           value;
1498
1499         value=(ssize_t) strtol(q,&q,10);
1500         (void) value;
1501       }
1502     switch (*q)
1503     {
1504       case 'd':
1505       case 'o':
1506       case 'x':
1507       {
1508         q++;
1509         c=(*q);
1510         *q='\0';
1511         (void) FormatLocaleString(filename+(p-format),(size_t) (MaxTextExtent-
1512           (p-format)),p,value);
1513         *q=c;
1514         (void) ConcatenateMagickString(filename,q,MaxTextExtent);
1515         canonical=MagickTrue;
1516         if (*(q-1) != '%')
1517           break;
1518         p++;
1519         break;
1520       }
1521       case '[':
1522       {
1523         char
1524           pattern[MaxTextExtent];
1525
1526         const char
1527           *value;
1528
1529         register char
1530           *r;
1531
1532         register ssize_t
1533           i;
1534
1535         ssize_t
1536           depth;
1537
1538         /*
1539           Image option.
1540         */
1541         /* FUTURE: Compare update with code from InterpretImageProperties()
1542            Note that a 'filename:' property should not need depth recursion.
1543         */
1544         if (strchr(p,']') == (char *) NULL)
1545           break;
1546         depth=1;
1547         r=q+1;
1548         for (i=0; (i < (MaxTextExtent-1L)) && (*r != '\0'); i++)
1549         {
1550           if (*r == '[')
1551             depth++;
1552           if (*r == ']')
1553             depth--;
1554           if (depth <= 0)
1555             break;
1556           pattern[i]=(*r++);
1557         }
1558         pattern[i]='\0';
1559         if (LocaleNCompare(pattern,"filename:",9) != 0)
1560           break;
1561         value=(const char *) NULL;
1562 #if 0
1563         // FUTURE: remove this code. -- Anthony  29 Arpil 2012
1564         // Removed as GetMagickProperty() will will never match a "filename:"
1565         // string as this is not a 'known' image property.
1566         //
1567         if ((image_info != (const ImageInfo *) NULL) &&
1568             (image != (const Image *) NULL))
1569           value=GetMagickProperty(image_info,image,pattern,exception);
1570         else
1571 #endif
1572         if (image != (Image *) NULL)
1573           value=GetImageProperty(image,pattern,exception);
1574         if ((value == (const char *) NULL) &&
1575             (image != (Image *) NULL))
1576           value=GetImageArtifact(image,pattern);
1577         if ((value == (const char *) NULL) &&
1578             (image_info != (ImageInfo *) NULL))
1579           value=GetImageOption(image_info,pattern);
1580         if (value == (const char *) NULL)
1581           break;
1582         q--;
1583         c=(*q);
1584         *q='\0';
1585         (void) CopyMagickString(filename+(p-format-length),value,(size_t)
1586           (MaxTextExtent-(p-format-length)));
1587         length+=strlen(pattern)-1;
1588         *q=c;
1589         (void) ConcatenateMagickString(filename,r+1,MaxTextExtent);
1590         canonical=MagickTrue;
1591         if (*(q-1) != '%')
1592           break;
1593         p++;
1594         break;
1595       }
1596       default:
1597         break;
1598     }
1599   }
1600   for (q=filename; *q != '\0'; q++)
1601     if ((*q == '%') && (*(q+1) == '%'))
1602       {
1603         (void) CopyMagickString(q,q+1,(size_t) (MaxTextExtent-(q-filename)));
1604         canonical=MagickTrue;
1605       }
1606   if (canonical == MagickFalse)
1607     (void) CopyMagickString(filename,format,MaxTextExtent);
1608   return(strlen(filename));
1609 }
1610 \f
1611 /*
1612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1613 %                                                                             %
1614 %                                                                             %
1615 %                                                                             %
1616 %   I s H i g h D y n a m i c R a n g e I m a g e                             %
1617 %                                                                             %
1618 %                                                                             %
1619 %                                                                             %
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 %
1622 %  IsHighDynamicRangeImage() returns MagickTrue if any pixel component is
1623 %  non-integer or exceeds the bounds of the quantum depth (e.g. for Q16
1624 %  0..65535.
1625 %
1626 %  The format of the IsHighDynamicRangeImage method is:
1627 %
1628 %      MagickBooleanType IsHighDynamicRangeImage(const Image *image,
1629 %        ExceptionInfo *exception)
1630 %
1631 %  A description of each parameter follows:
1632 %
1633 %    o image: the image.
1634 %
1635 %    o exception: return any errors or warnings in this structure.
1636 %
1637 */
1638 MagickExport MagickBooleanType IsHighDynamicRangeImage(const Image *image,
1639   ExceptionInfo *exception)
1640 {
1641 #if !defined(MAGICKCORE_HDRI_SUPPORT)
1642   (void) image;
1643   (void) exception;
1644   return(MagickFalse);
1645 #else
1646   CacheView
1647     *image_view;
1648
1649   MagickBooleanType
1650     status;
1651
1652   ssize_t
1653     y;
1654
1655   assert(image != (Image *) NULL);
1656   assert(image->signature == MagickSignature);
1657   if (image->debug != MagickFalse)
1658     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1659   status=MagickTrue;
1660   image_view=AcquireVirtualCacheView(image,exception);
1661 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1662   #pragma omp parallel for schedule(static,4) shared(status) \
1663     magick_threads(image,image,image->rows,1)
1664 #endif
1665   for (y=0; y < (ssize_t) image->rows; y++)
1666   {
1667     register const Quantum
1668       *p;
1669
1670     register ssize_t
1671       x;
1672
1673     if (status == MagickFalse)
1674       continue;
1675     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1676     if (p == (const Quantum *) NULL)
1677       {
1678         status=MagickFalse;
1679         continue;
1680       }
1681     for (x=0; x < (ssize_t) image->columns; x++)
1682     {
1683       register ssize_t
1684         i;
1685
1686       if (GetPixelMask(image,p) != 0)
1687         {
1688           p+=GetPixelChannels(image);
1689           continue;
1690         }
1691       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1692       {
1693         double
1694           pixel;
1695
1696         PixelTrait
1697           traits;
1698
1699         traits=GetPixelChannelTraits(image,(PixelChannel) i);
1700         if (traits == UndefinedPixelTrait)
1701           continue;
1702         pixel=(double) p[i];
1703         if ((pixel < 0.0) || (pixel > QuantumRange) ||
1704             (pixel != (QuantumAny) pixel))
1705           break;
1706       }
1707       p+=GetPixelChannels(image);
1708       if (i < (ssize_t) GetPixelChannels(image))
1709         status=MagickFalse;
1710     }
1711     if (x < (ssize_t) image->columns)
1712       status=MagickFalse;
1713   }
1714   image_view=DestroyCacheView(image_view);
1715   return(status != MagickFalse ? MagickFalse : MagickTrue);
1716 #endif
1717 }
1718 \f
1719 /*
1720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721 %                                                                             %
1722 %                                                                             %
1723 %                                                                             %
1724 %     I s I m a g e O b j e c t                                               %
1725 %                                                                             %
1726 %                                                                             %
1727 %                                                                             %
1728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1729 %
1730 %  IsImageObject() returns MagickTrue if the image sequence contains a valid
1731 %  set of image objects.
1732 %
1733 %  The format of the IsImageObject method is:
1734 %
1735 %      MagickBooleanType IsImageObject(const Image *image)
1736 %
1737 %  A description of each parameter follows:
1738 %
1739 %    o image: the image.
1740 %
1741 */
1742 MagickExport MagickBooleanType IsImageObject(const Image *image)
1743 {
1744   register const Image
1745     *p;
1746
1747   assert(image != (Image *) NULL);
1748   if (image->debug != MagickFalse)
1749     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1750   for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1751     if (p->signature != MagickSignature)
1752       return(MagickFalse);
1753   return(MagickTrue);
1754 }
1755 \f
1756 /*
1757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1758 %                                                                             %
1759 %                                                                             %
1760 %                                                                             %
1761 %     I s T a i n t I m a g e                                                 %
1762 %                                                                             %
1763 %                                                                             %
1764 %                                                                             %
1765 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1766 %
1767 %  IsTaintImage() returns MagickTrue any pixel in the image has been altered
1768 %  since it was first constituted.
1769 %
1770 %  The format of the IsTaintImage method is:
1771 %
1772 %      MagickBooleanType IsTaintImage(const Image *image)
1773 %
1774 %  A description of each parameter follows:
1775 %
1776 %    o image: the image.
1777 %
1778 */
1779 MagickExport MagickBooleanType IsTaintImage(const Image *image)
1780 {
1781   char
1782     magick[MaxTextExtent],
1783     filename[MaxTextExtent];
1784
1785   register const Image
1786     *p;
1787
1788   assert(image != (Image *) NULL);
1789   if (image->debug != MagickFalse)
1790     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1791   assert(image->signature == MagickSignature);
1792   (void) CopyMagickString(magick,image->magick,MaxTextExtent);
1793   (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1794   for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1795   {
1796     if (p->taint != MagickFalse)
1797       return(MagickTrue);
1798     if (LocaleCompare(p->magick,magick) != 0)
1799       return(MagickTrue);
1800     if (LocaleCompare(p->filename,filename) != 0)
1801       return(MagickTrue);
1802   }
1803   return(MagickFalse);
1804 }
1805 \f
1806 /*
1807 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1808 %                                                                             %
1809 %                                                                             %
1810 %                                                                             %
1811 %   M o d i f y I m a g e                                                     %
1812 %                                                                             %
1813 %                                                                             %
1814 %                                                                             %
1815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1816 %
1817 %  ModifyImage() ensures that there is only a single reference to the image
1818 %  to be modified, updating the provided image pointer to point to a clone of
1819 %  the original image if necessary.
1820 %
1821 %  The format of the ModifyImage method is:
1822 %
1823 %      MagickBooleanType ModifyImage(Image *image,ExceptionInfo *exception)
1824 %
1825 %  A description of each parameter follows:
1826 %
1827 %    o image: the image.
1828 %
1829 %    o exception: return any errors or warnings in this structure.
1830 %
1831 */
1832 MagickExport MagickBooleanType ModifyImage(Image **image,
1833   ExceptionInfo *exception)
1834 {
1835   Image
1836     *clone_image;
1837
1838   assert(image != (Image **) NULL);
1839   assert(*image != (Image *) NULL);
1840   assert((*image)->signature == MagickSignature);
1841   if ((*image)->debug != MagickFalse)
1842     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
1843   if (GetImageReferenceCount(*image) <= 1)
1844     return(MagickTrue);
1845   clone_image=CloneImage(*image,0,0,MagickTrue,exception);
1846   LockSemaphoreInfo((*image)->semaphore);
1847   (*image)->reference_count--;
1848   UnlockSemaphoreInfo((*image)->semaphore);
1849   *image=clone_image;
1850   return(MagickTrue);
1851 }
1852 \f
1853 /*
1854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1855 %                                                                             %
1856 %                                                                             %
1857 %                                                                             %
1858 %   N e w M a g i c k I m a g e                                               %
1859 %                                                                             %
1860 %                                                                             %
1861 %                                                                             %
1862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1863 %
1864 %  NewMagickImage() creates a blank image canvas of the specified size and
1865 %  background color.
1866 %
1867 %  The format of the NewMagickImage method is:
1868 %
1869 %      Image *NewMagickImage(const ImageInfo *image_info,
1870 %        const size_t width,const size_t height,const PixelInfo *background,
1871 %        ExceptionInfo *exception)
1872 %
1873 %  A description of each parameter follows:
1874 %
1875 %    o image: the image.
1876 %
1877 %    o width: the image width.
1878 %
1879 %    o height: the image height.
1880 %
1881 %    o background: the image color.
1882 %
1883 %    o exception: return any errors or warnings in this structure.
1884 %
1885 */
1886 MagickExport Image *NewMagickImage(const ImageInfo *image_info,
1887   const size_t width,const size_t height,const PixelInfo *background,
1888   ExceptionInfo *exception)
1889 {
1890   CacheView
1891     *image_view;
1892
1893   Image
1894     *image;
1895
1896   MagickBooleanType
1897     status;
1898
1899   ssize_t
1900     y;
1901
1902   assert(image_info != (const ImageInfo *) NULL);
1903   if (image_info->debug != MagickFalse)
1904     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1905   assert(image_info->signature == MagickSignature);
1906   assert(background != (const PixelInfo *) NULL);
1907   image=AcquireImage(image_info,exception);
1908   image->columns=width;
1909   image->rows=height;
1910   image->colorspace=background->colorspace;
1911   image->alpha_trait=background->alpha_trait;
1912   image->fuzz=background->fuzz;
1913   image->depth=background->depth;
1914   status=MagickTrue;
1915   image_view=AcquireAuthenticCacheView(image,exception);
1916 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1917   #pragma omp parallel for schedule(static,4) shared(status) \
1918     magick_threads(image,image,image->rows,1)
1919 #endif
1920   for (y=0; y < (ssize_t) image->rows; y++)
1921   {
1922     register Quantum
1923       *restrict q;
1924
1925     register ssize_t
1926       x;
1927
1928     if (status == MagickFalse)
1929       continue;
1930     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1931     if (q == (Quantum *) NULL)
1932       {
1933         status=MagickFalse;
1934         continue;
1935       }
1936     for (x=0; x < (ssize_t) image->columns; x++)
1937     {
1938       SetPixelInfoPixel(image,background,q);
1939       q+=GetPixelChannels(image);
1940     }
1941     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1942       status=MagickFalse;
1943   }
1944   image_view=DestroyCacheView(image_view);
1945   if (status == MagickFalse)
1946     image=DestroyImage(image);
1947   return(image);
1948 }
1949 \f
1950 /*
1951 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1952 %                                                                             %
1953 %                                                                             %
1954 %                                                                             %
1955 %   R e f e r e n c e I m a g e                                               %
1956 %                                                                             %
1957 %                                                                             %
1958 %                                                                             %
1959 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1960 %
1961 %  ReferenceImage() increments the reference count associated with an image
1962 %  returning a pointer to the image.
1963 %
1964 %  The format of the ReferenceImage method is:
1965 %
1966 %      Image *ReferenceImage(Image *image)
1967 %
1968 %  A description of each parameter follows:
1969 %
1970 %    o image: the image.
1971 %
1972 */
1973 MagickExport Image *ReferenceImage(Image *image)
1974 {
1975   assert(image != (Image *) NULL);
1976   if (image->debug != MagickFalse)
1977     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1978   assert(image->signature == MagickSignature);
1979   LockSemaphoreInfo(image->semaphore);
1980   image->reference_count++;
1981   UnlockSemaphoreInfo(image->semaphore);
1982   return(image);
1983 }
1984 \f
1985 /*
1986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1987 %                                                                             %
1988 %                                                                             %
1989 %                                                                             %
1990 %   R e s e t I m a g e P a g e                                               %
1991 %                                                                             %
1992 %                                                                             %
1993 %                                                                             %
1994 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1995 %
1996 %  ResetImagePage() resets the image page canvas and position.
1997 %
1998 %  The format of the ResetImagePage method is:
1999 %
2000 %      MagickBooleanType ResetImagePage(Image *image,const char *page)
2001 %
2002 %  A description of each parameter follows:
2003 %
2004 %    o image: the image.
2005 %
2006 %    o page: the relative page specification.
2007 %
2008 */
2009 MagickExport MagickBooleanType ResetImagePage(Image *image,const char *page)
2010 {
2011   MagickStatusType
2012     flags;
2013
2014   RectangleInfo
2015     geometry;
2016
2017   assert(image != (Image *) NULL);
2018   assert(image->signature == MagickSignature);
2019   if (image->debug != MagickFalse)
2020     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2021   flags=ParseAbsoluteGeometry(page,&geometry);
2022   if ((flags & WidthValue) != 0)
2023     {
2024       if ((flags & HeightValue) == 0)
2025         geometry.height=geometry.width;
2026       image->page.width=geometry.width;
2027       image->page.height=geometry.height;
2028     }
2029   if ((flags & AspectValue) != 0)
2030     {
2031       if ((flags & XValue) != 0)
2032         image->page.x+=geometry.x;
2033       if ((flags & YValue) != 0)
2034         image->page.y+=geometry.y;
2035     }
2036   else
2037     {
2038       if ((flags & XValue) != 0)
2039         {
2040           image->page.x=geometry.x;
2041           if ((image->page.width == 0) && (geometry.x > 0))
2042             image->page.width=image->columns+geometry.x;
2043         }
2044       if ((flags & YValue) != 0)
2045         {
2046           image->page.y=geometry.y;
2047           if ((image->page.height == 0) && (geometry.y > 0))
2048             image->page.height=image->rows+geometry.y;
2049         }
2050     }
2051   return(MagickTrue);
2052 }
2053 \f
2054 /*
2055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2056 %                                                                             %
2057 %                                                                             %
2058 %                                                                             %
2059 %   S e t I m a g e A l p h a C h a n n e l                                   %
2060 %                                                                             %
2061 %                                                                             %
2062 %                                                                             %
2063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2064 %
2065 %  SetImageAlphaChannel() activates, deactivates, resets, or sets the alpha
2066 %  channel.
2067 %
2068 %  The format of the SetImageAlphaChannel method is:
2069 %
2070 %      MagickBooleanType SetImageAlphaChannel(Image *image,
2071 %        const AlphaChannelOption alpha_type,ExceptionInfo *exception)
2072 %
2073 %  A description of each parameter follows:
2074 %
2075 %    o image: the image.
2076 %
2077 %    o alpha_type:  The alpha channel type: ActivateAlphaChannel,
2078 %      CopyAlphaChannel, DeactivateAlphaChannel, ExtractAlphaChannel,
2079 %      OpaqueAlphaChannel, SetAlphaChannel, ShapeAlphaChannel, and
2080 %      TransparentAlphaChannel.
2081 %
2082 %    o exception: return any errors or warnings in this structure.
2083 %
2084 */
2085
2086 static inline void FlattenPixelInfo(const Image *image,const PixelInfo *p,
2087   const double alpha,const Quantum *q,const double beta,
2088   Quantum *composite)
2089 {
2090   double
2091     Da,
2092     gamma,
2093     Sa;
2094
2095   register ssize_t
2096     i;
2097
2098   /*
2099     Compose pixel p over pixel q with the given alpha.
2100   */
2101   Sa=QuantumScale*alpha;
2102   Da=QuantumScale*beta,
2103   gamma=Sa*(-Da)+Sa+Da;
2104   gamma=PerceptibleReciprocal(gamma);
2105   for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2106   {
2107     PixelChannel
2108       channel;
2109
2110     PixelTrait
2111       traits;
2112
2113     channel=GetPixelChannelChannel(image,i);
2114     traits=GetPixelChannelTraits(image,channel);
2115     if (traits == UndefinedPixelTrait)
2116       continue;
2117     switch (channel)
2118     {
2119       case RedPixelChannel:
2120       {
2121         composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
2122           (double) p->red,alpha));
2123         break;
2124       }
2125       case GreenPixelChannel:
2126       {
2127         composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
2128           (double) p->green,alpha));
2129         break;
2130       }
2131       case BluePixelChannel:
2132       {
2133         composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
2134           (double) p->blue,alpha));
2135         break;
2136       }
2137       case BlackPixelChannel:
2138       {
2139         composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
2140           (double) p->black,alpha));
2141         break;
2142       }
2143       case AlphaPixelChannel:
2144       {
2145         composite[i]=ClampToQuantum(QuantumRange*(Sa*(-Da)+Sa+Da));
2146         break;
2147       }
2148       default:
2149         break;
2150     }
2151   }
2152 }
2153
2154 MagickExport MagickBooleanType SetImageAlphaChannel(Image *image,
2155   const AlphaChannelOption alpha_type,ExceptionInfo *exception)
2156 {
2157   MagickBooleanType
2158     status;
2159
2160   assert(image != (Image *) NULL);
2161   if (image->debug != MagickFalse)
2162     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2163   assert(image->signature == MagickSignature);
2164   status=MagickTrue;
2165   switch (alpha_type)
2166   {
2167     case ActivateAlphaChannel:
2168     {
2169       image->alpha_trait=BlendPixelTrait;
2170       break;
2171     }
2172     case BackgroundAlphaChannel:
2173     {
2174       CacheView
2175         *image_view;
2176
2177       ssize_t
2178         y;
2179
2180       /*
2181         Set transparent pixels to background color.
2182       */
2183       if (image->alpha_trait != BlendPixelTrait)
2184         break;
2185       if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2186         break;
2187       image_view=AcquireAuthenticCacheView(image,exception);
2188 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2189       #pragma omp parallel for schedule(static,4) shared(status) \
2190         magick_threads(image,image,image->rows,1)
2191 #endif
2192       for (y=0; y < (ssize_t) image->rows; y++)
2193       {
2194         register Quantum
2195           *restrict q;
2196
2197         register ssize_t
2198           x;
2199
2200         if (status == MagickFalse)
2201           continue;
2202         q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
2203           exception);
2204         if (q == (Quantum *) NULL)
2205           {
2206             status=MagickFalse;
2207             continue;
2208           }
2209         for (x=0; x < (ssize_t) image->columns; x++)
2210         {
2211           if (GetPixelAlpha(image,q) == TransparentAlpha)
2212             SetPixelInfoPixel(image,&image->background_color,q);
2213           q+=GetPixelChannels(image);
2214         }
2215         if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2216           status=MagickFalse;
2217       }
2218       image_view=DestroyCacheView(image_view);
2219       return(status);
2220     }
2221     case CopyAlphaChannel:
2222     case ShapeAlphaChannel:
2223     {
2224       /*
2225         Copy pixel intensity to the alpha channel.
2226       */
2227       status=CompositeImage(image,image,IntensityCompositeOp,MagickTrue,0,0,
2228         exception);
2229       if (alpha_type == ShapeAlphaChannel)
2230         (void) LevelImageColors(image,&image->background_color,
2231           &image->background_color,MagickTrue,exception);
2232       break;
2233     }
2234     case DeactivateAlphaChannel:
2235     {
2236       image->alpha_trait=CopyPixelTrait;
2237       break;
2238     }
2239     case ExtractAlphaChannel:
2240     {
2241       status=CompositeImage(image,image,AlphaCompositeOp,MagickTrue,0,0,
2242         exception);
2243       image->alpha_trait=UndefinedPixelTrait;
2244       break;
2245     }
2246     case OpaqueAlphaChannel:
2247     {
2248       status=SetImageAlpha(image,OpaqueAlpha,exception);
2249       break;
2250     }
2251     case RemoveAlphaChannel:
2252     {
2253       CacheView
2254         *image_view;
2255
2256       ssize_t
2257         y;
2258
2259       /*
2260         Remove transparency.
2261       */
2262       if (image->alpha_trait != BlendPixelTrait)
2263         break;
2264       if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2265         break;
2266       image_view=AcquireAuthenticCacheView(image,exception);
2267 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2268       #pragma omp parallel for schedule(static,4) shared(status) \
2269         magick_threads(image,image,image->rows,1)
2270 #endif
2271       for (y=0; y < (ssize_t) image->rows; y++)
2272       {
2273         register Quantum
2274           *restrict q;
2275
2276         register ssize_t
2277           x;
2278
2279         if (status == MagickFalse)
2280           continue;
2281         q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
2282           exception);
2283         if (q == (Quantum *) NULL)
2284           {
2285             status=MagickFalse;
2286             continue;
2287           }
2288         for (x=0; x < (ssize_t) image->columns; x++)
2289         {
2290           FlattenPixelInfo(image,&image->background_color,
2291             image->background_color.alpha,q,(double)
2292             GetPixelAlpha(image,q),q);
2293           q+=GetPixelChannels(image);
2294         }
2295         if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2296           status=MagickFalse;
2297       }
2298       image_view=DestroyCacheView(image_view);
2299       image->alpha_trait=image->background_color.alpha_trait;
2300       return(status);
2301     }
2302     case SetAlphaChannel:
2303     {
2304       if (image->alpha_trait != BlendPixelTrait)
2305         status=SetImageAlpha(image,OpaqueAlpha,exception);
2306       break;
2307     }
2308     case TransparentAlphaChannel:
2309     {
2310       status=SetImageAlpha(image,TransparentAlpha,exception);
2311       break;
2312     }
2313     case UndefinedAlphaChannel:
2314       break;
2315   }
2316   if (status == MagickFalse)
2317     return(status);
2318   return(SyncImagePixelCache(image,exception));
2319 }
2320 \f
2321 /*
2322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2323 %                                                                             %
2324 %                                                                             %
2325 %                                                                             %
2326 %   S e t I m a g e B a c k g r o u n d C o l o r                             %
2327 %                                                                             %
2328 %                                                                             %
2329 %                                                                             %
2330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2331 %
2332 %  SetImageBackgroundColor() initializes the image pixels to the image
2333 %  background color.  The background color is defined by the background_color
2334 %  member of the image structure.
2335 %
2336 %  The format of the SetImage method is:
2337 %
2338 %      MagickBooleanType SetImageBackgroundColor(Image *image,
2339 %        ExceptionInfo *exception)
2340 %
2341 %  A description of each parameter follows:
2342 %
2343 %    o image: the image.
2344 %
2345 %    o exception: return any errors or warnings in this structure.
2346 %
2347 */
2348 MagickExport MagickBooleanType SetImageBackgroundColor(Image *image,
2349   ExceptionInfo *exception)
2350 {
2351   CacheView
2352     *image_view;
2353
2354   MagickBooleanType
2355     status;
2356
2357   ssize_t
2358     y;
2359
2360   assert(image != (Image *) NULL);
2361   if (image->debug != MagickFalse)
2362     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2363   assert(image->signature == MagickSignature);
2364   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2365     return(MagickFalse);
2366   if ((IsPixelInfoGray(&image->background_color) == MagickFalse) &&
2367       (IsGrayColorspace(image->colorspace) != MagickFalse))
2368     (void) TransformImageColorspace(image,RGBColorspace,exception);
2369   if ((image->background_color.alpha_trait == BlendPixelTrait) &&
2370       (image->alpha_trait != BlendPixelTrait))
2371     (void) SetImageAlpha(image,OpaqueAlpha,exception);
2372   /*
2373     Set image background color.
2374   */
2375   status=MagickTrue;
2376   image_view=AcquireAuthenticCacheView(image,exception);
2377   for (y=0; y < (ssize_t) image->rows; y++)
2378   {
2379     register Quantum
2380       *restrict q;
2381
2382     register ssize_t
2383       x;
2384
2385     if (status == MagickFalse)
2386       continue;
2387     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2388     if (q == (Quantum *) NULL)
2389       {
2390         status=MagickFalse;
2391         continue;
2392       }
2393     for (x=0; x < (ssize_t) image->columns; x++)
2394     {
2395       SetPixelInfoPixel(image,&image->background_color,q);
2396       q+=GetPixelChannels(image);
2397     }
2398     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2399       status=MagickFalse;
2400   }
2401   image_view=DestroyCacheView(image_view);
2402   return(status);
2403 }
2404 \f
2405 /*
2406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2407 %                                                                             %
2408 %                                                                             %
2409 %                                                                             %
2410 %   S e t I m a g e C h a n n e l M a s k                                     %
2411 %                                                                             %
2412 %                                                                             %
2413 %                                                                             %
2414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2415 %
2416 %  SetImageChannelMask() sets the image channel mask from the specified channel
2417 %  mask.
2418 %
2419 %  The format of the SetImageChannelMask method is:
2420 %
2421 %      ChannelType SetImageChannelMask(Image *image,
2422 %        const ChannelType channel_mask)
2423 %
2424 %  A description of each parameter follows:
2425 %
2426 %    o image: the image.
2427 %
2428 %    o channel_mask: the channel mask.
2429 %
2430 */
2431 MagickExport ChannelType SetImageChannelMask(Image *image,
2432   const ChannelType channel_mask)
2433 {
2434   ChannelType
2435     mask;
2436
2437   mask=image->channel_mask;
2438   image->channel_mask=channel_mask;
2439   SetPixelChannelMask(image,channel_mask);
2440   return(mask);
2441 }
2442 \f
2443 /*
2444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2445 %                                                                             %
2446 %                                                                             %
2447 %                                                                             %
2448 %   S e t I m a g e C o l o r                                                 %
2449 %                                                                             %
2450 %                                                                             %
2451 %                                                                             %
2452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2453 %
2454 %  SetImageColor() set the entire image canvas to the specified color.
2455 %
2456 %  The format of the SetImageColor method is:
2457 %
2458 %      MagickBooleanType SetImageColor(Image *image,const PixelInfo *color,
2459 %        ExeptionInfo *exception)
2460 %
2461 %  A description of each parameter follows:
2462 %
2463 %    o image: the image.
2464 %
2465 %    o background: the image color.
2466 %
2467 %    o exception: return any errors or warnings in this structure.
2468 %
2469 */
2470 MagickExport MagickBooleanType SetImageColor(Image *image,
2471   const PixelInfo *color,ExceptionInfo *exception)
2472 {
2473   CacheView
2474     *image_view;
2475
2476   MagickBooleanType
2477     status;
2478
2479   ssize_t
2480     y;
2481
2482   assert(image != (Image *) NULL);
2483   if (image->debug != MagickFalse)
2484     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2485   assert(image->signature == MagickSignature);
2486   assert(color != (const PixelInfo *) NULL);
2487   image->colorspace=color->colorspace;
2488   image->alpha_trait=color->alpha_trait;
2489   image->fuzz=color->fuzz;
2490   image->depth=color->depth;
2491   status=MagickTrue;
2492   image_view=AcquireAuthenticCacheView(image,exception);
2493 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2494   #pragma omp parallel for schedule(static,4) shared(status) \
2495     magick_threads(image,image,image->rows,1)
2496 #endif
2497   for (y=0; y < (ssize_t) image->rows; y++)
2498   {
2499     register Quantum
2500       *restrict q;
2501
2502     register ssize_t
2503       x;
2504
2505     if (status == MagickFalse)
2506       continue;
2507     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2508     if (q == (Quantum *) NULL)
2509       {
2510         status=MagickFalse;
2511         continue;
2512       }
2513     for (x=0; x < (ssize_t) image->columns; x++)
2514     {
2515       SetPixelInfoPixel(image,color,q);
2516       q+=GetPixelChannels(image);
2517     }
2518     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2519       status=MagickFalse;
2520   }
2521   image_view=DestroyCacheView(image_view);
2522   return(status);
2523 }
2524 \f
2525 /*
2526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2527 %                                                                             %
2528 %                                                                             %
2529 %                                                                             %
2530 %   S e t I m a g e S t o r a g e C l a s s                                   %
2531 %                                                                             %
2532 %                                                                             %
2533 %                                                                             %
2534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2535 %
2536 %  SetImageStorageClass() sets the image class: DirectClass for true color
2537 %  images or PseudoClass for colormapped images.
2538 %
2539 %  The format of the SetImageStorageClass method is:
2540 %
2541 %      MagickBooleanType SetImageStorageClass(Image *image,
2542 %        const ClassType storage_class,ExceptionInfo *exception)
2543 %
2544 %  A description of each parameter follows:
2545 %
2546 %    o image: the image.
2547 %
2548 %    o storage_class:  The image class.
2549 %
2550 %    o exception: return any errors or warnings in this structure.
2551 %
2552 */
2553 MagickExport MagickBooleanType SetImageStorageClass(Image *image,
2554   const ClassType storage_class,ExceptionInfo *exception)
2555 {
2556   image->storage_class=storage_class;
2557   return(SyncImagePixelCache(image,exception));
2558 }
2559 \f
2560 /*
2561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2562 %                                                                             %
2563 %                                                                             %
2564 %                                                                             %
2565 %   S e t I m a g e E x t e n t                                               %
2566 %                                                                             %
2567 %                                                                             %
2568 %                                                                             %
2569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2570 %
2571 %  SetImageExtent() sets the image size (i.e. columns & rows).
2572 %
2573 %  The format of the SetImageExtent method is:
2574 %
2575 %      MagickBooleanType SetImageExtent(Image *image,const size_t columns,
2576 %        const size_t rows,ExceptionInfo *exception)
2577 %
2578 %  A description of each parameter follows:
2579 %
2580 %    o image: the image.
2581 %
2582 %    o columns:  The image width in pixels.
2583 %
2584 %    o rows:  The image height in pixels.
2585 %
2586 %    o exception: return any errors or warnings in this structure.
2587 %
2588 */
2589 MagickExport MagickBooleanType SetImageExtent(Image *image,const size_t columns,
2590   const size_t rows,ExceptionInfo *exception)
2591 {
2592   if ((columns == 0) || (rows == 0))
2593     return(MagickFalse);
2594   image->columns=columns;
2595   image->rows=rows;
2596   return(SyncImagePixelCache(image,exception));
2597 }
2598 \f
2599 /*
2600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2601 %                                                                             %
2602 %                                                                             %
2603 %                                                                             %
2604 +   S e t I m a g e I n f o                                                   %
2605 %                                                                             %
2606 %                                                                             %
2607 %                                                                             %
2608 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2609 %
2610 %  SetImageInfo() initializes the 'magick' field of the ImageInfo structure.
2611 %  It is set to a type of image format based on the prefix or suffix of the
2612 %  filename.  For example, 'ps:image' returns PS indicating a Postscript image.
2613 %  JPEG is returned for this filename: 'image.jpg'.  The filename prefix has
2614 %  precendence over the suffix.  Use an optional index enclosed in brackets
2615 %  after a file name to specify a desired scene of a multi-resolution image
2616 %  format like Photo CD (e.g. img0001.pcd[4]).  A True (non-zero) return value
2617 %  indicates success.
2618 %
2619 %  The format of the SetImageInfo method is:
2620 %
2621 %      MagickBooleanType SetImageInfo(ImageInfo *image_info,
2622 %        const unsigned int frames,ExceptionInfo *exception)
2623 %
2624 %  A description of each parameter follows:
2625 %
2626 %    o image_info: the image info.
2627 %
2628 %    o frames: the number of images you intend to write.
2629 %
2630 %    o exception: return any errors or warnings in this structure.
2631 %
2632 */
2633 MagickExport MagickBooleanType SetImageInfo(ImageInfo *image_info,
2634   const unsigned int frames,ExceptionInfo *exception)
2635 {
2636   char
2637     extension[MaxTextExtent],
2638     filename[MaxTextExtent],
2639     magic[MaxTextExtent],
2640     *q,
2641     subimage[MaxTextExtent];
2642
2643   const MagicInfo
2644     *magic_info;
2645
2646   const MagickInfo
2647     *magick_info;
2648
2649   ExceptionInfo
2650     *sans_exception;
2651
2652   Image
2653     *image;
2654
2655   MagickBooleanType
2656     status;
2657
2658   register const char
2659     *p;
2660
2661   ssize_t
2662     count;
2663
2664   unsigned char
2665     magick[2*MaxTextExtent];
2666
2667   /*
2668     Look for 'image.format' in filename.
2669   */
2670   assert(image_info != (ImageInfo *) NULL);
2671   assert(image_info->signature == MagickSignature);
2672   if (image_info->debug != MagickFalse)
2673     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2674       image_info->filename);
2675   *subimage='\0';
2676   if (frames == 0)
2677     {
2678       GetPathComponent(image_info->filename,SubimagePath,subimage);
2679       if (*subimage != '\0')
2680         {
2681           /*
2682             Look for scene specification (e.g. img0001.pcd[4]).
2683           */
2684           if (IsSceneGeometry(subimage,MagickFalse) == MagickFalse)
2685             {
2686               if (IsGeometry(subimage) != MagickFalse)
2687                 (void) CloneString(&image_info->extract,subimage);
2688             }
2689           else
2690             {
2691               size_t
2692                 first,
2693                 last;
2694
2695               (void) CloneString(&image_info->scenes,subimage);
2696               image_info->scene=StringToUnsignedLong(image_info->scenes);
2697               image_info->number_scenes=image_info->scene;
2698               p=image_info->scenes;
2699               for (q=(char *) image_info->scenes; *q != '\0'; p++)
2700               {
2701                 while ((isspace((int) ((unsigned char) *p)) != 0) ||
2702                        (*p == ','))
2703                   p++;
2704                 first=(size_t) strtol(p,&q,10);
2705                 last=first;
2706                 while (isspace((int) ((unsigned char) *q)) != 0)
2707                   q++;
2708                 if (*q == '-')
2709                   last=(size_t) strtol(q+1,&q,10);
2710                 if (first > last)
2711                   Swap(first,last);
2712                 if (first < image_info->scene)
2713                   image_info->scene=first;
2714                 if (last > image_info->number_scenes)
2715                   image_info->number_scenes=last;
2716                 p=q;
2717               }
2718               image_info->number_scenes-=image_info->scene-1;
2719             }
2720         }
2721     }
2722   *extension='\0';
2723   GetPathComponent(image_info->filename,ExtensionPath,extension);
2724 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2725   if (*extension != '\0')
2726     if ((LocaleCompare(extension,"gz") == 0) ||
2727         (LocaleCompare(extension,"Z") == 0) ||
2728         (LocaleCompare(extension,"svgz") == 0) ||
2729         (LocaleCompare(extension,"wmz") == 0))
2730       {
2731         char
2732           path[MaxTextExtent];
2733
2734         (void) CopyMagickString(path,image_info->filename,MaxTextExtent);
2735         path[strlen(path)-strlen(extension)-1]='\0';
2736         GetPathComponent(path,ExtensionPath,extension);
2737       }
2738 #endif
2739 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2740   if (*extension != '\0')
2741     if (LocaleCompare(extension,"bz2") == 0)
2742       {
2743         char
2744           path[MaxTextExtent];
2745
2746         (void) CopyMagickString(path,image_info->filename,MaxTextExtent);
2747         path[strlen(path)-strlen(extension)-1]='\0';
2748         GetPathComponent(path,ExtensionPath,extension);
2749       }
2750 #endif
2751   image_info->affirm=MagickFalse;
2752   sans_exception=AcquireExceptionInfo();
2753   if (*extension != '\0')
2754     {
2755       MagickFormatType
2756         format_type;
2757
2758       register ssize_t
2759         i;
2760
2761       static const char
2762         *format_type_formats[] =
2763         {
2764           "AUTOTRACE",
2765           "BROWSE",
2766           "DCRAW",
2767           "EDIT",
2768           "EPHEMERAL",
2769           "LAUNCH",
2770           "MPEG:DECODE",
2771           "MPEG:ENCODE",
2772           "PRINT",
2773           "PS:ALPHA",
2774           "PS:CMYK",
2775           "PS:COLOR",
2776           "PS:GRAY",
2777           "PS:MONO",
2778           "SCAN",
2779           "SHOW",
2780           "WIN",
2781           (char *) NULL
2782         };
2783
2784       /*
2785         User specified image format.
2786       */
2787       (void) CopyMagickString(magic,extension,MaxTextExtent);
2788       LocaleUpper(magic);
2789       /*
2790         Look for explicit image formats.
2791       */
2792       format_type=UndefinedFormatType;
2793       i=0;
2794       while ((format_type == UndefinedFormatType) &&
2795              (format_type_formats[i] != (char *) NULL))
2796       {
2797         if ((*magic == *format_type_formats[i]) &&
2798             (LocaleCompare(magic,format_type_formats[i]) == 0))
2799           format_type=ExplicitFormatType;
2800         i++;
2801       }
2802       magick_info=GetMagickInfo(magic,sans_exception);
2803       if ((magick_info != (const MagickInfo *) NULL) &&
2804           (magick_info->format_type != UndefinedFormatType))
2805         format_type=magick_info->format_type;
2806       if (format_type == UndefinedFormatType)
2807         (void) CopyMagickString(image_info->magick,magic,MaxTextExtent);
2808       else
2809         if (format_type == ExplicitFormatType)
2810           {
2811             image_info->affirm=MagickTrue;
2812             (void) CopyMagickString(image_info->magick,magic,MaxTextExtent);
2813           }
2814       if (LocaleCompare(magic,"RGB") == 0)
2815         image_info->affirm=MagickFalse;  /* maybe SGI disguised as RGB */
2816     }
2817   /*
2818     Look for explicit 'format:image' in filename.
2819   */
2820   *magic='\0';
2821   GetPathComponent(image_info->filename,MagickPath,magic);
2822   if (*magic == '\0')
2823     (void) CopyMagickString(magic,image_info->magick,MaxTextExtent);
2824   else
2825     {
2826       /*
2827         User specified image format.
2828       */
2829       LocaleUpper(magic);
2830       if (IsMagickConflict(magic) == MagickFalse)
2831         {
2832           (void) CopyMagickString(image_info->magick,magic,MaxTextExtent);
2833           if (LocaleCompare(magic,"EPHEMERAL") != 0)
2834             image_info->affirm=MagickTrue;
2835           else
2836             image_info->temporary=MagickTrue;
2837         }
2838     }
2839   magick_info=GetMagickInfo(magic,sans_exception);
2840   sans_exception=DestroyExceptionInfo(sans_exception);
2841   if ((magick_info == (const MagickInfo *) NULL) ||
2842       (GetMagickEndianSupport(magick_info) == MagickFalse))
2843     image_info->endian=UndefinedEndian;
2844   GetPathComponent(image_info->filename,CanonicalPath,filename);
2845   (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
2846   if ((image_info->adjoin != MagickFalse) && (frames > 1))
2847     {
2848       /*
2849         Test for multiple image support (e.g. image%02d.png).
2850       */
2851       (void) InterpretImageFilename(image_info,(Image *) NULL,
2852         image_info->filename,(int) image_info->scene,filename,exception);
2853       if ((LocaleCompare(filename,image_info->filename) != 0) &&
2854           (strchr(filename,'%') == (char *) NULL))
2855         image_info->adjoin=MagickFalse;
2856     }
2857   if ((image_info->adjoin != MagickFalse) && (frames > 0))
2858     {
2859       /*
2860         Some image formats do not support multiple frames per file.
2861       */
2862       magick_info=GetMagickInfo(magic,exception);
2863       if (magick_info != (const MagickInfo *) NULL)
2864         if (GetMagickAdjoin(magick_info) == MagickFalse)
2865           image_info->adjoin=MagickFalse;
2866     }
2867   if (image_info->affirm != MagickFalse)
2868     return(MagickTrue);
2869   if (frames == 0)
2870     {
2871       /*
2872         Determine the image format from the first few bytes of the file.
2873       */
2874       image=AcquireImage(image_info,exception);
2875       (void) CopyMagickString(image->filename,image_info->filename,
2876         MaxTextExtent);
2877       status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
2878       if (status == MagickFalse)
2879         {
2880           image=DestroyImage(image);
2881           return(MagickFalse);
2882         }
2883       if ((IsBlobSeekable(image) == MagickFalse) ||
2884           (IsBlobExempt(image) != MagickFalse))
2885         {
2886           /*
2887             Copy standard input or pipe to temporary file.
2888           */
2889           *filename='\0';
2890           status=ImageToFile(image,filename,exception);
2891           (void) CloseBlob(image);
2892           if (status == MagickFalse)
2893             {
2894               image=DestroyImage(image);
2895               return(MagickFalse);
2896             }
2897           SetImageInfoFile(image_info,(FILE *) NULL);
2898           (void) CopyMagickString(image->filename,filename,MaxTextExtent);
2899           status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
2900           if (status == MagickFalse)
2901             {
2902               image=DestroyImage(image);
2903               return(MagickFalse);
2904             }
2905           (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
2906           image_info->temporary=MagickTrue;
2907         }
2908       (void) ResetMagickMemory(magick,0,sizeof(magick));
2909       count=ReadBlob(image,2*MaxTextExtent,magick);
2910       (void) CloseBlob(image);
2911       image=DestroyImage(image);
2912       /*
2913         Check magic.xml configuration file.
2914       */
2915       sans_exception=AcquireExceptionInfo();
2916       magic_info=GetMagicInfo(magick,(size_t) count,sans_exception);
2917       if ((magic_info != (const MagicInfo *) NULL) &&
2918           (GetMagicName(magic_info) != (char *) NULL))
2919         {
2920           (void) CopyMagickString(image_info->magick,GetMagicName(magic_info),
2921             MaxTextExtent);
2922           magick_info=GetMagickInfo(image_info->magick,sans_exception);
2923           if ((magick_info == (const MagickInfo *) NULL) ||
2924               (GetMagickEndianSupport(magick_info) == MagickFalse))
2925             image_info->endian=UndefinedEndian;
2926           sans_exception=DestroyExceptionInfo(sans_exception);
2927           return(MagickTrue);
2928         }
2929       magick_info=GetMagickInfo(image_info->magick,sans_exception);
2930       if ((magick_info == (const MagickInfo *) NULL) ||
2931           (GetMagickEndianSupport(magick_info) == MagickFalse))
2932         image_info->endian=UndefinedEndian;
2933       sans_exception=DestroyExceptionInfo(sans_exception);
2934     }
2935   return(MagickTrue);
2936 }
2937 \f
2938 /*
2939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2940 %                                                                             %
2941 %                                                                             %
2942 %                                                                             %
2943 %   S e t I m a g e I n f o B l o b                                           %
2944 %                                                                             %
2945 %                                                                             %
2946 %                                                                             %
2947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2948 %
2949 %  SetImageInfoBlob() sets the image info blob member.
2950 %
2951 %  The format of the SetImageInfoBlob method is:
2952 %
2953 %      void SetImageInfoBlob(ImageInfo *image_info,const void *blob,
2954 %        const size_t length)
2955 %
2956 %  A description of each parameter follows:
2957 %
2958 %    o image_info: the image info.
2959 %
2960 %    o blob: the blob.
2961 %
2962 %    o length: the blob length.
2963 %
2964 */
2965 MagickExport void SetImageInfoBlob(ImageInfo *image_info,const void *blob,
2966   const size_t length)
2967 {
2968   assert(image_info != (ImageInfo *) NULL);
2969   assert(image_info->signature == MagickSignature);
2970   if (image_info->debug != MagickFalse)
2971     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2972       image_info->filename);
2973   image_info->blob=(void *) blob;
2974   image_info->length=length;
2975 }
2976 \f
2977 /*
2978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2979 %                                                                             %
2980 %                                                                             %
2981 %                                                                             %
2982 %   S e t I m a g e I n f o F i l e                                           %
2983 %                                                                             %
2984 %                                                                             %
2985 %                                                                             %
2986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2987 %
2988 %  SetImageInfoFile() sets the image info file member.
2989 %
2990 %  The format of the SetImageInfoFile method is:
2991 %
2992 %      void SetImageInfoFile(ImageInfo *image_info,FILE *file)
2993 %
2994 %  A description of each parameter follows:
2995 %
2996 %    o image_info: the image info.
2997 %
2998 %    o file: the file.
2999 %
3000 */
3001 MagickExport void SetImageInfoFile(ImageInfo *image_info,FILE *file)
3002 {
3003   assert(image_info != (ImageInfo *) NULL);
3004   assert(image_info->signature == MagickSignature);
3005   if (image_info->debug != MagickFalse)
3006     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3007       image_info->filename);
3008   image_info->file=file;
3009 }
3010 \f
3011 /*
3012 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3013 %                                                                             %
3014 %                                                                             %
3015 %                                                                             %
3016 %   S e t I m a g e M a s k                                                   %
3017 %                                                                             %
3018 %                                                                             %
3019 %                                                                             %
3020 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3021 %
3022 %  SetImageMask() associates a mask with the image.  The mask must be the same
3023 %  dimensions as the image.
3024 %
3025 %  The format of the SetImageMask method is:
3026 %
3027 %      MagickBooleanType SetImageMask(Image *image,const Image *mask,
3028 %        ExceptionInfo *exception)
3029 %
3030 %  A description of each parameter follows:
3031 %
3032 %    o image: the image.
3033 %
3034 %    o mask: the image mask.
3035 %
3036 %    o exception: return any errors or warnings in this structure.
3037 %
3038 */
3039 MagickExport MagickBooleanType SetImageMask(Image *image,const Image *mask,
3040   ExceptionInfo *exception)
3041 {
3042   CacheView
3043     *mask_view,
3044     *image_view;
3045
3046   MagickBooleanType
3047     status;
3048
3049   ssize_t
3050     y;
3051
3052   /*
3053     Set image mask.
3054   */
3055   assert(image != (Image *) NULL);
3056   if (image->debug != MagickFalse)
3057     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3058   assert(image->signature == MagickSignature);
3059   if (mask == (const Image *) NULL)
3060     {
3061       image->mask=MagickFalse;
3062       return(MagickTrue);
3063     }
3064   status=MagickTrue;
3065   image->mask=MagickTrue;
3066   mask_view=AcquireVirtualCacheView(mask,exception);
3067   image_view=AcquireAuthenticCacheView(image,exception);
3068   for (y=0; y < (ssize_t) image->rows; y++)
3069   {
3070     register const Quantum
3071       *restrict p;
3072
3073     register Quantum
3074       *restrict q;
3075
3076     register ssize_t
3077       x;
3078
3079     if (status == MagickFalse)
3080       continue;
3081     p=GetCacheViewVirtualPixels(mask_view,0,y,mask->columns,1,exception);
3082     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3083     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3084       {
3085         status=MagickFalse;
3086         continue;
3087       }
3088     for (x=0; x < (ssize_t) image->columns; x++)
3089     {
3090       SetPixelMask(image,GetPixelGray(mask,p),q);
3091       p+=GetPixelChannels(mask);
3092       q+=GetPixelChannels(image);
3093     }
3094     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3095       status=MagickFalse;
3096   }
3097   mask_view=DestroyCacheView(mask_view);
3098   image_view=DestroyCacheView(image_view);
3099   return(status);
3100 }
3101 \f
3102 /*
3103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3104 %                                                                             %
3105 %                                                                             %
3106 %                                                                             %
3107 %     S e t I m a g e A l p h a                                               %
3108 %                                                                             %
3109 %                                                                             %
3110 %                                                                             %
3111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3112 %
3113 %  SetImageAlpha() sets the alpha levels of the image.
3114 %
3115 %  The format of the SetImageAlpha method is:
3116 %
3117 %      MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
3118 %        ExceptionInfo *exception)
3119 %
3120 %  A description of each parameter follows:
3121 %
3122 %    o image: the image.
3123 %
3124 %    o Alpha: the level of transparency: 0 is fully opaque and QuantumRange is
3125 %      fully transparent.
3126 %
3127 */
3128 MagickExport MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
3129   ExceptionInfo *exception)
3130 {
3131   CacheView
3132     *image_view;
3133
3134   MagickBooleanType
3135     status;
3136
3137   ssize_t
3138     y;
3139
3140   assert(image != (Image *) NULL);
3141   if (image->debug != MagickFalse)
3142     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3143   assert(image->signature == MagickSignature);
3144   image->alpha_trait=BlendPixelTrait;
3145   status=MagickTrue;
3146   image_view=AcquireAuthenticCacheView(image,exception);
3147 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3148   #pragma omp parallel for schedule(static,4) shared(status) \
3149     magick_threads(image,image,image->rows,1)
3150 #endif
3151   for (y=0; y < (ssize_t) image->rows; y++)
3152   {
3153     register Quantum
3154       *restrict q;
3155
3156     register ssize_t
3157       x;
3158
3159     if (status == MagickFalse)
3160       continue;
3161     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3162     if (q == (Quantum *) NULL)
3163       {
3164         status=MagickFalse;
3165         continue;
3166       }
3167     for (x=0; x < (ssize_t) image->columns; x++)
3168     {
3169       SetPixelAlpha(image,alpha,q);
3170       q+=GetPixelChannels(image);
3171     }
3172     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3173       status=MagickFalse;
3174   }
3175   image_view=DestroyCacheView(image_view);
3176   return(status);
3177 }
3178 \f
3179 /*
3180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3181 %                                                                             %
3182 %                                                                             %
3183 %                                                                             %
3184 %   S e t I m a g e V i r t u a l P i x e l M e t h o d                       %
3185 %                                                                             %
3186 %                                                                             %
3187 %                                                                             %
3188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3189 %
3190 %  SetImageVirtualPixelMethod() sets the "virtual pixels" method for the
3191 %  image and returns the previous setting.  A virtual pixel is any pixel access
3192 %  that is outside the boundaries of the image cache.
3193 %
3194 %  The format of the SetImageVirtualPixelMethod() method is:
3195 %
3196 %      VirtualPixelMethod SetImageVirtualPixelMethod(Image *image,
3197 %        const VirtualPixelMethod virtual_pixel_method,ExceptionInfo *exception)
3198 %
3199 %  A description of each parameter follows:
3200 %
3201 %    o image: the image.
3202 %
3203 %    o virtual_pixel_method: choose the type of virtual pixel.
3204 %
3205 %    o exception: return any errors or warnings in this structure.
3206 %
3207 */
3208 MagickExport VirtualPixelMethod SetImageVirtualPixelMethod(Image *image,
3209   const VirtualPixelMethod virtual_pixel_method,ExceptionInfo *exception)
3210 {
3211   assert(image != (const Image *) NULL);
3212   assert(image->signature == MagickSignature);
3213   if (image->debug != MagickFalse)
3214     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3215   return(SetPixelCacheVirtualMethod(image,virtual_pixel_method,exception));
3216 }
3217 \f
3218 /*
3219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3220 %                                                                             %
3221 %                                                                             %
3222 %                                                                             %
3223 %     S m u s h I m a g e s                                                   %
3224 %                                                                             %
3225 %                                                                             %
3226 %                                                                             %
3227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3228 %
3229 %  SmushImages() takes all images from the current image pointer to the end
3230 %  of the image list and smushes them to each other top-to-bottom if the
3231 %  stack parameter is true, otherwise left-to-right.
3232 %
3233 %  The current gravity setting now effects how the image is justified in the
3234 %  final image.
3235 %
3236 %  The format of the SmushImages method is:
3237 %
3238 %      Image *SmushImages(const Image *images,const MagickBooleanType stack,
3239 %        ExceptionInfo *exception)
3240 %
3241 %  A description of each parameter follows:
3242 %
3243 %    o images: the image sequence.
3244 %
3245 %    o stack: A value other than 0 stacks the images top-to-bottom.
3246 %
3247 %    o offset: minimum distance in pixels between images.
3248 %
3249 %    o exception: return any errors or warnings in this structure.
3250 %
3251 */
3252
3253 static ssize_t SmushXGap(const Image *smush_image,const Image *images,
3254   const ssize_t offset,ExceptionInfo *exception)
3255 {
3256   CacheView
3257     *left_view,
3258     *right_view;
3259
3260   const Image
3261     *left_image,
3262     *right_image;
3263
3264   RectangleInfo
3265     left_geometry,
3266     right_geometry;
3267
3268   register const Quantum
3269     *p;
3270
3271   register ssize_t
3272     i,
3273     y;
3274
3275   size_t
3276     gap;
3277
3278   ssize_t
3279     x;
3280
3281   if (images->previous == (Image *) NULL)
3282     return(0);
3283   right_image=images;
3284   SetGeometry(smush_image,&right_geometry);
3285   GravityAdjustGeometry(right_image->columns,right_image->rows,
3286     right_image->gravity,&right_geometry);
3287   left_image=images->previous;
3288   SetGeometry(smush_image,&left_geometry);
3289   GravityAdjustGeometry(left_image->columns,left_image->rows,
3290     left_image->gravity,&left_geometry);
3291   gap=right_image->columns;
3292   left_view=AcquireVirtualCacheView(left_image,exception);
3293   right_view=AcquireVirtualCacheView(right_image,exception);
3294   for (y=0; y < (ssize_t) smush_image->rows; y++)
3295   {
3296     for (x=(ssize_t) left_image->columns-1; x > 0; x--)
3297     {
3298       p=GetCacheViewVirtualPixels(left_view,x,left_geometry.y+y,1,1,exception);
3299       if ((p == (const Quantum *) NULL) ||
3300           (GetPixelAlpha(left_image,p) != TransparentAlpha) ||
3301           ((left_image->columns-x-1) >= gap))
3302         break;
3303     }
3304     i=(ssize_t) left_image->columns-x-1;
3305     for (x=0; x < (ssize_t) right_image->columns; x++)
3306     {
3307       p=GetCacheViewVirtualPixels(right_view,x,right_geometry.y+y,1,1,
3308         exception);
3309       if ((p == (const Quantum *) NULL) ||
3310           (GetPixelAlpha(right_image,p) != TransparentAlpha) ||
3311           ((x+i) >= (ssize_t) gap))
3312         break;
3313     }
3314     if ((x+i) < (ssize_t) gap)
3315       gap=(size_t) (x+i);
3316   }
3317   right_view=DestroyCacheView(right_view);
3318   left_view=DestroyCacheView(left_view);
3319   if (y < (ssize_t) smush_image->rows)
3320     return(offset);
3321   return((ssize_t) gap-offset);
3322 }
3323
3324 static ssize_t SmushYGap(const Image *smush_image,const Image *images,
3325   const ssize_t offset,ExceptionInfo *exception)
3326 {
3327   CacheView
3328     *bottom_view,
3329     *top_view;
3330
3331   const Image
3332     *bottom_image,
3333     *top_image;
3334
3335   RectangleInfo
3336     bottom_geometry,
3337     top_geometry;
3338
3339   register const Quantum
3340     *p;
3341
3342   register ssize_t
3343     i,
3344     x;
3345
3346   size_t
3347     gap;
3348
3349   ssize_t
3350     y;
3351
3352   if (images->previous == (Image *) NULL)
3353     return(0);
3354   bottom_image=images;
3355   SetGeometry(smush_image,&bottom_geometry);
3356   GravityAdjustGeometry(bottom_image->columns,bottom_image->rows,
3357     bottom_image->gravity,&bottom_geometry);
3358   top_image=images->previous;
3359   SetGeometry(smush_image,&top_geometry);
3360   GravityAdjustGeometry(top_image->columns,top_image->rows,top_image->gravity,
3361     &top_geometry);
3362   gap=bottom_image->rows;
3363   top_view=AcquireVirtualCacheView(top_image,exception);
3364   bottom_view=AcquireVirtualCacheView(bottom_image,exception);
3365   for (x=0; x < (ssize_t) smush_image->columns; x++)
3366   {
3367     for (y=(ssize_t) top_image->rows-1; y > 0; y--)
3368     {
3369       p=GetCacheViewVirtualPixels(top_view,top_geometry.x+x,y,1,1,exception);
3370       if ((p == (const Quantum *) NULL) ||
3371           (GetPixelAlpha(top_image,p) != TransparentAlpha) ||
3372           ((top_image->rows-y-1) >= gap))
3373         break;
3374     }
3375     i=(ssize_t) top_image->rows-y-1;
3376     for (y=0; y < (ssize_t) bottom_image->rows; y++)
3377     {
3378       p=GetCacheViewVirtualPixels(bottom_view,bottom_geometry.x+x,y,1,1,
3379         exception);
3380       if ((p == (const Quantum *) NULL) ||
3381           (GetPixelAlpha(bottom_image,p) != TransparentAlpha) ||
3382           ((y+i) >= (ssize_t) gap))
3383         break;
3384     }
3385     if ((y+i) < (ssize_t) gap)
3386       gap=(size_t) (y+i);
3387   }
3388   bottom_view=DestroyCacheView(bottom_view);
3389   top_view=DestroyCacheView(top_view);
3390   if (x < (ssize_t) smush_image->columns)
3391     return(offset);
3392   return((ssize_t) gap-offset);
3393 }
3394
3395 MagickExport Image *SmushImages(const Image *images,
3396   const MagickBooleanType stack,const ssize_t offset,ExceptionInfo *exception)
3397 {
3398 #define SmushImageTag  "Smush/Image"
3399
3400   const Image
3401     *image;
3402
3403   Image
3404     *smush_image;
3405
3406   MagickBooleanType
3407     proceed,
3408     status;
3409
3410   MagickOffsetType
3411     n;
3412
3413   PixelTrait
3414     alpha_trait;
3415
3416   RectangleInfo
3417     geometry;
3418
3419   register const Image
3420     *next;
3421
3422   size_t
3423     height,
3424     number_images,
3425     width;
3426
3427   ssize_t
3428     x_offset,
3429     y_offset;
3430
3431   /*
3432     Compute maximum area of smushed area.
3433   */
3434   assert(images != (Image *) NULL);
3435   assert(images->signature == MagickSignature);
3436   if (images->debug != MagickFalse)
3437     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
3438   assert(exception != (ExceptionInfo *) NULL);
3439   assert(exception->signature == MagickSignature);
3440   image=images;
3441   alpha_trait=image->alpha_trait;
3442   number_images=1;
3443   width=image->columns;
3444   height=image->rows;
3445   next=GetNextImageInList(image);
3446   for ( ; next != (Image *) NULL; next=GetNextImageInList(next))
3447   {
3448     if (next->alpha_trait == BlendPixelTrait)
3449       alpha_trait=BlendPixelTrait;
3450     number_images++;
3451     if (stack != MagickFalse)
3452       {
3453         if (next->columns > width)
3454           width=next->columns;
3455         height+=next->rows;
3456         if (next->previous != (Image *) NULL)
3457           height+=offset;
3458         continue;
3459       }
3460     width+=next->columns;
3461     if (next->previous != (Image *) NULL)
3462       width+=offset;
3463     if (next->rows > height)
3464       height=next->rows;
3465   }
3466   /*
3467     Smush images.
3468   */
3469   smush_image=CloneImage(image,width,height,MagickTrue,exception);
3470   if (smush_image == (Image *) NULL)
3471     return((Image *) NULL);
3472   if (SetImageStorageClass(smush_image,DirectClass,exception) == MagickFalse)
3473     {
3474       smush_image=DestroyImage(smush_image);
3475       return((Image *) NULL);
3476     }
3477   smush_image->alpha_trait=alpha_trait;
3478   (void) SetImageBackgroundColor(smush_image,exception);
3479   status=MagickTrue;
3480   x_offset=0;
3481   y_offset=0;
3482   for (n=0; n < (MagickOffsetType) number_images; n++)
3483   {
3484     SetGeometry(smush_image,&geometry);
3485     GravityAdjustGeometry(image->columns,image->rows,image->gravity,&geometry);
3486     if (stack != MagickFalse)
3487       {
3488         x_offset-=geometry.x;
3489         y_offset-=SmushYGap(smush_image,image,offset,exception);
3490       }
3491     else
3492       {
3493         x_offset-=SmushXGap(smush_image,image,offset,exception);
3494         y_offset-=geometry.y;
3495       }
3496     status=CompositeImage(smush_image,image,OverCompositeOp,MagickTrue,x_offset,
3497       y_offset,exception);
3498     proceed=SetImageProgress(image,SmushImageTag,n,number_images);
3499     if (proceed == MagickFalse)
3500       break;
3501     if (stack == MagickFalse)
3502       {
3503         x_offset+=(ssize_t) image->columns;
3504         y_offset=0;
3505       }
3506     else
3507       {
3508         x_offset=0;
3509         y_offset+=(ssize_t) image->rows;
3510       }
3511     image=GetNextImageInList(image);
3512   }
3513   if (stack == MagickFalse)
3514     smush_image->columns=(size_t) x_offset;
3515   else
3516     smush_image->rows=(size_t) y_offset;
3517   if (status == MagickFalse)
3518     smush_image=DestroyImage(smush_image);
3519   return(smush_image);
3520 }
3521 \f
3522 /*
3523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3524 %                                                                             %
3525 %                                                                             %
3526 %                                                                             %
3527 %   S t r i p I m a g e                                                       %
3528 %                                                                             %
3529 %                                                                             %
3530 %                                                                             %
3531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3532 %
3533 %  StripImage() strips an image of all profiles and comments.
3534 %
3535 %  The format of the StripImage method is:
3536 %
3537 %      MagickBooleanType StripImage(Image *image,ExceptionInfo *exception)
3538 %
3539 %  A description of each parameter follows:
3540 %
3541 %    o image: the image.
3542 %
3543 %    o exception: return any errors or warnings in this structure.
3544 %
3545 */
3546 MagickExport MagickBooleanType StripImage(Image *image,ExceptionInfo *exception)
3547 {
3548   MagickBooleanType
3549     status;
3550
3551   assert(image != (Image *) NULL);
3552   if (image->debug != MagickFalse)
3553     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3554   (void) exception;
3555   DestroyImageProfiles(image);
3556   (void) DeleteImageProperty(image,"comment");
3557   (void) DeleteImageProperty(image,"date:create");
3558   (void) DeleteImageProperty(image,"date:modify");
3559   status=SetImageArtifact(image,"png:include-chunk","none,trns,gama");
3560   return(status);
3561 }
3562 \f
3563 /*
3564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3565 %                                                                             %
3566 %                                                                             %
3567 %                                                                             %
3568 +   S y n c I m a g e                                                         %
3569 %                                                                             %
3570 %                                                                             %
3571 %                                                                             %
3572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3573 %
3574 %  SyncImage() initializes the red, green, and blue intensities of each pixel
3575 %  as defined by the colormap index.
3576 %
3577 %  The format of the SyncImage method is:
3578 %
3579 %      MagickBooleanType SyncImage(Image *image,ExceptionInfo *exception)
3580 %
3581 %  A description of each parameter follows:
3582 %
3583 %    o image: the image.
3584 %
3585 %    o exception: return any errors or warnings in this structure.
3586 %
3587 */
3588
3589 static inline Quantum PushColormapIndex(Image *image,const Quantum index,
3590   MagickBooleanType *range_exception)
3591 {
3592   if ((size_t) index < image->colors)
3593     return(index);
3594   *range_exception=MagickTrue;
3595   return((Quantum) 0);
3596 }
3597
3598 MagickExport MagickBooleanType SyncImage(Image *image,ExceptionInfo *exception)
3599 {
3600   CacheView
3601     *image_view;
3602
3603   MagickBooleanType
3604     range_exception,
3605     status;
3606
3607   ssize_t
3608     y;
3609
3610   assert(image != (Image *) NULL);
3611   if (image->debug != MagickFalse)
3612     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3613   assert(image->signature == MagickSignature);
3614   if (image->storage_class == DirectClass)
3615     return(MagickFalse);
3616   range_exception=MagickFalse;
3617   status=MagickTrue;
3618   image_view=AcquireAuthenticCacheView(image,exception);
3619 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3620   #pragma omp parallel for schedule(static,4) shared(range_exception,status) \
3621     magick_threads(image,image,image->rows,1)
3622 #endif
3623   for (y=0; y < (ssize_t) image->rows; y++)
3624   {
3625     Quantum
3626       index;
3627
3628     register Quantum
3629       *restrict q;
3630
3631     register ssize_t
3632       x;
3633
3634     if (status == MagickFalse)
3635       continue;
3636     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3637     if (q == (Quantum *) NULL)
3638       {
3639         status=MagickFalse;
3640         continue;
3641       }
3642     for (x=0; x < (ssize_t) image->columns; x++)
3643     {
3644       index=PushColormapIndex(image,GetPixelIndex(image,q),&range_exception);
3645       SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
3646       q+=GetPixelChannels(image);
3647     }
3648     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3649       status=MagickFalse;
3650   }
3651   image_view=DestroyCacheView(image_view);
3652   if ((image->ping == MagickFalse) && (range_exception != MagickFalse))
3653     (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
3654       "InvalidColormapIndex","'%s'",image->filename);
3655   return(status);
3656 }
3657 \f
3658 /*
3659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3660 %                                                                             %
3661 %                                                                             %
3662 %                                                                             %
3663 %   S y n c I m a g e S e t t i n g s                                         %
3664 %                                                                             %
3665 %                                                                             %
3666 %                                                                             %
3667 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3668 %
3669 %  SyncImageSettings() syncs any image_info global options into per-image
3670 %  attributes.
3671 %
3672 %  Note: in IMv6 free form 'options' were always mapped into 'artifacts', so
3673 %  that operations and coders can find such settings.  In IMv7 if a desired
3674 %  per-image artifact is not set, then it will directly look for a global
3675 %  option as a fallback, as such this copy is no longer needed, only the
3676 %  link set up.
3677 %
3678 %  The format of the SyncImageSettings method is:
3679 %
3680 %      MagickBooleanType SyncImageSettings(const ImageInfo *image_info,
3681 %        Image *image,ExceptionInfo *exception)
3682 %      MagickBooleanType SyncImagesSettings(const ImageInfo *image_info,
3683 %        Image *image,ExceptionInfo *exception)
3684 %
3685 %  A description of each parameter follows:
3686 %
3687 %    o image_info: the image info.
3688 %
3689 %    o image: the image.
3690 %
3691 %    o exception: return any errors or warnings in this structure.
3692 %
3693 */
3694
3695 MagickExport MagickBooleanType SyncImagesSettings(ImageInfo *image_info,
3696   Image *images,ExceptionInfo *exception)
3697 {
3698   Image
3699     *image;
3700
3701   assert(image_info != (const ImageInfo *) NULL);
3702   assert(image_info->signature == MagickSignature);
3703   assert(images != (Image *) NULL);
3704   assert(images->signature == MagickSignature);
3705   if (images->debug != MagickFalse)
3706     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
3707   image=images;
3708   for ( ; image != (Image *) NULL; image=GetNextImageInList(image))
3709     (void) SyncImageSettings(image_info,image,exception);
3710   (void) DeleteImageOption(image_info,"page");
3711   return(MagickTrue);
3712 }
3713
3714 MagickExport MagickBooleanType SyncImageSettings(const ImageInfo *image_info,
3715   Image *image,ExceptionInfo *exception)
3716 {
3717   const char
3718     *option;
3719
3720   GeometryInfo
3721     geometry_info;
3722
3723   MagickStatusType
3724     flags;
3725
3726   ResolutionType
3727     units;
3728
3729   /*
3730     Sync image options.
3731   */
3732   assert(image_info != (const ImageInfo *) NULL);
3733   assert(image_info->signature == MagickSignature);
3734   assert(image != (Image *) NULL);
3735   assert(image->signature == MagickSignature);
3736   if (image->debug != MagickFalse)
3737     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3738   option=GetImageOption(image_info,"background");
3739   if (option != (const char *) NULL)
3740     (void) QueryColorCompliance(option,AllCompliance,&image->background_color,
3741       exception);
3742   option=GetImageOption(image_info,"black-point-compensation");
3743   if (option != (const char *) NULL)
3744     image->black_point_compensation=(MagickBooleanType) ParseCommandOption(
3745       MagickBooleanOptions,MagickFalse,option);
3746   option=GetImageOption(image_info,"blue-primary");
3747   if (option != (const char *) NULL)
3748     {
3749       flags=ParseGeometry(option,&geometry_info);
3750       image->chromaticity.blue_primary.x=geometry_info.rho;
3751       image->chromaticity.blue_primary.y=geometry_info.sigma;
3752       if ((flags & SigmaValue) == 0)
3753         image->chromaticity.blue_primary.y=image->chromaticity.blue_primary.x;
3754     }
3755   option=GetImageOption(image_info,"bordercolor");
3756   if (option != (const char *) NULL)
3757     (void) QueryColorCompliance(option,AllCompliance,&image->border_color,
3758       exception);
3759   option=GetImageOption(image_info,"channel");
3760   if (option != (const char *) NULL)
3761     (void) SetPixelChannelMask(image,(ChannelType)
3762       ParseChannelOption(option));
3763   /* FUTURE: do not sync compose to per-image compose setting here */
3764   option=GetImageOption(image_info,"compose");
3765   if (option != (const char *) NULL)
3766     image->compose=(CompositeOperator) ParseCommandOption(MagickComposeOptions,
3767       MagickFalse,option);
3768   /* -- */
3769   option=GetImageOption(image_info,"compress");
3770   if (option != (const char *) NULL)
3771     image->compression=(CompressionType) ParseCommandOption(
3772       MagickCompressOptions,MagickFalse,option);
3773   option=GetImageOption(image_info,"debug");
3774   if (option != (const char *) NULL)
3775     image->debug=(MagickBooleanType) ParseCommandOption(MagickBooleanOptions,
3776       MagickFalse,option);
3777   option=GetImageOption(image_info,"density");
3778   if (option != (const char *) NULL)
3779     {
3780       GeometryInfo
3781         geometry_info;
3782
3783       flags=ParseGeometry(option,&geometry_info);
3784       image->resolution.x=geometry_info.rho;
3785       image->resolution.y=geometry_info.sigma;
3786       if ((flags & SigmaValue) == 0)
3787         image->resolution.y=image->resolution.x;
3788     }
3789   option=GetImageOption(image_info,"depth");
3790   if (option != (const char *) NULL)
3791     image->depth=StringToUnsignedLong(option);
3792   option=GetImageOption(image_info,"endian");
3793   if (option != (const char *) NULL)
3794     image->endian=(EndianType) ParseCommandOption(MagickEndianOptions,
3795       MagickFalse,option);
3796   option=GetImageOption(image_info,"filter");
3797   if (option != (const char *) NULL)
3798     image->filter=(FilterTypes) ParseCommandOption(MagickFilterOptions,
3799       MagickFalse,option);
3800   option=GetImageOption(image_info,"fuzz");
3801   if (option != (const char *) NULL)
3802     image->fuzz=StringToDoubleInterval(option,(double) QuantumRange+1.0);
3803   option=GetImageOption(image_info,"gravity");
3804   if (option != (const char *) NULL)
3805     image->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
3806       MagickFalse,option);
3807   option=GetImageOption(image_info,"green-primary");
3808   if (option != (const char *) NULL)
3809     {
3810       flags=ParseGeometry(option,&geometry_info);
3811       image->chromaticity.green_primary.x=geometry_info.rho;
3812       image->chromaticity.green_primary.y=geometry_info.sigma;
3813       if ((flags & SigmaValue) == 0)
3814         image->chromaticity.green_primary.y=image->chromaticity.green_primary.x;
3815     }
3816   option=GetImageOption(image_info,"intent");
3817   if (option != (const char *) NULL)
3818     image->rendering_intent=(RenderingIntent) ParseCommandOption(
3819       MagickIntentOptions,MagickFalse,option);
3820   option=GetImageOption(image_info,"interlace");
3821   if (option != (const char *) NULL)
3822     image->interlace=(InterlaceType) ParseCommandOption(MagickInterlaceOptions,
3823       MagickFalse,option);
3824   option=GetImageOption(image_info,"interpolate");
3825   if (option != (const char *) NULL)
3826     image->interpolate=(PixelInterpolateMethod) ParseCommandOption(
3827       MagickInterpolateOptions,MagickFalse,option);
3828   option=GetImageOption(image_info,"loop");
3829   if (option != (const char *) NULL)
3830     image->iterations=StringToUnsignedLong(option);
3831   option=GetImageOption(image_info,"mattecolor");
3832   if (option != (const char *) NULL)
3833     (void) QueryColorCompliance(option,AllCompliance,&image->matte_color,
3834       exception);
3835   option=GetImageOption(image_info,"orient");
3836   if (option != (const char *) NULL)
3837     image->orientation=(OrientationType) ParseCommandOption(
3838       MagickOrientationOptions,MagickFalse,option);
3839   option=GetImageOption(image_info,"page");
3840   if (option != (const char *) NULL)
3841     {
3842       char
3843         *geometry;
3844
3845       geometry=GetPageGeometry(option);
3846       flags=ParseAbsoluteGeometry(geometry,&image->page);
3847       geometry=DestroyString(geometry);
3848     }
3849   option=GetImageOption(image_info,"quality");
3850   if (option != (const char *) NULL)
3851     image->quality=StringToUnsignedLong(option);
3852   option=GetImageOption(image_info,"red-primary");
3853   if (option != (const char *) NULL)
3854     {
3855       flags=ParseGeometry(option,&geometry_info);
3856       image->chromaticity.red_primary.x=geometry_info.rho;
3857       image->chromaticity.red_primary.y=geometry_info.sigma;
3858       if ((flags & SigmaValue) == 0)
3859         image->chromaticity.red_primary.y=image->chromaticity.red_primary.x;
3860     }
3861   if (image_info->quality != UndefinedCompressionQuality)
3862     image->quality=image_info->quality;
3863   option=GetImageOption(image_info,"scene");
3864   if (option != (const char *) NULL)
3865     image->scene=StringToUnsignedLong(option);
3866   option=GetImageOption(image_info,"taint");
3867   if (option != (const char *) NULL)
3868     image->taint=(MagickBooleanType) ParseCommandOption(MagickBooleanOptions,
3869       MagickFalse,option);
3870   option=GetImageOption(image_info,"tile-offset");
3871   if (option != (const char *) NULL)
3872     {
3873       char
3874         *geometry;
3875
3876       geometry=GetPageGeometry(option);
3877       flags=ParseAbsoluteGeometry(geometry,&image->tile_offset);
3878       geometry=DestroyString(geometry);
3879     }
3880   option=GetImageOption(image_info,"transparent-color");
3881   if (option != (const char *) NULL)
3882     (void) QueryColorCompliance(option,AllCompliance,&image->transparent_color,
3883       exception);
3884   option=GetImageOption(image_info,"type");
3885   if (option != (const char *) NULL)
3886     image->type=(ImageType) ParseCommandOption(MagickTypeOptions,MagickFalse,
3887       option);
3888   option=GetImageOption(image_info,"units");
3889   units=image_info->units;
3890   if (option != (const char *) NULL)
3891     units=(ResolutionType) ParseCommandOption(MagickResolutionOptions,
3892       MagickFalse,option);
3893   if (units != UndefinedResolution)
3894     {
3895       if (image->units != units)
3896         switch (image->units)
3897         {
3898           case PixelsPerInchResolution:
3899           {
3900             if (units == PixelsPerCentimeterResolution)
3901               {
3902                 image->resolution.x/=2.54;
3903                 image->resolution.y/=2.54;
3904               }
3905             break;
3906           }
3907           case PixelsPerCentimeterResolution:
3908           {
3909             if (units == PixelsPerInchResolution)
3910               {
3911                 image->resolution.x=(double) ((size_t) (100.0*2.54*
3912                   image->resolution.x+0.5))/100.0;
3913                 image->resolution.y=(double) ((size_t) (100.0*2.54*
3914                   image->resolution.y+0.5))/100.0;
3915               }
3916             break;
3917           }
3918           default:
3919             break;
3920         }
3921       image->units=units;
3922     }
3923   option=GetImageOption(image_info,"virtual-pixel");
3924   if (option != (const char *) NULL)
3925     (void) SetImageVirtualPixelMethod(image,(VirtualPixelMethod)
3926       ParseCommandOption(MagickVirtualPixelOptions,MagickFalse,option),
3927       exception);
3928   option=GetImageOption(image_info,"white-point");
3929   if (option != (const char *) NULL)
3930     {
3931       flags=ParseGeometry(option,&geometry_info);
3932       image->chromaticity.white_point.x=geometry_info.rho;
3933       image->chromaticity.white_point.y=geometry_info.sigma;
3934       if ((flags & SigmaValue) == 0)
3935         image->chromaticity.white_point.y=image->chromaticity.white_point.x;
3936     }
3937   ResetImageOptionIterator(image_info);
3938 #if 0
3939   {
3940     /* IMv6: Copy freeform global options into per-image artifacts, so
3941      * various operations and coders can access them.
3942      *
3943      * This has a problem, as per-image artefacts may have been set in
3944      * parenthesis, but may not be unset when parenthesis ends.
3945      */
3946     char
3947       property[MaxTextExtent];
3948
3949     const char
3950       *value;
3951
3952     for (option=GetNextImageOption(image_info); option != (const char *) NULL; )
3953     {
3954       value=GetImageOption(image_info,option);
3955       if (value != (const char *) NULL)
3956         {
3957           (void) FormatLocaleString(property,MaxTextExtent,"%s",option);
3958           (void) SetImageArtifact(image,property,value);
3959         }
3960       option=GetNextImageOption(image_info);
3961     }
3962   }
3963 #else
3964   /* IMv7: pointer to allow the lookup of pre-image artefact will fallback to
3965      a global option setting/define.  This saves a lot of duplication of
3966      global options into per-image artifacts, while ensuring only specifically
3967      set per-image artifacts are preverved when parenthesis ends.
3968
3969      This pointer is never explictally freed, as it is only used as a back
3970      reference, not as the main pointer to the image_info structure.  Images
3971      being removed from a image_info image list (or yet to be added to such),
3972      should have this pointer reset to NULL.
3973   */
3974   image->image_info = image_info;
3975 #endif
3976   return(MagickTrue);
3977 }