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