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