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