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