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