MagickCore 7.1.1
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
cache.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% CCCC AAA CCCC H H EEEEE %
7% C A A C H H E %
8% C AAAAA C HHHHH EEE %
9% C A A C H H E %
10% CCCC A A CCCC H H EEEEE %
11% %
12% %
13% MagickCore Pixel Cache Methods %
14% %
15% Software Design %
16% Cristy %
17% July 1999 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
43#include "MagickCore/studio.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/cache-private.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colorspace-private.h"
50#include "MagickCore/composite-private.h"
51#include "MagickCore/distribute-cache-private.h"
52#include "MagickCore/exception.h"
53#include "MagickCore/exception-private.h"
54#include "MagickCore/geometry.h"
55#include "MagickCore/list.h"
56#include "MagickCore/log.h"
57#include "MagickCore/magick.h"
58#include "MagickCore/memory_.h"
59#include "MagickCore/memory-private.h"
60#include "MagickCore/nt-base-private.h"
61#include "MagickCore/option.h"
62#include "MagickCore/pixel.h"
63#include "MagickCore/pixel-accessor.h"
64#include "MagickCore/pixel-private.h"
65#include "MagickCore/policy.h"
66#include "MagickCore/quantum.h"
67#include "MagickCore/random_.h"
68#include "MagickCore/registry.h"
69#include "MagickCore/resource_.h"
70#include "MagickCore/semaphore.h"
71#include "MagickCore/splay-tree.h"
72#include "MagickCore/string_.h"
73#include "MagickCore/string-private.h"
74#include "MagickCore/timer-private.h"
75#include "MagickCore/thread-private.h"
76#include "MagickCore/utility.h"
77#include "MagickCore/utility-private.h"
78#if defined(MAGICKCORE_ZLIB_DELEGATE)
79#include "zlib.h"
80#endif
81
82/*
83 Define declarations.
84*/
85#define CacheTick(offset,extent) QuantumTick((MagickOffsetType) offset,extent)
86#define IsFileDescriptorLimitExceeded() (GetMagickResource(FileResource) > \
87 GetMagickResourceLimit(FileResource) ? MagickTrue : MagickFalse)
88
89/*
90 Typedef declarations.
91*/
92typedef struct _MagickModulo
93{
94 ssize_t
95 quotient,
96 remainder;
98
99/*
100 Forward declarations.
101*/
102#if defined(__cplusplus) || defined(c_plusplus)
103extern "C" {
104#endif
105
106static Cache
107 GetImagePixelCache(Image *,const MagickBooleanType,ExceptionInfo *)
108 magick_hot_spot;
109
110static const Quantum
111 *GetVirtualPixelCache(const Image *,const VirtualPixelMethod,const ssize_t,
112 const ssize_t,const size_t,const size_t,ExceptionInfo *),
113 *GetVirtualPixelsCache(const Image *);
114
115static const void
116 *GetVirtualMetacontentFromCache(const Image *);
117
118static MagickBooleanType
119 GetOneAuthenticPixelFromCache(Image *,const ssize_t,const ssize_t,Quantum *,
120 ExceptionInfo *),
121 GetOneVirtualPixelFromCache(const Image *,const VirtualPixelMethod,
122 const ssize_t,const ssize_t,Quantum *,ExceptionInfo *),
123 OpenPixelCache(Image *,const MapMode,ExceptionInfo *),
124 OpenPixelCacheOnDisk(CacheInfo *,const MapMode),
125 ReadPixelCachePixels(CacheInfo *magick_restrict,NexusInfo *magick_restrict,
126 ExceptionInfo *),
127 ReadPixelCacheMetacontent(CacheInfo *magick_restrict,
128 NexusInfo *magick_restrict,ExceptionInfo *),
129 SyncAuthenticPixelsCache(Image *,ExceptionInfo *),
130 WritePixelCachePixels(CacheInfo *magick_restrict,NexusInfo *magick_restrict,
131 ExceptionInfo *),
132 WritePixelCacheMetacontent(CacheInfo *,NexusInfo *magick_restrict,
133 ExceptionInfo *);
134
135static Quantum
136 *GetAuthenticPixelsCache(Image *,const ssize_t,const ssize_t,const size_t,
137 const size_t,ExceptionInfo *),
138 *QueueAuthenticPixelsCache(Image *,const ssize_t,const ssize_t,const size_t,
139 const size_t,ExceptionInfo *),
140 *SetPixelCacheNexusPixels(const CacheInfo *magick_restrict,const MapMode,
141 const ssize_t,const ssize_t,const size_t,const size_t,
142 const MagickBooleanType,NexusInfo *magick_restrict,ExceptionInfo *)
143 magick_hot_spot;
144
145#if defined(MAGICKCORE_OPENCL_SUPPORT)
146static void
147 CopyOpenCLBuffer(CacheInfo *magick_restrict);
148#endif
149
150#if defined(__cplusplus) || defined(c_plusplus)
151}
152#endif
153
154/*
155 Global declarations.
156*/
157static SemaphoreInfo
158 *cache_semaphore = (SemaphoreInfo *) NULL;
159
160static ssize_t
161 cache_anonymous_memory = (-1);
162
163/*
164%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165% %
166% %
167% %
168+ A c q u i r e P i x e l C a c h e %
169% %
170% %
171% %
172%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173%
174% AcquirePixelCache() acquires a pixel cache.
175%
176% The format of the AcquirePixelCache() method is:
177%
178% Cache AcquirePixelCache(const size_t number_threads)
179%
180% A description of each parameter follows:
181%
182% o number_threads: the number of nexus threads.
183%
184*/
185MagickPrivate Cache AcquirePixelCache(const size_t number_threads)
186{
188 *magick_restrict cache_info;
189
190 char
191 *value;
192
193 cache_info=(CacheInfo *) AcquireAlignedMemory(1,sizeof(*cache_info));
194 if (cache_info == (CacheInfo *) NULL)
195 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
196 (void) memset(cache_info,0,sizeof(*cache_info));
197 cache_info->type=UndefinedCache;
198 cache_info->mode=IOMode;
199 cache_info->disk_mode=IOMode;
200 cache_info->colorspace=sRGBColorspace;
201 cache_info->file=(-1);
202 cache_info->id=GetMagickThreadId();
203 cache_info->number_threads=number_threads;
204 if (GetOpenMPMaximumThreads() > cache_info->number_threads)
205 cache_info->number_threads=GetOpenMPMaximumThreads();
206 if (GetMagickResourceLimit(ThreadResource) > cache_info->number_threads)
207 cache_info->number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
208 if (cache_info->number_threads == 0)
209 cache_info->number_threads=1;
210 cache_info->nexus_info=AcquirePixelCacheNexus(cache_info->number_threads);
211 if (cache_info->nexus_info == (NexusInfo **) NULL)
212 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
213 value=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
214 if (value != (const char *) NULL)
215 {
216 cache_info->synchronize=IsStringTrue(value);
217 value=DestroyString(value);
218 }
219 value=GetPolicyValue("cache:synchronize");
220 if (value != (const char *) NULL)
221 {
222 cache_info->synchronize=IsStringTrue(value);
223 value=DestroyString(value);
224 }
225 cache_info->width_limit=MagickMin(GetMagickResourceLimit(WidthResource),
226 (MagickSizeType) MAGICK_SSIZE_MAX);
227 cache_info->height_limit=MagickMin(GetMagickResourceLimit(HeightResource),
228 (MagickSizeType) MAGICK_SSIZE_MAX);
229 cache_info->semaphore=AcquireSemaphoreInfo();
230 cache_info->reference_count=1;
231 cache_info->file_semaphore=AcquireSemaphoreInfo();
232 cache_info->debug=(GetLogEventMask() & CacheEvent) != 0 ? MagickTrue :
233 MagickFalse;
234 cache_info->signature=MagickCoreSignature;
235 return((Cache ) cache_info);
236}
237
238/*
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240% %
241% %
242% %
243% A c q u i r e P i x e l C a c h e N e x u s %
244% %
245% %
246% %
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248%
249% AcquirePixelCacheNexus() allocates the NexusInfo structure.
250%
251% The format of the AcquirePixelCacheNexus method is:
252%
253% NexusInfo **AcquirePixelCacheNexus(const size_t number_threads)
254%
255% A description of each parameter follows:
256%
257% o number_threads: the number of nexus threads.
258%
259*/
260MagickPrivate NexusInfo **AcquirePixelCacheNexus(const size_t number_threads)
261{
263 **magick_restrict nexus_info;
264
265 ssize_t
266 i;
267
268 nexus_info=(NexusInfo **) MagickAssumeAligned(AcquireAlignedMemory(2*
269 number_threads,sizeof(*nexus_info)));
270 if (nexus_info == (NexusInfo **) NULL)
271 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
272 *nexus_info=(NexusInfo *) AcquireQuantumMemory(number_threads,
273 2*sizeof(**nexus_info));
274 if (*nexus_info == (NexusInfo *) NULL)
275 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
276 (void) memset(*nexus_info,0,2*number_threads*sizeof(**nexus_info));
277 for (i=0; i < (ssize_t) (2*number_threads); i++)
278 {
279 nexus_info[i]=(*nexus_info+i);
280 if (i < (ssize_t) number_threads)
281 nexus_info[i]->virtual_nexus=(*nexus_info+number_threads+i);
282 nexus_info[i]->signature=MagickCoreSignature;
283 }
284 return(nexus_info);
285}
286
287/*
288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289% %
290% %
291% %
292% A c q u i r e P i x e l C a c h e P i x e l s %
293% %
294% %
295% %
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297%
298% AcquirePixelCachePixels() returns the pixels associated with the specified
299% image.
300%
301% The format of the AcquirePixelCachePixels() method is:
302%
303% void *AcquirePixelCachePixels(const Image *image,size_t *length,
304% ExceptionInfo *exception)
305%
306% A description of each parameter follows:
307%
308% o image: the image.
309%
310% o length: the pixel cache length.
311%
312% o exception: return any errors or warnings in this structure.
313%
314*/
315MagickExport void *AcquirePixelCachePixels(const Image *image,size_t *length,
316 ExceptionInfo *exception)
317{
319 *magick_restrict cache_info;
320
321 assert(image != (const Image *) NULL);
322 assert(image->signature == MagickCoreSignature);
323 assert(exception != (ExceptionInfo *) NULL);
324 assert(exception->signature == MagickCoreSignature);
325 assert(image->cache != (Cache) NULL);
326 (void) exception;
327 cache_info=(CacheInfo *) image->cache;
328 assert(cache_info->signature == MagickCoreSignature);
329 *length=0;
330 if ((cache_info->type != MemoryCache) && (cache_info->type != MapCache))
331 return((void *) NULL);
332 *length=(size_t) cache_info->length;
333 return(cache_info->pixels);
334}
335
336/*
337%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338% %
339% %
340% %
341+ C a c h e C o m p o n e n t G e n e s i s %
342% %
343% %
344% %
345%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346%
347% CacheComponentGenesis() instantiates the cache component.
348%
349% The format of the CacheComponentGenesis method is:
350%
351% MagickBooleanType CacheComponentGenesis(void)
352%
353*/
354MagickPrivate MagickBooleanType CacheComponentGenesis(void)
355{
356 if (cache_semaphore == (SemaphoreInfo *) NULL)
357 cache_semaphore=AcquireSemaphoreInfo();
358 return(MagickTrue);
359}
360
361/*
362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363% %
364% %
365% %
366+ C a c h e C o m p o n e n t T e r m i n u s %
367% %
368% %
369% %
370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371%
372% CacheComponentTerminus() destroys the cache component.
373%
374% The format of the CacheComponentTerminus() method is:
375%
376% CacheComponentTerminus(void)
377%
378*/
379MagickPrivate void CacheComponentTerminus(void)
380{
381 if (cache_semaphore == (SemaphoreInfo *) NULL)
382 ActivateSemaphoreInfo(&cache_semaphore);
383 /* no op-- nothing to destroy */
384 RelinquishSemaphoreInfo(&cache_semaphore);
385}
386
387/*
388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389% %
390% %
391% %
392+ C l i p P i x e l C a c h e N e x u s %
393% %
394% %
395% %
396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397%
398% ClipPixelCacheNexus() clips the cache nexus as defined by the image clip
399% mask. The method returns MagickTrue if the pixel region is clipped,
400% otherwise MagickFalse.
401%
402% The format of the ClipPixelCacheNexus() method is:
403%
404% MagickBooleanType ClipPixelCacheNexus(Image *image,NexusInfo *nexus_info,
405% ExceptionInfo *exception)
406%
407% A description of each parameter follows:
408%
409% o image: the image.
410%
411% o nexus_info: the cache nexus to clip.
412%
413% o exception: return any errors or warnings in this structure.
414%
415*/
416static MagickBooleanType ClipPixelCacheNexus(Image *image,
417 NexusInfo *nexus_info,ExceptionInfo *exception)
418{
420 *magick_restrict cache_info;
421
422 Quantum
423 *magick_restrict p,
424 *magick_restrict q;
425
426 ssize_t
427 y;
428
429 /*
430 Apply clip mask.
431 */
432 if (IsEventLogging() != MagickFalse)
433 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
434 if ((image->channels & WriteMaskChannel) == 0)
435 return(MagickTrue);
436 if ((nexus_info->region.width == 0) || (nexus_info->region.height == 0))
437 return(MagickTrue);
438 cache_info=(CacheInfo *) image->cache;
439 if (cache_info == (Cache) NULL)
440 return(MagickFalse);
441 p=GetAuthenticPixelCacheNexus(image,nexus_info->region.x,nexus_info->region.y,
442 nexus_info->region.width,nexus_info->region.height,
443 nexus_info->virtual_nexus,exception);
444 q=nexus_info->pixels;
445 if ((p == (Quantum *) NULL) || (q == (Quantum *) NULL))
446 return(MagickFalse);
447 for (y=0; y < (ssize_t) nexus_info->region.height; y++)
448 {
449 ssize_t
450 x;
451
452 for (x=0; x < (ssize_t) nexus_info->region.width; x++)
453 {
454 double
455 mask_alpha;
456
457 ssize_t
458 i;
459
460 mask_alpha=QuantumScale*(double) GetPixelWriteMask(image,p);
461 if (fabs(mask_alpha) >= MagickEpsilon)
462 {
463 for (i=0; i < (ssize_t) image->number_channels; i++)
464 {
465 PixelChannel channel = GetPixelChannelChannel(image,i);
466 PixelTrait traits = GetPixelChannelTraits(image,channel);
467 if ((traits & UpdatePixelTrait) == 0)
468 continue;
469 q[i]=ClampToQuantum(MagickOver_((double) p[i],mask_alpha*(double)
470 GetPixelAlpha(image,p),(double) q[i],(double)
471 GetPixelAlpha(image,q)));
472 }
473 SetPixelAlpha(image,GetPixelAlpha(image,p),q);
474 }
475 p+=GetPixelChannels(image);
476 q+=GetPixelChannels(image);
477 }
478 }
479 return(MagickTrue);
480}
481
482/*
483%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484% %
485% %
486% %
487+ C l o n e P i x e l C a c h e %
488% %
489% %
490% %
491%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492%
493% ClonePixelCache() clones a pixel cache.
494%
495% The format of the ClonePixelCache() method is:
496%
497% Cache ClonePixelCache(const Cache cache)
498%
499% A description of each parameter follows:
500%
501% o cache: the pixel cache.
502%
503*/
504MagickPrivate Cache ClonePixelCache(const Cache cache)
505{
507 *magick_restrict clone_info;
508
509 const CacheInfo
510 *magick_restrict cache_info;
511
512 assert(cache != NULL);
513 cache_info=(const CacheInfo *) cache;
514 assert(cache_info->signature == MagickCoreSignature);
515 if (IsEventLogging() != MagickFalse)
516 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
517 cache_info->filename);
518 clone_info=(CacheInfo *) AcquirePixelCache(cache_info->number_threads);
519 clone_info->virtual_pixel_method=cache_info->virtual_pixel_method;
520 return((Cache ) clone_info);
521}
522
523/*
524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525% %
526% %
527% %
528+ C l o n e P i x e l C a c h e M e t h o d s %
529% %
530% %
531% %
532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533%
534% ClonePixelCacheMethods() clones the pixel cache methods from one cache to
535% another.
536%
537% The format of the ClonePixelCacheMethods() method is:
538%
539% void ClonePixelCacheMethods(Cache clone,const Cache cache)
540%
541% A description of each parameter follows:
542%
543% o clone: Specifies a pointer to a Cache structure.
544%
545% o cache: the pixel cache.
546%
547*/
548MagickPrivate void ClonePixelCacheMethods(Cache clone,const Cache cache)
549{
551 *magick_restrict cache_info,
552 *magick_restrict source_info;
553
554 assert(clone != (Cache) NULL);
555 source_info=(CacheInfo *) clone;
556 assert(source_info->signature == MagickCoreSignature);
557 if (IsEventLogging() != MagickFalse)
558 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
559 source_info->filename);
560 assert(cache != (Cache) NULL);
561 cache_info=(CacheInfo *) cache;
562 assert(cache_info->signature == MagickCoreSignature);
563 source_info->methods=cache_info->methods;
564}
565
566/*
567%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568% %
569% %
570% %
571+ C l o n e P i x e l C a c h e R e p o s i t o r y %
572% %
573% %
574% %
575%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576%
577% ClonePixelCacheRepository() clones the source pixel cache to the destination
578% cache.
579%
580% The format of the ClonePixelCacheRepository() method is:
581%
582% MagickBooleanType ClonePixelCacheRepository(CacheInfo *clone_info,
583% CacheInfo *cache_info,ExceptionInfo *exception)
584%
585% A description of each parameter follows:
586%
587% o clone_info: the pixel cache.
588%
589% o cache_info: the source pixel cache.
590%
591% o exception: return any errors or warnings in this structure.
592%
593*/
594
595static MagickBooleanType ClonePixelCacheOnDisk(
596 CacheInfo *magick_restrict cache_info,CacheInfo *magick_restrict clone_info)
597{
598 MagickSizeType
599 extent;
600
601 size_t
602 quantum;
603
604 ssize_t
605 count;
606
607 struct stat
608 file_stats;
609
610 unsigned char
611 *buffer;
612
613 /*
614 Clone pixel cache on disk with identical morphology.
615 */
616 if ((OpenPixelCacheOnDisk(cache_info,ReadMode) == MagickFalse) ||
617 (OpenPixelCacheOnDisk(clone_info,IOMode) == MagickFalse))
618 return(MagickFalse);
619 if ((lseek(cache_info->file,0,SEEK_SET) < 0) ||
620 (lseek(clone_info->file,0,SEEK_SET) < 0))
621 return(MagickFalse);
622 quantum=(size_t) MagickMaxBufferExtent;
623 if ((fstat(cache_info->file,&file_stats) == 0) && (file_stats.st_size > 0))
624 {
625#if defined(MAGICKCORE_HAVE_LINUX_SENDFILE)
626 if (cache_info->length < 0x7ffff000)
627 {
628 count=sendfile(clone_info->file,cache_info->file,(off_t *) NULL,
629 (size_t) cache_info->length);
630 if (count == (ssize_t) cache_info->length)
631 return(MagickTrue);
632 if ((lseek(cache_info->file,0,SEEK_SET) < 0) ||
633 (lseek(clone_info->file,0,SEEK_SET) < 0))
634 return(MagickFalse);
635 }
636#endif
637 quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
638 }
639 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
640 if (buffer == (unsigned char *) NULL)
641 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
642 extent=0;
643 while ((count=read(cache_info->file,buffer,quantum)) > 0)
644 {
645 ssize_t
646 number_bytes;
647
648 number_bytes=write(clone_info->file,buffer,(size_t) count);
649 if (number_bytes != count)
650 break;
651 extent+=(size_t) number_bytes;
652 }
653 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
654 if (extent != cache_info->length)
655 return(MagickFalse);
656 return(MagickTrue);
657}
658
659static MagickBooleanType ClonePixelCacheRepository(
660 CacheInfo *magick_restrict clone_info,CacheInfo *magick_restrict cache_info,
661 ExceptionInfo *exception)
662{
663#define MaxCacheThreads ((size_t) GetMagickResourceLimit(ThreadResource))
664#define cache_number_threads(source,destination,chunk,multithreaded) \
665 num_threads((multithreaded) == 0 ? 1 : \
666 (((source)->type != MemoryCache) && ((source)->type != MapCache)) || \
667 (((destination)->type != MemoryCache) && ((destination)->type != MapCache)) ? \
668 MagickMax(MagickMin((ssize_t) GetMagickResourceLimit(ThreadResource),2),1) : \
669 MagickMax(MagickMin((ssize_t) GetMagickResourceLimit(ThreadResource),(ssize_t) (chunk)/256),1))
670
671 MagickBooleanType
672 optimize,
673 status;
674
676 **magick_restrict cache_nexus,
677 **magick_restrict clone_nexus;
678
679 size_t
680 length;
681
682 ssize_t
683 y;
684
685 assert(cache_info != (CacheInfo *) NULL);
686 assert(clone_info != (CacheInfo *) NULL);
687 assert(exception != (ExceptionInfo *) NULL);
688 if (cache_info->type == PingCache)
689 return(MagickTrue);
690 length=cache_info->number_channels*sizeof(*cache_info->channel_map);
691 if ((cache_info->storage_class == clone_info->storage_class) &&
692 (cache_info->colorspace == clone_info->colorspace) &&
693 (cache_info->alpha_trait == clone_info->alpha_trait) &&
694 (cache_info->channels == clone_info->channels) &&
695 (cache_info->columns == clone_info->columns) &&
696 (cache_info->rows == clone_info->rows) &&
697 (cache_info->number_channels == clone_info->number_channels) &&
698 (memcmp(cache_info->channel_map,clone_info->channel_map,length) == 0) &&
699 (cache_info->metacontent_extent == clone_info->metacontent_extent))
700 {
701 /*
702 Identical pixel cache morphology.
703 */
704 if (((cache_info->type == MemoryCache) ||
705 (cache_info->type == MapCache)) &&
706 ((clone_info->type == MemoryCache) || (clone_info->type == MapCache)))
707 {
708 (void) memcpy(clone_info->pixels,cache_info->pixels,
709 cache_info->number_channels*cache_info->columns*cache_info->rows*
710 sizeof(*cache_info->pixels));
711 if ((cache_info->metacontent_extent != 0) &&
712 (clone_info->metacontent_extent != 0))
713 (void) memcpy(clone_info->metacontent,cache_info->metacontent,
714 cache_info->columns*cache_info->rows*
715 clone_info->metacontent_extent*sizeof(unsigned char));
716 return(MagickTrue);
717 }
718 if ((cache_info->type == DiskCache) && (clone_info->type == DiskCache))
719 return(ClonePixelCacheOnDisk(cache_info,clone_info));
720 }
721 /*
722 Mismatched pixel cache morphology.
723 */
724 cache_nexus=AcquirePixelCacheNexus(cache_info->number_threads);
725 clone_nexus=AcquirePixelCacheNexus(clone_info->number_threads);
726 length=cache_info->number_channels*sizeof(*cache_info->channel_map);
727 optimize=(cache_info->number_channels == clone_info->number_channels) &&
728 (memcmp(cache_info->channel_map,clone_info->channel_map,length) == 0) ?
729 MagickTrue : MagickFalse;
730 length=(size_t) MagickMin(cache_info->number_channels*cache_info->columns,
731 clone_info->number_channels*clone_info->columns);
732 status=MagickTrue;
733#if defined(MAGICKCORE_OPENMP_SUPPORT)
734 #pragma omp parallel for schedule(static) shared(status) \
735 cache_number_threads(cache_info,clone_info,(int) cache_info->rows,4)
736#endif
737 for (y=0; y < (ssize_t) cache_info->rows; y++)
738 {
739 const int
740 id = GetOpenMPThreadId();
741
742 Quantum
743 *pixels;
744
745 ssize_t
746 x;
747
748 if (status == MagickFalse)
749 continue;
750 if (y >= (ssize_t) clone_info->rows)
751 continue;
752 pixels=SetPixelCacheNexusPixels(cache_info,ReadMode,0,y,
753 cache_info->columns,1,MagickFalse,cache_nexus[id],exception);
754 if (pixels == (Quantum *) NULL)
755 continue;
756 status=ReadPixelCachePixels(cache_info,cache_nexus[id],exception);
757 if (status == MagickFalse)
758 continue;
759 pixels=SetPixelCacheNexusPixels(clone_info,WriteMode,0,y,
760 clone_info->columns,1,MagickFalse,clone_nexus[id],exception);
761 if (pixels == (Quantum *) NULL)
762 continue;
763 (void) memset(clone_nexus[id]->pixels,0,(size_t) clone_nexus[id]->length);
764 if (optimize != MagickFalse)
765 (void) memcpy(clone_nexus[id]->pixels,cache_nexus[id]->pixels,length*
766 sizeof(Quantum));
767 else
768 {
769 const Quantum
770 *magick_restrict p;
771
772 Quantum
773 *magick_restrict q;
774
775 /*
776 Mismatched pixel channel map.
777 */
778 p=cache_nexus[id]->pixels;
779 q=clone_nexus[id]->pixels;
780 for (x=0; x < (ssize_t) cache_info->columns; x++)
781 {
782 ssize_t
783 i;
784
785 if (x == (ssize_t) clone_info->columns)
786 break;
787 for (i=0; i < (ssize_t) clone_info->number_channels; i++)
788 {
789 PixelChannel
790 channel;
791
792 PixelTrait
793 traits;
794
795 channel=clone_info->channel_map[i].channel;
796 traits=cache_info->channel_map[channel].traits;
797 if (traits != UndefinedPixelTrait)
798 *q=*(p+cache_info->channel_map[channel].offset);
799 q++;
800 }
801 p+=cache_info->number_channels;
802 }
803 }
804 status=WritePixelCachePixels(clone_info,clone_nexus[id],exception);
805 }
806 if ((cache_info->metacontent_extent != 0) &&
807 (clone_info->metacontent_extent != 0))
808 {
809 /*
810 Clone metacontent.
811 */
812 length=(size_t) MagickMin(cache_info->metacontent_extent,
813 clone_info->metacontent_extent);
814#if defined(MAGICKCORE_OPENMP_SUPPORT)
815 #pragma omp parallel for schedule(static) shared(status) \
816 cache_number_threads(cache_info,clone_info,(int) cache_info->rows,4)
817#endif
818 for (y=0; y < (ssize_t) cache_info->rows; y++)
819 {
820 const int
821 id = GetOpenMPThreadId();
822
823 Quantum
824 *pixels;
825
826 if (status == MagickFalse)
827 continue;
828 if (y >= (ssize_t) clone_info->rows)
829 continue;
830 pixels=SetPixelCacheNexusPixels(cache_info,ReadMode,0,y,
831 cache_info->columns,1,MagickFalse,cache_nexus[id],exception);
832 if (pixels == (Quantum *) NULL)
833 continue;
834 status=ReadPixelCacheMetacontent(cache_info,cache_nexus[id],exception);
835 if (status == MagickFalse)
836 continue;
837 pixels=SetPixelCacheNexusPixels(clone_info,WriteMode,0,y,
838 clone_info->columns,1,MagickFalse,clone_nexus[id],exception);
839 if (pixels == (Quantum *) NULL)
840 continue;
841 if ((clone_nexus[id]->metacontent != (void *) NULL) &&
842 (cache_nexus[id]->metacontent != (void *) NULL))
843 (void) memcpy(clone_nexus[id]->metacontent,
844 cache_nexus[id]->metacontent,length*sizeof(unsigned char));
845 status=WritePixelCacheMetacontent(clone_info,clone_nexus[id],exception);
846 }
847 }
848 clone_nexus=DestroyPixelCacheNexus(clone_nexus,clone_info->number_threads);
849 cache_nexus=DestroyPixelCacheNexus(cache_nexus,cache_info->number_threads);
850 if (cache_info->debug != MagickFalse)
851 {
852 char
853 message[MagickPathExtent];
854
855 (void) FormatLocaleString(message,MagickPathExtent,"%s => %s",
856 CommandOptionToMnemonic(MagickCacheOptions,(ssize_t) cache_info->type),
857 CommandOptionToMnemonic(MagickCacheOptions,(ssize_t) clone_info->type));
858 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",message);
859 }
860 return(status);
861}
862
863/*
864%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
865% %
866% %
867% %
868+ D e s t r o y I m a g e P i x e l C a c h e %
869% %
870% %
871% %
872%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
873%
874% DestroyImagePixelCache() deallocates memory associated with the pixel cache.
875%
876% The format of the DestroyImagePixelCache() method is:
877%
878% void DestroyImagePixelCache(Image *image)
879%
880% A description of each parameter follows:
881%
882% o image: the image.
883%
884*/
885static void DestroyImagePixelCache(Image *image)
886{
887 assert(image != (Image *) NULL);
888 assert(image->signature == MagickCoreSignature);
889 if (IsEventLogging() != MagickFalse)
890 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
891 if (image->cache != (void *) NULL)
892 image->cache=DestroyPixelCache(image->cache);
893}
894
895/*
896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897% %
898% %
899% %
900+ D e s t r o y I m a g e P i x e l s %
901% %
902% %
903% %
904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905%
906% DestroyImagePixels() deallocates memory associated with the pixel cache.
907%
908% The format of the DestroyImagePixels() method is:
909%
910% void DestroyImagePixels(Image *image)
911%
912% A description of each parameter follows:
913%
914% o image: the image.
915%
916*/
917MagickExport void DestroyImagePixels(Image *image)
918{
920 *magick_restrict cache_info;
921
922 assert(image != (const Image *) NULL);
923 assert(image->signature == MagickCoreSignature);
924 if (IsEventLogging() != MagickFalse)
925 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
926 assert(image->cache != (Cache) NULL);
927 cache_info=(CacheInfo *) image->cache;
928 assert(cache_info->signature == MagickCoreSignature);
929 if (cache_info->methods.destroy_pixel_handler != (DestroyPixelHandler) NULL)
930 {
931 cache_info->methods.destroy_pixel_handler(image);
932 return;
933 }
934 image->cache=DestroyPixelCache(image->cache);
935}
936
937/*
938%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939% %
940% %
941% %
942+ D e s t r o y P i x e l C a c h e %
943% %
944% %
945% %
946%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
947%
948% DestroyPixelCache() deallocates memory associated with the pixel cache.
949%
950% The format of the DestroyPixelCache() method is:
951%
952% Cache DestroyPixelCache(Cache cache)
953%
954% A description of each parameter follows:
955%
956% o cache: the pixel cache.
957%
958*/
959
960static MagickBooleanType ClosePixelCacheOnDisk(CacheInfo *cache_info)
961{
962 int
963 status;
964
965 status=(-1);
966 if (cache_info->file != -1)
967 {
968 status=close(cache_info->file);
969 cache_info->file=(-1);
970 RelinquishMagickResource(FileResource,1);
971 }
972 return(status == -1 ? MagickFalse : MagickTrue);
973}
974
975static inline void RelinquishPixelCachePixels(CacheInfo *cache_info)
976{
977 switch (cache_info->type)
978 {
979 case MemoryCache:
980 {
981 (void) ShredMagickMemory(cache_info->pixels,(size_t) cache_info->length);
982#if defined(MAGICKCORE_OPENCL_SUPPORT)
983 if (cache_info->opencl != (MagickCLCacheInfo) NULL)
984 {
985 cache_info->opencl=RelinquishMagickCLCacheInfo(cache_info->opencl,
986 MagickTrue);
987 cache_info->pixels=(Quantum *) NULL;
988 break;
989 }
990#endif
991 if (cache_info->mapped == MagickFalse)
992 cache_info->pixels=(Quantum *) RelinquishAlignedMemory(
993 cache_info->pixels);
994 else
995 {
996 (void) UnmapBlob(cache_info->pixels,(size_t) cache_info->length);
997 cache_info->pixels=(Quantum *) NULL;
998 }
999 RelinquishMagickResource(MemoryResource,cache_info->length);
1000 break;
1001 }
1002 case MapCache:
1003 {
1004 (void) UnmapBlob(cache_info->pixels,(size_t) cache_info->length);
1005 cache_info->pixels=(Quantum *) NULL;
1006 if ((cache_info->mode != ReadMode) && (cache_info->mode != PersistMode))
1007 (void) RelinquishUniqueFileResource(cache_info->cache_filename);
1008 *cache_info->cache_filename='\0';
1009 RelinquishMagickResource(MapResource,cache_info->length);
1010 magick_fallthrough;
1011 }
1012 case DiskCache:
1013 {
1014 if (cache_info->file != -1)
1015 (void) ClosePixelCacheOnDisk(cache_info);
1016 if ((cache_info->mode != ReadMode) && (cache_info->mode != PersistMode))
1017 (void) RelinquishUniqueFileResource(cache_info->cache_filename);
1018 *cache_info->cache_filename='\0';
1019 RelinquishMagickResource(DiskResource,cache_info->length);
1020 break;
1021 }
1022 case DistributedCache:
1023 {
1024 *cache_info->cache_filename='\0';
1025 (void) RelinquishDistributePixelCache((DistributeCacheInfo *)
1026 cache_info->server_info);
1027 break;
1028 }
1029 default:
1030 break;
1031 }
1032 cache_info->type=UndefinedCache;
1033 cache_info->mapped=MagickFalse;
1034 cache_info->metacontent=(void *) NULL;
1035}
1036
1037MagickPrivate Cache DestroyPixelCache(Cache cache)
1038{
1039 CacheInfo
1040 *magick_restrict cache_info;
1041
1042 assert(cache != (Cache) NULL);
1043 cache_info=(CacheInfo *) cache;
1044 assert(cache_info->signature == MagickCoreSignature);
1045 if (IsEventLogging() != MagickFalse)
1046 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1047 cache_info->filename);
1048 LockSemaphoreInfo(cache_info->semaphore);
1049 cache_info->reference_count--;
1050 if (cache_info->reference_count != 0)
1051 {
1052 UnlockSemaphoreInfo(cache_info->semaphore);
1053 return((Cache) NULL);
1054 }
1055 UnlockSemaphoreInfo(cache_info->semaphore);
1056 if (cache_info->debug != MagickFalse)
1057 {
1058 char
1059 message[MagickPathExtent];
1060
1061 (void) FormatLocaleString(message,MagickPathExtent,"destroy %s",
1062 cache_info->filename);
1063 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",message);
1064 }
1065 RelinquishPixelCachePixels(cache_info);
1066 if (cache_info->server_info != (DistributeCacheInfo *) NULL)
1067 cache_info->server_info=DestroyDistributeCacheInfo((DistributeCacheInfo *)
1068 cache_info->server_info);
1069 if (cache_info->nexus_info != (NexusInfo **) NULL)
1070 cache_info->nexus_info=DestroyPixelCacheNexus(cache_info->nexus_info,
1071 cache_info->number_threads);
1072 if (cache_info->random_info != (RandomInfo *) NULL)
1073 cache_info->random_info=DestroyRandomInfo(cache_info->random_info);
1074 if (cache_info->file_semaphore != (SemaphoreInfo *) NULL)
1075 RelinquishSemaphoreInfo(&cache_info->file_semaphore);
1076 if (cache_info->semaphore != (SemaphoreInfo *) NULL)
1077 RelinquishSemaphoreInfo(&cache_info->semaphore);
1078 cache_info->signature=(~MagickCoreSignature);
1079 cache_info=(CacheInfo *) RelinquishAlignedMemory(cache_info);
1080 cache=(Cache) NULL;
1081 return(cache);
1082}
1083
1084/*
1085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1086% %
1087% %
1088% %
1089+ D e s t r o y P i x e l C a c h e N e x u s %
1090% %
1091% %
1092% %
1093%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1094%
1095% DestroyPixelCacheNexus() destroys a pixel cache nexus.
1096%
1097% The format of the DestroyPixelCacheNexus() method is:
1098%
1099% NexusInfo **DestroyPixelCacheNexus(NexusInfo *nexus_info,
1100% const size_t number_threads)
1101%
1102% A description of each parameter follows:
1103%
1104% o nexus_info: the nexus to destroy.
1105%
1106% o number_threads: the number of nexus threads.
1107%
1108*/
1109
1110static inline void RelinquishCacheNexusPixels(NexusInfo *nexus_info)
1111{
1112 if (nexus_info->mapped == MagickFalse)
1113 (void) RelinquishAlignedMemory(nexus_info->cache);
1114 else
1115 (void) UnmapBlob(nexus_info->cache,(size_t) nexus_info->length);
1116 nexus_info->cache=(Quantum *) NULL;
1117 nexus_info->pixels=(Quantum *) NULL;
1118 nexus_info->metacontent=(void *) NULL;
1119 nexus_info->length=0;
1120 nexus_info->mapped=MagickFalse;
1121}
1122
1123MagickPrivate NexusInfo **DestroyPixelCacheNexus(NexusInfo **nexus_info,
1124 const size_t number_threads)
1125{
1126 ssize_t
1127 i;
1128
1129 assert(nexus_info != (NexusInfo **) NULL);
1130 for (i=0; i < (ssize_t) (2*number_threads); i++)
1131 {
1132 if (nexus_info[i]->cache != (Quantum *) NULL)
1133 RelinquishCacheNexusPixels(nexus_info[i]);
1134 nexus_info[i]->signature=(~MagickCoreSignature);
1135 }
1136 *nexus_info=(NexusInfo *) RelinquishMagickMemory(*nexus_info);
1137 nexus_info=(NexusInfo **) RelinquishAlignedMemory(nexus_info);
1138 return(nexus_info);
1139}
1140
1141
1142/*
1143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1144% %
1145% %
1146% %
1147% G e t A u t h e n t i c M e t a c o n t e n t %
1148% %
1149% %
1150% %
1151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1152%
1153% GetAuthenticMetacontent() returns the authentic metacontent corresponding
1154% with the last call to QueueAuthenticPixels() or GetVirtualPixels(). NULL is
1155% returned if the associated pixels are not available.
1156%
1157% The format of the GetAuthenticMetacontent() method is:
1158%
1159% void *GetAuthenticMetacontent(const Image *image)
1160%
1161% A description of each parameter follows:
1162%
1163% o image: the image.
1164%
1165*/
1166MagickExport void *GetAuthenticMetacontent(const Image *image)
1167{
1168 CacheInfo
1169 *magick_restrict cache_info;
1170
1171 const int
1172 id = GetOpenMPThreadId();
1173
1174 assert(image != (const Image *) NULL);
1175 assert(image->signature == MagickCoreSignature);
1176 assert(image->cache != (Cache) NULL);
1177 cache_info=(CacheInfo *) image->cache;
1178 assert(cache_info->signature == MagickCoreSignature);
1179 if (cache_info->methods.get_authentic_metacontent_from_handler !=
1180 (GetAuthenticMetacontentFromHandler) NULL)
1181 {
1182 void
1183 *metacontent;
1184
1185 metacontent=cache_info->methods.
1186 get_authentic_metacontent_from_handler(image);
1187 return(metacontent);
1188 }
1189 assert(id < (int) cache_info->number_threads);
1190 return(cache_info->nexus_info[id]->metacontent);
1191}
1192
1193/*
1194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1195% %
1196% %
1197% %
1198+ G e t A u t h e n t i c M e t a c o n t e n t F r o m C a c h e %
1199% %
1200% %
1201% %
1202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1203%
1204% GetAuthenticMetacontentFromCache() returns the meta-content corresponding
1205% with the last call to QueueAuthenticPixelsCache() or
1206% GetAuthenticPixelsCache().
1207%
1208% The format of the GetAuthenticMetacontentFromCache() method is:
1209%
1210% void *GetAuthenticMetacontentFromCache(const Image *image)
1211%
1212% A description of each parameter follows:
1213%
1214% o image: the image.
1215%
1216*/
1217static void *GetAuthenticMetacontentFromCache(const Image *image)
1218{
1219 CacheInfo
1220 *magick_restrict cache_info;
1221
1222 const int
1223 id = GetOpenMPThreadId();
1224
1225 assert(image != (const Image *) NULL);
1226 assert(image->signature == MagickCoreSignature);
1227 assert(image->cache != (Cache) NULL);
1228 cache_info=(CacheInfo *) image->cache;
1229 assert(cache_info->signature == MagickCoreSignature);
1230 assert(id < (int) cache_info->number_threads);
1231 return(cache_info->nexus_info[id]->metacontent);
1232}
1233
1234#if defined(MAGICKCORE_OPENCL_SUPPORT)
1235/*
1236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1237% %
1238% %
1239% %
1240+ G e t A u t h e n t i c O p e n C L B u f f e r %
1241% %
1242% %
1243% %
1244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245%
1246% GetAuthenticOpenCLBuffer() returns an OpenCL buffer used to execute OpenCL
1247% operations.
1248%
1249% The format of the GetAuthenticOpenCLBuffer() method is:
1250%
1251% cl_mem GetAuthenticOpenCLBuffer(const Image *image,
1252% MagickCLDevice device,ExceptionInfo *exception)
1253%
1254% A description of each parameter follows:
1255%
1256% o image: the image.
1257%
1258% o device: the device to use.
1259%
1260% o exception: return any errors or warnings in this structure.
1261%
1262*/
1263MagickPrivate cl_mem GetAuthenticOpenCLBuffer(const Image *image,
1264 MagickCLDevice device,ExceptionInfo *exception)
1265{
1266 CacheInfo
1267 *magick_restrict cache_info;
1268
1269 assert(image != (const Image *) NULL);
1270 assert(device != (const MagickCLDevice) NULL);
1271 cache_info=(CacheInfo *) image->cache;
1272 if ((cache_info->type == UndefinedCache) || (cache_info->reference_count > 1))
1273 {
1274 SyncImagePixelCache((Image *) image,exception);
1275 cache_info=(CacheInfo *) image->cache;
1276 }
1277 if ((cache_info->type != MemoryCache) || (cache_info->mapped != MagickFalse))
1278 return((cl_mem) NULL);
1279 LockSemaphoreInfo(cache_info->semaphore);
1280 if ((cache_info->opencl != (MagickCLCacheInfo) NULL) &&
1281 (cache_info->opencl->device->context != device->context))
1282 cache_info->opencl=CopyMagickCLCacheInfo(cache_info->opencl);
1283 if (cache_info->opencl == (MagickCLCacheInfo) NULL)
1284 {
1285 assert(cache_info->pixels != (Quantum *) NULL);
1286 cache_info->opencl=AcquireMagickCLCacheInfo(device,cache_info->pixels,
1287 cache_info->length);
1288 }
1289 if (cache_info->opencl != (MagickCLCacheInfo) NULL)
1290 RetainOpenCLMemObject(cache_info->opencl->buffer);
1291 UnlockSemaphoreInfo(cache_info->semaphore);
1292 if (cache_info->opencl == (MagickCLCacheInfo) NULL)
1293 return((cl_mem) NULL);
1294 assert(cache_info->opencl->pixels == cache_info->pixels);
1295 return(cache_info->opencl->buffer);
1296}
1297#endif
1298
1299/*
1300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301% %
1302% %
1303% %
1304+ G e t A u t h e n t i c P i x e l C a c h e N e x u s %
1305% %
1306% %
1307% %
1308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1309%
1310% GetAuthenticPixelCacheNexus() gets authentic pixels from the in-memory or
1311% disk pixel cache as defined by the geometry parameters. A pointer to the
1312% pixels is returned if the pixels are transferred, otherwise a NULL is
1313% returned.
1314%
1315% The format of the GetAuthenticPixelCacheNexus() method is:
1316%
1317% Quantum *GetAuthenticPixelCacheNexus(Image *image,const ssize_t x,
1318% const ssize_t y,const size_t columns,const size_t rows,
1319% NexusInfo *nexus_info,ExceptionInfo *exception)
1320%
1321% A description of each parameter follows:
1322%
1323% o image: the image.
1324%
1325% o x,y,columns,rows: These values define the perimeter of a region of
1326% pixels.
1327%
1328% o nexus_info: the cache nexus to return.
1329%
1330% o exception: return any errors or warnings in this structure.
1331%
1332*/
1333
1334MagickPrivate Quantum *GetAuthenticPixelCacheNexus(Image *image,const ssize_t x,
1335 const ssize_t y,const size_t columns,const size_t rows,NexusInfo *nexus_info,
1336 ExceptionInfo *exception)
1337{
1338 CacheInfo
1339 *magick_restrict cache_info;
1340
1341 Quantum
1342 *magick_restrict pixels;
1343
1344 /*
1345 Transfer pixels from the cache.
1346 */
1347 assert(image != (Image *) NULL);
1348 assert(image->signature == MagickCoreSignature);
1349 pixels=QueueAuthenticPixelCacheNexus(image,x,y,columns,rows,MagickTrue,
1350 nexus_info,exception);
1351 if (pixels == (Quantum *) NULL)
1352 return((Quantum *) NULL);
1353 cache_info=(CacheInfo *) image->cache;
1354 assert(cache_info->signature == MagickCoreSignature);
1355 if (nexus_info->authentic_pixel_cache != MagickFalse)
1356 return(pixels);
1357 if (ReadPixelCachePixels(cache_info,nexus_info,exception) == MagickFalse)
1358 return((Quantum *) NULL);
1359 if (cache_info->metacontent_extent != 0)
1360 if (ReadPixelCacheMetacontent(cache_info,nexus_info,exception) == MagickFalse)
1361 return((Quantum *) NULL);
1362 return(pixels);
1363}
1364
1365/*
1366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1367% %
1368% %
1369% %
1370+ G e t A u t h e n t i c P i x e l s F r o m C a c h e %
1371% %
1372% %
1373% %
1374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1375%
1376% GetAuthenticPixelsFromCache() returns the pixels associated with the last
1377% call to the QueueAuthenticPixelsCache() or GetAuthenticPixelsCache() methods.
1378%
1379% The format of the GetAuthenticPixelsFromCache() method is:
1380%
1381% Quantum *GetAuthenticPixelsFromCache(const Image image)
1382%
1383% A description of each parameter follows:
1384%
1385% o image: the image.
1386%
1387*/
1388static Quantum *GetAuthenticPixelsFromCache(const Image *image)
1389{
1390 CacheInfo
1391 *magick_restrict cache_info;
1392
1393 const int
1394 id = GetOpenMPThreadId();
1395
1396 assert(image != (const Image *) NULL);
1397 assert(image->signature == MagickCoreSignature);
1398 assert(image->cache != (Cache) NULL);
1399 cache_info=(CacheInfo *) image->cache;
1400 assert(cache_info->signature == MagickCoreSignature);
1401 assert(id < (int) cache_info->number_threads);
1402 return(cache_info->nexus_info[id]->pixels);
1403}
1404
1405/*
1406%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1407% %
1408% %
1409% %
1410% G e t A u t h e n t i c P i x e l Q u e u e %
1411% %
1412% %
1413% %
1414%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1415%
1416% GetAuthenticPixelQueue() returns the authentic pixels associated
1417% corresponding with the last call to QueueAuthenticPixels() or
1418% GetAuthenticPixels().
1419%
1420% The format of the GetAuthenticPixelQueue() method is:
1421%
1422% Quantum *GetAuthenticPixelQueue(const Image image)
1423%
1424% A description of each parameter follows:
1425%
1426% o image: the image.
1427%
1428*/
1429MagickExport Quantum *GetAuthenticPixelQueue(const Image *image)
1430{
1431 CacheInfo
1432 *magick_restrict cache_info;
1433
1434 const int
1435 id = GetOpenMPThreadId();
1436
1437 assert(image != (const Image *) NULL);
1438 assert(image->signature == MagickCoreSignature);
1439 assert(image->cache != (Cache) NULL);
1440 cache_info=(CacheInfo *) image->cache;
1441 assert(cache_info->signature == MagickCoreSignature);
1442 if (cache_info->methods.get_authentic_pixels_from_handler !=
1443 (GetAuthenticPixelsFromHandler) NULL)
1444 return(cache_info->methods.get_authentic_pixels_from_handler(image));
1445 assert(id < (int) cache_info->number_threads);
1446 return(cache_info->nexus_info[id]->pixels);
1447}
1448
1449/*
1450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451% %
1452% %
1453% %
1454% G e t A u t h e n t i c P i x e l s %
1455% %
1456% %
1457% %
1458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1459%
1460% GetAuthenticPixels() obtains a pixel region for read/write access. If the
1461% region is successfully accessed, a pointer to a Quantum array
1462% representing the region is returned, otherwise NULL is returned.
1463%
1464% The returned pointer may point to a temporary working copy of the pixels
1465% or it may point to the original pixels in memory. Performance is maximized
1466% if the selected region is part of one row, or one or more full rows, since
1467% then there is opportunity to access the pixels in-place (without a copy)
1468% if the image is in memory, or in a memory-mapped file. The returned pointer
1469% must *never* be deallocated by the user.
1470%
1471% Pixels accessed via the returned pointer represent a simple array of type
1472% Quantum. If the image has corresponding metacontent,call
1473% GetAuthenticMetacontent() after invoking GetAuthenticPixels() to obtain the
1474% meta-content corresponding to the region. Once the Quantum array has
1475% been updated, the changes must be saved back to the underlying image using
1476% SyncAuthenticPixels() or they may be lost.
1477%
1478% The format of the GetAuthenticPixels() method is:
1479%
1480% Quantum *GetAuthenticPixels(Image *image,const ssize_t x,
1481% const ssize_t y,const size_t columns,const size_t rows,
1482% ExceptionInfo *exception)
1483%
1484% A description of each parameter follows:
1485%
1486% o image: the image.
1487%
1488% o x,y,columns,rows: These values define the perimeter of a region of
1489% pixels.
1490%
1491% o exception: return any errors or warnings in this structure.
1492%
1493*/
1494MagickExport Quantum *GetAuthenticPixels(Image *image,const ssize_t x,
1495 const ssize_t y,const size_t columns,const size_t rows,
1496 ExceptionInfo *exception)
1497{
1498 CacheInfo
1499 *magick_restrict cache_info;
1500
1501 const int
1502 id = GetOpenMPThreadId();
1503
1504 Quantum
1505 *pixels;
1506
1507 assert(image != (Image *) NULL);
1508 assert(image->signature == MagickCoreSignature);
1509 assert(image->cache != (Cache) NULL);
1510 cache_info=(CacheInfo *) image->cache;
1511 assert(cache_info->signature == MagickCoreSignature);
1512 if (cache_info->methods.get_authentic_pixels_handler !=
1513 (GetAuthenticPixelsHandler) NULL)
1514 {
1515 pixels=cache_info->methods.get_authentic_pixels_handler(image,x,y,columns,
1516 rows,exception);
1517 return(pixels);
1518 }
1519 assert(id < (int) cache_info->number_threads);
1520 pixels=GetAuthenticPixelCacheNexus(image,x,y,columns,rows,
1521 cache_info->nexus_info[id],exception);
1522 return(pixels);
1523}
1524
1525/*
1526%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1527% %
1528% %
1529% %
1530+ G e t A u t h e n t i c P i x e l s C a c h e %
1531% %
1532% %
1533% %
1534%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1535%
1536% GetAuthenticPixelsCache() gets pixels from the in-memory or disk pixel cache
1537% as defined by the geometry parameters. A pointer to the pixels is returned
1538% if the pixels are transferred, otherwise a NULL is returned.
1539%
1540% The format of the GetAuthenticPixelsCache() method is:
1541%
1542% Quantum *GetAuthenticPixelsCache(Image *image,const ssize_t x,
1543% const ssize_t y,const size_t columns,const size_t rows,
1544% ExceptionInfo *exception)
1545%
1546% A description of each parameter follows:
1547%
1548% o image: the image.
1549%
1550% o x,y,columns,rows: These values define the perimeter of a region of
1551% pixels.
1552%
1553% o exception: return any errors or warnings in this structure.
1554%
1555*/
1556static Quantum *GetAuthenticPixelsCache(Image *image,const ssize_t x,
1557 const ssize_t y,const size_t columns,const size_t rows,
1558 ExceptionInfo *exception)
1559{
1560 CacheInfo
1561 *magick_restrict cache_info;
1562
1563 const int
1564 id = GetOpenMPThreadId();
1565
1566 Quantum
1567 *magick_restrict pixels;
1568
1569 assert(image != (const Image *) NULL);
1570 assert(image->signature == MagickCoreSignature);
1571 assert(image->cache != (Cache) NULL);
1572 cache_info=(CacheInfo *) image->cache;
1573 if (cache_info == (Cache) NULL)
1574 return((Quantum *) NULL);
1575 assert(cache_info->signature == MagickCoreSignature);
1576 assert(id < (int) cache_info->number_threads);
1577 pixels=GetAuthenticPixelCacheNexus(image,x,y,columns,rows,
1578 cache_info->nexus_info[id],exception);
1579 return(pixels);
1580}
1581
1582/*
1583%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1584% %
1585% %
1586% %
1587+ G e t I m a g e E x t e n t %
1588% %
1589% %
1590% %
1591%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1592%
1593% GetImageExtent() returns the extent of the pixels associated corresponding
1594% with the last call to QueueAuthenticPixels() or GetAuthenticPixels().
1595%
1596% The format of the GetImageExtent() method is:
1597%
1598% MagickSizeType GetImageExtent(const Image *image)
1599%
1600% A description of each parameter follows:
1601%
1602% o image: the image.
1603%
1604*/
1605MagickExport MagickSizeType GetImageExtent(const Image *image)
1606{
1607 CacheInfo
1608 *magick_restrict cache_info;
1609
1610 const int
1611 id = GetOpenMPThreadId();
1612
1613 assert(image != (Image *) NULL);
1614 assert(image->signature == MagickCoreSignature);
1615 if (IsEventLogging() != MagickFalse)
1616 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1617 assert(image->cache != (Cache) NULL);
1618 cache_info=(CacheInfo *) image->cache;
1619 assert(cache_info->signature == MagickCoreSignature);
1620 assert(id < (int) cache_info->number_threads);
1621 return(GetPixelCacheNexusExtent(cache_info,cache_info->nexus_info[id]));
1622}
1623
1624/*
1625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1626% %
1627% %
1628% %
1629+ G e t I m a g e P i x e l C a c h e %
1630% %
1631% %
1632% %
1633%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1634%
1635% GetImagePixelCache() ensures that there is only a single reference to the
1636% pixel cache to be modified, updating the provided cache pointer to point to
1637% a clone of the original pixel cache if necessary.
1638%
1639% The format of the GetImagePixelCache method is:
1640%
1641% Cache GetImagePixelCache(Image *image,const MagickBooleanType clone,
1642% ExceptionInfo *exception)
1643%
1644% A description of each parameter follows:
1645%
1646% o image: the image.
1647%
1648% o clone: any value other than MagickFalse clones the cache pixels.
1649%
1650% o exception: return any errors or warnings in this structure.
1651%
1652*/
1653
1654static inline MagickBooleanType ValidatePixelCacheMorphology(
1655 const Image *magick_restrict image)
1656{
1657 const CacheInfo
1658 *magick_restrict cache_info;
1659
1660 const PixelChannelMap
1661 *magick_restrict p,
1662 *magick_restrict q;
1663
1664 /*
1665 Does the image match the pixel cache morphology?
1666 */
1667 cache_info=(CacheInfo *) image->cache;
1668 p=image->channel_map;
1669 q=cache_info->channel_map;
1670 if ((image->storage_class != cache_info->storage_class) ||
1671 (image->colorspace != cache_info->colorspace) ||
1672 (image->alpha_trait != cache_info->alpha_trait) ||
1673 (image->channels != cache_info->channels) ||
1674 (image->columns != cache_info->columns) ||
1675 (image->rows != cache_info->rows) ||
1676 (image->number_channels != cache_info->number_channels) ||
1677 (memcmp(p,q,image->number_channels*sizeof(*p)) != 0) ||
1678 (image->metacontent_extent != cache_info->metacontent_extent) ||
1679 (cache_info->nexus_info == (NexusInfo **) NULL))
1680 return(MagickFalse);
1681 return(MagickTrue);
1682}
1683
1684static Cache GetImagePixelCache(Image *image,const MagickBooleanType clone,
1685 ExceptionInfo *exception)
1686{
1687 CacheInfo
1688 *magick_restrict cache_info;
1689
1690 MagickBooleanType
1691 destroy,
1692 status = MagickTrue;
1693
1694 static MagickSizeType
1695 cpu_throttle = MagickResourceInfinity,
1696 cycles = 0;
1697
1698 if (IsImageTTLExpired(image) != MagickFalse)
1699 {
1700#if defined(ESTALE)
1701 errno=ESTALE;
1702#endif
1703 (void) ThrowMagickException(exception,GetMagickModule(),
1704 ResourceLimitError,"TimeLimitExceeded","`%s'",image->filename);
1705 return((Cache) NULL);
1706 }
1707 if (cpu_throttle == MagickResourceInfinity)
1708 cpu_throttle=GetMagickResourceLimit(ThrottleResource);
1709 if ((cpu_throttle != 0) && ((cycles++ % 4096) == 0))
1710 MagickDelay(cpu_throttle);
1711 LockSemaphoreInfo(image->semaphore);
1712 assert(image->cache != (Cache) NULL);
1713 cache_info=(CacheInfo *) image->cache;
1714#if defined(MAGICKCORE_OPENCL_SUPPORT)
1715 CopyOpenCLBuffer(cache_info);
1716#endif
1717 destroy=MagickFalse;
1718 if ((cache_info->reference_count > 1) || (cache_info->mode == ReadMode))
1719 {
1720 LockSemaphoreInfo(cache_info->semaphore);
1721 if ((cache_info->reference_count > 1) || (cache_info->mode == ReadMode))
1722 {
1723 CacheInfo
1724 *clone_info;
1725
1726 Image
1727 clone_image;
1728
1729 /*
1730 Clone pixel cache.
1731 */
1732 clone_image=(*image);
1733 clone_image.semaphore=AcquireSemaphoreInfo();
1734 clone_image.reference_count=1;
1735 clone_image.cache=ClonePixelCache(cache_info);
1736 clone_info=(CacheInfo *) clone_image.cache;
1737 status=OpenPixelCache(&clone_image,IOMode,exception);
1738 if (status == MagickFalse)
1739 clone_info=(CacheInfo *) DestroyPixelCache(clone_info);
1740 else
1741 {
1742 if (clone != MagickFalse)
1743 status=ClonePixelCacheRepository(clone_info,cache_info,
1744 exception);
1745 if (status == MagickFalse)
1746 clone_info=(CacheInfo *) DestroyPixelCache(clone_info);
1747 else
1748 {
1749 destroy=MagickTrue;
1750 image->cache=clone_info;
1751 }
1752 }
1753 RelinquishSemaphoreInfo(&clone_image.semaphore);
1754 }
1755 UnlockSemaphoreInfo(cache_info->semaphore);
1756 }
1757 if (destroy != MagickFalse)
1758 cache_info=(CacheInfo *) DestroyPixelCache(cache_info);
1759 if (status != MagickFalse)
1760 {
1761 /*
1762 Ensure the image matches the pixel cache morphology.
1763 */
1764 if (image->type != UndefinedType)
1765 image->type=UndefinedType;
1766 if (ValidatePixelCacheMorphology(image) == MagickFalse)
1767 {
1768 status=OpenPixelCache(image,IOMode,exception);
1769 cache_info=(CacheInfo *) image->cache;
1770 if (cache_info->file != -1)
1771 (void) ClosePixelCacheOnDisk(cache_info);
1772 }
1773 }
1774 UnlockSemaphoreInfo(image->semaphore);
1775 if (status == MagickFalse)
1776 return((Cache) NULL);
1777 return(image->cache);
1778}
1779
1780/*
1781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1782% %
1783% %
1784% %
1785+ G e t I m a g e P i x e l C a c h e T y p e %
1786% %
1787% %
1788% %
1789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1790%
1791% GetImagePixelCacheType() returns the pixel cache type: UndefinedCache,
1792% DiskCache, MemoryCache, MapCache, or PingCache.
1793%
1794% The format of the GetImagePixelCacheType() method is:
1795%
1796% CacheType GetImagePixelCacheType(const Image *image)
1797%
1798% A description of each parameter follows:
1799%
1800% o image: the image.
1801%
1802*/
1803MagickExport CacheType GetImagePixelCacheType(const Image *image)
1804{
1805 CacheInfo
1806 *magick_restrict cache_info;
1807
1808 assert(image != (Image *) NULL);
1809 assert(image->signature == MagickCoreSignature);
1810 assert(image->cache != (Cache) NULL);
1811 cache_info=(CacheInfo *) image->cache;
1812 assert(cache_info->signature == MagickCoreSignature);
1813 return(cache_info->type);
1814}
1815
1816/*
1817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818% %
1819% %
1820% %
1821% G e t O n e A u t h e n t i c P i x e l %
1822% %
1823% %
1824% %
1825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1826%
1827% GetOneAuthenticPixel() returns a single pixel at the specified (x,y)
1828% location. The image background color is returned if an error occurs.
1829%
1830% The format of the GetOneAuthenticPixel() method is:
1831%
1832% MagickBooleanType GetOneAuthenticPixel(const Image image,const ssize_t x,
1833% const ssize_t y,Quantum *pixel,ExceptionInfo *exception)
1834%
1835% A description of each parameter follows:
1836%
1837% o image: the image.
1838%
1839% o x,y: These values define the location of the pixel to return.
1840%
1841% o pixel: return a pixel at the specified (x,y) location.
1842%
1843% o exception: return any errors or warnings in this structure.
1844%
1845*/
1846
1847static inline MagickBooleanType CopyPixel(const Image *image,
1848 const Quantum *source,Quantum *destination)
1849{
1850 ssize_t
1851 i;
1852
1853 if (source == (const Quantum *) NULL)
1854 {
1855 destination[RedPixelChannel]=ClampToQuantum(image->background_color.red);
1856 destination[GreenPixelChannel]=ClampToQuantum(
1857 image->background_color.green);
1858 destination[BluePixelChannel]=ClampToQuantum(
1859 image->background_color.blue);
1860 destination[BlackPixelChannel]=ClampToQuantum(
1861 image->background_color.black);
1862 destination[AlphaPixelChannel]=ClampToQuantum(
1863 image->background_color.alpha);
1864 return(MagickFalse);
1865 }
1866 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1867 {
1868 PixelChannel channel = GetPixelChannelChannel(image,i);
1869 destination[channel]=source[i];
1870 }
1871 return(MagickTrue);
1872}
1873
1874MagickExport MagickBooleanType GetOneAuthenticPixel(Image *image,
1875 const ssize_t x,const ssize_t y,Quantum *pixel,ExceptionInfo *exception)
1876{
1877 CacheInfo
1878 *magick_restrict cache_info;
1879
1880 Quantum
1881 *magick_restrict q;
1882
1883 assert(image != (Image *) NULL);
1884 assert(image->signature == MagickCoreSignature);
1885 assert(image->cache != (Cache) NULL);
1886 cache_info=(CacheInfo *) image->cache;
1887 assert(cache_info->signature == MagickCoreSignature);
1888 (void) memset(pixel,0,MaxPixelChannels*sizeof(*pixel));
1889 if (cache_info->methods.get_one_authentic_pixel_from_handler != (GetOneAuthenticPixelFromHandler) NULL)
1890 return(cache_info->methods.get_one_authentic_pixel_from_handler(image,x,y,pixel,exception));
1891 q=GetAuthenticPixelsCache(image,x,y,1UL,1UL,exception);
1892 return(CopyPixel(image,q,pixel));
1893}
1894
1895/*
1896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1897% %
1898% %
1899% %
1900+ G e t O n e A u t h e n t i c P i x e l F r o m C a c h e %
1901% %
1902% %
1903% %
1904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1905%
1906% GetOneAuthenticPixelFromCache() returns a single pixel at the specified (x,y)
1907% location. The image background color is returned if an error occurs.
1908%
1909% The format of the GetOneAuthenticPixelFromCache() method is:
1910%
1911% MagickBooleanType GetOneAuthenticPixelFromCache(const Image image,
1912% const ssize_t x,const ssize_t y,Quantum *pixel,
1913% ExceptionInfo *exception)
1914%
1915% A description of each parameter follows:
1916%
1917% o image: the image.
1918%
1919% o x,y: These values define the location of the pixel to return.
1920%
1921% o pixel: return a pixel at the specified (x,y) location.
1922%
1923% o exception: return any errors or warnings in this structure.
1924%
1925*/
1926static MagickBooleanType GetOneAuthenticPixelFromCache(Image *image,
1927 const ssize_t x,const ssize_t y,Quantum *pixel,ExceptionInfo *exception)
1928{
1929 CacheInfo
1930 *magick_restrict cache_info;
1931
1932 const int
1933 id = GetOpenMPThreadId();
1934
1935 Quantum
1936 *magick_restrict q;
1937
1938 assert(image != (const Image *) NULL);
1939 assert(image->signature == MagickCoreSignature);
1940 assert(image->cache != (Cache) NULL);
1941 cache_info=(CacheInfo *) image->cache;
1942 assert(cache_info->signature == MagickCoreSignature);
1943 assert(id < (int) cache_info->number_threads);
1944 (void) memset(pixel,0,MaxPixelChannels*sizeof(*pixel));
1945 q=GetAuthenticPixelCacheNexus(image,x,y,1UL,1UL,cache_info->nexus_info[id],
1946 exception);
1947 return(CopyPixel(image,q,pixel));
1948}
1949
1950/*
1951%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1952% %
1953% %
1954% %
1955% G e t O n e V i r t u a l P i x e l %
1956% %
1957% %
1958% %
1959%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1960%
1961% GetOneVirtualPixel() returns a single virtual pixel at the specified
1962% (x,y) location. The image background color is returned if an error occurs.
1963% If you plan to modify the pixel, use GetOneAuthenticPixel() instead.
1964%
1965% The format of the GetOneVirtualPixel() method is:
1966%
1967% MagickBooleanType GetOneVirtualPixel(const Image image,const ssize_t x,
1968% const ssize_t y,Quantum *pixel,ExceptionInfo exception)
1969%
1970% A description of each parameter follows:
1971%
1972% o image: the image.
1973%
1974% o x,y: These values define the location of the pixel to return.
1975%
1976% o pixel: return a pixel at the specified (x,y) location.
1977%
1978% o exception: return any errors or warnings in this structure.
1979%
1980*/
1981MagickExport MagickBooleanType GetOneVirtualPixel(const Image *image,
1982 const ssize_t x,const ssize_t y,Quantum *pixel,ExceptionInfo *exception)
1983{
1984 CacheInfo
1985 *magick_restrict cache_info;
1986
1987 const int
1988 id = GetOpenMPThreadId();
1989
1990 const Quantum
1991 *p;
1992
1993 assert(image != (const Image *) NULL);
1994 assert(image->signature == MagickCoreSignature);
1995 assert(image->cache != (Cache) NULL);
1996 cache_info=(CacheInfo *) image->cache;
1997 assert(cache_info->signature == MagickCoreSignature);
1998 (void) memset(pixel,0,MaxPixelChannels*sizeof(*pixel));
1999 if (cache_info->methods.get_one_virtual_pixel_from_handler !=
2000 (GetOneVirtualPixelFromHandler) NULL)
2001 return(cache_info->methods.get_one_virtual_pixel_from_handler(image,
2002 GetPixelCacheVirtualMethod(image),x,y,pixel,exception));
2003 assert(id < (int) cache_info->number_threads);
2004 p=GetVirtualPixelCacheNexus(image,GetPixelCacheVirtualMethod(image),x,y,
2005 1UL,1UL,cache_info->nexus_info[id],exception);
2006 return(CopyPixel(image,p,pixel));
2007}
2008
2009/*
2010%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2011% %
2012% %
2013% %
2014+ G e t O n e V i r t u a l P i x e l F r o m C a c h e %
2015% %
2016% %
2017% %
2018%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2019%
2020% GetOneVirtualPixelFromCache() returns a single virtual pixel at the
2021% specified (x,y) location. The image background color is returned if an
2022% error occurs.
2023%
2024% The format of the GetOneVirtualPixelFromCache() method is:
2025%
2026% MagickBooleanType GetOneVirtualPixelFromCache(const Image image,
2027% const VirtualPixelMethod method,const ssize_t x,const ssize_t y,
2028% Quantum *pixel,ExceptionInfo *exception)
2029%
2030% A description of each parameter follows:
2031%
2032% o image: the image.
2033%
2034% o virtual_pixel_method: the virtual pixel method.
2035%
2036% o x,y: These values define the location of the pixel to return.
2037%
2038% o pixel: return a pixel at the specified (x,y) location.
2039%
2040% o exception: return any errors or warnings in this structure.
2041%
2042*/
2043static MagickBooleanType GetOneVirtualPixelFromCache(const Image *image,
2044 const VirtualPixelMethod virtual_pixel_method,const ssize_t x,const ssize_t y,
2045 Quantum *pixel,ExceptionInfo *exception)
2046{
2047 CacheInfo
2048 *magick_restrict cache_info;
2049
2050 const int
2051 id = GetOpenMPThreadId();
2052
2053 const Quantum
2054 *p;
2055
2056 assert(image != (const Image *) NULL);
2057 assert(image->signature == MagickCoreSignature);
2058 assert(image->cache != (Cache) NULL);
2059 cache_info=(CacheInfo *) image->cache;
2060 assert(cache_info->signature == MagickCoreSignature);
2061 assert(id < (int) cache_info->number_threads);
2062 (void) memset(pixel,0,MaxPixelChannels*sizeof(*pixel));
2063 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,x,y,1UL,1UL,
2064 cache_info->nexus_info[id],exception);
2065 return(CopyPixel(image,p,pixel));
2066}
2067
2068/*
2069%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2070% %
2071% %
2072% %
2073% G e t O n e V i r t u a l P i x e l I n f o %
2074% %
2075% %
2076% %
2077%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2078%
2079% GetOneVirtualPixelInfo() returns a single pixel at the specified (x,y)
2080% location. The image background color is returned if an error occurs. If
2081% you plan to modify the pixel, use GetOneAuthenticPixel() instead.
2082%
2083% The format of the GetOneVirtualPixelInfo() method is:
2084%
2085% MagickBooleanType GetOneVirtualPixelInfo(const Image image,
2086% const VirtualPixelMethod virtual_pixel_method,const ssize_t x,
2087% const ssize_t y,PixelInfo *pixel,ExceptionInfo exception)
2088%
2089% A description of each parameter follows:
2090%
2091% o image: the image.
2092%
2093% o virtual_pixel_method: the virtual pixel method.
2094%
2095% o x,y: these values define the location of the pixel to return.
2096%
2097% o pixel: return a pixel at the specified (x,y) location.
2098%
2099% o exception: return any errors or warnings in this structure.
2100%
2101*/
2102MagickExport MagickBooleanType GetOneVirtualPixelInfo(const Image *image,
2103 const VirtualPixelMethod virtual_pixel_method,const ssize_t x,const ssize_t y,
2104 PixelInfo *pixel,ExceptionInfo *exception)
2105{
2106 CacheInfo
2107 *magick_restrict cache_info;
2108
2109 const int
2110 id = GetOpenMPThreadId();
2111
2112 const Quantum
2113 *magick_restrict p;
2114
2115 assert(image != (const Image *) NULL);
2116 assert(image->signature == MagickCoreSignature);
2117 assert(image->cache != (Cache) NULL);
2118 cache_info=(CacheInfo *) image->cache;
2119 assert(cache_info->signature == MagickCoreSignature);
2120 assert(id < (int) cache_info->number_threads);
2121 GetPixelInfo(image,pixel);
2122 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,x,y,1UL,1UL,
2123 cache_info->nexus_info[id],exception);
2124 if (p == (const Quantum *) NULL)
2125 return(MagickFalse);
2126 GetPixelInfoPixel(image,p,pixel);
2127 return(MagickTrue);
2128}
2129
2130/*
2131%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2132% %
2133% %
2134% %
2135+ G e t P i x e l C a c h e C o l o r s p a c e %
2136% %
2137% %
2138% %
2139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2140%
2141% GetPixelCacheColorspace() returns the colorspace of the pixel cache.
2142%
2143% The format of the GetPixelCacheColorspace() method is:
2144%
2145% Colorspace GetPixelCacheColorspace(const Cache cache)
2146%
2147% A description of each parameter follows:
2148%
2149% o cache: the pixel cache.
2150%
2151*/
2152MagickPrivate ColorspaceType GetPixelCacheColorspace(const Cache cache)
2153{
2154 CacheInfo
2155 *magick_restrict cache_info;
2156
2157 assert(cache != (Cache) NULL);
2158 cache_info=(CacheInfo *) cache;
2159 assert(cache_info->signature == MagickCoreSignature);
2160 if (IsEventLogging() != MagickFalse)
2161 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2162 cache_info->filename);
2163 return(cache_info->colorspace);
2164}
2165
2166/*
2167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2168% %
2169% %
2170% %
2171+ G e t P i x e l C a c h e F i l e n a m e %
2172% %
2173% %
2174% %
2175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2176%
2177% GetPixelCacheFilename() returns the filename associated with the pixel
2178% cache.
2179%
2180% The format of the GetPixelCacheFilename() method is:
2181%
2182% const char *GetPixelCacheFilename(const Image *image)
2183%
2184% A description of each parameter follows:
2185%
2186% o image: the image.
2187%
2188*/
2189MagickExport const char *GetPixelCacheFilename(const Image *image)
2190{
2191 CacheInfo
2192 *magick_restrict cache_info;
2193
2194 assert(image != (const Image *) NULL);
2195 assert(image->signature == MagickCoreSignature);
2196 assert(image->cache != (Cache) NULL);
2197 cache_info=(CacheInfo *) image->cache;
2198 assert(cache_info->signature == MagickCoreSignature);
2199 return(cache_info->cache_filename);
2200}
2201
2202/*
2203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2204% %
2205% %
2206% %
2207+ G e t P i x e l C a c h e M e t h o d s %
2208% %
2209% %
2210% %
2211%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2212%
2213% GetPixelCacheMethods() initializes the CacheMethods structure.
2214%
2215% The format of the GetPixelCacheMethods() method is:
2216%
2217% void GetPixelCacheMethods(CacheMethods *cache_methods)
2218%
2219% A description of each parameter follows:
2220%
2221% o cache_methods: Specifies a pointer to a CacheMethods structure.
2222%
2223*/
2224MagickPrivate void GetPixelCacheMethods(CacheMethods *cache_methods)
2225{
2226 assert(cache_methods != (CacheMethods *) NULL);
2227 (void) memset(cache_methods,0,sizeof(*cache_methods));
2228 cache_methods->get_virtual_pixel_handler=GetVirtualPixelCache;
2229 cache_methods->get_virtual_pixels_handler=GetVirtualPixelsCache;
2230 cache_methods->get_virtual_metacontent_from_handler=
2231 GetVirtualMetacontentFromCache;
2232 cache_methods->get_one_virtual_pixel_from_handler=GetOneVirtualPixelFromCache;
2233 cache_methods->get_authentic_pixels_handler=GetAuthenticPixelsCache;
2234 cache_methods->get_authentic_metacontent_from_handler=
2235 GetAuthenticMetacontentFromCache;
2236 cache_methods->get_authentic_pixels_from_handler=GetAuthenticPixelsFromCache;
2237 cache_methods->get_one_authentic_pixel_from_handler=
2238 GetOneAuthenticPixelFromCache;
2239 cache_methods->queue_authentic_pixels_handler=QueueAuthenticPixelsCache;
2240 cache_methods->sync_authentic_pixels_handler=SyncAuthenticPixelsCache;
2241 cache_methods->destroy_pixel_handler=DestroyImagePixelCache;
2242}
2243
2244/*
2245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2246% %
2247% %
2248% %
2249+ G e t P i x e l C a c h e N e x u s E x t e n t %
2250% %
2251% %
2252% %
2253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2254%
2255% GetPixelCacheNexusExtent() returns the extent of the pixels associated
2256% corresponding with the last call to SetPixelCacheNexusPixels() or
2257% GetPixelCacheNexusPixels().
2258%
2259% The format of the GetPixelCacheNexusExtent() method is:
2260%
2261% MagickSizeType GetPixelCacheNexusExtent(const Cache cache,
2262% NexusInfo *nexus_info)
2263%
2264% A description of each parameter follows:
2265%
2266% o nexus_info: the nexus info.
2267%
2268*/
2269MagickPrivate MagickSizeType GetPixelCacheNexusExtent(const Cache cache,
2270 NexusInfo *magick_restrict nexus_info)
2271{
2272 CacheInfo
2273 *magick_restrict cache_info;
2274
2275 MagickSizeType
2276 extent;
2277
2278 assert(cache != NULL);
2279 cache_info=(CacheInfo *) cache;
2280 assert(cache_info->signature == MagickCoreSignature);
2281 extent=(MagickSizeType) nexus_info->region.width*nexus_info->region.height;
2282 if (extent == 0)
2283 return((MagickSizeType) cache_info->columns*cache_info->rows);
2284 return(extent);
2285}
2286
2287/*
2288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2289% %
2290% %
2291% %
2292+ G e t P i x e l C a c h e P i x e l s %
2293% %
2294% %
2295% %
2296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2297%
2298% GetPixelCachePixels() returns the pixels associated with the specified image.
2299%
2300% The format of the GetPixelCachePixels() method is:
2301%
2302% void *GetPixelCachePixels(Image *image,MagickSizeType *length,
2303% ExceptionInfo *exception)
2304%
2305% A description of each parameter follows:
2306%
2307% o image: the image.
2308%
2309% o length: the pixel cache length.
2310%
2311% o exception: return any errors or warnings in this structure.
2312%
2313*/
2314MagickExport void *GetPixelCachePixels(Image *image,MagickSizeType *length,
2315 ExceptionInfo *magick_unused(exception))
2316{
2317 CacheInfo
2318 *magick_restrict cache_info;
2319
2320 assert(image != (const Image *) NULL);
2321 assert(image->signature == MagickCoreSignature);
2322 assert(image->cache != (Cache) NULL);
2323 assert(length != (MagickSizeType *) NULL);
2324 magick_unreferenced(exception);
2325 cache_info=(CacheInfo *) image->cache;
2326 assert(cache_info->signature == MagickCoreSignature);
2327 *length=cache_info->length;
2328 if ((cache_info->type != MemoryCache) && (cache_info->type != MapCache))
2329 return((void *) NULL);
2330 return((void *) cache_info->pixels);
2331}
2332
2333/*
2334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2335% %
2336% %
2337% %
2338+ G e t P i x e l C a c h e S t o r a g e C l a s s %
2339% %
2340% %
2341% %
2342%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2343%
2344% GetPixelCacheStorageClass() returns the class type of the pixel cache.
2345%
2346% The format of the GetPixelCacheStorageClass() method is:
2347%
2348% ClassType GetPixelCacheStorageClass(Cache cache)
2349%
2350% A description of each parameter follows:
2351%
2352% o type: GetPixelCacheStorageClass returns DirectClass or PseudoClass.
2353%
2354% o cache: the pixel cache.
2355%
2356*/
2357MagickPrivate ClassType GetPixelCacheStorageClass(const Cache cache)
2358{
2359 CacheInfo
2360 *magick_restrict cache_info;
2361
2362 assert(cache != (Cache) NULL);
2363 cache_info=(CacheInfo *) cache;
2364 assert(cache_info->signature == MagickCoreSignature);
2365 if (IsEventLogging() != MagickFalse)
2366 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2367 cache_info->filename);
2368 return(cache_info->storage_class);
2369}
2370
2371/*
2372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2373% %
2374% %
2375% %
2376+ G e t P i x e l C a c h e T i l e S i z e %
2377% %
2378% %
2379% %
2380%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2381%
2382% GetPixelCacheTileSize() returns the pixel cache tile size.
2383%
2384% The format of the GetPixelCacheTileSize() method is:
2385%
2386% void GetPixelCacheTileSize(const Image *image,size_t *width,
2387% size_t *height)
2388%
2389% A description of each parameter follows:
2390%
2391% o image: the image.
2392%
2393% o width: the optimized cache tile width in pixels.
2394%
2395% o height: the optimized cache tile height in pixels.
2396%
2397*/
2398MagickPrivate void GetPixelCacheTileSize(const Image *image,size_t *width,
2399 size_t *height)
2400{
2401 CacheInfo
2402 *magick_restrict cache_info;
2403
2404 assert(image != (Image *) NULL);
2405 assert(image->signature == MagickCoreSignature);
2406 if (IsEventLogging() != MagickFalse)
2407 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2408 cache_info=(CacheInfo *) image->cache;
2409 assert(cache_info->signature == MagickCoreSignature);
2410 *width=2048UL/(MagickMax(cache_info->number_channels,1)*sizeof(Quantum));
2411 if (GetImagePixelCacheType(image) == DiskCache)
2412 *width=8192UL/(MagickMax(cache_info->number_channels,1)*sizeof(Quantum));
2413 *height=(*width);
2414}
2415
2416/*
2417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2418% %
2419% %
2420% %
2421+ G e t P i x e l C a c h e V i r t u a l M e t h o d %
2422% %
2423% %
2424% %
2425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2426%
2427% GetPixelCacheVirtualMethod() gets the "virtual pixels" method for the
2428% pixel cache. A virtual pixel is any pixel access that is outside the
2429% boundaries of the image cache.
2430%
2431% The format of the GetPixelCacheVirtualMethod() method is:
2432%
2433% VirtualPixelMethod GetPixelCacheVirtualMethod(const Image *image)
2434%
2435% A description of each parameter follows:
2436%
2437% o image: the image.
2438%
2439*/
2440MagickPrivate VirtualPixelMethod GetPixelCacheVirtualMethod(const Image *image)
2441{
2442 CacheInfo
2443 *magick_restrict cache_info;
2444
2445 assert(image != (Image *) NULL);
2446 assert(image->signature == MagickCoreSignature);
2447 assert(image->cache != (Cache) NULL);
2448 cache_info=(CacheInfo *) image->cache;
2449 assert(cache_info->signature == MagickCoreSignature);
2450 return(cache_info->virtual_pixel_method);
2451}
2452
2453/*
2454%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2455% %
2456% %
2457% %
2458+ G e t V i r t u a l M e t a c o n t e n t F r o m C a c h e %
2459% %
2460% %
2461% %
2462%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2463%
2464% GetVirtualMetacontentFromCache() returns the meta-content corresponding with
2465% the last call to QueueAuthenticPixelsCache() or GetVirtualPixelCache().
2466%
2467% The format of the GetVirtualMetacontentFromCache() method is:
2468%
2469% void *GetVirtualMetacontentFromCache(const Image *image)
2470%
2471% A description of each parameter follows:
2472%
2473% o image: the image.
2474%
2475*/
2476static const void *GetVirtualMetacontentFromCache(const Image *image)
2477{
2478 CacheInfo
2479 *magick_restrict cache_info;
2480
2481 const int
2482 id = GetOpenMPThreadId();
2483
2484 const void
2485 *magick_restrict metacontent;
2486
2487 assert(image != (const Image *) NULL);
2488 assert(image->signature == MagickCoreSignature);
2489 assert(image->cache != (Cache) NULL);
2490 cache_info=(CacheInfo *) image->cache;
2491 assert(cache_info->signature == MagickCoreSignature);
2492 assert(id < (int) cache_info->number_threads);
2493 metacontent=GetVirtualMetacontentFromNexus(cache_info,
2494 cache_info->nexus_info[id]);
2495 return(metacontent);
2496}
2497
2498/*
2499%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2500% %
2501% %
2502% %
2503+ G e t V i r t u a l M e t a c o n t e n t F r o m N e x u s %
2504% %
2505% %
2506% %
2507%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2508%
2509% GetVirtualMetacontentFromNexus() returns the meta-content for the specified
2510% cache nexus.
2511%
2512% The format of the GetVirtualMetacontentFromNexus() method is:
2513%
2514% const void *GetVirtualMetacontentFromNexus(const Cache cache,
2515% NexusInfo *nexus_info)
2516%
2517% A description of each parameter follows:
2518%
2519% o cache: the pixel cache.
2520%
2521% o nexus_info: the cache nexus to return the meta-content.
2522%
2523*/
2524MagickPrivate const void *GetVirtualMetacontentFromNexus(const Cache cache,
2525 NexusInfo *magick_restrict nexus_info)
2526{
2527 CacheInfo
2528 *magick_restrict cache_info;
2529
2530 assert(cache != (Cache) NULL);
2531 cache_info=(CacheInfo *) cache;
2532 assert(cache_info->signature == MagickCoreSignature);
2533 if (cache_info->storage_class == UndefinedClass)
2534 return((void *) NULL);
2535 return(nexus_info->metacontent);
2536}
2537
2538/*
2539%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2540% %
2541% %
2542% %
2543% G e t V i r t u a l M e t a c o n t e n t %
2544% %
2545% %
2546% %
2547%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2548%
2549% GetVirtualMetacontent() returns the virtual metacontent corresponding with
2550% the last call to QueueAuthenticPixels() or GetVirtualPixels(). NULL is
2551% returned if the meta-content are not available.
2552%
2553% The format of the GetVirtualMetacontent() method is:
2554%
2555% const void *GetVirtualMetacontent(const Image *image)
2556%
2557% A description of each parameter follows:
2558%
2559% o image: the image.
2560%
2561*/
2562MagickExport const void *GetVirtualMetacontent(const Image *image)
2563{
2564 CacheInfo
2565 *magick_restrict cache_info;
2566
2567 const int
2568 id = GetOpenMPThreadId();
2569
2570 const void
2571 *magick_restrict metacontent;
2572
2573 assert(image != (const Image *) NULL);
2574 assert(image->signature == MagickCoreSignature);
2575 assert(image->cache != (Cache) NULL);
2576 cache_info=(CacheInfo *) image->cache;
2577 assert(cache_info->signature == MagickCoreSignature);
2578 if (cache_info->methods.get_virtual_metacontent_from_handler != (GetVirtualMetacontentFromHandler) NULL)
2579 {
2580 metacontent=cache_info->methods.get_virtual_metacontent_from_handler(
2581 image);
2582 if (metacontent != (const void *) NULL)
2583 return(metacontent);
2584 }
2585 assert(id < (int) cache_info->number_threads);
2586 metacontent=GetVirtualMetacontentFromNexus(cache_info,
2587 cache_info->nexus_info[id]);
2588 return(metacontent);
2589}
2590
2591/*
2592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2593% %
2594% %
2595% %
2596+ G e t V i r t u a l P i x e l C a c h e N e x u s %
2597% %
2598% %
2599% %
2600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2601%
2602% GetVirtualPixelCacheNexus() gets virtual pixels from the in-memory or disk
2603% pixel cache as defined by the geometry parameters. A pointer to the pixels
2604% is returned if the pixels are transferred, otherwise a NULL is returned.
2605%
2606% The format of the GetVirtualPixelCacheNexus() method is:
2607%
2608% Quantum *GetVirtualPixelCacheNexus(const Image *image,
2609% const VirtualPixelMethod method,const ssize_t x,const ssize_t y,
2610% const size_t columns,const size_t rows,NexusInfo *nexus_info,
2611% ExceptionInfo *exception)
2612%
2613% A description of each parameter follows:
2614%
2615% o image: the image.
2616%
2617% o virtual_pixel_method: the virtual pixel method.
2618%
2619% o x,y,columns,rows: These values define the perimeter of a region of
2620% pixels.
2621%
2622% o nexus_info: the cache nexus to acquire.
2623%
2624% o exception: return any errors or warnings in this structure.
2625%
2626*/
2627
2628static ssize_t
2629 DitherMatrix[64] =
2630 {
2631 0, 48, 12, 60, 3, 51, 15, 63,
2632 32, 16, 44, 28, 35, 19, 47, 31,
2633 8, 56, 4, 52, 11, 59, 7, 55,
2634 40, 24, 36, 20, 43, 27, 39, 23,
2635 2, 50, 14, 62, 1, 49, 13, 61,
2636 34, 18, 46, 30, 33, 17, 45, 29,
2637 10, 58, 6, 54, 9, 57, 5, 53,
2638 42, 26, 38, 22, 41, 25, 37, 21
2639 };
2640
2641static inline ssize_t DitherX(const ssize_t x,const size_t columns)
2642{
2643 ssize_t
2644 index;
2645
2646 index=x+DitherMatrix[x & 0x07]-32L;
2647 if (index < 0L)
2648 return(0L);
2649 if (index >= (ssize_t) columns)
2650 return((ssize_t) columns-1L);
2651 return(index);
2652}
2653
2654static inline ssize_t DitherY(const ssize_t y,const size_t rows)
2655{
2656 ssize_t
2657 index;
2658
2659 index=y+DitherMatrix[y & 0x07]-32L;
2660 if (index < 0L)
2661 return(0L);
2662 if (index >= (ssize_t) rows)
2663 return((ssize_t) rows-1L);
2664 return(index);
2665}
2666
2667static inline ssize_t EdgeX(const ssize_t x,const size_t columns)
2668{
2669 if (x < 0L)
2670 return(0L);
2671 if (x >= (ssize_t) columns)
2672 return((ssize_t) (columns-1));
2673 return(x);
2674}
2675
2676static inline ssize_t EdgeY(const ssize_t y,const size_t rows)
2677{
2678 if (y < 0L)
2679 return(0L);
2680 if (y >= (ssize_t) rows)
2681 return((ssize_t) (rows-1));
2682 return(y);
2683}
2684
2685static inline MagickBooleanType IsOffsetOverflow(const ssize_t x,
2686 const ssize_t y)
2687{
2688 if (((y > 0) && (x > (MAGICK_SSIZE_MAX-y))) ||
2689 ((y < 0) && (x < (MAGICK_SSIZE_MIN-y))))
2690 return(MagickFalse);
2691 return(MagickTrue);
2692}
2693
2694static inline ssize_t RandomX(RandomInfo *random_info,const size_t columns)
2695{
2696 return((ssize_t) (columns*GetPseudoRandomValue(random_info)));
2697}
2698
2699static inline ssize_t RandomY(RandomInfo *random_info,const size_t rows)
2700{
2701 return((ssize_t) (rows*GetPseudoRandomValue(random_info)));
2702}
2703
2704static inline MagickModulo VirtualPixelModulo(const ssize_t offset,
2705 const size_t extent)
2706{
2708 modulo;
2709
2710 modulo.quotient=offset;
2711 modulo.remainder=0;
2712 if (extent != 0)
2713 {
2714 modulo.quotient=offset/((ssize_t) extent);
2715 modulo.remainder=offset % ((ssize_t) extent);
2716 }
2717 if ((modulo.remainder != 0) && ((offset ^ ((ssize_t) extent)) < 0))
2718 {
2719 modulo.quotient-=1;
2720 modulo.remainder+=((ssize_t) extent);
2721 }
2722 return(modulo);
2723}
2724
2725MagickPrivate const Quantum *GetVirtualPixelCacheNexus(const Image *image,
2726 const VirtualPixelMethod virtual_pixel_method,const ssize_t x,const ssize_t y,
2727 const size_t columns,const size_t rows,NexusInfo *nexus_info,
2728 ExceptionInfo *exception)
2729{
2730 CacheInfo
2731 *magick_restrict cache_info;
2732
2733 const Quantum
2734 *magick_restrict p;
2735
2736 const void
2737 *magick_restrict r;
2738
2739 MagickOffsetType
2740 offset;
2741
2742 MagickSizeType
2743 length,
2744 number_pixels;
2745
2746 NexusInfo
2747 *magick_restrict virtual_nexus;
2748
2749 Quantum
2750 *magick_restrict pixels,
2751 *magick_restrict q,
2752 virtual_pixel[MaxPixelChannels];
2753
2754 ssize_t
2755 i,
2756 u,
2757 v;
2758
2759 unsigned char
2760 *magick_restrict s;
2761
2762 void
2763 *magick_restrict virtual_metacontent;
2764
2765 /*
2766 Acquire pixels.
2767 */
2768 assert(image != (const Image *) NULL);
2769 assert(image->signature == MagickCoreSignature);
2770 assert(image->cache != (Cache) NULL);
2771 cache_info=(CacheInfo *) image->cache;
2772 assert(cache_info->signature == MagickCoreSignature);
2773 if (cache_info->type == UndefinedCache)
2774 return((const Quantum *) NULL);
2775#if defined(MAGICKCORE_OPENCL_SUPPORT)
2776 CopyOpenCLBuffer(cache_info);
2777#endif
2778 pixels=SetPixelCacheNexusPixels(cache_info,ReadMode,x,y,columns,rows,
2779 ((image->channels & WriteMaskChannel) != 0) ||
2780 ((image->channels & CompositeMaskChannel) != 0) ? MagickTrue : MagickFalse,
2781 nexus_info,exception);
2782 if (pixels == (Quantum *) NULL)
2783 return((const Quantum *) NULL);
2784 if (IsValidPixelOffset(nexus_info->region.y,cache_info->columns) == MagickFalse)
2785 return((const Quantum *) NULL);
2786 offset=nexus_info->region.y*(MagickOffsetType) cache_info->columns;
2787 if (IsOffsetOverflow(offset,nexus_info->region.x) == MagickFalse)
2788 return((const Quantum *) NULL);
2789 offset+=nexus_info->region.x;
2790 length=(MagickSizeType) (nexus_info->region.height-1L)*cache_info->columns+
2791 nexus_info->region.width-1L;
2792 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
2793 if ((offset >= 0) && (((MagickSizeType) offset+length) < number_pixels))
2794 if ((x >= 0) && ((x+(ssize_t) columns-1) < (ssize_t) cache_info->columns) &&
2795 (y >= 0) && ((y+(ssize_t) rows-1) < (ssize_t) cache_info->rows))
2796 {
2797 MagickBooleanType
2798 status;
2799
2800 /*
2801 Pixel request is inside cache extents.
2802 */
2803 if (nexus_info->authentic_pixel_cache != MagickFalse)
2804 return(pixels);
2805 status=ReadPixelCachePixels(cache_info,nexus_info,exception);
2806 if (status == MagickFalse)
2807 return((const Quantum *) NULL);
2808 if (cache_info->metacontent_extent != 0)
2809 {
2810 status=ReadPixelCacheMetacontent(cache_info,nexus_info,exception);
2811 if (status == MagickFalse)
2812 return((const Quantum *) NULL);
2813 }
2814 return(pixels);
2815 }
2816 /*
2817 Pixel request is outside cache extents.
2818 */
2819 virtual_nexus=nexus_info->virtual_nexus;
2820 q=pixels;
2821 s=(unsigned char *) nexus_info->metacontent;
2822 (void) memset(virtual_pixel,0,cache_info->number_channels*
2823 sizeof(*virtual_pixel));
2824 virtual_metacontent=(void *) NULL;
2825 switch (virtual_pixel_method)
2826 {
2827 case BackgroundVirtualPixelMethod:
2828 case BlackVirtualPixelMethod:
2829 case GrayVirtualPixelMethod:
2830 case TransparentVirtualPixelMethod:
2831 case MaskVirtualPixelMethod:
2832 case WhiteVirtualPixelMethod:
2833 case EdgeVirtualPixelMethod:
2834 case CheckerTileVirtualPixelMethod:
2835 case HorizontalTileVirtualPixelMethod:
2836 case VerticalTileVirtualPixelMethod:
2837 {
2838 if (cache_info->metacontent_extent != 0)
2839 {
2840 /*
2841 Acquire a metacontent buffer.
2842 */
2843 virtual_metacontent=(void *) AcquireQuantumMemory(1,
2844 cache_info->metacontent_extent);
2845 if (virtual_metacontent == (void *) NULL)
2846 {
2847 (void) ThrowMagickException(exception,GetMagickModule(),
2848 CacheError,"UnableToGetCacheNexus","`%s'",image->filename);
2849 return((const Quantum *) NULL);
2850 }
2851 (void) memset(virtual_metacontent,0,cache_info->metacontent_extent);
2852 }
2853 switch (virtual_pixel_method)
2854 {
2855 case BlackVirtualPixelMethod:
2856 {
2857 for (i=0; i < (ssize_t) cache_info->number_channels; i++)
2858 SetPixelChannel(image,(PixelChannel) i,(Quantum) 0,virtual_pixel);
2859 SetPixelAlpha(image,OpaqueAlpha,virtual_pixel);
2860 break;
2861 }
2862 case GrayVirtualPixelMethod:
2863 {
2864 for (i=0; i < (ssize_t) cache_info->number_channels; i++)
2865 SetPixelChannel(image,(PixelChannel) i,QuantumRange/2,
2866 virtual_pixel);
2867 SetPixelAlpha(image,OpaqueAlpha,virtual_pixel);
2868 break;
2869 }
2870 case TransparentVirtualPixelMethod:
2871 {
2872 for (i=0; i < (ssize_t) cache_info->number_channels; i++)
2873 SetPixelChannel(image,(PixelChannel) i,(Quantum) 0,virtual_pixel);
2874 SetPixelAlpha(image,TransparentAlpha,virtual_pixel);
2875 break;
2876 }
2877 case MaskVirtualPixelMethod:
2878 case WhiteVirtualPixelMethod:
2879 {
2880 for (i=0; i < (ssize_t) cache_info->number_channels; i++)
2881 SetPixelChannel(image,(PixelChannel) i,QuantumRange,virtual_pixel);
2882 SetPixelAlpha(image,OpaqueAlpha,virtual_pixel);
2883 break;
2884 }
2885 default:
2886 {
2887 SetPixelRed(image,ClampToQuantum(image->background_color.red),
2888 virtual_pixel);
2889 SetPixelGreen(image,ClampToQuantum(image->background_color.green),
2890 virtual_pixel);
2891 SetPixelBlue(image,ClampToQuantum(image->background_color.blue),
2892 virtual_pixel);
2893 SetPixelBlack(image,ClampToQuantum(image->background_color.black),
2894 virtual_pixel);
2895 SetPixelAlpha(image,ClampToQuantum(image->background_color.alpha),
2896 virtual_pixel);
2897 break;
2898 }
2899 }
2900 break;
2901 }
2902 default:
2903 break;
2904 }
2905 for (v=0; v < (ssize_t) rows; v++)
2906 {
2907 ssize_t
2908 y_offset;
2909
2910 y_offset=y+v;
2911 if ((virtual_pixel_method == EdgeVirtualPixelMethod) ||
2912 (virtual_pixel_method == UndefinedVirtualPixelMethod))
2913 y_offset=EdgeY(y_offset,cache_info->rows);
2914 for (u=0; u < (ssize_t) columns; u+=(ssize_t) length)
2915 {
2916 ssize_t
2917 x_offset;
2918
2919 x_offset=x+u;
2920 length=(MagickSizeType) MagickMin((ssize_t) cache_info->columns-
2921 x_offset,(ssize_t) columns-u);
2922 if (((x_offset < 0) || (x_offset >= (ssize_t) cache_info->columns)) ||
2923 ((y_offset < 0) || (y_offset >= (ssize_t) cache_info->rows)) ||
2924 (length == 0))
2925 {
2927 x_modulo,
2928 y_modulo;
2929
2930 /*
2931 Transfer a single pixel.
2932 */
2933 length=(MagickSizeType) 1;
2934 switch (virtual_pixel_method)
2935 {
2936 case EdgeVirtualPixelMethod:
2937 default:
2938 {
2939 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
2940 EdgeX(x_offset,cache_info->columns),
2941 EdgeY(y_offset,cache_info->rows),1UL,1UL,virtual_nexus,
2942 exception);
2943 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
2944 break;
2945 }
2946 case RandomVirtualPixelMethod:
2947 {
2948 if (cache_info->random_info == (RandomInfo *) NULL)
2949 cache_info->random_info=AcquireRandomInfo();
2950 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
2951 RandomX(cache_info->random_info,cache_info->columns),
2952 RandomY(cache_info->random_info,cache_info->rows),1UL,1UL,
2953 virtual_nexus,exception);
2954 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
2955 break;
2956 }
2957 case DitherVirtualPixelMethod:
2958 {
2959 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
2960 DitherX(x_offset,cache_info->columns),
2961 DitherY(y_offset,cache_info->rows),1UL,1UL,virtual_nexus,
2962 exception);
2963 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
2964 break;
2965 }
2966 case TileVirtualPixelMethod:
2967 {
2968 x_modulo=VirtualPixelModulo(x_offset,cache_info->columns);
2969 y_modulo=VirtualPixelModulo(y_offset,cache_info->rows);
2970 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
2971 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus,
2972 exception);
2973 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
2974 break;
2975 }
2976 case MirrorVirtualPixelMethod:
2977 {
2978 x_modulo=VirtualPixelModulo(x_offset,cache_info->columns);
2979 if ((x_modulo.quotient & 0x01) == 1L)
2980 x_modulo.remainder=(ssize_t) cache_info->columns-
2981 x_modulo.remainder-1L;
2982 y_modulo=VirtualPixelModulo(y_offset,cache_info->rows);
2983 if ((y_modulo.quotient & 0x01) == 1L)
2984 y_modulo.remainder=(ssize_t) cache_info->rows-
2985 y_modulo.remainder-1L;
2986 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
2987 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus,
2988 exception);
2989 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
2990 break;
2991 }
2992 case HorizontalTileEdgeVirtualPixelMethod:
2993 {
2994 x_modulo=VirtualPixelModulo(x_offset,cache_info->columns);
2995 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
2996 x_modulo.remainder,EdgeY(y_offset,cache_info->rows),1UL,1UL,
2997 virtual_nexus,exception);
2998 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
2999 break;
3000 }
3001 case VerticalTileEdgeVirtualPixelMethod:
3002 {
3003 y_modulo=VirtualPixelModulo(y_offset,cache_info->rows);
3004 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
3005 EdgeX(x_offset,cache_info->columns),y_modulo.remainder,1UL,1UL,
3006 virtual_nexus,exception);
3007 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
3008 break;
3009 }
3010 case BackgroundVirtualPixelMethod:
3011 case BlackVirtualPixelMethod:
3012 case GrayVirtualPixelMethod:
3013 case TransparentVirtualPixelMethod:
3014 case MaskVirtualPixelMethod:
3015 case WhiteVirtualPixelMethod:
3016 {
3017 p=virtual_pixel;
3018 r=virtual_metacontent;
3019 break;
3020 }
3021 case CheckerTileVirtualPixelMethod:
3022 {
3023 x_modulo=VirtualPixelModulo(x_offset,cache_info->columns);
3024 y_modulo=VirtualPixelModulo(y_offset,cache_info->rows);
3025 if (((x_modulo.quotient ^ y_modulo.quotient) & 0x01) != 0L)
3026 {
3027 p=virtual_pixel;
3028 r=virtual_metacontent;
3029 break;
3030 }
3031 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
3032 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus,
3033 exception);
3034 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
3035 break;
3036 }
3037 case HorizontalTileVirtualPixelMethod:
3038 {
3039 if ((y_offset < 0) || (y_offset >= (ssize_t) cache_info->rows))
3040 {
3041 p=virtual_pixel;
3042 r=virtual_metacontent;
3043 break;
3044 }
3045 x_modulo=VirtualPixelModulo(x_offset,cache_info->columns);
3046 y_modulo=VirtualPixelModulo(y_offset,cache_info->rows);
3047 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
3048 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus,
3049 exception);
3050 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
3051 break;
3052 }
3053 case VerticalTileVirtualPixelMethod:
3054 {
3055 if ((x_offset < 0) || (x_offset >= (ssize_t) cache_info->columns))
3056 {
3057 p=virtual_pixel;
3058 r=virtual_metacontent;
3059 break;
3060 }
3061 x_modulo=VirtualPixelModulo(x_offset,cache_info->columns);
3062 y_modulo=VirtualPixelModulo(y_offset,cache_info->rows);
3063 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,
3064 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus,
3065 exception);
3066 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
3067 break;
3068 }
3069 }
3070 if (p == (const Quantum *) NULL)
3071 break;
3072 (void) memcpy(q,p,(size_t) (cache_info->number_channels*length*
3073 sizeof(*p)));
3074 q+=cache_info->number_channels;
3075 if ((s != (void *) NULL) && (r != (const void *) NULL))
3076 {
3077 (void) memcpy(s,r,(size_t) cache_info->metacontent_extent);
3078 s+=cache_info->metacontent_extent;
3079 }
3080 continue;
3081 }
3082 /*
3083 Transfer a run of pixels.
3084 */
3085 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,x_offset,y_offset,
3086 (size_t) length,1UL,virtual_nexus,exception);
3087 if (p == (const Quantum *) NULL)
3088 break;
3089 r=GetVirtualMetacontentFromNexus(cache_info,virtual_nexus);
3090 (void) memcpy(q,p,(size_t) (cache_info->number_channels*length*
3091 sizeof(*p)));
3092 q+=cache_info->number_channels*length;
3093 if ((r != (void *) NULL) && (s != (const void *) NULL))
3094 {
3095 (void) memcpy(s,r,(size_t) length);
3096 s+=length*cache_info->metacontent_extent;
3097 }
3098 }
3099 if (u < (ssize_t) columns)
3100 break;
3101 }
3102 /*
3103 Free resources.
3104 */
3105 if (virtual_metacontent != (void *) NULL)
3106 virtual_metacontent=(void *) RelinquishMagickMemory(virtual_metacontent);
3107 if (v < (ssize_t) rows)
3108 return((const Quantum *) NULL);
3109 return(pixels);
3110}
3111
3112/*
3113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3114% %
3115% %
3116% %
3117+ G e t V i r t u a l P i x e l C a c h e %
3118% %
3119% %
3120% %
3121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3122%
3123% GetVirtualPixelCache() get virtual pixels from the in-memory or disk pixel
3124% cache as defined by the geometry parameters. A pointer to the pixels
3125% is returned if the pixels are transferred, otherwise a NULL is returned.
3126%
3127% The format of the GetVirtualPixelCache() method is:
3128%
3129% const Quantum *GetVirtualPixelCache(const Image *image,
3130% const VirtualPixelMethod virtual_pixel_method,const ssize_t x,
3131% const ssize_t y,const size_t columns,const size_t rows,
3132% ExceptionInfo *exception)
3133%
3134% A description of each parameter follows:
3135%
3136% o image: the image.
3137%
3138% o virtual_pixel_method: the virtual pixel method.
3139%
3140% o x,y,columns,rows: These values define the perimeter of a region of
3141% pixels.
3142%
3143% o exception: return any errors or warnings in this structure.
3144%
3145*/
3146static const Quantum *GetVirtualPixelCache(const Image *image,
3147 const VirtualPixelMethod virtual_pixel_method,const ssize_t x,const ssize_t y,
3148 const size_t columns,const size_t rows,ExceptionInfo *exception)
3149{
3150 CacheInfo
3151 *magick_restrict cache_info;
3152
3153 const int
3154 id = GetOpenMPThreadId();
3155
3156 const Quantum
3157 *magick_restrict p;
3158
3159 assert(image != (const Image *) NULL);
3160 assert(image->signature == MagickCoreSignature);
3161 assert(image->cache != (Cache) NULL);
3162 cache_info=(CacheInfo *) image->cache;
3163 assert(cache_info->signature == MagickCoreSignature);
3164 assert(id < (int) cache_info->number_threads);
3165 p=GetVirtualPixelCacheNexus(image,virtual_pixel_method,x,y,columns,rows,
3166 cache_info->nexus_info[id],exception);
3167 return(p);
3168}
3169
3170/*
3171%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3172% %
3173% %
3174% %
3175% G e t V i r t u a l P i x e l Q u e u e %
3176% %
3177% %
3178% %
3179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3180%
3181% GetVirtualPixelQueue() returns the virtual pixels associated corresponding
3182% with the last call to QueueAuthenticPixels() or GetVirtualPixels().
3183%
3184% The format of the GetVirtualPixelQueue() method is:
3185%
3186% const Quantum *GetVirtualPixelQueue(const Image image)
3187%
3188% A description of each parameter follows:
3189%
3190% o image: the image.
3191%
3192*/
3193MagickExport const Quantum *GetVirtualPixelQueue(const Image *image)
3194{
3195 CacheInfo
3196 *magick_restrict cache_info;
3197
3198 const int
3199 id = GetOpenMPThreadId();
3200
3201 assert(image != (const Image *) NULL);
3202 assert(image->signature == MagickCoreSignature);
3203 assert(image->cache != (Cache) NULL);
3204 cache_info=(CacheInfo *) image->cache;
3205 assert(cache_info->signature == MagickCoreSignature);
3206 if (cache_info->methods.get_virtual_pixels_handler !=
3207 (GetVirtualPixelsHandler) NULL)
3208 return(cache_info->methods.get_virtual_pixels_handler(image));
3209 assert(id < (int) cache_info->number_threads);
3210 return(GetVirtualPixelsNexus(cache_info,cache_info->nexus_info[id]));
3211}
3212
3213/*
3214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3215% %
3216% %
3217% %
3218% G e t V i r t u a l P i x e l s %
3219% %
3220% %
3221% %
3222%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3223%
3224% GetVirtualPixels() returns an immutable pixel region. If the
3225% region is successfully accessed, a pointer to it is returned, otherwise
3226% NULL is returned. The returned pointer may point to a temporary working
3227% copy of the pixels or it may point to the original pixels in memory.
3228% Performance is maximized if the selected region is part of one row, or one
3229% or more full rows, since there is opportunity to access the pixels in-place
3230% (without a copy) if the image is in memory, or in a memory-mapped file. The
3231% returned pointer must *never* be deallocated by the user.
3232%
3233% Pixels accessed via the returned pointer represent a simple array of type
3234% Quantum. If the image type is CMYK or the storage class is PseudoClass,
3235% call GetAuthenticMetacontent() after invoking GetAuthenticPixels() to
3236% access the meta-content (of type void) corresponding to the
3237% region.
3238%
3239% If you plan to modify the pixels, use GetAuthenticPixels() instead.
3240%
3241% Note, the GetVirtualPixels() and GetAuthenticPixels() methods are not thread-
3242% safe. In a threaded environment, use GetCacheViewVirtualPixels() or
3243% GetCacheViewAuthenticPixels() instead.
3244%
3245% The format of the GetVirtualPixels() method is:
3246%
3247% const Quantum *GetVirtualPixels(const Image *image,const ssize_t x,
3248% const ssize_t y,const size_t columns,const size_t rows,
3249% ExceptionInfo *exception)
3250%
3251% A description of each parameter follows:
3252%
3253% o image: the image.
3254%
3255% o x,y,columns,rows: These values define the perimeter of a region of
3256% pixels.
3257%
3258% o exception: return any errors or warnings in this structure.
3259%
3260*/
3261MagickExport const Quantum *GetVirtualPixels(const Image *image,
3262 const ssize_t x,const ssize_t y,const size_t columns,const size_t rows,
3263 ExceptionInfo *exception)
3264{
3265 CacheInfo
3266 *magick_restrict cache_info;
3267
3268 const int
3269 id = GetOpenMPThreadId();
3270
3271 const Quantum
3272 *magick_restrict p;
3273
3274 assert(image != (const Image *) NULL);
3275 assert(image->signature == MagickCoreSignature);
3276 assert(image->cache != (Cache) NULL);
3277 cache_info=(CacheInfo *) image->cache;
3278 assert(cache_info->signature == MagickCoreSignature);
3279 if (cache_info->methods.get_virtual_pixel_handler !=
3280 (GetVirtualPixelHandler) NULL)
3281 return(cache_info->methods.get_virtual_pixel_handler(image,
3282 GetPixelCacheVirtualMethod(image),x,y,columns,rows,exception));
3283 assert(id < (int) cache_info->number_threads);
3284 p=GetVirtualPixelCacheNexus(image,GetPixelCacheVirtualMethod(image),x,y,
3285 columns,rows,cache_info->nexus_info[id],exception);
3286 return(p);
3287}
3288
3289/*
3290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3291% %
3292% %
3293% %
3294+ G e t V i r t u a l P i x e l s F r o m C a c h e %
3295% %
3296% %
3297% %
3298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3299%
3300% GetVirtualPixelsCache() returns the pixels associated corresponding with the
3301% last call to QueueAuthenticPixelsCache() or GetVirtualPixelCache().
3302%
3303% The format of the GetVirtualPixelsCache() method is:
3304%
3305% Quantum *GetVirtualPixelsCache(const Image *image)
3306%
3307% A description of each parameter follows:
3308%
3309% o image: the image.
3310%
3311*/
3312static const Quantum *GetVirtualPixelsCache(const Image *image)
3313{
3314 CacheInfo
3315 *magick_restrict cache_info;
3316
3317 const int
3318 id = GetOpenMPThreadId();
3319
3320 assert(image != (const Image *) NULL);
3321 assert(image->signature == MagickCoreSignature);
3322 assert(image->cache != (Cache) NULL);
3323 cache_info=(CacheInfo *) image->cache;
3324 assert(cache_info->signature == MagickCoreSignature);
3325 assert(id < (int) cache_info->number_threads);
3326 return(GetVirtualPixelsNexus(image->cache,cache_info->nexus_info[id]));
3327}
3328
3329/*
3330%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3331% %
3332% %
3333% %
3334+ G e t V i r t u a l P i x e l s N e x u s %
3335% %
3336% %
3337% %
3338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3339%
3340% GetVirtualPixelsNexus() returns the pixels associated with the specified
3341% cache nexus.
3342%
3343% The format of the GetVirtualPixelsNexus() method is:
3344%
3345% const Quantum *GetVirtualPixelsNexus(const Cache cache,
3346% NexusInfo *nexus_info)
3347%
3348% A description of each parameter follows:
3349%
3350% o cache: the pixel cache.
3351%
3352% o nexus_info: the cache nexus to return the colormap pixels.
3353%
3354*/
3355MagickPrivate const Quantum *GetVirtualPixelsNexus(const Cache cache,
3356 NexusInfo *magick_restrict nexus_info)
3357{
3358 CacheInfo
3359 *magick_restrict cache_info;
3360
3361 assert(cache != (Cache) NULL);
3362 cache_info=(CacheInfo *) cache;
3363 assert(cache_info->signature == MagickCoreSignature);
3364 if (cache_info->storage_class == UndefinedClass)
3365 return((Quantum *) NULL);
3366 return((const Quantum *) nexus_info->pixels);
3367}
3368
3369/*
3370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3371% %
3372% %
3373% %
3374+ M a s k P i x e l C a c h e N e x u s %
3375% %
3376% %
3377% %
3378%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3379%
3380% MaskPixelCacheNexus() masks the cache nexus as defined by the composite mask.
3381% The method returns MagickTrue if the pixel region is masked, otherwise
3382% MagickFalse.
3383%
3384% The format of the MaskPixelCacheNexus() method is:
3385%
3386% MagickBooleanType MaskPixelCacheNexus(Image *image,
3387% NexusInfo *nexus_info,ExceptionInfo *exception)
3388%
3389% A description of each parameter follows:
3390%
3391% o image: the image.
3392%
3393% o nexus_info: the cache nexus to clip.
3394%
3395% o exception: return any errors or warnings in this structure.
3396%
3397*/
3398
3399static inline Quantum ApplyPixelCompositeMask(const Quantum p,
3400 const MagickRealType alpha,const Quantum q,const MagickRealType beta)
3401{
3402 double
3403 gamma;
3404
3405 if (fabs((double) (alpha-(double) TransparentAlpha)) < MagickEpsilon)
3406 return(q);
3407 gamma=1.0-QuantumScale*QuantumScale*alpha*beta;
3408 gamma=PerceptibleReciprocal(gamma);
3409 return(ClampToQuantum(gamma*MagickOver_((double) p,alpha,(double) q,beta)));
3410}
3411
3412static MagickBooleanType MaskPixelCacheNexus(Image *image,NexusInfo *nexus_info,
3413 ExceptionInfo *exception)
3414{
3415 CacheInfo
3416 *magick_restrict cache_info;
3417
3418 Quantum
3419 *magick_restrict p,
3420 *magick_restrict q;
3421
3422 ssize_t
3423 y;
3424
3425 /*
3426 Apply composite mask.
3427 */
3428 if (IsEventLogging() != MagickFalse)
3429 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3430 if ((image->channels & CompositeMaskChannel) == 0)
3431 return(MagickTrue);
3432 if ((nexus_info->region.width == 0) || (nexus_info->region.height == 0))
3433 return(MagickTrue);
3434 cache_info=(CacheInfo *) image->cache;
3435 if (cache_info == (Cache) NULL)
3436 return(MagickFalse);
3437 p=GetAuthenticPixelCacheNexus(image,nexus_info->region.x,nexus_info->region.y,
3438 nexus_info->region.width,nexus_info->region.height,
3439 nexus_info->virtual_nexus,exception);
3440 q=nexus_info->pixels;
3441 if ((p == (Quantum *) NULL) || (q == (Quantum *) NULL))
3442 return(MagickFalse);
3443 for (y=0; y < (ssize_t) nexus_info->region.height; y++)
3444 {
3445 ssize_t
3446 x;
3447
3448 for (x=0; x < (ssize_t) nexus_info->region.width; x++)
3449 {
3450 double
3451 alpha;
3452
3453 ssize_t
3454 i;
3455
3456 alpha=(double) GetPixelCompositeMask(image,p);
3457 for (i=0; i < (ssize_t) image->number_channels; i++)
3458 {
3459 PixelChannel channel = GetPixelChannelChannel(image,i);
3460 PixelTrait traits = GetPixelChannelTraits(image,channel);
3461 if ((traits & UpdatePixelTrait) == 0)
3462 continue;
3463 q[i]=ApplyPixelCompositeMask(q[i],alpha,p[i],GetPixelAlpha(image,p));
3464 }
3465 p+=GetPixelChannels(image);
3466 q+=GetPixelChannels(image);
3467 }
3468 }
3469 return(MagickTrue);
3470}
3471
3472/*
3473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3474% %
3475% %
3476% %
3477+ O p e n P i x e l C a c h e %
3478% %
3479% %
3480% %
3481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3482%
3483% OpenPixelCache() allocates the pixel cache. This includes defining the cache
3484% dimensions, allocating space for the image pixels and optionally the
3485% metacontent, and memory mapping the cache if it is disk based. The cache
3486% nexus array is initialized as well.
3487%
3488% The format of the OpenPixelCache() method is:
3489%
3490% MagickBooleanType OpenPixelCache(Image *image,const MapMode mode,
3491% ExceptionInfo *exception)
3492%
3493% A description of each parameter follows:
3494%
3495% o image: the image.
3496%
3497% o mode: ReadMode, WriteMode, or IOMode.
3498%
3499% o exception: return any errors or warnings in this structure.
3500%
3501*/
3502
3503static MagickBooleanType OpenPixelCacheOnDisk(CacheInfo *cache_info,
3504 const MapMode mode)
3505{
3506 int
3507 file;
3508
3509 /*
3510 Open pixel cache on disk.
3511 */
3512 if ((cache_info->file != -1) && (cache_info->disk_mode == mode))
3513 return(MagickTrue); /* cache already open and in the proper mode */
3514 if (*cache_info->cache_filename == '\0')
3515 file=AcquireUniqueFileResource(cache_info->cache_filename);
3516 else
3517 switch (mode)
3518 {
3519 case ReadMode:
3520 {
3521 file=open_utf8(cache_info->cache_filename,O_RDONLY | O_BINARY,0);
3522 break;
3523 }
3524 case WriteMode:
3525 {
3526 file=open_utf8(cache_info->cache_filename,O_WRONLY | O_CREAT |
3527 O_BINARY | O_EXCL,S_MODE);
3528 if (file == -1)
3529 file=open_utf8(cache_info->cache_filename,O_WRONLY | O_BINARY,S_MODE);
3530 break;
3531 }
3532 case IOMode:
3533 default:
3534 {
3535 file=open_utf8(cache_info->cache_filename,O_RDWR | O_CREAT | O_BINARY |
3536 O_EXCL,S_MODE);
3537 if (file == -1)
3538 file=open_utf8(cache_info->cache_filename,O_RDWR | O_BINARY,S_MODE);
3539 break;
3540 }
3541 }
3542 if (file == -1)
3543 return(MagickFalse);
3544 (void) AcquireMagickResource(FileResource,1);
3545 if (cache_info->file != -1)
3546 (void) ClosePixelCacheOnDisk(cache_info);
3547 cache_info->file=file;
3548 cache_info->disk_mode=mode;
3549 return(MagickTrue);
3550}
3551
3552static inline MagickOffsetType WritePixelCacheRegion(
3553 const CacheInfo *magick_restrict cache_info,const MagickOffsetType offset,
3554 const MagickSizeType length,const unsigned char *magick_restrict buffer)
3555{
3556 MagickOffsetType
3557 i;
3558
3559 ssize_t
3560 count = 0;
3561
3562#if !defined(MAGICKCORE_HAVE_PWRITE)
3563 if (lseek(cache_info->file,offset,SEEK_SET) < 0)
3564 return((MagickOffsetType) -1);
3565#endif
3566 for (i=0; i < (MagickOffsetType) length; i+=count)
3567 {
3568#if !defined(MAGICKCORE_HAVE_PWRITE)
3569 count=write(cache_info->file,buffer+i,(size_t) MagickMin(length-
3570 (MagickSizeType) i,MAGICK_SSIZE_MAX));
3571#else
3572 count=pwrite(cache_info->file,buffer+i,(size_t) MagickMin(length-
3573 (MagickSizeType) i,MAGICK_SSIZE_MAX),offset+i);
3574#endif
3575 if (count <= 0)
3576 {
3577 count=0;
3578 if (errno != EINTR)
3579 break;
3580 }
3581 }
3582 return(i);
3583}
3584
3585static MagickBooleanType SetPixelCacheExtent(Image *image,MagickSizeType length)
3586{
3587 CacheInfo
3588 *magick_restrict cache_info;
3589
3590 MagickOffsetType
3591 offset;
3592
3593 cache_info=(CacheInfo *) image->cache;
3594 if (cache_info->debug != MagickFalse)
3595 {
3596 char
3597 format[MagickPathExtent],
3598 message[MagickPathExtent];
3599
3600 (void) FormatMagickSize(length,MagickFalse,"B",MagickPathExtent,format);
3601 (void) FormatLocaleString(message,MagickPathExtent,
3602 "extend %s (%s[%d], disk, %s)",cache_info->filename,
3603 cache_info->cache_filename,cache_info->file,format);
3604 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",message);
3605 }
3606 if (length != (MagickSizeType) ((MagickOffsetType) length))
3607 return(MagickFalse);
3608 offset=(MagickOffsetType) lseek(cache_info->file,0,SEEK_END);
3609 if (offset < 0)
3610 return(MagickFalse);
3611 if ((MagickSizeType) offset < length)
3612 {
3613 MagickOffsetType
3614 count,
3615 extent;
3616
3617 extent=(MagickOffsetType) length-1;
3618 count=WritePixelCacheRegion(cache_info,extent,1,(const unsigned char *)
3619 "");
3620 if (count != 1)
3621 return(MagickFalse);
3622#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
3623 if (cache_info->synchronize != MagickFalse)
3624 if (posix_fallocate(cache_info->file,offset+1,extent-offset) != 0)
3625 return(MagickFalse);
3626#endif
3627 }
3628 offset=(MagickOffsetType) lseek(cache_info->file,0,SEEK_SET);
3629 if (offset < 0)
3630 return(MagickFalse);
3631 return(MagickTrue);
3632}
3633
3634static MagickBooleanType OpenPixelCache(Image *image,const MapMode mode,
3635 ExceptionInfo *exception)
3636{
3637 CacheInfo
3638 *magick_restrict cache_info,
3639 source_info;
3640
3641 char
3642 format[MagickPathExtent],
3643 message[MagickPathExtent];
3644
3645 const char
3646 *hosts,
3647 *type;
3648
3649 MagickBooleanType
3650 status;
3651
3652 MagickSizeType
3653 length,
3654 number_pixels;
3655
3656 size_t
3657 columns,
3658 packet_size;
3659
3660 assert(image != (const Image *) NULL);
3661 assert(image->signature == MagickCoreSignature);
3662 assert(image->cache != (Cache) NULL);
3663 if (IsEventLogging() != MagickFalse)
3664 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3665 if (cache_anonymous_memory < 0)
3666 {
3667 char
3668 *value;
3669
3670 /*
3671 Does the security policy require anonymous mapping for pixel cache?
3672 */
3673 cache_anonymous_memory=0;
3674 value=GetPolicyValue("pixel-cache-memory");
3675 if (value == (char *) NULL)
3676 value=GetPolicyValue("cache:memory-map");
3677 if (LocaleCompare(value,"anonymous") == 0)
3678 {
3679#if defined(MAGICKCORE_HAVE_MMAP) && defined(MAP_ANONYMOUS)
3680 cache_anonymous_memory=1;
3681#else
3682 (void) ThrowMagickException(exception,GetMagickModule(),
3683 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn",
3684 "'%s' (policy requires anonymous memory mapping)",image->filename);
3685#endif
3686 }
3687 value=DestroyString(value);
3688 }
3689 if ((image->columns == 0) || (image->rows == 0))
3690 ThrowBinaryException(CacheError,"NoPixelsDefinedInCache",image->filename);
3691 cache_info=(CacheInfo *) image->cache;
3692 assert(cache_info->signature == MagickCoreSignature);
3693 if (((MagickSizeType) image->columns > cache_info->width_limit) ||
3694 ((MagickSizeType) image->rows > cache_info->height_limit))
3695 ThrowBinaryException(ImageError,"WidthOrHeightExceedsLimit",
3696 image->filename);
3697 if (GetMagickResourceLimit(ListLengthResource) != MagickResourceInfinity)
3698 {
3699 length=GetImageListLength(image);
3700 if (AcquireMagickResource(ListLengthResource,length) == MagickFalse)
3701 ThrowBinaryException(ResourceLimitError,"ListLengthExceedsLimit",
3702 image->filename);
3703 }
3704 source_info=(*cache_info);
3705 source_info.file=(-1);
3706 (void) FormatLocaleString(cache_info->filename,MagickPathExtent,"%s[%.20g]",
3707 image->filename,(double) image->scene);
3708 cache_info->storage_class=image->storage_class;
3709 cache_info->colorspace=image->colorspace;
3710 cache_info->alpha_trait=image->alpha_trait;
3711 cache_info->channels=image->channels;
3712 cache_info->rows=image->rows;
3713 cache_info->columns=image->columns;
3714 status=ResetPixelChannelMap(image,exception);
3715 if (status == MagickFalse)
3716 return(MagickFalse);
3717 cache_info->number_channels=GetPixelChannels(image);
3718 (void) memcpy(cache_info->channel_map,image->channel_map,MaxPixelChannels*
3719 sizeof(*image->channel_map));
3720 cache_info->metacontent_extent=image->metacontent_extent;
3721 cache_info->mode=mode;
3722 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
3723 packet_size=MagickMax(cache_info->number_channels,1)*sizeof(Quantum);
3724 if (image->metacontent_extent != 0)
3725 packet_size+=cache_info->metacontent_extent;
3726 length=number_pixels*packet_size;
3727 columns=(size_t) (length/cache_info->rows/packet_size);
3728 if ((cache_info->columns != columns) || ((ssize_t) cache_info->columns < 0) ||
3729 ((ssize_t) cache_info->rows < 0))
3730 ThrowBinaryException(ResourceLimitError,"PixelCacheAllocationFailed",
3731 image->filename);
3732 cache_info->length=length;
3733 if (image->ping != MagickFalse)
3734 {
3735 cache_info->type=PingCache;
3736 return(MagickTrue);
3737 }
3738 status=AcquireMagickResource(AreaResource,(MagickSizeType)
3739 cache_info->columns*cache_info->rows);
3740 if (cache_info->mode == PersistMode)
3741 status=MagickFalse;
3742 length=number_pixels*(cache_info->number_channels*sizeof(Quantum)+
3743 cache_info->metacontent_extent);
3744 if ((status != MagickFalse) &&
3745 (length == (MagickSizeType) ((size_t) length)) &&
3746 ((cache_info->type == UndefinedCache) ||
3747 (cache_info->type == MemoryCache)))
3748 {
3749 status=AcquireMagickResource(MemoryResource,cache_info->length);
3750 if (status != MagickFalse)
3751 {
3752 status=MagickTrue;
3753 if (cache_anonymous_memory <= 0)
3754 {
3755 cache_info->mapped=MagickFalse;
3756 cache_info->pixels=(Quantum *) MagickAssumeAligned(
3757 AcquireAlignedMemory(1,(size_t) cache_info->length));
3758 }
3759 else
3760 {
3761 cache_info->mapped=MagickTrue;
3762 cache_info->pixels=(Quantum *) MapBlob(-1,IOMode,0,(size_t)
3763 cache_info->length);
3764 }
3765 if (cache_info->pixels == (Quantum *) NULL)
3766 {
3767 cache_info->mapped=source_info.mapped;
3768 cache_info->pixels=source_info.pixels;
3769 }
3770 else
3771 {
3772 /*
3773 Create memory pixel cache.
3774 */
3775 cache_info->type=MemoryCache;
3776 cache_info->metacontent=(void *) NULL;
3777 if (cache_info->metacontent_extent != 0)
3778 cache_info->metacontent=(void *) (cache_info->pixels+
3779 cache_info->number_channels*number_pixels);
3780 if ((source_info.storage_class != UndefinedClass) &&
3781 (mode != ReadMode))
3782 {
3783 status=ClonePixelCacheRepository(cache_info,&source_info,
3784 exception);
3785 RelinquishPixelCachePixels(&source_info);
3786 }
3787 if (cache_info->debug != MagickFalse)
3788 {
3789 (void) FormatMagickSize(cache_info->length,MagickTrue,"B",
3790 MagickPathExtent,format);
3791 type=CommandOptionToMnemonic(MagickCacheOptions,(ssize_t)
3792 cache_info->type);
3793 (void) FormatLocaleString(message,MagickPathExtent,
3794 "open %s (%s %s, %.20gx%.20gx%.20g %s)",
3795 cache_info->filename,cache_info->mapped != MagickFalse ?
3796 "Anonymous" : "Heap",type,(double) cache_info->columns,
3797 (double) cache_info->rows,(double)
3798 cache_info->number_channels,format);
3799 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",
3800 message);
3801 }
3802 cache_info->storage_class=image->storage_class;
3803 if (status == 0)
3804 {
3805 cache_info->type=UndefinedCache;
3806 return(MagickFalse);
3807 }
3808 return(MagickTrue);
3809 }
3810 }
3811 }
3812 status=AcquireMagickResource(DiskResource,cache_info->length);
3813 hosts=(const char *) GetImageRegistry(StringRegistryType,"cache:hosts",
3814 exception);
3815 if ((status == MagickFalse) && (hosts != (const char *) NULL))
3816 {
3818 *server_info;
3819
3820 /*
3821 Distribute the pixel cache to a remote server.
3822 */
3823 server_info=AcquireDistributeCacheInfo(exception);
3824 if (server_info != (DistributeCacheInfo *) NULL)
3825 {
3826 status=OpenDistributePixelCache(server_info,image);
3827 if (status == MagickFalse)
3828 {
3829 ThrowFileException(exception,CacheError,"UnableToOpenPixelCache",
3830 GetDistributeCacheHostname(server_info));
3831 server_info=DestroyDistributeCacheInfo(server_info);
3832 }
3833 else
3834 {
3835 /*
3836 Create a distributed pixel cache.
3837 */
3838 status=MagickTrue;
3839 cache_info->type=DistributedCache;
3840 cache_info->server_info=server_info;
3841 (void) FormatLocaleString(cache_info->cache_filename,
3842 MagickPathExtent,"%s:%d",GetDistributeCacheHostname(
3843 (DistributeCacheInfo *) cache_info->server_info),
3844 GetDistributeCachePort((DistributeCacheInfo *)
3845 cache_info->server_info));
3846 if ((source_info.storage_class != UndefinedClass) &&
3847 (mode != ReadMode))
3848 {
3849 status=ClonePixelCacheRepository(cache_info,&source_info,
3850 exception);
3851 RelinquishPixelCachePixels(&source_info);
3852 }
3853 if (cache_info->debug != MagickFalse)
3854 {
3855 (void) FormatMagickSize(cache_info->length,MagickFalse,"B",
3856 MagickPathExtent,format);
3857 type=CommandOptionToMnemonic(MagickCacheOptions,(ssize_t)
3858 cache_info->type);
3859 (void) FormatLocaleString(message,MagickPathExtent,
3860 "open %s (%s[%d], %s, %.20gx%.20gx%.20g %s)",
3861 cache_info->filename,cache_info->cache_filename,
3862 GetDistributeCacheFile((DistributeCacheInfo *)
3863 cache_info->server_info),type,(double) cache_info->columns,
3864 (double) cache_info->rows,(double)
3865 cache_info->number_channels,format);
3866 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",
3867 message);
3868 }
3869 if (status == 0)
3870 {
3871 cache_info->type=UndefinedCache;
3872 return(MagickFalse);
3873 }
3874 return(MagickTrue);
3875 }
3876 }
3877 cache_info->type=UndefinedCache;
3878 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
3879 "CacheResourcesExhausted","`%s'",image->filename);
3880 return(MagickFalse);
3881 }
3882 /*
3883 Create pixel cache on disk.
3884 */
3885 if (status == MagickFalse)
3886 {
3887 cache_info->type=UndefinedCache;
3888 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
3889 "CacheResourcesExhausted","`%s'",image->filename);
3890 return(MagickFalse);
3891 }
3892 if ((source_info.storage_class != UndefinedClass) && (mode != ReadMode) &&
3893 (cache_info->mode != PersistMode))
3894 {
3895 (void) ClosePixelCacheOnDisk(cache_info);
3896 *cache_info->cache_filename='\0';
3897 }
3898 if (OpenPixelCacheOnDisk(cache_info,mode) == MagickFalse)
3899 {
3900 cache_info->type=UndefinedCache;
3901 ThrowFileException(exception,CacheError,"UnableToOpenPixelCache",
3902 image->filename);
3903 return(MagickFalse);
3904 }
3905 status=SetPixelCacheExtent(image,(MagickSizeType) cache_info->offset+
3906 cache_info->length);
3907 if (status == MagickFalse)
3908 {
3909 cache_info->type=UndefinedCache;
3910 ThrowFileException(exception,CacheError,"UnableToExtendCache",
3911 image->filename);
3912 return(MagickFalse);
3913 }
3914 cache_info->type=DiskCache;
3915 length=number_pixels*(cache_info->number_channels*sizeof(Quantum)+
3916 cache_info->metacontent_extent);
3917 if (length == (MagickSizeType) ((size_t) length))
3918 {
3919 status=AcquireMagickResource(MapResource,cache_info->length);
3920 if (status != MagickFalse)
3921 {
3922 cache_info->pixels=(Quantum *) MapBlob(cache_info->file,mode,
3923 cache_info->offset,(size_t) cache_info->length);
3924 if (cache_info->pixels == (Quantum *) NULL)
3925 {
3926 cache_info->mapped=source_info.mapped;
3927 cache_info->pixels=source_info.pixels;
3928 RelinquishMagickResource(MapResource,cache_info->length);
3929 }
3930 else
3931 {
3932 /*
3933 Create file-backed memory-mapped pixel cache.
3934 */
3935 (void) ClosePixelCacheOnDisk(cache_info);
3936 cache_info->type=MapCache;
3937 cache_info->mapped=MagickTrue;
3938 cache_info->metacontent=(void *) NULL;
3939 if (cache_info->metacontent_extent != 0)
3940 cache_info->metacontent=(void *) (cache_info->pixels+
3941 cache_info->number_channels*number_pixels);
3942 if ((source_info.storage_class != UndefinedClass) &&
3943 (mode != ReadMode))
3944 {
3945 status=ClonePixelCacheRepository(cache_info,&source_info,
3946 exception);
3947 RelinquishPixelCachePixels(&source_info);
3948 }
3949 if (cache_info->debug != MagickFalse)
3950 {
3951 (void) FormatMagickSize(cache_info->length,MagickTrue,"B",
3952 MagickPathExtent,format);
3953 type=CommandOptionToMnemonic(MagickCacheOptions,(ssize_t)
3954 cache_info->type);
3955 (void) FormatLocaleString(message,MagickPathExtent,
3956 "open %s (%s[%d], %s, %.20gx%.20gx%.20g %s)",
3957 cache_info->filename,cache_info->cache_filename,
3958 cache_info->file,type,(double) cache_info->columns,
3959 (double) cache_info->rows,(double)
3960 cache_info->number_channels,format);
3961 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",
3962 message);
3963 }
3964 if (status == 0)
3965 {
3966 cache_info->type=UndefinedCache;
3967 return(MagickFalse);
3968 }
3969 return(MagickTrue);
3970 }
3971 }
3972 }
3973 status=MagickTrue;
3974 if ((source_info.storage_class != UndefinedClass) && (mode != ReadMode))
3975 {
3976 status=ClonePixelCacheRepository(cache_info,&source_info,exception);
3977 RelinquishPixelCachePixels(&source_info);
3978 }
3979 if (cache_info->debug != MagickFalse)
3980 {
3981 (void) FormatMagickSize(cache_info->length,MagickFalse,"B",
3982 MagickPathExtent,format);
3983 type=CommandOptionToMnemonic(MagickCacheOptions,(ssize_t)
3984 cache_info->type);
3985 (void) FormatLocaleString(message,MagickPathExtent,
3986 "open %s (%s[%d], %s, %.20gx%.20gx%.20g %s)",cache_info->filename,
3987 cache_info->cache_filename,cache_info->file,type,(double)
3988 cache_info->columns,(double) cache_info->rows,(double)
3989 cache_info->number_channels,format);
3990 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",message);
3991 }
3992 if (status == 0)
3993 {
3994 cache_info->type=UndefinedCache;
3995 return(MagickFalse);
3996 }
3997 return(MagickTrue);
3998}
3999
4000/*
4001%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4002% %
4003% %
4004% %
4005+ P e r s i s t P i x e l C a c h e %
4006% %
4007% %
4008% %
4009%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4010%
4011% PersistPixelCache() attaches to or initializes a persistent pixel cache. A
4012% persistent pixel cache is one that resides on disk and is not destroyed
4013% when the program exits.
4014%
4015% The format of the PersistPixelCache() method is:
4016%
4017% MagickBooleanType PersistPixelCache(Image *image,const char *filename,
4018% const MagickBooleanType attach,MagickOffsetType *offset,
4019% ExceptionInfo *exception)
4020%
4021% A description of each parameter follows:
4022%
4023% o image: the image.
4024%
4025% o filename: the persistent pixel cache filename.
4026%
4027% o attach: A value other than zero initializes the persistent pixel cache.
4028%
4029% o initialize: A value other than zero initializes the persistent pixel
4030% cache.
4031%
4032% o offset: the offset in the persistent cache to store pixels.
4033%
4034% o exception: return any errors or warnings in this structure.
4035%
4036*/
4037MagickExport MagickBooleanType PersistPixelCache(Image *image,
4038 const char *filename,const MagickBooleanType attach,MagickOffsetType *offset,
4039 ExceptionInfo *exception)
4040{
4041 CacheInfo
4042 *magick_restrict cache_info,
4043 *magick_restrict clone_info;
4044
4045 MagickBooleanType
4046 status;
4047
4048 ssize_t
4049 page_size;
4050
4051 assert(image != (Image *) NULL);
4052 assert(image->signature == MagickCoreSignature);
4053 if (IsEventLogging() != MagickFalse)
4054 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4055 assert(image->cache != (void *) NULL);
4056 assert(filename != (const char *) NULL);
4057 assert(offset != (MagickOffsetType *) NULL);
4058 page_size=GetMagickPageSize();
4059 cache_info=(CacheInfo *) image->cache;
4060 assert(cache_info->signature == MagickCoreSignature);
4061#if defined(MAGICKCORE_OPENCL_SUPPORT)
4062 CopyOpenCLBuffer(cache_info);
4063#endif
4064 if (attach != MagickFalse)
4065 {
4066 /*
4067 Attach existing persistent pixel cache.
4068 */
4069 if (cache_info->debug != MagickFalse)
4070 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
4071 "attach persistent cache");
4072 (void) CopyMagickString(cache_info->cache_filename,filename,
4073 MagickPathExtent);
4074 cache_info->type=MapCache;
4075 cache_info->offset=(*offset);
4076 if (OpenPixelCache(image,ReadMode,exception) == MagickFalse)
4077 return(MagickFalse);
4078 *offset=(*offset+(MagickOffsetType) cache_info->length+page_size-
4079 ((MagickOffsetType) cache_info->length % page_size));
4080 return(MagickTrue);
4081 }
4082 /*
4083 Clone persistent pixel cache.
4084 */
4085 status=AcquireMagickResource(DiskResource,cache_info->length);
4086 if (status == MagickFalse)
4087 {
4088 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
4089 "CacheResourcesExhausted","`%s'",image->filename);
4090 return(MagickFalse);
4091 }
4092 clone_info=(CacheInfo *) ClonePixelCache(cache_info);
4093 clone_info->type=DiskCache;
4094 (void) CopyMagickString(clone_info->cache_filename,filename,MagickPathExtent);
4095 clone_info->file=(-1);
4096 clone_info->storage_class=cache_info->storage_class;
4097 clone_info->colorspace=cache_info->colorspace;
4098 clone_info->alpha_trait=cache_info->alpha_trait;
4099 clone_info->channels=cache_info->channels;
4100 clone_info->columns=cache_info->columns;
4101 clone_info->rows=cache_info->rows;
4102 clone_info->number_channels=cache_info->number_channels;
4103 clone_info->metacontent_extent=cache_info->metacontent_extent;
4104 clone_info->mode=PersistMode;
4105 clone_info->length=cache_info->length;
4106 (void) memcpy(clone_info->channel_map,cache_info->channel_map,
4107 MaxPixelChannels*sizeof(*cache_info->channel_map));
4108 clone_info->offset=(*offset);
4109 status=OpenPixelCacheOnDisk(clone_info,WriteMode);
4110 if (status != MagickFalse)
4111 status=ClonePixelCacheRepository(clone_info,cache_info,exception);
4112 *offset=(*offset+(MagickOffsetType) cache_info->length+page_size-
4113 ((MagickOffsetType) cache_info->length % page_size));
4114 clone_info=(CacheInfo *) DestroyPixelCache(clone_info);
4115 return(status);
4116}
4117
4118/*
4119%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4120% %
4121% %
4122% %
4123+ Q u e u e A u t h e n t i c P i x e l C a c h e N e x u s %
4124% %
4125% %
4126% %
4127%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4128%
4129% QueueAuthenticPixelCacheNexus() allocates an region to store image pixels as
4130% defined by the region rectangle and returns a pointer to the region. This
4131% region is subsequently transferred from the pixel cache with
4132% SyncAuthenticPixelsCache(). A pointer to the pixels is returned if the
4133% pixels are transferred, otherwise a NULL is returned.
4134%
4135% The format of the QueueAuthenticPixelCacheNexus() method is:
4136%
4137% Quantum *QueueAuthenticPixelCacheNexus(Image *image,const ssize_t x,
4138% const ssize_t y,const size_t columns,const size_t rows,
4139% const MagickBooleanType clone,NexusInfo *nexus_info,
4140% ExceptionInfo *exception)
4141%
4142% A description of each parameter follows:
4143%
4144% o image: the image.
4145%
4146% o x,y,columns,rows: These values define the perimeter of a region of
4147% pixels.
4148%
4149% o nexus_info: the cache nexus to set.
4150%
4151% o clone: clone the pixel cache.
4152%
4153% o exception: return any errors or warnings in this structure.
4154%
4155*/
4156MagickPrivate Quantum *QueueAuthenticPixelCacheNexus(Image *image,
4157 const ssize_t x,const ssize_t y,const size_t columns,const size_t rows,
4158 const MagickBooleanType clone,NexusInfo *nexus_info,ExceptionInfo *exception)
4159{
4160 CacheInfo
4161 *magick_restrict cache_info;
4162
4163 MagickOffsetType
4164 offset;
4165
4166 MagickSizeType
4167 number_pixels;
4168
4169 Quantum
4170 *magick_restrict pixels;
4171
4172 /*
4173 Validate pixel cache geometry.
4174 */
4175 assert(image != (const Image *) NULL);
4176 assert(image->signature == MagickCoreSignature);
4177 assert(image->cache != (Cache) NULL);
4178 cache_info=(CacheInfo *) GetImagePixelCache(image,clone,exception);
4179 if (cache_info == (Cache) NULL)
4180 return((Quantum *) NULL);
4181 assert(cache_info->signature == MagickCoreSignature);
4182 if ((cache_info->columns == 0) || (cache_info->rows == 0) || (x < 0) ||
4183 (y < 0) || (x >= (ssize_t) cache_info->columns) ||
4184 (y >= (ssize_t) cache_info->rows))
4185 {
4186 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
4187 "PixelsAreNotAuthentic","`%s'",image->filename);
4188 return((Quantum *) NULL);
4189 }
4190 if (IsValidPixelOffset(y,cache_info->columns) == MagickFalse)
4191 return((Quantum *) NULL);
4192 offset=y*(MagickOffsetType) cache_info->columns+x;
4193 if (offset < 0)
4194 return((Quantum *) NULL);
4195 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
4196 offset+=((MagickOffsetType) rows-1)*(MagickOffsetType) cache_info->columns+
4197 (MagickOffsetType) columns-1;
4198 if ((MagickSizeType) offset >= number_pixels)
4199 return((Quantum *) NULL);
4200 /*
4201 Return pixel cache.
4202 */
4203 pixels=SetPixelCacheNexusPixels(cache_info,WriteMode,x,y,columns,rows,
4204 ((image->channels & WriteMaskChannel) != 0) ||
4205 ((image->channels & CompositeMaskChannel) != 0) ? MagickTrue : MagickFalse,
4206 nexus_info,exception);
4207 return(pixels);
4208}
4209
4210/*
4211%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4212% %
4213% %
4214% %
4215+ Q u e u e A u t h e n t i c P i x e l s C a c h e %
4216% %
4217% %
4218% %
4219%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4220%
4221% QueueAuthenticPixelsCache() allocates an region to store image pixels as
4222% defined by the region rectangle and returns a pointer to the region. This
4223% region is subsequently transferred from the pixel cache with
4224% SyncAuthenticPixelsCache(). A pointer to the pixels is returned if the
4225% pixels are transferred, otherwise a NULL is returned.
4226%
4227% The format of the QueueAuthenticPixelsCache() method is:
4228%
4229% Quantum *QueueAuthenticPixelsCache(Image *image,const ssize_t x,
4230% const ssize_t y,const size_t columns,const size_t rows,
4231% ExceptionInfo *exception)
4232%
4233% A description of each parameter follows:
4234%
4235% o image: the image.
4236%
4237% o x,y,columns,rows: These values define the perimeter of a region of
4238% pixels.
4239%
4240% o exception: return any errors or warnings in this structure.
4241%
4242*/
4243static Quantum *QueueAuthenticPixelsCache(Image *image,const ssize_t x,
4244 const ssize_t y,const size_t columns,const size_t rows,
4245 ExceptionInfo *exception)
4246{
4247 CacheInfo
4248 *magick_restrict cache_info;
4249
4250 const int
4251 id = GetOpenMPThreadId();
4252
4253 Quantum
4254 *magick_restrict pixels;
4255
4256 assert(image != (const Image *) NULL);
4257 assert(image->signature == MagickCoreSignature);
4258 assert(image->cache != (Cache) NULL);
4259 cache_info=(CacheInfo *) image->cache;
4260 assert(cache_info->signature == MagickCoreSignature);
4261 assert(id < (int) cache_info->number_threads);
4262 pixels=QueueAuthenticPixelCacheNexus(image,x,y,columns,rows,MagickFalse,
4263 cache_info->nexus_info[id],exception);
4264 return(pixels);
4265}
4266
4267/*
4268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4269% %
4270% %
4271% %
4272% Q u e u e A u t h e n t i c P i x e l s %
4273% %
4274% %
4275% %
4276%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4277%
4278% QueueAuthenticPixels() queues a mutable pixel region. If the region is
4279% successfully initialized a pointer to a Quantum array representing the
4280% region is returned, otherwise NULL is returned. The returned pointer may
4281% point to a temporary working buffer for the pixels or it may point to the
4282% final location of the pixels in memory.
4283%
4284% Write-only access means that any existing pixel values corresponding to
4285% the region are ignored. This is useful if the initial image is being
4286% created from scratch, or if the existing pixel values are to be
4287% completely replaced without need to refer to their preexisting values.
4288% The application is free to read and write the pixel buffer returned by
4289% QueueAuthenticPixels() any way it pleases. QueueAuthenticPixels() does not
4290% initialize the pixel array values. Initializing pixel array values is the
4291% application's responsibility.
4292%
4293% Performance is maximized if the selected region is part of one row, or
4294% one or more full rows, since then there is opportunity to access the
4295% pixels in-place (without a copy) if the image is in memory, or in a
4296% memory-mapped file. The returned pointer must *never* be deallocated
4297% by the user.
4298%
4299% Pixels accessed via the returned pointer represent a simple array of type
4300% Quantum. If the image type is CMYK or the storage class is PseudoClass,
4301% call GetAuthenticMetacontent() after invoking GetAuthenticPixels() to
4302% obtain the meta-content (of type void) corresponding to the region.
4303% Once the Quantum (and/or Quantum) array has been updated, the
4304% changes must be saved back to the underlying image using
4305% SyncAuthenticPixels() or they may be lost.
4306%
4307% The format of the QueueAuthenticPixels() method is:
4308%
4309% Quantum *QueueAuthenticPixels(Image *image,const ssize_t x,
4310% const ssize_t y,const size_t columns,const size_t rows,
4311% ExceptionInfo *exception)
4312%
4313% A description of each parameter follows:
4314%
4315% o image: the image.
4316%
4317% o x,y,columns,rows: These values define the perimeter of a region of
4318% pixels.
4319%
4320% o exception: return any errors or warnings in this structure.
4321%
4322*/
4323MagickExport Quantum *QueueAuthenticPixels(Image *image,const ssize_t x,
4324 const ssize_t y,const size_t columns,const size_t rows,
4325 ExceptionInfo *exception)
4326{
4327 CacheInfo
4328 *magick_restrict cache_info;
4329
4330 const int
4331 id = GetOpenMPThreadId();
4332
4333 Quantum
4334 *magick_restrict pixels;
4335
4336 assert(image != (Image *) NULL);
4337 assert(image->signature == MagickCoreSignature);
4338 assert(image->cache != (Cache) NULL);
4339 cache_info=(CacheInfo *) image->cache;
4340 assert(cache_info->signature == MagickCoreSignature);
4341 if (cache_info->methods.queue_authentic_pixels_handler !=
4342 (QueueAuthenticPixelsHandler) NULL)
4343 {
4344 pixels=cache_info->methods.queue_authentic_pixels_handler(image,x,y,
4345 columns,rows,exception);
4346 return(pixels);
4347 }
4348 assert(id < (int) cache_info->number_threads);
4349 pixels=QueueAuthenticPixelCacheNexus(image,x,y,columns,rows,MagickFalse,
4350 cache_info->nexus_info[id],exception);
4351 return(pixels);
4352}
4353
4354/*
4355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4356% %
4357% %
4358% %
4359+ R e a d P i x e l C a c h e M e t a c o n t e n t %
4360% %
4361% %
4362% %
4363%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4364%
4365% ReadPixelCacheMetacontent() reads metacontent from the specified region of
4366% the pixel cache.
4367%
4368% The format of the ReadPixelCacheMetacontent() method is:
4369%
4370% MagickBooleanType ReadPixelCacheMetacontent(CacheInfo *cache_info,
4371% NexusInfo *nexus_info,ExceptionInfo *exception)
4372%
4373% A description of each parameter follows:
4374%
4375% o cache_info: the pixel cache.
4376%
4377% o nexus_info: the cache nexus to read the metacontent.
4378%
4379% o exception: return any errors or warnings in this structure.
4380%
4381*/
4382
4383static inline MagickOffsetType ReadPixelCacheRegion(
4384 const CacheInfo *magick_restrict cache_info,const MagickOffsetType offset,
4385 const MagickSizeType length,unsigned char *magick_restrict buffer)
4386{
4387 MagickOffsetType
4388 i;
4389
4390 ssize_t
4391 count = 0;
4392
4393#if !defined(MAGICKCORE_HAVE_PREAD)
4394 if (lseek(cache_info->file,offset,SEEK_SET) < 0)
4395 return((MagickOffsetType) -1);
4396#endif
4397 for (i=0; i < (MagickOffsetType) length; i+=count)
4398 {
4399#if !defined(MAGICKCORE_HAVE_PREAD)
4400 count=read(cache_info->file,buffer+i,(size_t) MagickMin(length-
4401 (MagickSizeType) i,(size_t) MAGICK_SSIZE_MAX));
4402#else
4403 count=pread(cache_info->file,buffer+i,(size_t) MagickMin(length-
4404 (MagickSizeType) i,(size_t) MAGICK_SSIZE_MAX),offset+i);
4405#endif
4406 if (count <= 0)
4407 {
4408 count=0;
4409 if (errno != EINTR)
4410 break;
4411 }
4412 }
4413 return(i);
4414}
4415
4416static MagickBooleanType ReadPixelCacheMetacontent(
4417 CacheInfo *magick_restrict cache_info,NexusInfo *magick_restrict nexus_info,
4418 ExceptionInfo *exception)
4419{
4420 MagickOffsetType
4421 count,
4422 offset;
4423
4424 MagickSizeType
4425 extent,
4426 length;
4427
4428 ssize_t
4429 y;
4430
4431 unsigned char
4432 *magick_restrict q;
4433
4434 size_t
4435 rows;
4436
4437 if (cache_info->metacontent_extent == 0)
4438 return(MagickFalse);
4439 if (nexus_info->authentic_pixel_cache != MagickFalse)
4440 return(MagickTrue);
4441 if (IsValidPixelOffset(nexus_info->region.y,cache_info->columns) == MagickFalse)
4442 return(MagickFalse);
4443 offset=nexus_info->region.y*(MagickOffsetType) cache_info->columns+
4444 nexus_info->region.x;
4445 length=(MagickSizeType) nexus_info->region.width*
4446 cache_info->metacontent_extent;
4447 extent=length*nexus_info->region.height;
4448 rows=nexus_info->region.height;
4449 y=0;
4450 q=(unsigned char *) nexus_info->metacontent;
4451 switch (cache_info->type)
4452 {
4453 case MemoryCache:
4454 case MapCache:
4455 {
4456 unsigned char
4457 *magick_restrict p;
4458
4459 /*
4460 Read meta-content from memory.
4461 */
4462 if ((cache_info->columns == nexus_info->region.width) &&
4463 (extent == (MagickSizeType) ((size_t) extent)))
4464 {
4465 length=extent;
4466 rows=1UL;
4467 }
4468 p=(unsigned char *) cache_info->metacontent+offset*(MagickOffsetType)
4469 cache_info->metacontent_extent;
4470 for (y=0; y < (ssize_t) rows; y++)
4471 {
4472 (void) memcpy(q,p,(size_t) length);
4473 p+=cache_info->metacontent_extent*cache_info->columns;
4474 q+=cache_info->metacontent_extent*nexus_info->region.width;
4475 }
4476 break;
4477 }
4478 case DiskCache:
4479 {
4480 /*
4481 Read meta content from disk.
4482 */
4483 LockSemaphoreInfo(cache_info->file_semaphore);
4484 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
4485 {
4486 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
4487 cache_info->cache_filename);
4488 UnlockSemaphoreInfo(cache_info->file_semaphore);
4489 return(MagickFalse);
4490 }
4491 if ((cache_info->columns == nexus_info->region.width) &&
4492 (extent <= MagickMaxBufferExtent))
4493 {
4494 length=extent;
4495 rows=1UL;
4496 }
4497 extent=(MagickSizeType) cache_info->columns*cache_info->rows;
4498 for (y=0; y < (ssize_t) rows; y++)
4499 {
4500 count=ReadPixelCacheRegion(cache_info,cache_info->offset+
4501 (MagickOffsetType) extent*(MagickOffsetType)
4502 cache_info->number_channels*(MagickOffsetType) sizeof(Quantum)+offset*
4503 (MagickOffsetType) cache_info->metacontent_extent,length,
4504 (unsigned char *) q);
4505 if (count != (MagickOffsetType) length)
4506 break;
4507 offset+=(MagickOffsetType) cache_info->columns;
4508 q+=cache_info->metacontent_extent*nexus_info->region.width;
4509 }
4510 if (IsFileDescriptorLimitExceeded() != MagickFalse)
4511 (void) ClosePixelCacheOnDisk(cache_info);
4512 UnlockSemaphoreInfo(cache_info->file_semaphore);
4513 break;
4514 }
4515 case DistributedCache:
4516 {
4518 region;
4519
4520 /*
4521 Read metacontent from distributed cache.
4522 */
4523 LockSemaphoreInfo(cache_info->file_semaphore);
4524 region=nexus_info->region;
4525 if ((cache_info->columns != nexus_info->region.width) ||
4526 (extent > MagickMaxBufferExtent))
4527 region.height=1UL;
4528 else
4529 {
4530 length=extent;
4531 rows=1UL;
4532 }
4533 for (y=0; y < (ssize_t) rows; y++)
4534 {
4535 count=ReadDistributePixelCacheMetacontent((DistributeCacheInfo *)
4536 cache_info->server_info,&region,length,(unsigned char *) q);
4537 if (count != (MagickOffsetType) length)
4538 break;
4539 q+=cache_info->metacontent_extent*nexus_info->region.width;
4540 region.y++;
4541 }
4542 UnlockSemaphoreInfo(cache_info->file_semaphore);
4543 break;
4544 }
4545 default:
4546 break;
4547 }
4548 if (y < (ssize_t) rows)
4549 {
4550 ThrowFileException(exception,CacheError,"UnableToReadPixelCache",
4551 cache_info->cache_filename);
4552 return(MagickFalse);
4553 }
4554 if ((cache_info->debug != MagickFalse) &&
4555 (CacheTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
4556 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
4557 "%s[%.20gx%.20g%+.20g%+.20g]",cache_info->filename,(double)
4558 nexus_info->region.width,(double) nexus_info->region.height,(double)
4559 nexus_info->region.x,(double) nexus_info->region.y);
4560 return(MagickTrue);
4561}
4562
4563/*
4564%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4565% %
4566% %
4567% %
4568+ R e a d P i x e l C a c h e P i x e l s %
4569% %
4570% %
4571% %
4572%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4573%
4574% ReadPixelCachePixels() reads pixels from the specified region of the pixel
4575% cache.
4576%
4577% The format of the ReadPixelCachePixels() method is:
4578%
4579% MagickBooleanType ReadPixelCachePixels(CacheInfo *cache_info,
4580% NexusInfo *nexus_info,ExceptionInfo *exception)
4581%
4582% A description of each parameter follows:
4583%
4584% o cache_info: the pixel cache.
4585%
4586% o nexus_info: the cache nexus to read the pixels.
4587%
4588% o exception: return any errors or warnings in this structure.
4589%
4590*/
4591static MagickBooleanType ReadPixelCachePixels(
4592 CacheInfo *magick_restrict cache_info,NexusInfo *magick_restrict nexus_info,
4593 ExceptionInfo *exception)
4594{
4595 MagickOffsetType
4596 count,
4597 offset;
4598
4599 MagickSizeType
4600 extent,
4601 length;
4602
4603 Quantum
4604 *magick_restrict q;
4605
4606 size_t
4607 number_channels,
4608 rows;
4609
4610 ssize_t
4611 y;
4612
4613 if (nexus_info->authentic_pixel_cache != MagickFalse)
4614 return(MagickTrue);
4615 if (IsValidPixelOffset(nexus_info->region.y,cache_info->columns) == MagickFalse)
4616 return(MagickFalse);
4617 offset=nexus_info->region.y*(MagickOffsetType) cache_info->columns;
4618 if ((offset/(MagickOffsetType) cache_info->columns) != nexus_info->region.y)
4619 return(MagickFalse);
4620 offset+=nexus_info->region.x;
4621 number_channels=cache_info->number_channels;
4622 length=(MagickSizeType) number_channels*nexus_info->region.width*
4623 sizeof(Quantum);
4624 if ((length/number_channels/sizeof(Quantum)) != nexus_info->region.width)
4625 return(MagickFalse);
4626 rows=nexus_info->region.height;
4627 extent=length*rows;
4628 if ((extent == 0) || ((extent/length) != rows))
4629 return(MagickFalse);
4630 y=0;
4631 q=nexus_info->pixels;
4632 switch (cache_info->type)
4633 {
4634 case MemoryCache:
4635 case MapCache:
4636 {
4637 Quantum
4638 *magick_restrict p;
4639
4640 /*
4641 Read pixels from memory.
4642 */
4643 if ((cache_info->columns == nexus_info->region.width) &&
4644 (extent == (MagickSizeType) ((size_t) extent)))
4645 {
4646 length=extent;
4647 rows=1UL;
4648 }
4649 p=cache_info->pixels+(MagickOffsetType) cache_info->number_channels*
4650 offset;
4651 for (y=0; y < (ssize_t) rows; y++)
4652 {
4653 (void) memcpy(q,p,(size_t) length);
4654 p+=cache_info->number_channels*cache_info->columns;
4655 q+=cache_info->number_channels*nexus_info->region.width;
4656 }
4657 break;
4658 }
4659 case DiskCache:
4660 {
4661 /*
4662 Read pixels from disk.
4663 */
4664 LockSemaphoreInfo(cache_info->file_semaphore);
4665 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
4666 {
4667 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
4668 cache_info->cache_filename);
4669 UnlockSemaphoreInfo(cache_info->file_semaphore);
4670 return(MagickFalse);
4671 }
4672 if ((cache_info->columns == nexus_info->region.width) &&
4673 (extent <= MagickMaxBufferExtent))
4674 {
4675 length=extent;
4676 rows=1UL;
4677 }
4678 for (y=0; y < (ssize_t) rows; y++)
4679 {
4680 count=ReadPixelCacheRegion(cache_info,cache_info->offset+offset*
4681 (MagickOffsetType) cache_info->number_channels*(MagickOffsetType)
4682 sizeof(*q),length,(unsigned char *) q);
4683 if (count != (MagickOffsetType) length)
4684 break;
4685 offset+=(MagickOffsetType) cache_info->columns;
4686 q+=cache_info->number_channels*nexus_info->region.width;
4687 }
4688 if (IsFileDescriptorLimitExceeded() != MagickFalse)
4689 (void) ClosePixelCacheOnDisk(cache_info);
4690 UnlockSemaphoreInfo(cache_info->file_semaphore);
4691 break;
4692 }
4693 case DistributedCache:
4694 {
4696 region;
4697
4698 /*
4699 Read pixels from distributed cache.
4700 */
4701 LockSemaphoreInfo(cache_info->file_semaphore);
4702 region=nexus_info->region;
4703 if ((cache_info->columns != nexus_info->region.width) ||
4704 (extent > MagickMaxBufferExtent))
4705 region.height=1UL;
4706 else
4707 {
4708 length=extent;
4709 rows=1UL;
4710 }
4711 for (y=0; y < (ssize_t) rows; y++)
4712 {
4713 count=ReadDistributePixelCachePixels((DistributeCacheInfo *)
4714 cache_info->server_info,&region,length,(unsigned char *) q);
4715 if (count != (MagickOffsetType) length)
4716 break;
4717 q+=cache_info->number_channels*nexus_info->region.width;
4718 region.y++;
4719 }
4720 UnlockSemaphoreInfo(cache_info->file_semaphore);
4721 break;
4722 }
4723 default:
4724 break;
4725 }
4726 if (y < (ssize_t) rows)
4727 {
4728 ThrowFileException(exception,CacheError,"UnableToReadPixelCache",
4729 cache_info->cache_filename);
4730 return(MagickFalse);
4731 }
4732 if ((cache_info->debug != MagickFalse) &&
4733 (CacheTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
4734 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
4735 "%s[%.20gx%.20g%+.20g%+.20g]",cache_info->filename,(double)
4736 nexus_info->region.width,(double) nexus_info->region.height,(double)
4737 nexus_info->region.x,(double) nexus_info->region.y);
4738 return(MagickTrue);
4739}
4740
4741/*
4742%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4743% %
4744% %
4745% %
4746+ R e f e r e n c e P i x e l C a c h e %
4747% %
4748% %
4749% %
4750%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4751%
4752% ReferencePixelCache() increments the reference count associated with the
4753% pixel cache returning a pointer to the cache.
4754%
4755% The format of the ReferencePixelCache method is:
4756%
4757% Cache ReferencePixelCache(Cache cache_info)
4758%
4759% A description of each parameter follows:
4760%
4761% o cache_info: the pixel cache.
4762%
4763*/
4764MagickPrivate Cache ReferencePixelCache(Cache cache)
4765{
4766 CacheInfo
4767 *magick_restrict cache_info;
4768
4769 assert(cache != (Cache *) NULL);
4770 cache_info=(CacheInfo *) cache;
4771 assert(cache_info->signature == MagickCoreSignature);
4772 LockSemaphoreInfo(cache_info->semaphore);
4773 cache_info->reference_count++;
4774 UnlockSemaphoreInfo(cache_info->semaphore);
4775 return(cache_info);
4776}
4777
4778/*
4779%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4780% %
4781% %
4782% %
4783+ R e s e t P i x e l C a c h e C h a n n e l s %
4784% %
4785% %
4786% %
4787%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4788%
4789% ResetPixelCacheChannels() resets the pixel cache channels.
4790%
4791% The format of the ResetPixelCacheChannels method is:
4792%
4793% void ResetPixelCacheChannels(Image *)
4794%
4795% A description of each parameter follows:
4796%
4797% o image: the image.
4798%
4799*/
4800MagickPrivate void ResetPixelCacheChannels(Image *image)
4801{
4802 CacheInfo
4803 *magick_restrict cache_info;
4804
4805 assert(image != (const Image *) NULL);
4806 assert(image->signature == MagickCoreSignature);
4807 assert(image->cache != (Cache) NULL);
4808 cache_info=(CacheInfo *) image->cache;
4809 assert(cache_info->signature == MagickCoreSignature);
4810 cache_info->number_channels=GetPixelChannels(image);
4811}
4812
4813/*
4814%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4815% %
4816% %
4817% %
4818+ R e s e t C a c h e A n o n y m o u s M e m o r y %
4819% %
4820% %
4821% %
4822%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4823%
4824% ResetCacheAnonymousMemory() resets the anonymous_memory value.
4825%
4826% The format of the ResetCacheAnonymousMemory method is:
4827%
4828% void ResetCacheAnonymousMemory(void)
4829%
4830*/
4831MagickPrivate void ResetCacheAnonymousMemory(void)
4832{
4833 cache_anonymous_memory=0;
4834}
4835
4836/*
4837%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4838% %
4839% %
4840% %
4841% R e s h a p e P i x e l C a c h e %
4842% %
4843% %
4844% %
4845%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4846%
4847% ReshapePixelCache() reshapes an existing pixel cache.
4848%
4849% The format of the ReshapePixelCache() method is:
4850%
4851% MagickBooleanType ReshapePixelCache(Image *image,const size_t columns,
4852% const size_t rows,ExceptionInfo *exception)
4853%
4854% A description of each parameter follows:
4855%
4856% o image: the image.
4857%
4858% o columns: the number of columns in the reshaped pixel cache.
4859%
4860% o rows: number of rows in the reshaped pixel cache.
4861%
4862% o exception: return any errors or warnings in this structure.
4863%
4864*/
4865MagickExport MagickBooleanType ReshapePixelCache(Image *image,
4866 const size_t columns,const size_t rows,ExceptionInfo *exception)
4867{
4868 CacheInfo
4869 *cache_info;
4870
4871 MagickSizeType
4872 extent;
4873
4874 assert(image != (Image *) NULL);
4875 assert(image->signature == MagickCoreSignature);
4876 if (IsEventLogging() != MagickFalse)
4877 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4878 assert(image->cache != (void *) NULL);
4879 extent=(MagickSizeType) columns*rows;
4880 if (extent > ((MagickSizeType) image->columns*image->rows))
4881 ThrowBinaryException(ImageError,"WidthOrHeightExceedsLimit",
4882 image->filename);
4883 image->columns=columns;
4884 image->rows=rows;
4885 cache_info=(CacheInfo *) image->cache;
4886 cache_info->columns=columns;
4887 cache_info->rows=rows;
4888 return(SyncImagePixelCache(image,exception));
4889}
4890
4891/*
4892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4893% %
4894% %
4895% %
4896+ S e t P i x e l C a c h e M e t h o d s %
4897% %
4898% %
4899% %
4900%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4901%
4902% SetPixelCacheMethods() sets the image pixel methods to the specified ones.
4903%
4904% The format of the SetPixelCacheMethods() method is:
4905%
4906% SetPixelCacheMethods(Cache *,CacheMethods *cache_methods)
4907%
4908% A description of each parameter follows:
4909%
4910% o cache: the pixel cache.
4911%
4912% o cache_methods: Specifies a pointer to a CacheMethods structure.
4913%
4914*/
4915MagickPrivate void SetPixelCacheMethods(Cache cache,CacheMethods *cache_methods)
4916{
4917 CacheInfo
4918 *magick_restrict cache_info;
4919
4920 GetOneAuthenticPixelFromHandler
4921 get_one_authentic_pixel_from_handler;
4922
4923 GetOneVirtualPixelFromHandler
4924 get_one_virtual_pixel_from_handler;
4925
4926 /*
4927 Set cache pixel methods.
4928 */
4929 assert(cache != (Cache) NULL);
4930 assert(cache_methods != (CacheMethods *) NULL);
4931 cache_info=(CacheInfo *) cache;
4932 assert(cache_info->signature == MagickCoreSignature);
4933 if (IsEventLogging() != MagickFalse)
4934 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4935 cache_info->filename);
4936 if (cache_methods->get_virtual_pixel_handler != (GetVirtualPixelHandler) NULL)
4937 cache_info->methods.get_virtual_pixel_handler=
4938 cache_methods->get_virtual_pixel_handler;
4939 if (cache_methods->destroy_pixel_handler != (DestroyPixelHandler) NULL)
4940 cache_info->methods.destroy_pixel_handler=
4941 cache_methods->destroy_pixel_handler;
4942 if (cache_methods->get_virtual_metacontent_from_handler !=
4943 (GetVirtualMetacontentFromHandler) NULL)
4944 cache_info->methods.get_virtual_metacontent_from_handler=
4945 cache_methods->get_virtual_metacontent_from_handler;
4946 if (cache_methods->get_authentic_pixels_handler !=
4947 (GetAuthenticPixelsHandler) NULL)
4948 cache_info->methods.get_authentic_pixels_handler=
4949 cache_methods->get_authentic_pixels_handler;
4950 if (cache_methods->queue_authentic_pixels_handler !=
4951 (QueueAuthenticPixelsHandler) NULL)
4952 cache_info->methods.queue_authentic_pixels_handler=
4953 cache_methods->queue_authentic_pixels_handler;
4954 if (cache_methods->sync_authentic_pixels_handler !=
4955 (SyncAuthenticPixelsHandler) NULL)
4956 cache_info->methods.sync_authentic_pixels_handler=
4957 cache_methods->sync_authentic_pixels_handler;
4958 if (cache_methods->get_authentic_pixels_from_handler !=
4959 (GetAuthenticPixelsFromHandler) NULL)
4960 cache_info->methods.get_authentic_pixels_from_handler=
4961 cache_methods->get_authentic_pixels_from_handler;
4962 if (cache_methods->get_authentic_metacontent_from_handler !=
4963 (GetAuthenticMetacontentFromHandler) NULL)
4964 cache_info->methods.get_authentic_metacontent_from_handler=
4965 cache_methods->get_authentic_metacontent_from_handler;
4966 get_one_virtual_pixel_from_handler=
4967 cache_info->methods.get_one_virtual_pixel_from_handler;
4968 if (get_one_virtual_pixel_from_handler !=
4969 (GetOneVirtualPixelFromHandler) NULL)
4970 cache_info->methods.get_one_virtual_pixel_from_handler=
4971 cache_methods->get_one_virtual_pixel_from_handler;
4972 get_one_authentic_pixel_from_handler=
4973 cache_methods->get_one_authentic_pixel_from_handler;
4974 if (get_one_authentic_pixel_from_handler !=
4975 (GetOneAuthenticPixelFromHandler) NULL)
4976 cache_info->methods.get_one_authentic_pixel_from_handler=
4977 cache_methods->get_one_authentic_pixel_from_handler;
4978}
4979
4980/*
4981%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4982% %
4983% %
4984% %
4985+ S e t P i x e l C a c h e N e x u s P i x e l s %
4986% %
4987% %
4988% %
4989%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4990%
4991% SetPixelCacheNexusPixels() defines the region of the cache for the
4992% specified cache nexus.
4993%
4994% The format of the SetPixelCacheNexusPixels() method is:
4995%
4996% Quantum SetPixelCacheNexusPixels(
4997% const CacheInfo *magick_restrict cache_info,const MapMode mode,
4998% const ssize_t x,const ssize_t y,const size_t width,const size_t height,
4999% const MagickBooleanType buffered,NexusInfo *magick_restrict nexus_info,
5000% ExceptionInfo *exception)
5001%
5002% A description of each parameter follows:
5003%
5004% o cache_info: the pixel cache.
5005%
5006% o mode: ReadMode, WriteMode, or IOMode.
5007%
5008% o x,y,width,height: define the region of this particular cache nexus.
5009%
5010% o buffered: if true, nexus pixels are buffered.
5011%
5012% o nexus_info: the cache nexus to set.
5013%
5014% o exception: return any errors or warnings in this structure.
5015%
5016*/
5017
5018static inline MagickBooleanType AcquireCacheNexusPixels(
5019 const CacheInfo *magick_restrict cache_info,const MagickSizeType length,
5020 NexusInfo *magick_restrict nexus_info,ExceptionInfo *exception)
5021{
5022 if (length != (MagickSizeType) ((size_t) length))
5023 {
5024 (void) ThrowMagickException(exception,GetMagickModule(),
5025 ResourceLimitError,"PixelCacheAllocationFailed","`%s'",
5026 cache_info->filename);
5027 return(MagickFalse);
5028 }
5029 nexus_info->length=0;
5030 nexus_info->mapped=MagickFalse;
5031 if (cache_anonymous_memory <= 0)
5032 {
5033 nexus_info->cache=(Quantum *) MagickAssumeAligned(AcquireAlignedMemory(1,
5034 (size_t) length));
5035 if (nexus_info->cache != (Quantum *) NULL)
5036 (void) memset(nexus_info->cache,0,(size_t) length);
5037 }
5038 else
5039 {
5040 nexus_info->cache=(Quantum *) MapBlob(-1,IOMode,0,(size_t) length);
5041 if (nexus_info->cache != (Quantum *) NULL)
5042 nexus_info->mapped=MagickTrue;
5043 }
5044 if (nexus_info->cache == (Quantum *) NULL)
5045 {
5046 (void) ThrowMagickException(exception,GetMagickModule(),
5047 ResourceLimitError,"PixelCacheAllocationFailed","`%s'",
5048 cache_info->filename);
5049 return(MagickFalse);
5050 }
5051 nexus_info->length=length;
5052 return(MagickTrue);
5053}
5054
5055static inline void PrefetchPixelCacheNexusPixels(const NexusInfo *nexus_info,
5056 const MapMode mode)
5057{
5058 if (nexus_info->length < CACHE_LINE_SIZE)
5059 return;
5060 if (mode == ReadMode)
5061 {
5062 MagickCachePrefetch((unsigned char *) nexus_info->pixels+CACHE_LINE_SIZE,
5063 0,1);
5064 return;
5065 }
5066 MagickCachePrefetch((unsigned char *) nexus_info->pixels+CACHE_LINE_SIZE,1,1);
5067}
5068
5069static Quantum *SetPixelCacheNexusPixels(
5070 const CacheInfo *magick_restrict cache_info,const MapMode mode,
5071 const ssize_t x,const ssize_t y,const size_t width,const size_t height,
5072 const MagickBooleanType buffered,NexusInfo *magick_restrict nexus_info,
5073 ExceptionInfo *exception)
5074{
5075 MagickBooleanType
5076 status;
5077
5078 MagickSizeType
5079 length,
5080 number_pixels;
5081
5082 assert(cache_info != (const CacheInfo *) NULL);
5083 assert(cache_info->signature == MagickCoreSignature);
5084 if (cache_info->type == UndefinedCache)
5085 return((Quantum *) NULL);
5086 assert(nexus_info->signature == MagickCoreSignature);
5087 (void) memset(&nexus_info->region,0,sizeof(nexus_info->region));
5088 if ((width == 0) || (height == 0))
5089 {
5090 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
5091 "NoPixelsDefinedInCache","`%s'",cache_info->filename);
5092 return((Quantum *) NULL);
5093 }
5094 if (((MagickSizeType) width > cache_info->width_limit) ||
5095 ((MagickSizeType) height > cache_info->height_limit))
5096 {
5097 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
5098 "WidthOrHeightExceedsLimit","`%s'",cache_info->filename);
5099 return((Quantum *) NULL);
5100 }
5101 if ((IsValidPixelOffset(x,width) == MagickFalse) ||
5102 (IsValidPixelOffset(y,height) == MagickFalse))
5103 {
5104 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
5105 "InvalidPixel","`%s'",cache_info->filename);
5106 return((Quantum *) NULL);
5107 }
5108 if (((cache_info->type == MemoryCache) || (cache_info->type == MapCache)) &&
5109 (buffered == MagickFalse))
5110 {
5111 if (((x >= 0) && (y >= 0) &&
5112 (((ssize_t) height+y-1) < (ssize_t) cache_info->rows)) &&
5113 (((x == 0) && (width == cache_info->columns)) || ((height == 1) &&
5114 (((ssize_t) width+x-1) < (ssize_t) cache_info->columns))))
5115 {
5116 MagickOffsetType
5117 offset;
5118
5119 /*
5120 Pixels are accessed directly from memory.
5121 */
5122 if (IsValidPixelOffset(y,cache_info->columns) == MagickFalse)
5123 return((Quantum *) NULL);
5124 offset=y*(MagickOffsetType) cache_info->columns+x;
5125 nexus_info->pixels=cache_info->pixels+(MagickOffsetType)
5126 cache_info->number_channels*offset;
5127 nexus_info->metacontent=(void *) NULL;
5128 if (cache_info->metacontent_extent != 0)
5129 nexus_info->metacontent=(unsigned char *) cache_info->metacontent+
5130 offset*(MagickOffsetType) cache_info->metacontent_extent;
5131 nexus_info->region.width=width;
5132 nexus_info->region.height=height;
5133 nexus_info->region.x=x;
5134 nexus_info->region.y=y;
5135 nexus_info->authentic_pixel_cache=MagickTrue;
5136 PrefetchPixelCacheNexusPixels(nexus_info,mode);
5137 return(nexus_info->pixels);
5138 }
5139 }
5140 /*
5141 Pixels are stored in a staging region until they are synced to the cache.
5142 */
5143 number_pixels=(MagickSizeType) width*height;
5144 length=MagickMax(number_pixels,MagickMax(cache_info->columns,
5145 cache_info->rows))*cache_info->number_channels*sizeof(*nexus_info->pixels);
5146 if (cache_info->metacontent_extent != 0)
5147 length+=number_pixels*cache_info->metacontent_extent;
5148 status=MagickTrue;
5149 if (nexus_info->cache == (Quantum *) NULL)
5150 status=AcquireCacheNexusPixels(cache_info,length,nexus_info,exception);
5151 else
5152 if (nexus_info->length < length)
5153 {
5154 RelinquishCacheNexusPixels(nexus_info);
5155 status=AcquireCacheNexusPixels(cache_info,length,nexus_info,exception);
5156 }
5157 if (status == MagickFalse)
5158 return((Quantum *) NULL);
5159 nexus_info->pixels=nexus_info->cache;
5160 nexus_info->metacontent=(void *) NULL;
5161 if (cache_info->metacontent_extent != 0)
5162 nexus_info->metacontent=(void *) (nexus_info->pixels+
5163 cache_info->number_channels*number_pixels);
5164 nexus_info->region.width=width;
5165 nexus_info->region.height=height;
5166 nexus_info->region.x=x;
5167 nexus_info->region.y=y;
5168 nexus_info->authentic_pixel_cache=cache_info->type == PingCache ?
5169 MagickTrue : MagickFalse;
5170 PrefetchPixelCacheNexusPixels(nexus_info,mode);
5171 return(nexus_info->pixels);
5172}
5173
5174/*
5175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5176% %
5177% %
5178% %
5179% S e t P i x e l C a c h e V i r t u a l M e t h o d %
5180% %
5181% %
5182% %
5183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5184%
5185% SetPixelCacheVirtualMethod() sets the "virtual pixels" method for the
5186% pixel cache and returns the previous setting. A virtual pixel is any pixel
5187% access that is outside the boundaries of the image cache.
5188%
5189% The format of the SetPixelCacheVirtualMethod() method is:
5190%
5191% VirtualPixelMethod SetPixelCacheVirtualMethod(Image *image,
5192% const VirtualPixelMethod virtual_pixel_method,ExceptionInfo *exception)
5193%
5194% A description of each parameter follows:
5195%
5196% o image: the image.
5197%
5198% o virtual_pixel_method: choose the type of virtual pixel.
5199%
5200% o exception: return any errors or warnings in this structure.
5201%
5202*/
5203
5204static MagickBooleanType SetCacheAlphaChannel(Image *image,const Quantum alpha,
5205 ExceptionInfo *exception)
5206{
5207 CacheView
5208 *magick_restrict image_view;
5209
5210 MagickBooleanType
5211 status;
5212
5213 ssize_t
5214 y;
5215
5216 assert(image != (Image *) NULL);
5217 assert(image->signature == MagickCoreSignature);
5218 if (IsEventLogging() != MagickFalse)
5219 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5220 assert(image->cache != (Cache) NULL);
5221 image->alpha_trait=BlendPixelTrait;
5222 status=MagickTrue;
5223 image_view=AcquireVirtualCacheView(image,exception); /* must be virtual */
5224#if defined(MAGICKCORE_OPENMP_SUPPORT)
5225 #pragma omp parallel for schedule(static) shared(status) \
5226 magick_number_threads(image,image,image->rows,2)
5227#endif
5228 for (y=0; y < (ssize_t) image->rows; y++)
5229 {
5230 Quantum
5231 *magick_restrict q;
5232
5233 ssize_t
5234 x;
5235
5236 if (status == MagickFalse)
5237 continue;
5238 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
5239 if (q == (Quantum *) NULL)
5240 {
5241 status=MagickFalse;
5242 continue;
5243 }
5244 for (x=0; x < (ssize_t) image->columns; x++)
5245 {
5246 SetPixelAlpha(image,alpha,q);
5247 q+=GetPixelChannels(image);
5248 }
5249 status=SyncCacheViewAuthenticPixels(image_view,exception);
5250 }
5251 image_view=DestroyCacheView(image_view);
5252 return(status);
5253}
5254
5255MagickPrivate VirtualPixelMethod SetPixelCacheVirtualMethod(Image *image,
5256 const VirtualPixelMethod virtual_pixel_method,ExceptionInfo *exception)
5257{
5258 CacheInfo
5259 *magick_restrict cache_info;
5260
5261 VirtualPixelMethod
5262 method;
5263
5264 assert(image != (Image *) NULL);
5265 assert(image->signature == MagickCoreSignature);
5266 if (IsEventLogging() != MagickFalse)
5267 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5268 assert(image->cache != (Cache) NULL);
5269 cache_info=(CacheInfo *) image->cache;
5270 assert(cache_info->signature == MagickCoreSignature);
5271 method=cache_info->virtual_pixel_method;
5272 cache_info->virtual_pixel_method=virtual_pixel_method;
5273 if ((image->columns != 0) && (image->rows != 0))
5274 switch (virtual_pixel_method)
5275 {
5276 case BackgroundVirtualPixelMethod:
5277 {
5278 if ((image->background_color.alpha_trait != UndefinedPixelTrait) &&
5279 ((image->alpha_trait & BlendPixelTrait) == 0))
5280 (void) SetCacheAlphaChannel(image,OpaqueAlpha,exception);
5281 if ((IsPixelInfoGray(&image->background_color) == MagickFalse) &&
5282 (IsGrayColorspace(image->colorspace) != MagickFalse))
5283 (void) SetImageColorspace(image,sRGBColorspace,exception);
5284 break;
5285 }
5286 case TransparentVirtualPixelMethod:
5287 {
5288 if ((image->alpha_trait & BlendPixelTrait) == 0)
5289 (void) SetCacheAlphaChannel(image,OpaqueAlpha,exception);
5290 break;
5291 }
5292 default:
5293 break;
5294 }
5295 return(method);
5296}
5297
5298#if defined(MAGICKCORE_OPENCL_SUPPORT)
5299/*
5300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5301% %
5302% %
5303% %
5304+ S y n c A u t h e n t i c O p e n C L B u f f e r %
5305% %
5306% %
5307% %
5308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5309%
5310% SyncAuthenticOpenCLBuffer() makes sure that all the OpenCL operations have
5311% been completed and updates the host memory.
5312%
5313% The format of the SyncAuthenticOpenCLBuffer() method is:
5314%
5315% void SyncAuthenticOpenCLBuffer(const Image *image)
5316%
5317% A description of each parameter follows:
5318%
5319% o image: the image.
5320%
5321*/
5322
5323static void CopyOpenCLBuffer(CacheInfo *magick_restrict cache_info)
5324{
5325 assert(cache_info != (CacheInfo *) NULL);
5326 assert(cache_info->signature == MagickCoreSignature);
5327 if ((cache_info->type != MemoryCache) ||
5328 (cache_info->opencl == (MagickCLCacheInfo) NULL))
5329 return;
5330 /*
5331 Ensure single threaded access to OpenCL environment.
5332 */
5333 LockSemaphoreInfo(cache_info->semaphore);
5334 cache_info->opencl=CopyMagickCLCacheInfo(cache_info->opencl);
5335 UnlockSemaphoreInfo(cache_info->semaphore);
5336}
5337
5338MagickPrivate void SyncAuthenticOpenCLBuffer(const Image *image)
5339{
5340 CacheInfo
5341 *magick_restrict cache_info;
5342
5343 assert(image != (const Image *) NULL);
5344 cache_info=(CacheInfo *) image->cache;
5345 CopyOpenCLBuffer(cache_info);
5346}
5347#endif
5348
5349/*
5350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5351% %
5352% %
5353% %
5354+ S y n c A u t h e n t i c P i x e l C a c h e N e x u s %
5355% %
5356% %
5357% %
5358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5359%
5360% SyncAuthenticPixelCacheNexus() saves the authentic image pixels to the
5361% in-memory or disk cache. The method returns MagickTrue if the pixel region
5362% is synced, otherwise MagickFalse.
5363%
5364% The format of the SyncAuthenticPixelCacheNexus() method is:
5365%
5366% MagickBooleanType SyncAuthenticPixelCacheNexus(Image *image,
5367% NexusInfo *nexus_info,ExceptionInfo *exception)
5368%
5369% A description of each parameter follows:
5370%
5371% o image: the image.
5372%
5373% o nexus_info: the cache nexus to sync.
5374%
5375% o exception: return any errors or warnings in this structure.
5376%
5377*/
5378MagickPrivate MagickBooleanType SyncAuthenticPixelCacheNexus(Image *image,
5379 NexusInfo *magick_restrict nexus_info,ExceptionInfo *exception)
5380{
5381 CacheInfo
5382 *magick_restrict cache_info;
5383
5384 MagickBooleanType
5385 status;
5386
5387 /*
5388 Transfer pixels to the cache.
5389 */
5390 assert(image != (Image *) NULL);
5391 assert(image->signature == MagickCoreSignature);
5392 if (image->cache == (Cache) NULL)
5393 ThrowBinaryException(CacheError,"PixelCacheIsNotOpen",image->filename);
5394 cache_info=(CacheInfo *) image->cache;
5395 assert(cache_info->signature == MagickCoreSignature);
5396 if (cache_info->type == UndefinedCache)
5397 return(MagickFalse);
5398 if (image->mask_trait != UpdatePixelTrait)
5399 {
5400 if (((image->channels & WriteMaskChannel) != 0) &&
5401 (ClipPixelCacheNexus(image,nexus_info,exception) == MagickFalse))
5402 return(MagickFalse);
5403 if (((image->channels & CompositeMaskChannel) != 0) &&
5404 (MaskPixelCacheNexus(image,nexus_info,exception) == MagickFalse))
5405 return(MagickFalse);
5406 }
5407 if (nexus_info->authentic_pixel_cache != MagickFalse)
5408 {
5409 if (image->taint == MagickFalse)
5410 image->taint=MagickTrue;
5411 return(MagickTrue);
5412 }
5413 assert(cache_info->signature == MagickCoreSignature);
5414 status=WritePixelCachePixels(cache_info,nexus_info,exception);
5415 if ((cache_info->metacontent_extent != 0) &&
5416 (WritePixelCacheMetacontent(cache_info,nexus_info,exception) == MagickFalse))
5417 return(MagickFalse);
5418 if ((status != MagickFalse) && (image->taint == MagickFalse))
5419 image->taint=MagickTrue;
5420 return(status);
5421}
5422
5423/*
5424%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5425% %
5426% %
5427% %
5428+ S y n c A u t h e n t i c P i x e l C a c h e %
5429% %
5430% %
5431% %
5432%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5433%
5434% SyncAuthenticPixelsCache() saves the authentic image pixels to the in-memory
5435% or disk cache. The method returns MagickTrue if the pixel region is synced,
5436% otherwise MagickFalse.
5437%
5438% The format of the SyncAuthenticPixelsCache() method is:
5439%
5440% MagickBooleanType SyncAuthenticPixelsCache(Image *image,
5441% ExceptionInfo *exception)
5442%
5443% A description of each parameter follows:
5444%
5445% o image: the image.
5446%
5447% o exception: return any errors or warnings in this structure.
5448%
5449*/
5450static MagickBooleanType SyncAuthenticPixelsCache(Image *image,
5451 ExceptionInfo *exception)
5452{
5453 CacheInfo
5454 *magick_restrict cache_info;
5455
5456 const int
5457 id = GetOpenMPThreadId();
5458
5459 MagickBooleanType
5460 status;
5461
5462 assert(image != (Image *) NULL);
5463 assert(image->signature == MagickCoreSignature);
5464 assert(image->cache != (Cache) NULL);
5465 cache_info=(CacheInfo *) image->cache;
5466 assert(cache_info->signature == MagickCoreSignature);
5467 assert(id < (int) cache_info->number_threads);
5468 status=SyncAuthenticPixelCacheNexus(image,cache_info->nexus_info[id],
5469 exception);
5470 return(status);
5471}
5472
5473/*
5474%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5475% %
5476% %
5477% %
5478% S y n c A u t h e n t i c P i x e l s %
5479% %
5480% %
5481% %
5482%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5483%
5484% SyncAuthenticPixels() saves the image pixels to the in-memory or disk cache.
5485% The method returns MagickTrue if the pixel region is flushed, otherwise
5486% MagickFalse.
5487%
5488% The format of the SyncAuthenticPixels() method is:
5489%
5490% MagickBooleanType SyncAuthenticPixels(Image *image,
5491% ExceptionInfo *exception)
5492%
5493% A description of each parameter follows:
5494%
5495% o image: the image.
5496%
5497% o exception: return any errors or warnings in this structure.
5498%
5499*/
5500MagickExport MagickBooleanType SyncAuthenticPixels(Image *image,
5501 ExceptionInfo *exception)
5502{
5503 CacheInfo
5504 *magick_restrict cache_info;
5505
5506 const int
5507 id = GetOpenMPThreadId();
5508
5509 MagickBooleanType
5510 status;
5511
5512 assert(image != (Image *) NULL);
5513 assert(image->signature == MagickCoreSignature);
5514 assert(image->cache != (Cache) NULL);
5515 cache_info=(CacheInfo *) image->cache;
5516 assert(cache_info->signature == MagickCoreSignature);
5517 if (cache_info->methods.sync_authentic_pixels_handler != (SyncAuthenticPixelsHandler) NULL)
5518 {
5519 status=cache_info->methods.sync_authentic_pixels_handler(image,
5520 exception);
5521 return(status);
5522 }
5523 assert(id < (int) cache_info->number_threads);
5524 status=SyncAuthenticPixelCacheNexus(image,cache_info->nexus_info[id],
5525 exception);
5526 return(status);
5527}
5528
5529/*
5530%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5531% %
5532% %
5533% %
5534+ S y n c I m a g e P i x e l C a c h e %
5535% %
5536% %
5537% %
5538%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5539%
5540% SyncImagePixelCache() saves the image pixels to the in-memory or disk cache.
5541% The method returns MagickTrue if the pixel region is flushed, otherwise
5542% MagickFalse.
5543%
5544% The format of the SyncImagePixelCache() method is:
5545%
5546% MagickBooleanType SyncImagePixelCache(Image *image,
5547% ExceptionInfo *exception)
5548%
5549% A description of each parameter follows:
5550%
5551% o image: the image.
5552%
5553% o exception: return any errors or warnings in this structure.
5554%
5555*/
5556MagickPrivate MagickBooleanType SyncImagePixelCache(Image *image,
5557 ExceptionInfo *exception)
5558{
5559 CacheInfo
5560 *magick_restrict cache_info;
5561
5562 assert(image != (Image *) NULL);
5563 assert(exception != (ExceptionInfo *) NULL);
5564 cache_info=(CacheInfo *) GetImagePixelCache(image,MagickTrue,exception);
5565 return(cache_info == (CacheInfo *) NULL ? MagickFalse : MagickTrue);
5566}
5567
5568/*
5569%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5570% %
5571% %
5572% %
5573+ W r i t e P i x e l C a c h e M e t a c o n t e n t %
5574% %
5575% %
5576% %
5577%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5578%
5579% WritePixelCacheMetacontent() writes the meta-content to the specified region
5580% of the pixel cache.
5581%
5582% The format of the WritePixelCacheMetacontent() method is:
5583%
5584% MagickBooleanType WritePixelCacheMetacontent(CacheInfo *cache_info,
5585% NexusInfo *nexus_info,ExceptionInfo *exception)
5586%
5587% A description of each parameter follows:
5588%
5589% o cache_info: the pixel cache.
5590%
5591% o nexus_info: the cache nexus to write the meta-content.
5592%
5593% o exception: return any errors or warnings in this structure.
5594%
5595*/
5596static MagickBooleanType WritePixelCacheMetacontent(CacheInfo *cache_info,
5597 NexusInfo *magick_restrict nexus_info,ExceptionInfo *exception)
5598{
5599 MagickOffsetType
5600 count,
5601 offset;
5602
5603 MagickSizeType
5604 extent,
5605 length;
5606
5607 const unsigned char
5608 *magick_restrict p;
5609
5610 ssize_t
5611 y;
5612
5613 size_t
5614 rows;
5615
5616 if (cache_info->metacontent_extent == 0)
5617 return(MagickFalse);
5618 if (nexus_info->authentic_pixel_cache != MagickFalse)
5619 return(MagickTrue);
5620 if (nexus_info->metacontent == (unsigned char *) NULL)
5621 return(MagickFalse);
5622 if (IsValidPixelOffset(nexus_info->region.y,cache_info->columns) == MagickFalse)
5623 return(MagickFalse);
5624 offset=nexus_info->region.y*(MagickOffsetType) cache_info->columns+
5625 nexus_info->region.x;
5626 length=(MagickSizeType) nexus_info->region.width*
5627 cache_info->metacontent_extent;
5628 extent=(MagickSizeType) length*nexus_info->region.height;
5629 rows=nexus_info->region.height;
5630 y=0;
5631 p=(unsigned char *) nexus_info->metacontent;
5632 switch (cache_info->type)
5633 {
5634 case MemoryCache:
5635 case MapCache:
5636 {
5637 unsigned char
5638 *magick_restrict q;
5639
5640 /*
5641 Write associated pixels to memory.
5642 */
5643 if ((cache_info->columns == nexus_info->region.width) &&
5644 (extent == (MagickSizeType) ((size_t) extent)))
5645 {
5646 length=extent;
5647 rows=1UL;
5648 }
5649 q=(unsigned char *) cache_info->metacontent+offset*
5650 (MagickOffsetType) cache_info->metacontent_extent;
5651 for (y=0; y < (ssize_t) rows; y++)
5652 {
5653 (void) memcpy(q,p,(size_t) length);
5654 p+=nexus_info->region.width*cache_info->metacontent_extent;
5655 q+=cache_info->columns*cache_info->metacontent_extent;
5656 }
5657 break;
5658 }
5659 case DiskCache:
5660 {
5661 /*
5662 Write associated pixels to disk.
5663 */
5664 LockSemaphoreInfo(cache_info->file_semaphore);
5665 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
5666 {
5667 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
5668 cache_info->cache_filename);
5669 UnlockSemaphoreInfo(cache_info->file_semaphore);
5670 return(MagickFalse);
5671 }
5672 if ((cache_info->columns == nexus_info->region.width) &&
5673 (extent <= MagickMaxBufferExtent))
5674 {
5675 length=extent;
5676 rows=1UL;
5677 }
5678 extent=(MagickSizeType) cache_info->columns*cache_info->rows;
5679 for (y=0; y < (ssize_t) rows; y++)
5680 {
5681 count=WritePixelCacheRegion(cache_info,cache_info->offset+
5682 (MagickOffsetType) extent*(MagickOffsetType)
5683 cache_info->number_channels*(MagickOffsetType) sizeof(Quantum)+offset*
5684 (MagickOffsetType) cache_info->metacontent_extent,length,
5685 (const unsigned char *) p);
5686 if (count != (MagickOffsetType) length)
5687 break;
5688 p+=cache_info->metacontent_extent*nexus_info->region.width;
5689 offset+=(MagickOffsetType) cache_info->columns;
5690 }
5691 if (IsFileDescriptorLimitExceeded() != MagickFalse)
5692 (void) ClosePixelCacheOnDisk(cache_info);
5693 UnlockSemaphoreInfo(cache_info->file_semaphore);
5694 break;
5695 }
5696 case DistributedCache:
5697 {
5699 region;
5700
5701 /*
5702 Write metacontent to distributed cache.
5703 */
5704 LockSemaphoreInfo(cache_info->file_semaphore);
5705 region=nexus_info->region;
5706 if ((cache_info->columns != nexus_info->region.width) ||
5707 (extent > MagickMaxBufferExtent))
5708 region.height=1UL;
5709 else
5710 {
5711 length=extent;
5712 rows=1UL;
5713 }
5714 for (y=0; y < (ssize_t) rows; y++)
5715 {
5716 count=WriteDistributePixelCacheMetacontent((DistributeCacheInfo *)
5717 cache_info->server_info,&region,length,(const unsigned char *) p);
5718 if (count != (MagickOffsetType) length)
5719 break;
5720 p+=cache_info->metacontent_extent*nexus_info->region.width;
5721 region.y++;
5722 }
5723 UnlockSemaphoreInfo(cache_info->file_semaphore);
5724 break;
5725 }
5726 default:
5727 break;
5728 }
5729 if (y < (ssize_t) rows)
5730 {
5731 ThrowFileException(exception,CacheError,"UnableToWritePixelCache",
5732 cache_info->cache_filename);
5733 return(MagickFalse);
5734 }
5735 if ((cache_info->debug != MagickFalse) &&
5736 (CacheTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
5737 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
5738 "%s[%.20gx%.20g%+.20g%+.20g]",cache_info->filename,(double)
5739 nexus_info->region.width,(double) nexus_info->region.height,(double)
5740 nexus_info->region.x,(double) nexus_info->region.y);
5741 return(MagickTrue);
5742}
5743
5744/*
5745%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5746% %
5747% %
5748% %
5749+ W r i t e C a c h e P i x e l s %
5750% %
5751% %
5752% %
5753%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5754%
5755% WritePixelCachePixels() writes image pixels to the specified region of the
5756% pixel cache.
5757%
5758% The format of the WritePixelCachePixels() method is:
5759%
5760% MagickBooleanType WritePixelCachePixels(CacheInfo *cache_info,
5761% NexusInfo *nexus_info,ExceptionInfo *exception)
5762%
5763% A description of each parameter follows:
5764%
5765% o cache_info: the pixel cache.
5766%
5767% o nexus_info: the cache nexus to write the pixels.
5768%
5769% o exception: return any errors or warnings in this structure.
5770%
5771*/
5772static MagickBooleanType WritePixelCachePixels(
5773 CacheInfo *magick_restrict cache_info,NexusInfo *magick_restrict nexus_info,
5774 ExceptionInfo *exception)
5775{
5776 MagickOffsetType
5777 count,
5778 offset;
5779
5780 MagickSizeType
5781 extent,
5782 length;
5783
5784 const Quantum
5785 *magick_restrict p;
5786
5787 ssize_t
5788 y;
5789
5790 size_t
5791 rows;
5792
5793 if (nexus_info->authentic_pixel_cache != MagickFalse)
5794 return(MagickTrue);
5795 if (IsValidPixelOffset(nexus_info->region.y,cache_info->columns) == MagickFalse)
5796 return(MagickFalse);
5797 offset=nexus_info->region.y*(MagickOffsetType) cache_info->columns+
5798 nexus_info->region.x;
5799 length=(MagickSizeType) cache_info->number_channels*nexus_info->region.width*
5800 sizeof(Quantum);
5801 extent=length*nexus_info->region.height;
5802 rows=nexus_info->region.height;
5803 y=0;
5804 p=nexus_info->pixels;
5805 switch (cache_info->type)
5806 {
5807 case MemoryCache:
5808 case MapCache:
5809 {
5810 Quantum
5811 *magick_restrict q;
5812
5813 /*
5814 Write pixels to memory.
5815 */
5816 if ((cache_info->columns == nexus_info->region.width) &&
5817 (extent == (MagickSizeType) ((size_t) extent)))
5818 {
5819 length=extent;
5820 rows=1UL;
5821 }
5822 q=cache_info->pixels+(MagickOffsetType) cache_info->number_channels*
5823 offset;
5824 for (y=0; y < (ssize_t) rows; y++)
5825 {
5826 (void) memcpy(q,p,(size_t) length);
5827 p+=cache_info->number_channels*nexus_info->region.width;
5828 q+=cache_info->number_channels*cache_info->columns;
5829 }
5830 break;
5831 }
5832 case DiskCache:
5833 {
5834 /*
5835 Write pixels to disk.
5836 */
5837 LockSemaphoreInfo(cache_info->file_semaphore);
5838 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
5839 {
5840 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
5841 cache_info->cache_filename);
5842 UnlockSemaphoreInfo(cache_info->file_semaphore);
5843 return(MagickFalse);
5844 }
5845 if ((cache_info->columns == nexus_info->region.width) &&
5846 (extent <= MagickMaxBufferExtent))
5847 {
5848 length=extent;
5849 rows=1UL;
5850 }
5851 for (y=0; y < (ssize_t) rows; y++)
5852 {
5853 count=WritePixelCacheRegion(cache_info,cache_info->offset+offset*
5854 (MagickOffsetType) cache_info->number_channels*(MagickOffsetType)
5855 sizeof(*p),length,(const unsigned char *) p);
5856 if (count != (MagickOffsetType) length)
5857 break;
5858 p+=cache_info->number_channels*nexus_info->region.width;
5859 offset+=(MagickOffsetType) cache_info->columns;
5860 }
5861 if (IsFileDescriptorLimitExceeded() != MagickFalse)
5862 (void) ClosePixelCacheOnDisk(cache_info);
5863 UnlockSemaphoreInfo(cache_info->file_semaphore);
5864 break;
5865 }
5866 case DistributedCache:
5867 {
5869 region;
5870
5871 /*
5872 Write pixels to distributed cache.
5873 */
5874 LockSemaphoreInfo(cache_info->file_semaphore);
5875 region=nexus_info->region;
5876 if ((cache_info->columns != nexus_info->region.width) ||
5877 (extent > MagickMaxBufferExtent))
5878 region.height=1UL;
5879 else
5880 {
5881 length=extent;
5882 rows=1UL;
5883 }
5884 for (y=0; y < (ssize_t) rows; y++)
5885 {
5886 count=WriteDistributePixelCachePixels((DistributeCacheInfo *)
5887 cache_info->server_info,&region,length,(const unsigned char *) p);
5888 if (count != (MagickOffsetType) length)
5889 break;
5890 p+=cache_info->number_channels*nexus_info->region.width;
5891 region.y++;
5892 }
5893 UnlockSemaphoreInfo(cache_info->file_semaphore);
5894 break;
5895 }
5896 default:
5897 break;
5898 }
5899 if (y < (ssize_t) rows)
5900 {
5901 ThrowFileException(exception,CacheError,"UnableToWritePixelCache",
5902 cache_info->cache_filename);
5903 return(MagickFalse);
5904 }
5905 if ((cache_info->debug != MagickFalse) &&
5906 (CacheTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
5907 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
5908 "%s[%.20gx%.20g%+.20g%+.20g]",cache_info->filename,(double)
5909 nexus_info->region.width,(double) nexus_info->region.height,(double)
5910 nexus_info->region.x,(double) nexus_info->region.y);
5911 return(MagickTrue);
5912}