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