blob: 98fb393481d7ec7ef167ef0a5bf09855f4fb5935 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% John Cristy %
17% July 1999 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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% http://www.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 "magick/studio.h"
44#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
47#include "magick/cache-private.h"
48#include "magick/color-private.h"
49#include "magick/composite-private.h"
50#include "magick/exception.h"
51#include "magick/exception-private.h"
cristy365e58f2010-02-15 02:00:01 +000052#include "magick/geometry.h"
cristy3ed852e2009-09-05 21:47:34 +000053#include "magick/list.h"
54#include "magick/log.h"
55#include "magick/magick.h"
56#include "magick/memory_.h"
cristy4789f0d2010-01-10 00:01:06 +000057#include "magick/pixel.h"
cristy3ed852e2009-09-05 21:47:34 +000058#include "magick/pixel-private.h"
59#include "magick/quantum.h"
60#include "magick/random_.h"
61#include "magick/resource_.h"
62#include "magick/semaphore.h"
63#include "magick/splay-tree.h"
64#include "magick/string_.h"
65#include "magick/thread-private.h"
66#include "magick/utility.h"
67#if defined(MAGICKCORE_ZLIB_DELEGATE)
68#include "zlib.h"
69#endif
70
71/*
72 Typedef declarations.
73*/
74typedef struct _MagickModulo
75{
76 long
77 quotient,
78 remainder;
79} MagickModulo;
80
81struct _NexusInfo
82{
83 MagickBooleanType
84 mapped;
85
86 RectangleInfo
87 region;
88
89 MagickSizeType
90 length;
91
92 PixelPacket
93 *cache,
94 *pixels;
95
96 IndexPacket
97 *indexes;
98
99 unsigned long
100 signature;
101};
102
103/*
104 Forward declarations.
105*/
106#if defined(__cplusplus) || defined(c_plusplus)
107extern "C" {
108#endif
109
110static const IndexPacket
111 *GetVirtualIndexesFromCache(const Image *);
112
113static const PixelPacket
114 *GetVirtualPixelCache(const Image *,const VirtualPixelMethod,const long,
115 const long,const unsigned long,const unsigned long,ExceptionInfo *),
116 *GetVirtualPixelsCache(const Image *);
117
118static MagickBooleanType
119 GetOneAuthenticPixelFromCache(Image *,const long,const long,PixelPacket *,
120 ExceptionInfo *),
121 GetOneVirtualPixelFromCache(const Image *,const VirtualPixelMethod,
122 const long,const long,PixelPacket *,ExceptionInfo *),
123 OpenPixelCache(Image *,const MapMode,ExceptionInfo *),
124 ReadPixelCacheIndexes(CacheInfo *,NexusInfo *,ExceptionInfo *),
125 ReadPixelCachePixels(CacheInfo *,NexusInfo *,ExceptionInfo *),
126 SyncAuthenticPixelsCache(Image *,ExceptionInfo *),
127 WritePixelCacheIndexes(CacheInfo *,NexusInfo *,ExceptionInfo *),
128 WritePixelCachePixels(CacheInfo *,NexusInfo *,ExceptionInfo *);
129
130static PixelPacket
131 *GetAuthenticPixelsCache(Image *,const long,const long,const unsigned long,
132 const unsigned long,ExceptionInfo *),
133 *QueueAuthenticPixelsCache(Image *,const long,const long,const unsigned long,
134 const unsigned long,ExceptionInfo *),
135 *SetPixelCacheNexusPixels(const Image *,const RectangleInfo *,NexusInfo *,
cristya4af2e32010-03-08 00:51:56 +0000136 ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000137
138#if defined(__cplusplus) || defined(c_plusplus)
139}
140#endif
141
142/*
143 Global declarations.
144*/
145static volatile MagickBooleanType
146 instantiate_cache = MagickFalse;
147
148static SemaphoreInfo
149 *cache_semaphore = (SemaphoreInfo *) NULL;
150
151static SplayTreeInfo
152 *cache_resources = (SplayTreeInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000153
154/*
155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156% %
157% %
158% %
159+ A c q u i r e P i x e l C a c h e %
160% %
161% %
162% %
163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164%
165% AcquirePixelCache() acquires a pixel cache.
166%
167% The format of the AcquirePixelCache() method is:
168%
169% Cache AcquirePixelCache(const unsigned long number_threads)
170%
171% A description of each parameter follows:
172%
173% o number_threads: the number of nexus threads.
174%
175*/
176MagickExport Cache AcquirePixelCache(const unsigned long number_threads)
177{
178 CacheInfo
179 *cache_info;
180
181 cache_info=(CacheInfo *) AcquireAlignedMemory(1,sizeof(*cache_info));
182 if (cache_info == (CacheInfo *) NULL)
183 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
184 (void) ResetMagickMemory(cache_info,0,sizeof(*cache_info));
185 cache_info->type=UndefinedCache;
cristy87528ea2009-09-10 14:53:56 +0000186 cache_info->mode=IOMode;
cristy3ed852e2009-09-05 21:47:34 +0000187 cache_info->colorspace=RGBColorspace;
188 cache_info->file=(-1);
189 cache_info->id=GetMagickThreadId();
190 cache_info->number_threads=number_threads;
191 if (number_threads == 0)
192 cache_info->number_threads=GetOpenMPMaximumThreads();
193 cache_info->nexus_info=AcquirePixelCacheNexus(cache_info->number_threads);
194 if (cache_info->nexus_info == (NexusInfo **) NULL)
195 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
196 GetPixelCacheMethods(&cache_info->methods);
197 cache_info->reference_count=1;
198 cache_info->semaphore=AllocateSemaphoreInfo();
199 cache_info->disk_semaphore=AllocateSemaphoreInfo();
200 cache_info->debug=IsEventLogging();
201 cache_info->signature=MagickSignature;
202 if ((cache_resources == (SplayTreeInfo *) NULL) &&
203 (instantiate_cache == MagickFalse))
204 {
cristy4e1dff62009-10-25 20:36:03 +0000205 if (cache_semaphore == (SemaphoreInfo *) NULL)
206 AcquireSemaphoreInfo(&cache_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000207 LockSemaphoreInfo(cache_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000208 if ((cache_resources == (SplayTreeInfo *) NULL) &&
209 (instantiate_cache == MagickFalse))
210 {
211 cache_resources=NewSplayTree((int (*)(const void *,const void *))
212 NULL,(void *(*)(void *)) NULL,(void *(*)(void *)) NULL);
213 instantiate_cache=MagickTrue;
214 }
cristyf84a1932010-01-03 18:00:18 +0000215 UnlockSemaphoreInfo(cache_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000216 }
217 (void) AddValueToSplayTree(cache_resources,cache_info,cache_info);
218 return((Cache ) cache_info);
219}
220
221/*
222%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223% %
224% %
225% %
226% A c q u i r e P i x e l C a c h e N e x u s %
227% %
228% %
229% %
230%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231%
232% AcquirePixelCacheNexus() allocates the NexusInfo structure.
233%
234% The format of the AcquirePixelCacheNexus method is:
235%
236% NexusInfo **AcquirePixelCacheNexus(const unsigned long number_threads)
237%
238% A description of each parameter follows:
239%
240% o number_threads: the number of nexus threads.
241%
242*/
243MagickExport NexusInfo **AcquirePixelCacheNexus(
244 const unsigned long number_threads)
245{
246 register long
247 i;
248
249 NexusInfo
250 **nexus_info;
251
252 nexus_info=(NexusInfo **) AcquireAlignedMemory(number_threads,
253 sizeof(*nexus_info));
254 if (nexus_info == (NexusInfo **) NULL)
255 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
256 for (i=0; i < (long) number_threads; i++)
257 {
258 nexus_info[i]=(NexusInfo *) AcquireAlignedMemory(1,sizeof(**nexus_info));
259 if (nexus_info[i] == (NexusInfo *) NULL)
260 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
261 (void) ResetMagickMemory(nexus_info[i],0,sizeof(*nexus_info[i]));
262 nexus_info[i]->signature=MagickSignature;
263 }
264 return(nexus_info);
265}
266
267/*
268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269% %
270% %
271% %
cristyd43a46b2010-01-21 02:13:41 +0000272+ A c q u i r e P i x e l C a c h e P i x e l s %
273% %
274% %
275% %
276%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277%
278% AcquirePixelCachePixels() returns the pixels associated with the specified
279% image.
280%
281% The format of the AcquirePixelCachePixels() method is:
282%
283% const void *AcquirePixelCachePixels(const Image *image,
284% MagickSizeType *length,ExceptionInfo *exception)
285%
286% A description of each parameter follows:
287%
288% o image: the image.
289%
290% o length: the pixel cache length.
291%
292% o exception: return any errors or warnings in this structure.
293%
294*/
295MagickExport const void *AcquirePixelCachePixels(const Image *image,
296 MagickSizeType *length,ExceptionInfo *exception)
297{
298 CacheInfo
299 *cache_info;
300
301 assert(image != (const Image *) NULL);
302 assert(image->signature == MagickSignature);
303 if (image->debug != MagickFalse)
304 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
305 assert(exception != (ExceptionInfo *) NULL);
306 assert(exception->signature == MagickSignature);
307 assert(image->cache != (Cache) NULL);
308 cache_info=(CacheInfo *) image->cache;
309 assert(cache_info->signature == MagickSignature);
310 *length=0;
311 if ((cache_info->type != MemoryCache) && (cache_info->type != MapCache))
312 return((const void *) NULL);
313 *length=cache_info->length;
314 return((const void *) cache_info->pixels);
315}
316
317/*
318%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319% %
320% %
321% %
cristyf34a1452009-10-24 22:29:27 +0000322+ C a c h e C o m p o n e n t G e n e s i s %
323% %
324% %
325% %
326%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327%
328% CacheComponentGenesis() instantiates the cache component.
329%
330% The format of the CacheComponentGenesis method is:
331%
332% MagickBooleanType CacheComponentGenesis(void)
333%
334*/
335MagickExport MagickBooleanType CacheComponentGenesis(void)
336{
cristy165b6092009-10-26 13:52:10 +0000337 AcquireSemaphoreInfo(&cache_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000338 return(MagickTrue);
339}
340
341/*
342%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343% %
344% %
345% %
346+ C a c h e C o m p o n e n t T e r m i n u s %
347% %
348% %
349% %
350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351%
352% CacheComponentTerminus() destroys the cache component.
353%
354% The format of the CacheComponentTerminus() method is:
355%
356% CacheComponentTerminus(void)
357%
358*/
359MagickExport void CacheComponentTerminus(void)
360{
cristy18b17442009-10-25 18:36:48 +0000361 if (cache_semaphore == (SemaphoreInfo *) NULL)
362 AcquireSemaphoreInfo(&cache_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000363 LockSemaphoreInfo(cache_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000364 if (cache_resources != (SplayTreeInfo *) NULL)
365 cache_resources=DestroySplayTree(cache_resources);
366 instantiate_cache=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +0000367 UnlockSemaphoreInfo(cache_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000368 DestroySemaphoreInfo(&cache_semaphore);
369}
370
371/*
372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373% %
374% %
375% %
cristy3ed852e2009-09-05 21:47:34 +0000376+ C l i p P i x e l C a c h e N e x u s %
377% %
378% %
379% %
380%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381%
382% ClipPixelCacheNexus() clips the cache nexus as defined by the image clip
383% mask. The method returns MagickTrue if the pixel region is clipped,
384% otherwise MagickFalse.
385%
386% The format of the ClipPixelCacheNexus() method is:
387%
388% MagickBooleanType ClipPixelCacheNexus(Image *image,NexusInfo *nexus_info,
389% ExceptionInfo *exception)
390%
391% A description of each parameter follows:
392%
393% o image: the image.
394%
395% o nexus_info: the cache nexus to clip.
396%
397% o exception: return any errors or warnings in this structure.
398%
399*/
400static MagickBooleanType ClipPixelCacheNexus(Image *image,
401 NexusInfo *nexus_info,ExceptionInfo *exception)
402{
403 CacheInfo
404 *cache_info;
405
406 MagickSizeType
407 number_pixels;
408
409 NexusInfo
410 **clip_nexus,
411 **image_nexus;
412
413 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000414 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +0000415
416 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000417 *restrict nexus_indexes,
418 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +0000419
420 register long
421 i;
422
423 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000424 *restrict p,
425 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000426
427 /*
428 Apply clip mask.
429 */
430 if (image->debug != MagickFalse)
431 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
432 if (image->clip_mask == (Image *) NULL)
433 return(MagickFalse);
434 cache_info=(CacheInfo *) GetImagePixelCache(image,MagickTrue,exception);
435 if (cache_info == (Cache) NULL)
436 return(MagickFalse);
437 image_nexus=AcquirePixelCacheNexus(1);
438 clip_nexus=AcquirePixelCacheNexus(1);
439 if ((image_nexus == (NexusInfo **) NULL) ||
440 (clip_nexus == (NexusInfo **) NULL))
441 ThrowBinaryException(CacheError,"UnableToGetCacheNexus",image->filename);
442 p=GetAuthenticPixelCacheNexus(image,nexus_info->region.x,nexus_info->region.y,
443 nexus_info->region.width,nexus_info->region.height,image_nexus[0],
444 exception);
445 indexes=GetPixelCacheNexusIndexes(image->cache,image_nexus[0]);
446 q=nexus_info->pixels;
447 nexus_indexes=nexus_info->indexes;
448 r=GetVirtualPixelsFromNexus(image->clip_mask,MaskVirtualPixelMethod,
449 nexus_info->region.x,nexus_info->region.y,nexus_info->region.width,
450 nexus_info->region.height,clip_nexus[0],exception);
451 number_pixels=(MagickSizeType) nexus_info->region.width*
452 nexus_info->region.height;
453 for (i=0; i < (long) number_pixels; i++)
454 {
455 if ((p == (PixelPacket *) NULL) || (r == (const PixelPacket *) NULL))
456 break;
457 if (PixelIntensityToQuantum(r) > ((Quantum) QuantumRange/2))
458 {
cristyce70c172010-01-07 17:15:30 +0000459 SetRedPixelComponent(q,GetRedPixelComponent(p));
460 SetGreenPixelComponent(q,GetGreenPixelComponent(p));
461 SetBluePixelComponent(q,GetBluePixelComponent(p));
462 SetOpacityPixelComponent(q,GetOpacityPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000463 if (cache_info->active_index_channel != MagickFalse)
464 nexus_indexes[i]=indexes[i];
465 }
466 p++;
467 q++;
468 r++;
469 }
470 clip_nexus=DestroyPixelCacheNexus(clip_nexus,1);
471 image_nexus=DestroyPixelCacheNexus(image_nexus,1);
472 if (i < (long) number_pixels)
473 return(MagickFalse);
474 return(MagickTrue);
475}
476
477/*
478%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479% %
480% %
481% %
482+ C l o n e P i x e l C a c h e %
483% %
484% %
485% %
486%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487%
488% ClonePixelCache() clones a pixel cache.
489%
490% The format of the ClonePixelCache() method is:
491%
492% Cache ClonePixelCache(const Cache cache)
493%
494% A description of each parameter follows:
495%
496% o cache: the pixel cache.
497%
498*/
499MagickExport Cache ClonePixelCache(const Cache cache)
500{
501 CacheInfo
502 *clone_info;
503
504 const CacheInfo
505 *cache_info;
506
507 assert(cache != (const Cache) NULL);
508 cache_info=(const CacheInfo *) cache;
509 assert(cache_info->signature == MagickSignature);
510 if (cache_info->debug != MagickFalse)
511 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
512 cache_info->filename);
513 clone_info=(CacheInfo *) AcquirePixelCache(cache_info->number_threads);
514 if (clone_info == (Cache) NULL)
515 return((Cache) NULL);
516 clone_info->virtual_pixel_method=cache_info->virtual_pixel_method;
517 return((Cache ) clone_info);
518}
519
520/*
521%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
522% %
523% %
524% %
525+ C l o n e P i x e l C a c h e N e x u s %
526% %
527% %
528% %
529%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530%
531% ClonePixelCacheNexus() clones the source cache nexus to the destination
532% nexus.
533%
534% The format of the ClonePixelCacheNexus() method is:
535%
536% MagickBooleanType ClonePixelCacheNexus(CacheInfo *destination,
537% CacheInfo *source,ExceptionInfo *exception)
538%
539% A description of each parameter follows:
540%
541% o destination: the destination cache nexus.
542%
543% o source: the source cache nexus.
544%
545% o exception: return any errors or warnings in this structure.
546%
547*/
548
549static inline MagickBooleanType AcquireCacheNexusPixels(CacheInfo *cache_info,
550 NexusInfo *nexus_info,ExceptionInfo *exception)
551{
552 if (nexus_info->length != (MagickSizeType) ((size_t) nexus_info->length))
553 return(MagickFalse);
554 nexus_info->mapped=MagickFalse;
555 nexus_info->cache=(PixelPacket *) AcquireMagickMemory((size_t)
556 nexus_info->length);
557 if (nexus_info->cache == (PixelPacket *) NULL)
558 {
559 nexus_info->mapped=MagickTrue;
560 nexus_info->cache=(PixelPacket *) MapBlob(-1,IOMode,0,(size_t)
561 nexus_info->length);
562 }
563 if (nexus_info->cache == (PixelPacket *) NULL)
564 {
565 (void) ThrowMagickException(exception,GetMagickModule(),
566 ResourceLimitError,"MemoryAllocationFailed","`%s'",
567 cache_info->filename);
568 return(MagickFalse);
569 }
570 return(MagickTrue);
571}
572
573static MagickBooleanType ClonePixelCacheNexus(CacheInfo *destination,
574 CacheInfo *source,ExceptionInfo *exception)
575{
576 MagickBooleanType
577 status;
578
579 MagickSizeType
580 number_pixels;
581
582 register long
583 i;
584
585 register const NexusInfo
586 *p;
587
588 register NexusInfo
589 *q;
590
591 status=MagickTrue;
592 for (i=0; i < (long) source->number_threads; i++)
593 {
594 p=source->nexus_info[i];
595 q=destination->nexus_info[i];
596 q->mapped=p->mapped;
597 q->region=p->region;
598 q->length=p->length;
599 q->cache=p->cache;
600 q->pixels=p->pixels;
601 q->indexes=p->indexes;
602 if (p->cache != (PixelPacket *) NULL)
603 {
604 status=AcquireCacheNexusPixels(source,q,exception);
605 if (status != MagickFalse)
606 {
607 (void) CopyMagickMemory(q->cache,p->cache,(size_t) p->length);
608 q->pixels=q->cache;
609 q->indexes=(IndexPacket *) NULL;
610 number_pixels=(MagickSizeType) q->region.width*q->region.height;
611 if (p->indexes != (IndexPacket *) NULL)
612 q->indexes=(IndexPacket *) (q->pixels+number_pixels);
613 }
614 }
615 }
616 return(status);
617}
618
619/*
620%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621% %
622% %
623% %
cristy60c44a82009-10-07 00:58:49 +0000624+ C l o n e P i x e l C a c h e P i x e l s %
cristy3ed852e2009-09-05 21:47:34 +0000625% %
626% %
627% %
628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
629% ClonePixelCachePixels() clones the source pixel cache to the destination
630% cache.
631%
632% The format of the ClonePixelCachePixels() method is:
633%
634% MagickBooleanType ClonePixelCachePixels(CacheInfo *cache_info,
635% CacheInfo *source_info,ExceptionInfo *exception)
636%
637% A description of each parameter follows:
638%
639% o cache_info: the pixel cache.
640%
641% o source_info: the source pixel cache.
642%
643% o exception: return any errors or warnings in this structure.
644%
645*/
646
647static MagickBooleanType ClosePixelCacheOnDisk(CacheInfo *cache_info)
648{
649 int
650 status;
651
cristy5ee247a2010-02-12 15:42:34 +0000652 status=(-1);
cristyf84a1932010-01-03 18:00:18 +0000653 LockSemaphoreInfo(cache_info->disk_semaphore);
cristy508d9312010-02-10 21:10:30 +0000654 if (cache_info->file != -1)
655 status=close(cache_info->file);
cristy3ed852e2009-09-05 21:47:34 +0000656 cache_info->file=(-1);
657 RelinquishMagickResource(FileResource,1);
cristyf84a1932010-01-03 18:00:18 +0000658 UnlockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000659 return(status == -1 ? MagickFalse : MagickTrue);
660}
661
662static void LimitPixelCacheDescriptors(void)
663{
664 register CacheInfo
665 *p,
666 *q;
667
668 /*
669 Limit # of open file descriptors.
670 */
671 if (GetMagickResource(FileResource) < GetMagickResourceLimit(FileResource))
672 return;
cristyf84a1932010-01-03 18:00:18 +0000673 LockSemaphoreInfo(cache_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000674 if (cache_resources == (SplayTreeInfo *) NULL)
675 {
cristyf84a1932010-01-03 18:00:18 +0000676 UnlockSemaphoreInfo(cache_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000677 return;
678 }
679 ResetSplayTreeIterator(cache_resources);
680 p=(CacheInfo *) GetNextKeyInSplayTree(cache_resources);
681 while (p != (CacheInfo *) NULL)
682 {
683 if ((p->type == DiskCache) && (p->file != -1))
cristy508d9312010-02-10 21:10:30 +0000684 break;
cristy3ed852e2009-09-05 21:47:34 +0000685 p=(CacheInfo *) GetNextKeyInSplayTree(cache_resources);
686 }
687 for (q=p; p != (CacheInfo *) NULL; )
688 {
689 if ((p->type == DiskCache) && (p->file != -1) &&
690 (p->timestamp < q->timestamp))
cristy508d9312010-02-10 21:10:30 +0000691 q=p;
cristy3ed852e2009-09-05 21:47:34 +0000692 p=(CacheInfo *) GetNextKeyInSplayTree(cache_resources);
693 }
694 if (q != (CacheInfo *) NULL)
cristy9e116922010-02-12 20:58:13 +0000695 {
696 /*
697 Close least recently used cache.
698 */
699 (void) close(q->file);
700 q->file=(-1);
701 }
cristyf84a1932010-01-03 18:00:18 +0000702 UnlockSemaphoreInfo(cache_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000703}
704
705static inline MagickSizeType MagickMax(const MagickSizeType x,
706 const MagickSizeType y)
707{
708 if (x > y)
709 return(x);
710 return(y);
711}
712
713static inline MagickSizeType MagickMin(const MagickSizeType x,
714 const MagickSizeType y)
715{
716 if (x < y)
717 return(x);
718 return(y);
719}
720
721static MagickBooleanType OpenPixelCacheOnDisk(CacheInfo *cache_info,
722 const MapMode mode)
723{
724 int
725 file;
726
727 /*
728 Open pixel cache on disk.
729 */
cristyf84a1932010-01-03 18:00:18 +0000730 LockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000731 if (cache_info->file != -1)
732 {
cristyf84a1932010-01-03 18:00:18 +0000733 UnlockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000734 return(MagickTrue); /* cache already open */
735 }
736 LimitPixelCacheDescriptors();
737 if (*cache_info->cache_filename == '\0')
738 file=AcquireUniqueFileResource(cache_info->cache_filename);
739 else
740 switch (mode)
741 {
742 case ReadMode:
743 {
744 file=open(cache_info->cache_filename,O_RDONLY | O_BINARY);
745 break;
746 }
747 case WriteMode:
748 {
749 file=open(cache_info->cache_filename,O_WRONLY | O_CREAT | O_BINARY |
750 O_EXCL,S_MODE);
751 if (file == -1)
752 file=open(cache_info->cache_filename,O_WRONLY | O_BINARY,S_MODE);
753 break;
754 }
755 case IOMode:
756 default:
757 {
758 file=open(cache_info->cache_filename,O_RDWR | O_CREAT | O_BINARY |
759 O_EXCL,S_MODE);
760 if (file == -1)
761 file=open(cache_info->cache_filename,O_RDWR | O_BINARY,S_MODE);
762 break;
763 }
764 }
765 if (file == -1)
766 {
cristyf84a1932010-01-03 18:00:18 +0000767 UnlockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000768 return(MagickFalse);
769 }
770 (void) AcquireMagickResource(FileResource,1);
771 cache_info->file=file;
772 cache_info->timestamp=time(0);
cristyf84a1932010-01-03 18:00:18 +0000773 UnlockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000774 return(MagickTrue);
775}
776
777static inline MagickOffsetType ReadPixelCacheRegion(CacheInfo *cache_info,
778 const MagickOffsetType offset,const MagickSizeType length,
cristyc47d1f82009-11-26 01:44:43 +0000779 unsigned char *restrict buffer)
cristy3ed852e2009-09-05 21:47:34 +0000780{
781 register MagickOffsetType
782 i;
783
784 ssize_t
785 count;
786
cristy08a88202010-03-04 19:18:05 +0000787 cache_info->timestamp=time(0);
cristy3ed852e2009-09-05 21:47:34 +0000788#if !defined(MAGICKCORE_HAVE_PREAD)
cristyf84a1932010-01-03 18:00:18 +0000789 LockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000790 if (MagickSeek(cache_info->file,offset,SEEK_SET) < 0)
791 {
cristyf84a1932010-01-03 18:00:18 +0000792 UnlockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000793 return((MagickOffsetType) -1);
794 }
795#endif
796 count=0;
797 for (i=0; i < (MagickOffsetType) length; i+=count)
798 {
799#if !defined(MAGICKCORE_HAVE_PREAD)
800 count=read(cache_info->file,buffer+i,(size_t) MagickMin(length-i,
801 (MagickSizeType) SSIZE_MAX));
802#else
803 count=pread(cache_info->file,buffer+i,(size_t) MagickMin(length-i,
804 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
805#endif
806 if (count > 0)
807 continue;
808 count=0;
809 if (errno != EINTR)
810 {
811 i=(-1);
812 break;
813 }
814 }
815#if !defined(MAGICKCORE_HAVE_PREAD)
cristyf84a1932010-01-03 18:00:18 +0000816 UnlockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000817#endif
818 return(i);
819}
820
821static inline MagickOffsetType WritePixelCacheRegion(CacheInfo *cache_info,
822 const MagickOffsetType offset,const MagickSizeType length,
cristyc47d1f82009-11-26 01:44:43 +0000823 const unsigned char *restrict buffer)
cristy3ed852e2009-09-05 21:47:34 +0000824{
825 register MagickOffsetType
826 i;
827
828 ssize_t
829 count;
830
cristy08a88202010-03-04 19:18:05 +0000831 cache_info->timestamp=time(0);
cristy3ed852e2009-09-05 21:47:34 +0000832#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyf84a1932010-01-03 18:00:18 +0000833 LockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000834 if (MagickSeek(cache_info->file,offset,SEEK_SET) < 0)
835 {
cristyf84a1932010-01-03 18:00:18 +0000836 UnlockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000837 return((MagickOffsetType) -1);
838 }
839#endif
840 count=0;
841 for (i=0; i < (MagickOffsetType) length; i+=count)
842 {
843#if !defined(MAGICKCORE_HAVE_PWRITE)
844 count=write(cache_info->file,buffer+i,(size_t) MagickMin(length-i,
845 (MagickSizeType) SSIZE_MAX));
846#else
847 count=pwrite(cache_info->file,buffer+i,(size_t) MagickMin(length-i,
848 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
849#endif
850 if (count > 0)
851 continue;
852 count=0;
853 if (errno != EINTR)
854 {
855 i=(-1);
856 break;
857 }
858 }
859#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyf84a1932010-01-03 18:00:18 +0000860 UnlockSemaphoreInfo(cache_info->disk_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000861#endif
862 return(i);
863}
864
865static MagickBooleanType CloneDiskToDiskPixelCache(CacheInfo *clone_info,
866 CacheInfo *cache_info,ExceptionInfo *exception)
867{
868 MagickOffsetType
869 count,
870 offset,
871 source_offset;
872
873 MagickSizeType
874 length;
875
876 register long
877 y;
878
879 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000880 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +0000881
882 unsigned long
883 columns,
884 rows;
885
886 if (cache_info->debug != MagickFalse)
887 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"disk => disk");
888 if (OpenPixelCacheOnDisk(clone_info,IOMode) == MagickFalse)
889 {
890 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
891 clone_info->cache_filename);
892 return(MagickFalse);
893 }
894 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
895 {
896 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
897 cache_info->cache_filename);
898 return(MagickFalse);
899 }
900 columns=(unsigned long) MagickMin(clone_info->columns,cache_info->columns);
901 rows=(unsigned long) MagickMin(clone_info->rows,cache_info->rows);
902 if ((clone_info->active_index_channel != MagickFalse) &&
903 (cache_info->active_index_channel != MagickFalse))
904 {
905 register IndexPacket
906 *indexes;
907
908 /*
909 Clone cache indexes.
910 */
911 length=MagickMax(clone_info->columns,cache_info->columns)*
912 sizeof(*indexes);
913 indexes=(IndexPacket *) AcquireMagickMemory((size_t) length);
914 if (indexes == (IndexPacket *) NULL)
915 {
916 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
917 "MemoryAllocationFailed","`%s'",cache_info->cache_filename);
918 return(MagickFalse);
919 }
920 (void) ResetMagickMemory(indexes,0,(size_t) length);
921 length=columns*sizeof(*indexes);
922 source_offset=(MagickOffsetType) cache_info->columns*cache_info->rows*
923 sizeof(*pixels)+cache_info->columns*rows*sizeof(*indexes);
924 offset=(MagickOffsetType) clone_info->columns*clone_info->rows*
925 sizeof(*pixels)+clone_info->columns*rows*sizeof(*indexes);
926 for (y=0; y < (long) rows; y++)
927 {
928 source_offset-=cache_info->columns*sizeof(*indexes);
929 count=ReadPixelCacheRegion(cache_info,cache_info->offset+source_offset,
930 length,(unsigned char *) indexes);
931 if ((MagickSizeType) count != length)
932 break;
933 offset-=clone_info->columns*sizeof(*indexes);
934 count=WritePixelCacheRegion(clone_info,clone_info->offset+offset,length,
935 (unsigned char *) indexes);
936 if ((MagickSizeType) count != length)
937 break;
938 }
939 if (y < (long) rows)
940 {
941 indexes=(IndexPacket *) RelinquishMagickMemory(indexes);
942 ThrowFileException(exception,CacheError,"UnableToCloneCache",
943 cache_info->cache_filename);
944 return(MagickFalse);
945 }
946 if (clone_info->columns > cache_info->columns)
947 {
948 length=(clone_info->columns-cache_info->columns)*sizeof(*indexes);
949 (void) ResetMagickMemory(indexes,0,(size_t) length);
950 offset=(MagickOffsetType) clone_info->columns*clone_info->rows*
951 sizeof(*pixels)+(clone_info->columns*rows+columns)*sizeof(*indexes);
952 for (y=0; y < (long) rows; y++)
953 {
954 offset-=clone_info->columns*sizeof(*indexes);
955 count=WritePixelCacheRegion(clone_info,clone_info->offset+offset,
956 length,(unsigned char *) indexes);
957 if ((MagickSizeType) count != length)
958 break;
959 }
960 if (y < (long) rows)
961 {
962 indexes=(IndexPacket *) RelinquishMagickMemory(indexes);
963 ThrowFileException(exception,CacheError,"UnableToCloneCache",
964 cache_info->cache_filename);
965 return(MagickFalse);
966 }
967 }
968 indexes=(IndexPacket *) RelinquishMagickMemory(indexes);
969 }
970 /*
971 Clone cache pixels.
972 */
973 length=MagickMax(clone_info->columns,cache_info->columns)*sizeof(*pixels);
974 pixels=(PixelPacket *) AcquireMagickMemory((size_t) length);
975 if (pixels == (PixelPacket *) NULL)
976 {
977 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
978 "MemoryAllocationFailed","`%s'",cache_info->cache_filename);
979 return(MagickFalse);
980 }
981 (void) ResetMagickMemory(pixels,0,(size_t) length);
982 length=columns*sizeof(*pixels);
983 source_offset=(MagickOffsetType) cache_info->columns*rows*sizeof(*pixels);
984 offset=(MagickOffsetType) clone_info->columns*rows*sizeof(*pixels);
985 for (y=0; y < (long) rows; y++)
986 {
987 source_offset-=cache_info->columns*sizeof(*pixels);
988 count=ReadPixelCacheRegion(cache_info,cache_info->offset+source_offset,
989 length,(unsigned char *) pixels);
990 if ((MagickSizeType) count != length)
991 break;
992 offset-=clone_info->columns*sizeof(*pixels);
993 count=WritePixelCacheRegion(clone_info,clone_info->offset+offset,length,
994 (unsigned char *) pixels);
995 if ((MagickSizeType) count != length)
996 break;
997 }
998 if (y < (long) rows)
999 {
1000 pixels=(PixelPacket *) RelinquishMagickMemory(pixels);
1001 ThrowFileException(exception,CacheError,"UnableToCloneCache",
1002 cache_info->cache_filename);
1003 return(MagickFalse);
1004 }
1005 if (clone_info->columns > cache_info->columns)
1006 {
1007 offset=(MagickOffsetType) (clone_info->columns*rows+columns)*
1008 sizeof(*pixels);
1009 length=(clone_info->columns-cache_info->columns)*sizeof(*pixels);
1010 (void) ResetMagickMemory(pixels,0,(size_t) length);
1011 for (y=0; y < (long) rows; y++)
1012 {
1013 offset-=clone_info->columns*sizeof(*pixels);
1014 count=WritePixelCacheRegion(clone_info,clone_info->offset+offset,length,
1015 (unsigned char *) pixels);
1016 if ((MagickSizeType) count != length)
1017 break;
1018 }
1019 if (y < (long) rows)
1020 {
1021 pixels=(PixelPacket *) RelinquishMagickMemory(pixels);
1022 ThrowFileException(exception,CacheError,"UnableToCloneCache",
1023 cache_info->cache_filename);
1024 return(MagickFalse);
1025 }
1026 }
1027 pixels=(PixelPacket *) RelinquishMagickMemory(pixels);
1028 return(MagickTrue);
1029}
1030
1031static MagickBooleanType CloneDiskToMemoryPixelCache(CacheInfo *clone_info,
1032 CacheInfo *cache_info,ExceptionInfo *exception)
1033{
1034 MagickOffsetType
1035 count,
1036 offset;
1037
1038 MagickSizeType
1039 length;
1040
1041 register long
1042 y;
1043
1044 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001045 *restrict pixels,
1046 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001047
1048 unsigned long
1049 columns,
1050 rows;
1051
1052 if (cache_info->debug != MagickFalse)
1053 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"disk => memory");
1054 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
1055 {
1056 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1057 cache_info->cache_filename);
1058 return(MagickFalse);
1059 }
1060 columns=(unsigned long) MagickMin(clone_info->columns,cache_info->columns);
1061 rows=(unsigned long) MagickMin(clone_info->rows,cache_info->rows);
1062 if ((clone_info->active_index_channel != MagickFalse) &&
1063 (cache_info->active_index_channel != MagickFalse))
1064 {
1065 register IndexPacket
1066 *indexes,
1067 *q;
1068
1069 /*
1070 Clone cache indexes.
1071 */
1072 length=MagickMax(clone_info->columns,cache_info->columns)*
1073 sizeof(*indexes);
1074 indexes=(IndexPacket *) AcquireMagickMemory((size_t) length);
1075 if (indexes == (IndexPacket *) NULL)
1076 {
1077 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
1078 "MemoryAllocationFailed","`%s'",cache_info->cache_filename);
1079 return(MagickFalse);
1080 }
1081 (void) ResetMagickMemory(indexes,0,(size_t) length);
1082 length=columns*sizeof(IndexPacket);
1083 offset=(MagickOffsetType) cache_info->columns*cache_info->rows*
1084 sizeof(*pixels)+cache_info->columns*rows*sizeof(*indexes);
1085 q=clone_info->indexes+clone_info->columns*rows;
1086 for (y=0; y < (long) rows; y++)
1087 {
1088 offset-=cache_info->columns*sizeof(IndexPacket);
1089 count=ReadPixelCacheRegion(cache_info,cache_info->offset+offset,
1090 length,(unsigned char *) indexes);
1091 if ((MagickSizeType) count != length)
1092 break;
1093 q-=clone_info->columns;
1094 (void) CopyMagickMemory(q,indexes,(size_t) length);
1095 if ((MagickSizeType) count != length)
1096 break;
1097 }
1098 if (y < (long) rows)
1099 {
1100 indexes=(IndexPacket *) RelinquishMagickMemory(indexes);
1101 ThrowFileException(exception,CacheError,"UnableToCloneCache",
1102 cache_info->cache_filename);
1103 return(MagickFalse);
1104 }
1105 indexes=(IndexPacket *) RelinquishMagickMemory(indexes);
1106 }
1107 /*
1108 Clone cache pixels.
1109 */
1110 length=MagickMax(clone_info->columns,cache_info->columns)*sizeof(*pixels);
1111 pixels=(PixelPacket *) AcquireMagickMemory((size_t) length);
1112 if (pixels == (PixelPacket *) NULL)
1113 {
1114 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
1115 "MemoryAllocationFailed","`%s'",cache_info->cache_filename);
1116 return(MagickFalse);
1117 }
1118 (void) ResetMagickMemory(pixels,0,(size_t) length);
1119 length=columns*sizeof(*pixels);
1120 offset=(MagickOffsetType) cache_info->columns*rows*sizeof(*pixels);
1121 q=clone_info->pixels+clone_info->columns*rows;
1122 for (y=0; y < (long) rows; y++)
1123 {
1124 offset-=cache_info->columns*sizeof(*pixels);
1125 count=ReadPixelCacheRegion(cache_info,cache_info->offset+offset,length,
1126 (unsigned char *) pixels);
1127 if ((MagickSizeType) count != length)
1128 break;
1129 q-=clone_info->columns;
1130 (void) CopyMagickMemory(q,pixels,(size_t) length);
1131 }
1132 if (y < (long) rows)
1133 {
1134 pixels=(PixelPacket *) RelinquishMagickMemory(pixels);
1135 ThrowFileException(exception,CacheError,"UnableToCloneCache",
1136 cache_info->cache_filename);
1137 return(MagickFalse);
1138 }
1139 pixels=(PixelPacket *) RelinquishMagickMemory(pixels);
1140 return(MagickTrue);
1141}
1142
1143static MagickBooleanType CloneMemoryToDiskPixelCache(CacheInfo *clone_info,
1144 CacheInfo *cache_info,ExceptionInfo *exception)
1145{
1146 MagickOffsetType
1147 count,
1148 offset;
1149
1150 MagickSizeType
1151 length;
1152
1153 register long
1154 y;
1155
1156 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001157 *restrict p,
1158 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +00001159
1160 unsigned long
1161 columns,
1162 rows;
1163
1164 if (cache_info->debug != MagickFalse)
1165 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"memory => disk");
1166 if (OpenPixelCacheOnDisk(clone_info,IOMode) == MagickFalse)
1167 {
1168 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1169 clone_info->cache_filename);
1170 return(MagickFalse);
1171 }
1172 columns=(unsigned long) MagickMin(clone_info->columns,cache_info->columns);
1173 rows=(unsigned long) MagickMin(clone_info->rows,cache_info->rows);
1174 if ((clone_info->active_index_channel != MagickFalse) &&
1175 (cache_info->active_index_channel != MagickFalse))
1176 {
1177 register IndexPacket
1178 *p,
1179 *indexes;
1180
1181 /*
1182 Clone cache indexes.
1183 */
1184 length=MagickMax(clone_info->columns,cache_info->columns)*
1185 sizeof(*indexes);
1186 indexes=(IndexPacket *) AcquireMagickMemory((size_t) length);
1187 if (indexes == (IndexPacket *) NULL)
1188 {
1189 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
1190 "MemoryAllocationFailed","`%s'",cache_info->cache_filename);
1191 return(MagickFalse);
1192 }
1193 (void) ResetMagickMemory(indexes,0,(size_t) length);
1194 length=columns*sizeof(*indexes);
1195 p=cache_info->indexes+cache_info->columns*rows;
1196 offset=(MagickOffsetType) clone_info->columns*clone_info->rows*
1197 sizeof(*pixels)+clone_info->columns*rows*sizeof(*indexes);
1198 for (y=0; y < (long) rows; y++)
1199 {
1200 p-=cache_info->columns;
1201 (void) CopyMagickMemory(indexes,p,(size_t) length);
1202 offset-=clone_info->columns*sizeof(*indexes);
1203 count=WritePixelCacheRegion(clone_info,clone_info->offset+offset,length,
1204 (unsigned char *) indexes);
1205 if ((MagickSizeType) count != length)
1206 break;
1207 }
1208 if (y < (long) rows)
1209 {
1210 indexes=(IndexPacket *) RelinquishMagickMemory(indexes);
1211 ThrowFileException(exception,CacheError,"UnableToCloneCache",
1212 cache_info->cache_filename);
1213 return(MagickFalse);
1214 }
1215 if (clone_info->columns > cache_info->columns)
1216 {
1217 length=(clone_info->columns-cache_info->columns)*sizeof(*indexes);
1218 (void) ResetMagickMemory(indexes,0,(size_t) length);
1219 offset=(MagickOffsetType) clone_info->columns*clone_info->rows*
1220 sizeof(*pixels)+(clone_info->columns*rows+columns)*sizeof(*indexes);
1221 for (y=0; y < (long) rows; y++)
1222 {
1223 offset-=clone_info->columns*sizeof(*indexes);
1224 count=WritePixelCacheRegion(clone_info,clone_info->offset+offset,
1225 length,(unsigned char *) indexes);
1226 if ((MagickSizeType) count != length)
1227 break;
1228 }
1229 if (y < (long) rows)
1230 {
1231 indexes=(IndexPacket *) RelinquishMagickMemory(indexes);
1232 ThrowFileException(exception,CacheError,"UnableToCloneCache",
1233 cache_info->cache_filename);
1234 return(MagickFalse);
1235 }
1236 }
1237 indexes=(IndexPacket *) RelinquishMagickMemory(indexes);
1238 }
1239 /*
1240 Clone cache pixels.
1241 */
1242 length=MagickMax(clone_info->columns,cache_info->columns)*sizeof(*pixels);
1243 pixels=(PixelPacket *) AcquireMagickMemory((size_t) length);
1244 if (pixels == (PixelPacket *) NULL)
1245 {
1246 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
1247 "MemoryAllocationFailed","`%s'",cache_info->cache_filename);
1248 return(MagickFalse);
1249 }
1250 (void) ResetMagickMemory(pixels,0,(size_t) length);
1251 length=columns*sizeof(*pixels);
1252 p=cache_info->pixels+cache_info->columns*rows;
1253 offset=(MagickOffsetType) clone_info->columns*rows*sizeof(*pixels);
1254 for (y=0; y < (long) rows; y++)
1255 {
1256 p-=cache_info->columns;
1257 (void) CopyMagickMemory(pixels,p,(size_t) length);
1258 offset-=clone_info->columns*sizeof(*pixels);
1259 count=WritePixelCacheRegion(clone_info,clone_info->offset+offset,length,
1260 (unsigned char *) pixels);
1261 if ((MagickSizeType) count != length)
1262 break;
1263 }
1264 if (y < (long) rows)
1265 {
1266 pixels=(PixelPacket *) RelinquishMagickMemory(pixels);
1267 ThrowFileException(exception,CacheError,"UnableToCloneCache",
1268 cache_info->cache_filename);
1269 return(MagickFalse);
1270 }
1271 if (clone_info->columns > cache_info->columns)
1272 {
1273 offset=(MagickOffsetType) (clone_info->columns*rows+columns)*
1274 sizeof(*pixels);
1275 length=(clone_info->columns-cache_info->columns)*sizeof(*pixels);
1276 (void) ResetMagickMemory(pixels,0,(size_t) length);
1277 for (y=0; y < (long) rows; y++)
1278 {
1279 offset-=clone_info->columns*sizeof(*pixels);
1280 count=WritePixelCacheRegion(clone_info,clone_info->offset+offset,length,
1281 (unsigned char *) pixels);
1282 if ((MagickSizeType) count != length)
1283 break;
1284 }
1285 if (y < (long) rows)
1286 {
1287 pixels=(PixelPacket *) RelinquishMagickMemory(pixels);
1288 ThrowFileException(exception,CacheError,"UnableToCloneCache",
1289 cache_info->cache_filename);
1290 return(MagickFalse);
1291 }
1292 }
1293 pixels=(PixelPacket *) RelinquishMagickMemory(pixels);
1294 return(MagickTrue);
1295}
1296
1297static MagickBooleanType CloneMemoryToMemoryPixelCache(CacheInfo *clone_info,
1298 CacheInfo *cache_info,ExceptionInfo *magick_unused(exception))
1299{
1300 register long
1301 y;
1302
1303 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001304 *restrict pixels,
1305 *restrict source_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001306
1307 size_t
1308 length;
1309
1310 unsigned long
1311 columns,
1312 rows;
1313
1314 if (cache_info->debug != MagickFalse)
1315 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"memory => memory");
1316 columns=(unsigned long) MagickMin(clone_info->columns,cache_info->columns);
1317 rows=(unsigned long) MagickMin(clone_info->rows,cache_info->rows);
1318 if ((clone_info->active_index_channel != MagickFalse) &&
1319 (cache_info->active_index_channel != MagickFalse))
1320 {
1321 register IndexPacket
1322 *indexes,
1323 *source_indexes;
1324
1325 /*
1326 Clone cache indexes.
1327 */
1328 length=columns*sizeof(*indexes);
1329 if (clone_info->columns == cache_info->columns)
1330 (void) CopyMagickMemory(clone_info->indexes,cache_info->indexes,
1331 length*rows);
1332 else
1333 {
1334 source_indexes=cache_info->indexes+cache_info->columns*rows;
1335 indexes=clone_info->indexes+clone_info->columns*rows;
1336 for (y=0; y < (long) rows; y++)
1337 {
1338 source_indexes-=cache_info->columns;
1339 indexes-=clone_info->columns;
1340 (void) CopyMagickMemory(indexes,source_indexes,length);
1341 }
1342 if (clone_info->columns > cache_info->columns)
1343 {
1344 length=(clone_info->columns-cache_info->columns)*
1345 sizeof(*indexes);
1346 indexes=clone_info->indexes+clone_info->columns*rows+
1347 cache_info->columns;
1348 for (y=0; y < (long) rows; y++)
1349 {
1350 indexes-=clone_info->columns;
1351 (void) ResetMagickMemory(indexes,0,length);
1352 }
1353 }
1354 }
1355 }
1356 /*
1357 Clone cache pixels.
1358 */
1359 length=columns*sizeof(*pixels);
1360 if (clone_info->columns == cache_info->columns)
1361 (void) CopyMagickMemory(clone_info->pixels,cache_info->pixels,length*rows);
1362 else
1363 {
1364 source_pixels=cache_info->pixels+cache_info->columns*rows;
1365 pixels=clone_info->pixels+clone_info->columns*rows;
1366 for (y=0; y < (long) rows; y++)
1367 {
1368 source_pixels-=cache_info->columns;
1369 pixels-=clone_info->columns;
1370 (void) CopyMagickMemory(pixels,source_pixels,length);
1371 }
1372 if (clone_info->columns > cache_info->columns)
1373 {
1374 length=(clone_info->columns-cache_info->columns)*sizeof(*pixels);
1375 pixels=clone_info->pixels+clone_info->columns*rows+
1376 cache_info->columns;
1377 for (y=0; y < (long) rows; y++)
1378 {
1379 pixels-=clone_info->columns;
1380 (void) ResetMagickMemory(pixels,0,length);
1381 }
1382 }
1383 }
1384 return(MagickTrue);
1385}
1386
1387static MagickBooleanType ClonePixelCachePixels(CacheInfo *clone_info,
1388 CacheInfo *cache_info,ExceptionInfo *exception)
1389{
1390 if ((clone_info->type != DiskCache) && (cache_info->type != DiskCache))
1391 return(CloneMemoryToMemoryPixelCache(clone_info,cache_info,exception));
1392 if ((clone_info->type == DiskCache) && (cache_info->type == DiskCache))
1393 return(CloneDiskToDiskPixelCache(clone_info,cache_info,exception));
1394 if (cache_info->type == DiskCache)
1395 return(CloneDiskToMemoryPixelCache(clone_info,cache_info,exception));
1396 return(CloneMemoryToDiskPixelCache(clone_info,cache_info,exception));
1397}
1398
1399/*
1400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401% %
1402% %
1403% %
1404+ C l o n e P i x e l C a c h e M e t h o d s %
1405% %
1406% %
1407% %
1408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409%
1410% ClonePixelCacheMethods() clones the pixel cache methods from one cache to
1411% another.
1412%
1413% The format of the ClonePixelCacheMethods() method is:
1414%
1415% void ClonePixelCacheMethods(Cache clone,const Cache cache)
1416%
1417% A description of each parameter follows:
1418%
1419% o clone: Specifies a pointer to a Cache structure.
1420%
1421% o cache: the pixel cache.
1422%
1423*/
1424MagickExport void ClonePixelCacheMethods(Cache clone,const Cache cache)
1425{
1426 CacheInfo
1427 *cache_info,
1428 *source_info;
1429
1430 assert(clone != (Cache) NULL);
1431 source_info=(CacheInfo *) clone;
1432 assert(source_info->signature == MagickSignature);
1433 if (source_info->debug != MagickFalse)
1434 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1435 source_info->filename);
1436 assert(cache != (Cache) NULL);
1437 cache_info=(CacheInfo *) cache;
1438 assert(cache_info->signature == MagickSignature);
1439 source_info->methods=cache_info->methods;
1440}
1441
1442/*
1443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1444% %
1445% %
1446% %
1447+ D e s t r o y I m a g e P i x e l C a c h e %
1448% %
1449% %
1450% %
1451%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1452%
1453% DestroyImagePixelCache() deallocates memory associated with the pixel cache.
1454%
1455% The format of the DestroyImagePixelCache() method is:
1456%
1457% void DestroyImagePixelCache(Image *image)
1458%
1459% A description of each parameter follows:
1460%
1461% o image: the image.
1462%
1463*/
1464static void DestroyImagePixelCache(Image *image)
1465{
1466 assert(image != (Image *) NULL);
1467 assert(image->signature == MagickSignature);
1468 if (image->debug != MagickFalse)
1469 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1470 if (image->cache == (void *) NULL)
1471 return;
1472 image->cache=DestroyPixelCache(image->cache);
1473}
1474
1475/*
1476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477% %
1478% %
1479% %
1480+ D e s t r o y I m a g e P i x e l s %
1481% %
1482% %
1483% %
1484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485%
1486% DestroyImagePixels() deallocates memory associated with the pixel cache.
1487%
1488% The format of the DestroyImagePixels() method is:
1489%
1490% void DestroyImagePixels(Image *image)
1491%
1492% A description of each parameter follows:
1493%
1494% o image: the image.
1495%
1496*/
1497MagickExport void DestroyImagePixels(Image *image)
1498{
1499 CacheInfo
1500 *cache_info;
1501
1502 assert(image != (const Image *) NULL);
1503 assert(image->signature == MagickSignature);
1504 if (image->debug != MagickFalse)
1505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1506 assert(image->cache != (Cache) NULL);
1507 cache_info=(CacheInfo *) image->cache;
1508 assert(cache_info->signature == MagickSignature);
1509 if (cache_info->methods.destroy_pixel_handler == (DestroyPixelHandler) NULL)
1510 return;
1511 cache_info->methods.destroy_pixel_handler(image);
1512}
1513
1514/*
1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1516% %
1517% %
1518% %
1519+ D e s t r o y P i x e l C a c h e %
1520% %
1521% %
1522% %
1523%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1524%
1525% DestroyPixelCache() deallocates memory associated with the pixel cache.
1526%
1527% The format of the DestroyPixelCache() method is:
1528%
1529% Cache DestroyPixelCache(Cache cache)
1530%
1531% A description of each parameter follows:
1532%
1533% o cache: the pixel cache.
1534%
1535*/
1536
1537static inline void RelinquishPixelCachePixels(CacheInfo *cache_info)
1538{
1539 switch (cache_info->type)
1540 {
1541 case MemoryCache:
1542 {
1543 if (cache_info->mapped == MagickFalse)
1544 cache_info->pixels=(PixelPacket *) RelinquishMagickMemory(
1545 cache_info->pixels);
1546 else
1547 cache_info->pixels=(PixelPacket *) UnmapBlob(cache_info->pixels,
1548 (size_t) cache_info->length);
1549 RelinquishMagickResource(MemoryResource,cache_info->length);
1550 break;
1551 }
1552 case MapCache:
1553 {
1554 cache_info->pixels=(PixelPacket *) UnmapBlob(cache_info->pixels,(size_t)
1555 cache_info->length);
1556 RelinquishMagickResource(MapResource,cache_info->length);
1557 }
1558 case DiskCache:
1559 {
1560 if (cache_info->file != -1)
1561 (void) ClosePixelCacheOnDisk(cache_info);
1562 RelinquishMagickResource(DiskResource,cache_info->length);
1563 break;
1564 }
1565 default:
1566 break;
1567 }
1568 cache_info->type=UndefinedCache;
1569 cache_info->mapped=MagickFalse;
1570 cache_info->indexes=(IndexPacket *) NULL;
1571}
1572
1573MagickExport Cache DestroyPixelCache(Cache cache)
1574{
1575 CacheInfo
1576 *cache_info;
1577
cristy3ed852e2009-09-05 21:47:34 +00001578 assert(cache != (Cache) NULL);
1579 cache_info=(CacheInfo *) cache;
1580 assert(cache_info->signature == MagickSignature);
1581 if (cache_info->debug != MagickFalse)
1582 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1583 cache_info->filename);
cristyf84a1932010-01-03 18:00:18 +00001584 LockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001585 cache_info->reference_count--;
1586 if (cache_info->reference_count != 0)
1587 {
cristyf84a1932010-01-03 18:00:18 +00001588 UnlockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001589 return((Cache) NULL);
1590 }
cristyf84a1932010-01-03 18:00:18 +00001591 UnlockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001592 if (cache_resources != (SplayTreeInfo *) NULL)
1593 (void) DeleteNodeByValueFromSplayTree(cache_resources,cache_info);
cristy5b8de732009-09-10 23:50:40 +00001594 if (cache_info->debug != MagickFalse)
1595 {
1596 char
1597 message[MaxTextExtent];
1598
1599 (void) FormatMagickString(message,MaxTextExtent,"destroy %s",
1600 cache_info->filename);
1601 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",message);
1602 }
cristyc2e1bdd2009-09-10 23:43:34 +00001603 if ((cache_info->mode == ReadMode) || ((cache_info->type != MapCache) &&
1604 (cache_info->type != DiskCache)))
1605 RelinquishPixelCachePixels(cache_info);
1606 else
1607 {
1608 RelinquishPixelCachePixels(cache_info);
1609 (void) RelinquishUniqueFileResource(cache_info->cache_filename);
1610 }
cristy3ed852e2009-09-05 21:47:34 +00001611 *cache_info->cache_filename='\0';
1612 if (cache_info->nexus_info != (NexusInfo **) NULL)
1613 cache_info->nexus_info=DestroyPixelCacheNexus(cache_info->nexus_info,
1614 cache_info->number_threads);
cristy3ed852e2009-09-05 21:47:34 +00001615 if (cache_info->random_info != (RandomInfo *) NULL)
1616 cache_info->random_info=DestroyRandomInfo(cache_info->random_info);
cristy3ed852e2009-09-05 21:47:34 +00001617 if (cache_info->disk_semaphore != (SemaphoreInfo *) NULL)
1618 DestroySemaphoreInfo(&cache_info->disk_semaphore);
1619 if (cache_info->semaphore != (SemaphoreInfo *) NULL)
1620 DestroySemaphoreInfo(&cache_info->semaphore);
cristy154a1e22010-02-15 00:28:37 +00001621 cache_info->signature=(~MagickSignature);
cristy3ed852e2009-09-05 21:47:34 +00001622 cache_info=(CacheInfo *) RelinquishAlignedMemory(cache_info);
1623 cache=(Cache) NULL;
1624 return(cache);
1625}
1626
1627/*
1628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1629% %
1630% %
1631% %
1632+ D e s t r o y P i x e l C a c h e N e x u s %
1633% %
1634% %
1635% %
1636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1637%
1638% DestroyPixelCacheNexus() destroys a pixel cache nexus.
1639%
1640% The format of the DestroyPixelCacheNexus() method is:
1641%
1642% NexusInfo **DestroyPixelCacheNexus(NexusInfo *nexus_info,
1643% const unsigned long number_threads)
1644%
1645% A description of each parameter follows:
1646%
1647% o nexus_info: the nexus to destroy.
1648%
1649% o number_threads: the number of nexus threads.
1650%
1651*/
1652
1653static inline void RelinquishCacheNexusPixels(NexusInfo *nexus_info)
1654{
1655 if (nexus_info->mapped == MagickFalse)
1656 (void) RelinquishMagickMemory(nexus_info->cache);
1657 else
1658 (void) UnmapBlob(nexus_info->cache,(size_t) nexus_info->length);
1659 nexus_info->cache=(PixelPacket *) NULL;
1660 nexus_info->pixels=(PixelPacket *) NULL;
1661 nexus_info->indexes=(IndexPacket *) NULL;
1662 nexus_info->length=0;
1663 nexus_info->mapped=MagickFalse;
1664}
1665
1666MagickExport NexusInfo **DestroyPixelCacheNexus(NexusInfo **nexus_info,
1667 const unsigned long number_threads)
1668{
1669 register long
1670 i;
1671
1672 assert(nexus_info != (NexusInfo **) NULL);
1673 for (i=0; i < (long) number_threads; i++)
1674 {
1675 if (nexus_info[i]->cache != (PixelPacket *) NULL)
1676 RelinquishCacheNexusPixels(nexus_info[i]);
1677 nexus_info[i]->signature=(~MagickSignature);
1678 nexus_info[i]=(NexusInfo *) RelinquishAlignedMemory(nexus_info[i]);
1679 }
1680 nexus_info=(NexusInfo **) RelinquishAlignedMemory(nexus_info);
1681 return(nexus_info);
1682}
1683
1684/*
1685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1686% %
1687% %
1688% %
cristy3ed852e2009-09-05 21:47:34 +00001689+ G e t A u t h e n t i c I n d e x e s F r o m C a c h e %
1690% %
1691% %
1692% %
1693%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1694%
1695% GetAuthenticIndexesFromCache() returns the indexes associated with the last
1696% call to QueueAuthenticPixelsCache() or GetAuthenticPixelsCache().
1697%
1698% The format of the GetAuthenticIndexesFromCache() method is:
1699%
1700% IndexPacket *GetAuthenticIndexesFromCache(const Image *image)
1701%
1702% A description of each parameter follows:
1703%
1704% o image: the image.
1705%
1706*/
1707static IndexPacket *GetAuthenticIndexesFromCache(const Image *image)
1708{
1709 CacheInfo
1710 *cache_info;
1711
1712 IndexPacket
1713 *indexes;
1714
1715 long
1716 id;
1717
1718 if (image->debug != MagickFalse)
1719 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1720 cache_info=(CacheInfo *) image->cache;
1721 id=GetOpenMPThreadId();
1722 assert(id < (long) cache_info->number_threads);
1723 indexes=GetPixelCacheNexusIndexes(image->cache,cache_info->nexus_info[id]);
1724 return(indexes);
1725}
1726
1727/*
1728%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1729% %
1730% %
1731% %
1732% G e t A u t h e n t i c I n d e x Q u e u e %
1733% %
1734% %
1735% %
1736%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1737%
1738% GetAuthenticIndexQueue() returns the authentic black channel or the colormap
1739% indexes associated with the last call to QueueAuthenticPixels() or
1740% GetVirtualPixels(). NULL is returned if the black channel or colormap
1741% indexes are not available.
1742%
1743% The format of the GetAuthenticIndexQueue() method is:
1744%
1745% IndexPacket *GetAuthenticIndexQueue(const Image *image)
1746%
1747% A description of each parameter follows:
1748%
1749% o image: the image.
1750%
1751*/
1752MagickExport IndexPacket *GetAuthenticIndexQueue(const Image *image)
1753{
1754 CacheInfo
1755 *cache_info;
1756
1757 assert(image != (const Image *) NULL);
1758 assert(image->signature == MagickSignature);
1759 if (image->debug != MagickFalse)
1760 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1761 assert(image->cache != (Cache) NULL);
1762 cache_info=(CacheInfo *) image->cache;
1763 assert(cache_info->signature == MagickSignature);
1764 if (cache_info->methods.get_authentic_indexes_from_handler ==
1765 (GetAuthenticIndexesFromHandler) NULL)
1766 return((IndexPacket *) NULL);
1767 return(cache_info->methods.get_authentic_indexes_from_handler(image));
1768}
1769
1770/*
1771%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1772% %
1773% %
1774% %
1775+ 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 %
1776% %
1777% %
1778% %
1779%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1780%
1781% GetAuthenticPixelCacheNexus() gets authentic pixels from the in-memory or
1782% disk pixel cache as defined by the geometry parameters. A pointer to the
1783% pixels is returned if the pixels are transferred, otherwise a NULL is
1784% returned.
1785%
1786% The format of the GetAuthenticPixelCacheNexus() method is:
1787%
1788% PixelPacket *GetAuthenticPixelCacheNexus(Image *image,const long x,
1789% const long y,const unsigned long columns,const unsigned long rows,
1790% NexusInfo *nexus_info,ExceptionInfo *exception)
1791%
1792% A description of each parameter follows:
1793%
1794% o image: the image.
1795%
1796% o x,y,columns,rows: These values define the perimeter of a region of
1797% pixels.
1798%
1799% o nexus_info: the cache nexus to return.
1800%
1801% o exception: return any errors or warnings in this structure.
1802%
1803*/
1804
1805static inline MagickBooleanType IsNexusInCore(const CacheInfo *cache_info,
1806 NexusInfo *nexus_info)
1807{
1808 MagickOffsetType
1809 offset;
1810
1811 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
1812 nexus_info->region.x;
1813 if (nexus_info->pixels != (cache_info->pixels+offset))
1814 return(MagickFalse);
1815 return(MagickTrue);
1816}
1817
1818MagickExport PixelPacket *GetAuthenticPixelCacheNexus(Image *image,const long x,
1819 const long y,const unsigned long columns,const unsigned long rows,
1820 NexusInfo *nexus_info,ExceptionInfo *exception)
1821{
1822 CacheInfo
1823 *cache_info;
1824
1825 PixelPacket
1826 *pixels;
1827
1828 /*
1829 Transfer pixels from the cache.
1830 */
1831 assert(image != (Image *) NULL);
1832 assert(image->signature == MagickSignature);
1833 if (image->debug != MagickFalse)
1834 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1835 pixels=QueueAuthenticNexus(image,x,y,columns,rows,nexus_info,exception);
1836 if (pixels == (PixelPacket *) NULL)
1837 return((PixelPacket *) NULL);
1838 cache_info=(CacheInfo *) image->cache;
1839 assert(cache_info->signature == MagickSignature);
1840 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
1841 return(pixels);
1842 if (ReadPixelCachePixels(cache_info,nexus_info,exception) == MagickFalse)
1843 return((PixelPacket *) NULL);
1844 if (cache_info->active_index_channel != MagickFalse)
1845 if (ReadPixelCacheIndexes(cache_info,nexus_info,exception) == MagickFalse)
1846 return((PixelPacket *) NULL);
1847 return(pixels);
1848}
1849
1850/*
1851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1852% %
1853% %
1854% %
1855+ 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 %
1856% %
1857% %
1858% %
1859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1860%
1861% GetAuthenticPixelsFromCache() returns the pixels associated with the last
1862% call to the QueueAuthenticPixelsCache() or GetAuthenticPixelsCache() methods.
1863%
1864% The format of the GetAuthenticPixelsFromCache() method is:
1865%
1866% PixelPacket *GetAuthenticPixelsFromCache(const Image image)
1867%
1868% A description of each parameter follows:
1869%
1870% o image: the image.
1871%
1872*/
1873static PixelPacket *GetAuthenticPixelsFromCache(const Image *image)
1874{
1875 CacheInfo
1876 *cache_info;
1877
1878 long
1879 id;
1880
1881 PixelPacket
1882 *pixels;
1883
1884 if (image->debug != MagickFalse)
1885 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1886 cache_info=(CacheInfo *) image->cache;
1887 id=GetOpenMPThreadId();
1888 assert(id < (long) cache_info->number_threads);
1889 pixels=GetPixelCacheNexusPixels(image->cache,cache_info->nexus_info[id]);
1890 return(pixels);
1891}
1892
1893/*
1894%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1895% %
1896% %
1897% %
1898% G e t A u t h e n t i c P i x e l Q u e u e %
1899% %
1900% %
1901% %
1902%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1903%
1904% GetAuthenticPixelQueue() returns the authentic pixels associated with the
1905% last call to QueueAuthenticPixels() or GetAuthenticPixels().
1906%
1907% The format of the GetAuthenticPixelQueue() method is:
1908%
1909% PixelPacket *GetAuthenticPixelQueue(const Image image)
1910%
1911% A description of each parameter follows:
1912%
1913% o image: the image.
1914%
1915*/
1916MagickExport PixelPacket *GetAuthenticPixelQueue(const Image *image)
1917{
1918 CacheInfo
1919 *cache_info;
1920
1921 assert(image != (const Image *) NULL);
1922 assert(image->signature == MagickSignature);
1923 if (image->debug != MagickFalse)
1924 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1925 assert(image->cache != (Cache) NULL);
1926 cache_info=(CacheInfo *) image->cache;
1927 assert(cache_info->signature == MagickSignature);
1928 if (cache_info->methods.get_authentic_pixels_from_handler ==
1929 (GetAuthenticPixelsFromHandler) NULL)
1930 return((PixelPacket *) NULL);
1931 return(cache_info->methods.get_authentic_pixels_from_handler(image));
1932}
1933
1934/*
1935%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1936% %
1937% %
1938% %
1939% G e t A u t h e n t i c P i x e l s %
1940% %
1941% %
1942% %
1943%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1944%
1945% GetAuthenticPixels() obtains a pixel region for read/write access. If the
1946% region is successfully accessed, a pointer to a PixelPacket array
1947% representing the region is returned, otherwise NULL is returned.
1948%
1949% The returned pointer may point to a temporary working copy of the pixels
1950% or it may point to the original pixels in memory. Performance is maximized
1951% if the selected region is part of one row, or one or more full rows, since
1952% then there is opportunity to access the pixels in-place (without a copy)
cristyad4e5b22010-01-10 00:04:19 +00001953% if the image is in memory, or in a memory-mapped file. The returned pointer
1954% must *never* be deallocated by the user.
cristy3ed852e2009-09-05 21:47:34 +00001955%
1956% Pixels accessed via the returned pointer represent a simple array of type
1957% PixelPacket. If the image type is CMYK or if the storage class is
1958% PseduoClass, call GetAuthenticIndexQueue() after invoking
1959% GetAuthenticPixels() to obtain the black color component or colormap indexes
1960% (of type IndexPacket) corresponding to the region. Once the PixelPacket
1961% (and/or IndexPacket) array has been updated, the changes must be saved back
1962% to the underlying image using SyncAuthenticPixels() or they may be lost.
1963%
1964% The format of the GetAuthenticPixels() method is:
1965%
1966% PixelPacket *GetAuthenticPixels(Image *image,const long x,const long y,
1967% const unsigned long columns,const unsigned long rows,
1968% ExceptionInfo *exception)
1969%
1970% A description of each parameter follows:
1971%
1972% o image: the image.
1973%
1974% o x,y,columns,rows: These values define the perimeter of a region of
1975% pixels.
1976%
1977% o exception: return any errors or warnings in this structure.
1978%
1979*/
1980MagickExport PixelPacket *GetAuthenticPixels(Image *image,const long x,
1981 const long y,const unsigned long columns,const unsigned long rows,
1982 ExceptionInfo *exception)
1983{
1984 CacheInfo
1985 *cache_info;
1986
1987 PixelPacket
1988 *pixels;
1989
1990 assert(image != (Image *) NULL);
1991 assert(image->signature == MagickSignature);
1992 if (image->debug != MagickFalse)
1993 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1994 assert(image->cache != (Cache) NULL);
1995 cache_info=(CacheInfo *) image->cache;
1996 assert(cache_info->signature == MagickSignature);
1997 if (cache_info->methods.get_authentic_pixels_handler ==
1998 (GetAuthenticPixelsHandler) NULL)
1999 return((PixelPacket *) NULL);
2000 pixels=cache_info->methods.get_authentic_pixels_handler(image,x,y,columns,
2001 rows,exception);
2002 return(pixels);
2003}
2004
2005/*
2006%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2007% %
2008% %
2009% %
2010+ G e t A u t h e n t i c P i x e l s C a c h e %
2011% %
2012% %
2013% %
2014%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2015%
2016% GetAuthenticPixelsCache() gets pixels from the in-memory or disk pixel cache
2017% as defined by the geometry parameters. A pointer to the pixels is returned
2018% if the pixels are transferred, otherwise a NULL is returned.
2019%
2020% The format of the GetAuthenticPixelsCache() method is:
2021%
2022% PixelPacket *GetAuthenticPixelsCache(Image *image,const long x,
2023% const long y,const unsigned long columns,const unsigned long rows,
2024% ExceptionInfo *exception)
2025%
2026% A description of each parameter follows:
2027%
2028% o image: the image.
2029%
2030% o x,y,columns,rows: These values define the perimeter of a region of
2031% pixels.
2032%
2033% o exception: return any errors or warnings in this structure.
2034%
2035*/
2036static PixelPacket *GetAuthenticPixelsCache(Image *image,const long x,
2037 const long y,const unsigned long columns,const unsigned long rows,
2038 ExceptionInfo *exception)
2039{
2040 CacheInfo
2041 *cache_info;
2042
2043 long
2044 id;
2045
2046 PixelPacket
2047 *pixels;
2048
2049 cache_info=(CacheInfo *) GetImagePixelCache(image,MagickTrue,exception);
2050 if (cache_info == (Cache) NULL)
2051 return((PixelPacket *) NULL);
2052 id=GetOpenMPThreadId();
2053 assert(id < (long) cache_info->number_threads);
2054 pixels=GetAuthenticPixelCacheNexus(image,x,y,columns,rows,
2055 cache_info->nexus_info[id],exception);
2056 return(pixels);
2057}
2058
2059/*
2060%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2061% %
2062% %
2063% %
2064+ G e t I m a g e E x t e n t %
2065% %
2066% %
2067% %
2068%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2069%
2070% GetImageExtent() returns the extent of the pixels associated with the
2071% last call to QueueAuthenticPixels() or GetAuthenticPixels().
2072%
2073% The format of the GetImageExtent() method is:
2074%
2075% MagickSizeType GetImageExtent(const Image *image)
2076%
2077% A description of each parameter follows:
2078%
2079% o image: the image.
2080%
2081*/
2082MagickExport MagickSizeType GetImageExtent(const Image *image)
2083{
2084 CacheInfo
2085 *cache_info;
2086
2087 long
2088 id;
2089
2090 MagickSizeType
2091 extent;
2092
2093 assert(image != (Image *) NULL);
2094 assert(image->signature == MagickSignature);
2095 if (image->debug != MagickFalse)
2096 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2097 assert(image->cache != (Cache) NULL);
2098 cache_info=(CacheInfo *) image->cache;
2099 assert(cache_info->signature == MagickSignature);
2100 id=GetOpenMPThreadId();
2101 assert(id < (long) cache_info->number_threads);
2102 extent=GetPixelCacheNexusExtent(image->cache,cache_info->nexus_info[id]);
2103 return(extent);
2104}
2105
2106/*
2107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2108% %
2109% %
2110% %
2111+ G e t I m a g e P i x e l C a c h e %
2112% %
2113% %
2114% %
2115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2116%
2117% GetImagePixelCache() ensures that there is only a single reference to the
2118% pixel cache to be modified, updating the provided cache pointer to point to
2119% a clone of the original pixel cache if necessary.
2120%
2121% The format of the GetImagePixelCache method is:
2122%
2123% Cache GetImagePixelCache(Image *image,const MagickBooleanType clone,
2124% ExceptionInfo *exception)
2125%
2126% A description of each parameter follows:
2127%
2128% o image: the image.
2129%
2130% o clone: any value other than MagickFalse clones the cache pixels.
2131%
2132% o exception: return any errors or warnings in this structure.
2133%
2134*/
2135
2136static inline MagickBooleanType ValidatePixelCacheMorphology(const Image *image)
2137{
2138 CacheInfo
2139 *cache_info;
2140
2141 /*
2142 Does the image match the pixel cache morphology?
2143 */
2144 cache_info=(CacheInfo *) image->cache;
2145 if ((image->storage_class != cache_info->storage_class) ||
2146 (image->colorspace != cache_info->colorspace) ||
2147 (image->columns != cache_info->columns) ||
2148 (image->rows != cache_info->rows) ||
2149 (cache_info->nexus_info == (NexusInfo **) NULL) ||
2150 (cache_info->number_threads < GetOpenMPMaximumThreads()))
2151 return(MagickFalse);
2152 return(MagickTrue);
2153}
2154
2155MagickExport Cache GetImagePixelCache(Image *image,
2156 const MagickBooleanType clone,ExceptionInfo *exception)
2157{
2158 CacheInfo
2159 *cache_info;
2160
cristy3ed852e2009-09-05 21:47:34 +00002161 MagickBooleanType
cristy4320e0e2009-09-10 15:00:08 +00002162 destroy,
cristy3ed852e2009-09-05 21:47:34 +00002163 status;
2164
cristy50a10922010-02-15 18:35:25 +00002165 static MagickSizeType
2166 time_limit = 0;
2167
cristy3ed852e2009-09-05 21:47:34 +00002168 if (image->debug != MagickFalse)
2169 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy50a10922010-02-15 18:35:25 +00002170 if (time_limit == 0)
2171 time_limit=GetMagickResourceLimit(TimeResource);
2172 if (time_limit != MagickResourceInfinity)
2173 {
2174 static time_t
2175 cache_timer = 0;
2176
2177 if (cache_timer == 0)
2178 cache_timer=time((time_t *) NULL);
2179 if ((MagickSizeType) (time((time_t *) NULL)-cache_timer) >= time_limit)
2180 ThrowFatalException(ResourceLimitFatalError,"TimeLimitExceeded");
2181 }
cristyc4f9f132010-03-04 18:50:01 +00002182 status=MagickTrue;
2183 LockSemaphoreInfo(image->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00002184 assert(image->cache != (Cache) NULL);
2185 cache_info=(CacheInfo *) image->cache;
cristy4320e0e2009-09-10 15:00:08 +00002186 destroy=MagickFalse;
cristy01b7eb02009-09-10 23:10:14 +00002187 if ((cache_info->reference_count > 1) || (cache_info->mode == ReadMode))
cristy3ed852e2009-09-05 21:47:34 +00002188 {
cristyaaa0cb62010-02-15 17:47:27 +00002189 LockSemaphoreInfo(cache_info->semaphore);
2190 if ((cache_info->reference_count > 1) || (cache_info->mode == ReadMode))
cristy3ed852e2009-09-05 21:47:34 +00002191 {
cristyaaa0cb62010-02-15 17:47:27 +00002192 Image
2193 clone_image;
2194
2195 CacheInfo
2196 *clone_info;
2197
2198 /*
2199 Clone pixel cache.
2200 */
2201 clone_image=(*image);
2202 clone_image.cache=ClonePixelCache(cache_info);
2203 clone_info=(CacheInfo *) clone_image.cache;
2204 status=ClonePixelCacheNexus(cache_info,clone_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00002205 if (status != MagickFalse)
2206 {
cristyaaa0cb62010-02-15 17:47:27 +00002207 status=OpenPixelCache(&clone_image,IOMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002208 if (status != MagickFalse)
2209 {
cristyaaa0cb62010-02-15 17:47:27 +00002210 if (clone != MagickFalse)
2211 status=ClonePixelCachePixels(clone_info,cache_info,
2212 exception);
2213 if (status != MagickFalse)
2214 {
2215 destroy=MagickTrue;
2216 image->cache=clone_image.cache;
2217 }
cristy3ed852e2009-09-05 21:47:34 +00002218 }
2219 }
2220 }
cristyaaa0cb62010-02-15 17:47:27 +00002221 UnlockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00002222 }
cristy4320e0e2009-09-10 15:00:08 +00002223 if (destroy != MagickFalse)
cristyf2e11662009-10-14 01:24:43 +00002224 cache_info=(CacheInfo *) DestroyPixelCache(cache_info);
cristy3ed852e2009-09-05 21:47:34 +00002225 if (status != MagickFalse)
2226 {
2227 /*
2228 Ensure the image matches the pixel cache morphology.
2229 */
2230 image->taint=MagickTrue;
2231 image->type=UndefinedType;
2232 if (image->colorspace == GRAYColorspace)
2233 image->colorspace=RGBColorspace;
2234 if (ValidatePixelCacheMorphology(image) == MagickFalse)
2235 status=OpenPixelCache(image,IOMode,exception);
2236 }
cristyf84a1932010-01-03 18:00:18 +00002237 UnlockSemaphoreInfo(image->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00002238 if (status == MagickFalse)
2239 return((Cache) NULL);
2240 return(image->cache);
2241}
2242
2243/*
2244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2245% %
2246% %
2247% %
2248% G e t O n e A u t h e n t i c P i x e l %
2249% %
2250% %
2251% %
2252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2253%
2254% GetOneAuthenticPixel() returns a single pixel at the specified (x,y)
2255% location. The image background color is returned if an error occurs.
2256%
2257% The format of the GetOneAuthenticPixel() method is:
2258%
2259% MagickBooleanType GetOneAuthenticPixel(const Image image,const long x,
2260% const long y,PixelPacket *pixel,ExceptionInfo *exception)
2261%
2262% A description of each parameter follows:
2263%
2264% o image: the image.
2265%
2266% o x,y: These values define the location of the pixel to return.
2267%
2268% o pixel: return a pixel at the specified (x,y) location.
2269%
2270% o exception: return any errors or warnings in this structure.
2271%
2272*/
2273MagickExport MagickBooleanType GetOneAuthenticPixel(Image *image,const long x,
2274 const long y,PixelPacket *pixel,ExceptionInfo *exception)
2275{
2276 CacheInfo
2277 *cache_info;
2278
2279 GetOneAuthenticPixelFromHandler
2280 get_one_authentic_pixel_from_handler;
2281
2282 MagickBooleanType
2283 status;
2284
2285 assert(image != (Image *) NULL);
2286 assert(image->signature == MagickSignature);
2287 if (image->debug != MagickFalse)
2288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2289 assert(image->cache != (Cache) NULL);
2290 cache_info=(CacheInfo *) image->cache;
2291 assert(cache_info->signature == MagickSignature);
2292 *pixel=image->background_color;
2293 get_one_authentic_pixel_from_handler=
2294 cache_info->methods.get_one_authentic_pixel_from_handler;
2295 if (get_one_authentic_pixel_from_handler ==
2296 (GetOneAuthenticPixelFromHandler) NULL)
2297 return(MagickFalse);
2298 status=cache_info->methods.get_one_authentic_pixel_from_handler(image,x,y,
2299 pixel,exception);
2300 return(status);
2301}
2302
2303/*
2304%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2305% %
2306% %
2307% %
2308+ 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 %
2309% %
2310% %
2311% %
2312%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2313%
2314% GetOneAuthenticPixelFromCache() returns a single pixel at the specified (x,y)
2315% location. The image background color is returned if an error occurs.
2316%
2317% The format of the GetOneAuthenticPixelFromCache() method is:
2318%
2319% MagickBooleanType GetOneAuthenticPixelFromCache(const Image image,
2320% const long x,const long y,PixelPacket *pixel,ExceptionInfo *exception)
2321%
2322% A description of each parameter follows:
2323%
2324% o image: the image.
2325%
2326% o x,y: These values define the location of the pixel to return.
2327%
2328% o pixel: return a pixel at the specified (x,y) location.
2329%
2330% o exception: return any errors or warnings in this structure.
2331%
2332*/
2333static MagickBooleanType GetOneAuthenticPixelFromCache(Image *image,
2334 const long x,const long y,PixelPacket *pixel,ExceptionInfo *exception)
2335{
2336 PixelPacket
2337 *pixels;
2338
2339 if (image->debug != MagickFalse)
2340 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2341 *pixel=image->background_color;
2342 pixels=GetAuthenticPixelsCache(image,x,y,1UL,1UL,exception);
2343 if (pixels == (PixelPacket *) NULL)
2344 return(MagickFalse);
2345 *pixel=(*pixels);
2346 return(MagickTrue);
2347}
2348
2349/*
2350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2351% %
2352% %
2353% %
2354% G e t O n e V i r t u a l M a g i c k P i x e l %
2355% %
2356% %
2357% %
2358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2359%
2360% GetOneVirtualMagickPixel() returns a single pixel at the specified (x,y)
2361% location. The image background color is returned if an error occurs. If
2362% you plan to modify the pixel, use GetOneAuthenticPixel() instead.
2363%
2364% The format of the GetOneVirtualMagickPixel() method is:
2365%
2366% MagickBooleanType GetOneVirtualMagickPixel(const Image image,
2367% const long x,const long y,MagickPixelPacket *pixel,
2368% ExceptionInfo exception)
2369%
2370% A description of each parameter follows:
2371%
2372% o image: the image.
2373%
2374% o x,y: these values define the location of the pixel to return.
2375%
2376% o pixel: return a pixel at the specified (x,y) location.
2377%
2378% o exception: return any errors or warnings in this structure.
2379%
2380*/
2381MagickExport MagickBooleanType GetOneVirtualMagickPixel(const Image *image,
2382 const long x,const long y,MagickPixelPacket *pixel,ExceptionInfo *exception)
2383{
2384 CacheInfo
2385 *cache_info;
2386
2387 register const IndexPacket
2388 *indexes;
2389
2390 register const PixelPacket
2391 *p;
2392
2393 assert(image != (const Image *) NULL);
2394 assert(image->signature == MagickSignature);
2395 assert(image->cache != (Cache) NULL);
2396 cache_info=(CacheInfo *) image->cache;
2397 assert(cache_info->signature == MagickSignature);
2398 GetMagickPixelPacket(image,pixel);
2399 p=GetVirtualPixelCache(image,GetPixelCacheVirtualMethod(image),x,y,1,1,
2400 exception);
2401 if (p == (const PixelPacket *) NULL)
2402 return(MagickFalse);
2403 indexes=GetVirtualIndexQueue(image);
2404 SetMagickPixelPacket(image,p,indexes,pixel);
2405 return(MagickTrue);
2406}
2407
2408/*
2409%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2410% %
2411% %
2412% %
2413% G e t O n e V i r t u a l M e t h o d P i x e l %
2414% %
2415% %
2416% %
2417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2418%
2419% GetOneVirtualMethodPixel() returns a single pixel at the specified (x,y)
2420% location as defined by specified pixel method. The image background color
2421% is returned if an error occurs. If you plan to modify the pixel, use
2422% GetOneAuthenticPixel() instead.
2423%
2424% The format of the GetOneVirtualMethodPixel() method is:
2425%
2426% MagickBooleanType GetOneVirtualMethodPixel(const Image image,
2427% const VirtualPixelMethod virtual_pixel_method,const long x,
2428% const long y,Pixelpacket *pixel,ExceptionInfo exception)
2429%
2430% A description of each parameter follows:
2431%
2432% o image: the image.
2433%
2434% o virtual_pixel_method: the virtual pixel method.
2435%
2436% o x,y: These values define the location of the pixel to return.
2437%
2438% o pixel: return a pixel at the specified (x,y) location.
2439%
2440% o exception: return any errors or warnings in this structure.
2441%
2442*/
2443MagickExport MagickBooleanType GetOneVirtualMethodPixel(const Image *image,
2444 const VirtualPixelMethod virtual_pixel_method,const long x,const long y,
2445 PixelPacket *pixel,ExceptionInfo *exception)
2446{
2447 GetOneVirtualPixelFromHandler
2448 get_one_virtual_pixel_from_handler;
2449
2450 CacheInfo
2451 *cache_info;
2452
2453 MagickBooleanType
2454 status;
2455
2456 assert(image != (const Image *) NULL);
2457 assert(image->signature == MagickSignature);
2458 assert(image->cache != (Cache) NULL);
2459 cache_info=(CacheInfo *) image->cache;
2460 assert(cache_info->signature == MagickSignature);
2461 *pixel=image->background_color;
2462 get_one_virtual_pixel_from_handler=
2463 cache_info->methods.get_one_virtual_pixel_from_handler;
2464 if (get_one_virtual_pixel_from_handler ==
2465 (GetOneVirtualPixelFromHandler) NULL)
2466 return(MagickFalse);
2467 status=get_one_virtual_pixel_from_handler(image,virtual_pixel_method,x,y,
2468 pixel,exception);
2469 return(status);
2470}
2471
2472/*
2473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2474% %
2475% %
2476% %
2477% G e t O n e V i r t u a l P i x e l %
2478% %
2479% %
2480% %
2481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2482%
2483% GetOneVirtualPixel() returns a single virtual pixel at the specified
2484% (x,y) location. The image background color is returned if an error occurs.
2485% If you plan to modify the pixel, use GetOneAuthenticPixel() instead.
2486%
2487% The format of the GetOneVirtualPixel() method is:
2488%
2489% MagickBooleanType GetOneVirtualPixel(const Image image,const long x,
2490% const long y,PixelPacket *pixel,ExceptionInfo exception)
2491%
2492% A description of each parameter follows:
2493%
2494% o image: the image.
2495%
2496% o x,y: These values define the location of the pixel to return.
2497%
2498% o pixel: return a pixel at the specified (x,y) location.
2499%
2500% o exception: return any errors or warnings in this structure.
2501%
2502*/
2503MagickExport MagickBooleanType GetOneVirtualPixel(const Image *image,
2504 const long x,const long y,PixelPacket *pixel,ExceptionInfo *exception)
2505{
2506 GetOneVirtualPixelFromHandler
2507 get_one_virtual_pixel_from_handler;
2508
2509 CacheInfo
2510 *cache_info;
2511
2512 MagickBooleanType
2513 status;
2514
2515 assert(image != (const Image *) NULL);
2516 assert(image->signature == MagickSignature);
2517 assert(image->cache != (Cache) NULL);
2518 cache_info=(CacheInfo *) image->cache;
2519 assert(cache_info->signature == MagickSignature);
2520 *pixel=image->background_color;
2521 get_one_virtual_pixel_from_handler=
2522 cache_info->methods.get_one_virtual_pixel_from_handler;
2523 if (get_one_virtual_pixel_from_handler ==
2524 (GetOneVirtualPixelFromHandler) NULL)
2525 return(MagickFalse);
2526 status=get_one_virtual_pixel_from_handler(image,GetPixelCacheVirtualMethod(
2527 image),x,y,pixel,exception);
2528 return(status);
2529}
2530
2531/*
2532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2533% %
2534% %
2535% %
2536+ 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 %
2537% %
2538% %
2539% %
2540%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2541%
2542% GetOneVirtualPixelFromCache() returns a single virtual pixel at the
2543% specified (x,y) location. The image background color is returned if an
2544% error occurs.
2545%
2546% The format of the GetOneVirtualPixelFromCache() method is:
2547%
2548% MagickBooleanType GetOneVirtualPixelFromCache(const Image image,
2549% const VirtualPixelPacket method,const long x,const long y,
2550% PixelPacket *pixel,ExceptionInfo *exception)
2551%
2552% A description of each parameter follows:
2553%
2554% o image: the image.
2555%
2556% o virtual_pixel_method: the virtual pixel method.
2557%
2558% o x,y: These values define the location of the pixel to return.
2559%
2560% o pixel: return a pixel at the specified (x,y) location.
2561%
2562% o exception: return any errors or warnings in this structure.
2563%
2564*/
2565static MagickBooleanType GetOneVirtualPixelFromCache(const Image *image,
2566 const VirtualPixelMethod virtual_pixel_method,const long x,const long y,
2567 PixelPacket *pixel,ExceptionInfo *exception)
2568{
2569 const PixelPacket
2570 *pixels;
2571
2572 *pixel=image->background_color;
2573 pixels=GetVirtualPixelCache(image,virtual_pixel_method,x,y,1UL,1UL,exception);
2574 if (pixels == (const PixelPacket *) NULL)
2575 return(MagickFalse);
2576 *pixel=(*pixels);
2577 return(MagickTrue);
2578}
2579
2580/*
2581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2582% %
2583% %
2584% %
2585+ G e t P i x e l C a c h e C o l o r s p a c e %
2586% %
2587% %
2588% %
2589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2590%
2591% GetPixelCacheColorspace() returns the class type of the pixel cache.
2592%
2593% The format of the GetPixelCacheColorspace() method is:
2594%
2595% Colorspace GetPixelCacheColorspace(Cache cache)
2596%
2597% A description of each parameter follows:
2598%
2599% o cache: the pixel cache.
2600%
2601*/
2602MagickExport ColorspaceType GetPixelCacheColorspace(const Cache cache)
2603{
2604 CacheInfo
2605 *cache_info;
2606
2607 assert(cache != (Cache) NULL);
2608 cache_info=(CacheInfo *) cache;
2609 assert(cache_info->signature == MagickSignature);
2610 if (cache_info->debug != MagickFalse)
2611 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2612 cache_info->filename);
2613 return(cache_info->colorspace);
2614}
2615
2616/*
2617%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2618% %
2619% %
2620% %
2621+ G e t P i x e l C a c h e M e t h o d s %
2622% %
2623% %
2624% %
2625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2626%
2627% GetPixelCacheMethods() initializes the CacheMethods structure.
2628%
2629% The format of the GetPixelCacheMethods() method is:
2630%
2631% void GetPixelCacheMethods(CacheMethods *cache_methods)
2632%
2633% A description of each parameter follows:
2634%
2635% o cache_methods: Specifies a pointer to a CacheMethods structure.
2636%
2637*/
2638MagickExport void GetPixelCacheMethods(CacheMethods *cache_methods)
2639{
2640 assert(cache_methods != (CacheMethods *) NULL);
2641 (void) ResetMagickMemory(cache_methods,0,sizeof(*cache_methods));
2642 cache_methods->get_virtual_pixel_handler=GetVirtualPixelCache;
2643 cache_methods->get_virtual_pixels_handler=GetVirtualPixelsCache;
2644 cache_methods->get_virtual_indexes_from_handler=GetVirtualIndexesFromCache;
2645 cache_methods->get_one_virtual_pixel_from_handler=GetOneVirtualPixelFromCache;
2646 cache_methods->get_authentic_pixels_handler=GetAuthenticPixelsCache;
2647 cache_methods->get_authentic_indexes_from_handler=
2648 GetAuthenticIndexesFromCache;
2649 cache_methods->get_authentic_pixels_from_handler=GetAuthenticPixelsFromCache;
2650 cache_methods->get_one_authentic_pixel_from_handler=
2651 GetOneAuthenticPixelFromCache;
2652 cache_methods->queue_authentic_pixels_handler=QueueAuthenticPixelsCache;
2653 cache_methods->sync_authentic_pixels_handler=SyncAuthenticPixelsCache;
2654 cache_methods->destroy_pixel_handler=DestroyImagePixelCache;
2655}
2656
2657/*
2658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2659% %
2660% %
2661% %
2662+ G e t P i x e l C a c h e N e x u s E x t e n t %
2663% %
2664% %
2665% %
2666%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2667%
2668% GetPixelCacheNexusExtent() returns the extent of the pixels associated with
2669% the last call to SetPixelCacheNexusPixels() or GetPixelCacheNexusPixels().
2670%
2671% The format of the GetPixelCacheNexusExtent() method is:
2672%
2673% MagickSizeType GetPixelCacheNexusExtent(const Cache cache,
2674% NexusInfo *nexus_info)
2675%
2676% A description of each parameter follows:
2677%
2678% o nexus_info: the nexus info.
2679%
2680*/
2681MagickExport MagickSizeType GetPixelCacheNexusExtent(const Cache cache,
2682 NexusInfo *nexus_info)
2683{
2684 CacheInfo
2685 *cache_info;
2686
2687 MagickSizeType
2688 extent;
2689
2690 if (cache == (Cache) NULL)
2691 return(0);
2692 cache_info=(CacheInfo *) cache;
2693 assert(cache_info->signature == MagickSignature);
2694 extent=(MagickSizeType) nexus_info->region.width*nexus_info->region.height;
2695 if (extent == 0)
2696 return((MagickSizeType) cache_info->columns*cache_info->rows);
2697 return(extent);
2698}
2699
2700/*
2701%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2702% %
2703% %
2704% %
2705+ G e t P i x e l C a c h e N e x u s I n d e x e s %
2706% %
2707% %
2708% %
2709%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2710%
2711% GetPixelCacheNexusIndexes() returns the indexes associated with the
2712% specified cache nexus.
2713%
2714% The format of the GetPixelCacheNexusIndexes() method is:
2715%
2716% IndexPacket *GetPixelCacheNexusIndexes(const Cache cache,
2717% NexusInfo *nexus_info)
2718%
2719% A description of each parameter follows:
2720%
2721% o cache: the pixel cache.
2722%
2723% o nexus_info: the cache nexus to return the colormap indexes.
2724%
2725*/
2726MagickExport IndexPacket *GetPixelCacheNexusIndexes(const Cache cache,
2727 NexusInfo *nexus_info)
2728{
2729 CacheInfo
2730 *cache_info;
2731
2732 if (cache == (Cache) NULL)
2733 return((IndexPacket *) NULL);
2734 cache_info=(CacheInfo *) cache;
2735 assert(cache_info->signature == MagickSignature);
2736 if (cache_info->storage_class == UndefinedClass)
2737 return((IndexPacket *) NULL);
2738 return(nexus_info->indexes);
2739}
2740
2741/*
2742%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2743% %
2744% %
2745% %
2746+ G e t P i x e l C a c h e N e x u s P i x e l s %
2747% %
2748% %
2749% %
2750%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2751%
2752% GetPixelCacheNexusPixels() returns the pixels associated with the specified
2753% cache nexus.
2754%
2755% The format of the GetPixelCacheNexusPixels() method is:
2756%
2757% PixelPacket *GetPixelCacheNexusPixels(const Cache cache,
2758% NexusInfo *nexus_info)
2759%
2760% A description of each parameter follows:
2761%
2762% o cache: the pixel cache.
2763%
2764% o nexus_info: the cache nexus to return the pixels.
2765%
2766*/
2767MagickExport PixelPacket *GetPixelCacheNexusPixels(const Cache cache,
2768 NexusInfo *nexus_info)
2769{
2770 CacheInfo
2771 *cache_info;
2772
2773 if (cache == (Cache) NULL)
2774 return((PixelPacket *) NULL);
2775 cache_info=(CacheInfo *) cache;
2776 assert(cache_info->signature == MagickSignature);
2777 if (cache_info->debug != MagickFalse)
2778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2779 cache_info->filename);
2780 if (cache_info->storage_class == UndefinedClass)
2781 return((PixelPacket *) NULL);
2782 return(nexus_info->pixels);
2783}
2784
2785/*
2786%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2787% %
2788% %
2789% %
cristy056ba772010-01-02 23:33:54 +00002790+ G e t P i x e l C a c h e P i x e l s %
2791% %
2792% %
2793% %
2794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2795%
2796% GetPixelCachePixels() returns the pixels associated with the specified image.
2797%
2798% The format of the GetPixelCachePixels() method is:
2799%
cristyf84a1932010-01-03 18:00:18 +00002800% void *GetPixelCachePixels(Image *image,MagickSizeType *length,
2801% ExceptionInfo *exception)
cristy056ba772010-01-02 23:33:54 +00002802%
2803% A description of each parameter follows:
2804%
2805% o image: the image.
2806%
2807% o length: the pixel cache length.
2808%
cristyf84a1932010-01-03 18:00:18 +00002809% o exception: return any errors or warnings in this structure.
2810%
cristy056ba772010-01-02 23:33:54 +00002811*/
cristyf84a1932010-01-03 18:00:18 +00002812MagickExport void *GetPixelCachePixels(Image *image,MagickSizeType *length,
2813 ExceptionInfo *exception)
cristy056ba772010-01-02 23:33:54 +00002814{
2815 CacheInfo
2816 *cache_info;
2817
2818 assert(image != (const Image *) NULL);
2819 assert(image->signature == MagickSignature);
2820 if (image->debug != MagickFalse)
2821 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2822 assert(image->cache != (Cache) NULL);
cristyc4c8d132010-01-07 01:58:38 +00002823 cache_info=(CacheInfo *) GetImagePixelCache(image,MagickTrue,exception);
cristy056ba772010-01-02 23:33:54 +00002824 assert(cache_info->signature == MagickSignature);
2825 *length=0;
cristyc4c8d132010-01-07 01:58:38 +00002826 if ((cache_info->type != MemoryCache) && (cache_info->type != MapCache))
cristy056ba772010-01-02 23:33:54 +00002827 return((void *) NULL);
2828 *length=cache_info->length;
2829 return((void *) cache_info->pixels);
2830}
2831
2832/*
2833%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2834% %
2835% %
2836% %
cristyb32b90a2009-09-07 21:45:48 +00002837+ 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 %
cristy3ed852e2009-09-05 21:47:34 +00002838% %
2839% %
2840% %
2841%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2842%
2843% GetPixelCacheStorageClass() returns the class type of the pixel cache.
2844%
2845% The format of the GetPixelCacheStorageClass() method is:
2846%
2847% ClassType GetPixelCacheStorageClass(Cache cache)
2848%
2849% A description of each parameter follows:
2850%
2851% o type: GetPixelCacheStorageClass returns DirectClass or PseudoClass.
2852%
2853% o cache: the pixel cache.
2854%
2855*/
2856MagickExport ClassType GetPixelCacheStorageClass(const Cache cache)
2857{
2858 CacheInfo
2859 *cache_info;
2860
2861 assert(cache != (Cache) NULL);
2862 cache_info=(CacheInfo *) cache;
2863 assert(cache_info->signature == MagickSignature);
2864 if (cache_info->debug != MagickFalse)
2865 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2866 cache_info->filename);
2867 return(cache_info->storage_class);
2868}
2869
2870/*
2871%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2872% %
2873% %
2874% %
cristyb32b90a2009-09-07 21:45:48 +00002875+ G e t P i x e l C a c h e T i l e S i z e %
2876% %
2877% %
2878% %
2879%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2880%
2881% GetPixelCacheTileSize() returns the pixel cache tile size.
2882%
2883% The format of the GetPixelCacheTileSize() method is:
2884%
2885% void GetPixelCacheTileSize(const Image *image,unsigned long *width,
2886% unsigned long *height)
2887%
2888% A description of each parameter follows:
2889%
2890% o image: the image.
2891%
2892% o width: the optimize cache tile width in pixels.
2893%
2894% o height: the optimize cache tile height in pixels.
2895%
2896*/
2897MagickExport void GetPixelCacheTileSize(const Image *image,unsigned long *width,
2898 unsigned long *height)
2899{
2900 CacheInfo
2901 *cache_info;
2902
2903 assert(image != (Image *) NULL);
2904 assert(image->signature == MagickSignature);
2905 if (image->debug != MagickFalse)
2906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2907 assert(image->cache != (Cache) NULL);
2908 cache_info=(CacheInfo *) image->cache;
2909 assert(cache_info->signature == MagickSignature);
2910 *width=2048UL/sizeof(PixelPacket);
2911 if (GetPixelCacheType(image) == DiskCache)
cristydaa97692009-09-13 02:10:35 +00002912 *width=8192UL/sizeof(PixelPacket);
cristyb32b90a2009-09-07 21:45:48 +00002913 *height=(*width);
2914}
2915
2916/*
2917%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2918% %
2919% %
2920% %
2921+ G e t P i x e l C a c h e T y p e %
2922% %
2923% %
2924% %
2925%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2926%
2927% GetPixelCacheType() returns the pixel cache type (e.g. memory, disk, etc.).
2928%
2929% The format of the GetPixelCacheType() method is:
2930%
2931% CacheType GetPixelCacheType(const Image *image)
2932%
2933% A description of each parameter follows:
2934%
2935% o image: the image.
2936%
2937*/
2938MagickExport CacheType GetPixelCacheType(const Image *image)
2939{
2940 CacheInfo
2941 *cache_info;
2942
2943 assert(image != (Image *) NULL);
2944 assert(image->signature == MagickSignature);
2945 if (image->debug != MagickFalse)
2946 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2947 assert(image->cache != (Cache) NULL);
2948 cache_info=(CacheInfo *) image->cache;
2949 assert(cache_info->signature == MagickSignature);
2950 return(cache_info->type);
2951}
2952
2953/*
2954%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2955% %
2956% %
2957% %
cristy3ed852e2009-09-05 21:47:34 +00002958+ 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 %
2959% %
2960% %
2961% %
2962%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2963%
2964% GetPixelCacheVirtualMethod() gets the "virtual pixels" method for the
2965% pixel cache. A virtual pixel is any pixel access that is outside the
2966% boundaries of the image cache.
2967%
2968% The format of the GetPixelCacheVirtualMethod() method is:
2969%
2970% VirtualPixelMethod GetPixelCacheVirtualMethod(const Image *image)
2971%
2972% A description of each parameter follows:
2973%
2974% o image: the image.
2975%
2976*/
2977MagickExport VirtualPixelMethod GetPixelCacheVirtualMethod(const Image *image)
2978{
2979 CacheInfo
2980 *cache_info;
2981
2982 assert(image != (Image *) NULL);
2983 assert(image->signature == MagickSignature);
2984 if (image->debug != MagickFalse)
2985 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2986 assert(image->cache != (Cache) NULL);
2987 cache_info=(CacheInfo *) image->cache;
2988 assert(cache_info->signature == MagickSignature);
2989 return(cache_info->virtual_pixel_method);
2990}
2991
2992/*
2993%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2994% %
2995% %
2996% %
2997+ G e t V i r t u a l I n d e x e s F r o m C a c h e %
2998% %
2999% %
3000% %
3001%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3002%
3003% GetVirtualIndexesFromCache() returns the indexes associated with the last
3004% call to QueueAuthenticPixelsCache() or GetVirtualPixelCache().
3005%
3006% The format of the GetVirtualIndexesFromCache() method is:
3007%
3008% IndexPacket *GetVirtualIndexesFromCache(const Image *image)
3009%
3010% A description of each parameter follows:
3011%
3012% o image: the image.
3013%
3014*/
3015static const IndexPacket *GetVirtualIndexesFromCache(const Image *image)
3016{
3017 CacheInfo
3018 *cache_info;
3019
3020 const IndexPacket
3021 *indexes;
3022
3023 long
3024 id;
3025
3026 if (image->debug != MagickFalse)
3027 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3028 cache_info=(CacheInfo *) image->cache;
3029 id=GetOpenMPThreadId();
3030 assert(id < (long) cache_info->number_threads);
3031 indexes=GetVirtualIndexesFromNexus(image->cache,cache_info->nexus_info[id]);
3032 return(indexes);
3033}
3034
3035/*
3036%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3037% %
3038% %
3039% %
3040+ G e t V i r t u a l I n d e x e s F r o m N e x u s %
3041% %
3042% %
3043% %
3044%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3045%
3046% GetVirtualIndexesFromNexus() returns the indexes associated with the
3047% specified cache nexus.
3048%
3049% The format of the GetVirtualIndexesFromNexus() method is:
3050%
3051% const IndexPacket *GetVirtualIndexesFromNexus(const Cache cache,
3052% NexusInfo *nexus_info)
3053%
3054% A description of each parameter follows:
3055%
3056% o cache: the pixel cache.
3057%
3058% o nexus_info: the cache nexus to return the colormap indexes.
3059%
3060*/
3061MagickExport const IndexPacket *GetVirtualIndexesFromNexus(const Cache cache,
3062 NexusInfo *nexus_info)
3063{
3064 CacheInfo
3065 *cache_info;
3066
3067 if (cache == (Cache) NULL)
3068 return((IndexPacket *) NULL);
3069 cache_info=(CacheInfo *) cache;
3070 assert(cache_info->signature == MagickSignature);
3071 if (cache_info->storage_class == UndefinedClass)
3072 return((IndexPacket *) NULL);
3073 return(nexus_info->indexes);
3074}
3075
3076/*
3077%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3078% %
3079% %
3080% %
3081% G e t V i r t u a l I n d e x Q u e u e %
3082% %
3083% %
3084% %
3085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3086%
3087% GetVirtualIndexQueue() returns the virtual black channel or the
3088% colormap indexes associated with the last call to QueueAuthenticPixels() or
3089% GetVirtualPixels(). NULL is returned if the black channel or colormap
3090% indexes are not available.
3091%
3092% The format of the GetVirtualIndexQueue() method is:
3093%
3094% const IndexPacket *GetVirtualIndexQueue(const Image *image)
3095%
3096% A description of each parameter follows:
3097%
3098% o image: the image.
3099%
3100*/
3101MagickExport const IndexPacket *GetVirtualIndexQueue(const Image *image)
3102{
3103 CacheInfo
3104 *cache_info;
3105
3106 assert(image != (const Image *) NULL);
3107 assert(image->signature == MagickSignature);
3108 if (image->debug != MagickFalse)
3109 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3110 assert(image->cache != (Cache) NULL);
3111 cache_info=(CacheInfo *) image->cache;
3112 assert(cache_info->signature == MagickSignature);
3113 if (cache_info->methods.get_virtual_indexes_from_handler ==
3114 (GetVirtualIndexesFromHandler) NULL)
3115 return((IndexPacket *) NULL);
3116 return(cache_info->methods.get_virtual_indexes_from_handler(image));
3117}
3118
3119/*
3120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3121% %
3122% %
3123% %
3124+ G e t V i r t u a l P i x e l s F r o m N e x u s %
3125% %
3126% %
3127% %
3128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3129%
3130% GetVirtualPixelsFromNexus() gets virtual pixels from the in-memory or disk
3131% pixel cache as defined by the geometry parameters. A pointer to the pixels
3132% is returned if the pixels are transferred, otherwise a NULL is returned.
3133%
3134% The format of the GetVirtualPixelsFromNexus() method is:
3135%
3136% PixelPacket *GetVirtualPixelsFromNexus(const Image *image,
3137% const VirtualPixelMethod method,const long x,const long y,
3138% const unsigned long columns,const unsigned long rows,
3139% NexusInfo *nexus_info,ExceptionInfo *exception)
3140%
3141% A description of each parameter follows:
3142%
3143% o image: the image.
3144%
3145% o virtual_pixel_method: the virtual pixel method.
3146%
3147% o x,y,columns,rows: These values define the perimeter of a region of
3148% pixels.
3149%
3150% o nexus_info: the cache nexus to acquire.
3151%
3152% o exception: return any errors or warnings in this structure.
3153%
3154*/
3155
3156static long
3157 DitherMatrix[64] =
3158 {
3159 0, 48, 12, 60, 3, 51, 15, 63,
3160 32, 16, 44, 28, 35, 19, 47, 31,
3161 8, 56, 4, 52, 11, 59, 7, 55,
3162 40, 24, 36, 20, 43, 27, 39, 23,
3163 2, 50, 14, 62, 1, 49, 13, 61,
3164 34, 18, 46, 30, 33, 17, 45, 29,
3165 10, 58, 6, 54, 9, 57, 5, 53,
3166 42, 26, 38, 22, 41, 25, 37, 21
3167 };
3168
cristy222528c2010-01-09 23:36:34 +00003169static inline long DitherX(const long x,const unsigned long columns)
cristy3ed852e2009-09-05 21:47:34 +00003170{
3171 long
3172 index;
3173
3174 index=x+DitherMatrix[x & 0x07]-32L;
3175 if (index < 0L)
3176 return(0L);
3177 if (index >= (long) columns)
3178 return((long) columns-1L);
3179 return(index);
3180}
3181
cristy222528c2010-01-09 23:36:34 +00003182static inline long DitherY(const long y,const unsigned long rows)
cristy3ed852e2009-09-05 21:47:34 +00003183{
3184 long
3185 index;
3186
3187 index=y+DitherMatrix[y & 0x07]-32L;
3188 if (index < 0L)
3189 return(0L);
3190 if (index >= (long) rows)
3191 return((long) rows-1L);
3192 return(index);
3193}
3194
cristy222528c2010-01-09 23:36:34 +00003195static inline long EdgeX(const long x,const unsigned long columns)
cristy3ed852e2009-09-05 21:47:34 +00003196{
3197 if (x < 0L)
3198 return(0L);
3199 if (x >= (long) columns)
cristy97be2b92010-01-09 23:37:55 +00003200 return((long) (columns-1));
cristy3ed852e2009-09-05 21:47:34 +00003201 return(x);
3202}
3203
cristy222528c2010-01-09 23:36:34 +00003204static inline long EdgeY(const long y,const unsigned long rows)
cristy3ed852e2009-09-05 21:47:34 +00003205{
3206 if (y < 0L)
3207 return(0L);
3208 if (y >= (long) rows)
cristy97be2b92010-01-09 23:37:55 +00003209 return((long) (rows-1));
cristy3ed852e2009-09-05 21:47:34 +00003210 return(y);
3211}
3212
cristy222528c2010-01-09 23:36:34 +00003213static inline long RandomX(RandomInfo *random_info,const unsigned long columns)
cristy3ed852e2009-09-05 21:47:34 +00003214{
3215 return((long) (columns*GetPseudoRandomValue(random_info)));
3216}
3217
cristy222528c2010-01-09 23:36:34 +00003218static inline long RandomY(RandomInfo *random_info,const unsigned long rows)
cristy3ed852e2009-09-05 21:47:34 +00003219{
3220 return((long) (rows*GetPseudoRandomValue(random_info)));
3221}
3222
3223/*
3224 VirtualPixelModulo() computes the remainder of dividing offset by extent. It
3225 returns not only the quotient (tile the offset falls in) but also the positive
3226 remainer within that tile such that 0 <= remainder < extent. This method is
3227 essentially a ldiv() using a floored modulo division rather than the normal
3228 default truncated modulo division.
3229*/
3230static inline MagickModulo VirtualPixelModulo(const long offset,
3231 const unsigned long extent)
3232{
3233 MagickModulo
3234 modulo;
3235
3236 modulo.quotient=offset/(long) extent;
3237 if (offset < 0L)
3238 modulo.quotient--;
3239 modulo.remainder=offset-modulo.quotient*(long) extent;
3240 return(modulo);
3241}
3242
3243MagickExport const PixelPacket *GetVirtualPixelsFromNexus(const Image *image,
3244 const VirtualPixelMethod virtual_pixel_method,const long x,const long y,
3245 const unsigned long columns,const unsigned long rows,NexusInfo *nexus_info,
3246 ExceptionInfo *exception)
3247{
3248 CacheInfo
3249 *cache_info;
3250
3251 MagickOffsetType
3252 offset;
3253
3254 MagickSizeType
3255 length,
3256 number_pixels;
3257
3258 NexusInfo
3259 **virtual_nexus;
3260
3261 PixelPacket
3262 *pixels,
3263 virtual_pixel;
3264
3265 RectangleInfo
3266 region;
3267
3268 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003269 *restrict nexus_indexes;
cristy3ed852e2009-09-05 21:47:34 +00003270
3271 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003272 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003273
3274 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003275 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00003276
3277 register long
3278 u,
3279 v;
3280
3281 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003282 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003283
3284 /*
3285 Acquire pixels.
3286 */
3287 if (image->debug != MagickFalse)
3288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3289 cache_info=(CacheInfo *) image->cache;
3290 if (cache_info->type == UndefinedCache)
3291 return((const PixelPacket *) NULL);
3292 region.x=x;
3293 region.y=y;
3294 region.width=columns;
3295 region.height=rows;
3296 pixels=SetPixelCacheNexusPixels(image,&region,nexus_info,exception);
3297 if (pixels == (PixelPacket *) NULL)
3298 return((const PixelPacket *) NULL);
3299 offset=(MagickOffsetType) region.y*cache_info->columns+region.x;
cristy4789f0d2010-01-10 00:01:06 +00003300 length=(MagickSizeType) (region.height-1L)*cache_info->columns+
3301 region.width-1L;
cristy3ed852e2009-09-05 21:47:34 +00003302 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
3303 if ((offset >= 0) && (((MagickSizeType) offset+length) < number_pixels))
3304 if ((x >= 0) && ((long) (x+columns) <= (long) cache_info->columns) &&
3305 (y >= 0) && ((long) (y+rows) <= (long) cache_info->rows))
3306 {
3307 MagickBooleanType
3308 status;
3309
3310 /*
3311 Pixel request is inside cache extents.
3312 */
3313 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
3314 return(pixels);
3315 status=ReadPixelCachePixels(cache_info,nexus_info,exception);
3316 if (status == MagickFalse)
3317 return((const PixelPacket *) NULL);
3318 if ((cache_info->storage_class == PseudoClass) ||
3319 (cache_info->colorspace == CMYKColorspace))
3320 {
3321 status=ReadPixelCacheIndexes(cache_info,nexus_info,exception);
3322 if (status == MagickFalse)
3323 return((const PixelPacket *) NULL);
3324 }
3325 return(pixels);
3326 }
3327 /*
3328 Pixel request is outside cache extents.
3329 */
3330 q=pixels;
3331 indexes=GetPixelCacheNexusIndexes(cache_info,nexus_info);
3332 virtual_nexus=AcquirePixelCacheNexus(1);
3333 if (virtual_nexus == (NexusInfo **) NULL)
3334 {
3335 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
3336 "UnableToGetCacheNexus","`%s'",image->filename);
3337 return((const PixelPacket *) NULL);
3338 }
3339 switch (virtual_pixel_method)
3340 {
3341 case BlackVirtualPixelMethod:
3342 {
cristy4789f0d2010-01-10 00:01:06 +00003343 SetRedPixelComponent(&virtual_pixel,0);
3344 SetGreenPixelComponent(&virtual_pixel,0);
3345 SetBluePixelComponent(&virtual_pixel,0);
3346 SetOpacityPixelComponent(&virtual_pixel,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00003347 break;
3348 }
3349 case GrayVirtualPixelMethod:
3350 {
cristy4789f0d2010-01-10 00:01:06 +00003351 SetRedPixelComponent(&virtual_pixel,(Quantum) QuantumRange/2);
3352 SetGreenPixelComponent(&virtual_pixel,(Quantum) QuantumRange/2);
3353 SetBluePixelComponent(&virtual_pixel,(Quantum) QuantumRange/2);
3354 SetOpacityPixelComponent(&virtual_pixel,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00003355 break;
3356 }
3357 case TransparentVirtualPixelMethod:
3358 {
cristy4789f0d2010-01-10 00:01:06 +00003359 SetRedPixelComponent(&virtual_pixel,0);
3360 SetGreenPixelComponent(&virtual_pixel,0);
3361 SetBluePixelComponent(&virtual_pixel,0);
3362 SetOpacityPixelComponent(&virtual_pixel,TransparentOpacity);
cristy3ed852e2009-09-05 21:47:34 +00003363 break;
3364 }
3365 case MaskVirtualPixelMethod:
3366 case WhiteVirtualPixelMethod:
3367 {
cristy4789f0d2010-01-10 00:01:06 +00003368 SetRedPixelComponent(&virtual_pixel,(Quantum) QuantumRange);
3369 SetGreenPixelComponent(&virtual_pixel,(Quantum) QuantumRange);
3370 SetBluePixelComponent(&virtual_pixel,(Quantum) QuantumRange);
3371 SetOpacityPixelComponent(&virtual_pixel,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00003372 break;
3373 }
3374 default:
3375 {
3376 virtual_pixel=image->background_color;
3377 break;
3378 }
3379 }
3380 for (v=0; v < (long) rows; v++)
3381 {
3382 for (u=0; u < (long) columns; u+=length)
3383 {
3384 length=(MagickSizeType) MagickMin(cache_info->columns-(x+u),columns-u);
3385 if ((((x+u) < 0) || ((x+u) >= (long) cache_info->columns)) ||
3386 (((y+v) < 0) || ((y+v) >= (long) cache_info->rows)) || (length == 0))
3387 {
3388 MagickModulo
3389 x_modulo,
3390 y_modulo;
3391
3392 /*
3393 Transfer a single pixel.
3394 */
3395 length=(MagickSizeType) 1;
3396 switch (virtual_pixel_method)
3397 {
3398 case BackgroundVirtualPixelMethod:
3399 case ConstantVirtualPixelMethod:
3400 case BlackVirtualPixelMethod:
3401 case GrayVirtualPixelMethod:
3402 case TransparentVirtualPixelMethod:
3403 case MaskVirtualPixelMethod:
3404 case WhiteVirtualPixelMethod:
3405 {
3406 p=(&virtual_pixel);
3407 break;
3408 }
3409 case EdgeVirtualPixelMethod:
3410 default:
3411 {
3412 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003413 EdgeX(x+u,cache_info->columns),EdgeY(y+v,cache_info->rows),
cristy3ed852e2009-09-05 21:47:34 +00003414 1UL,1UL,virtual_nexus[0],exception);
3415 break;
3416 }
3417 case RandomVirtualPixelMethod:
3418 {
3419 if (cache_info->random_info == (RandomInfo *) NULL)
3420 cache_info->random_info=AcquireRandomInfo();
3421 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003422 RandomX(cache_info->random_info,cache_info->columns),
3423 RandomY(cache_info->random_info,cache_info->rows),1UL,1UL,
cristy3ed852e2009-09-05 21:47:34 +00003424 virtual_nexus[0],exception);
3425 break;
3426 }
3427 case DitherVirtualPixelMethod:
3428 {
3429 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003430 DitherX(x+u,cache_info->columns),DitherY(y+v,cache_info->rows),
cristy3ed852e2009-09-05 21:47:34 +00003431 1UL,1UL,virtual_nexus[0],exception);
3432 break;
3433 }
3434 case TileVirtualPixelMethod:
3435 {
3436 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3437 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3438 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3439 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3440 exception);
3441 break;
3442 }
3443 case MirrorVirtualPixelMethod:
3444 {
3445 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3446 if ((x_modulo.quotient & 0x01) == 1L)
3447 x_modulo.remainder=(long) cache_info->columns-
3448 x_modulo.remainder-1L;
3449 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3450 if ((y_modulo.quotient & 0x01) == 1L)
3451 y_modulo.remainder=(long) cache_info->rows-
3452 y_modulo.remainder-1L;
3453 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3454 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3455 exception);
3456 break;
3457 }
3458 case CheckerTileVirtualPixelMethod:
3459 {
3460 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3461 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3462 if (((x_modulo.quotient ^ y_modulo.quotient) & 0x01) != 0L)
3463 {
3464 p=(&virtual_pixel);
3465 break;
3466 }
3467 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3468 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3469 exception);
3470 break;
3471 }
3472 case HorizontalTileVirtualPixelMethod:
3473 {
3474 if (((y+v) < 0) || ((y+v) >= (long) cache_info->rows))
3475 {
3476 p=(&virtual_pixel);
3477 break;
3478 }
3479 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3480 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3481 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3482 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3483 exception);
3484 break;
3485 }
3486 case VerticalTileVirtualPixelMethod:
3487 {
3488 if (((x+u) < 0) || ((x+u) >= (long) cache_info->columns))
3489 {
3490 p=(&virtual_pixel);
3491 break;
3492 }
3493 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3494 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3495 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3496 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3497 exception);
3498 break;
3499 }
3500 case HorizontalTileEdgeVirtualPixelMethod:
3501 {
3502 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3503 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003504 x_modulo.remainder,EdgeY(y+v,cache_info->rows),1UL,1UL,
cristy3ed852e2009-09-05 21:47:34 +00003505 virtual_nexus[0],exception);
3506 break;
3507 }
3508 case VerticalTileEdgeVirtualPixelMethod:
3509 {
3510 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3511 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003512 EdgeX(x+u,cache_info->columns),y_modulo.remainder,1UL,1UL,
cristy3ed852e2009-09-05 21:47:34 +00003513 virtual_nexus[0],exception);
3514 break;
3515 }
3516 }
3517 if (p == (const PixelPacket *) NULL)
3518 break;
3519 *q++=(*p);
3520 if (indexes != (IndexPacket *) NULL)
3521 {
3522 nexus_indexes=GetVirtualIndexesFromNexus(cache_info,
3523 virtual_nexus[0]);
3524 if (nexus_indexes != (const IndexPacket *) NULL)
3525 *indexes++=(*nexus_indexes);
3526 }
3527 continue;
3528 }
3529 /*
3530 Transfer a run of pixels.
3531 */
3532 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,x+u,y+v,
3533 (unsigned long) length,1UL,virtual_nexus[0],exception);
3534 if (p == (const PixelPacket *) NULL)
3535 break;
3536 (void) CopyMagickMemory(q,p,(size_t) length*sizeof(*p));
3537 q+=length;
3538 if (indexes != (IndexPacket *) NULL)
3539 {
3540 nexus_indexes=GetVirtualIndexesFromNexus(cache_info,virtual_nexus[0]);
3541 if (nexus_indexes != (const IndexPacket *) NULL)
3542 {
3543 (void) CopyMagickMemory(indexes,nexus_indexes,(size_t) length*
3544 sizeof(*nexus_indexes));
3545 indexes+=length;
3546 }
3547 }
3548 }
3549 }
3550 virtual_nexus=DestroyPixelCacheNexus(virtual_nexus,1);
3551 return(pixels);
3552}
3553
3554/*
3555%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3556% %
3557% %
3558% %
3559+ G e t V i r t u a l P i x e l C a c h e %
3560% %
3561% %
3562% %
3563%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3564%
3565% GetVirtualPixelCache() get virtual pixels from the in-memory or disk pixel
3566% cache as defined by the geometry parameters. A pointer to the pixels
3567% is returned if the pixels are transferred, otherwise a NULL is returned.
3568%
3569% The format of the GetVirtualPixelCache() method is:
3570%
3571% const PixelPacket *GetVirtualPixelCache(const Image *image,
3572% const VirtualPixelMethod virtual_pixel_method,const long x,
3573% const long y,const unsigned long columns,const unsigned long rows,
3574% ExceptionInfo *exception)
3575%
3576% A description of each parameter follows:
3577%
3578% o image: the image.
3579%
3580% o virtual_pixel_method: the virtual pixel method.
3581%
3582% o x,y,columns,rows: These values define the perimeter of a region of
3583% pixels.
3584%
3585% o exception: return any errors or warnings in this structure.
3586%
3587*/
3588static const PixelPacket *GetVirtualPixelCache(const Image *image,
3589 const VirtualPixelMethod virtual_pixel_method,const long x,const long y,
3590 const unsigned long columns,const unsigned long rows,ExceptionInfo *exception)
3591{
3592 CacheInfo
3593 *cache_info;
3594
3595 const PixelPacket
3596 *pixels;
3597
3598 long
3599 id;
3600
3601 if (image->debug != MagickFalse)
3602 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3603 cache_info=(CacheInfo *) image->cache;
3604 id=GetOpenMPThreadId();
3605 assert(id < (long) cache_info->number_threads);
3606 pixels=GetVirtualPixelsFromNexus(image,virtual_pixel_method,x,y,columns,rows,
3607 cache_info->nexus_info[id],exception);
3608 return(pixels);
3609}
3610
3611/*
3612%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3613% %
3614% %
3615% %
3616% G e t V i r t u a l P i x e l Q u e u e %
3617% %
3618% %
3619% %
3620%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3621%
3622% GetVirtualPixelQueue() returns the virtual pixels associated with the
3623% last call to QueueAuthenticPixels() or GetVirtualPixels().
3624%
3625% The format of the GetVirtualPixelQueue() method is:
3626%
3627% const PixelPacket *GetVirtualPixelQueue(const Image image)
3628%
3629% A description of each parameter follows:
3630%
3631% o image: the image.
3632%
3633*/
3634MagickExport const PixelPacket *GetVirtualPixelQueue(const Image *image)
3635{
3636 CacheInfo
3637 *cache_info;
3638
3639 assert(image != (const Image *) NULL);
3640 assert(image->signature == MagickSignature);
3641 if (image->debug != MagickFalse)
3642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3643 assert(image->cache != (Cache) NULL);
3644 cache_info=(CacheInfo *) image->cache;
3645 assert(cache_info->signature == MagickSignature);
3646 if (cache_info->methods.get_virtual_pixels_handler ==
3647 (GetVirtualPixelsHandler) NULL)
3648 return((PixelPacket *) NULL);
3649 return(cache_info->methods.get_virtual_pixels_handler(image));
3650}
3651
3652/*
3653%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3654% %
3655% %
3656% %
3657% G e t V i r t u a l P i x e l s %
3658% %
3659% %
3660% %
3661%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3662%
3663% GetVirtualPixels() returns an immutable pixel region. If the
3664% region is successfully accessed, a pointer to it is returned, otherwise
cristyad4e5b22010-01-10 00:04:19 +00003665% NULL is returned. The returned pointer may point to a temporary working
cristy3ed852e2009-09-05 21:47:34 +00003666% copy of the pixels or it may point to the original pixels in memory.
3667% Performance is maximized if the selected region is part of one row, or one
3668% or more full rows, since there is opportunity to access the pixels in-place
cristyad4e5b22010-01-10 00:04:19 +00003669% (without a copy) if the image is in memory, or in a memory-mapped file. The
3670% returned pointer must *never* be deallocated by the user.
cristy3ed852e2009-09-05 21:47:34 +00003671%
3672% Pixels accessed via the returned pointer represent a simple array of type
3673% PixelPacket. If the image type is CMYK or the storage class is PseudoClass,
3674% call GetAuthenticIndexQueue() after invoking GetAuthenticPixels() to access
3675% the black color component or to obtain the colormap indexes (of type
3676% IndexPacket) corresponding to the region.
3677%
3678% If you plan to modify the pixels, use GetAuthenticPixels() instead.
3679%
3680% Note, the GetVirtualPixels() and GetAuthenticPixels() methods are not thread-
3681% safe. In a threaded environment, use GetCacheViewVirtualPixels() or
3682% GetCacheViewAuthenticPixels() instead.
3683%
3684% The format of the GetVirtualPixels() method is:
3685%
3686% const PixelPacket *GetVirtualPixels(const Image *image,const long x,
3687% const long y,const unsigned long columns,const unsigned long rows,
3688% ExceptionInfo *exception)
3689%
3690% A description of each parameter follows:
3691%
3692% o image: the image.
3693%
3694% o x,y,columns,rows: These values define the perimeter of a region of
3695% pixels.
3696%
3697% o exception: return any errors or warnings in this structure.
3698%
3699*/
3700MagickExport const PixelPacket *GetVirtualPixels(const Image *image,
3701 const long x,const long y,const unsigned long columns,
3702 const unsigned long rows,ExceptionInfo *exception)
3703{
3704 CacheInfo
3705 *cache_info;
3706
3707 const PixelPacket
3708 *pixels;
3709
3710 assert(image != (const Image *) NULL);
3711 assert(image->signature == MagickSignature);
3712 if (image->debug != MagickFalse)
3713 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3714 assert(image->cache != (Cache) NULL);
3715 cache_info=(CacheInfo *) image->cache;
3716 assert(cache_info->signature == MagickSignature);
3717 if (cache_info->methods.get_virtual_pixel_handler ==
3718 (GetVirtualPixelHandler) NULL)
3719 return((const PixelPacket *) NULL);
3720 pixels=cache_info->methods.get_virtual_pixel_handler(image,
3721 GetPixelCacheVirtualMethod(image),x,y,columns,rows,exception);
3722 return(pixels);
3723}
3724
3725/*
3726%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3727% %
3728% %
3729% %
3730+ 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 %
3731% %
3732% %
3733% %
3734%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3735%
3736% GetVirtualPixelsCache() returns the pixels associated with the last call
3737% to QueueAuthenticPixelsCache() or GetVirtualPixelCache().
3738%
3739% The format of the GetVirtualPixelsCache() method is:
3740%
3741% PixelPacket *GetVirtualPixelsCache(const Image *image)
3742%
3743% A description of each parameter follows:
3744%
3745% o image: the image.
3746%
3747*/
3748static const PixelPacket *GetVirtualPixelsCache(const Image *image)
3749{
3750 CacheInfo
3751 *cache_info;
3752
3753 const PixelPacket
3754 *pixels;
3755
3756 long
3757 id;
3758
3759 if (image->debug != MagickFalse)
3760 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3761 cache_info=(CacheInfo *) image->cache;
3762 id=GetOpenMPThreadId();
3763 assert(id < (long) cache_info->number_threads);
3764 pixels=GetVirtualPixelsNexus(image->cache,cache_info->nexus_info[id]);
3765 return(pixels);
3766}
3767
3768/*
3769%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3770% %
3771% %
3772% %
3773+ G e t V i r t u a l P i x e l s N e x u s %
3774% %
3775% %
3776% %
3777%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3778%
3779% GetVirtualPixelsNexus() returns the pixels associated with the specified
3780% cache nexus.
3781%
3782% The format of the GetVirtualPixelsNexus() method is:
3783%
3784% const IndexPacket *GetVirtualPixelsNexus(const Cache cache,
3785% NexusInfo *nexus_info)
3786%
3787% A description of each parameter follows:
3788%
3789% o cache: the pixel cache.
3790%
3791% o nexus_info: the cache nexus to return the colormap pixels.
3792%
3793*/
3794MagickExport const PixelPacket *GetVirtualPixelsNexus(const Cache cache,
3795 NexusInfo *nexus_info)
3796{
3797 CacheInfo
3798 *cache_info;
3799
3800 if (cache == (Cache) NULL)
3801 return((PixelPacket *) NULL);
3802 cache_info=(CacheInfo *) cache;
3803 assert(cache_info->signature == MagickSignature);
3804 if (cache_info->storage_class == UndefinedClass)
3805 return((PixelPacket *) NULL);
3806 return((const PixelPacket *) nexus_info->pixels);
3807}
3808
3809/*
3810%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3811% %
3812% %
3813% %
3814+ M a s k P i x e l C a c h e N e x u s %
3815% %
3816% %
3817% %
3818%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3819%
3820% MaskPixelCacheNexus() masks the cache nexus as defined by the image mask.
3821% The method returns MagickTrue if the pixel region is masked, otherwise
3822% MagickFalse.
3823%
3824% The format of the MaskPixelCacheNexus() method is:
3825%
3826% MagickBooleanType MaskPixelCacheNexus(Image *image,
3827% NexusInfo *nexus_info,ExceptionInfo *exception)
3828%
3829% A description of each parameter follows:
3830%
3831% o image: the image.
3832%
3833% o nexus_info: the cache nexus to clip.
3834%
3835% o exception: return any errors or warnings in this structure.
3836%
3837*/
3838
3839static inline void MagickPixelCompositeMask(const MagickPixelPacket *p,
3840 const MagickRealType alpha,const MagickPixelPacket *q,
3841 const MagickRealType beta,MagickPixelPacket *composite)
3842{
3843 MagickRealType
3844 gamma;
3845
3846 if (alpha == TransparentOpacity)
3847 {
3848 *composite=(*q);
3849 return;
3850 }
3851 gamma=1.0-QuantumScale*QuantumScale*alpha*beta;
3852 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
3853 composite->red=gamma*MagickOver_(p->red,alpha,q->red,beta);
3854 composite->green=gamma*MagickOver_(p->green,alpha,q->green,beta);
3855 composite->blue=gamma*MagickOver_(p->blue,alpha,q->blue,beta);
3856 if ((p->colorspace == CMYKColorspace) && (q->colorspace == CMYKColorspace))
3857 composite->index=gamma*MagickOver_(p->index,alpha,q->index,beta);
3858}
3859
3860static MagickBooleanType MaskPixelCacheNexus(Image *image,NexusInfo *nexus_info,
3861 ExceptionInfo *exception)
3862{
3863 CacheInfo
3864 *cache_info;
3865
3866 MagickPixelPacket
3867 alpha,
3868 beta;
3869
3870 MagickSizeType
3871 number_pixels;
3872
3873 NexusInfo
3874 **clip_nexus,
3875 **image_nexus;
3876
3877 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003878 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00003879
3880 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003881 *restrict nexus_indexes,
3882 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00003883
3884 register long
3885 i;
3886
3887 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003888 *restrict p,
3889 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003890
3891 /*
3892 Apply clip mask.
3893 */
3894 if (image->debug != MagickFalse)
3895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3896 if (image->mask == (Image *) NULL)
3897 return(MagickFalse);
3898 cache_info=(CacheInfo *) GetImagePixelCache(image,MagickTrue,exception);
3899 if (cache_info == (Cache) NULL)
3900 return(MagickFalse);
3901 image_nexus=AcquirePixelCacheNexus(1);
3902 clip_nexus=AcquirePixelCacheNexus(1);
3903 if ((image_nexus == (NexusInfo **) NULL) ||
3904 (clip_nexus == (NexusInfo **) NULL))
3905 ThrowBinaryException(CacheError,"UnableToGetCacheNexus",image->filename);
cristy09449f72010-01-23 03:08:36 +00003906 p=GetAuthenticPixelCacheNexus(image,nexus_info->region.x,
3907 nexus_info->region.y,nexus_info->region.width,nexus_info->region.height,
3908 image_nexus[0],exception);
cristy3ed852e2009-09-05 21:47:34 +00003909 indexes=GetPixelCacheNexusIndexes(image->cache,image_nexus[0]);
3910 q=nexus_info->pixels;
3911 nexus_indexes=nexus_info->indexes;
3912 r=GetVirtualPixelsFromNexus(image->mask,MaskVirtualPixelMethod,
3913 nexus_info->region.x,nexus_info->region.y,nexus_info->region.width,
3914 nexus_info->region.height,clip_nexus[0],&image->exception);
3915 GetMagickPixelPacket(image,&alpha);
3916 GetMagickPixelPacket(image,&beta);
3917 number_pixels=(MagickSizeType) nexus_info->region.width*
3918 nexus_info->region.height;
3919 for (i=0; i < (long) number_pixels; i++)
3920 {
3921 if ((p == (PixelPacket *) NULL) || (r == (const PixelPacket *) NULL))
3922 break;
3923 SetMagickPixelPacket(image,p,indexes+i,&alpha);
3924 SetMagickPixelPacket(image,q,nexus_indexes+i,&beta);
3925 MagickPixelCompositeMask(&beta,(MagickRealType) PixelIntensityToQuantum(r),
3926 &alpha,alpha.opacity,&beta);
cristyce70c172010-01-07 17:15:30 +00003927 q->red=ClampToQuantum(beta.red);
3928 q->green=ClampToQuantum(beta.green);
3929 q->blue=ClampToQuantum(beta.blue);
3930 q->opacity=ClampToQuantum(beta.opacity);
cristy3ed852e2009-09-05 21:47:34 +00003931 if (cache_info->active_index_channel != MagickFalse)
3932 nexus_indexes[i]=indexes[i];
3933 p++;
3934 q++;
3935 r++;
3936 }
3937 clip_nexus=DestroyPixelCacheNexus(clip_nexus,1);
3938 image_nexus=DestroyPixelCacheNexus(image_nexus,1);
3939 if (i < (long) number_pixels)
3940 return(MagickFalse);
3941 return(MagickTrue);
3942}
3943
3944/*
3945%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3946% %
3947% %
3948% %
3949+ O p e n P i x e l C a c h e %
3950% %
3951% %
3952% %
3953%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3954%
3955% OpenPixelCache() allocates the pixel cache. This includes defining the cache
3956% dimensions, allocating space for the image pixels and optionally the
3957% colormap indexes, and memory mapping the cache if it is disk based. The
3958% cache nexus array is initialized as well.
3959%
3960% The format of the OpenPixelCache() method is:
3961%
3962% MagickBooleanType OpenPixelCache(Image *image,const MapMode mode,
3963% ExceptionInfo *exception)
3964%
3965% A description of each parameter follows:
3966%
3967% o image: the image.
3968%
3969% o mode: ReadMode, WriteMode, or IOMode.
3970%
3971% o exception: return any errors or warnings in this structure.
3972%
3973*/
3974
cristyd43a46b2010-01-21 02:13:41 +00003975static inline void AllocatePixelCachePixels(CacheInfo *cache_info)
cristy3ed852e2009-09-05 21:47:34 +00003976{
3977 cache_info->mapped=MagickFalse;
3978 cache_info->pixels=(PixelPacket *) AcquireMagickMemory((size_t)
3979 cache_info->length);
3980 if (cache_info->pixels == (PixelPacket *) NULL)
3981 {
3982 cache_info->mapped=MagickTrue;
3983 cache_info->pixels=(PixelPacket *) MapBlob(-1,IOMode,0,(size_t)
3984 cache_info->length);
3985 }
3986}
3987
3988static MagickBooleanType ExtendCache(Image *image,MagickSizeType length)
3989{
3990 CacheInfo
3991 *cache_info;
3992
3993 MagickOffsetType
3994 count,
3995 extent,
3996 offset;
3997
3998 cache_info=(CacheInfo *) image->cache;
3999 if (image->debug != MagickFalse)
4000 {
4001 char
4002 format[MaxTextExtent],
4003 message[MaxTextExtent];
4004
cristyb9080c92009-12-01 20:13:26 +00004005 (void) FormatMagickSize(length,MagickFalse,format);
cristy3ed852e2009-09-05 21:47:34 +00004006 (void) FormatMagickString(message,MaxTextExtent,
4007 "extend %s (%s[%d], disk, %s)",cache_info->filename,
4008 cache_info->cache_filename,cache_info->file,format);
4009 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",message);
4010 }
4011 if (length != (MagickSizeType) ((MagickOffsetType) length))
4012 return(MagickFalse);
4013 extent=(MagickOffsetType) MagickSeek(cache_info->file,0,SEEK_END);
4014 if (extent < 0)
4015 return(MagickFalse);
4016 if ((MagickSizeType) extent >= length)
4017 return(MagickTrue);
4018 offset=(MagickOffsetType) length-1;
4019 count=WritePixelCacheRegion(cache_info,offset,1,(const unsigned char *) "");
4020 return(count == (MagickOffsetType) 1 ? MagickTrue : MagickFalse);
4021}
4022
4023static MagickBooleanType OpenPixelCache(Image *image,const MapMode mode,
4024 ExceptionInfo *exception)
4025{
4026 char
4027 format[MaxTextExtent],
4028 message[MaxTextExtent];
4029
4030 CacheInfo
4031 *cache_info,
4032 source_info;
4033
4034 MagickSizeType
4035 length,
4036 number_pixels;
4037
4038 MagickStatusType
4039 status;
4040
4041 size_t
4042 packet_size;
4043
4044 unsigned long
4045 columns;
4046
4047 if (image->debug != MagickFalse)
4048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4049 if ((image->columns == 0) || (image->rows == 0))
4050 ThrowBinaryException(CacheError,"NoPixelsDefinedInCache",image->filename);
4051 cache_info=(CacheInfo *) image->cache;
4052 source_info=(*cache_info);
4053 source_info.file=(-1);
4054 (void) FormatMagickString(cache_info->filename,MaxTextExtent,"%s[%ld]",
4055 image->filename,GetImageIndexInList(image));
cristy87528ea2009-09-10 14:53:56 +00004056 cache_info->mode=mode;
cristy3ed852e2009-09-05 21:47:34 +00004057 cache_info->rows=image->rows;
4058 cache_info->columns=image->columns;
4059 cache_info->active_index_channel=((image->storage_class == PseudoClass) ||
4060 (image->colorspace == CMYKColorspace)) ? MagickTrue : MagickFalse;
4061 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
4062 packet_size=sizeof(PixelPacket);
4063 if (cache_info->active_index_channel != MagickFalse)
4064 packet_size+=sizeof(IndexPacket);
4065 length=number_pixels*packet_size;
4066 columns=(unsigned long) (length/cache_info->rows/packet_size);
4067 if (cache_info->columns != columns)
4068 ThrowBinaryException(ResourceLimitError,"PixelCacheAllocationFailed",
4069 image->filename);
4070 cache_info->length=length;
4071 status=AcquireMagickResource(AreaResource,cache_info->length);
4072 length=number_pixels*(sizeof(PixelPacket)+sizeof(IndexPacket));
4073 if ((status != MagickFalse) && (length == (MagickSizeType) ((size_t) length)))
4074 {
4075 status=AcquireMagickResource(MemoryResource,cache_info->length);
4076 if (((cache_info->type == UndefinedCache) && (status != MagickFalse)) ||
4077 (cache_info->type == MemoryCache))
4078 {
cristyd43a46b2010-01-21 02:13:41 +00004079 AllocatePixelCachePixels(cache_info);
cristy3ed852e2009-09-05 21:47:34 +00004080 if (cache_info->pixels == (PixelPacket *) NULL)
4081 cache_info->pixels=source_info.pixels;
4082 else
4083 {
4084 /*
4085 Create memory pixel cache.
4086 */
4087 if (image->debug != MagickFalse)
4088 {
cristy97e7a572009-12-05 15:07:53 +00004089 (void) FormatMagickSize(cache_info->length,MagickTrue,
cristyb9080c92009-12-01 20:13:26 +00004090 format);
cristy3ed852e2009-09-05 21:47:34 +00004091 (void) FormatMagickString(message,MaxTextExtent,
4092 "open %s (%s memory, %lux%lu %s)",cache_info->filename,
4093 cache_info->mapped != MagickFalse ? "anonymous" : "heap",
4094 cache_info->columns,cache_info->rows,format);
4095 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",
4096 message);
4097 }
4098 cache_info->storage_class=image->storage_class;
4099 cache_info->colorspace=image->colorspace;
4100 cache_info->type=MemoryCache;
4101 cache_info->indexes=(IndexPacket *) NULL;
4102 if (cache_info->active_index_channel != MagickFalse)
4103 cache_info->indexes=(IndexPacket *) (cache_info->pixels+
4104 number_pixels);
4105 if (source_info.storage_class != UndefinedClass)
4106 {
4107 status|=ClonePixelCachePixels(cache_info,&source_info,
4108 exception);
4109 RelinquishPixelCachePixels(&source_info);
4110 }
4111 return(MagickTrue);
4112 }
4113 }
4114 RelinquishMagickResource(MemoryResource,cache_info->length);
4115 }
4116 /*
4117 Create pixel cache on disk.
4118 */
4119 status=AcquireMagickResource(DiskResource,cache_info->length);
4120 if (status == MagickFalse)
4121 {
4122 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
4123 "CacheResourcesExhausted","`%s'",image->filename);
4124 return(MagickFalse);
4125 }
4126 if (OpenPixelCacheOnDisk(cache_info,mode) == MagickFalse)
4127 {
4128 RelinquishMagickResource(DiskResource,cache_info->length);
4129 ThrowFileException(exception,CacheError,"UnableToOpenPixelCache",
4130 image->filename);
4131 return(MagickFalse);
4132 }
4133 status=ExtendCache(image,(MagickSizeType) cache_info->offset+
4134 cache_info->length);
4135 if (status == MagickFalse)
4136 {
4137 ThrowFileException(exception,CacheError,"UnableToExtendCache",
4138 image->filename);
4139 return(MagickFalse);
4140 }
4141 cache_info->storage_class=image->storage_class;
4142 cache_info->colorspace=image->colorspace;
4143 length=number_pixels*(sizeof(PixelPacket)+sizeof(IndexPacket));
4144 status=AcquireMagickResource(AreaResource,cache_info->length);
4145 if ((status == MagickFalse) || (length != (MagickSizeType) ((size_t) length)))
4146 cache_info->type=DiskCache;
4147 else
4148 {
4149 status=AcquireMagickResource(MapResource,cache_info->length);
4150 if ((status == MagickFalse) && (cache_info->type != MapCache) &&
4151 (cache_info->type != MemoryCache))
4152 cache_info->type=DiskCache;
4153 else
4154 {
4155 cache_info->pixels=(PixelPacket *) MapBlob(cache_info->file,mode,
4156 cache_info->offset,(size_t) cache_info->length);
4157 if (cache_info->pixels == (PixelPacket *) NULL)
4158 {
4159 cache_info->pixels=source_info.pixels;
4160 cache_info->type=DiskCache;
4161 }
4162 else
4163 {
4164 /*
4165 Create file-backed memory-mapped pixel cache.
4166 */
4167 (void) ClosePixelCacheOnDisk(cache_info);
4168 cache_info->type=MapCache;
4169 cache_info->mapped=MagickTrue;
4170 cache_info->indexes=(IndexPacket *) NULL;
4171 if (cache_info->active_index_channel != MagickFalse)
4172 cache_info->indexes=(IndexPacket *) (cache_info->pixels+
4173 number_pixels);
4174 if ((source_info.type != UndefinedCache) && (mode != ReadMode))
4175 {
4176 status=ClonePixelCachePixels(cache_info,&source_info,
4177 exception);
4178 RelinquishPixelCachePixels(&source_info);
4179 }
4180 if (image->debug != MagickFalse)
4181 {
cristy97e7a572009-12-05 15:07:53 +00004182 (void) FormatMagickSize(cache_info->length,MagickTrue,
cristyb9080c92009-12-01 20:13:26 +00004183 format);
cristy3ed852e2009-09-05 21:47:34 +00004184 (void) FormatMagickString(message,MaxTextExtent,
4185 "open %s (%s[%d], memory-mapped, %lux%lu %s)",
4186 cache_info->filename,cache_info->cache_filename,
4187 cache_info->file,cache_info->columns,cache_info->rows,
4188 format);
4189 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",
4190 message);
4191 }
4192 return(MagickTrue);
4193 }
4194 }
4195 RelinquishMagickResource(MapResource,cache_info->length);
4196 }
4197 if ((source_info.type != UndefinedCache) && (mode != ReadMode))
4198 {
4199 status=ClonePixelCachePixels(cache_info,&source_info,exception);
4200 RelinquishPixelCachePixels(&source_info);
4201 }
4202 if (image->debug != MagickFalse)
4203 {
cristyb9080c92009-12-01 20:13:26 +00004204 (void) FormatMagickSize(cache_info->length,MagickFalse,format);
cristy3ed852e2009-09-05 21:47:34 +00004205 (void) FormatMagickString(message,MaxTextExtent,
4206 "open %s (%s[%d], disk, %lux%lu %s)",cache_info->filename,
4207 cache_info->cache_filename,cache_info->file,cache_info->columns,
4208 cache_info->rows,format);
4209 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",message);
4210 }
4211 return(MagickTrue);
4212}
4213
4214/*
4215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4216% %
4217% %
4218% %
4219+ P e r s i s t P i x e l C a c h e %
4220% %
4221% %
4222% %
4223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4224%
4225% PersistPixelCache() attaches to or initializes a persistent pixel cache. A
4226% persistent pixel cache is one that resides on disk and is not destroyed
4227% when the program exits.
4228%
4229% The format of the PersistPixelCache() method is:
4230%
4231% MagickBooleanType PersistPixelCache(Image *image,const char *filename,
4232% const MagickBooleanType attach,MagickOffsetType *offset,
4233% ExceptionInfo *exception)
4234%
4235% A description of each parameter follows:
4236%
4237% o image: the image.
4238%
4239% o filename: the persistent pixel cache filename.
4240%
cristy01b7eb02009-09-10 23:10:14 +00004241% o attach: A value other than zero initializes the persistent pixel
4242% cache.
4243%
cristy3ed852e2009-09-05 21:47:34 +00004244% o initialize: A value other than zero initializes the persistent pixel
4245% cache.
4246%
4247% o offset: the offset in the persistent cache to store pixels.
4248%
4249% o exception: return any errors or warnings in this structure.
4250%
4251*/
4252MagickExport MagickBooleanType PersistPixelCache(Image *image,
4253 const char *filename,const MagickBooleanType attach,MagickOffsetType *offset,
4254 ExceptionInfo *exception)
4255{
4256 CacheInfo
4257 *cache_info,
4258 *clone_info;
4259
4260 Image
4261 clone_image;
4262
4263 long
cristy688f07b2009-09-27 15:19:13 +00004264 page_size;
cristy3ed852e2009-09-05 21:47:34 +00004265
4266 MagickBooleanType
4267 status;
4268
4269 assert(image != (Image *) NULL);
4270 assert(image->signature == MagickSignature);
4271 if (image->debug != MagickFalse)
4272 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4273 assert(image->cache != (void *) NULL);
4274 assert(filename != (const char *) NULL);
4275 assert(offset != (MagickOffsetType *) NULL);
cristy688f07b2009-09-27 15:19:13 +00004276 page_size=GetMagickPageSize();
cristy3ed852e2009-09-05 21:47:34 +00004277 cache_info=(CacheInfo *) image->cache;
4278 assert(cache_info->signature == MagickSignature);
4279 if (attach != MagickFalse)
4280 {
4281 /*
cristy01b7eb02009-09-10 23:10:14 +00004282 Attach existing persistent pixel cache.
cristy3ed852e2009-09-05 21:47:34 +00004283 */
4284 if (image->debug != MagickFalse)
4285 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
4286 "attach persistent cache");
4287 (void) CopyMagickString(cache_info->cache_filename,filename,
4288 MaxTextExtent);
4289 cache_info->type=DiskCache;
4290 cache_info->offset=(*offset);
4291 if (OpenPixelCache(image,ReadMode,exception) == MagickFalse)
4292 return(MagickFalse);
cristy688f07b2009-09-27 15:19:13 +00004293 *offset+=cache_info->length+page_size-(cache_info->length % page_size);
cristy3ed852e2009-09-05 21:47:34 +00004294 return(MagickTrue);
4295 }
cristy01b7eb02009-09-10 23:10:14 +00004296 if ((cache_info->mode != ReadMode) && (cache_info->type != MemoryCache) &&
4297 (cache_info->reference_count == 1))
cristy3ed852e2009-09-05 21:47:34 +00004298 {
cristyf84a1932010-01-03 18:00:18 +00004299 LockSemaphoreInfo(cache_info->semaphore);
cristy09449f72010-01-23 03:08:36 +00004300 if ((cache_info->mode != ReadMode) &&
4301 (cache_info->type != MemoryCache) &&
cristy3ed852e2009-09-05 21:47:34 +00004302 (cache_info->reference_count == 1))
4303 {
4304 int
4305 status;
4306
4307 /*
cristy01b7eb02009-09-10 23:10:14 +00004308 Usurp existing persistent pixel cache.
cristy3ed852e2009-09-05 21:47:34 +00004309 */
4310 status=rename(cache_info->cache_filename,filename);
4311 if (status == 0)
4312 {
4313 (void) CopyMagickString(cache_info->cache_filename,filename,
4314 MaxTextExtent);
cristy688f07b2009-09-27 15:19:13 +00004315 *offset+=cache_info->length+page_size-(cache_info->length %
4316 page_size);
cristyf84a1932010-01-03 18:00:18 +00004317 UnlockSemaphoreInfo(cache_info->semaphore);
cristy87528ea2009-09-10 14:53:56 +00004318 cache_info=(CacheInfo *) ReferencePixelCache(cache_info);
cristy3ed852e2009-09-05 21:47:34 +00004319 if (image->debug != MagickFalse)
4320 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
4321 "Usurp resident persistent cache");
cristy3ed852e2009-09-05 21:47:34 +00004322 return(MagickTrue);
4323 }
4324 }
cristyf84a1932010-01-03 18:00:18 +00004325 UnlockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00004326 }
4327 /*
cristy01b7eb02009-09-10 23:10:14 +00004328 Clone persistent pixel cache.
cristy3ed852e2009-09-05 21:47:34 +00004329 */
4330 clone_image=(*image);
4331 clone_info=(CacheInfo *) clone_image.cache;
4332 image->cache=ClonePixelCache(cache_info);
4333 cache_info=(CacheInfo *) ReferencePixelCache(image->cache);
4334 (void) CopyMagickString(cache_info->cache_filename,filename,MaxTextExtent);
4335 cache_info->type=DiskCache;
4336 cache_info->offset=(*offset);
4337 cache_info=(CacheInfo *) image->cache;
4338 status=ClonePixelCacheNexus(cache_info,clone_info,exception);
4339 if (status != MagickFalse)
4340 {
4341 status=OpenPixelCache(image,IOMode,exception);
4342 if (status != MagickFalse)
4343 status=ClonePixelCachePixels(cache_info,clone_info,&image->exception);
4344 }
cristy688f07b2009-09-27 15:19:13 +00004345 *offset+=cache_info->length+page_size-(cache_info->length % page_size);
cristy3ed852e2009-09-05 21:47:34 +00004346 clone_info=(CacheInfo *) DestroyPixelCache(clone_info);
4347 return(status);
4348}
4349
4350/*
4351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4352% %
4353% %
4354% %
4355+ Q u e u e A u t h e n t i c N e x u s %
4356% %
4357% %
4358% %
4359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4360%
4361% QueueAuthenticNexus() allocates an region to store image pixels as defined
4362% by the region rectangle and returns a pointer to the region. This region is
4363% subsequently transferred from the pixel cache with
4364% SyncAuthenticPixelsCache(). A pointer to the pixels is returned if the
4365% pixels are transferred, otherwise a NULL is returned.
4366%
4367% The format of the QueueAuthenticNexus() method is:
4368%
4369% PixelPacket *QueueAuthenticNexus(Image *image,const long x,const long y,
4370% const unsigned long columns,const unsigned long rows,
4371% NexusInfo *nexus_info,ExceptionInfo *exception)
4372%
4373% A description of each parameter follows:
4374%
4375% o image: the image.
4376%
4377% o x,y,columns,rows: These values define the perimeter of a region of
4378% pixels.
4379%
4380% o nexus_info: the cache nexus to set.
4381%
4382% o exception: return any errors or warnings in this structure.
4383%
4384*/
4385MagickExport PixelPacket *QueueAuthenticNexus(Image *image,const long x,
4386 const long y,const unsigned long columns,const unsigned long rows,
4387 NexusInfo *nexus_info,ExceptionInfo *exception)
4388{
4389 CacheInfo
4390 *cache_info;
4391
4392 MagickOffsetType
4393 offset;
4394
4395 MagickSizeType
4396 number_pixels;
4397
4398 RectangleInfo
4399 region;
4400
4401 /*
4402 Validate pixel cache geometry.
4403 */
4404 cache_info=(CacheInfo *) image->cache;
4405 if ((cache_info->columns == 0) && (cache_info->rows == 0))
4406 {
4407 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
4408 "NoPixelsDefinedInCache","`%s'",image->filename);
4409 return((PixelPacket *) NULL);
4410 }
4411 if ((x < 0) || (y < 0) || (x >= (long) cache_info->columns) ||
4412 (y >= (long) cache_info->rows))
4413 {
4414 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
4415 "PixelsAreNotAuthentic","`%s'",image->filename);
4416 return((PixelPacket *) NULL);
4417 }
4418 offset=(MagickOffsetType) y*cache_info->columns+x;
4419 if (offset < 0)
4420 return((PixelPacket *) NULL);
4421 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
4422 offset+=(MagickOffsetType) (rows-1)*cache_info->columns+columns-1;
4423 if ((MagickSizeType) offset >= number_pixels)
4424 return((PixelPacket *) NULL);
4425 /*
4426 Return pixel cache.
4427 */
4428 region.x=x;
4429 region.y=y;
4430 region.width=columns;
4431 region.height=rows;
4432 return(SetPixelCacheNexusPixels(image,&region,nexus_info,exception));
4433}
4434
4435/*
4436%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4437% %
4438% %
4439% %
4440+ 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 %
4441% %
4442% %
4443% %
4444%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4445%
4446% QueueAuthenticPixelsCache() allocates an region to store image pixels as
4447% defined by the region rectangle and returns a pointer to the region. This
4448% region is subsequently transferred from the pixel cache with
4449% SyncAuthenticPixelsCache(). A pointer to the pixels is returned if the
4450% pixels are transferred, otherwise a NULL is returned.
4451%
4452% The format of the QueueAuthenticPixelsCache() method is:
4453%
4454% PixelPacket *QueueAuthenticPixelsCache(Image *image,const long x,
4455% const long y,const unsigned long columns,const unsigned long rows,
4456% ExceptionInfo *exception)
4457%
4458% A description of each parameter follows:
4459%
4460% o image: the image.
4461%
4462% o x,y,columns,rows: These values define the perimeter of a region of
4463% pixels.
4464%
4465% o exception: return any errors or warnings in this structure.
4466%
4467*/
4468static PixelPacket *QueueAuthenticPixelsCache(Image *image,const long x,
4469 const long y,const unsigned long columns,const unsigned long rows,
4470 ExceptionInfo *exception)
4471{
4472 CacheInfo
4473 *cache_info;
4474
4475 long
4476 id;
4477
4478 PixelPacket
4479 *pixels;
4480
4481 cache_info=(CacheInfo *) GetImagePixelCache(image,MagickFalse,exception);
4482 if (cache_info == (Cache) NULL)
4483 return((PixelPacket *) NULL);
4484 id=GetOpenMPThreadId();
4485 assert(id < (long) cache_info->number_threads);
4486 pixels=QueueAuthenticNexus(image,x,y,columns,rows,cache_info->nexus_info[id],
4487 exception);
4488 return(pixels);
4489}
4490
4491/*
4492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4493% %
4494% %
4495% %
4496% Q u e u e A u t h e n t i c P i x e l s %
4497% %
4498% %
4499% %
4500%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4501%
4502% QueueAuthenticPixels() queues a mutable pixel region. If the region is
4503% successfully intialized a pointer to a PixelPacket array representing the
4504% region is returned, otherwise NULL is returned. The returned pointer may
4505% point to a temporary working buffer for the pixels or it may point to the
4506% final location of the pixels in memory.
4507%
4508% Write-only access means that any existing pixel values corresponding to
4509% the region are ignored. This is useful if the initial image is being
4510% created from scratch, or if the existing pixel values are to be
4511% completely replaced without need to refer to their pre-existing values.
4512% The application is free to read and write the pixel buffer returned by
4513% QueueAuthenticPixels() any way it pleases. QueueAuthenticPixels() does not
4514% initialize the pixel array values. Initializing pixel array values is the
4515% application's responsibility.
4516%
4517% Performance is maximized if the selected region is part of one row, or
4518% one or more full rows, since then there is opportunity to access the
cristyad4e5b22010-01-10 00:04:19 +00004519% pixels in-place (without a copy) if the image is in memory, or in a
4520% memory-mapped file. The returned pointer must *never* be deallocated
cristy3ed852e2009-09-05 21:47:34 +00004521% by the user.
4522%
4523% Pixels accessed via the returned pointer represent a simple array of type
4524% PixelPacket. If the image type is CMYK or the storage class is PseudoClass,
4525% call GetAuthenticIndexQueue() after invoking GetAuthenticPixels() to obtain
4526% the black color component or the colormap indexes (of type IndexPacket)
4527% corresponding to the region. Once the PixelPacket (and/or IndexPacket)
4528% array has been updated, the changes must be saved back to the underlying
4529% image using SyncAuthenticPixels() or they may be lost.
4530%
4531% The format of the QueueAuthenticPixels() method is:
4532%
4533% PixelPacket *QueueAuthenticPixels(Image *image,const long x,const long y,
4534% const unsigned long columns,const unsigned long rows,
4535% ExceptionInfo *exception)
4536%
4537% A description of each parameter follows:
4538%
4539% o image: the image.
4540%
4541% o x,y,columns,rows: These values define the perimeter of a region of
4542% pixels.
4543%
4544% o exception: return any errors or warnings in this structure.
4545%
4546*/
4547MagickExport PixelPacket *QueueAuthenticPixels(Image *image,const long x,
4548 const long y,const unsigned long columns,const unsigned long rows,
4549 ExceptionInfo *exception)
4550{
4551 CacheInfo
4552 *cache_info;
4553
4554 PixelPacket
4555 *pixels;
4556
4557 assert(image != (Image *) NULL);
4558 assert(image->signature == MagickSignature);
4559 if (image->debug != MagickFalse)
4560 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4561 assert(image->cache != (Cache) NULL);
4562 cache_info=(CacheInfo *) image->cache;
4563 assert(cache_info->signature == MagickSignature);
4564 if (cache_info->methods.queue_authentic_pixels_handler ==
4565 (QueueAuthenticPixelsHandler) NULL)
4566 return((PixelPacket *) NULL);
4567 pixels=cache_info->methods.queue_authentic_pixels_handler(image,x,y,columns,
4568 rows,exception);
4569 return(pixels);
4570}
4571
4572/*
4573%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4574% %
4575% %
4576% %
4577+ R e a d P i x e l C a c h e I n d e x e s %
4578% %
4579% %
4580% %
4581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4582%
4583% ReadPixelCacheIndexes() reads colormap indexes from the specified region of
4584% the pixel cache.
4585%
4586% The format of the ReadPixelCacheIndexes() method is:
4587%
4588% MagickBooleanType ReadPixelCacheIndexes(CacheInfo *cache_info,
4589% NexusInfo *nexus_info,ExceptionInfo *exception)
4590%
4591% A description of each parameter follows:
4592%
4593% o cache_info: the pixel cache.
4594%
4595% o nexus_info: the cache nexus to read the colormap indexes.
4596%
4597% o exception: return any errors or warnings in this structure.
4598%
4599*/
4600static MagickBooleanType ReadPixelCacheIndexes(CacheInfo *cache_info,
4601 NexusInfo *nexus_info,ExceptionInfo *exception)
4602{
4603 MagickOffsetType
4604 count,
4605 offset;
4606
4607 MagickSizeType
4608 length,
4609 number_pixels;
4610
4611 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004612 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004613
4614 register long
4615 y;
4616
4617 unsigned long
4618 rows;
4619
4620 if (cache_info->debug != MagickFalse)
4621 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4622 cache_info->filename);
4623 if (cache_info->active_index_channel == MagickFalse)
4624 return(MagickFalse);
4625 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
4626 return(MagickTrue);
4627 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
4628 nexus_info->region.x;
4629 length=(MagickSizeType) nexus_info->region.width*sizeof(IndexPacket);
4630 rows=nexus_info->region.height;
4631 number_pixels=length*rows;
cristy3ed852e2009-09-05 21:47:34 +00004632 q=nexus_info->indexes;
4633 switch (cache_info->type)
4634 {
4635 case MemoryCache:
4636 case MapCache:
4637 {
4638 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004639 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004640
4641 /*
4642 Read indexes from memory.
4643 */
cristydd341db2010-03-04 19:06:38 +00004644 if ((cache_info->columns == nexus_info->region.width) &&
4645 (number_pixels == (MagickSizeType) ((size_t) number_pixels)))
4646 {
4647 length=number_pixels;
4648 rows=1UL;
4649 }
cristy3ed852e2009-09-05 21:47:34 +00004650 p=cache_info->indexes+offset;
4651 for (y=0; y < (long) rows; y++)
4652 {
4653 (void) CopyMagickMemory(q,p,(size_t) length);
4654 p+=cache_info->columns;
4655 q+=nexus_info->region.width;
4656 }
4657 break;
4658 }
4659 case DiskCache:
4660 {
4661 /*
4662 Read indexes from disk.
4663 */
4664 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
4665 {
4666 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
4667 cache_info->cache_filename);
4668 return(MagickFalse);
4669 }
cristydd341db2010-03-04 19:06:38 +00004670 if ((cache_info->columns == nexus_info->region.width) &&
4671 (number_pixels < MagickMaxBufferExtent))
4672 {
4673 length=number_pixels;
4674 rows=1UL;
4675 }
cristy3ed852e2009-09-05 21:47:34 +00004676 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
4677 for (y=0; y < (long) rows; y++)
4678 {
4679 count=ReadPixelCacheRegion(cache_info,cache_info->offset+number_pixels*
4680 sizeof(PixelPacket)+offset*sizeof(*q),length,(unsigned char *) q);
4681 if ((MagickSizeType) count < length)
4682 break;
4683 offset+=cache_info->columns;
4684 q+=nexus_info->region.width;
4685 }
4686 if (y < (long) rows)
4687 {
4688 ThrowFileException(exception,CacheError,"UnableToReadPixelCache",
4689 cache_info->cache_filename);
4690 return(MagickFalse);
4691 }
4692 break;
4693 }
4694 default:
4695 break;
4696 }
4697 if ((cache_info->debug != MagickFalse) &&
4698 (QuantumTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
4699 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s[%lux%lu%+ld%+ld]",
4700 cache_info->filename,nexus_info->region.width,nexus_info->region.height,
4701 nexus_info->region.x,nexus_info->region.y);
4702 return(MagickTrue);
4703}
4704
4705/*
4706%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4707% %
4708% %
4709% %
4710+ R e a d P i x e l C a c h e P i x e l s %
4711% %
4712% %
4713% %
4714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4715%
4716% ReadPixelCachePixels() reads pixels from the specified region of the pixel
4717% cache.
4718%
4719% The format of the ReadPixelCachePixels() method is:
4720%
4721% MagickBooleanType ReadPixelCachePixels(CacheInfo *cache_info,
4722% NexusInfo *nexus_info,ExceptionInfo *exception)
4723%
4724% A description of each parameter follows:
4725%
4726% o cache_info: the pixel cache.
4727%
4728% o nexus_info: the cache nexus to read the pixels.
4729%
4730% o exception: return any errors or warnings in this structure.
4731%
4732*/
4733static MagickBooleanType ReadPixelCachePixels(CacheInfo *cache_info,
4734 NexusInfo *nexus_info,ExceptionInfo *exception)
4735{
4736 MagickOffsetType
4737 count,
4738 offset;
4739
4740 MagickSizeType
4741 length,
4742 number_pixels;
4743
4744 register long
4745 y;
4746
4747 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004748 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004749
4750 unsigned long
4751 rows;
4752
4753 if (cache_info->debug != MagickFalse)
4754 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4755 cache_info->filename);
4756 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
4757 return(MagickTrue);
4758 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
4759 nexus_info->region.x;
4760 length=(MagickSizeType) nexus_info->region.width*sizeof(PixelPacket);
4761 rows=nexus_info->region.height;
4762 number_pixels=length*rows;
cristy3ed852e2009-09-05 21:47:34 +00004763 q=nexus_info->pixels;
4764 switch (cache_info->type)
4765 {
4766 case MemoryCache:
4767 case MapCache:
4768 {
4769 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004770 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004771
4772 /*
4773 Read pixels from memory.
4774 */
cristydd341db2010-03-04 19:06:38 +00004775 if ((cache_info->columns == nexus_info->region.width) &&
4776 (number_pixels == (MagickSizeType) ((size_t) number_pixels)))
4777 {
4778 length=number_pixels;
4779 rows=1UL;
4780 }
cristy3ed852e2009-09-05 21:47:34 +00004781 p=cache_info->pixels+offset;
4782 for (y=0; y < (long) rows; y++)
4783 {
4784 (void) CopyMagickMemory(q,p,(size_t) length);
4785 p+=cache_info->columns;
4786 q+=nexus_info->region.width;
4787 }
4788 break;
4789 }
4790 case DiskCache:
4791 {
4792 /*
4793 Read pixels from disk.
4794 */
4795 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
4796 {
4797 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
4798 cache_info->cache_filename);
4799 return(MagickFalse);
4800 }
cristydd341db2010-03-04 19:06:38 +00004801 if ((cache_info->columns == nexus_info->region.width) &&
4802 (number_pixels < MagickMaxBufferExtent))
4803 {
4804 length=number_pixels;
4805 rows=1UL;
4806 }
cristy3ed852e2009-09-05 21:47:34 +00004807 for (y=0; y < (long) rows; y++)
4808 {
4809 count=ReadPixelCacheRegion(cache_info,cache_info->offset+offset*
4810 sizeof(*q),length,(unsigned char *) q);
4811 if ((MagickSizeType) count < length)
4812 break;
4813 offset+=cache_info->columns;
4814 q+=nexus_info->region.width;
4815 }
4816 if (y < (long) rows)
4817 {
4818 ThrowFileException(exception,CacheError,"UnableToReadPixelCache",
4819 cache_info->cache_filename);
4820 return(MagickFalse);
4821 }
4822 break;
4823 }
4824 default:
4825 break;
4826 }
4827 if ((cache_info->debug != MagickFalse) &&
4828 (QuantumTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
4829 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s[%lux%lu%+ld%+ld]",
4830 cache_info->filename,nexus_info->region.width,nexus_info->region.height,
4831 nexus_info->region.x,nexus_info->region.y);
4832 return(MagickTrue);
4833}
4834
4835/*
4836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4837% %
4838% %
4839% %
4840+ R e f e r e n c e P i x e l C a c h e %
4841% %
4842% %
4843% %
4844%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4845%
4846% ReferencePixelCache() increments the reference count associated with the
4847% pixel cache returning a pointer to the cache.
4848%
4849% The format of the ReferencePixelCache method is:
4850%
4851% Cache ReferencePixelCache(Cache cache_info)
4852%
4853% A description of each parameter follows:
4854%
4855% o cache_info: the pixel cache.
4856%
4857*/
4858MagickExport Cache ReferencePixelCache(Cache cache)
4859{
4860 CacheInfo
4861 *cache_info;
4862
4863 assert(cache != (Cache *) NULL);
4864 cache_info=(CacheInfo *) cache;
4865 assert(cache_info->signature == MagickSignature);
4866 if (cache_info->debug != MagickFalse)
4867 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4868 cache_info->filename);
cristyf84a1932010-01-03 18:00:18 +00004869 LockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00004870 cache_info->reference_count++;
cristyf84a1932010-01-03 18:00:18 +00004871 UnlockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00004872 return(cache_info);
4873}
4874
4875/*
4876%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4877% %
4878% %
4879% %
4880+ S e t P i x e l C a c h e M e t h o d s %
4881% %
4882% %
4883% %
4884%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4885%
4886% SetPixelCacheMethods() sets the image pixel methods to the specified ones.
4887%
4888% The format of the SetPixelCacheMethods() method is:
4889%
4890% SetPixelCacheMethods(Cache *,CacheMethods *cache_methods)
4891%
4892% A description of each parameter follows:
4893%
4894% o cache: the pixel cache.
4895%
4896% o cache_methods: Specifies a pointer to a CacheMethods structure.
4897%
4898*/
4899MagickExport void SetPixelCacheMethods(Cache cache,CacheMethods *cache_methods)
4900{
4901 CacheInfo
4902 *cache_info;
4903
4904 GetOneAuthenticPixelFromHandler
4905 get_one_authentic_pixel_from_handler;
4906
4907 GetOneVirtualPixelFromHandler
4908 get_one_virtual_pixel_from_handler;
4909
4910 /*
4911 Set cache pixel methods.
4912 */
4913 assert(cache != (Cache) NULL);
4914 assert(cache_methods != (CacheMethods *) NULL);
4915 cache_info=(CacheInfo *) cache;
4916 assert(cache_info->signature == MagickSignature);
4917 if (cache_info->debug != MagickFalse)
4918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4919 cache_info->filename);
4920 if (cache_methods->get_virtual_pixel_handler != (GetVirtualPixelHandler) NULL)
4921 cache_info->methods.get_virtual_pixel_handler=
4922 cache_methods->get_virtual_pixel_handler;
4923 if (cache_methods->destroy_pixel_handler != (DestroyPixelHandler) NULL)
4924 cache_info->methods.destroy_pixel_handler=
4925 cache_methods->destroy_pixel_handler;
4926 if (cache_methods->get_virtual_indexes_from_handler !=
4927 (GetVirtualIndexesFromHandler) NULL)
4928 cache_info->methods.get_virtual_indexes_from_handler=
4929 cache_methods->get_virtual_indexes_from_handler;
4930 if (cache_methods->get_authentic_pixels_handler !=
4931 (GetAuthenticPixelsHandler) NULL)
4932 cache_info->methods.get_authentic_pixels_handler=
4933 cache_methods->get_authentic_pixels_handler;
4934 if (cache_methods->queue_authentic_pixels_handler !=
4935 (QueueAuthenticPixelsHandler) NULL)
4936 cache_info->methods.queue_authentic_pixels_handler=
4937 cache_methods->queue_authentic_pixels_handler;
4938 if (cache_methods->sync_authentic_pixels_handler !=
4939 (SyncAuthenticPixelsHandler) NULL)
4940 cache_info->methods.sync_authentic_pixels_handler=
4941 cache_methods->sync_authentic_pixels_handler;
4942 if (cache_methods->get_authentic_pixels_from_handler !=
4943 (GetAuthenticPixelsFromHandler) NULL)
4944 cache_info->methods.get_authentic_pixels_from_handler=
4945 cache_methods->get_authentic_pixels_from_handler;
4946 if (cache_methods->get_authentic_indexes_from_handler !=
4947 (GetAuthenticIndexesFromHandler) NULL)
4948 cache_info->methods.get_authentic_indexes_from_handler=
4949 cache_methods->get_authentic_indexes_from_handler;
4950 get_one_virtual_pixel_from_handler=
4951 cache_info->methods.get_one_virtual_pixel_from_handler;
4952 if (get_one_virtual_pixel_from_handler !=
4953 (GetOneVirtualPixelFromHandler) NULL)
4954 cache_info->methods.get_one_virtual_pixel_from_handler=
4955 cache_methods->get_one_virtual_pixel_from_handler;
4956 get_one_authentic_pixel_from_handler=
4957 cache_methods->get_one_authentic_pixel_from_handler;
4958 if (get_one_authentic_pixel_from_handler !=
4959 (GetOneAuthenticPixelFromHandler) NULL)
4960 cache_info->methods.get_one_authentic_pixel_from_handler=
4961 cache_methods->get_one_authentic_pixel_from_handler;
4962}
4963
4964/*
4965%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4966% %
4967% %
4968% %
4969+ S e t P i x e l C a c h e N e x u s P i x e l s %
4970% %
4971% %
4972% %
4973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4974%
4975% SetPixelCacheNexusPixels() defines the region of the cache for the
4976% specified cache nexus.
4977%
4978% The format of the SetPixelCacheNexusPixels() method is:
4979%
4980% PixelPacket SetPixelCacheNexusPixels(const Image *image,
4981% const RectangleInfo *region,NexusInfo *nexus_info,
4982% ExceptionInfo *exception)
4983%
4984% A description of each parameter follows:
4985%
4986% o image: the image.
4987%
4988% o region: A pointer to the RectangleInfo structure that defines the
4989% region of this particular cache nexus.
4990%
4991% o nexus_info: the cache nexus to set.
4992%
4993% o exception: return any errors or warnings in this structure.
4994%
4995*/
4996static PixelPacket *SetPixelCacheNexusPixels(const Image *image,
4997 const RectangleInfo *region,NexusInfo *nexus_info,ExceptionInfo *exception)
4998{
4999 CacheInfo
5000 *cache_info;
5001
5002 MagickBooleanType
5003 status;
5004
cristy3ed852e2009-09-05 21:47:34 +00005005 MagickSizeType
5006 length,
5007 number_pixels;
5008
5009 if (image->debug != MagickFalse)
5010 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5011 cache_info=(CacheInfo *) image->cache;
5012 assert(cache_info->signature == MagickSignature);
5013 if (cache_info->type == UndefinedCache)
5014 return((PixelPacket *) NULL);
5015 nexus_info->region.width=region->width == 0UL ? 1UL : region->width;
5016 nexus_info->region.height=region->height == 0UL ? 1UL : region->height;
5017 nexus_info->region.x=region->x;
5018 nexus_info->region.y=region->y;
5019 if ((cache_info->type != DiskCache) && (image->clip_mask == (Image *) NULL) &&
5020 (image->mask == (Image *) NULL))
5021 {
cristybad067a2010-02-15 17:20:55 +00005022 long
5023 x,
5024 y;
cristy3ed852e2009-09-05 21:47:34 +00005025
cristybad067a2010-02-15 17:20:55 +00005026 x=nexus_info->region.x+nexus_info->region.width-1;
5027 y=nexus_info->region.y+nexus_info->region.height-1;
5028 if (((nexus_info->region.x >= 0) && (x < (long) cache_info->columns) &&
5029 (nexus_info->region.y >= 0) && (y < (long) cache_info->rows)) &&
cristy731c3532010-02-15 15:40:03 +00005030 ((nexus_info->region.height == 1UL) ||
5031 ((nexus_info->region.x == 0) &&
5032 ((nexus_info->region.width == cache_info->columns) ||
5033 ((nexus_info->region.width % cache_info->columns) == 0)))))
5034 {
5035 MagickOffsetType
5036 offset;
5037
5038 /*
5039 Pixels are accessed directly from memory.
5040 */
5041 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
5042 nexus_info->region.x;
5043 nexus_info->pixels=cache_info->pixels+offset;
5044 nexus_info->indexes=(IndexPacket *) NULL;
5045 if (cache_info->active_index_channel != MagickFalse)
5046 nexus_info->indexes=cache_info->indexes+offset;
5047 return(nexus_info->pixels);
cristy3ed852e2009-09-05 21:47:34 +00005048 }
5049 }
5050 /*
5051 Pixels are stored in a cache region until they are synced to the cache.
5052 */
5053 number_pixels=(MagickSizeType) nexus_info->region.width*
5054 nexus_info->region.height;
5055 length=number_pixels*sizeof(PixelPacket);
5056 if (cache_info->active_index_channel != MagickFalse)
5057 length+=number_pixels*sizeof(IndexPacket);
5058 if (nexus_info->cache == (PixelPacket *) NULL)
5059 {
5060 nexus_info->length=length;
5061 status=AcquireCacheNexusPixels(cache_info,nexus_info,exception);
5062 if (status == MagickFalse)
5063 return((PixelPacket *) NULL);
5064 }
5065 else
5066 if (nexus_info->length != length)
5067 {
5068 RelinquishCacheNexusPixels(nexus_info);
5069 nexus_info->length=length;
5070 status=AcquireCacheNexusPixels(cache_info,nexus_info,exception);
5071 if (status == MagickFalse)
5072 return((PixelPacket *) NULL);
5073 }
5074 nexus_info->pixels=nexus_info->cache;
5075 nexus_info->indexes=(IndexPacket *) NULL;
5076 if (cache_info->active_index_channel != MagickFalse)
5077 nexus_info->indexes=(IndexPacket *) (nexus_info->pixels+number_pixels);
5078 return(nexus_info->pixels);
5079}
5080
5081/*
5082%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5083% %
5084% %
5085% %
5086% 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 %
5087% %
5088% %
5089% %
5090%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5091%
5092% SetPixelCacheVirtualMethod() sets the "virtual pixels" method for the
5093% pixel cache and returns the previous setting. A virtual pixel is any pixel
5094% access that is outside the boundaries of the image cache.
5095%
5096% The format of the SetPixelCacheVirtualMethod() method is:
5097%
5098% VirtualPixelMethod SetPixelCacheVirtualMethod(const Image *image,
5099% const VirtualPixelMethod virtual_pixel_method)
5100%
5101% A description of each parameter follows:
5102%
5103% o image: the image.
5104%
5105% o virtual_pixel_method: choose the type of virtual pixel.
5106%
5107*/
5108MagickExport VirtualPixelMethod SetPixelCacheVirtualMethod(const Image *image,
5109 const VirtualPixelMethod virtual_pixel_method)
5110{
5111 CacheInfo
5112 *cache_info;
5113
5114 VirtualPixelMethod
5115 method;
5116
5117 assert(image != (Image *) NULL);
5118 assert(image->signature == MagickSignature);
5119 if (image->debug != MagickFalse)
5120 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5121 assert(image->cache != (Cache) NULL);
5122 cache_info=(CacheInfo *) image->cache;
5123 assert(cache_info->signature == MagickSignature);
5124 method=cache_info->virtual_pixel_method;
5125 cache_info->virtual_pixel_method=virtual_pixel_method;
5126 return(method);
5127}
5128
5129/*
5130%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5131% %
5132% %
5133% %
5134+ 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 %
5135% %
5136% %
5137% %
5138%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5139%
5140% SyncAuthenticPixelCacheNexus() saves the authentic image pixels to the
5141% in-memory or disk cache. The method returns MagickTrue if the pixel region
5142% is synced, otherwise MagickFalse.
5143%
5144% The format of the SyncAuthenticPixelCacheNexus() method is:
5145%
5146% MagickBooleanType SyncAuthenticPixelCacheNexus(Image *image,
5147% NexusInfo *nexus_info,ExceptionInfo *exception)
5148%
5149% A description of each parameter follows:
5150%
5151% o image: the image.
5152%
5153% o nexus_info: the cache nexus to sync.
5154%
5155% o exception: return any errors or warnings in this structure.
5156%
5157*/
5158MagickExport MagickBooleanType SyncAuthenticPixelCacheNexus(Image *image,
5159 NexusInfo *nexus_info,ExceptionInfo *exception)
5160{
5161 CacheInfo
5162 *cache_info;
5163
5164 MagickBooleanType
5165 status;
5166
5167 /*
5168 Transfer pixels to the cache.
5169 */
5170 assert(image != (Image *) NULL);
5171 assert(image->signature == MagickSignature);
5172 if (image->debug != MagickFalse)
5173 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5174 if (image->cache == (Cache) NULL)
5175 ThrowBinaryException(CacheError,"PixelCacheIsNotOpen",image->filename);
5176 cache_info=(CacheInfo *) image->cache;
5177 if (cache_info->type == UndefinedCache)
5178 return(MagickFalse);
5179 if ((image->clip_mask != (Image *) NULL) &&
5180 (ClipPixelCacheNexus(image,nexus_info,exception) == MagickFalse))
5181 return(MagickFalse);
5182 if ((image->mask != (Image *) NULL) &&
5183 (MaskPixelCacheNexus(image,nexus_info,exception) == MagickFalse))
5184 return(MagickFalse);
5185 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
5186 return(MagickTrue);
5187 assert(cache_info->signature == MagickSignature);
5188 status=WritePixelCachePixels(cache_info,nexus_info,exception);
5189 if ((cache_info->active_index_channel != MagickFalse) &&
5190 (WritePixelCacheIndexes(cache_info,nexus_info,exception) == MagickFalse))
5191 return(MagickFalse);
5192 return(status);
5193}
5194
5195/*
5196%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5197% %
5198% %
5199% %
5200+ S y n c A u t h e n t i c P i x e l C a c h e %
5201% %
5202% %
5203% %
5204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5205%
5206% SyncAuthenticPixelsCache() saves the authentic image pixels to the in-memory
5207% or disk cache. The method returns MagickTrue if the pixel region is synced,
5208% otherwise MagickFalse.
5209%
5210% The format of the SyncAuthenticPixelsCache() method is:
5211%
5212% MagickBooleanType SyncAuthenticPixelsCache(Image *image,
5213% ExceptionInfo *exception)
5214%
5215% A description of each parameter follows:
5216%
5217% o image: the image.
5218%
5219% o exception: return any errors or warnings in this structure.
5220%
5221*/
5222static MagickBooleanType SyncAuthenticPixelsCache(Image *image,
5223 ExceptionInfo *exception)
5224{
5225 CacheInfo
5226 *cache_info;
5227
5228 long
5229 id;
5230
5231 MagickBooleanType
5232 status;
5233
5234 cache_info=(CacheInfo *) image->cache;
5235 id=GetOpenMPThreadId();
5236 assert(id < (long) cache_info->number_threads);
5237 status=SyncAuthenticPixelCacheNexus(image,cache_info->nexus_info[id],
5238 exception);
5239 return(status);
5240}
5241
5242/*
5243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5244% %
5245% %
5246% %
5247% S y n c A u t h e n t i c P i x e l s %
5248% %
5249% %
5250% %
5251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5252%
5253% SyncAuthenticPixels() saves the image pixels to the in-memory or disk cache.
5254% The method returns MagickTrue if the pixel region is flushed, otherwise
5255% MagickFalse.
5256%
5257% The format of the SyncAuthenticPixels() method is:
5258%
5259% MagickBooleanType SyncAuthenticPixels(Image *image,
5260% ExceptionInfo *exception)
5261%
5262% A description of each parameter follows:
5263%
5264% o image: the image.
5265%
5266% o exception: return any errors or warnings in this structure.
5267%
5268*/
5269MagickExport MagickBooleanType SyncAuthenticPixels(Image *image,
5270 ExceptionInfo *exception)
5271{
5272 CacheInfo
5273 *cache_info;
5274
5275 assert(image != (Image *) NULL);
5276 assert(image->signature == MagickSignature);
5277 if (image->debug != MagickFalse)
5278 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5279 assert(image->cache != (Cache) NULL);
5280 cache_info=(CacheInfo *) image->cache;
5281 assert(cache_info->signature == MagickSignature);
5282 if (cache_info->methods.sync_authentic_pixels_handler ==
5283 (SyncAuthenticPixelsHandler) NULL)
5284 return(MagickFalse);
5285 return(cache_info->methods.sync_authentic_pixels_handler(image,exception));
5286}
5287
5288/*
5289%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5290% %
5291% %
5292% %
5293+ W r i t e P i x e l C a c h e I n d e x e s %
5294% %
5295% %
5296% %
5297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5298%
5299% WritePixelCacheIndexes() writes the colormap indexes to the specified
5300% region of the pixel cache.
5301%
5302% The format of the WritePixelCacheIndexes() method is:
5303%
5304% MagickBooleanType WritePixelCacheIndexes(CacheInfo *cache_info,
5305% NexusInfo *nexus_info,ExceptionInfo *exception)
5306%
5307% A description of each parameter follows:
5308%
5309% o cache_info: the pixel cache.
5310%
5311% o nexus_info: the cache nexus to write the colormap indexes.
5312%
5313% o exception: return any errors or warnings in this structure.
5314%
5315*/
5316static MagickBooleanType WritePixelCacheIndexes(CacheInfo *cache_info,
5317 NexusInfo *nexus_info,ExceptionInfo *exception)
5318{
5319 MagickOffsetType
5320 count,
5321 offset;
5322
5323 MagickSizeType
5324 length,
5325 number_pixels;
5326
5327 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005328 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005329
5330 register long
5331 y;
5332
5333 unsigned long
5334 rows;
5335
5336 if (cache_info->debug != MagickFalse)
5337 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
5338 cache_info->filename);
5339 if (cache_info->active_index_channel == MagickFalse)
5340 return(MagickFalse);
5341 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
5342 return(MagickTrue);
5343 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
5344 nexus_info->region.x;
5345 length=(MagickSizeType) nexus_info->region.width*sizeof(IndexPacket);
5346 rows=nexus_info->region.height;
5347 number_pixels=(MagickSizeType) length*rows;
cristy3ed852e2009-09-05 21:47:34 +00005348 p=nexus_info->indexes;
5349 switch (cache_info->type)
5350 {
5351 case MemoryCache:
5352 case MapCache:
5353 {
5354 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005355 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005356
5357 /*
5358 Write indexes to memory.
5359 */
cristydd341db2010-03-04 19:06:38 +00005360 if ((cache_info->columns == nexus_info->region.width) &&
5361 (number_pixels == (MagickSizeType) ((size_t) number_pixels)))
5362 {
5363 length=number_pixels;
5364 rows=1UL;
5365 }
cristy3ed852e2009-09-05 21:47:34 +00005366 q=cache_info->indexes+offset;
5367 for (y=0; y < (long) rows; y++)
5368 {
5369 (void) CopyMagickMemory(q,p,(size_t) length);
5370 p+=nexus_info->region.width;
5371 q+=cache_info->columns;
5372 }
5373 break;
5374 }
5375 case DiskCache:
5376 {
5377 /*
5378 Write indexes to disk.
5379 */
5380 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
5381 {
5382 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
5383 cache_info->cache_filename);
5384 return(MagickFalse);
5385 }
cristydd341db2010-03-04 19:06:38 +00005386 if ((cache_info->columns == nexus_info->region.width) &&
5387 (number_pixels < MagickMaxBufferExtent))
5388 {
5389 length=number_pixels;
5390 rows=1UL;
5391 }
cristy3ed852e2009-09-05 21:47:34 +00005392 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
5393 for (y=0; y < (long) rows; y++)
5394 {
5395 count=WritePixelCacheRegion(cache_info,cache_info->offset+number_pixels*
5396 sizeof(PixelPacket)+offset*sizeof(*p),length,
5397 (const unsigned char *) p);
5398 if ((MagickSizeType) count < length)
5399 break;
5400 p+=nexus_info->region.width;
5401 offset+=cache_info->columns;
5402 }
5403 if (y < (long) rows)
5404 {
5405 ThrowFileException(exception,CacheError,"UnableToWritePixelCache",
5406 cache_info->cache_filename);
5407 return(MagickFalse);
5408 }
5409 break;
5410 }
5411 default:
5412 break;
5413 }
5414 if ((cache_info->debug != MagickFalse) &&
5415 (QuantumTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
5416 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s[%lux%lu%+ld%+ld]",
5417 cache_info->filename,nexus_info->region.width,nexus_info->region.height,
5418 nexus_info->region.x,nexus_info->region.y);
5419 return(MagickTrue);
5420}
5421
5422/*
5423%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5424% %
5425% %
5426% %
5427+ W r i t e C a c h e P i x e l s %
5428% %
5429% %
5430% %
5431%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5432%
5433% WritePixelCachePixels() writes image pixels to the specified region of the
5434% pixel cache.
5435%
5436% The format of the WritePixelCachePixels() method is:
5437%
5438% MagickBooleanType WritePixelCachePixels(CacheInfo *cache_info,
5439% NexusInfo *nexus_info,ExceptionInfo *exception)
5440%
5441% A description of each parameter follows:
5442%
5443% o cache_info: the pixel cache.
5444%
5445% o nexus_info: the cache nexus to write the pixels.
5446%
5447% o exception: return any errors or warnings in this structure.
5448%
5449*/
5450static MagickBooleanType WritePixelCachePixels(CacheInfo *cache_info,
5451 NexusInfo *nexus_info,ExceptionInfo *exception)
5452{
5453 MagickOffsetType
5454 count,
5455 offset;
5456
5457 MagickSizeType
5458 length,
5459 number_pixels;
5460
cristy3ed852e2009-09-05 21:47:34 +00005461 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005462 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005463
cristy25ffffb2009-12-07 17:15:07 +00005464 register long
5465 y;
5466
cristy3ed852e2009-09-05 21:47:34 +00005467 unsigned long
5468 rows;
5469
5470 if (cache_info->debug != MagickFalse)
5471 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
5472 cache_info->filename);
5473 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
5474 return(MagickTrue);
5475 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
5476 nexus_info->region.x;
5477 length=(MagickSizeType) nexus_info->region.width*sizeof(PixelPacket);
5478 rows=nexus_info->region.height;
5479 number_pixels=length*rows;
cristy3ed852e2009-09-05 21:47:34 +00005480 p=nexus_info->pixels;
5481 switch (cache_info->type)
5482 {
5483 case MemoryCache:
5484 case MapCache:
5485 {
5486 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005487 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005488
5489 /*
5490 Write pixels to memory.
5491 */
cristydd341db2010-03-04 19:06:38 +00005492 if ((cache_info->columns == nexus_info->region.width) &&
5493 (number_pixels == (MagickSizeType) ((size_t) number_pixels)))
5494 {
5495 length=number_pixels;
5496 rows=1UL;
5497 }
cristy3ed852e2009-09-05 21:47:34 +00005498 q=cache_info->pixels+offset;
5499 for (y=0; y < (long) rows; y++)
5500 {
5501 (void) CopyMagickMemory(q,p,(size_t) length);
5502 p+=nexus_info->region.width;
5503 q+=cache_info->columns;
5504 }
5505 break;
5506 }
5507 case DiskCache:
5508 {
5509 /*
5510 Write pixels to disk.
5511 */
5512 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
5513 {
5514 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
5515 cache_info->cache_filename);
5516 return(MagickFalse);
5517 }
cristydd341db2010-03-04 19:06:38 +00005518 if ((cache_info->columns == nexus_info->region.width) &&
5519 (number_pixels < MagickMaxBufferExtent))
5520 {
5521 length=number_pixels;
5522 rows=1UL;
5523 }
cristy3ed852e2009-09-05 21:47:34 +00005524 for (y=0; y < (long) rows; y++)
5525 {
5526 count=WritePixelCacheRegion(cache_info,cache_info->offset+offset*
5527 sizeof(*p),length,(const unsigned char *) p);
5528 if ((MagickSizeType) count < length)
5529 break;
5530 p+=nexus_info->region.width;
5531 offset+=cache_info->columns;
5532 }
5533 if (y < (long) rows)
5534 {
5535 ThrowFileException(exception,CacheError,"UnableToWritePixelCache",
5536 cache_info->cache_filename);
5537 return(MagickFalse);
5538 }
5539 break;
5540 }
5541 default:
5542 break;
5543 }
5544 if ((cache_info->debug != MagickFalse) &&
5545 (QuantumTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
5546 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s[%lux%lu%+ld%+ld]",
5547 cache_info->filename,nexus_info->region.width,nexus_info->region.height,
5548 nexus_info->region.x,nexus_info->region.y);
5549 return(MagickTrue);
5550}