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