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