]> 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->timestamp=time((time_t *) NULL);
209   image->debug=IsEventLogging();
210   image->reference_count=1;
211   image->semaphore=AllocateSemaphoreInfo();
212   image->signature=MagickSignature;
213   if (image_info == (ImageInfo *) NULL)
214     return(image);
215   /*
216     Transfer image info.
217   */
218   SetBlobExempt(image,image_info->file != (FILE *) NULL ? MagickTrue :
219     MagickFalse);
220   (void) CopyMagickString(image->filename,image_info->filename,MaxTextExtent);
221   (void) CopyMagickString(image->magick_filename,image_info->filename,
222     MaxTextExtent);
223   (void) CopyMagickString(image->magick,image_info->magick,MaxTextExtent);
224   if (image_info->size != (char *) NULL)
225     {
226       (void) ParseAbsoluteGeometry(image_info->size,&image->extract_info);
227       image->columns=image->extract_info.width;
228       image->rows=image->extract_info.height;
229       image->offset=image->extract_info.x;
230       image->extract_info.x=0;
231       image->extract_info.y=0;
232     }
233   if (image_info->extract != (char *) NULL)
234     {
235       RectangleInfo
236         geometry;
237
238       flags=ParseAbsoluteGeometry(image_info->extract,&geometry);
239       if (((flags & XValue) != 0) || ((flags & YValue) != 0))
240         {
241           image->extract_info=geometry;
242           Swap(image->columns,image->extract_info.width);
243           Swap(image->rows,image->extract_info.height);
244         }
245     }
246   image->compression=image_info->compression;
247   image->quality=image_info->quality;
248   image->endian=image_info->endian;
249   image->interlace=image_info->interlace;
250   image->units=image_info->units;
251   if (image_info->density != (char *) NULL)
252     {
253       GeometryInfo
254         geometry_info;
255
256       flags=ParseGeometry(image_info->density,&geometry_info);
257       image->resolution.x=geometry_info.rho;
258       image->resolution.y=geometry_info.sigma;
259       if ((flags & SigmaValue) == 0)
260         image->resolution.y=image->resolution.x;
261     }
262   if (image_info->page != (char *) NULL)
263     {
264       char
265         *geometry;
266
267       image->page=image->extract_info;
268       geometry=GetPageGeometry(image_info->page);
269       (void) ParseAbsoluteGeometry(geometry,&image->page);
270       geometry=DestroyString(geometry);
271     }
272   if (image_info->depth != 0)
273     image->depth=image_info->depth;
274   image->dither=image_info->dither;
275   image->background_color=image_info->background_color;
276   image->border_color=image_info->border_color;
277   image->matte_color=image_info->matte_color;
278   image->transparent_color=image_info->transparent_color;
279   image->ping=image_info->ping;
280   image->progress_monitor=image_info->progress_monitor;
281   image->client_data=image_info->client_data;
282   if (image_info->cache != (void *) NULL)
283     ClonePixelCacheMethods(image->cache,image_info->cache);
284   /*
285     Set all global options that map to per-image settings.
286   */
287   (void) SyncImageSettings(image_info,image,exception);
288   /*
289     Global options that are only set for new images.
290   */
291   image->image_info=(ImageInfo *) NULL;
292   option=GetImageOption(image_info,"delay");
293   if (option != (const char *) NULL)
294     {
295       GeometryInfo
296         geometry_info;
297
298       flags=ParseGeometry(option,&geometry_info);
299       if ((flags & GreaterValue) != 0)
300         {
301           if (image->delay > (size_t) floor(geometry_info.rho+0.5))
302             image->delay=(size_t) floor(geometry_info.rho+0.5);
303         }
304       else
305         if ((flags & LessValue) != 0)
306           {
307             if (image->delay < (size_t) floor(geometry_info.rho+0.5))
308               image->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
309           }
310         else
311           image->delay=(size_t) floor(geometry_info.rho+0.5);
312       if ((flags & SigmaValue) != 0)
313         image->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
314     }
315   option=GetImageOption(image_info,"dispose");
316   if (option != (const char *) NULL)
317     image->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
318       MagickFalse,option);
319   return(image);
320 }
321 \f
322 /*
323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 %                                                                             %
325 %                                                                             %
326 %                                                                             %
327 %   A c q u i r e I m a g e I n f o                                           %
328 %                                                                             %
329 %                                                                             %
330 %                                                                             %
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 %
333 %  AcquireImageInfo() allocates the ImageInfo structure.
334 %
335 %  The format of the AcquireImageInfo method is:
336 %
337 %      ImageInfo *AcquireImageInfo(void)
338 %
339 */
340 MagickExport ImageInfo *AcquireImageInfo(void)
341 {
342   ImageInfo
343     *image_info;
344
345   image_info=(ImageInfo *) AcquireMagickMemory(sizeof(*image_info));
346   if (image_info == (ImageInfo *) NULL)
347     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
348   GetImageInfo(image_info);
349   return(image_info);
350 }
351 \f
352 /*
353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 %                                                                             %
355 %                                                                             %
356 %                                                                             %
357 %   A c q u i r e N e x t I m a g e                                           %
358 %                                                                             %
359 %                                                                             %
360 %                                                                             %
361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362 %
363 %  AcquireNextImage() initializes the next image in a sequence to
364 %  default values.  The next member of image points to the newly allocated
365 %  image.  If there is a memory shortage, next is assigned NULL.
366 %
367 %  The format of the AcquireNextImage method is:
368 %
369 %      void AcquireNextImage(const ImageInfo *image_info,Image *image,
370 %        ExceptionInfo *exception)
371 %
372 %  A description of each parameter follows:
373 %
374 %    o image_info: Many of the image default values are set from this
375 %      structure.  For example, filename, compression, depth, background color,
376 %      and others.
377 %
378 %    o image: the image.
379 %
380 %    o exception: return any errors or warnings in this structure.
381 %
382 */
383 MagickExport void AcquireNextImage(const ImageInfo *image_info,Image *image,
384   ExceptionInfo *exception)
385 {
386   /*
387     Allocate image structure.
388   */
389   assert(image != (Image *) NULL);
390   assert(image->signature == MagickSignature);
391   if (image->debug != MagickFalse)
392     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
393   image->next=AcquireImage(image_info,exception);
394   if (GetNextImageInList(image) == (Image *) NULL)
395     return;
396   (void) CopyMagickString(GetNextImageInList(image)->filename,image->filename,
397     MaxTextExtent);
398   if (image_info != (ImageInfo *) NULL)
399     (void) CopyMagickString(GetNextImageInList(image)->filename,
400       image_info->filename,MaxTextExtent);
401   DestroyBlob(GetNextImageInList(image));
402   image->next->blob=ReferenceBlob(image->blob);
403   image->next->endian=image->endian;
404   image->next->scene=image->scene+1;
405   image->next->previous=image;
406 }
407 \f
408 /*
409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 %                                                                             %
411 %                                                                             %
412 %                                                                             %
413 %     A p p e n d I m a g e s                                                 %
414 %                                                                             %
415 %                                                                             %
416 %                                                                             %
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 %
419 %  AppendImages() takes all images from the current image pointer to the end
420 %  of the image list and appends them to each other top-to-bottom if the
421 %  stack parameter is true, otherwise left-to-right.
422 %
423 %  The current gravity setting effects how the image is justified in the
424 %  final image.
425 %
426 %  The format of the AppendImages method is:
427 %
428 %      Image *AppendImages(const Image *images,const MagickBooleanType stack,
429 %        ExceptionInfo *exception)
430 %
431 %  A description of each parameter follows:
432 %
433 %    o images: the image sequence.
434 %
435 %    o stack: A value other than 0 stacks the images top-to-bottom.
436 %
437 %    o exception: return any errors or warnings in this structure.
438 %
439 */
440 MagickExport Image *AppendImages(const Image *images,
441   const MagickBooleanType stack,ExceptionInfo *exception)
442 {
443 #define AppendImageTag  "Append/Image"
444
445   CacheView
446     *append_view;
447
448   Image
449     *append_image;
450
451   MagickBooleanType
452     status;
453
454   MagickOffsetType
455     n;
456
457   PixelTrait
458     alpha_trait;
459
460   RectangleInfo
461     geometry;
462
463   register const Image
464     *next;
465
466   size_t
467     height,
468     number_images,
469     width;
470
471   ssize_t
472     x_offset,
473     y,
474     y_offset;
475
476   /*
477     Compute maximum area of appended area.
478   */
479   assert(images != (Image *) NULL);
480   assert(images->signature == MagickSignature);
481   if (images->debug != MagickFalse)
482     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
483   assert(exception != (ExceptionInfo *) NULL);
484   assert(exception->signature == MagickSignature);
485   alpha_trait=images->alpha_trait;
486   number_images=1;
487   width=images->columns;
488   height=images->rows;
489   next=GetNextImageInList(images);
490   for ( ; next != (Image *) NULL; next=GetNextImageInList(next))
491   {
492     if (next->alpha_trait == BlendPixelTrait)
493       alpha_trait=BlendPixelTrait;
494     number_images++;
495     if (stack != MagickFalse)
496       {
497         if (next->columns > width)
498           width=next->columns;
499         height+=next->rows;
500         continue;
501       }
502     width+=next->columns;
503     if (next->rows > height)
504       height=next->rows;
505   }
506   /*
507     Append images.
508   */
509   append_image=CloneImage(images,width,height,MagickTrue,exception);
510   if (append_image == (Image *) NULL)
511     return((Image *) NULL);
512   if (SetImageStorageClass(append_image,DirectClass,exception) == MagickFalse)
513     {
514       append_image=DestroyImage(append_image);
515       return((Image *) NULL);
516     }
517   append_image->alpha_trait=alpha_trait;
518   (void) SetImageBackgroundColor(append_image,exception);
519   status=MagickTrue;
520   x_offset=0;
521   y_offset=0;
522   next=images;
523   append_view=AcquireAuthenticCacheView(append_image,exception);
524   for (n=0; n < (MagickOffsetType) number_images; n++)
525   {
526     CacheView
527       *image_view;
528
529     Image
530       *image;
531
532     MagickBooleanType
533       proceed;
534
535     image=CloneImage(next,0,0,MagickTrue,exception);
536     if (image == (Image *) NULL)
537       break;
538     (void) TransformImageColorspace(image,append_image->colorspace,exception);
539     SetGeometry(append_image,&geometry);
540     GravityAdjustGeometry(image->columns,image->rows,image->gravity,&geometry);
541     if (stack != MagickFalse)
542       x_offset-=geometry.x;
543     else
544       y_offset-=geometry.y;
545     image_view=AcquireVirtualCacheView(image,exception);
546 #if defined(MAGICKCORE_OPENMP_SUPPORT)
547     #pragma omp parallel for schedule(static,4) shared(status) \
548       magick_threads(image,image,image->rows,1)
549 #endif
550     for (y=0; y < (ssize_t) image->rows; y++)
551     {
552       MagickBooleanType
553         sync;
554
555       PixelInfo
556         pixel;
557
558       register const Quantum
559         *restrict p;
560
561       register Quantum
562         *restrict q;
563
564       register ssize_t
565         x;
566
567       if (status == MagickFalse)
568         continue;
569       p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
570       q=QueueCacheViewAuthenticPixels(append_view,x_offset,y+y_offset,
571         image->columns,1,exception);
572       if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
573         {
574           status=MagickFalse;
575           continue;
576         }
577       GetPixelInfo(image,&pixel);
578       for (x=0; x < (ssize_t) image->columns; x++)
579       {
580         if (GetPixelMask(image,p) == 0)
581           {
582             p+=GetPixelChannels(image);
583             q+=GetPixelChannels(append_image);
584             continue;
585           }
586         GetPixelInfoPixel(image,p,&pixel);
587         SetPixelInfoPixel(append_image,&pixel,q);
588         p+=GetPixelChannels(image);
589         q+=GetPixelChannels(append_image);
590       }
591       sync=SyncCacheViewAuthenticPixels(append_view,exception);
592       if (sync == MagickFalse)
593         status=MagickFalse;
594     }
595     image_view=DestroyCacheView(image_view);
596     if (stack == MagickFalse)
597       {
598         x_offset+=(ssize_t) image->columns;
599         y_offset=0;
600       }
601     else
602       {
603         x_offset=0;
604         y_offset+=(ssize_t) image->rows;
605       }
606     image=DestroyImage(image);
607     proceed=SetImageProgress(append_image,AppendImageTag,n,number_images);
608     if (proceed == MagickFalse)
609       break;
610     next=GetNextImageInList(next);
611   }
612   append_view=DestroyCacheView(append_view);
613   if (status == MagickFalse)
614     append_image=DestroyImage(append_image);
615   return(append_image);
616 }
617 \f
618 /*
619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 %                                                                             %
621 %                                                                             %
622 %                                                                             %
623 %   C a t c h I m a g e E x c e p t i o n                                     %
624 %                                                                             %
625 %                                                                             %
626 %                                                                             %
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 %
629 %  CatchImageException() returns if no exceptions are found in the image
630 %  sequence, otherwise it determines the most severe exception and reports
631 %  it as a warning or error depending on the severity.
632 %
633 %  The format of the CatchImageException method is:
634 %
635 %      ExceptionType CatchImageException(Image *image)
636 %
637 %  A description of each parameter follows:
638 %
639 %    o image: An image sequence.
640 %
641 */
642 MagickExport ExceptionType CatchImageException(Image *image)
643 {
644   ExceptionInfo
645     *exception;
646
647   ExceptionType
648     severity;
649
650   assert(image != (const Image *) NULL);
651   assert(image->signature == MagickSignature);
652   if (image->debug != MagickFalse)
653     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
654   exception=AcquireExceptionInfo();
655   CatchException(exception);
656   severity=exception->severity;
657   exception=DestroyExceptionInfo(exception);
658   return(severity);
659 }
660 \f
661 /*
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 %                                                                             %
664 %                                                                             %
665 %                                                                             %
666 %   C l i p I m a g e P a t h                                                 %
667 %                                                                             %
668 %                                                                             %
669 %                                                                             %
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671 %
672 %  ClipImagePath() sets the image clip mask based any clipping path information
673 %  if it exists.
674 %
675 %  The format of the ClipImagePath method is:
676 %
677 %      MagickBooleanType ClipImagePath(Image *image,const char *pathname,
678 %        const MagickBooleanType inside,ExceptionInfo *exception)
679 %
680 %  A description of each parameter follows:
681 %
682 %    o image: the image.
683 %
684 %    o pathname: name of clipping path resource. If name is preceded by #, use
685 %      clipping path numbered by name.
686 %
687 %    o inside: if non-zero, later operations take effect inside clipping path.
688 %      Otherwise later operations take effect outside clipping path.
689 %
690 %    o exception: return any errors or warnings in this structure.
691 %
692 */
693
694 MagickExport MagickBooleanType ClipImage(Image *image,ExceptionInfo *exception)
695 {
696   return(ClipImagePath(image,"#1",MagickTrue,exception));
697 }
698
699 MagickExport MagickBooleanType ClipImagePath(Image *image,const char *pathname,
700   const MagickBooleanType inside,ExceptionInfo *exception)
701 {
702 #define ClipImagePathTag  "ClipPath/Image"
703
704   char
705     *property;
706
707   const char
708     *value;
709
710   Image
711     *clip_mask;
712
713   ImageInfo
714     *image_info;
715
716   assert(image != (const Image *) NULL);
717   assert(image->signature == MagickSignature);
718   if (image->debug != MagickFalse)
719     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
720   assert(pathname != NULL);
721   property=AcquireString(pathname);
722   (void) FormatLocaleString(property,MaxTextExtent,"8BIM:1999,2998:%s",
723     pathname);
724   value=GetImageProperty(image,property,exception);
725   property=DestroyString(property);
726   if (value == (const char *) NULL)
727     {
728       ThrowFileException(exception,OptionError,"NoClipPathDefined",
729         image->filename);
730       return(MagickFalse);
731     }
732   image_info=AcquireImageInfo();
733   (void) CopyMagickString(image_info->filename,image->filename,MaxTextExtent);
734   (void) ConcatenateMagickString(image_info->filename,pathname,MaxTextExtent);
735   clip_mask=BlobToImage(image_info,value,strlen(value),exception);
736   image_info=DestroyImageInfo(image_info);
737   if (clip_mask == (Image *) NULL)
738     return(MagickFalse);
739   if (clip_mask->storage_class == PseudoClass)
740     {
741       (void) SyncImage(clip_mask,exception);
742       if (SetImageStorageClass(clip_mask,DirectClass,exception) == MagickFalse)
743         return(MagickFalse);
744     }
745   if (inside == MagickFalse)
746     (void) NegateImage(clip_mask,MagickFalse,exception);
747   (void) FormatLocaleString(clip_mask->magick_filename,MaxTextExtent,
748     "8BIM:1999,2998:%s\nPS",pathname);
749   (void) SetImageMask(image,clip_mask,exception);
750   clip_mask=DestroyImage(clip_mask);
751   return(MagickTrue);
752 }
753 \f
754 /*
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 %                                                                             %
757 %                                                                             %
758 %                                                                             %
759 %   C l o n e I m a g e                                                       %
760 %                                                                             %
761 %                                                                             %
762 %                                                                             %
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
764 %
765 %  CloneImage() copies an image and returns the copy as a new image object.
766 %
767 %  If the specified columns and rows is 0, an exact copy of the image is
768 %  returned, otherwise the pixel data is undefined and must be initialized
769 %  with the QueueAuthenticPixels() and SyncAuthenticPixels() methods.  On
770 %  failure, a NULL image is returned and exception describes the reason for the
771 %  failure.
772 %
773 %  The format of the CloneImage method is:
774 %
775 %      Image *CloneImage(const Image *image,const size_t columns,
776 %        const size_t rows,const MagickBooleanType orphan,
777 %        ExceptionInfo *exception)
778 %
779 %  A description of each parameter follows:
780 %
781 %    o image: the image.
782 %
783 %    o columns: the number of columns in the cloned image.
784 %
785 %    o rows: the number of rows in the cloned image.
786 %
787 %    o detach:  With a value other than 0, the cloned image is detached from
788 %      its parent I/O stream.
789 %
790 %    o exception: return any errors or warnings in this structure.
791 %
792 */
793 MagickExport Image *CloneImage(const Image *image,const size_t columns,
794   const size_t rows,const MagickBooleanType detach,ExceptionInfo *exception)
795 {
796   Image
797     *clone_image;
798
799   double
800     scale;
801
802   size_t
803     length;
804
805   /*
806     Clone the image.
807   */
808   assert(image != (const Image *) NULL);
809   assert(image->signature == MagickSignature);
810   if (image->debug != MagickFalse)
811     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
812   assert(exception != (ExceptionInfo *) NULL);
813   assert(exception->signature == MagickSignature);
814   clone_image=(Image *) AcquireMagickMemory(sizeof(*clone_image));
815   if (clone_image == (Image *) NULL)
816     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
817   (void) ResetMagickMemory(clone_image,0,sizeof(*clone_image));
818   clone_image->signature=MagickSignature;
819   clone_image->storage_class=image->storage_class;
820   clone_image->number_channels=image->number_channels;
821   clone_image->number_meta_channels=image->number_meta_channels;
822   clone_image->metacontent_extent=image->metacontent_extent;
823   clone_image->colorspace=image->colorspace;
824   clone_image->mask=image->mask;
825   clone_image->alpha_trait=image->alpha_trait;
826   clone_image->columns=image->columns;
827   clone_image->rows=image->rows;
828   clone_image->dither=image->dither;
829   if (image->colormap != (PixelInfo *) NULL)
830     {
831       /*
832         Allocate and copy the image colormap.
833       */
834       clone_image->colors=image->colors;
835       length=(size_t) image->colors;
836       clone_image->colormap=(PixelInfo *) AcquireQuantumMemory(length,
837         sizeof(*clone_image->colormap));
838       if (clone_image->colormap == (PixelInfo *) NULL)
839         ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
840       (void) CopyMagickMemory(clone_image->colormap,image->colormap,length*
841         sizeof(*clone_image->colormap));
842     }
843   (void) CloneImageProfiles(clone_image,image);
844   (void) CloneImageProperties(clone_image,image);
845   (void) CloneImageArtifacts(clone_image,image);
846   GetTimerInfo(&clone_image->timer);
847   if (image->ascii85 != (void *) NULL)
848     Ascii85Initialize(clone_image);
849   clone_image->magick_columns=image->magick_columns;
850   clone_image->magick_rows=image->magick_rows;
851   clone_image->type=image->type;
852   clone_image->channel_mask=image->channel_mask;
853   clone_image->channel_map=ClonePixelChannelMap(image->channel_map);
854   (void) CopyMagickString(clone_image->magick_filename,image->magick_filename,
855     MaxTextExtent);
856   (void) CopyMagickString(clone_image->magick,image->magick,MaxTextExtent);
857   (void) CopyMagickString(clone_image->filename,image->filename,MaxTextExtent);
858   clone_image->progress_monitor=image->progress_monitor;
859   clone_image->client_data=image->client_data;
860   clone_image->reference_count=1;
861   clone_image->next=image->next;
862   clone_image->previous=image->previous;
863   clone_image->list=NewImageList();
864   if (detach == MagickFalse)
865     clone_image->blob=ReferenceBlob(image->blob);
866   else
867     {
868       clone_image->next=NewImageList();
869       clone_image->previous=NewImageList();
870       clone_image->blob=CloneBlobInfo((BlobInfo *) NULL);
871     }
872   clone_image->ping=image->ping;
873   clone_image->debug=IsEventLogging();
874   clone_image->semaphore=AllocateSemaphoreInfo();
875   if ((columns == 0) && (rows == 0))
876     {
877       if (image->montage != (char *) NULL)
878         (void) CloneString(&clone_image->montage,image->montage);
879       if (image->directory != (char *) NULL)
880         (void) CloneString(&clone_image->directory,image->directory);
881       clone_image->cache=ReferencePixelCache(image->cache);
882       return(clone_image);
883     }
884   scale=(double) columns/(double) image->columns;
885   clone_image->page.width=(size_t) floor(scale*image->page.width+0.5);
886   clone_image->page.x=(ssize_t) ceil(scale*image->page.x-0.5);
887   clone_image->tile_offset.x=(ssize_t) ceil(scale*image->tile_offset.x-0.5);
888   scale=(double) rows/(double) image->rows;
889   clone_image->page.height=(size_t) floor(scale*image->page.height+0.5);
890   clone_image->page.y=(ssize_t) ceil(scale*image->page.y-0.5);
891   clone_image->tile_offset.y=(ssize_t) ceil(scale*image->tile_offset.y-0.5);
892   clone_image->columns=columns;
893   clone_image->rows=rows;
894   clone_image->cache=ClonePixelCache(image->cache);
895   return(clone_image);
896 }
897 \f
898 /*
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
900 %                                                                             %
901 %                                                                             %
902 %                                                                             %
903 %   C l o n e I m a g e I n f o                                               %
904 %                                                                             %
905 %                                                                             %
906 %                                                                             %
907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
908 %
909 %  CloneImageInfo() makes a copy of the given image info structure.  If
910 %  NULL is specified, a new image info structure is created initialized to
911 %  default values.
912 %
913 %  The format of the CloneImageInfo method is:
914 %
915 %      ImageInfo *CloneImageInfo(const ImageInfo *image_info)
916 %
917 %  A description of each parameter follows:
918 %
919 %    o image_info: the image info.
920 %
921 */
922 MagickExport ImageInfo *CloneImageInfo(const ImageInfo *image_info)
923 {
924   ImageInfo
925     *clone_info;
926
927   clone_info=AcquireImageInfo();
928   if (image_info == (ImageInfo *) NULL)
929     return(clone_info);
930   clone_info->compression=image_info->compression;
931   clone_info->temporary=image_info->temporary;
932   clone_info->adjoin=image_info->adjoin;
933   clone_info->antialias=image_info->antialias;
934   clone_info->scene=image_info->scene;
935   clone_info->number_scenes=image_info->number_scenes;
936   clone_info->depth=image_info->depth;
937   (void) CloneString(&clone_info->size,image_info->size);
938   (void) CloneString(&clone_info->extract,image_info->extract);
939   (void) CloneString(&clone_info->scenes,image_info->scenes);
940   (void) CloneString(&clone_info->page,image_info->page);
941   clone_info->interlace=image_info->interlace;
942   clone_info->endian=image_info->endian;
943   clone_info->units=image_info->units;
944   clone_info->quality=image_info->quality;
945   (void) CloneString(&clone_info->sampling_factor,image_info->sampling_factor);
946   (void) CloneString(&clone_info->server_name,image_info->server_name);
947   (void) CloneString(&clone_info->font,image_info->font);
948   (void) CloneString(&clone_info->texture,image_info->texture);
949   (void) CloneString(&clone_info->density,image_info->density);
950   clone_info->pointsize=image_info->pointsize;
951   clone_info->fuzz=image_info->fuzz;
952   clone_info->background_color=image_info->background_color;
953   clone_info->border_color=image_info->border_color;
954   clone_info->matte_color=image_info->matte_color;
955   clone_info->transparent_color=image_info->transparent_color;
956   clone_info->dither=image_info->dither;
957   clone_info->monochrome=image_info->monochrome;
958   clone_info->colorspace=image_info->colorspace;
959   clone_info->type=image_info->type;
960   clone_info->orientation=image_info->orientation;
961   clone_info->preview_type=image_info->preview_type;
962   clone_info->group=image_info->group;
963   clone_info->ping=image_info->ping;
964   clone_info->verbose=image_info->verbose;
965   (void) CloneString(&clone_info->view,image_info->view);
966   clone_info->progress_monitor=image_info->progress_monitor;
967   clone_info->client_data=image_info->client_data;
968   clone_info->cache=image_info->cache;
969   if (image_info->cache != (void *) NULL)
970     clone_info->cache=ReferencePixelCache(image_info->cache);
971   if (image_info->profile != (void *) NULL)
972     clone_info->profile=(void *) CloneStringInfo((StringInfo *)
973       image_info->profile);
974   SetImageInfoFile(clone_info,image_info->file);
975   SetImageInfoBlob(clone_info,image_info->blob,image_info->length);
976   clone_info->stream=image_info->stream;
977   (void) CopyMagickString(clone_info->magick,image_info->magick,MaxTextExtent);
978   (void) CopyMagickString(clone_info->unique,image_info->unique,MaxTextExtent);
979   (void) CopyMagickString(clone_info->zero,image_info->zero,MaxTextExtent);
980   (void) CopyMagickString(clone_info->filename,image_info->filename,
981     MaxTextExtent);
982   clone_info->channel=image_info->channel;
983   (void) CloneImageOptions(clone_info,image_info);
984   clone_info->debug=IsEventLogging();
985   clone_info->signature=image_info->signature;
986   return(clone_info);
987 }
988 \f
989 /*
990 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
991 %                                                                             %
992 %                                                                             %
993 %                                                                             %
994 %   D e s t r o y I m a g e                                                   %
995 %                                                                             %
996 %                                                                             %
997 %                                                                             %
998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
999 %
1000 %  DestroyImage() dereferences an image, deallocating memory associated with
1001 %  the image if the reference count becomes zero.
1002 %
1003 %  The format of the DestroyImage method is:
1004 %
1005 %      Image *DestroyImage(Image *image)
1006 %
1007 %  A description of each parameter follows:
1008 %
1009 %    o image: the image.
1010 %
1011 */
1012 MagickExport Image *DestroyImage(Image *image)
1013 {
1014   MagickBooleanType
1015     destroy;
1016
1017   /*
1018     Dereference image.
1019   */
1020   assert(image != (Image *) NULL);
1021   assert(image->signature == MagickSignature);
1022   if (image->debug != MagickFalse)
1023     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1024   destroy=MagickFalse;
1025   LockSemaphoreInfo(image->semaphore);
1026   image->reference_count--;
1027   if (image->reference_count == 0)
1028     destroy=MagickTrue;
1029   UnlockSemaphoreInfo(image->semaphore);
1030   if (destroy == MagickFalse)
1031     return((Image *) NULL);
1032   /*
1033     Destroy image.
1034   */
1035   DestroyImagePixels(image);
1036   image->channel_map=DestroyPixelChannelMap(image->channel_map);
1037   if (image->montage != (char *) NULL)
1038     image->montage=DestroyString(image->montage);
1039   if (image->directory != (char *) NULL)
1040     image->directory=DestroyString(image->directory);
1041   if (image->colormap != (PixelInfo *) NULL)
1042     image->colormap=(PixelInfo *) RelinquishMagickMemory(image->colormap);
1043   if (image->geometry != (char *) NULL)
1044     image->geometry=DestroyString(image->geometry);
1045   DestroyImageProfiles(image);
1046   DestroyImageProperties(image);
1047   DestroyImageArtifacts(image);
1048   if (image->ascii85 != (Ascii85Info*) NULL)
1049     image->ascii85=(Ascii85Info *) RelinquishMagickMemory(image->ascii85);
1050   DestroyBlob(image);
1051   if (image->semaphore != (SemaphoreInfo *) NULL)
1052     DestroySemaphoreInfo(&image->semaphore);
1053   image->signature=(~MagickSignature);
1054   image=(Image *) RelinquishMagickMemory(image);
1055   return(image);
1056 }
1057 \f
1058 /*
1059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060 %                                                                             %
1061 %                                                                             %
1062 %                                                                             %
1063 %   D e s t r o y I m a g e I n f o                                           %
1064 %                                                                             %
1065 %                                                                             %
1066 %                                                                             %
1067 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1068 %
1069 %  DestroyImageInfo() deallocates memory associated with an ImageInfo
1070 %  structure.
1071 %
1072 %  The format of the DestroyImageInfo method is:
1073 %
1074 %      ImageInfo *DestroyImageInfo(ImageInfo *image_info)
1075 %
1076 %  A description of each parameter follows:
1077 %
1078 %    o image_info: the image info.
1079 %
1080 */
1081 MagickExport ImageInfo *DestroyImageInfo(ImageInfo *image_info)
1082 {
1083   assert(image_info != (ImageInfo *) NULL);
1084   assert(image_info->signature == MagickSignature);
1085   if (image_info->debug != MagickFalse)
1086     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1087       image_info->filename);
1088   if (image_info->size != (char *) NULL)
1089     image_info->size=DestroyString(image_info->size);
1090   if (image_info->extract != (char *) NULL)
1091     image_info->extract=DestroyString(image_info->extract);
1092   if (image_info->scenes != (char *) NULL)
1093     image_info->scenes=DestroyString(image_info->scenes);
1094   if (image_info->page != (char *) NULL)
1095     image_info->page=DestroyString(image_info->page);
1096   if (image_info->sampling_factor != (char *) NULL)
1097     image_info->sampling_factor=DestroyString(
1098       image_info->sampling_factor);
1099   if (image_info->server_name != (char *) NULL)
1100     image_info->server_name=DestroyString(
1101       image_info->server_name);
1102   if (image_info->font != (char *) NULL)
1103     image_info->font=DestroyString(image_info->font);
1104   if (image_info->texture != (char *) NULL)
1105     image_info->texture=DestroyString(image_info->texture);
1106   if (image_info->density != (char *) NULL)
1107     image_info->density=DestroyString(image_info->density);
1108   if (image_info->view != (char *) NULL)
1109     image_info->view=DestroyString(image_info->view);
1110   if (image_info->cache != (void *) NULL)
1111     image_info->cache=DestroyPixelCache(image_info->cache);
1112   if (image_info->profile != (StringInfo *) NULL)
1113     image_info->profile=(void *) DestroyStringInfo((StringInfo *)
1114       image_info->profile);
1115   DestroyImageOptions(image_info);
1116   image_info->signature=(~MagickSignature);
1117   image_info=(ImageInfo *) RelinquishMagickMemory(image_info);
1118   return(image_info);
1119 }
1120 \f
1121 /*
1122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1123 %                                                                             %
1124 %                                                                             %
1125 %                                                                             %
1126 +   D i s a s s o c i a t e I m a g e S t r e a m                             %
1127 %                                                                             %
1128 %                                                                             %
1129 %                                                                             %
1130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1131 %
1132 %  DisassociateImageStream() disassociates the image stream.
1133 %
1134 %  The format of the DisassociateImageStream method is:
1135 %
1136 %      MagickBooleanType DisassociateImageStream(const Image *image)
1137 %
1138 %  A description of each parameter follows:
1139 %
1140 %    o image: the image.
1141 %
1142 */
1143 MagickExport void DisassociateImageStream(Image *image)
1144 {
1145   assert(image != (const Image *) NULL);
1146   assert(image->signature == MagickSignature);
1147   if (image->debug != MagickFalse)
1148     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1149   (void) DetachBlob(image->blob);
1150 }
1151 \f
1152 /*
1153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1154 %                                                                             %
1155 %                                                                             %
1156 %                                                                             %
1157 %   G e t I m a g e A l p h a C h a n n e l                                   %
1158 %                                                                             %
1159 %                                                                             %
1160 %                                                                             %
1161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1162 %
1163 %  GetImageAlphaChannel() returns MagickFalse if the image alpha channel is
1164 %  not activated.  That is, the image is RGB rather than RGBA or CMYK rather
1165 %  than CMYKA.
1166 %
1167 %  The format of the GetImageAlphaChannel method is:
1168 %
1169 %      MagickBooleanType GetImageAlphaChannel(const Image *image)
1170 %
1171 %  A description of each parameter follows:
1172 %
1173 %    o image: the image.
1174 %
1175 */
1176 MagickExport MagickBooleanType GetImageAlphaChannel(const Image *image)
1177 {
1178   assert(image != (const Image *) NULL);
1179   if (image->debug != MagickFalse)
1180     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1181   assert(image->signature == MagickSignature);
1182   return(image->alpha_trait == BlendPixelTrait ? MagickTrue : MagickFalse);
1183 }
1184 \f
1185 /*
1186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1187 %                                                                             %
1188 %                                                                             %
1189 %                                                                             %
1190 %   G e t I m a g e I n f o                                                   %
1191 %                                                                             %
1192 %                                                                             %
1193 %                                                                             %
1194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1195 %
1196 %  GetImageInfo() initializes image_info to default values.
1197 %
1198 %  The format of the GetImageInfo method is:
1199 %
1200 %      void GetImageInfo(ImageInfo *image_info)
1201 %
1202 %  A description of each parameter follows:
1203 %
1204 %    o image_info: the image info.
1205 %
1206 */
1207 MagickExport void GetImageInfo(ImageInfo *image_info)
1208 {
1209   char
1210     *synchronize;
1211
1212   ExceptionInfo
1213     *exception;
1214
1215   /*
1216     File and image dimension members.
1217   */
1218   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1219   assert(image_info != (ImageInfo *) NULL);
1220   (void) ResetMagickMemory(image_info,0,sizeof(*image_info));
1221   image_info->adjoin=MagickTrue;
1222   image_info->interlace=NoInterlace;
1223   image_info->channel=DefaultChannels;
1224   image_info->quality=UndefinedCompressionQuality;
1225   image_info->antialias=MagickTrue;
1226   image_info->dither=MagickTrue;
1227   synchronize=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
1228   if (synchronize != (const char *) NULL)
1229     {
1230       image_info->synchronize=IsStringTrue(synchronize);
1231       synchronize=DestroyString(synchronize);
1232     }
1233   exception=AcquireExceptionInfo();
1234   (void) QueryColorCompliance(BackgroundColor,AllCompliance,
1235     &image_info->background_color,exception);
1236   (void) QueryColorCompliance(BorderColor,AllCompliance,
1237     &image_info->border_color,exception);
1238   (void) QueryColorCompliance(MatteColor,AllCompliance,&image_info->matte_color,
1239     exception);
1240   (void) QueryColorCompliance(TransparentColor,AllCompliance,
1241     &image_info->transparent_color,exception);
1242   exception=DestroyExceptionInfo(exception);
1243   image_info->debug=IsEventLogging();
1244   image_info->signature=MagickSignature;
1245 }
1246 \f
1247 /*
1248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1249 %                                                                             %
1250 %                                                                             %
1251 %                                                                             %
1252 %   G e t I m a g e I n f o F i l e                                           %
1253 %                                                                             %
1254 %                                                                             %
1255 %                                                                             %
1256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1257 %
1258 %  GetImageInfoFile() returns the image info file member.
1259 %
1260 %  The format of the GetImageInfoFile method is:
1261 %
1262 %      FILE *GetImageInfoFile(const ImageInfo *image_info)
1263 %
1264 %  A description of each parameter follows:
1265 %
1266 %    o image_info: the image info.
1267 %
1268 */
1269 MagickExport FILE *GetImageInfoFile(const ImageInfo *image_info)
1270 {
1271   return(image_info->file);
1272 }
1273 \f
1274 /*
1275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1276 %                                                                             %
1277 %                                                                             %
1278 %                                                                             %
1279 %   G e t I m a g e M a s k                                                   %
1280 %                                                                             %
1281 %                                                                             %
1282 %                                                                             %
1283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1284 %
1285 %  GetImageMask() returns the mask associated with the image.
1286 %
1287 %  The format of the GetImageMask method is:
1288 %
1289 %      Image *GetImageMask(const Image *image,ExceptionInfo *exception)
1290 %
1291 %  A description of each parameter follows:
1292 %
1293 %    o image: the image.
1294 %
1295 */
1296 MagickExport Image *GetImageMask(const Image *image,ExceptionInfo *exception)
1297 {
1298   CacheView
1299     *mask_view,
1300     *image_view;
1301
1302   Image
1303     *mask_image;
1304
1305   MagickBooleanType
1306     status;
1307
1308   ssize_t
1309     y;
1310
1311   /*
1312     Get image mask.
1313   */
1314   assert(image != (Image *) NULL);
1315   if (image->debug != MagickFalse)
1316     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1317   assert(image->signature == MagickSignature);
1318   mask_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1319   if (mask_image == (Image *) NULL)
1320     return((Image *) NULL);
1321   status=MagickTrue;
1322   (void) SetImageColorspace(mask_image,GRAYColorspace,exception);
1323   mask_image->mask=MagickFalse;
1324   image_view=AcquireVirtualCacheView(image,exception);
1325   mask_view=AcquireAuthenticCacheView(mask_image,exception);
1326   for (y=0; y < (ssize_t) image->rows; y++)
1327   {
1328     register const Quantum
1329       *restrict p;
1330
1331     register Quantum
1332       *restrict q;
1333
1334     register ssize_t
1335       x;
1336
1337     if (status == MagickFalse)
1338       continue;
1339     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1340     q=GetCacheViewAuthenticPixels(mask_view,0,y,mask_image->columns,1,
1341       exception);
1342     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1343       {
1344         status=MagickFalse;
1345         continue;
1346       }
1347     for (x=0; x < (ssize_t) image->columns; x++)
1348     {
1349       SetPixelGray(mask_image,GetPixelMask(image,p),q);
1350       p+=GetPixelChannels(image);
1351       q+=GetPixelChannels(mask_image);
1352     }
1353     if (SyncCacheViewAuthenticPixels(mask_view,exception) == MagickFalse)
1354       status=MagickFalse;
1355   }
1356   mask_view=DestroyCacheView(mask_view);
1357   image_view=DestroyCacheView(image_view);
1358   if (status == MagickFalse)
1359     mask_image=DestroyImage(mask_image);
1360   return(mask_image);
1361 }
1362 \f
1363 /*
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1365 %                                                                             %
1366 %                                                                             %
1367 %                                                                             %
1368 +   G e t I m a g e R e f e r e n c e C o u n t                               %
1369 %                                                                             %
1370 %                                                                             %
1371 %                                                                             %
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1373 %
1374 %  GetImageReferenceCount() returns the image reference count.
1375 %
1376 %  The format of the GetReferenceCount method is:
1377 %
1378 %      ssize_t GetImageReferenceCount(Image *image)
1379 %
1380 %  A description of each parameter follows:
1381 %
1382 %    o image: the image.
1383 %
1384 */
1385 MagickExport ssize_t GetImageReferenceCount(Image *image)
1386 {
1387   ssize_t
1388     reference_count;
1389
1390   assert(image != (Image *) NULL);
1391   assert(image->signature == MagickSignature);
1392   if (image->debug != MagickFalse)
1393     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1394   LockSemaphoreInfo(image->semaphore);
1395   reference_count=image->reference_count;
1396   UnlockSemaphoreInfo(image->semaphore);
1397   return(reference_count);
1398 }
1399 \f
1400 /*
1401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1402 %                                                                             %
1403 %                                                                             %
1404 %                                                                             %
1405 %   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                       %
1406 %                                                                             %
1407 %                                                                             %
1408 %                                                                             %
1409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 %
1411 %  GetImageVirtualPixelMethod() gets the "virtual pixels" method for the
1412 %  image.  A virtual pixel is any pixel access that is outside the boundaries
1413 %  of the image cache.
1414 %
1415 %  The format of the GetImageVirtualPixelMethod() method is:
1416 %
1417 %      VirtualPixelMethod GetImageVirtualPixelMethod(const Image *image)
1418 %
1419 %  A description of each parameter follows:
1420 %
1421 %    o image: the image.
1422 %
1423 */
1424 MagickExport VirtualPixelMethod GetImageVirtualPixelMethod(const Image *image)
1425 {
1426   assert(image != (Image *) NULL);
1427   assert(image->signature == MagickSignature);
1428   if (image->debug != MagickFalse)
1429     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1430   return(GetPixelCacheVirtualMethod(image));
1431 }
1432 \f
1433 /*
1434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1435 %                                                                             %
1436 %                                                                             %
1437 %                                                                             %
1438 %  I n t e r p r e t I m a g e F i l e n a m e                                %
1439 %                                                                             %
1440 %                                                                             %
1441 %                                                                             %
1442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1443 %
1444 %  InterpretImageFilename() interprets embedded characters in an image filename.
1445 %  The filename length is returned.
1446 %
1447 %  The format of the InterpretImageFilename method is:
1448 %
1449 %      size_t InterpretImageFilename(const ImageInfo *image_info,Image *image,
1450 %        const char *format,int value,char *filename,ExceptionInfo *exception)
1451 %
1452 %  A description of each parameter follows.
1453 %
1454 %    o image_info: the image info..
1455 %
1456 %    o image: the image.
1457 %
1458 %    o format:  A filename describing the format to use to write the numeric
1459 %      argument. Only the first numeric format identifier is replaced.
1460 %
1461 %    o value:  Numeric value to substitute into format filename.
1462 %
1463 %    o filename:  return the formatted filename in this character buffer.
1464 %
1465 %    o exception: return any errors or warnings in this structure.
1466 %
1467 */
1468 MagickExport size_t InterpretImageFilename(const ImageInfo *image_info,
1469   Image *image,const char *format,int value,char *filename,
1470   ExceptionInfo *exception)
1471 {
1472   char
1473     *q;
1474
1475   int
1476     c;
1477
1478   MagickBooleanType
1479     canonical;
1480
1481   register const char
1482     *p;
1483
1484   size_t
1485     length;
1486
1487   canonical=MagickFalse;
1488   length=0;
1489   (void) CopyMagickString(filename,format,MaxTextExtent);
1490   for (p=strchr(format,'%'); p != (char *) NULL; p=strchr(p+1,'%'))
1491   {
1492     q=(char *) p+1;
1493     if (*q == '%')
1494       {
1495         p=q+1;
1496         continue;
1497       }
1498     if (*q == '0')
1499       {
1500         ssize_t
1501           value;
1502
1503         value=(ssize_t) strtol(q,&q,10);
1504         (void) value;
1505       }
1506     switch (*q)
1507     {
1508       case 'd':
1509       case 'o':
1510       case 'x':
1511       {
1512         q++;
1513         c=(*q);
1514         *q='\0';
1515         (void) FormatLocaleString(filename+(p-format),(size_t) (MaxTextExtent-
1516           (p-format)),p,value);
1517         *q=c;
1518         (void) ConcatenateMagickString(filename,q,MaxTextExtent);
1519         canonical=MagickTrue;
1520         if (*(q-1) != '%')
1521           break;
1522         p++;
1523         break;
1524       }
1525       case '[':
1526       {
1527         char
1528           pattern[MaxTextExtent];
1529
1530         const char
1531           *value;
1532
1533         register char
1534           *r;
1535
1536         register ssize_t
1537           i;
1538
1539         ssize_t
1540           depth;
1541
1542         /*
1543           Image option.
1544         */
1545         /* FUTURE: Compare update with code from InterpretImageProperties()
1546            Note that a 'filename:' property should not need depth recursion.
1547         */
1548         if (strchr(p,']') == (char *) NULL)
1549           break;
1550         depth=1;
1551         r=q+1;
1552         for (i=0; (i < (MaxTextExtent-1L)) && (*r != '\0'); i++)
1553         {
1554           if (*r == '[')
1555             depth++;
1556           if (*r == ']')
1557             depth--;
1558           if (depth <= 0)
1559             break;
1560           pattern[i]=(*r++);
1561         }
1562         pattern[i]='\0';
1563         if (LocaleNCompare(pattern,"filename:",9) != 0)
1564           break;
1565         value=(const char *) NULL;
1566 #if 0
1567         // FUTURE: remove this code. -- Anthony  29 Arpil 2012
1568         // Removed as GetMagickProperty() will will never match a "filename:"
1569         // string as this is not a 'known' image property.
1570         //
1571         if ((image_info != (const ImageInfo *) NULL) &&
1572             (image != (const Image *) NULL))
1573           value=GetMagickProperty(image_info,image,pattern,exception);
1574         else
1575 #endif
1576         if (image != (Image *) NULL)
1577           value=GetImageProperty(image,pattern,exception);
1578         if ((value == (const char *) NULL) && (image != (Image *) NULL))
1579           value=GetImageArtifact(image,pattern);
1580         if ((value == (const char *) NULL) &&
1581             (image_info != (ImageInfo *) NULL))
1582           value=GetImageOption(image_info,pattern);
1583         if (value == (const char *) NULL)
1584           break;
1585         q--;
1586         c=(*q);
1587         *q='\0';
1588         (void) CopyMagickString(filename+(p-format-length),value,(size_t)
1589           (MaxTextExtent-(p-format-length)));
1590         length+=strlen(pattern)-1;
1591         *q=c;
1592         (void) ConcatenateMagickString(filename,r+1,MaxTextExtent);
1593         canonical=MagickTrue;
1594         if (*(q-1) != '%')
1595           break;
1596         p++;
1597         break;
1598       }
1599       default:
1600         break;
1601     }
1602   }
1603   for (q=filename; *q != '\0'; q++)
1604     if ((*q == '%') && (*(q+1) == '%'))
1605       {
1606         (void) CopyMagickString(q,q+1,(size_t) (MaxTextExtent-(q-filename)));
1607         canonical=MagickTrue;
1608       }
1609   if (canonical == MagickFalse)
1610     (void) CopyMagickString(filename,format,MaxTextExtent);
1611   return(strlen(filename));
1612 }
1613 \f
1614 /*
1615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1616 %                                                                             %
1617 %                                                                             %
1618 %                                                                             %
1619 %   I s H i g h D y n a m i c R a n g e I m a g e                             %
1620 %                                                                             %
1621 %                                                                             %
1622 %                                                                             %
1623 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 %
1625 %  IsHighDynamicRangeImage() returns MagickTrue if any pixel component is
1626 %  non-integer or exceeds the bounds of the quantum depth (e.g. for Q16
1627 %  0..65535.
1628 %
1629 %  The format of the IsHighDynamicRangeImage method is:
1630 %
1631 %      MagickBooleanType IsHighDynamicRangeImage(const Image *image,
1632 %        ExceptionInfo *exception)
1633 %
1634 %  A description of each parameter follows:
1635 %
1636 %    o image: the image.
1637 %
1638 %    o exception: return any errors or warnings in this structure.
1639 %
1640 */
1641 MagickExport MagickBooleanType IsHighDynamicRangeImage(const Image *image,
1642   ExceptionInfo *exception)
1643 {
1644 #if !defined(MAGICKCORE_HDRI_SUPPORT)
1645   (void) image;
1646   (void) exception;
1647   return(MagickFalse);
1648 #else
1649   CacheView
1650     *image_view;
1651
1652   MagickBooleanType
1653     status;
1654
1655   ssize_t
1656     y;
1657
1658   assert(image != (Image *) NULL);
1659   assert(image->signature == MagickSignature);
1660   if (image->debug != MagickFalse)
1661     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1662   status=MagickTrue;
1663   image_view=AcquireVirtualCacheView(image,exception);
1664 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1665   #pragma omp parallel for schedule(static,4) shared(status) \
1666     magick_threads(image,image,image->rows,1)
1667 #endif
1668   for (y=0; y < (ssize_t) image->rows; y++)
1669   {
1670     register const Quantum
1671       *p;
1672
1673     register ssize_t
1674       x;
1675
1676     if (status == MagickFalse)
1677       continue;
1678     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1679     if (p == (const Quantum *) NULL)
1680       {
1681         status=MagickFalse;
1682         continue;
1683       }
1684     for (x=0; x < (ssize_t) image->columns; x++)
1685     {
1686       register ssize_t
1687         i;
1688
1689       if (GetPixelMask(image,p) == 0)
1690         {
1691           p+=GetPixelChannels(image);
1692           continue;
1693         }
1694       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1695       {
1696         double
1697           pixel;
1698
1699         PixelTrait
1700           traits;
1701
1702         traits=GetPixelChannelTraits(image,(PixelChannel) i);
1703         if (traits == UndefinedPixelTrait)
1704           continue;
1705         pixel=(double) p[i];
1706         if ((pixel < 0.0) || (pixel > QuantumRange) ||
1707             (pixel != (QuantumAny) pixel))
1708           break;
1709       }
1710       p+=GetPixelChannels(image);
1711       if (i < (ssize_t) GetPixelChannels(image))
1712         status=MagickFalse;
1713     }
1714     if (x < (ssize_t) image->columns)
1715       status=MagickFalse;
1716   }
1717   image_view=DestroyCacheView(image_view);
1718   return(status != MagickFalse ? MagickFalse : MagickTrue);
1719 #endif
1720 }
1721 \f
1722 /*
1723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1724 %                                                                             %
1725 %                                                                             %
1726 %                                                                             %
1727 %     I s I m a g e O b j e c t                                               %
1728 %                                                                             %
1729 %                                                                             %
1730 %                                                                             %
1731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1732 %
1733 %  IsImageObject() returns MagickTrue if the image sequence contains a valid
1734 %  set of image objects.
1735 %
1736 %  The format of the IsImageObject method is:
1737 %
1738 %      MagickBooleanType IsImageObject(const Image *image)
1739 %
1740 %  A description of each parameter follows:
1741 %
1742 %    o image: the image.
1743 %
1744 */
1745 MagickExport MagickBooleanType IsImageObject(const Image *image)
1746 {
1747   register const Image
1748     *p;
1749
1750   assert(image != (Image *) NULL);
1751   if (image->debug != MagickFalse)
1752     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1753   for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1754     if (p->signature != MagickSignature)
1755       return(MagickFalse);
1756   return(MagickTrue);
1757 }
1758 \f
1759 /*
1760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1761 %                                                                             %
1762 %                                                                             %
1763 %                                                                             %
1764 %     I s T a i n t I m a g e                                                 %
1765 %                                                                             %
1766 %                                                                             %
1767 %                                                                             %
1768 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1769 %
1770 %  IsTaintImage() returns MagickTrue any pixel in the image has been altered
1771 %  since it was first constituted.
1772 %
1773 %  The format of the IsTaintImage method is:
1774 %
1775 %      MagickBooleanType IsTaintImage(const Image *image)
1776 %
1777 %  A description of each parameter follows:
1778 %
1779 %    o image: the image.
1780 %
1781 */
1782 MagickExport MagickBooleanType IsTaintImage(const Image *image)
1783 {
1784   char
1785     magick[MaxTextExtent],
1786     filename[MaxTextExtent];
1787
1788   register const Image
1789     *p;
1790
1791   assert(image != (Image *) NULL);
1792   if (image->debug != MagickFalse)
1793     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1794   assert(image->signature == MagickSignature);
1795   (void) CopyMagickString(magick,image->magick,MaxTextExtent);
1796   (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1797   for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1798   {
1799     if (p->taint != MagickFalse)
1800       return(MagickTrue);
1801     if (LocaleCompare(p->magick,magick) != 0)
1802       return(MagickTrue);
1803     if (LocaleCompare(p->filename,filename) != 0)
1804       return(MagickTrue);
1805   }
1806   return(MagickFalse);
1807 }
1808 \f
1809 /*
1810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1811 %                                                                             %
1812 %                                                                             %
1813 %                                                                             %
1814 %   M o d i f y I m a g e                                                     %
1815 %                                                                             %
1816 %                                                                             %
1817 %                                                                             %
1818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1819 %
1820 %  ModifyImage() ensures that there is only a single reference to the image
1821 %  to be modified, updating the provided image pointer to point to a clone of
1822 %  the original image if necessary.
1823 %
1824 %  The format of the ModifyImage method is:
1825 %
1826 %      MagickBooleanType ModifyImage(Image *image,ExceptionInfo *exception)
1827 %
1828 %  A description of each parameter follows:
1829 %
1830 %    o image: the image.
1831 %
1832 %    o exception: return any errors or warnings in this structure.
1833 %
1834 */
1835 MagickExport MagickBooleanType ModifyImage(Image **image,
1836   ExceptionInfo *exception)
1837 {
1838   Image
1839     *clone_image;
1840
1841   assert(image != (Image **) NULL);
1842   assert(*image != (Image *) NULL);
1843   assert((*image)->signature == MagickSignature);
1844   if ((*image)->debug != MagickFalse)
1845     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
1846   if (GetImageReferenceCount(*image) <= 1)
1847     return(MagickTrue);
1848   clone_image=CloneImage(*image,0,0,MagickTrue,exception);
1849   LockSemaphoreInfo((*image)->semaphore);
1850   (*image)->reference_count--;
1851   UnlockSemaphoreInfo((*image)->semaphore);
1852   *image=clone_image;
1853   return(MagickTrue);
1854 }
1855 \f
1856 /*
1857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1858 %                                                                             %
1859 %                                                                             %
1860 %                                                                             %
1861 %   N e w M a g i c k I m a g e                                               %
1862 %                                                                             %
1863 %                                                                             %
1864 %                                                                             %
1865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1866 %
1867 %  NewMagickImage() creates a blank image canvas of the specified size and
1868 %  background color.
1869 %
1870 %  The format of the NewMagickImage method is:
1871 %
1872 %      Image *NewMagickImage(const ImageInfo *image_info,
1873 %        const size_t width,const size_t height,const PixelInfo *background,
1874 %        ExceptionInfo *exception)
1875 %
1876 %  A description of each parameter follows:
1877 %
1878 %    o image: the image.
1879 %
1880 %    o width: the image width.
1881 %
1882 %    o height: the image height.
1883 %
1884 %    o background: the image color.
1885 %
1886 %    o exception: return any errors or warnings in this structure.
1887 %
1888 */
1889 MagickExport Image *NewMagickImage(const ImageInfo *image_info,
1890   const size_t width,const size_t height,const PixelInfo *background,
1891   ExceptionInfo *exception)
1892 {
1893   CacheView
1894     *image_view;
1895
1896   Image
1897     *image;
1898
1899   MagickBooleanType
1900     status;
1901
1902   ssize_t
1903     y;
1904
1905   assert(image_info != (const ImageInfo *) NULL);
1906   if (image_info->debug != MagickFalse)
1907     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1908   assert(image_info->signature == MagickSignature);
1909   assert(background != (const PixelInfo *) NULL);
1910   image=AcquireImage(image_info,exception);
1911   image->columns=width;
1912   image->rows=height;
1913   image->colorspace=background->colorspace;
1914   image->alpha_trait=background->alpha_trait;
1915   image->fuzz=background->fuzz;
1916   image->depth=background->depth;
1917   status=MagickTrue;
1918   image_view=AcquireAuthenticCacheView(image,exception);
1919 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1920   #pragma omp parallel for schedule(static,4) shared(status) \
1921     magick_threads(image,image,image->rows,1)
1922 #endif
1923   for (y=0; y < (ssize_t) image->rows; y++)
1924   {
1925     register Quantum
1926       *restrict q;
1927
1928     register ssize_t
1929       x;
1930
1931     if (status == MagickFalse)
1932       continue;
1933     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1934     if (q == (Quantum *) NULL)
1935       {
1936         status=MagickFalse;
1937         continue;
1938       }
1939     for (x=0; x < (ssize_t) image->columns; x++)
1940     {
1941       SetPixelInfoPixel(image,background,q);
1942       q+=GetPixelChannels(image);
1943     }
1944     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1945       status=MagickFalse;
1946   }
1947   image_view=DestroyCacheView(image_view);
1948   if (status == MagickFalse)
1949     image=DestroyImage(image);
1950   return(image);
1951 }
1952 \f
1953 /*
1954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1955 %                                                                             %
1956 %                                                                             %
1957 %                                                                             %
1958 %   R e f e r e n c e I m a g e                                               %
1959 %                                                                             %
1960 %                                                                             %
1961 %                                                                             %
1962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1963 %
1964 %  ReferenceImage() increments the reference count associated with an image
1965 %  returning a pointer to the image.
1966 %
1967 %  The format of the ReferenceImage method is:
1968 %
1969 %      Image *ReferenceImage(Image *image)
1970 %
1971 %  A description of each parameter follows:
1972 %
1973 %    o image: the image.
1974 %
1975 */
1976 MagickExport Image *ReferenceImage(Image *image)
1977 {
1978   assert(image != (Image *) NULL);
1979   if (image->debug != MagickFalse)
1980     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1981   assert(image->signature == MagickSignature);
1982   LockSemaphoreInfo(image->semaphore);
1983   image->reference_count++;
1984   UnlockSemaphoreInfo(image->semaphore);
1985   return(image);
1986 }
1987 \f
1988 /*
1989 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1990 %                                                                             %
1991 %                                                                             %
1992 %                                                                             %
1993 %   R e s e t I m a g e P a g e                                               %
1994 %                                                                             %
1995 %                                                                             %
1996 %                                                                             %
1997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1998 %
1999 %  ResetImagePage() resets the image page canvas and position.
2000 %
2001 %  The format of the ResetImagePage method is:
2002 %
2003 %      MagickBooleanType ResetImagePage(Image *image,const char *page)
2004 %
2005 %  A description of each parameter follows:
2006 %
2007 %    o image: the image.
2008 %
2009 %    o page: the relative page specification.
2010 %
2011 */
2012 MagickExport MagickBooleanType ResetImagePage(Image *image,const char *page)
2013 {
2014   MagickStatusType
2015     flags;
2016
2017   RectangleInfo
2018     geometry;
2019
2020   assert(image != (Image *) NULL);
2021   assert(image->signature == MagickSignature);
2022   if (image->debug != MagickFalse)
2023     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2024   flags=ParseAbsoluteGeometry(page,&geometry);
2025   if ((flags & WidthValue) != 0)
2026     {
2027       if ((flags & HeightValue) == 0)
2028         geometry.height=geometry.width;
2029       image->page.width=geometry.width;
2030       image->page.height=geometry.height;
2031     }
2032   if ((flags & AspectValue) != 0)
2033     {
2034       if ((flags & XValue) != 0)
2035         image->page.x+=geometry.x;
2036       if ((flags & YValue) != 0)
2037         image->page.y+=geometry.y;
2038     }
2039   else
2040     {
2041       if ((flags & XValue) != 0)
2042         {
2043           image->page.x=geometry.x;
2044           if ((image->page.width == 0) && (geometry.x > 0))
2045             image->page.width=image->columns+geometry.x;
2046         }
2047       if ((flags & YValue) != 0)
2048         {
2049           image->page.y=geometry.y;
2050           if ((image->page.height == 0) && (geometry.y > 0))
2051             image->page.height=image->rows+geometry.y;
2052         }
2053     }
2054   return(MagickTrue);
2055 }
2056 \f
2057 /*
2058 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2059 %                                                                             %
2060 %                                                                             %
2061 %                                                                             %
2062 %   S e t I m a g e A l p h a C h a n n e l                                   %
2063 %                                                                             %
2064 %                                                                             %
2065 %                                                                             %
2066 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2067 %
2068 %  SetImageAlphaChannel() activates, deactivates, resets, or sets the alpha
2069 %  channel.
2070 %
2071 %  The format of the SetImageAlphaChannel method is:
2072 %
2073 %      MagickBooleanType SetImageAlphaChannel(Image *image,
2074 %        const AlphaChannelOption alpha_type,ExceptionInfo *exception)
2075 %
2076 %  A description of each parameter follows:
2077 %
2078 %    o image: the image.
2079 %
2080 %    o alpha_type:  The alpha channel type: ActivateAlphaChannel,
2081 %      CopyAlphaChannel, DeactivateAlphaChannel, ExtractAlphaChannel,
2082 %      OpaqueAlphaChannel, SetAlphaChannel, ShapeAlphaChannel, and
2083 %      TransparentAlphaChannel.
2084 %
2085 %    o exception: return any errors or warnings in this structure.
2086 %
2087 */
2088
2089 static inline void FlattenPixelInfo(const Image *image,const PixelInfo *p,
2090   const double alpha,const Quantum *q,const double beta,
2091   Quantum *composite)
2092 {
2093   double
2094     Da,
2095     gamma,
2096     Sa;
2097
2098   register ssize_t
2099     i;
2100
2101   /*
2102     Compose pixel p over pixel q with the given alpha.
2103   */
2104   Sa=QuantumScale*alpha;
2105   Da=QuantumScale*beta,
2106   gamma=Sa*(-Da)+Sa+Da;
2107   gamma=PerceptibleReciprocal(gamma);
2108   for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2109   {
2110     PixelChannel channel=GetPixelChannelChannel(image,i);
2111     PixelTrait traits=GetPixelChannelTraits(image,channel);
2112     if (traits == UndefinedPixelTrait)
2113       continue;
2114     switch (channel)
2115     {
2116       case RedPixelChannel:
2117       {
2118         composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
2119           (double) p->red,alpha));
2120         break;
2121       }
2122       case GreenPixelChannel:
2123       {
2124         composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
2125           (double) p->green,alpha));
2126         break;
2127       }
2128       case BluePixelChannel:
2129       {
2130         composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
2131           (double) p->blue,alpha));
2132         break;
2133       }
2134       case BlackPixelChannel:
2135       {
2136         composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
2137           (double) p->black,alpha));
2138         break;
2139       }
2140       case AlphaPixelChannel:
2141       {
2142         composite[i]=ClampToQuantum(QuantumRange*(Sa*(-Da)+Sa+Da));
2143         break;
2144       }
2145       default:
2146         break;
2147     }
2148   }
2149 }
2150
2151 MagickExport MagickBooleanType SetImageAlphaChannel(Image *image,
2152   const AlphaChannelOption alpha_type,ExceptionInfo *exception)
2153 {
2154   MagickBooleanType
2155     status;
2156
2157   assert(image != (Image *) NULL);
2158   if (image->debug != MagickFalse)
2159     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2160   assert(image->signature == MagickSignature);
2161   status=MagickTrue;
2162   switch (alpha_type)
2163   {
2164     case ActivateAlphaChannel:
2165     {
2166       image->alpha_trait=BlendPixelTrait;
2167       break;
2168     }
2169     case BackgroundAlphaChannel:
2170     {
2171       CacheView
2172         *image_view;
2173
2174       ssize_t
2175         y;
2176
2177       /*
2178         Set transparent pixels to background color.
2179       */
2180       if (image->alpha_trait != BlendPixelTrait)
2181         break;
2182       if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2183         break;
2184       image_view=AcquireAuthenticCacheView(image,exception);
2185 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2186       #pragma omp parallel for schedule(static,4) shared(status) \
2187         magick_threads(image,image,image->rows,1)
2188 #endif
2189       for (y=0; y < (ssize_t) image->rows; y++)
2190       {
2191         register Quantum
2192           *restrict q;
2193
2194         register ssize_t
2195           x;
2196
2197         if (status == MagickFalse)
2198           continue;
2199         q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
2200           exception);
2201         if (q == (Quantum *) NULL)
2202           {
2203             status=MagickFalse;
2204             continue;
2205           }
2206         for (x=0; x < (ssize_t) image->columns; x++)
2207         {
2208           if (GetPixelAlpha(image,q) == TransparentAlpha)
2209             {
2210               SetPixelInfoPixel(image,&image->background_color,q);
2211               SetPixelChannel(image,AlphaPixelChannel,TransparentAlpha,q);
2212             }
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=CopyPixelTrait;
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,sRGBColorspace,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) SeekBlob(image,-((MagickOffsetType) count),SEEK_CUR);
2911       (void) CloseBlob(image);
2912       image=DestroyImage(image);
2913       /*
2914         Check magic.xml configuration file.
2915       */
2916       sans_exception=AcquireExceptionInfo();
2917       magic_info=GetMagicInfo(magick,(size_t) count,sans_exception);
2918       if ((magic_info != (const MagicInfo *) NULL) &&
2919           (GetMagicName(magic_info) != (char *) NULL))
2920         {
2921           (void) CopyMagickString(image_info->magick,GetMagicName(magic_info),
2922             MaxTextExtent);
2923           magick_info=GetMagickInfo(image_info->magick,sans_exception);
2924           if ((magick_info == (const MagickInfo *) NULL) ||
2925               (GetMagickEndianSupport(magick_info) == MagickFalse))
2926             image_info->endian=UndefinedEndian;
2927           sans_exception=DestroyExceptionInfo(sans_exception);
2928           return(MagickTrue);
2929         }
2930       magick_info=GetMagickInfo(image_info->magick,sans_exception);
2931       if ((magick_info == (const MagickInfo *) NULL) ||
2932           (GetMagickEndianSupport(magick_info) == MagickFalse))
2933         image_info->endian=UndefinedEndian;
2934       sans_exception=DestroyExceptionInfo(sans_exception);
2935     }
2936   return(MagickTrue);
2937 }
2938 \f
2939 /*
2940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2941 %                                                                             %
2942 %                                                                             %
2943 %                                                                             %
2944 %   S e t I m a g e I n f o B l o b                                           %
2945 %                                                                             %
2946 %                                                                             %
2947 %                                                                             %
2948 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2949 %
2950 %  SetImageInfoBlob() sets the image info blob member.
2951 %
2952 %  The format of the SetImageInfoBlob method is:
2953 %
2954 %      void SetImageInfoBlob(ImageInfo *image_info,const void *blob,
2955 %        const size_t length)
2956 %
2957 %  A description of each parameter follows:
2958 %
2959 %    o image_info: the image info.
2960 %
2961 %    o blob: the blob.
2962 %
2963 %    o length: the blob length.
2964 %
2965 */
2966 MagickExport void SetImageInfoBlob(ImageInfo *image_info,const void *blob,
2967   const size_t length)
2968 {
2969   assert(image_info != (ImageInfo *) NULL);
2970   assert(image_info->signature == MagickSignature);
2971   if (image_info->debug != MagickFalse)
2972     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2973       image_info->filename);
2974   image_info->blob=(void *) blob;
2975   image_info->length=length;
2976 }
2977 \f
2978 /*
2979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2980 %                                                                             %
2981 %                                                                             %
2982 %                                                                             %
2983 %   S e t I m a g e I n f o F i l e                                           %
2984 %                                                                             %
2985 %                                                                             %
2986 %                                                                             %
2987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2988 %
2989 %  SetImageInfoFile() sets the image info file member.
2990 %
2991 %  The format of the SetImageInfoFile method is:
2992 %
2993 %      void SetImageInfoFile(ImageInfo *image_info,FILE *file)
2994 %
2995 %  A description of each parameter follows:
2996 %
2997 %    o image_info: the image info.
2998 %
2999 %    o file: the file.
3000 %
3001 */
3002 MagickExport void SetImageInfoFile(ImageInfo *image_info,FILE *file)
3003 {
3004   assert(image_info != (ImageInfo *) NULL);
3005   assert(image_info->signature == MagickSignature);
3006   if (image_info->debug != MagickFalse)
3007     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3008       image_info->filename);
3009   image_info->file=file;
3010 }
3011 \f
3012 /*
3013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3014 %                                                                             %
3015 %                                                                             %
3016 %                                                                             %
3017 %   S e t I m a g e M a s k                                                   %
3018 %                                                                             %
3019 %                                                                             %
3020 %                                                                             %
3021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3022 %
3023 %  SetImageMask() associates a mask with the image.  The mask must be the same
3024 %  dimensions as the image.
3025 %
3026 %  The format of the SetImageMask method is:
3027 %
3028 %      MagickBooleanType SetImageMask(Image *image,const Image *mask,
3029 %        ExceptionInfo *exception)
3030 %
3031 %  A description of each parameter follows:
3032 %
3033 %    o image: the image.
3034 %
3035 %    o mask: the image mask.
3036 %
3037 %    o exception: return any errors or warnings in this structure.
3038 %
3039 */
3040 MagickExport MagickBooleanType SetImageMask(Image *image,const Image *mask,
3041   ExceptionInfo *exception)
3042 {
3043   CacheView
3044     *mask_view,
3045     *image_view;
3046
3047   MagickBooleanType
3048     status;
3049
3050   ssize_t
3051     y;
3052
3053   /*
3054     Set image mask.
3055   */
3056   assert(image != (Image *) NULL);
3057   if (image->debug != MagickFalse)
3058     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3059   assert(image->signature == MagickSignature);
3060   if (mask == (const Image *) NULL)
3061     {
3062       image->mask=MagickFalse;
3063       return(SyncImagePixelCache(image,exception));
3064     }
3065   image->mask=MagickTrue;
3066   if (SyncImagePixelCache(image,exception) == MagickFalse)
3067     return(MagickFalse);
3068   status=MagickTrue;
3069   mask_view=AcquireVirtualCacheView(mask,exception);
3070   image_view=AcquireAuthenticCacheView(image,exception);
3071 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3072   #pragma omp parallel for schedule(static,4) shared(status) \
3073     magick_threads(mask,image,1,1)
3074 #endif
3075   for (y=0; y < (ssize_t) image->rows; y++)
3076   {
3077     register const Quantum
3078       *restrict p;
3079
3080     register Quantum
3081       *restrict q;
3082
3083     register ssize_t
3084       x;
3085
3086     if (status == MagickFalse)
3087       continue;
3088     p=GetCacheViewVirtualPixels(mask_view,0,y,mask->columns,1,exception);
3089     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3090     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3091       {
3092         status=MagickFalse;
3093         continue;
3094       }
3095     for (x=0; x < (ssize_t) image->columns; x++)
3096     {
3097       SetPixelMask(image,ClampToQuantum(GetPixelIntensity(mask,p)),q);
3098       p+=GetPixelChannels(mask);
3099       q+=GetPixelChannels(image);
3100     }
3101     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3102       status=MagickFalse;
3103   }
3104   mask_view=DestroyCacheView(mask_view);
3105   image_view=DestroyCacheView(image_view);
3106   return(status);
3107 }
3108 \f
3109 /*
3110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3111 %                                                                             %
3112 %                                                                             %
3113 %                                                                             %
3114 %     S e t I m a g e A l p h a                                               %
3115 %                                                                             %
3116 %                                                                             %
3117 %                                                                             %
3118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3119 %
3120 %  SetImageAlpha() sets the alpha levels of the image.
3121 %
3122 %  The format of the SetImageAlpha method is:
3123 %
3124 %      MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
3125 %        ExceptionInfo *exception)
3126 %
3127 %  A description of each parameter follows:
3128 %
3129 %    o image: the image.
3130 %
3131 %    o Alpha: the level of transparency: 0 is fully opaque and QuantumRange is
3132 %      fully transparent.
3133 %
3134 */
3135 MagickExport MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
3136   ExceptionInfo *exception)
3137 {
3138   CacheView
3139     *image_view;
3140
3141   MagickBooleanType
3142     status;
3143
3144   ssize_t
3145     y;
3146
3147   assert(image != (Image *) NULL);
3148   if (image->debug != MagickFalse)
3149     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3150   assert(image->signature == MagickSignature);
3151   image->alpha_trait=BlendPixelTrait;
3152   status=MagickTrue;
3153   image_view=AcquireAuthenticCacheView(image,exception);
3154 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3155   #pragma omp parallel for schedule(static,4) shared(status) \
3156     magick_threads(image,image,image->rows,1)
3157 #endif
3158   for (y=0; y < (ssize_t) image->rows; y++)
3159   {
3160     register Quantum
3161       *restrict q;
3162
3163     register ssize_t
3164       x;
3165
3166     if (status == MagickFalse)
3167       continue;
3168     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3169     if (q == (Quantum *) NULL)
3170       {
3171         status=MagickFalse;
3172         continue;
3173       }
3174     for (x=0; x < (ssize_t) image->columns; x++)
3175     {
3176       SetPixelAlpha(image,alpha,q);
3177       q+=GetPixelChannels(image);
3178     }
3179     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3180       status=MagickFalse;
3181   }
3182   image_view=DestroyCacheView(image_view);
3183   return(status);
3184 }
3185 \f
3186 /*
3187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3188 %                                                                             %
3189 %                                                                             %
3190 %                                                                             %
3191 %   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                       %
3192 %                                                                             %
3193 %                                                                             %
3194 %                                                                             %
3195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3196 %
3197 %  SetImageVirtualPixelMethod() sets the "virtual pixels" method for the
3198 %  image and returns the previous setting.  A virtual pixel is any pixel access
3199 %  that is outside the boundaries of the image cache.
3200 %
3201 %  The format of the SetImageVirtualPixelMethod() method is:
3202 %
3203 %      VirtualPixelMethod SetImageVirtualPixelMethod(Image *image,
3204 %        const VirtualPixelMethod virtual_pixel_method,ExceptionInfo *exception)
3205 %
3206 %  A description of each parameter follows:
3207 %
3208 %    o image: the image.
3209 %
3210 %    o virtual_pixel_method: choose the type of virtual pixel.
3211 %
3212 %    o exception: return any errors or warnings in this structure.
3213 %
3214 */
3215 MagickExport VirtualPixelMethod SetImageVirtualPixelMethod(Image *image,
3216   const VirtualPixelMethod virtual_pixel_method,ExceptionInfo *exception)
3217 {
3218   assert(image != (const Image *) NULL);
3219   assert(image->signature == MagickSignature);
3220   if (image->debug != MagickFalse)
3221     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3222   return(SetPixelCacheVirtualMethod(image,virtual_pixel_method,exception));
3223 }
3224 \f
3225 /*
3226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3227 %                                                                             %
3228 %                                                                             %
3229 %                                                                             %
3230 %     S m u s h I m a g e s                                                   %
3231 %                                                                             %
3232 %                                                                             %
3233 %                                                                             %
3234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3235 %
3236 %  SmushImages() takes all images from the current image pointer to the end
3237 %  of the image list and smushes them to each other top-to-bottom if the
3238 %  stack parameter is true, otherwise left-to-right.
3239 %
3240 %  The current gravity setting now effects how the image is justified in the
3241 %  final image.
3242 %
3243 %  The format of the SmushImages method is:
3244 %
3245 %      Image *SmushImages(const Image *images,const MagickBooleanType stack,
3246 %        ExceptionInfo *exception)
3247 %
3248 %  A description of each parameter follows:
3249 %
3250 %    o images: the image sequence.
3251 %
3252 %    o stack: A value other than 0 stacks the images top-to-bottom.
3253 %
3254 %    o offset: minimum distance in pixels between images.
3255 %
3256 %    o exception: return any errors or warnings in this structure.
3257 %
3258 */
3259
3260 static ssize_t SmushXGap(const Image *smush_image,const Image *images,
3261   const ssize_t offset,ExceptionInfo *exception)
3262 {
3263   CacheView
3264     *left_view,
3265     *right_view;
3266
3267   const Image
3268     *left_image,
3269     *right_image;
3270
3271   RectangleInfo
3272     left_geometry,
3273     right_geometry;
3274
3275   register const Quantum
3276     *p;
3277
3278   register ssize_t
3279     i,
3280     y;
3281
3282   size_t
3283     gap;
3284
3285   ssize_t
3286     x;
3287
3288   if (images->previous == (Image *) NULL)
3289     return(0);
3290   right_image=images;
3291   SetGeometry(smush_image,&right_geometry);
3292   GravityAdjustGeometry(right_image->columns,right_image->rows,
3293     right_image->gravity,&right_geometry);
3294   left_image=images->previous;
3295   SetGeometry(smush_image,&left_geometry);
3296   GravityAdjustGeometry(left_image->columns,left_image->rows,
3297     left_image->gravity,&left_geometry);
3298   gap=right_image->columns;
3299   left_view=AcquireVirtualCacheView(left_image,exception);
3300   right_view=AcquireVirtualCacheView(right_image,exception);
3301   for (y=0; y < (ssize_t) smush_image->rows; y++)
3302   {
3303     for (x=(ssize_t) left_image->columns-1; x > 0; x--)
3304     {
3305       p=GetCacheViewVirtualPixels(left_view,x,left_geometry.y+y,1,1,exception);
3306       if ((p == (const Quantum *) NULL) ||
3307           (GetPixelAlpha(left_image,p) != TransparentAlpha) ||
3308           ((left_image->columns-x-1) >= gap))
3309         break;
3310     }
3311     i=(ssize_t) left_image->columns-x-1;
3312     for (x=0; x < (ssize_t) right_image->columns; x++)
3313     {
3314       p=GetCacheViewVirtualPixels(right_view,x,right_geometry.y+y,1,1,
3315         exception);
3316       if ((p == (const Quantum *) NULL) ||
3317           (GetPixelAlpha(right_image,p) != TransparentAlpha) ||
3318           ((x+i) >= (ssize_t) gap))
3319         break;
3320     }
3321     if ((x+i) < (ssize_t) gap)
3322       gap=(size_t) (x+i);
3323   }
3324   right_view=DestroyCacheView(right_view);
3325   left_view=DestroyCacheView(left_view);
3326   if (y < (ssize_t) smush_image->rows)
3327     return(offset);
3328   return((ssize_t) gap-offset);
3329 }
3330
3331 static ssize_t SmushYGap(const Image *smush_image,const Image *images,
3332   const ssize_t offset,ExceptionInfo *exception)
3333 {
3334   CacheView
3335     *bottom_view,
3336     *top_view;
3337
3338   const Image
3339     *bottom_image,
3340     *top_image;
3341
3342   RectangleInfo
3343     bottom_geometry,
3344     top_geometry;
3345
3346   register const Quantum
3347     *p;
3348
3349   register ssize_t
3350     i,
3351     x;
3352
3353   size_t
3354     gap;
3355
3356   ssize_t
3357     y;
3358
3359   if (images->previous == (Image *) NULL)
3360     return(0);
3361   bottom_image=images;
3362   SetGeometry(smush_image,&bottom_geometry);
3363   GravityAdjustGeometry(bottom_image->columns,bottom_image->rows,
3364     bottom_image->gravity,&bottom_geometry);
3365   top_image=images->previous;
3366   SetGeometry(smush_image,&top_geometry);
3367   GravityAdjustGeometry(top_image->columns,top_image->rows,top_image->gravity,
3368     &top_geometry);
3369   gap=bottom_image->rows;
3370   top_view=AcquireVirtualCacheView(top_image,exception);
3371   bottom_view=AcquireVirtualCacheView(bottom_image,exception);
3372   for (x=0; x < (ssize_t) smush_image->columns; x++)
3373   {
3374     for (y=(ssize_t) top_image->rows-1; y > 0; y--)
3375     {
3376       p=GetCacheViewVirtualPixels(top_view,top_geometry.x+x,y,1,1,exception);
3377       if ((p == (const Quantum *) NULL) ||
3378           (GetPixelAlpha(top_image,p) != TransparentAlpha) ||
3379           ((top_image->rows-y-1) >= gap))
3380         break;
3381     }
3382     i=(ssize_t) top_image->rows-y-1;
3383     for (y=0; y < (ssize_t) bottom_image->rows; y++)
3384     {
3385       p=GetCacheViewVirtualPixels(bottom_view,bottom_geometry.x+x,y,1,1,
3386         exception);
3387       if ((p == (const Quantum *) NULL) ||
3388           (GetPixelAlpha(bottom_image,p) != TransparentAlpha) ||
3389           ((y+i) >= (ssize_t) gap))
3390         break;
3391     }
3392     if ((y+i) < (ssize_t) gap)
3393       gap=(size_t) (y+i);
3394   }
3395   bottom_view=DestroyCacheView(bottom_view);
3396   top_view=DestroyCacheView(top_view);
3397   if (x < (ssize_t) smush_image->columns)
3398     return(offset);
3399   return((ssize_t) gap-offset);
3400 }
3401
3402 MagickExport Image *SmushImages(const Image *images,
3403   const MagickBooleanType stack,const ssize_t offset,ExceptionInfo *exception)
3404 {
3405 #define SmushImageTag  "Smush/Image"
3406
3407   const Image
3408     *image;
3409
3410   Image
3411     *smush_image;
3412
3413   MagickBooleanType
3414     proceed,
3415     status;
3416
3417   MagickOffsetType
3418     n;
3419
3420   PixelTrait
3421     alpha_trait;
3422
3423   RectangleInfo
3424     geometry;
3425
3426   register const Image
3427     *next;
3428
3429   size_t
3430     height,
3431     number_images,
3432     width;
3433
3434   ssize_t
3435     x_offset,
3436     y_offset;
3437
3438   /*
3439     Compute maximum area of smushed area.
3440   */
3441   assert(images != (Image *) NULL);
3442   assert(images->signature == MagickSignature);
3443   if (images->debug != MagickFalse)
3444     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
3445   assert(exception != (ExceptionInfo *) NULL);
3446   assert(exception->signature == MagickSignature);
3447   image=images;
3448   alpha_trait=image->alpha_trait;
3449   number_images=1;
3450   width=image->columns;
3451   height=image->rows;
3452   next=GetNextImageInList(image);
3453   for ( ; next != (Image *) NULL; next=GetNextImageInList(next))
3454   {
3455     if (next->alpha_trait == BlendPixelTrait)
3456       alpha_trait=BlendPixelTrait;
3457     number_images++;
3458     if (stack != MagickFalse)
3459       {
3460         if (next->columns > width)
3461           width=next->columns;
3462         height+=next->rows;
3463         if (next->previous != (Image *) NULL)
3464           height+=offset;
3465         continue;
3466       }
3467     width+=next->columns;
3468     if (next->previous != (Image *) NULL)
3469       width+=offset;
3470     if (next->rows > height)
3471       height=next->rows;
3472   }
3473   /*
3474     Smush images.
3475   */
3476   smush_image=CloneImage(image,width,height,MagickTrue,exception);
3477   if (smush_image == (Image *) NULL)
3478     return((Image *) NULL);
3479   if (SetImageStorageClass(smush_image,DirectClass,exception) == MagickFalse)
3480     {
3481       smush_image=DestroyImage(smush_image);
3482       return((Image *) NULL);
3483     }
3484   smush_image->alpha_trait=alpha_trait;
3485   (void) SetImageBackgroundColor(smush_image,exception);
3486   status=MagickTrue;
3487   x_offset=0;
3488   y_offset=0;
3489   for (n=0; n < (MagickOffsetType) number_images; n++)
3490   {
3491     SetGeometry(smush_image,&geometry);
3492     GravityAdjustGeometry(image->columns,image->rows,image->gravity,&geometry);
3493     if (stack != MagickFalse)
3494       {
3495         x_offset-=geometry.x;
3496         y_offset-=SmushYGap(smush_image,image,offset,exception);
3497       }
3498     else
3499       {
3500         x_offset-=SmushXGap(smush_image,image,offset,exception);
3501         y_offset-=geometry.y;
3502       }
3503     status=CompositeImage(smush_image,image,OverCompositeOp,MagickTrue,x_offset,
3504       y_offset,exception);
3505     proceed=SetImageProgress(image,SmushImageTag,n,number_images);
3506     if (proceed == MagickFalse)
3507       break;
3508     if (stack == MagickFalse)
3509       {
3510         x_offset+=(ssize_t) image->columns;
3511         y_offset=0;
3512       }
3513     else
3514       {
3515         x_offset=0;
3516         y_offset+=(ssize_t) image->rows;
3517       }
3518     image=GetNextImageInList(image);
3519   }
3520   if (stack == MagickFalse)
3521     smush_image->columns=(size_t) x_offset;
3522   else
3523     smush_image->rows=(size_t) y_offset;
3524   if (status == MagickFalse)
3525     smush_image=DestroyImage(smush_image);
3526   return(smush_image);
3527 }
3528 \f
3529 /*
3530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3531 %                                                                             %
3532 %                                                                             %
3533 %                                                                             %
3534 %   S t r i p I m a g e                                                       %
3535 %                                                                             %
3536 %                                                                             %
3537 %                                                                             %
3538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3539 %
3540 %  StripImage() strips an image of all profiles and comments.
3541 %
3542 %  The format of the StripImage method is:
3543 %
3544 %      MagickBooleanType StripImage(Image *image,ExceptionInfo *exception)
3545 %
3546 %  A description of each parameter follows:
3547 %
3548 %    o image: the image.
3549 %
3550 %    o exception: return any errors or warnings in this structure.
3551 %
3552 */
3553 MagickExport MagickBooleanType StripImage(Image *image,ExceptionInfo *exception)
3554 {
3555   MagickBooleanType
3556     status;
3557
3558   assert(image != (Image *) NULL);
3559   if (image->debug != MagickFalse)
3560     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3561   (void) exception;
3562   DestroyImageProfiles(image);
3563   (void) DeleteImageProperty(image,"comment");
3564   (void) DeleteImageProperty(image,"date:create");
3565   (void) DeleteImageProperty(image,"date:modify");
3566   status=SetImageArtifact(image,"png:include-chunk","none,trns,gama");
3567   return(status);
3568 }
3569 \f
3570 /*
3571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3572 %                                                                             %
3573 %                                                                             %
3574 %                                                                             %
3575 +   S y n c I m a g e                                                         %
3576 %                                                                             %
3577 %                                                                             %
3578 %                                                                             %
3579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3580 %
3581 %  SyncImage() initializes the red, green, and blue intensities of each pixel
3582 %  as defined by the colormap index.
3583 %
3584 %  The format of the SyncImage method is:
3585 %
3586 %      MagickBooleanType SyncImage(Image *image,ExceptionInfo *exception)
3587 %
3588 %  A description of each parameter follows:
3589 %
3590 %    o image: the image.
3591 %
3592 %    o exception: return any errors or warnings in this structure.
3593 %
3594 */
3595
3596 static inline Quantum PushColormapIndex(Image *image,const Quantum index,
3597   MagickBooleanType *range_exception)
3598 {
3599   if ((size_t) index < image->colors)
3600     return(index);
3601   *range_exception=MagickTrue;
3602   return((Quantum) 0);
3603 }
3604
3605 MagickExport MagickBooleanType SyncImage(Image *image,ExceptionInfo *exception)
3606 {
3607   CacheView
3608     *image_view;
3609
3610   MagickBooleanType
3611     range_exception,
3612     status;
3613
3614   ssize_t
3615     y;
3616
3617   assert(image != (Image *) NULL);
3618   if (image->debug != MagickFalse)
3619     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3620   assert(image->signature == MagickSignature);
3621   if (image->storage_class == DirectClass)
3622     return(MagickFalse);
3623   range_exception=MagickFalse;
3624   status=MagickTrue;
3625   image_view=AcquireAuthenticCacheView(image,exception);
3626 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3627   #pragma omp parallel for schedule(static,4) shared(range_exception,status) \
3628     magick_threads(image,image,image->rows,1)
3629 #endif
3630   for (y=0; y < (ssize_t) image->rows; y++)
3631   {
3632     Quantum
3633       index;
3634
3635     register Quantum
3636       *restrict q;
3637
3638     register ssize_t
3639       x;
3640
3641     if (status == MagickFalse)
3642       continue;
3643     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3644     if (q == (Quantum *) NULL)
3645       {
3646         status=MagickFalse;
3647         continue;
3648       }
3649     for (x=0; x < (ssize_t) image->columns; x++)
3650     {
3651       index=PushColormapIndex(image,GetPixelIndex(image,q),&range_exception);
3652       SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
3653       q+=GetPixelChannels(image);
3654     }
3655     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3656       status=MagickFalse;
3657   }
3658   image_view=DestroyCacheView(image_view);
3659   if ((image->ping == MagickFalse) && (range_exception != MagickFalse))
3660     (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
3661       "InvalidColormapIndex","`%s'",image->filename);
3662   return(status);
3663 }
3664 \f
3665 /*
3666 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3667 %                                                                             %
3668 %                                                                             %
3669 %                                                                             %
3670 %   S y n c I m a g e S e t t i n g s                                         %
3671 %                                                                             %
3672 %                                                                             %
3673 %                                                                             %
3674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3675 %
3676 %  SyncImageSettings() syncs any image_info global options into per-image
3677 %  attributes.
3678 %
3679 %  Note: in IMv6 free form 'options' were always mapped into 'artifacts', so
3680 %  that operations and coders can find such settings.  In IMv7 if a desired
3681 %  per-image artifact is not set, then it will directly look for a global
3682 %  option as a fallback, as such this copy is no longer needed, only the
3683 %  link set up.
3684 %
3685 %  The format of the SyncImageSettings method is:
3686 %
3687 %      MagickBooleanType SyncImageSettings(const ImageInfo *image_info,
3688 %        Image *image,ExceptionInfo *exception)
3689 %      MagickBooleanType SyncImagesSettings(const ImageInfo *image_info,
3690 %        Image *image,ExceptionInfo *exception)
3691 %
3692 %  A description of each parameter follows:
3693 %
3694 %    o image_info: the image info.
3695 %
3696 %    o image: the image.
3697 %
3698 %    o exception: return any errors or warnings in this structure.
3699 %
3700 */
3701
3702 MagickExport MagickBooleanType SyncImagesSettings(ImageInfo *image_info,
3703   Image *images,ExceptionInfo *exception)
3704 {
3705   Image
3706     *image;
3707
3708   assert(image_info != (const ImageInfo *) NULL);
3709   assert(image_info->signature == MagickSignature);
3710   assert(images != (Image *) NULL);
3711   assert(images->signature == MagickSignature);
3712   if (images->debug != MagickFalse)
3713     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
3714   image=images;
3715   for ( ; image != (Image *) NULL; image=GetNextImageInList(image))
3716     (void) SyncImageSettings(image_info,image,exception);
3717   (void) DeleteImageOption(image_info,"page");
3718   return(MagickTrue);
3719 }
3720
3721 MagickExport MagickBooleanType SyncImageSettings(const ImageInfo *image_info,
3722   Image *image,ExceptionInfo *exception)
3723 {
3724   const char
3725     *option;
3726
3727   GeometryInfo
3728     geometry_info;
3729
3730   MagickStatusType
3731     flags;
3732
3733   ResolutionType
3734     units;
3735
3736   /*
3737     Sync image options.
3738   */
3739   assert(image_info != (const ImageInfo *) NULL);
3740   assert(image_info->signature == MagickSignature);
3741   assert(image != (Image *) NULL);
3742   assert(image->signature == MagickSignature);
3743   if (image->debug != MagickFalse)
3744     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3745   option=GetImageOption(image_info,"background");
3746   if (option != (const char *) NULL)
3747     (void) QueryColorCompliance(option,AllCompliance,&image->background_color,
3748       exception);
3749   option=GetImageOption(image_info,"black-point-compensation");
3750   if (option != (const char *) NULL)
3751     image->black_point_compensation=(MagickBooleanType) ParseCommandOption(
3752       MagickBooleanOptions,MagickFalse,option);
3753   option=GetImageOption(image_info,"blue-primary");
3754   if (option != (const char *) NULL)
3755     {
3756       flags=ParseGeometry(option,&geometry_info);
3757       image->chromaticity.blue_primary.x=geometry_info.rho;
3758       image->chromaticity.blue_primary.y=geometry_info.sigma;
3759       if ((flags & SigmaValue) == 0)
3760         image->chromaticity.blue_primary.y=image->chromaticity.blue_primary.x;
3761     }
3762   option=GetImageOption(image_info,"bordercolor");
3763   if (option != (const char *) NULL)
3764     (void) QueryColorCompliance(option,AllCompliance,&image->border_color,
3765       exception);
3766   option=GetImageOption(image_info,"channel");
3767   if (option != (const char *) NULL)
3768     (void) SetPixelChannelMask(image,(ChannelType)
3769       ParseChannelOption(option));
3770   /* FUTURE: do not sync compose to per-image compose setting here */
3771   option=GetImageOption(image_info,"compose");
3772   if (option != (const char *) NULL)
3773     image->compose=(CompositeOperator) ParseCommandOption(MagickComposeOptions,
3774       MagickFalse,option);
3775   /* -- */
3776   option=GetImageOption(image_info,"compress");
3777   if (option != (const char *) NULL)
3778     image->compression=(CompressionType) ParseCommandOption(
3779       MagickCompressOptions,MagickFalse,option);
3780   option=GetImageOption(image_info,"debug");
3781   if (option != (const char *) NULL)
3782     image->debug=(MagickBooleanType) ParseCommandOption(MagickBooleanOptions,
3783       MagickFalse,option);
3784   option=GetImageOption(image_info,"density");
3785   if (option != (const char *) NULL)
3786     {
3787       GeometryInfo
3788         geometry_info;
3789
3790       flags=ParseGeometry(option,&geometry_info);
3791       image->resolution.x=geometry_info.rho;
3792       image->resolution.y=geometry_info.sigma;
3793       if ((flags & SigmaValue) == 0)
3794         image->resolution.y=image->resolution.x;
3795     }
3796   option=GetImageOption(image_info,"depth");
3797   if (option != (const char *) NULL)
3798     image->depth=StringToUnsignedLong(option);
3799   option=GetImageOption(image_info,"endian");
3800   if (option != (const char *) NULL)
3801     image->endian=(EndianType) ParseCommandOption(MagickEndianOptions,
3802       MagickFalse,option);
3803   option=GetImageOption(image_info,"filter");
3804   if (option != (const char *) NULL)
3805     image->filter=(FilterTypes) ParseCommandOption(MagickFilterOptions,
3806       MagickFalse,option);
3807   option=GetImageOption(image_info,"fuzz");
3808   if (option != (const char *) NULL)
3809     image->fuzz=StringToDoubleInterval(option,(double) QuantumRange+1.0);
3810   option=GetImageOption(image_info,"gravity");
3811   if (option != (const char *) NULL)
3812     image->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
3813       MagickFalse,option);
3814   option=GetImageOption(image_info,"green-primary");
3815   if (option != (const char *) NULL)
3816     {
3817       flags=ParseGeometry(option,&geometry_info);
3818       image->chromaticity.green_primary.x=geometry_info.rho;
3819       image->chromaticity.green_primary.y=geometry_info.sigma;
3820       if ((flags & SigmaValue) == 0)
3821         image->chromaticity.green_primary.y=image->chromaticity.green_primary.x;
3822     }
3823   option=GetImageOption(image_info,"intent");
3824   if (option != (const char *) NULL)
3825     image->rendering_intent=(RenderingIntent) ParseCommandOption(
3826       MagickIntentOptions,MagickFalse,option);
3827   option=GetImageOption(image_info,"interlace");
3828   if (option != (const char *) NULL)
3829     image->interlace=(InterlaceType) ParseCommandOption(MagickInterlaceOptions,
3830       MagickFalse,option);
3831   option=GetImageOption(image_info,"interpolate");
3832   if (option != (const char *) NULL)
3833     image->interpolate=(PixelInterpolateMethod) ParseCommandOption(
3834       MagickInterpolateOptions,MagickFalse,option);
3835   option=GetImageOption(image_info,"loop");
3836   if (option != (const char *) NULL)
3837     image->iterations=StringToUnsignedLong(option);
3838   option=GetImageOption(image_info,"mattecolor");
3839   if (option != (const char *) NULL)
3840     (void) QueryColorCompliance(option,AllCompliance,&image->matte_color,
3841       exception);
3842   option=GetImageOption(image_info,"orient");
3843   if (option != (const char *) NULL)
3844     image->orientation=(OrientationType) ParseCommandOption(
3845       MagickOrientationOptions,MagickFalse,option);
3846   option=GetImageOption(image_info,"page");
3847   if (option != (const char *) NULL)
3848     {
3849       char
3850         *geometry;
3851
3852       geometry=GetPageGeometry(option);
3853       flags=ParseAbsoluteGeometry(geometry,&image->page);
3854       geometry=DestroyString(geometry);
3855     }
3856   option=GetImageOption(image_info,"quality");
3857   if (option != (const char *) NULL)
3858     image->quality=StringToUnsignedLong(option);
3859   option=GetImageOption(image_info,"red-primary");
3860   if (option != (const char *) NULL)
3861     {
3862       flags=ParseGeometry(option,&geometry_info);
3863       image->chromaticity.red_primary.x=geometry_info.rho;
3864       image->chromaticity.red_primary.y=geometry_info.sigma;
3865       if ((flags & SigmaValue) == 0)
3866         image->chromaticity.red_primary.y=image->chromaticity.red_primary.x;
3867     }
3868   if (image_info->quality != UndefinedCompressionQuality)
3869     image->quality=image_info->quality;
3870   option=GetImageOption(image_info,"scene");
3871   if (option != (const char *) NULL)
3872     image->scene=StringToUnsignedLong(option);
3873   option=GetImageOption(image_info,"taint");
3874   if (option != (const char *) NULL)
3875     image->taint=(MagickBooleanType) ParseCommandOption(MagickBooleanOptions,
3876       MagickFalse,option);
3877   option=GetImageOption(image_info,"tile-offset");
3878   if (option != (const char *) NULL)
3879     {
3880       char
3881         *geometry;
3882
3883       geometry=GetPageGeometry(option);
3884       flags=ParseAbsoluteGeometry(geometry,&image->tile_offset);
3885       geometry=DestroyString(geometry);
3886     }
3887   option=GetImageOption(image_info,"transparent-color");
3888   if (option != (const char *) NULL)
3889     (void) QueryColorCompliance(option,AllCompliance,&image->transparent_color,
3890       exception);
3891   option=GetImageOption(image_info,"type");
3892   if (option != (const char *) NULL)
3893     image->type=(ImageType) ParseCommandOption(MagickTypeOptions,MagickFalse,
3894       option);
3895   option=GetImageOption(image_info,"units");
3896   units=image_info->units;
3897   if (option != (const char *) NULL)
3898     units=(ResolutionType) ParseCommandOption(MagickResolutionOptions,
3899       MagickFalse,option);
3900   if (units != UndefinedResolution)
3901     {
3902       if (image->units != units)
3903         switch (image->units)
3904         {
3905           case PixelsPerInchResolution:
3906           {
3907             if (units == PixelsPerCentimeterResolution)
3908               {
3909                 image->resolution.x/=2.54;
3910                 image->resolution.y/=2.54;
3911               }
3912             break;
3913           }
3914           case PixelsPerCentimeterResolution:
3915           {
3916             if (units == PixelsPerInchResolution)
3917               {
3918                 image->resolution.x=(double) ((size_t) (100.0*2.54*
3919                   image->resolution.x+0.5))/100.0;
3920                 image->resolution.y=(double) ((size_t) (100.0*2.54*
3921                   image->resolution.y+0.5))/100.0;
3922               }
3923             break;
3924           }
3925           default:
3926             break;
3927         }
3928       image->units=units;
3929     }
3930   option=GetImageOption(image_info,"virtual-pixel");
3931   if (option != (const char *) NULL)
3932     (void) SetImageVirtualPixelMethod(image,(VirtualPixelMethod)
3933       ParseCommandOption(MagickVirtualPixelOptions,MagickFalse,option),
3934       exception);
3935   option=GetImageOption(image_info,"white-point");
3936   if (option != (const char *) NULL)
3937     {
3938       flags=ParseGeometry(option,&geometry_info);
3939       image->chromaticity.white_point.x=geometry_info.rho;
3940       image->chromaticity.white_point.y=geometry_info.sigma;
3941       if ((flags & SigmaValue) == 0)
3942         image->chromaticity.white_point.y=image->chromaticity.white_point.x;
3943     }
3944   ResetImageOptionIterator(image_info);
3945 #if 0
3946   {
3947     /* IMv6: Copy freeform global options into per-image artifacts, so
3948      * various operations and coders can access them.
3949      *
3950      * This has a problem, as per-image artefacts may have been set in
3951      * parenthesis, but may not be unset when parenthesis ends.
3952      */
3953     char
3954       property[MaxTextExtent];
3955
3956     const char
3957       *value;
3958
3959     for (option=GetNextImageOption(image_info); option != (const char *) NULL; )
3960     {
3961       value=GetImageOption(image_info,option);
3962       if (value != (const char *) NULL)
3963         {
3964           (void) FormatLocaleString(property,MaxTextExtent,"%s",option);
3965           (void) SetImageArtifact(image,property,value);
3966         }
3967       option=GetNextImageOption(image_info);
3968     }
3969   }
3970 #else
3971   /* IMv7: pointer to allow the lookup of pre-image artefact will fallback to
3972      a global option setting/define.  This saves a lot of duplication of
3973      global options into per-image artifacts, while ensuring only specifically
3974      set per-image artifacts are preverved when parenthesis ends.
3975
3976      This pointer is never explictally freed, as it is only used as a back
3977      reference, not as the main pointer to the image_info structure.  Images
3978      being removed from a image_info image list (or yet to be added to such),
3979      should have this pointer reset to NULL.
3980   */
3981   image->image_info=image_info;
3982 #endif
3983   return(MagickTrue);
3984 }