]> 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=RGBColorspace;
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         Flatten image pixels over the background pixels.
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       return(status);
2642     }
2643     case SetAlphaChannel:
2644     {
2645       if (image->matte == MagickFalse)
2646         status=SetImageAlpha(image,OpaqueAlpha,exception);
2647       break;
2648     }
2649     case TransparentAlphaChannel:
2650     {
2651       status=SetImageAlpha(image,TransparentAlpha,exception);
2652       break;
2653     }
2654     case UndefinedAlphaChannel:
2655       break;
2656   }
2657   if (status == MagickFalse)
2658     return(status);
2659   return(SyncImagePixelCache(image,exception));
2660 }
2661 \f
2662 /*
2663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2664 %                                                                             %
2665 %                                                                             %
2666 %                                                                             %
2667 %   S e t I m a g e B a c k g r o u n d C o l o r                             %
2668 %                                                                             %
2669 %                                                                             %
2670 %                                                                             %
2671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2672 %
2673 %  SetImageBackgroundColor() initializes the image pixels to the image
2674 %  background color.  The background color is defined by the background_color
2675 %  member of the image structure.
2676 %
2677 %  The format of the SetImage method is:
2678 %
2679 %      MagickBooleanType SetImageBackgroundColor(Image *image,
2680 %        ExceptionInfo *exception)
2681 %
2682 %  A description of each parameter follows:
2683 %
2684 %    o image: the image.
2685 %
2686 %    o exception: return any errors or warnings in this structure.
2687 %
2688 */
2689 MagickExport MagickBooleanType SetImageBackgroundColor(Image *image,
2690   ExceptionInfo *exception)
2691 {
2692   CacheView
2693     *image_view;
2694
2695   MagickBooleanType
2696     status;
2697
2698   ssize_t
2699     y;
2700
2701   assert(image != (Image *) NULL);
2702   if (image->debug != MagickFalse)
2703     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2704   assert(image->signature == MagickSignature);
2705   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2706     return(MagickFalse);
2707   if (image->background_color.matte != MagickFalse)
2708     image->matte=MagickTrue;
2709   /*
2710     Set image background color.
2711   */
2712   status=MagickTrue;
2713   image_view=AcquireCacheView(image);
2714   for (y=0; y < (ssize_t) image->rows; y++)
2715   {
2716     register Quantum
2717       *restrict q;
2718
2719     register ssize_t
2720       x;
2721
2722     if (status == MagickFalse)
2723       continue;
2724     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2725     if (q == (Quantum *) NULL)
2726       {
2727         status=MagickFalse;
2728         continue;
2729       }
2730     for (x=0; x < (ssize_t) image->columns; x++)
2731     {
2732       SetPixelInfoPixel(image,&image->background_color,q);
2733       q+=GetPixelChannels(image);
2734     }
2735     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2736       status=MagickFalse;
2737   }
2738   image_view=DestroyCacheView(image_view);
2739   return(status);
2740 }
2741 \f
2742 /*
2743 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2744 %                                                                             %
2745 %                                                                             %
2746 %                                                                             %
2747 %   S e t I m a g e C o l o r                                                 %
2748 %                                                                             %
2749 %                                                                             %
2750 %                                                                             %
2751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2752 %
2753 %  SetImageColor() set the entire image canvas to the specified color.
2754 %
2755 %  The format of the SetImageColor method is:
2756 %
2757 %      MagickBooleanType SetImageColor(Image *image,const PixelInfo *color,
2758 %        ExeptionInfo *exception)
2759 %
2760 %  A description of each parameter follows:
2761 %
2762 %    o image: the image.
2763 %
2764 %    o background: the image color.
2765 %
2766 %    o exception: return any errors or warnings in this structure.
2767 %
2768 */
2769 MagickExport MagickBooleanType SetImageColor(Image *image,
2770   const PixelInfo *color,ExceptionInfo *exception)
2771 {
2772   CacheView
2773     *image_view;
2774
2775   MagickBooleanType
2776     status;
2777
2778   ssize_t
2779     y;
2780
2781   assert(image != (Image *) NULL);
2782   if (image->debug != MagickFalse)
2783     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2784   assert(image->signature == MagickSignature);
2785   assert(color != (const PixelInfo *) NULL);
2786   image->colorspace=color->colorspace;
2787   image->matte=color->matte;
2788   image->fuzz=color->fuzz;
2789   image->depth=color->depth;
2790   status=MagickTrue;
2791   image_view=AcquireCacheView(image);
2792 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2793   #pragma omp parallel for schedule(static,4) shared(status)
2794 #endif
2795   for (y=0; y < (ssize_t) image->rows; y++)
2796   {
2797     register Quantum
2798       *restrict q;
2799
2800     register ssize_t
2801       x;
2802
2803     if (status == MagickFalse)
2804       continue;
2805     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2806     if (q == (Quantum *) NULL)
2807       {
2808         status=MagickFalse;
2809         continue;
2810       }
2811     for (x=0; x < (ssize_t) image->columns; x++)
2812     {
2813       SetPixelInfoPixel(image,color,q);
2814       q+=GetPixelChannels(image);
2815     }
2816     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2817       status=MagickFalse;
2818   }
2819   image_view=DestroyCacheView(image_view);
2820   return(status);
2821 }
2822 \f
2823 /*
2824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2825 %                                                                             %
2826 %                                                                             %
2827 %                                                                             %
2828 %   S e t I m a g e S t o r a g e C l a s s                                   %
2829 %                                                                             %
2830 %                                                                             %
2831 %                                                                             %
2832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2833 %
2834 %  SetImageStorageClass() sets the image class: DirectClass for true color
2835 %  images or PseudoClass for colormapped images.
2836 %
2837 %  The format of the SetImageStorageClass method is:
2838 %
2839 %      MagickBooleanType SetImageStorageClass(Image *image,
2840 %        const ClassType storage_class,ExceptionInfo *exception)
2841 %
2842 %  A description of each parameter follows:
2843 %
2844 %    o image: the image.
2845 %
2846 %    o storage_class:  The image class.
2847 %
2848 %    o exception: return any errors or warnings in this structure.
2849 %
2850 */
2851 MagickExport MagickBooleanType SetImageStorageClass(Image *image,
2852   const ClassType storage_class,ExceptionInfo *exception)
2853 {
2854   image->storage_class=storage_class;
2855   return(SyncImagePixelCache(image,exception));
2856 }
2857 \f
2858 /*
2859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2860 %                                                                             %
2861 %                                                                             %
2862 %                                                                             %
2863 %   S e t I m a g e E x t e n t                                               %
2864 %                                                                             %
2865 %                                                                             %
2866 %                                                                             %
2867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2868 %
2869 %  SetImageExtent() sets the image size (i.e. columns & rows).
2870 %
2871 %  The format of the SetImageExtent method is:
2872 %
2873 %      MagickBooleanType SetImageExtent(Image *image,const size_t columns,
2874 %        const size_t rows,ExceptionInfo *exception)
2875 %
2876 %  A description of each parameter follows:
2877 %
2878 %    o image: the image.
2879 %
2880 %    o columns:  The image width in pixels.
2881 %
2882 %    o rows:  The image height in pixels.
2883 %
2884 %    o exception: return any errors or warnings in this structure.
2885 %
2886 */
2887 MagickExport MagickBooleanType SetImageExtent(Image *image,const size_t columns,
2888   const size_t rows,ExceptionInfo *exception)
2889 {
2890   if ((columns == 0) || (rows == 0))
2891     return(MagickFalse);
2892   image->columns=columns;
2893   image->rows=rows;
2894   return(SyncImagePixelCache(image,exception));
2895 }
2896 \f
2897 /*
2898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2899 %                                                                             %
2900 %                                                                             %
2901 %                                                                             %
2902 +   S e t I m a g e I n f o                                                   %
2903 %                                                                             %
2904 %                                                                             %
2905 %                                                                             %
2906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2907 %
2908 %  SetImageInfo() initializes the `magick' field of the ImageInfo structure.
2909 %  It is set to a type of image format based on the prefix or suffix of the
2910 %  filename.  For example, `ps:image' returns PS indicating a Postscript image.
2911 %  JPEG is returned for this filename: `image.jpg'.  The filename prefix has
2912 %  precendence over the suffix.  Use an optional index enclosed in brackets
2913 %  after a file name to specify a desired scene of a multi-resolution image
2914 %  format like Photo CD (e.g. img0001.pcd[4]).  A True (non-zero) return value
2915 %  indicates success.
2916 %
2917 %  The format of the SetImageInfo method is:
2918 %
2919 %      MagickBooleanType SetImageInfo(ImageInfo *image_info,
2920 %        const unsigned int frames,ExceptionInfo *exception)
2921 %
2922 %  A description of each parameter follows:
2923 %
2924 %    o image_info: the image info.
2925 %
2926 %    o frames: the number of images you intend to write.
2927 %
2928 %    o exception: return any errors or warnings in this structure.
2929 %
2930 */
2931 MagickExport MagickBooleanType SetImageInfo(ImageInfo *image_info,
2932   const unsigned int frames,ExceptionInfo *exception)
2933 {
2934   char
2935     extension[MaxTextExtent],
2936     filename[MaxTextExtent],
2937     magic[MaxTextExtent],
2938     *q,
2939     subimage[MaxTextExtent];
2940
2941   const MagicInfo
2942     *magic_info;
2943
2944   const MagickInfo
2945     *magick_info;
2946
2947   ExceptionInfo
2948     *sans_exception;
2949
2950   Image
2951     *image;
2952
2953   MagickBooleanType
2954     status;
2955
2956   register const char
2957     *p;
2958
2959   ssize_t
2960     count;
2961
2962   unsigned char
2963     magick[2*MaxTextExtent];
2964
2965   /*
2966     Look for 'image.format' in filename.
2967   */
2968   assert(image_info != (ImageInfo *) NULL);
2969   assert(image_info->signature == MagickSignature);
2970   if (image_info->debug != MagickFalse)
2971     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2972       image_info->filename);
2973   *subimage='\0';
2974   if (frames == 0)
2975     {
2976       GetPathComponent(image_info->filename,SubimagePath,subimage);
2977       if (*subimage != '\0')
2978         {
2979           /*
2980             Look for scene specification (e.g. img0001.pcd[4]).
2981           */
2982           if (IsSceneGeometry(subimage,MagickFalse) == MagickFalse)
2983             {
2984               if (IsGeometry(subimage) != MagickFalse)
2985                 (void) CloneString(&image_info->extract,subimage);
2986             }
2987           else
2988             {
2989               size_t
2990                 first,
2991                 last;
2992
2993               (void) CloneString(&image_info->scenes,subimage);
2994               image_info->scene=StringToUnsignedLong(image_info->scenes);
2995               image_info->number_scenes=image_info->scene;
2996               p=image_info->scenes;
2997               for (q=(char *) image_info->scenes; *q != '\0'; p++)
2998               {
2999                 while ((isspace((int) ((unsigned char) *p)) != 0) ||
3000                        (*p == ','))
3001                   p++;
3002                 first=(size_t) strtol(p,&q,10);
3003                 last=first;
3004                 while (isspace((int) ((unsigned char) *q)) != 0)
3005                   q++;
3006                 if (*q == '-')
3007                   last=(size_t) strtol(q+1,&q,10);
3008                 if (first > last)
3009                   Swap(first,last);
3010                 if (first < image_info->scene)
3011                   image_info->scene=first;
3012                 if (last > image_info->number_scenes)
3013                   image_info->number_scenes=last;
3014                 p=q;
3015               }
3016               image_info->number_scenes-=image_info->scene-1;
3017             }
3018         }
3019     }
3020   *extension='\0';
3021   GetPathComponent(image_info->filename,ExtensionPath,extension);
3022 #if defined(MAGICKCORE_ZLIB_DELEGATE)
3023   if (*extension != '\0')
3024     if ((LocaleCompare(extension,"gz") == 0) ||
3025         (LocaleCompare(extension,"Z") == 0) ||
3026         (LocaleCompare(extension,"wmz") == 0))
3027       {
3028         char
3029           path[MaxTextExtent];
3030
3031         (void) CopyMagickString(path,image_info->filename,MaxTextExtent);
3032         path[strlen(path)-strlen(extension)-1]='\0';
3033         GetPathComponent(path,ExtensionPath,extension);
3034       }
3035 #endif
3036 #if defined(MAGICKCORE_BZLIB_DELEGATE)
3037   if (*extension != '\0')
3038     if (LocaleCompare(extension,"bz2") == 0)
3039       {
3040         char
3041           path[MaxTextExtent];
3042
3043         (void) CopyMagickString(path,image_info->filename,MaxTextExtent);
3044         path[strlen(path)-strlen(extension)-1]='\0';
3045         GetPathComponent(path,ExtensionPath,extension);
3046       }
3047 #endif
3048   image_info->affirm=MagickFalse;
3049   sans_exception=AcquireExceptionInfo();
3050   if (*extension != '\0')
3051     {
3052       MagickFormatType
3053         format_type;
3054
3055       register ssize_t
3056         i;
3057
3058       static const char
3059         *format_type_formats[] =
3060         {
3061           "AUTOTRACE",
3062           "BROWSE",
3063           "DCRAW",
3064           "EDIT",
3065           "EPHEMERAL",
3066           "LAUNCH",
3067           "MPEG:DECODE",
3068           "MPEG:ENCODE",
3069           "PRINT",
3070           "PS:ALPHA",
3071           "PS:CMYK",
3072           "PS:COLOR",
3073           "PS:GRAY",
3074           "PS:MONO",
3075           "SCAN",
3076           "SHOW",
3077           "WIN",
3078           (char *) NULL
3079         };
3080
3081       /*
3082         User specified image format.
3083       */
3084       (void) CopyMagickString(magic,extension,MaxTextExtent);
3085       LocaleUpper(magic);
3086       /*
3087         Look for explicit image formats.
3088       */
3089       format_type=UndefinedFormatType;
3090       i=0;
3091       while ((format_type == UndefinedFormatType) &&
3092              (format_type_formats[i] != (char *) NULL))
3093       {
3094         if ((*magic == *format_type_formats[i]) &&
3095             (LocaleCompare(magic,format_type_formats[i]) == 0))
3096           format_type=ExplicitFormatType;
3097         i++;
3098       }
3099       magick_info=GetMagickInfo(magic,sans_exception);
3100       if ((magick_info != (const MagickInfo *) NULL) &&
3101           (magick_info->format_type != UndefinedFormatType))
3102         format_type=magick_info->format_type;
3103       if (format_type == UndefinedFormatType)
3104         (void) CopyMagickString(image_info->magick,magic,MaxTextExtent);
3105       else
3106         if (format_type == ExplicitFormatType)
3107           {
3108             image_info->affirm=MagickTrue;
3109             (void) CopyMagickString(image_info->magick,magic,MaxTextExtent);
3110           }
3111       if (LocaleCompare(magic,"RGB") == 0)
3112         image_info->affirm=MagickFalse;  /* maybe SGI disguised as RGB */
3113     }
3114   /*
3115     Look for explicit 'format:image' in filename.
3116   */
3117   *magic='\0';
3118   GetPathComponent(image_info->filename,MagickPath,magic);
3119   if (*magic == '\0')
3120     (void) CopyMagickString(magic,image_info->magick,MaxTextExtent);
3121   else
3122     {
3123       /*
3124         User specified image format.
3125       */
3126       LocaleUpper(magic);
3127       if (IsMagickConflict(magic) == MagickFalse)
3128         {
3129           (void) CopyMagickString(image_info->magick,magic,MaxTextExtent);
3130           if (LocaleCompare(magic,"EPHEMERAL") != 0)
3131             image_info->affirm=MagickTrue;
3132           else
3133             image_info->temporary=MagickTrue;
3134         }
3135     }
3136   magick_info=GetMagickInfo(magic,sans_exception);
3137   sans_exception=DestroyExceptionInfo(sans_exception);
3138   if ((magick_info == (const MagickInfo *) NULL) ||
3139       (GetMagickEndianSupport(magick_info) == MagickFalse))
3140     image_info->endian=UndefinedEndian;
3141   GetPathComponent(image_info->filename,CanonicalPath,filename);
3142   (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
3143   if ((image_info->adjoin != MagickFalse) && (frames > 1))
3144     {
3145       /*
3146         Test for multiple image support (e.g. image%02d.png).
3147       */
3148       (void) InterpretImageFilename(image_info,(Image *) NULL,
3149         image_info->filename,(int) image_info->scene,filename,exception);
3150       if ((LocaleCompare(filename,image_info->filename) != 0) &&
3151           (strchr(filename,'%') == (char *) NULL))
3152         image_info->adjoin=MagickFalse;
3153     }
3154   if ((image_info->adjoin != MagickFalse) && (frames > 0))
3155     {
3156       /*
3157         Some image formats do not support multiple frames per file.
3158       */
3159       magick_info=GetMagickInfo(magic,exception);
3160       if (magick_info != (const MagickInfo *) NULL)
3161         if (GetMagickAdjoin(magick_info) == MagickFalse)
3162           image_info->adjoin=MagickFalse;
3163     }
3164   if (image_info->affirm != MagickFalse)
3165     return(MagickTrue);
3166   if (frames == 0)
3167     {
3168       /*
3169         Determine the image format from the first few bytes of the file.
3170       */
3171       image=AcquireImage(image_info,exception);
3172       (void) CopyMagickString(image->filename,image_info->filename,
3173         MaxTextExtent);
3174       status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
3175       if (status == MagickFalse)
3176         {
3177           image=DestroyImage(image);
3178           return(MagickFalse);
3179         }
3180       if ((IsBlobSeekable(image) == MagickFalse) ||
3181           (IsBlobExempt(image) != MagickFalse))
3182         {
3183           /*
3184             Copy standard input or pipe to temporary file.
3185           */
3186           *filename='\0';
3187           status=ImageToFile(image,filename,exception);
3188           (void) CloseBlob(image);
3189           if (status == MagickFalse)
3190             {
3191               image=DestroyImage(image);
3192               return(MagickFalse);
3193             }
3194           SetImageInfoFile(image_info,(FILE *) NULL);
3195           (void) CopyMagickString(image->filename,filename,MaxTextExtent);
3196           status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
3197           if (status == MagickFalse)
3198             {
3199               image=DestroyImage(image);
3200               return(MagickFalse);
3201             }
3202           (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
3203           image_info->temporary=MagickTrue;
3204         }
3205       (void) ResetMagickMemory(magick,0,sizeof(magick));
3206       count=ReadBlob(image,2*MaxTextExtent,magick);
3207       (void) CloseBlob(image);
3208       image=DestroyImage(image);
3209       /*
3210         Check magic.xml configuration file.
3211       */
3212       sans_exception=AcquireExceptionInfo();
3213       magic_info=GetMagicInfo(magick,(size_t) count,sans_exception);
3214       if ((magic_info != (const MagicInfo *) NULL) &&
3215           (GetMagicName(magic_info) != (char *) NULL))
3216         {
3217           (void) CopyMagickString(image_info->magick,GetMagicName(magic_info),
3218             MaxTextExtent);
3219           magick_info=GetMagickInfo(image_info->magick,sans_exception);
3220           if ((magick_info == (const MagickInfo *) NULL) ||
3221               (GetMagickEndianSupport(magick_info) == MagickFalse))
3222             image_info->endian=UndefinedEndian;
3223           sans_exception=DestroyExceptionInfo(sans_exception);
3224           return(MagickTrue);
3225         }
3226       magick_info=GetMagickInfo(image_info->magick,sans_exception);
3227       if ((magick_info == (const MagickInfo *) NULL) ||
3228           (GetMagickEndianSupport(magick_info) == MagickFalse))
3229         image_info->endian=UndefinedEndian;
3230       sans_exception=DestroyExceptionInfo(sans_exception);
3231     }
3232   return(MagickTrue);
3233 }
3234 \f
3235 /*
3236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3237 %                                                                             %
3238 %                                                                             %
3239 %                                                                             %
3240 %   S e t I m a g e I n f o B l o b                                           %
3241 %                                                                             %
3242 %                                                                             %
3243 %                                                                             %
3244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3245 %
3246 %  SetImageInfoBlob() sets the image info blob member.
3247 %
3248 %  The format of the SetImageInfoBlob method is:
3249 %
3250 %      void SetImageInfoBlob(ImageInfo *image_info,const void *blob,
3251 %        const size_t length)
3252 %
3253 %  A description of each parameter follows:
3254 %
3255 %    o image_info: the image info.
3256 %
3257 %    o blob: the blob.
3258 %
3259 %    o length: the blob length.
3260 %
3261 */
3262 MagickExport void SetImageInfoBlob(ImageInfo *image_info,const void *blob,
3263   const size_t length)
3264 {
3265   assert(image_info != (ImageInfo *) NULL);
3266   assert(image_info->signature == MagickSignature);
3267   if (image_info->debug != MagickFalse)
3268     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3269       image_info->filename);
3270   image_info->blob=(void *) blob;
3271   image_info->length=length;
3272 }
3273 \f
3274 /*
3275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3276 %                                                                             %
3277 %                                                                             %
3278 %                                                                             %
3279 %   S e t I m a g e I n f o F i l e                                           %
3280 %                                                                             %
3281 %                                                                             %
3282 %                                                                             %
3283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3284 %
3285 %  SetImageInfoFile() sets the image info file member.
3286 %
3287 %  The format of the SetImageInfoFile method is:
3288 %
3289 %      void SetImageInfoFile(ImageInfo *image_info,FILE *file)
3290 %
3291 %  A description of each parameter follows:
3292 %
3293 %    o image_info: the image info.
3294 %
3295 %    o file: the file.
3296 %
3297 */
3298 MagickExport void SetImageInfoFile(ImageInfo *image_info,FILE *file)
3299 {
3300   assert(image_info != (ImageInfo *) NULL);
3301   assert(image_info->signature == MagickSignature);
3302   if (image_info->debug != MagickFalse)
3303     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3304       image_info->filename);
3305   image_info->file=file;
3306 }
3307 \f
3308 /*
3309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3310 %                                                                             %
3311 %                                                                             %
3312 %                                                                             %
3313 %   S e t I m a g e M a s k                                                   %
3314 %                                                                             %
3315 %                                                                             %
3316 %                                                                             %
3317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3318 %
3319 %  SetImageMask() associates a mask with the image.  The mask must be the same
3320 %  dimensions as the image.
3321 %
3322 %  The format of the SetImageMask method is:
3323 %
3324 %      MagickBooleanType SetImageMask(Image *image,const Image *mask,
3325 %        ExceptionInfo *exception)
3326 %
3327 %  A description of each parameter follows:
3328 %
3329 %    o image: the image.
3330 %
3331 %    o mask: the image mask.
3332 %
3333 %    o exception: return any errors or warnings in this structure.
3334 %
3335 */
3336 MagickExport MagickBooleanType SetImageMask(Image *image,const Image *mask,
3337   ExceptionInfo *exception)
3338 {
3339   CacheView
3340     *mask_view,
3341     *image_view;
3342
3343   MagickBooleanType
3344     status;
3345
3346   ssize_t
3347     y;
3348
3349   /*
3350     Set image mask.
3351   */
3352   assert(image != (Image *) NULL);
3353   if (image->debug != MagickFalse)
3354     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3355   assert(image->signature == MagickSignature);
3356   if (mask == (const Image *) NULL)
3357     {
3358       image->mask=MagickFalse;
3359       return(MagickTrue);
3360     }
3361   if ((mask->columns != image->columns) || (mask->rows != image->rows))
3362     ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename);
3363   status=MagickTrue;
3364   image->mask=MagickTrue;
3365   image_view=AcquireCacheView(image);
3366   mask_view=AcquireCacheView(mask);
3367   for (y=0; y < (ssize_t) image->rows; y++)
3368   {
3369     register const Quantum
3370       *restrict p;
3371
3372     register Quantum
3373       *restrict q;
3374
3375     register ssize_t
3376       x;
3377
3378     if (status == MagickFalse)
3379       continue;
3380     p=GetCacheViewVirtualPixels(mask_view,0,y,mask->columns,1,exception);
3381     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3382     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3383       {
3384         status=MagickFalse;
3385         continue;
3386       }
3387     for (x=0; x < (ssize_t) image->columns; x++)
3388     {
3389       SetPixelMask(image,GetPixelGray(mask,p),q);
3390       p+=GetPixelChannels(mask);
3391       q+=GetPixelChannels(image);
3392     }
3393     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3394       status=MagickFalse;
3395   }
3396   mask_view=DestroyCacheView(mask_view);
3397   image_view=DestroyCacheView(image_view);
3398   return(status);
3399 }
3400 \f
3401 /*
3402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3403 %                                                                             %
3404 %                                                                             %
3405 %                                                                             %
3406 %     S e t I m a g e A l p h a                                               %
3407 %                                                                             %
3408 %                                                                             %
3409 %                                                                             %
3410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3411 %
3412 %  SetImageAlpha() sets the alpha levels of the image.
3413 %
3414 %  The format of the SetImageAlpha method is:
3415 %
3416 %      MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
3417 %        ExceptionInfo *exception)
3418 %
3419 %  A description of each parameter follows:
3420 %
3421 %    o image: the image.
3422 %
3423 %    o Alpha: the level of transparency: 0 is fully opaque and QuantumRange is
3424 %      fully transparent.
3425 %
3426 */
3427 MagickExport MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
3428   ExceptionInfo *exception)
3429 {
3430   CacheView
3431     *image_view;
3432
3433   MagickBooleanType
3434     status;
3435
3436   ssize_t
3437     y;
3438
3439   assert(image != (Image *) NULL);
3440   if (image->debug != MagickFalse)
3441     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3442   assert(image->signature == MagickSignature);
3443   image->matte=MagickTrue;
3444   status=MagickTrue;
3445   image_view=AcquireCacheView(image);
3446 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3447   #pragma omp parallel for schedule(static,4) shared(status)
3448 #endif
3449   for (y=0; y < (ssize_t) image->rows; y++)
3450   {
3451     register Quantum
3452       *restrict q;
3453
3454     register ssize_t
3455       x;
3456
3457     if (status == MagickFalse)
3458       continue;
3459     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3460     if (q == (Quantum *) NULL)
3461       {
3462         status=MagickFalse;
3463         continue;
3464       }
3465     for (x=0; x < (ssize_t) image->columns; x++)
3466     {
3467       SetPixelAlpha(image,alpha,q);
3468       q+=GetPixelChannels(image);
3469     }
3470     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3471       status=MagickFalse;
3472   }
3473   image_view=DestroyCacheView(image_view);
3474   return(status);
3475 }
3476 \f
3477 /*
3478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3479 %                                                                             %
3480 %                                                                             %
3481 %                                                                             %
3482 %   S e t I m a g e T y p e                                                   %
3483 %                                                                             %
3484 %                                                                             %
3485 %                                                                             %
3486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3487 %
3488 %  SetImageType() sets the type of image.  Choose from these types:
3489 %
3490 %        Bilevel        Grayscale       GrayscaleMatte
3491 %        Palette        PaletteMatte    TrueColor
3492 %        TrueColorMatte ColorSeparation ColorSeparationMatte
3493 %        OptimizeType
3494 %
3495 %  The format of the SetImageType method is:
3496 %
3497 %      MagickBooleanType SetImageType(Image *image,const ImageType type,
3498 %        ExceptionInfo *exception)
3499 %
3500 %  A description of each parameter follows:
3501 %
3502 %    o image: the image.
3503 %
3504 %    o type: Image type.
3505 %
3506 %    o exception: return any errors or warnings in this structure.
3507 %
3508 */
3509 MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
3510   ExceptionInfo *exception)
3511 {
3512   const char
3513     *artifact;
3514
3515   ImageInfo
3516     *image_info;
3517
3518   MagickBooleanType
3519     status;
3520
3521   QuantizeInfo
3522     *quantize_info;
3523
3524   assert(image != (Image *) NULL);
3525   if (image->debug != MagickFalse)
3526     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3527   assert(image->signature == MagickSignature);
3528   status=MagickTrue;
3529   image_info=AcquireImageInfo();
3530   image_info->dither=image->dither;
3531   artifact=GetImageArtifact(image,"dither");
3532   if (artifact != (const char *) NULL)
3533     (void) SetImageOption(image_info,"dither",artifact);
3534   switch (type)
3535   {
3536     case BilevelType:
3537     {
3538       if (IsImageMonochrome(image,exception) == MagickFalse)
3539         {
3540           quantize_info=AcquireQuantizeInfo(image_info);
3541           quantize_info->number_colors=2;
3542           quantize_info->colorspace=GRAYColorspace;
3543           status=QuantizeImage(quantize_info,image,exception);
3544           quantize_info=DestroyQuantizeInfo(quantize_info);
3545         }
3546       image->matte=MagickFalse;
3547       break;
3548     }
3549     case GrayscaleType:
3550     {
3551       if (IsImageGray(image,exception) == MagickFalse)
3552         status=TransformImageColorspace(image,GRAYColorspace,exception);
3553       image->matte=MagickFalse;
3554       break;
3555     }
3556     case GrayscaleMatteType:
3557     {
3558       if (IsImageGray(image,exception) == MagickFalse)
3559         status=TransformImageColorspace(image,GRAYColorspace,exception);
3560       if (image->matte == MagickFalse)
3561         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3562       break;
3563     }
3564     case PaletteType:
3565     {
3566       if (IsRGBColorspace(image->colorspace) == MagickFalse)
3567         status=TransformImageColorspace(image,RGBColorspace,exception);
3568       if ((image->storage_class == DirectClass) || (image->colors > 256))
3569         {
3570           quantize_info=AcquireQuantizeInfo(image_info);
3571           quantize_info->number_colors=256;
3572           status=QuantizeImage(quantize_info,image,exception);
3573           quantize_info=DestroyQuantizeInfo(quantize_info);
3574         }
3575       image->matte=MagickFalse;
3576       break;
3577     }
3578     case PaletteBilevelMatteType:
3579     {
3580       ChannelType
3581         channel_mask;
3582
3583       if (IsRGBColorspace(image->colorspace) == MagickFalse)
3584         status=TransformImageColorspace(image,RGBColorspace,exception);
3585       if (image->matte == MagickFalse)
3586         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3587       channel_mask=SetPixelChannelMask(image,AlphaChannel);
3588       (void) BilevelImage(image,(double) QuantumRange/2.0,exception);
3589       (void) SetPixelChannelMask(image,channel_mask);
3590       quantize_info=AcquireQuantizeInfo(image_info);
3591       status=QuantizeImage(quantize_info,image,exception);
3592       quantize_info=DestroyQuantizeInfo(quantize_info);
3593       break;
3594     }
3595     case PaletteMatteType:
3596     {
3597       if (IsRGBColorspace(image->colorspace) == MagickFalse)
3598         status=TransformImageColorspace(image,RGBColorspace,exception);
3599       if (image->matte == MagickFalse)
3600         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3601       quantize_info=AcquireQuantizeInfo(image_info);
3602       quantize_info->colorspace=TransparentColorspace;
3603       status=QuantizeImage(quantize_info,image,exception);
3604       quantize_info=DestroyQuantizeInfo(quantize_info);
3605       break;
3606     }
3607     case TrueColorType:
3608     {
3609       if (IsRGBColorspace(image->colorspace) == MagickFalse)
3610         status=TransformImageColorspace(image,RGBColorspace,exception);
3611       if (image->storage_class != DirectClass)
3612         status=SetImageStorageClass(image,DirectClass,exception);
3613       image->matte=MagickFalse;
3614       break;
3615     }
3616     case TrueColorMatteType:
3617     {
3618       if (IsRGBColorspace(image->colorspace) == MagickFalse)
3619         status=TransformImageColorspace(image,RGBColorspace,exception);
3620       if (image->storage_class != DirectClass)
3621         status=SetImageStorageClass(image,DirectClass,exception);
3622       if (image->matte == MagickFalse)
3623         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3624       break;
3625     }
3626     case ColorSeparationType:
3627     {
3628       if (image->colorspace != CMYKColorspace)
3629         {
3630           if (IsRGBColorspace(image->colorspace) == MagickFalse)
3631             status=TransformImageColorspace(image,RGBColorspace,exception);
3632           status=TransformImageColorspace(image,CMYKColorspace,exception);
3633         }
3634       if (image->storage_class != DirectClass)
3635         status=SetImageStorageClass(image,DirectClass,exception);
3636       image->matte=MagickFalse;
3637       break;
3638     }
3639     case ColorSeparationMatteType:
3640     {
3641       if (image->colorspace != CMYKColorspace)
3642         {
3643           if (IsRGBColorspace(image->colorspace) == MagickFalse)
3644             status=TransformImageColorspace(image,RGBColorspace,exception);
3645           status=TransformImageColorspace(image,CMYKColorspace,exception);
3646         }
3647       if (image->storage_class != DirectClass)
3648         status=SetImageStorageClass(image,DirectClass,exception);
3649       if (image->matte == MagickFalse)
3650         status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
3651       break;
3652     }
3653     case OptimizeType:
3654     case UndefinedType:
3655       break;
3656   }
3657   image->type=type;
3658   image_info=DestroyImageInfo(image_info);
3659   return(status);
3660 }
3661 \f
3662 /*
3663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3664 %                                                                             %
3665 %                                                                             %
3666 %                                                                             %
3667 %   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                       %
3668 %                                                                             %
3669 %                                                                             %
3670 %                                                                             %
3671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3672 %
3673 %  SetImageVirtualPixelMethod() sets the "virtual pixels" method for the
3674 %  image and returns the previous setting.  A virtual pixel is any pixel access
3675 %  that is outside the boundaries of the image cache.
3676 %
3677 %  The format of the SetImageVirtualPixelMethod() method is:
3678 %
3679 %      VirtualPixelMethod SetImageVirtualPixelMethod(const Image *image,
3680 %        const VirtualPixelMethod virtual_pixel_method)
3681 %
3682 %  A description of each parameter follows:
3683 %
3684 %    o image: the image.
3685 %
3686 %    o virtual_pixel_method: choose the type of virtual pixel.
3687 %
3688 */
3689 MagickExport VirtualPixelMethod SetImageVirtualPixelMethod(const Image *image,
3690   const VirtualPixelMethod virtual_pixel_method)
3691 {
3692   assert(image != (const Image *) NULL);
3693   assert(image->signature == MagickSignature);
3694   if (image->debug != MagickFalse)
3695     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3696   return(SetPixelCacheVirtualMethod(image,virtual_pixel_method));
3697 }
3698 \f
3699 /*
3700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3701 %                                                                             %
3702 %                                                                             %
3703 %                                                                             %
3704 %     S m u s h I m a g e s                                                   %
3705 %                                                                             %
3706 %                                                                             %
3707 %                                                                             %
3708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3709 %
3710 %  SmushImages() takes all images from the current image pointer to the end
3711 %  of the image list and smushes them to each other top-to-bottom if the
3712 %  stack parameter is true, otherwise left-to-right.
3713 %
3714 %  The current gravity setting now effects how the image is justified in the
3715 %  final image.
3716 %
3717 %  The format of the SmushImages method is:
3718 %
3719 %      Image *SmushImages(const Image *images,const MagickBooleanType stack,
3720 %        ExceptionInfo *exception)
3721 %
3722 %  A description of each parameter follows:
3723 %
3724 %    o images: the image sequence.
3725 %
3726 %    o stack: A value other than 0 stacks the images top-to-bottom.
3727 %
3728 %    o offset: minimum distance in pixels between images.
3729 %
3730 %    o exception: return any errors or warnings in this structure.
3731 %
3732 */
3733
3734 static ssize_t SmushXGap(const Image *smush_image,const Image *images,
3735   const ssize_t offset,ExceptionInfo *exception)
3736 {
3737   CacheView
3738     *left_view,
3739     *right_view;
3740
3741   const Image
3742     *left_image,
3743     *right_image;
3744
3745   RectangleInfo
3746     left_geometry,
3747     right_geometry;
3748
3749   register const Quantum
3750     *p;
3751
3752   register ssize_t
3753     i,
3754     y;
3755
3756   size_t
3757     gap;
3758
3759   ssize_t
3760     x;
3761
3762   if (images->previous == (Image *) NULL)
3763     return(0);
3764   right_image=images;
3765   SetGeometry(smush_image,&right_geometry);
3766   GravityAdjustGeometry(right_image->columns,right_image->rows,
3767     right_image->gravity,&right_geometry);
3768   left_image=images->previous;
3769   SetGeometry(smush_image,&left_geometry);
3770   GravityAdjustGeometry(left_image->columns,left_image->rows,
3771     left_image->gravity,&left_geometry);
3772   gap=right_image->columns;
3773   left_view=AcquireCacheView(left_image);
3774   right_view=AcquireCacheView(right_image);
3775   for (y=0; y < (ssize_t) smush_image->rows; y++)
3776   {
3777     for (x=(ssize_t) left_image->columns-1; x > 0; x--)
3778     {
3779       p=GetCacheViewVirtualPixels(left_view,x,left_geometry.y+y,1,1,exception);
3780       if ((p == (const Quantum *) NULL) ||
3781           (GetPixelAlpha(left_image,p) != TransparentAlpha) ||
3782           ((left_image->columns-x-1) >= gap))
3783         break;
3784     }
3785     i=(ssize_t) left_image->columns-x-1;
3786     for (x=0; x < (ssize_t) right_image->columns; x++)
3787     {
3788       p=GetCacheViewVirtualPixels(right_view,x,right_geometry.y+y,1,1,
3789         exception);
3790       if ((p == (const Quantum *) NULL) ||
3791           (GetPixelAlpha(right_image,p) != TransparentAlpha) ||
3792           ((x+i) >= (ssize_t) gap))
3793         break;
3794     }
3795     if ((x+i) < (ssize_t) gap)
3796       gap=(size_t) (x+i);
3797   }
3798   right_view=DestroyCacheView(right_view);
3799   left_view=DestroyCacheView(left_view);
3800   if (y < (ssize_t) smush_image->rows)
3801     return(offset);
3802   return((ssize_t) gap-offset);
3803 }
3804
3805 static ssize_t SmushYGap(const Image *smush_image,const Image *images,
3806   const ssize_t offset,ExceptionInfo *exception)
3807 {
3808   CacheView
3809     *bottom_view,
3810     *top_view;
3811
3812   const Image
3813     *bottom_image,
3814     *top_image;
3815
3816   RectangleInfo
3817     bottom_geometry,
3818     top_geometry;
3819
3820   register const Quantum
3821     *p;
3822
3823   register ssize_t
3824     i,
3825     x;
3826
3827   size_t
3828     gap;
3829
3830   ssize_t
3831     y;
3832
3833   if (images->previous == (Image *) NULL)
3834     return(0);
3835   bottom_image=images;
3836   SetGeometry(smush_image,&bottom_geometry);
3837   GravityAdjustGeometry(bottom_image->columns,bottom_image->rows,
3838     bottom_image->gravity,&bottom_geometry);
3839   top_image=images->previous;
3840   SetGeometry(smush_image,&top_geometry);
3841   GravityAdjustGeometry(top_image->columns,top_image->rows,top_image->gravity,
3842     &top_geometry);
3843   gap=bottom_image->rows;
3844   top_view=AcquireCacheView(top_image);
3845   bottom_view=AcquireCacheView(bottom_image);
3846   for (x=0; x < (ssize_t) smush_image->columns; x++)
3847   {
3848     for (y=(ssize_t) top_image->rows-1; y > 0; y--)
3849     {
3850       p=GetCacheViewVirtualPixels(top_view,top_geometry.x+x,y,1,1,exception);
3851       if ((p == (const Quantum *) NULL) ||
3852           (GetPixelAlpha(top_image,p) != TransparentAlpha) ||
3853           ((top_image->rows-y-1) >= gap))
3854         break;
3855     }
3856     i=(ssize_t) top_image->rows-y-1;
3857     for (y=0; y < (ssize_t) bottom_image->rows; y++)
3858     {
3859       p=GetCacheViewVirtualPixels(bottom_view,bottom_geometry.x+x,y,1,1,
3860         exception);
3861       if ((p == (const Quantum *) NULL) ||
3862           (GetPixelAlpha(bottom_image,p) != TransparentAlpha) ||
3863           ((y+i) >= (ssize_t) gap))
3864         break;
3865     }
3866     if ((y+i) < (ssize_t) gap)
3867       gap=(size_t) (y+i);
3868   }
3869   bottom_view=DestroyCacheView(bottom_view);
3870   top_view=DestroyCacheView(top_view);
3871   if (x < (ssize_t) smush_image->columns)
3872     return(offset);
3873   return((ssize_t) gap-offset);
3874 }
3875
3876 MagickExport Image *SmushImages(const Image *images,
3877   const MagickBooleanType stack,const ssize_t offset,ExceptionInfo *exception)
3878 {
3879 #define SmushImageTag  "Smush/Image"
3880
3881   CacheView
3882     *smush_view;
3883
3884   const Image
3885     *image;
3886
3887   Image
3888     *smush_image;
3889
3890   MagickBooleanType
3891     matte,
3892     proceed,
3893     status;
3894
3895   MagickOffsetType
3896     n;
3897
3898   RectangleInfo
3899     geometry;
3900
3901   register const Image
3902     *next;
3903
3904   size_t
3905     height,
3906     number_images,
3907     width;
3908
3909   ssize_t
3910     x_offset,
3911     y_offset;
3912
3913   /*
3914     Compute maximum area of smushed area.
3915   */
3916   assert(images != (Image *) NULL);
3917   assert(images->signature == MagickSignature);
3918   if (images->debug != MagickFalse)
3919     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
3920   assert(exception != (ExceptionInfo *) NULL);
3921   assert(exception->signature == MagickSignature);
3922   image=images;
3923   matte=image->matte;
3924   number_images=1;
3925   width=image->columns;
3926   height=image->rows;
3927   next=GetNextImageInList(image);
3928   for ( ; next != (Image *) NULL; next=GetNextImageInList(next))
3929   {
3930     if (next->matte != MagickFalse)
3931       matte=MagickTrue;
3932     number_images++;
3933     if (stack != MagickFalse)
3934       {
3935         if (next->columns > width)
3936           width=next->columns;
3937         height+=next->rows;
3938         if (next->previous != (Image *) NULL)
3939           height+=offset;
3940         continue;
3941       }
3942     width+=next->columns;
3943     if (next->previous != (Image *) NULL)
3944       width+=offset;
3945     if (next->rows > height)
3946       height=next->rows;
3947   }
3948   /*
3949     Smush images.
3950   */
3951   smush_image=CloneImage(image,width,height,MagickTrue,exception);
3952   if (smush_image == (Image *) NULL)
3953     return((Image *) NULL);
3954   if (SetImageStorageClass(smush_image,DirectClass,exception) == MagickFalse)
3955     {
3956       smush_image=DestroyImage(smush_image);
3957       return((Image *) NULL);
3958     }
3959   smush_image->matte=matte;
3960   (void) SetImageBackgroundColor(smush_image,exception);
3961   status=MagickTrue;
3962   x_offset=0;
3963   y_offset=0;
3964   smush_view=AcquireCacheView(smush_image);
3965   for (n=0; n < (MagickOffsetType) number_images; n++)
3966   {
3967     SetGeometry(smush_image,&geometry);
3968     GravityAdjustGeometry(image->columns,image->rows,image->gravity,&geometry);
3969     if (stack != MagickFalse)
3970       {
3971         x_offset-=geometry.x;
3972         y_offset-=SmushYGap(smush_image,image,offset,exception);
3973       }
3974     else
3975       {
3976         x_offset-=SmushXGap(smush_image,image,offset,exception);
3977         y_offset-=geometry.y;
3978       }
3979     status=CompositeImage(smush_image,OverCompositeOp,image,x_offset,y_offset,
3980       exception);
3981     proceed=SetImageProgress(image,SmushImageTag,n,number_images);
3982     if (proceed == MagickFalse)
3983       break;
3984     if (stack == MagickFalse)
3985       {
3986         x_offset+=(ssize_t) image->columns;
3987         y_offset=0;
3988       }
3989     else
3990       {
3991         x_offset=0;
3992         y_offset+=(ssize_t) image->rows;
3993       }
3994     image=GetNextImageInList(image);
3995   }
3996   if (stack == MagickFalse)
3997     smush_image->columns=(size_t) x_offset;
3998   else
3999     smush_image->rows=(size_t) y_offset;
4000   smush_view=DestroyCacheView(smush_view);
4001   if (status == MagickFalse)
4002     smush_image=DestroyImage(smush_image);
4003   return(smush_image);
4004 }
4005 \f
4006 /*
4007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4008 %                                                                             %
4009 %                                                                             %
4010 %                                                                             %
4011 %   S t r i p I m a g e                                                       %
4012 %                                                                             %
4013 %                                                                             %
4014 %                                                                             %
4015 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4016 %
4017 %  StripImage() strips an image of all profiles and comments.
4018 %
4019 %  The format of the StripImage method is:
4020 %
4021 %      MagickBooleanType StripImage(Image *image,ExceptionInfo *exception)
4022 %
4023 %  A description of each parameter follows:
4024 %
4025 %    o image: the image.
4026 %
4027 %    o exception: return any errors or warnings in this structure.
4028 %
4029 */
4030 MagickExport MagickBooleanType StripImage(Image *image,ExceptionInfo *exception)
4031 {
4032   MagickBooleanType
4033     status;
4034
4035   assert(image != (Image *) NULL);
4036   if (image->debug != MagickFalse)
4037     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4038   (void) exception;
4039   DestroyImageProfiles(image);
4040   (void) DeleteImageProperty(image,"comment");
4041   (void) DeleteImageProperty(image,"date:create");
4042   (void) DeleteImageProperty(image,"date:modify");
4043   status=SetImageArtifact(image,"png:include-chunk","none,trns,gama");
4044   return(status);
4045 }
4046 \f
4047 /*
4048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4049 %                                                                             %
4050 %                                                                             %
4051 %                                                                             %
4052 +   S y n c I m a g e                                                         %
4053 %                                                                             %
4054 %                                                                             %
4055 %                                                                             %
4056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4057 %
4058 %  SyncImage() initializes the red, green, and blue intensities of each pixel
4059 %  as defined by the colormap index.
4060 %
4061 %  The format of the SyncImage method is:
4062 %
4063 %      MagickBooleanType SyncImage(Image *image,ExceptionInfo *exception)
4064 %
4065 %  A description of each parameter follows:
4066 %
4067 %    o image: the image.
4068 %
4069 %    o exception: return any errors or warnings in this structure.
4070 %
4071 */
4072
4073 static inline Quantum PushColormapIndex(Image *image,const Quantum index,
4074   MagickBooleanType *range_exception)
4075 {
4076   if ((size_t) index < image->colors)
4077     return(index);
4078   *range_exception=MagickTrue;
4079   return(0);
4080 }
4081
4082 MagickExport MagickBooleanType SyncImage(Image *image,ExceptionInfo *exception)
4083 {
4084   CacheView
4085     *image_view;
4086
4087   MagickBooleanType
4088     range_exception,
4089     status;
4090
4091   ssize_t
4092     y;
4093
4094   assert(image != (Image *) NULL);
4095   if (image->debug != MagickFalse)
4096     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4097   assert(image->signature == MagickSignature);
4098   if (image->storage_class == DirectClass)
4099     return(MagickFalse);
4100   range_exception=MagickFalse;
4101   status=MagickTrue;
4102   image_view=AcquireCacheView(image);
4103 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4104   #pragma omp parallel for schedule(static,4) shared(range_exception,status)
4105 #endif
4106   for (y=0; y < (ssize_t) image->rows; y++)
4107   {
4108     Quantum
4109       index;
4110
4111     register Quantum
4112       *restrict q;
4113
4114     register ssize_t
4115       x;
4116
4117     if (status == MagickFalse)
4118       continue;
4119     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4120     if (q == (Quantum *) NULL)
4121       {
4122         status=MagickFalse;
4123         continue;
4124       }
4125     for (x=0; x < (ssize_t) image->columns; x++)
4126     {
4127       index=PushColormapIndex(image,GetPixelIndex(image,q),&range_exception);
4128       SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
4129       q+=GetPixelChannels(image);
4130     }
4131     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4132       status=MagickFalse;
4133   }
4134   image_view=DestroyCacheView(image_view);
4135   if (range_exception != MagickFalse)
4136     (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
4137       "InvalidColormapIndex","`%s'",image->filename);
4138   return(status);
4139 }
4140 \f
4141 /*
4142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4143 %                                                                             %
4144 %                                                                             %
4145 %                                                                             %
4146 %   S y n c I m a g e S e t t i n g s                                         %
4147 %                                                                             %
4148 %                                                                             %
4149 %                                                                             %
4150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4151 %
4152 %  SyncImageSettings() syncs the image_info options into per-image attributes.
4153 %
4154 %  The format of the SyncImageSettings method is:
4155 %
4156 %      MagickBooleanType SyncImageSettings(const ImageInfo *image_info,
4157 %        Image *image,ExceptionInfo *exception)
4158 %      MagickBooleanType SyncImagesSettings(const ImageInfo *image_info,
4159 %        Image *image,ExceptionInfo *exception)
4160 %
4161 %  A description of each parameter follows:
4162 %
4163 %    o image_info: the image info.
4164 %
4165 %    o image: the image.
4166 %
4167 %    o exception: return any errors or warnings in this structure.
4168 %
4169 */
4170
4171 MagickExport MagickBooleanType SyncImagesSettings(ImageInfo *image_info,
4172   Image *images,ExceptionInfo *exception)
4173 {
4174   Image
4175     *image;
4176
4177   assert(image_info != (const ImageInfo *) NULL);
4178   assert(image_info->signature == MagickSignature);
4179   assert(images != (Image *) NULL);
4180   assert(images->signature == MagickSignature);
4181   if (images->debug != MagickFalse)
4182     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
4183   image=images;
4184   for ( ; image != (Image *) NULL; image=GetNextImageInList(image))
4185     (void) SyncImageSettings(image_info,image,exception);
4186   (void) DeleteImageOption(image_info,"page");
4187   return(MagickTrue);
4188 }
4189
4190 MagickExport MagickBooleanType SyncImageSettings(const ImageInfo *image_info,
4191   Image *image,ExceptionInfo *exception)
4192 {
4193   char
4194     property[MaxTextExtent];
4195
4196   const char
4197     *option,
4198     *value;
4199
4200   GeometryInfo
4201     geometry_info;
4202
4203   MagickStatusType
4204     flags;
4205
4206   ResolutionType
4207     units;
4208
4209   /*
4210     Sync image options.
4211   */
4212   assert(image_info != (const ImageInfo *) NULL);
4213   assert(image_info->signature == MagickSignature);
4214   assert(image != (Image *) NULL);
4215   assert(image->signature == MagickSignature);
4216   if (image->debug != MagickFalse)
4217     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4218   option=GetImageOption(image_info,"background");
4219   if (option != (const char *) NULL)
4220     (void) QueryColorCompliance(option,AllCompliance,&image->background_color,
4221       exception);
4222   option=GetImageOption(image_info,"bias");
4223   if (option != (const char *) NULL)
4224     image->bias=StringToDoubleInterval(option,(double) QuantumRange+1.0);
4225   option=GetImageOption(image_info,"black-point-compensation");
4226   if (option != (const char *) NULL)
4227     image->black_point_compensation=(MagickBooleanType) ParseCommandOption(
4228       MagickBooleanOptions,MagickFalse,option);
4229   option=GetImageOption(image_info,"blue-primary");
4230   if (option != (const char *) NULL)
4231     {
4232       flags=ParseGeometry(option,&geometry_info);
4233       image->chromaticity.blue_primary.x=geometry_info.rho;
4234       image->chromaticity.blue_primary.y=geometry_info.sigma;
4235       if ((flags & SigmaValue) == 0)
4236         image->chromaticity.blue_primary.y=image->chromaticity.blue_primary.x;
4237     }
4238   option=GetImageOption(image_info,"bordercolor");
4239   if (option != (const char *) NULL)
4240     (void) QueryColorCompliance(option,AllCompliance,&image->border_color,
4241       exception);
4242   /* FUTURE: do not sync compose to per-image compose setting here */
4243   option=GetImageOption(image_info,"compose");
4244   if (option != (const char *) NULL)
4245     image->compose=(CompositeOperator) ParseCommandOption(MagickComposeOptions,
4246       MagickFalse,option);
4247   /* -- */
4248   option=GetImageOption(image_info,"compress");
4249   if (option != (const char *) NULL)
4250     image->compression=(CompressionType) ParseCommandOption(
4251       MagickCompressOptions,MagickFalse,option);
4252   option=GetImageOption(image_info,"debug");
4253   if (option != (const char *) NULL)
4254     image->debug=(MagickBooleanType) ParseCommandOption(MagickBooleanOptions,
4255       MagickFalse,option);
4256   option=GetImageOption(image_info,"density");
4257   if (option != (const char *) NULL)
4258     {
4259       GeometryInfo
4260         geometry_info;
4261
4262       flags=ParseGeometry(option,&geometry_info);
4263       image->resolution.x=geometry_info.rho;
4264       image->resolution.y=geometry_info.sigma;
4265       if ((flags & SigmaValue) == 0)
4266         image->resolution.y=image->resolution.x;
4267     }
4268   option=GetImageOption(image_info,"depth");
4269   if (option != (const char *) NULL)
4270     image->depth=StringToUnsignedLong(option);
4271   option=GetImageOption(image_info,"endian");
4272   if (option != (const char *) NULL)
4273     image->endian=(EndianType) ParseCommandOption(MagickEndianOptions,
4274       MagickFalse,option);
4275   option=GetImageOption(image_info,"filter");
4276   if (option != (const char *) NULL)
4277     image->filter=(FilterTypes) ParseCommandOption(MagickFilterOptions,
4278       MagickFalse,option);
4279   option=GetImageOption(image_info,"fuzz");
4280   if (option != (const char *) NULL)
4281     image->fuzz=StringToDoubleInterval(option,(double) QuantumRange+1.0);
4282   option=GetImageOption(image_info,"gravity");
4283   if (option != (const char *) NULL)
4284     image->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
4285       MagickFalse,option);
4286   option=GetImageOption(image_info,"green-primary");
4287   if (option != (const char *) NULL)
4288     {
4289       flags=ParseGeometry(option,&geometry_info);
4290       image->chromaticity.green_primary.x=geometry_info.rho;
4291       image->chromaticity.green_primary.y=geometry_info.sigma;
4292       if ((flags & SigmaValue) == 0)
4293         image->chromaticity.green_primary.y=image->chromaticity.green_primary.x;
4294     }
4295   option=GetImageOption(image_info,"intent");
4296   if (option != (const char *) NULL)
4297     image->rendering_intent=(RenderingIntent) ParseCommandOption(
4298       MagickIntentOptions,MagickFalse,option);
4299   option=GetImageOption(image_info,"interlace");
4300   if (option != (const char *) NULL)
4301     image->interlace=(InterlaceType) ParseCommandOption(MagickInterlaceOptions,
4302       MagickFalse,option);
4303   option=GetImageOption(image_info,"interpolate");
4304   if (option != (const char *) NULL)
4305     image->interpolate=(PixelInterpolateMethod) ParseCommandOption(
4306       MagickInterpolateOptions,MagickFalse,option);
4307   option=GetImageOption(image_info,"loop");
4308   if (option != (const char *) NULL)
4309     image->iterations=StringToUnsignedLong(option);
4310   option=GetImageOption(image_info,"mattecolor");
4311   if (option != (const char *) NULL)
4312     (void) QueryColorCompliance(option,AllCompliance,&image->matte_color,
4313       exception);
4314   option=GetImageOption(image_info,"orient");
4315   if (option != (const char *) NULL)
4316     image->orientation=(OrientationType) ParseCommandOption(
4317       MagickOrientationOptions,MagickFalse,option);
4318   option=GetImageOption(image_info,"page");
4319   if (option != (const char *) NULL)
4320     {
4321       char
4322         *geometry;
4323
4324       geometry=GetPageGeometry(option);
4325       flags=ParseAbsoluteGeometry(geometry,&image->page);
4326       geometry=DestroyString(geometry);
4327     }
4328   option=GetImageOption(image_info,"quality");
4329   if (option != (const char *) NULL)
4330     image->quality=StringToUnsignedLong(option);
4331   option=GetImageOption(image_info,"red-primary");
4332   if (option != (const char *) NULL)
4333     {
4334       flags=ParseGeometry(option,&geometry_info);
4335       image->chromaticity.red_primary.x=geometry_info.rho;
4336       image->chromaticity.red_primary.y=geometry_info.sigma;
4337       if ((flags & SigmaValue) == 0)
4338         image->chromaticity.red_primary.y=image->chromaticity.red_primary.x;
4339     }
4340   if (image_info->quality != UndefinedCompressionQuality)
4341     image->quality=image_info->quality;
4342   option=GetImageOption(image_info,"scene");
4343   if (option != (const char *) NULL)
4344     image->scene=StringToUnsignedLong(option);
4345   option=GetImageOption(image_info,"taint");
4346   if (option != (const char *) NULL)
4347     image->taint=(MagickBooleanType) ParseCommandOption(MagickBooleanOptions,
4348       MagickFalse,option);
4349   option=GetImageOption(image_info,"tile-offset");
4350   if (option != (const char *) NULL)
4351     {
4352       char
4353         *geometry;
4354
4355       geometry=GetPageGeometry(option);
4356       flags=ParseAbsoluteGeometry(geometry,&image->tile_offset);
4357       geometry=DestroyString(geometry);
4358     }
4359   option=GetImageOption(image_info,"transparent-color");
4360   if (option != (const char *) NULL)
4361     (void) QueryColorCompliance(option,AllCompliance,&image->transparent_color,
4362       exception);
4363   option=GetImageOption(image_info,"type");
4364   if (option != (const char *) NULL)
4365     image->type=(ImageType) ParseCommandOption(MagickTypeOptions,MagickFalse,
4366       option);
4367   option=GetImageOption(image_info,"units");
4368   units=image_info->units;
4369   if (option != (const char *) NULL)
4370     units=(ResolutionType) ParseCommandOption(MagickResolutionOptions,
4371       MagickFalse,option);
4372   if (units != UndefinedResolution)
4373     {
4374       if (image->units != units)
4375         switch (image->units)
4376         {
4377           case PixelsPerInchResolution:
4378           {
4379             if (units == PixelsPerCentimeterResolution)
4380               {
4381                 image->resolution.x/=2.54;
4382                 image->resolution.y/=2.54;
4383               }
4384             break;
4385           }
4386           case PixelsPerCentimeterResolution:
4387           {
4388             if (units == PixelsPerInchResolution)
4389               {
4390                 image->resolution.x=(double) ((size_t) (100.0*2.54*
4391                   image->resolution.x+0.5))/100.0;
4392                 image->resolution.y=(double) ((size_t) (100.0*2.54*
4393                   image->resolution.y+0.5))/100.0;
4394               }
4395             break;
4396           }
4397           default:
4398             break;
4399         }
4400       image->units=units;
4401     }
4402   option=GetImageOption(image_info,"virtual-pixel");
4403   if (option != (const char *) NULL)
4404     (void) SetImageVirtualPixelMethod(image, (VirtualPixelMethod)
4405          ParseCommandOption(MagickVirtualPixelOptions,MagickFalse,option));
4406   option=GetImageOption(image_info,"white-point");
4407   if (option != (const char *) NULL)
4408     {
4409       flags=ParseGeometry(option,&geometry_info);
4410       image->chromaticity.white_point.x=geometry_info.rho;
4411       image->chromaticity.white_point.y=geometry_info.sigma;
4412       if ((flags & SigmaValue) == 0)
4413         image->chromaticity.white_point.y=image->chromaticity.white_point.x;
4414     }
4415   ResetImageOptionIterator(image_info);
4416   for (option=GetNextImageOption(image_info); option != (const char *) NULL; )
4417   {
4418     value=GetImageOption(image_info,option);
4419     if (value != (const char *) NULL)
4420       {
4421         (void) FormatLocaleString(property,MaxTextExtent,"%s",option);
4422         (void) SetImageArtifact(image,property,value);
4423       }
4424     option=GetNextImageOption(image_info);
4425   }
4426   return(MagickTrue);
4427 }