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