blob: 5e1e45f6d6cd677abc0946cbac6f3172f619b664 [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);
cristydf415c82010-03-11 16:47:50 +00003299 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
3300 nexus_info->region.x;
3301 length=(MagickSizeType) (nexus_info->region.height-1L)*cache_info->columns+
3302 nexus_info->region.width-1L;
cristy3ed852e2009-09-05 21:47:34 +00003303 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
3304 if ((offset >= 0) && (((MagickSizeType) offset+length) < number_pixels))
3305 if ((x >= 0) && ((long) (x+columns) <= (long) cache_info->columns) &&
3306 (y >= 0) && ((long) (y+rows) <= (long) cache_info->rows))
3307 {
3308 MagickBooleanType
3309 status;
3310
3311 /*
3312 Pixel request is inside cache extents.
3313 */
3314 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
3315 return(pixels);
3316 status=ReadPixelCachePixels(cache_info,nexus_info,exception);
3317 if (status == MagickFalse)
3318 return((const PixelPacket *) NULL);
3319 if ((cache_info->storage_class == PseudoClass) ||
3320 (cache_info->colorspace == CMYKColorspace))
3321 {
3322 status=ReadPixelCacheIndexes(cache_info,nexus_info,exception);
3323 if (status == MagickFalse)
3324 return((const PixelPacket *) NULL);
3325 }
3326 return(pixels);
3327 }
3328 /*
3329 Pixel request is outside cache extents.
3330 */
3331 q=pixels;
3332 indexes=GetPixelCacheNexusIndexes(cache_info,nexus_info);
3333 virtual_nexus=AcquirePixelCacheNexus(1);
3334 if (virtual_nexus == (NexusInfo **) NULL)
3335 {
3336 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
3337 "UnableToGetCacheNexus","`%s'",image->filename);
3338 return((const PixelPacket *) NULL);
3339 }
3340 switch (virtual_pixel_method)
3341 {
3342 case BlackVirtualPixelMethod:
3343 {
cristy4789f0d2010-01-10 00:01:06 +00003344 SetRedPixelComponent(&virtual_pixel,0);
3345 SetGreenPixelComponent(&virtual_pixel,0);
3346 SetBluePixelComponent(&virtual_pixel,0);
3347 SetOpacityPixelComponent(&virtual_pixel,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00003348 break;
3349 }
3350 case GrayVirtualPixelMethod:
3351 {
cristy4789f0d2010-01-10 00:01:06 +00003352 SetRedPixelComponent(&virtual_pixel,(Quantum) QuantumRange/2);
3353 SetGreenPixelComponent(&virtual_pixel,(Quantum) QuantumRange/2);
3354 SetBluePixelComponent(&virtual_pixel,(Quantum) QuantumRange/2);
3355 SetOpacityPixelComponent(&virtual_pixel,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00003356 break;
3357 }
3358 case TransparentVirtualPixelMethod:
3359 {
cristy4789f0d2010-01-10 00:01:06 +00003360 SetRedPixelComponent(&virtual_pixel,0);
3361 SetGreenPixelComponent(&virtual_pixel,0);
3362 SetBluePixelComponent(&virtual_pixel,0);
3363 SetOpacityPixelComponent(&virtual_pixel,TransparentOpacity);
cristy3ed852e2009-09-05 21:47:34 +00003364 break;
3365 }
3366 case MaskVirtualPixelMethod:
3367 case WhiteVirtualPixelMethod:
3368 {
cristy4789f0d2010-01-10 00:01:06 +00003369 SetRedPixelComponent(&virtual_pixel,(Quantum) QuantumRange);
3370 SetGreenPixelComponent(&virtual_pixel,(Quantum) QuantumRange);
3371 SetBluePixelComponent(&virtual_pixel,(Quantum) QuantumRange);
3372 SetOpacityPixelComponent(&virtual_pixel,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00003373 break;
3374 }
3375 default:
3376 {
3377 virtual_pixel=image->background_color;
3378 break;
3379 }
3380 }
3381 for (v=0; v < (long) rows; v++)
3382 {
3383 for (u=0; u < (long) columns; u+=length)
3384 {
3385 length=(MagickSizeType) MagickMin(cache_info->columns-(x+u),columns-u);
3386 if ((((x+u) < 0) || ((x+u) >= (long) cache_info->columns)) ||
3387 (((y+v) < 0) || ((y+v) >= (long) cache_info->rows)) || (length == 0))
3388 {
3389 MagickModulo
3390 x_modulo,
3391 y_modulo;
3392
3393 /*
3394 Transfer a single pixel.
3395 */
3396 length=(MagickSizeType) 1;
3397 switch (virtual_pixel_method)
3398 {
3399 case BackgroundVirtualPixelMethod:
3400 case ConstantVirtualPixelMethod:
3401 case BlackVirtualPixelMethod:
3402 case GrayVirtualPixelMethod:
3403 case TransparentVirtualPixelMethod:
3404 case MaskVirtualPixelMethod:
3405 case WhiteVirtualPixelMethod:
3406 {
3407 p=(&virtual_pixel);
3408 break;
3409 }
3410 case EdgeVirtualPixelMethod:
3411 default:
3412 {
3413 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003414 EdgeX(x+u,cache_info->columns),EdgeY(y+v,cache_info->rows),
cristy3ed852e2009-09-05 21:47:34 +00003415 1UL,1UL,virtual_nexus[0],exception);
3416 break;
3417 }
3418 case RandomVirtualPixelMethod:
3419 {
3420 if (cache_info->random_info == (RandomInfo *) NULL)
3421 cache_info->random_info=AcquireRandomInfo();
3422 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003423 RandomX(cache_info->random_info,cache_info->columns),
3424 RandomY(cache_info->random_info,cache_info->rows),1UL,1UL,
cristy3ed852e2009-09-05 21:47:34 +00003425 virtual_nexus[0],exception);
3426 break;
3427 }
3428 case DitherVirtualPixelMethod:
3429 {
3430 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003431 DitherX(x+u,cache_info->columns),DitherY(y+v,cache_info->rows),
cristy3ed852e2009-09-05 21:47:34 +00003432 1UL,1UL,virtual_nexus[0],exception);
3433 break;
3434 }
3435 case TileVirtualPixelMethod:
3436 {
3437 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3438 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3439 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3440 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3441 exception);
3442 break;
3443 }
3444 case MirrorVirtualPixelMethod:
3445 {
3446 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3447 if ((x_modulo.quotient & 0x01) == 1L)
3448 x_modulo.remainder=(long) cache_info->columns-
3449 x_modulo.remainder-1L;
3450 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3451 if ((y_modulo.quotient & 0x01) == 1L)
3452 y_modulo.remainder=(long) cache_info->rows-
3453 y_modulo.remainder-1L;
3454 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3455 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3456 exception);
3457 break;
3458 }
3459 case CheckerTileVirtualPixelMethod:
3460 {
3461 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3462 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3463 if (((x_modulo.quotient ^ y_modulo.quotient) & 0x01) != 0L)
3464 {
3465 p=(&virtual_pixel);
3466 break;
3467 }
3468 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3469 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3470 exception);
3471 break;
3472 }
3473 case HorizontalTileVirtualPixelMethod:
3474 {
3475 if (((y+v) < 0) || ((y+v) >= (long) cache_info->rows))
3476 {
3477 p=(&virtual_pixel);
3478 break;
3479 }
3480 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3481 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3482 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3483 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3484 exception);
3485 break;
3486 }
3487 case VerticalTileVirtualPixelMethod:
3488 {
3489 if (((x+u) < 0) || ((x+u) >= (long) cache_info->columns))
3490 {
3491 p=(&virtual_pixel);
3492 break;
3493 }
3494 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3495 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3496 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
3497 x_modulo.remainder,y_modulo.remainder,1UL,1UL,virtual_nexus[0],
3498 exception);
3499 break;
3500 }
3501 case HorizontalTileEdgeVirtualPixelMethod:
3502 {
3503 x_modulo=VirtualPixelModulo(x+u,cache_info->columns);
3504 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003505 x_modulo.remainder,EdgeY(y+v,cache_info->rows),1UL,1UL,
cristy3ed852e2009-09-05 21:47:34 +00003506 virtual_nexus[0],exception);
3507 break;
3508 }
3509 case VerticalTileEdgeVirtualPixelMethod:
3510 {
3511 y_modulo=VirtualPixelModulo(y+v,cache_info->rows);
3512 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,
cristy222528c2010-01-09 23:36:34 +00003513 EdgeX(x+u,cache_info->columns),y_modulo.remainder,1UL,1UL,
cristy3ed852e2009-09-05 21:47:34 +00003514 virtual_nexus[0],exception);
3515 break;
3516 }
3517 }
3518 if (p == (const PixelPacket *) NULL)
3519 break;
3520 *q++=(*p);
3521 if (indexes != (IndexPacket *) NULL)
3522 {
3523 nexus_indexes=GetVirtualIndexesFromNexus(cache_info,
3524 virtual_nexus[0]);
3525 if (nexus_indexes != (const IndexPacket *) NULL)
3526 *indexes++=(*nexus_indexes);
3527 }
3528 continue;
3529 }
3530 /*
3531 Transfer a run of pixels.
3532 */
3533 p=GetVirtualPixelsFromNexus(image,virtual_pixel_method,x+u,y+v,
3534 (unsigned long) length,1UL,virtual_nexus[0],exception);
3535 if (p == (const PixelPacket *) NULL)
3536 break;
3537 (void) CopyMagickMemory(q,p,(size_t) length*sizeof(*p));
3538 q+=length;
3539 if (indexes != (IndexPacket *) NULL)
3540 {
3541 nexus_indexes=GetVirtualIndexesFromNexus(cache_info,virtual_nexus[0]);
3542 if (nexus_indexes != (const IndexPacket *) NULL)
3543 {
3544 (void) CopyMagickMemory(indexes,nexus_indexes,(size_t) length*
3545 sizeof(*nexus_indexes));
3546 indexes+=length;
3547 }
3548 }
3549 }
3550 }
3551 virtual_nexus=DestroyPixelCacheNexus(virtual_nexus,1);
3552 return(pixels);
3553}
3554
3555/*
3556%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3557% %
3558% %
3559% %
3560+ G e t V i r t u a l P i x e l C a c h e %
3561% %
3562% %
3563% %
3564%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3565%
3566% GetVirtualPixelCache() get virtual pixels from the in-memory or disk pixel
3567% cache as defined by the geometry parameters. A pointer to the pixels
3568% is returned if the pixels are transferred, otherwise a NULL is returned.
3569%
3570% The format of the GetVirtualPixelCache() method is:
3571%
3572% const PixelPacket *GetVirtualPixelCache(const Image *image,
3573% const VirtualPixelMethod virtual_pixel_method,const long x,
3574% const long y,const unsigned long columns,const unsigned long rows,
3575% ExceptionInfo *exception)
3576%
3577% A description of each parameter follows:
3578%
3579% o image: the image.
3580%
3581% o virtual_pixel_method: the virtual pixel method.
3582%
3583% o x,y,columns,rows: These values define the perimeter of a region of
3584% pixels.
3585%
3586% o exception: return any errors or warnings in this structure.
3587%
3588*/
3589static const PixelPacket *GetVirtualPixelCache(const Image *image,
3590 const VirtualPixelMethod virtual_pixel_method,const long x,const long y,
3591 const unsigned long columns,const unsigned long rows,ExceptionInfo *exception)
3592{
3593 CacheInfo
3594 *cache_info;
3595
3596 const PixelPacket
3597 *pixels;
3598
3599 long
3600 id;
3601
3602 if (image->debug != MagickFalse)
3603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3604 cache_info=(CacheInfo *) image->cache;
3605 id=GetOpenMPThreadId();
3606 assert(id < (long) cache_info->number_threads);
3607 pixels=GetVirtualPixelsFromNexus(image,virtual_pixel_method,x,y,columns,rows,
3608 cache_info->nexus_info[id],exception);
3609 return(pixels);
3610}
3611
3612/*
3613%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3614% %
3615% %
3616% %
3617% G e t V i r t u a l P i x e l Q u e u e %
3618% %
3619% %
3620% %
3621%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3622%
3623% GetVirtualPixelQueue() returns the virtual pixels associated with the
3624% last call to QueueAuthenticPixels() or GetVirtualPixels().
3625%
3626% The format of the GetVirtualPixelQueue() method is:
3627%
3628% const PixelPacket *GetVirtualPixelQueue(const Image image)
3629%
3630% A description of each parameter follows:
3631%
3632% o image: the image.
3633%
3634*/
3635MagickExport const PixelPacket *GetVirtualPixelQueue(const Image *image)
3636{
3637 CacheInfo
3638 *cache_info;
3639
3640 assert(image != (const Image *) NULL);
3641 assert(image->signature == MagickSignature);
3642 if (image->debug != MagickFalse)
3643 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3644 assert(image->cache != (Cache) NULL);
3645 cache_info=(CacheInfo *) image->cache;
3646 assert(cache_info->signature == MagickSignature);
3647 if (cache_info->methods.get_virtual_pixels_handler ==
3648 (GetVirtualPixelsHandler) NULL)
3649 return((PixelPacket *) NULL);
3650 return(cache_info->methods.get_virtual_pixels_handler(image));
3651}
3652
3653/*
3654%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3655% %
3656% %
3657% %
3658% G e t V i r t u a l P i x e l s %
3659% %
3660% %
3661% %
3662%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3663%
3664% GetVirtualPixels() returns an immutable pixel region. If the
3665% region is successfully accessed, a pointer to it is returned, otherwise
cristyad4e5b22010-01-10 00:04:19 +00003666% NULL is returned. The returned pointer may point to a temporary working
cristy3ed852e2009-09-05 21:47:34 +00003667% copy of the pixels or it may point to the original pixels in memory.
3668% Performance is maximized if the selected region is part of one row, or one
3669% or more full rows, since there is opportunity to access the pixels in-place
cristyad4e5b22010-01-10 00:04:19 +00003670% (without a copy) if the image is in memory, or in a memory-mapped file. The
3671% returned pointer must *never* be deallocated by the user.
cristy3ed852e2009-09-05 21:47:34 +00003672%
3673% Pixels accessed via the returned pointer represent a simple array of type
3674% PixelPacket. If the image type is CMYK or the storage class is PseudoClass,
3675% call GetAuthenticIndexQueue() after invoking GetAuthenticPixels() to access
3676% the black color component or to obtain the colormap indexes (of type
3677% IndexPacket) corresponding to the region.
3678%
3679% If you plan to modify the pixels, use GetAuthenticPixels() instead.
3680%
3681% Note, the GetVirtualPixels() and GetAuthenticPixels() methods are not thread-
3682% safe. In a threaded environment, use GetCacheViewVirtualPixels() or
3683% GetCacheViewAuthenticPixels() instead.
3684%
3685% The format of the GetVirtualPixels() method is:
3686%
3687% const PixelPacket *GetVirtualPixels(const Image *image,const long x,
3688% const long y,const unsigned long columns,const unsigned long rows,
3689% ExceptionInfo *exception)
3690%
3691% A description of each parameter follows:
3692%
3693% o image: the image.
3694%
3695% o x,y,columns,rows: These values define the perimeter of a region of
3696% pixels.
3697%
3698% o exception: return any errors or warnings in this structure.
3699%
3700*/
3701MagickExport const PixelPacket *GetVirtualPixels(const Image *image,
3702 const long x,const long y,const unsigned long columns,
3703 const unsigned long rows,ExceptionInfo *exception)
3704{
3705 CacheInfo
3706 *cache_info;
3707
3708 const PixelPacket
3709 *pixels;
3710
3711 assert(image != (const Image *) NULL);
3712 assert(image->signature == MagickSignature);
3713 if (image->debug != MagickFalse)
3714 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3715 assert(image->cache != (Cache) NULL);
3716 cache_info=(CacheInfo *) image->cache;
3717 assert(cache_info->signature == MagickSignature);
3718 if (cache_info->methods.get_virtual_pixel_handler ==
3719 (GetVirtualPixelHandler) NULL)
3720 return((const PixelPacket *) NULL);
3721 pixels=cache_info->methods.get_virtual_pixel_handler(image,
3722 GetPixelCacheVirtualMethod(image),x,y,columns,rows,exception);
3723 return(pixels);
3724}
3725
3726/*
3727%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3728% %
3729% %
3730% %
3731+ 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 %
3732% %
3733% %
3734% %
3735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3736%
3737% GetVirtualPixelsCache() returns the pixels associated with the last call
3738% to QueueAuthenticPixelsCache() or GetVirtualPixelCache().
3739%
3740% The format of the GetVirtualPixelsCache() method is:
3741%
3742% PixelPacket *GetVirtualPixelsCache(const Image *image)
3743%
3744% A description of each parameter follows:
3745%
3746% o image: the image.
3747%
3748*/
3749static const PixelPacket *GetVirtualPixelsCache(const Image *image)
3750{
3751 CacheInfo
3752 *cache_info;
3753
3754 const PixelPacket
3755 *pixels;
3756
3757 long
3758 id;
3759
3760 if (image->debug != MagickFalse)
3761 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3762 cache_info=(CacheInfo *) image->cache;
3763 id=GetOpenMPThreadId();
3764 assert(id < (long) cache_info->number_threads);
3765 pixels=GetVirtualPixelsNexus(image->cache,cache_info->nexus_info[id]);
3766 return(pixels);
3767}
3768
3769/*
3770%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3771% %
3772% %
3773% %
3774+ G e t V i r t u a l P i x e l s N e x u s %
3775% %
3776% %
3777% %
3778%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3779%
3780% GetVirtualPixelsNexus() returns the pixels associated with the specified
3781% cache nexus.
3782%
3783% The format of the GetVirtualPixelsNexus() method is:
3784%
3785% const IndexPacket *GetVirtualPixelsNexus(const Cache cache,
3786% NexusInfo *nexus_info)
3787%
3788% A description of each parameter follows:
3789%
3790% o cache: the pixel cache.
3791%
3792% o nexus_info: the cache nexus to return the colormap pixels.
3793%
3794*/
3795MagickExport const PixelPacket *GetVirtualPixelsNexus(const Cache cache,
3796 NexusInfo *nexus_info)
3797{
3798 CacheInfo
3799 *cache_info;
3800
3801 if (cache == (Cache) NULL)
3802 return((PixelPacket *) NULL);
3803 cache_info=(CacheInfo *) cache;
3804 assert(cache_info->signature == MagickSignature);
3805 if (cache_info->storage_class == UndefinedClass)
3806 return((PixelPacket *) NULL);
3807 return((const PixelPacket *) nexus_info->pixels);
3808}
3809
3810/*
3811%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3812% %
3813% %
3814% %
3815+ M a s k P i x e l C a c h e N e x u s %
3816% %
3817% %
3818% %
3819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3820%
3821% MaskPixelCacheNexus() masks the cache nexus as defined by the image mask.
3822% The method returns MagickTrue if the pixel region is masked, otherwise
3823% MagickFalse.
3824%
3825% The format of the MaskPixelCacheNexus() method is:
3826%
3827% MagickBooleanType MaskPixelCacheNexus(Image *image,
3828% NexusInfo *nexus_info,ExceptionInfo *exception)
3829%
3830% A description of each parameter follows:
3831%
3832% o image: the image.
3833%
3834% o nexus_info: the cache nexus to clip.
3835%
3836% o exception: return any errors or warnings in this structure.
3837%
3838*/
3839
3840static inline void MagickPixelCompositeMask(const MagickPixelPacket *p,
3841 const MagickRealType alpha,const MagickPixelPacket *q,
3842 const MagickRealType beta,MagickPixelPacket *composite)
3843{
3844 MagickRealType
3845 gamma;
3846
3847 if (alpha == TransparentOpacity)
3848 {
3849 *composite=(*q);
3850 return;
3851 }
3852 gamma=1.0-QuantumScale*QuantumScale*alpha*beta;
3853 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
3854 composite->red=gamma*MagickOver_(p->red,alpha,q->red,beta);
3855 composite->green=gamma*MagickOver_(p->green,alpha,q->green,beta);
3856 composite->blue=gamma*MagickOver_(p->blue,alpha,q->blue,beta);
3857 if ((p->colorspace == CMYKColorspace) && (q->colorspace == CMYKColorspace))
3858 composite->index=gamma*MagickOver_(p->index,alpha,q->index,beta);
3859}
3860
3861static MagickBooleanType MaskPixelCacheNexus(Image *image,NexusInfo *nexus_info,
3862 ExceptionInfo *exception)
3863{
3864 CacheInfo
3865 *cache_info;
3866
3867 MagickPixelPacket
3868 alpha,
3869 beta;
3870
3871 MagickSizeType
3872 number_pixels;
3873
3874 NexusInfo
3875 **clip_nexus,
3876 **image_nexus;
3877
3878 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003879 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00003880
3881 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00003882 *restrict nexus_indexes,
3883 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00003884
3885 register long
3886 i;
3887
3888 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003889 *restrict p,
3890 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003891
3892 /*
3893 Apply clip mask.
3894 */
3895 if (image->debug != MagickFalse)
3896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3897 if (image->mask == (Image *) NULL)
3898 return(MagickFalse);
3899 cache_info=(CacheInfo *) GetImagePixelCache(image,MagickTrue,exception);
3900 if (cache_info == (Cache) NULL)
3901 return(MagickFalse);
3902 image_nexus=AcquirePixelCacheNexus(1);
3903 clip_nexus=AcquirePixelCacheNexus(1);
3904 if ((image_nexus == (NexusInfo **) NULL) ||
3905 (clip_nexus == (NexusInfo **) NULL))
3906 ThrowBinaryException(CacheError,"UnableToGetCacheNexus",image->filename);
cristy09449f72010-01-23 03:08:36 +00003907 p=GetAuthenticPixelCacheNexus(image,nexus_info->region.x,
3908 nexus_info->region.y,nexus_info->region.width,nexus_info->region.height,
3909 image_nexus[0],exception);
cristy3ed852e2009-09-05 21:47:34 +00003910 indexes=GetPixelCacheNexusIndexes(image->cache,image_nexus[0]);
3911 q=nexus_info->pixels;
3912 nexus_indexes=nexus_info->indexes;
3913 r=GetVirtualPixelsFromNexus(image->mask,MaskVirtualPixelMethod,
3914 nexus_info->region.x,nexus_info->region.y,nexus_info->region.width,
3915 nexus_info->region.height,clip_nexus[0],&image->exception);
3916 GetMagickPixelPacket(image,&alpha);
3917 GetMagickPixelPacket(image,&beta);
3918 number_pixels=(MagickSizeType) nexus_info->region.width*
3919 nexus_info->region.height;
3920 for (i=0; i < (long) number_pixels; i++)
3921 {
3922 if ((p == (PixelPacket *) NULL) || (r == (const PixelPacket *) NULL))
3923 break;
3924 SetMagickPixelPacket(image,p,indexes+i,&alpha);
3925 SetMagickPixelPacket(image,q,nexus_indexes+i,&beta);
3926 MagickPixelCompositeMask(&beta,(MagickRealType) PixelIntensityToQuantum(r),
3927 &alpha,alpha.opacity,&beta);
cristyce70c172010-01-07 17:15:30 +00003928 q->red=ClampToQuantum(beta.red);
3929 q->green=ClampToQuantum(beta.green);
3930 q->blue=ClampToQuantum(beta.blue);
3931 q->opacity=ClampToQuantum(beta.opacity);
cristy3ed852e2009-09-05 21:47:34 +00003932 if (cache_info->active_index_channel != MagickFalse)
3933 nexus_indexes[i]=indexes[i];
3934 p++;
3935 q++;
3936 r++;
3937 }
3938 clip_nexus=DestroyPixelCacheNexus(clip_nexus,1);
3939 image_nexus=DestroyPixelCacheNexus(image_nexus,1);
3940 if (i < (long) number_pixels)
3941 return(MagickFalse);
3942 return(MagickTrue);
3943}
3944
3945/*
3946%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3947% %
3948% %
3949% %
3950+ O p e n P i x e l C a c h e %
3951% %
3952% %
3953% %
3954%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3955%
3956% OpenPixelCache() allocates the pixel cache. This includes defining the cache
3957% dimensions, allocating space for the image pixels and optionally the
3958% colormap indexes, and memory mapping the cache if it is disk based. The
3959% cache nexus array is initialized as well.
3960%
3961% The format of the OpenPixelCache() method is:
3962%
3963% MagickBooleanType OpenPixelCache(Image *image,const MapMode mode,
3964% ExceptionInfo *exception)
3965%
3966% A description of each parameter follows:
3967%
3968% o image: the image.
3969%
3970% o mode: ReadMode, WriteMode, or IOMode.
3971%
3972% o exception: return any errors or warnings in this structure.
3973%
3974*/
3975
cristyd43a46b2010-01-21 02:13:41 +00003976static inline void AllocatePixelCachePixels(CacheInfo *cache_info)
cristy3ed852e2009-09-05 21:47:34 +00003977{
3978 cache_info->mapped=MagickFalse;
3979 cache_info->pixels=(PixelPacket *) AcquireMagickMemory((size_t)
3980 cache_info->length);
3981 if (cache_info->pixels == (PixelPacket *) NULL)
3982 {
3983 cache_info->mapped=MagickTrue;
3984 cache_info->pixels=(PixelPacket *) MapBlob(-1,IOMode,0,(size_t)
3985 cache_info->length);
3986 }
3987}
3988
3989static MagickBooleanType ExtendCache(Image *image,MagickSizeType length)
3990{
3991 CacheInfo
3992 *cache_info;
3993
3994 MagickOffsetType
3995 count,
3996 extent,
3997 offset;
3998
3999 cache_info=(CacheInfo *) image->cache;
4000 if (image->debug != MagickFalse)
4001 {
4002 char
4003 format[MaxTextExtent],
4004 message[MaxTextExtent];
4005
cristyb9080c92009-12-01 20:13:26 +00004006 (void) FormatMagickSize(length,MagickFalse,format);
cristy3ed852e2009-09-05 21:47:34 +00004007 (void) FormatMagickString(message,MaxTextExtent,
4008 "extend %s (%s[%d], disk, %s)",cache_info->filename,
4009 cache_info->cache_filename,cache_info->file,format);
4010 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",message);
4011 }
4012 if (length != (MagickSizeType) ((MagickOffsetType) length))
4013 return(MagickFalse);
4014 extent=(MagickOffsetType) MagickSeek(cache_info->file,0,SEEK_END);
4015 if (extent < 0)
4016 return(MagickFalse);
4017 if ((MagickSizeType) extent >= length)
4018 return(MagickTrue);
4019 offset=(MagickOffsetType) length-1;
4020 count=WritePixelCacheRegion(cache_info,offset,1,(const unsigned char *) "");
4021 return(count == (MagickOffsetType) 1 ? MagickTrue : MagickFalse);
4022}
4023
4024static MagickBooleanType OpenPixelCache(Image *image,const MapMode mode,
4025 ExceptionInfo *exception)
4026{
4027 char
4028 format[MaxTextExtent],
4029 message[MaxTextExtent];
4030
4031 CacheInfo
4032 *cache_info,
4033 source_info;
4034
4035 MagickSizeType
4036 length,
4037 number_pixels;
4038
4039 MagickStatusType
4040 status;
4041
4042 size_t
4043 packet_size;
4044
4045 unsigned long
4046 columns;
4047
4048 if (image->debug != MagickFalse)
4049 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4050 if ((image->columns == 0) || (image->rows == 0))
4051 ThrowBinaryException(CacheError,"NoPixelsDefinedInCache",image->filename);
4052 cache_info=(CacheInfo *) image->cache;
4053 source_info=(*cache_info);
4054 source_info.file=(-1);
4055 (void) FormatMagickString(cache_info->filename,MaxTextExtent,"%s[%ld]",
4056 image->filename,GetImageIndexInList(image));
cristy87528ea2009-09-10 14:53:56 +00004057 cache_info->mode=mode;
cristy3ed852e2009-09-05 21:47:34 +00004058 cache_info->rows=image->rows;
4059 cache_info->columns=image->columns;
4060 cache_info->active_index_channel=((image->storage_class == PseudoClass) ||
4061 (image->colorspace == CMYKColorspace)) ? MagickTrue : MagickFalse;
4062 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
4063 packet_size=sizeof(PixelPacket);
4064 if (cache_info->active_index_channel != MagickFalse)
4065 packet_size+=sizeof(IndexPacket);
4066 length=number_pixels*packet_size;
4067 columns=(unsigned long) (length/cache_info->rows/packet_size);
4068 if (cache_info->columns != columns)
4069 ThrowBinaryException(ResourceLimitError,"PixelCacheAllocationFailed",
4070 image->filename);
4071 cache_info->length=length;
4072 status=AcquireMagickResource(AreaResource,cache_info->length);
4073 length=number_pixels*(sizeof(PixelPacket)+sizeof(IndexPacket));
4074 if ((status != MagickFalse) && (length == (MagickSizeType) ((size_t) length)))
4075 {
4076 status=AcquireMagickResource(MemoryResource,cache_info->length);
4077 if (((cache_info->type == UndefinedCache) && (status != MagickFalse)) ||
4078 (cache_info->type == MemoryCache))
4079 {
cristyd43a46b2010-01-21 02:13:41 +00004080 AllocatePixelCachePixels(cache_info);
cristy3ed852e2009-09-05 21:47:34 +00004081 if (cache_info->pixels == (PixelPacket *) NULL)
4082 cache_info->pixels=source_info.pixels;
4083 else
4084 {
4085 /*
4086 Create memory pixel cache.
4087 */
4088 if (image->debug != MagickFalse)
4089 {
cristy97e7a572009-12-05 15:07:53 +00004090 (void) FormatMagickSize(cache_info->length,MagickTrue,
cristyb9080c92009-12-01 20:13:26 +00004091 format);
cristy3ed852e2009-09-05 21:47:34 +00004092 (void) FormatMagickString(message,MaxTextExtent,
4093 "open %s (%s memory, %lux%lu %s)",cache_info->filename,
4094 cache_info->mapped != MagickFalse ? "anonymous" : "heap",
4095 cache_info->columns,cache_info->rows,format);
4096 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",
4097 message);
4098 }
4099 cache_info->storage_class=image->storage_class;
4100 cache_info->colorspace=image->colorspace;
4101 cache_info->type=MemoryCache;
4102 cache_info->indexes=(IndexPacket *) NULL;
4103 if (cache_info->active_index_channel != MagickFalse)
4104 cache_info->indexes=(IndexPacket *) (cache_info->pixels+
4105 number_pixels);
4106 if (source_info.storage_class != UndefinedClass)
4107 {
4108 status|=ClonePixelCachePixels(cache_info,&source_info,
4109 exception);
4110 RelinquishPixelCachePixels(&source_info);
4111 }
4112 return(MagickTrue);
4113 }
4114 }
4115 RelinquishMagickResource(MemoryResource,cache_info->length);
4116 }
4117 /*
4118 Create pixel cache on disk.
4119 */
4120 status=AcquireMagickResource(DiskResource,cache_info->length);
4121 if (status == MagickFalse)
4122 {
4123 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
4124 "CacheResourcesExhausted","`%s'",image->filename);
4125 return(MagickFalse);
4126 }
4127 if (OpenPixelCacheOnDisk(cache_info,mode) == MagickFalse)
4128 {
4129 RelinquishMagickResource(DiskResource,cache_info->length);
4130 ThrowFileException(exception,CacheError,"UnableToOpenPixelCache",
4131 image->filename);
4132 return(MagickFalse);
4133 }
4134 status=ExtendCache(image,(MagickSizeType) cache_info->offset+
4135 cache_info->length);
4136 if (status == MagickFalse)
4137 {
4138 ThrowFileException(exception,CacheError,"UnableToExtendCache",
4139 image->filename);
4140 return(MagickFalse);
4141 }
4142 cache_info->storage_class=image->storage_class;
4143 cache_info->colorspace=image->colorspace;
4144 length=number_pixels*(sizeof(PixelPacket)+sizeof(IndexPacket));
4145 status=AcquireMagickResource(AreaResource,cache_info->length);
4146 if ((status == MagickFalse) || (length != (MagickSizeType) ((size_t) length)))
4147 cache_info->type=DiskCache;
4148 else
4149 {
4150 status=AcquireMagickResource(MapResource,cache_info->length);
4151 if ((status == MagickFalse) && (cache_info->type != MapCache) &&
4152 (cache_info->type != MemoryCache))
4153 cache_info->type=DiskCache;
4154 else
4155 {
4156 cache_info->pixels=(PixelPacket *) MapBlob(cache_info->file,mode,
4157 cache_info->offset,(size_t) cache_info->length);
4158 if (cache_info->pixels == (PixelPacket *) NULL)
4159 {
4160 cache_info->pixels=source_info.pixels;
4161 cache_info->type=DiskCache;
4162 }
4163 else
4164 {
4165 /*
4166 Create file-backed memory-mapped pixel cache.
4167 */
4168 (void) ClosePixelCacheOnDisk(cache_info);
4169 cache_info->type=MapCache;
4170 cache_info->mapped=MagickTrue;
4171 cache_info->indexes=(IndexPacket *) NULL;
4172 if (cache_info->active_index_channel != MagickFalse)
4173 cache_info->indexes=(IndexPacket *) (cache_info->pixels+
4174 number_pixels);
4175 if ((source_info.type != UndefinedCache) && (mode != ReadMode))
4176 {
4177 status=ClonePixelCachePixels(cache_info,&source_info,
4178 exception);
4179 RelinquishPixelCachePixels(&source_info);
4180 }
4181 if (image->debug != MagickFalse)
4182 {
cristy97e7a572009-12-05 15:07:53 +00004183 (void) FormatMagickSize(cache_info->length,MagickTrue,
cristyb9080c92009-12-01 20:13:26 +00004184 format);
cristy3ed852e2009-09-05 21:47:34 +00004185 (void) FormatMagickString(message,MaxTextExtent,
4186 "open %s (%s[%d], memory-mapped, %lux%lu %s)",
4187 cache_info->filename,cache_info->cache_filename,
4188 cache_info->file,cache_info->columns,cache_info->rows,
4189 format);
4190 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",
4191 message);
4192 }
4193 return(MagickTrue);
4194 }
4195 }
4196 RelinquishMagickResource(MapResource,cache_info->length);
4197 }
4198 if ((source_info.type != UndefinedCache) && (mode != ReadMode))
4199 {
4200 status=ClonePixelCachePixels(cache_info,&source_info,exception);
4201 RelinquishPixelCachePixels(&source_info);
4202 }
4203 if (image->debug != MagickFalse)
4204 {
cristyb9080c92009-12-01 20:13:26 +00004205 (void) FormatMagickSize(cache_info->length,MagickFalse,format);
cristy3ed852e2009-09-05 21:47:34 +00004206 (void) FormatMagickString(message,MaxTextExtent,
4207 "open %s (%s[%d], disk, %lux%lu %s)",cache_info->filename,
4208 cache_info->cache_filename,cache_info->file,cache_info->columns,
4209 cache_info->rows,format);
4210 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",message);
4211 }
4212 return(MagickTrue);
4213}
4214
4215/*
4216%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4217% %
4218% %
4219% %
4220+ P e r s i s t P i x e l C a c h e %
4221% %
4222% %
4223% %
4224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4225%
4226% PersistPixelCache() attaches to or initializes a persistent pixel cache. A
4227% persistent pixel cache is one that resides on disk and is not destroyed
4228% when the program exits.
4229%
4230% The format of the PersistPixelCache() method is:
4231%
4232% MagickBooleanType PersistPixelCache(Image *image,const char *filename,
4233% const MagickBooleanType attach,MagickOffsetType *offset,
4234% ExceptionInfo *exception)
4235%
4236% A description of each parameter follows:
4237%
4238% o image: the image.
4239%
4240% o filename: the persistent pixel cache filename.
4241%
cristy01b7eb02009-09-10 23:10:14 +00004242% o attach: A value other than zero initializes the persistent pixel
4243% cache.
4244%
cristy3ed852e2009-09-05 21:47:34 +00004245% o initialize: A value other than zero initializes the persistent pixel
4246% cache.
4247%
4248% o offset: the offset in the persistent cache to store pixels.
4249%
4250% o exception: return any errors or warnings in this structure.
4251%
4252*/
4253MagickExport MagickBooleanType PersistPixelCache(Image *image,
4254 const char *filename,const MagickBooleanType attach,MagickOffsetType *offset,
4255 ExceptionInfo *exception)
4256{
4257 CacheInfo
4258 *cache_info,
4259 *clone_info;
4260
4261 Image
4262 clone_image;
4263
4264 long
cristy688f07b2009-09-27 15:19:13 +00004265 page_size;
cristy3ed852e2009-09-05 21:47:34 +00004266
4267 MagickBooleanType
4268 status;
4269
4270 assert(image != (Image *) NULL);
4271 assert(image->signature == MagickSignature);
4272 if (image->debug != MagickFalse)
4273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4274 assert(image->cache != (void *) NULL);
4275 assert(filename != (const char *) NULL);
4276 assert(offset != (MagickOffsetType *) NULL);
cristy688f07b2009-09-27 15:19:13 +00004277 page_size=GetMagickPageSize();
cristy3ed852e2009-09-05 21:47:34 +00004278 cache_info=(CacheInfo *) image->cache;
4279 assert(cache_info->signature == MagickSignature);
4280 if (attach != MagickFalse)
4281 {
4282 /*
cristy01b7eb02009-09-10 23:10:14 +00004283 Attach existing persistent pixel cache.
cristy3ed852e2009-09-05 21:47:34 +00004284 */
4285 if (image->debug != MagickFalse)
4286 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
4287 "attach persistent cache");
4288 (void) CopyMagickString(cache_info->cache_filename,filename,
4289 MaxTextExtent);
4290 cache_info->type=DiskCache;
4291 cache_info->offset=(*offset);
4292 if (OpenPixelCache(image,ReadMode,exception) == MagickFalse)
4293 return(MagickFalse);
cristy688f07b2009-09-27 15:19:13 +00004294 *offset+=cache_info->length+page_size-(cache_info->length % page_size);
cristy3ed852e2009-09-05 21:47:34 +00004295 return(MagickTrue);
4296 }
cristy01b7eb02009-09-10 23:10:14 +00004297 if ((cache_info->mode != ReadMode) && (cache_info->type != MemoryCache) &&
4298 (cache_info->reference_count == 1))
cristy3ed852e2009-09-05 21:47:34 +00004299 {
cristyf84a1932010-01-03 18:00:18 +00004300 LockSemaphoreInfo(cache_info->semaphore);
cristy09449f72010-01-23 03:08:36 +00004301 if ((cache_info->mode != ReadMode) &&
4302 (cache_info->type != MemoryCache) &&
cristy3ed852e2009-09-05 21:47:34 +00004303 (cache_info->reference_count == 1))
4304 {
4305 int
4306 status;
4307
4308 /*
cristy01b7eb02009-09-10 23:10:14 +00004309 Usurp existing persistent pixel cache.
cristy3ed852e2009-09-05 21:47:34 +00004310 */
4311 status=rename(cache_info->cache_filename,filename);
4312 if (status == 0)
4313 {
4314 (void) CopyMagickString(cache_info->cache_filename,filename,
4315 MaxTextExtent);
cristy688f07b2009-09-27 15:19:13 +00004316 *offset+=cache_info->length+page_size-(cache_info->length %
4317 page_size);
cristyf84a1932010-01-03 18:00:18 +00004318 UnlockSemaphoreInfo(cache_info->semaphore);
cristy87528ea2009-09-10 14:53:56 +00004319 cache_info=(CacheInfo *) ReferencePixelCache(cache_info);
cristy3ed852e2009-09-05 21:47:34 +00004320 if (image->debug != MagickFalse)
4321 (void) LogMagickEvent(CacheEvent,GetMagickModule(),
4322 "Usurp resident persistent cache");
cristy3ed852e2009-09-05 21:47:34 +00004323 return(MagickTrue);
4324 }
4325 }
cristyf84a1932010-01-03 18:00:18 +00004326 UnlockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00004327 }
4328 /*
cristy01b7eb02009-09-10 23:10:14 +00004329 Clone persistent pixel cache.
cristy3ed852e2009-09-05 21:47:34 +00004330 */
4331 clone_image=(*image);
4332 clone_info=(CacheInfo *) clone_image.cache;
4333 image->cache=ClonePixelCache(cache_info);
4334 cache_info=(CacheInfo *) ReferencePixelCache(image->cache);
4335 (void) CopyMagickString(cache_info->cache_filename,filename,MaxTextExtent);
4336 cache_info->type=DiskCache;
4337 cache_info->offset=(*offset);
4338 cache_info=(CacheInfo *) image->cache;
4339 status=ClonePixelCacheNexus(cache_info,clone_info,exception);
4340 if (status != MagickFalse)
4341 {
4342 status=OpenPixelCache(image,IOMode,exception);
4343 if (status != MagickFalse)
4344 status=ClonePixelCachePixels(cache_info,clone_info,&image->exception);
4345 }
cristy688f07b2009-09-27 15:19:13 +00004346 *offset+=cache_info->length+page_size-(cache_info->length % page_size);
cristy3ed852e2009-09-05 21:47:34 +00004347 clone_info=(CacheInfo *) DestroyPixelCache(clone_info);
4348 return(status);
4349}
4350
4351/*
4352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4353% %
4354% %
4355% %
4356+ Q u e u e A u t h e n t i c N e x u s %
4357% %
4358% %
4359% %
4360%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4361%
4362% QueueAuthenticNexus() allocates an region to store image pixels as defined
4363% by the region rectangle and returns a pointer to the region. This region is
4364% subsequently transferred from the pixel cache with
4365% SyncAuthenticPixelsCache(). A pointer to the pixels is returned if the
4366% pixels are transferred, otherwise a NULL is returned.
4367%
4368% The format of the QueueAuthenticNexus() method is:
4369%
4370% PixelPacket *QueueAuthenticNexus(Image *image,const long x,const long y,
4371% const unsigned long columns,const unsigned long rows,
4372% NexusInfo *nexus_info,ExceptionInfo *exception)
4373%
4374% A description of each parameter follows:
4375%
4376% o image: the image.
4377%
4378% o x,y,columns,rows: These values define the perimeter of a region of
4379% pixels.
4380%
4381% o nexus_info: the cache nexus to set.
4382%
4383% o exception: return any errors or warnings in this structure.
4384%
4385*/
4386MagickExport PixelPacket *QueueAuthenticNexus(Image *image,const long x,
4387 const long y,const unsigned long columns,const unsigned long rows,
4388 NexusInfo *nexus_info,ExceptionInfo *exception)
4389{
4390 CacheInfo
4391 *cache_info;
4392
4393 MagickOffsetType
4394 offset;
4395
4396 MagickSizeType
4397 number_pixels;
4398
4399 RectangleInfo
4400 region;
4401
4402 /*
4403 Validate pixel cache geometry.
4404 */
4405 cache_info=(CacheInfo *) image->cache;
4406 if ((cache_info->columns == 0) && (cache_info->rows == 0))
4407 {
4408 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
4409 "NoPixelsDefinedInCache","`%s'",image->filename);
4410 return((PixelPacket *) NULL);
4411 }
4412 if ((x < 0) || (y < 0) || (x >= (long) cache_info->columns) ||
4413 (y >= (long) cache_info->rows))
4414 {
4415 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
4416 "PixelsAreNotAuthentic","`%s'",image->filename);
4417 return((PixelPacket *) NULL);
4418 }
4419 offset=(MagickOffsetType) y*cache_info->columns+x;
4420 if (offset < 0)
4421 return((PixelPacket *) NULL);
4422 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
4423 offset+=(MagickOffsetType) (rows-1)*cache_info->columns+columns-1;
4424 if ((MagickSizeType) offset >= number_pixels)
4425 return((PixelPacket *) NULL);
4426 /*
4427 Return pixel cache.
4428 */
4429 region.x=x;
4430 region.y=y;
4431 region.width=columns;
4432 region.height=rows;
4433 return(SetPixelCacheNexusPixels(image,&region,nexus_info,exception));
4434}
4435
4436/*
4437%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4438% %
4439% %
4440% %
4441+ 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 %
4442% %
4443% %
4444% %
4445%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4446%
4447% QueueAuthenticPixelsCache() allocates an region to store image pixels as
4448% defined by the region rectangle and returns a pointer to the region. This
4449% region is subsequently transferred from the pixel cache with
4450% SyncAuthenticPixelsCache(). A pointer to the pixels is returned if the
4451% pixels are transferred, otherwise a NULL is returned.
4452%
4453% The format of the QueueAuthenticPixelsCache() method is:
4454%
4455% PixelPacket *QueueAuthenticPixelsCache(Image *image,const long x,
4456% const long y,const unsigned long columns,const unsigned long rows,
4457% ExceptionInfo *exception)
4458%
4459% A description of each parameter follows:
4460%
4461% o image: the image.
4462%
4463% o x,y,columns,rows: These values define the perimeter of a region of
4464% pixels.
4465%
4466% o exception: return any errors or warnings in this structure.
4467%
4468*/
4469static PixelPacket *QueueAuthenticPixelsCache(Image *image,const long x,
4470 const long y,const unsigned long columns,const unsigned long rows,
4471 ExceptionInfo *exception)
4472{
4473 CacheInfo
4474 *cache_info;
4475
4476 long
4477 id;
4478
4479 PixelPacket
4480 *pixels;
4481
4482 cache_info=(CacheInfo *) GetImagePixelCache(image,MagickFalse,exception);
4483 if (cache_info == (Cache) NULL)
4484 return((PixelPacket *) NULL);
4485 id=GetOpenMPThreadId();
4486 assert(id < (long) cache_info->number_threads);
4487 pixels=QueueAuthenticNexus(image,x,y,columns,rows,cache_info->nexus_info[id],
4488 exception);
4489 return(pixels);
4490}
4491
4492/*
4493%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4494% %
4495% %
4496% %
4497% Q u e u e A u t h e n t i c P i x e l s %
4498% %
4499% %
4500% %
4501%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4502%
4503% QueueAuthenticPixels() queues a mutable pixel region. If the region is
4504% successfully intialized a pointer to a PixelPacket array representing the
4505% region is returned, otherwise NULL is returned. The returned pointer may
4506% point to a temporary working buffer for the pixels or it may point to the
4507% final location of the pixels in memory.
4508%
4509% Write-only access means that any existing pixel values corresponding to
4510% the region are ignored. This is useful if the initial image is being
4511% created from scratch, or if the existing pixel values are to be
4512% completely replaced without need to refer to their pre-existing values.
4513% The application is free to read and write the pixel buffer returned by
4514% QueueAuthenticPixels() any way it pleases. QueueAuthenticPixels() does not
4515% initialize the pixel array values. Initializing pixel array values is the
4516% application's responsibility.
4517%
4518% Performance is maximized if the selected region is part of one row, or
4519% one or more full rows, since then there is opportunity to access the
cristyad4e5b22010-01-10 00:04:19 +00004520% pixels in-place (without a copy) if the image is in memory, or in a
4521% memory-mapped file. The returned pointer must *never* be deallocated
cristy3ed852e2009-09-05 21:47:34 +00004522% by the user.
4523%
4524% Pixels accessed via the returned pointer represent a simple array of type
4525% PixelPacket. If the image type is CMYK or the storage class is PseudoClass,
4526% call GetAuthenticIndexQueue() after invoking GetAuthenticPixels() to obtain
4527% the black color component or the colormap indexes (of type IndexPacket)
4528% corresponding to the region. Once the PixelPacket (and/or IndexPacket)
4529% array has been updated, the changes must be saved back to the underlying
4530% image using SyncAuthenticPixels() or they may be lost.
4531%
4532% The format of the QueueAuthenticPixels() method is:
4533%
4534% PixelPacket *QueueAuthenticPixels(Image *image,const long x,const long y,
4535% const unsigned long columns,const unsigned long rows,
4536% ExceptionInfo *exception)
4537%
4538% A description of each parameter follows:
4539%
4540% o image: the image.
4541%
4542% o x,y,columns,rows: These values define the perimeter of a region of
4543% pixels.
4544%
4545% o exception: return any errors or warnings in this structure.
4546%
4547*/
4548MagickExport PixelPacket *QueueAuthenticPixels(Image *image,const long x,
4549 const long y,const unsigned long columns,const unsigned long rows,
4550 ExceptionInfo *exception)
4551{
4552 CacheInfo
4553 *cache_info;
4554
4555 PixelPacket
4556 *pixels;
4557
4558 assert(image != (Image *) NULL);
4559 assert(image->signature == MagickSignature);
4560 if (image->debug != MagickFalse)
4561 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4562 assert(image->cache != (Cache) NULL);
4563 cache_info=(CacheInfo *) image->cache;
4564 assert(cache_info->signature == MagickSignature);
4565 if (cache_info->methods.queue_authentic_pixels_handler ==
4566 (QueueAuthenticPixelsHandler) NULL)
4567 return((PixelPacket *) NULL);
4568 pixels=cache_info->methods.queue_authentic_pixels_handler(image,x,y,columns,
4569 rows,exception);
4570 return(pixels);
4571}
4572
4573/*
4574%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4575% %
4576% %
4577% %
4578+ R e a d P i x e l C a c h e I n d e x e s %
4579% %
4580% %
4581% %
4582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4583%
4584% ReadPixelCacheIndexes() reads colormap indexes from the specified region of
4585% the pixel cache.
4586%
4587% The format of the ReadPixelCacheIndexes() method is:
4588%
4589% MagickBooleanType ReadPixelCacheIndexes(CacheInfo *cache_info,
4590% NexusInfo *nexus_info,ExceptionInfo *exception)
4591%
4592% A description of each parameter follows:
4593%
4594% o cache_info: the pixel cache.
4595%
4596% o nexus_info: the cache nexus to read the colormap indexes.
4597%
4598% o exception: return any errors or warnings in this structure.
4599%
4600*/
4601static MagickBooleanType ReadPixelCacheIndexes(CacheInfo *cache_info,
4602 NexusInfo *nexus_info,ExceptionInfo *exception)
4603{
4604 MagickOffsetType
4605 count,
4606 offset;
4607
4608 MagickSizeType
4609 length,
4610 number_pixels;
4611
4612 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004613 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004614
4615 register long
4616 y;
4617
4618 unsigned long
4619 rows;
4620
4621 if (cache_info->debug != MagickFalse)
4622 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4623 cache_info->filename);
4624 if (cache_info->active_index_channel == MagickFalse)
4625 return(MagickFalse);
4626 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
4627 return(MagickTrue);
4628 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
4629 nexus_info->region.x;
4630 length=(MagickSizeType) nexus_info->region.width*sizeof(IndexPacket);
4631 rows=nexus_info->region.height;
4632 number_pixels=length*rows;
cristy3ed852e2009-09-05 21:47:34 +00004633 q=nexus_info->indexes;
4634 switch (cache_info->type)
4635 {
4636 case MemoryCache:
4637 case MapCache:
4638 {
4639 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00004640 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004641
4642 /*
4643 Read indexes from memory.
4644 */
cristydd341db2010-03-04 19:06:38 +00004645 if ((cache_info->columns == nexus_info->region.width) &&
4646 (number_pixels == (MagickSizeType) ((size_t) number_pixels)))
4647 {
4648 length=number_pixels;
4649 rows=1UL;
4650 }
cristy3ed852e2009-09-05 21:47:34 +00004651 p=cache_info->indexes+offset;
4652 for (y=0; y < (long) rows; y++)
4653 {
4654 (void) CopyMagickMemory(q,p,(size_t) length);
4655 p+=cache_info->columns;
4656 q+=nexus_info->region.width;
4657 }
4658 break;
4659 }
4660 case DiskCache:
4661 {
4662 /*
4663 Read indexes from disk.
4664 */
4665 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
4666 {
4667 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
4668 cache_info->cache_filename);
4669 return(MagickFalse);
4670 }
cristydd341db2010-03-04 19:06:38 +00004671 if ((cache_info->columns == nexus_info->region.width) &&
4672 (number_pixels < MagickMaxBufferExtent))
4673 {
4674 length=number_pixels;
4675 rows=1UL;
4676 }
cristy3ed852e2009-09-05 21:47:34 +00004677 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
4678 for (y=0; y < (long) rows; y++)
4679 {
4680 count=ReadPixelCacheRegion(cache_info,cache_info->offset+number_pixels*
4681 sizeof(PixelPacket)+offset*sizeof(*q),length,(unsigned char *) q);
4682 if ((MagickSizeType) count < length)
4683 break;
4684 offset+=cache_info->columns;
4685 q+=nexus_info->region.width;
4686 }
4687 if (y < (long) rows)
4688 {
4689 ThrowFileException(exception,CacheError,"UnableToReadPixelCache",
4690 cache_info->cache_filename);
4691 return(MagickFalse);
4692 }
4693 break;
4694 }
4695 default:
4696 break;
4697 }
4698 if ((cache_info->debug != MagickFalse) &&
4699 (QuantumTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
4700 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s[%lux%lu%+ld%+ld]",
4701 cache_info->filename,nexus_info->region.width,nexus_info->region.height,
4702 nexus_info->region.x,nexus_info->region.y);
4703 return(MagickTrue);
4704}
4705
4706/*
4707%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4708% %
4709% %
4710% %
4711+ R e a d P i x e l C a c h e P i x e l s %
4712% %
4713% %
4714% %
4715%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4716%
4717% ReadPixelCachePixels() reads pixels from the specified region of the pixel
4718% cache.
4719%
4720% The format of the ReadPixelCachePixels() method is:
4721%
4722% MagickBooleanType ReadPixelCachePixels(CacheInfo *cache_info,
4723% NexusInfo *nexus_info,ExceptionInfo *exception)
4724%
4725% A description of each parameter follows:
4726%
4727% o cache_info: the pixel cache.
4728%
4729% o nexus_info: the cache nexus to read the pixels.
4730%
4731% o exception: return any errors or warnings in this structure.
4732%
4733*/
4734static MagickBooleanType ReadPixelCachePixels(CacheInfo *cache_info,
4735 NexusInfo *nexus_info,ExceptionInfo *exception)
4736{
4737 MagickOffsetType
4738 count,
4739 offset;
4740
4741 MagickSizeType
4742 length,
4743 number_pixels;
4744
4745 register long
4746 y;
4747
4748 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004749 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004750
4751 unsigned long
4752 rows;
4753
4754 if (cache_info->debug != MagickFalse)
4755 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4756 cache_info->filename);
4757 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
4758 return(MagickTrue);
4759 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
4760 nexus_info->region.x;
4761 length=(MagickSizeType) nexus_info->region.width*sizeof(PixelPacket);
4762 rows=nexus_info->region.height;
4763 number_pixels=length*rows;
cristy3ed852e2009-09-05 21:47:34 +00004764 q=nexus_info->pixels;
4765 switch (cache_info->type)
4766 {
4767 case MemoryCache:
4768 case MapCache:
4769 {
4770 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00004771 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004772
4773 /*
4774 Read pixels from memory.
4775 */
cristydd341db2010-03-04 19:06:38 +00004776 if ((cache_info->columns == nexus_info->region.width) &&
4777 (number_pixels == (MagickSizeType) ((size_t) number_pixels)))
4778 {
4779 length=number_pixels;
4780 rows=1UL;
4781 }
cristy3ed852e2009-09-05 21:47:34 +00004782 p=cache_info->pixels+offset;
4783 for (y=0; y < (long) rows; y++)
4784 {
4785 (void) CopyMagickMemory(q,p,(size_t) length);
4786 p+=cache_info->columns;
4787 q+=nexus_info->region.width;
4788 }
4789 break;
4790 }
4791 case DiskCache:
4792 {
4793 /*
4794 Read pixels from disk.
4795 */
4796 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
4797 {
4798 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
4799 cache_info->cache_filename);
4800 return(MagickFalse);
4801 }
cristydd341db2010-03-04 19:06:38 +00004802 if ((cache_info->columns == nexus_info->region.width) &&
4803 (number_pixels < MagickMaxBufferExtent))
4804 {
4805 length=number_pixels;
4806 rows=1UL;
4807 }
cristy3ed852e2009-09-05 21:47:34 +00004808 for (y=0; y < (long) rows; y++)
4809 {
4810 count=ReadPixelCacheRegion(cache_info,cache_info->offset+offset*
4811 sizeof(*q),length,(unsigned char *) q);
4812 if ((MagickSizeType) count < length)
4813 break;
4814 offset+=cache_info->columns;
4815 q+=nexus_info->region.width;
4816 }
4817 if (y < (long) rows)
4818 {
4819 ThrowFileException(exception,CacheError,"UnableToReadPixelCache",
4820 cache_info->cache_filename);
4821 return(MagickFalse);
4822 }
4823 break;
4824 }
4825 default:
4826 break;
4827 }
4828 if ((cache_info->debug != MagickFalse) &&
4829 (QuantumTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
4830 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s[%lux%lu%+ld%+ld]",
4831 cache_info->filename,nexus_info->region.width,nexus_info->region.height,
4832 nexus_info->region.x,nexus_info->region.y);
4833 return(MagickTrue);
4834}
4835
4836/*
4837%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4838% %
4839% %
4840% %
4841+ R e f e r e n c e P i x e l C a c h e %
4842% %
4843% %
4844% %
4845%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4846%
4847% ReferencePixelCache() increments the reference count associated with the
4848% pixel cache returning a pointer to the cache.
4849%
4850% The format of the ReferencePixelCache method is:
4851%
4852% Cache ReferencePixelCache(Cache cache_info)
4853%
4854% A description of each parameter follows:
4855%
4856% o cache_info: the pixel cache.
4857%
4858*/
4859MagickExport Cache ReferencePixelCache(Cache cache)
4860{
4861 CacheInfo
4862 *cache_info;
4863
4864 assert(cache != (Cache *) NULL);
4865 cache_info=(CacheInfo *) cache;
4866 assert(cache_info->signature == MagickSignature);
4867 if (cache_info->debug != MagickFalse)
4868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4869 cache_info->filename);
cristyf84a1932010-01-03 18:00:18 +00004870 LockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00004871 cache_info->reference_count++;
cristyf84a1932010-01-03 18:00:18 +00004872 UnlockSemaphoreInfo(cache_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00004873 return(cache_info);
4874}
4875
4876/*
4877%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4878% %
4879% %
4880% %
4881+ S e t P i x e l C a c h e M e t h o d s %
4882% %
4883% %
4884% %
4885%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4886%
4887% SetPixelCacheMethods() sets the image pixel methods to the specified ones.
4888%
4889% The format of the SetPixelCacheMethods() method is:
4890%
4891% SetPixelCacheMethods(Cache *,CacheMethods *cache_methods)
4892%
4893% A description of each parameter follows:
4894%
4895% o cache: the pixel cache.
4896%
4897% o cache_methods: Specifies a pointer to a CacheMethods structure.
4898%
4899*/
4900MagickExport void SetPixelCacheMethods(Cache cache,CacheMethods *cache_methods)
4901{
4902 CacheInfo
4903 *cache_info;
4904
4905 GetOneAuthenticPixelFromHandler
4906 get_one_authentic_pixel_from_handler;
4907
4908 GetOneVirtualPixelFromHandler
4909 get_one_virtual_pixel_from_handler;
4910
4911 /*
4912 Set cache pixel methods.
4913 */
4914 assert(cache != (Cache) NULL);
4915 assert(cache_methods != (CacheMethods *) NULL);
4916 cache_info=(CacheInfo *) cache;
4917 assert(cache_info->signature == MagickSignature);
4918 if (cache_info->debug != MagickFalse)
4919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4920 cache_info->filename);
4921 if (cache_methods->get_virtual_pixel_handler != (GetVirtualPixelHandler) NULL)
4922 cache_info->methods.get_virtual_pixel_handler=
4923 cache_methods->get_virtual_pixel_handler;
4924 if (cache_methods->destroy_pixel_handler != (DestroyPixelHandler) NULL)
4925 cache_info->methods.destroy_pixel_handler=
4926 cache_methods->destroy_pixel_handler;
4927 if (cache_methods->get_virtual_indexes_from_handler !=
4928 (GetVirtualIndexesFromHandler) NULL)
4929 cache_info->methods.get_virtual_indexes_from_handler=
4930 cache_methods->get_virtual_indexes_from_handler;
4931 if (cache_methods->get_authentic_pixels_handler !=
4932 (GetAuthenticPixelsHandler) NULL)
4933 cache_info->methods.get_authentic_pixels_handler=
4934 cache_methods->get_authentic_pixels_handler;
4935 if (cache_methods->queue_authentic_pixels_handler !=
4936 (QueueAuthenticPixelsHandler) NULL)
4937 cache_info->methods.queue_authentic_pixels_handler=
4938 cache_methods->queue_authentic_pixels_handler;
4939 if (cache_methods->sync_authentic_pixels_handler !=
4940 (SyncAuthenticPixelsHandler) NULL)
4941 cache_info->methods.sync_authentic_pixels_handler=
4942 cache_methods->sync_authentic_pixels_handler;
4943 if (cache_methods->get_authentic_pixels_from_handler !=
4944 (GetAuthenticPixelsFromHandler) NULL)
4945 cache_info->methods.get_authentic_pixels_from_handler=
4946 cache_methods->get_authentic_pixels_from_handler;
4947 if (cache_methods->get_authentic_indexes_from_handler !=
4948 (GetAuthenticIndexesFromHandler) NULL)
4949 cache_info->methods.get_authentic_indexes_from_handler=
4950 cache_methods->get_authentic_indexes_from_handler;
4951 get_one_virtual_pixel_from_handler=
4952 cache_info->methods.get_one_virtual_pixel_from_handler;
4953 if (get_one_virtual_pixel_from_handler !=
4954 (GetOneVirtualPixelFromHandler) NULL)
4955 cache_info->methods.get_one_virtual_pixel_from_handler=
4956 cache_methods->get_one_virtual_pixel_from_handler;
4957 get_one_authentic_pixel_from_handler=
4958 cache_methods->get_one_authentic_pixel_from_handler;
4959 if (get_one_authentic_pixel_from_handler !=
4960 (GetOneAuthenticPixelFromHandler) NULL)
4961 cache_info->methods.get_one_authentic_pixel_from_handler=
4962 cache_methods->get_one_authentic_pixel_from_handler;
4963}
4964
4965/*
4966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4967% %
4968% %
4969% %
4970+ S e t P i x e l C a c h e N e x u s P i x e l s %
4971% %
4972% %
4973% %
4974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4975%
4976% SetPixelCacheNexusPixels() defines the region of the cache for the
4977% specified cache nexus.
4978%
4979% The format of the SetPixelCacheNexusPixels() method is:
4980%
4981% PixelPacket SetPixelCacheNexusPixels(const Image *image,
4982% const RectangleInfo *region,NexusInfo *nexus_info,
4983% ExceptionInfo *exception)
4984%
4985% A description of each parameter follows:
4986%
4987% o image: the image.
4988%
4989% o region: A pointer to the RectangleInfo structure that defines the
4990% region of this particular cache nexus.
4991%
4992% o nexus_info: the cache nexus to set.
4993%
4994% o exception: return any errors or warnings in this structure.
4995%
4996*/
4997static PixelPacket *SetPixelCacheNexusPixels(const Image *image,
4998 const RectangleInfo *region,NexusInfo *nexus_info,ExceptionInfo *exception)
4999{
5000 CacheInfo
5001 *cache_info;
5002
5003 MagickBooleanType
5004 status;
5005
cristy3ed852e2009-09-05 21:47:34 +00005006 MagickSizeType
5007 length,
5008 number_pixels;
5009
5010 if (image->debug != MagickFalse)
5011 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5012 cache_info=(CacheInfo *) image->cache;
5013 assert(cache_info->signature == MagickSignature);
5014 if (cache_info->type == UndefinedCache)
5015 return((PixelPacket *) NULL);
5016 nexus_info->region.width=region->width == 0UL ? 1UL : region->width;
5017 nexus_info->region.height=region->height == 0UL ? 1UL : region->height;
5018 nexus_info->region.x=region->x;
5019 nexus_info->region.y=region->y;
5020 if ((cache_info->type != DiskCache) && (image->clip_mask == (Image *) NULL) &&
5021 (image->mask == (Image *) NULL))
5022 {
cristybad067a2010-02-15 17:20:55 +00005023 long
5024 x,
5025 y;
cristy3ed852e2009-09-05 21:47:34 +00005026
cristybad067a2010-02-15 17:20:55 +00005027 x=nexus_info->region.x+nexus_info->region.width-1;
5028 y=nexus_info->region.y+nexus_info->region.height-1;
5029 if (((nexus_info->region.x >= 0) && (x < (long) cache_info->columns) &&
5030 (nexus_info->region.y >= 0) && (y < (long) cache_info->rows)) &&
cristy731c3532010-02-15 15:40:03 +00005031 ((nexus_info->region.height == 1UL) ||
5032 ((nexus_info->region.x == 0) &&
5033 ((nexus_info->region.width == cache_info->columns) ||
5034 ((nexus_info->region.width % cache_info->columns) == 0)))))
5035 {
5036 MagickOffsetType
5037 offset;
5038
5039 /*
5040 Pixels are accessed directly from memory.
5041 */
5042 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
5043 nexus_info->region.x;
5044 nexus_info->pixels=cache_info->pixels+offset;
5045 nexus_info->indexes=(IndexPacket *) NULL;
5046 if (cache_info->active_index_channel != MagickFalse)
5047 nexus_info->indexes=cache_info->indexes+offset;
5048 return(nexus_info->pixels);
cristy3ed852e2009-09-05 21:47:34 +00005049 }
5050 }
5051 /*
5052 Pixels are stored in a cache region until they are synced to the cache.
5053 */
5054 number_pixels=(MagickSizeType) nexus_info->region.width*
5055 nexus_info->region.height;
5056 length=number_pixels*sizeof(PixelPacket);
5057 if (cache_info->active_index_channel != MagickFalse)
5058 length+=number_pixels*sizeof(IndexPacket);
5059 if (nexus_info->cache == (PixelPacket *) NULL)
5060 {
5061 nexus_info->length=length;
5062 status=AcquireCacheNexusPixels(cache_info,nexus_info,exception);
5063 if (status == MagickFalse)
5064 return((PixelPacket *) NULL);
5065 }
5066 else
5067 if (nexus_info->length != length)
5068 {
5069 RelinquishCacheNexusPixels(nexus_info);
5070 nexus_info->length=length;
5071 status=AcquireCacheNexusPixels(cache_info,nexus_info,exception);
5072 if (status == MagickFalse)
5073 return((PixelPacket *) NULL);
5074 }
5075 nexus_info->pixels=nexus_info->cache;
5076 nexus_info->indexes=(IndexPacket *) NULL;
5077 if (cache_info->active_index_channel != MagickFalse)
5078 nexus_info->indexes=(IndexPacket *) (nexus_info->pixels+number_pixels);
5079 return(nexus_info->pixels);
5080}
5081
5082/*
5083%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5084% %
5085% %
5086% %
5087% 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 %
5088% %
5089% %
5090% %
5091%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5092%
5093% SetPixelCacheVirtualMethod() sets the "virtual pixels" method for the
5094% pixel cache and returns the previous setting. A virtual pixel is any pixel
5095% access that is outside the boundaries of the image cache.
5096%
5097% The format of the SetPixelCacheVirtualMethod() method is:
5098%
5099% VirtualPixelMethod SetPixelCacheVirtualMethod(const Image *image,
5100% const VirtualPixelMethod virtual_pixel_method)
5101%
5102% A description of each parameter follows:
5103%
5104% o image: the image.
5105%
5106% o virtual_pixel_method: choose the type of virtual pixel.
5107%
5108*/
5109MagickExport VirtualPixelMethod SetPixelCacheVirtualMethod(const Image *image,
5110 const VirtualPixelMethod virtual_pixel_method)
5111{
5112 CacheInfo
5113 *cache_info;
5114
5115 VirtualPixelMethod
5116 method;
5117
5118 assert(image != (Image *) NULL);
5119 assert(image->signature == MagickSignature);
5120 if (image->debug != MagickFalse)
5121 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5122 assert(image->cache != (Cache) NULL);
5123 cache_info=(CacheInfo *) image->cache;
5124 assert(cache_info->signature == MagickSignature);
5125 method=cache_info->virtual_pixel_method;
5126 cache_info->virtual_pixel_method=virtual_pixel_method;
5127 return(method);
5128}
5129
5130/*
5131%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5132% %
5133% %
5134% %
5135+ 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 %
5136% %
5137% %
5138% %
5139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5140%
5141% SyncAuthenticPixelCacheNexus() saves the authentic image pixels to the
5142% in-memory or disk cache. The method returns MagickTrue if the pixel region
5143% is synced, otherwise MagickFalse.
5144%
5145% The format of the SyncAuthenticPixelCacheNexus() method is:
5146%
5147% MagickBooleanType SyncAuthenticPixelCacheNexus(Image *image,
5148% NexusInfo *nexus_info,ExceptionInfo *exception)
5149%
5150% A description of each parameter follows:
5151%
5152% o image: the image.
5153%
5154% o nexus_info: the cache nexus to sync.
5155%
5156% o exception: return any errors or warnings in this structure.
5157%
5158*/
5159MagickExport MagickBooleanType SyncAuthenticPixelCacheNexus(Image *image,
5160 NexusInfo *nexus_info,ExceptionInfo *exception)
5161{
5162 CacheInfo
5163 *cache_info;
5164
5165 MagickBooleanType
5166 status;
5167
5168 /*
5169 Transfer pixels to the cache.
5170 */
5171 assert(image != (Image *) NULL);
5172 assert(image->signature == MagickSignature);
5173 if (image->debug != MagickFalse)
5174 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5175 if (image->cache == (Cache) NULL)
5176 ThrowBinaryException(CacheError,"PixelCacheIsNotOpen",image->filename);
5177 cache_info=(CacheInfo *) image->cache;
5178 if (cache_info->type == UndefinedCache)
5179 return(MagickFalse);
5180 if ((image->clip_mask != (Image *) NULL) &&
5181 (ClipPixelCacheNexus(image,nexus_info,exception) == MagickFalse))
5182 return(MagickFalse);
5183 if ((image->mask != (Image *) NULL) &&
5184 (MaskPixelCacheNexus(image,nexus_info,exception) == MagickFalse))
5185 return(MagickFalse);
5186 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
5187 return(MagickTrue);
5188 assert(cache_info->signature == MagickSignature);
5189 status=WritePixelCachePixels(cache_info,nexus_info,exception);
5190 if ((cache_info->active_index_channel != MagickFalse) &&
5191 (WritePixelCacheIndexes(cache_info,nexus_info,exception) == MagickFalse))
5192 return(MagickFalse);
5193 return(status);
5194}
5195
5196/*
5197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5198% %
5199% %
5200% %
5201+ S y n c A u t h e n t i c P i x e l C a c h e %
5202% %
5203% %
5204% %
5205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5206%
5207% SyncAuthenticPixelsCache() saves the authentic image pixels to the in-memory
5208% or disk cache. The method returns MagickTrue if the pixel region is synced,
5209% otherwise MagickFalse.
5210%
5211% The format of the SyncAuthenticPixelsCache() method is:
5212%
5213% MagickBooleanType SyncAuthenticPixelsCache(Image *image,
5214% ExceptionInfo *exception)
5215%
5216% A description of each parameter follows:
5217%
5218% o image: the image.
5219%
5220% o exception: return any errors or warnings in this structure.
5221%
5222*/
5223static MagickBooleanType SyncAuthenticPixelsCache(Image *image,
5224 ExceptionInfo *exception)
5225{
5226 CacheInfo
5227 *cache_info;
5228
5229 long
5230 id;
5231
5232 MagickBooleanType
5233 status;
5234
5235 cache_info=(CacheInfo *) image->cache;
5236 id=GetOpenMPThreadId();
5237 assert(id < (long) cache_info->number_threads);
5238 status=SyncAuthenticPixelCacheNexus(image,cache_info->nexus_info[id],
5239 exception);
5240 return(status);
5241}
5242
5243/*
5244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5245% %
5246% %
5247% %
5248% S y n c A u t h e n t i c P i x e l s %
5249% %
5250% %
5251% %
5252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5253%
5254% SyncAuthenticPixels() saves the image pixels to the in-memory or disk cache.
5255% The method returns MagickTrue if the pixel region is flushed, otherwise
5256% MagickFalse.
5257%
5258% The format of the SyncAuthenticPixels() method is:
5259%
5260% MagickBooleanType SyncAuthenticPixels(Image *image,
5261% ExceptionInfo *exception)
5262%
5263% A description of each parameter follows:
5264%
5265% o image: the image.
5266%
5267% o exception: return any errors or warnings in this structure.
5268%
5269*/
5270MagickExport MagickBooleanType SyncAuthenticPixels(Image *image,
5271 ExceptionInfo *exception)
5272{
5273 CacheInfo
5274 *cache_info;
5275
5276 assert(image != (Image *) NULL);
5277 assert(image->signature == MagickSignature);
5278 if (image->debug != MagickFalse)
5279 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5280 assert(image->cache != (Cache) NULL);
5281 cache_info=(CacheInfo *) image->cache;
5282 assert(cache_info->signature == MagickSignature);
5283 if (cache_info->methods.sync_authentic_pixels_handler ==
5284 (SyncAuthenticPixelsHandler) NULL)
5285 return(MagickFalse);
5286 return(cache_info->methods.sync_authentic_pixels_handler(image,exception));
5287}
5288
5289/*
5290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5291% %
5292% %
5293% %
5294+ W r i t e P i x e l C a c h e I n d e x e s %
5295% %
5296% %
5297% %
5298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5299%
5300% WritePixelCacheIndexes() writes the colormap indexes to the specified
5301% region of the pixel cache.
5302%
5303% The format of the WritePixelCacheIndexes() method is:
5304%
5305% MagickBooleanType WritePixelCacheIndexes(CacheInfo *cache_info,
5306% NexusInfo *nexus_info,ExceptionInfo *exception)
5307%
5308% A description of each parameter follows:
5309%
5310% o cache_info: the pixel cache.
5311%
5312% o nexus_info: the cache nexus to write the colormap indexes.
5313%
5314% o exception: return any errors or warnings in this structure.
5315%
5316*/
5317static MagickBooleanType WritePixelCacheIndexes(CacheInfo *cache_info,
5318 NexusInfo *nexus_info,ExceptionInfo *exception)
5319{
5320 MagickOffsetType
5321 count,
5322 offset;
5323
5324 MagickSizeType
5325 length,
5326 number_pixels;
5327
5328 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005329 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005330
5331 register long
5332 y;
5333
5334 unsigned long
5335 rows;
5336
5337 if (cache_info->debug != MagickFalse)
5338 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
5339 cache_info->filename);
5340 if (cache_info->active_index_channel == MagickFalse)
5341 return(MagickFalse);
5342 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
5343 return(MagickTrue);
5344 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
5345 nexus_info->region.x;
5346 length=(MagickSizeType) nexus_info->region.width*sizeof(IndexPacket);
5347 rows=nexus_info->region.height;
5348 number_pixels=(MagickSizeType) length*rows;
cristy3ed852e2009-09-05 21:47:34 +00005349 p=nexus_info->indexes;
5350 switch (cache_info->type)
5351 {
5352 case MemoryCache:
5353 case MapCache:
5354 {
5355 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00005356 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005357
5358 /*
5359 Write indexes to memory.
5360 */
cristydd341db2010-03-04 19:06:38 +00005361 if ((cache_info->columns == nexus_info->region.width) &&
5362 (number_pixels == (MagickSizeType) ((size_t) number_pixels)))
5363 {
5364 length=number_pixels;
5365 rows=1UL;
5366 }
cristy3ed852e2009-09-05 21:47:34 +00005367 q=cache_info->indexes+offset;
5368 for (y=0; y < (long) rows; y++)
5369 {
5370 (void) CopyMagickMemory(q,p,(size_t) length);
5371 p+=nexus_info->region.width;
5372 q+=cache_info->columns;
5373 }
5374 break;
5375 }
5376 case DiskCache:
5377 {
5378 /*
5379 Write indexes to disk.
5380 */
5381 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
5382 {
5383 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
5384 cache_info->cache_filename);
5385 return(MagickFalse);
5386 }
cristydd341db2010-03-04 19:06:38 +00005387 if ((cache_info->columns == nexus_info->region.width) &&
5388 (number_pixels < MagickMaxBufferExtent))
5389 {
5390 length=number_pixels;
5391 rows=1UL;
5392 }
cristy3ed852e2009-09-05 21:47:34 +00005393 number_pixels=(MagickSizeType) cache_info->columns*cache_info->rows;
5394 for (y=0; y < (long) rows; y++)
5395 {
5396 count=WritePixelCacheRegion(cache_info,cache_info->offset+number_pixels*
5397 sizeof(PixelPacket)+offset*sizeof(*p),length,
5398 (const unsigned char *) p);
5399 if ((MagickSizeType) count < length)
5400 break;
5401 p+=nexus_info->region.width;
5402 offset+=cache_info->columns;
5403 }
5404 if (y < (long) rows)
5405 {
5406 ThrowFileException(exception,CacheError,"UnableToWritePixelCache",
5407 cache_info->cache_filename);
5408 return(MagickFalse);
5409 }
5410 break;
5411 }
5412 default:
5413 break;
5414 }
5415 if ((cache_info->debug != MagickFalse) &&
5416 (QuantumTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
5417 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s[%lux%lu%+ld%+ld]",
5418 cache_info->filename,nexus_info->region.width,nexus_info->region.height,
5419 nexus_info->region.x,nexus_info->region.y);
5420 return(MagickTrue);
5421}
5422
5423/*
5424%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5425% %
5426% %
5427% %
5428+ W r i t e C a c h e P i x e l s %
5429% %
5430% %
5431% %
5432%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5433%
5434% WritePixelCachePixels() writes image pixels to the specified region of the
5435% pixel cache.
5436%
5437% The format of the WritePixelCachePixels() method is:
5438%
5439% MagickBooleanType WritePixelCachePixels(CacheInfo *cache_info,
5440% NexusInfo *nexus_info,ExceptionInfo *exception)
5441%
5442% A description of each parameter follows:
5443%
5444% o cache_info: the pixel cache.
5445%
5446% o nexus_info: the cache nexus to write the pixels.
5447%
5448% o exception: return any errors or warnings in this structure.
5449%
5450*/
5451static MagickBooleanType WritePixelCachePixels(CacheInfo *cache_info,
5452 NexusInfo *nexus_info,ExceptionInfo *exception)
5453{
5454 MagickOffsetType
5455 count,
5456 offset;
5457
5458 MagickSizeType
5459 length,
5460 number_pixels;
5461
cristy3ed852e2009-09-05 21:47:34 +00005462 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005463 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005464
cristy25ffffb2009-12-07 17:15:07 +00005465 register long
5466 y;
5467
cristy3ed852e2009-09-05 21:47:34 +00005468 unsigned long
5469 rows;
5470
5471 if (cache_info->debug != MagickFalse)
5472 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
5473 cache_info->filename);
5474 if (IsNexusInCore(cache_info,nexus_info) != MagickFalse)
5475 return(MagickTrue);
5476 offset=(MagickOffsetType) nexus_info->region.y*cache_info->columns+
5477 nexus_info->region.x;
5478 length=(MagickSizeType) nexus_info->region.width*sizeof(PixelPacket);
5479 rows=nexus_info->region.height;
5480 number_pixels=length*rows;
cristy3ed852e2009-09-05 21:47:34 +00005481 p=nexus_info->pixels;
5482 switch (cache_info->type)
5483 {
5484 case MemoryCache:
5485 case MapCache:
5486 {
5487 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00005488 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005489
5490 /*
5491 Write pixels to memory.
5492 */
cristydd341db2010-03-04 19:06:38 +00005493 if ((cache_info->columns == nexus_info->region.width) &&
5494 (number_pixels == (MagickSizeType) ((size_t) number_pixels)))
5495 {
5496 length=number_pixels;
5497 rows=1UL;
5498 }
cristy3ed852e2009-09-05 21:47:34 +00005499 q=cache_info->pixels+offset;
5500 for (y=0; y < (long) rows; y++)
5501 {
5502 (void) CopyMagickMemory(q,p,(size_t) length);
5503 p+=nexus_info->region.width;
5504 q+=cache_info->columns;
5505 }
5506 break;
5507 }
5508 case DiskCache:
5509 {
5510 /*
5511 Write pixels to disk.
5512 */
5513 if (OpenPixelCacheOnDisk(cache_info,IOMode) == MagickFalse)
5514 {
5515 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
5516 cache_info->cache_filename);
5517 return(MagickFalse);
5518 }
cristydd341db2010-03-04 19:06:38 +00005519 if ((cache_info->columns == nexus_info->region.width) &&
5520 (number_pixels < MagickMaxBufferExtent))
5521 {
5522 length=number_pixels;
5523 rows=1UL;
5524 }
cristy3ed852e2009-09-05 21:47:34 +00005525 for (y=0; y < (long) rows; y++)
5526 {
5527 count=WritePixelCacheRegion(cache_info,cache_info->offset+offset*
5528 sizeof(*p),length,(const unsigned char *) p);
5529 if ((MagickSizeType) count < length)
5530 break;
5531 p+=nexus_info->region.width;
5532 offset+=cache_info->columns;
5533 }
5534 if (y < (long) rows)
5535 {
5536 ThrowFileException(exception,CacheError,"UnableToWritePixelCache",
5537 cache_info->cache_filename);
5538 return(MagickFalse);
5539 }
5540 break;
5541 }
5542 default:
5543 break;
5544 }
5545 if ((cache_info->debug != MagickFalse) &&
5546 (QuantumTick(nexus_info->region.y,cache_info->rows) != MagickFalse))
5547 (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s[%lux%lu%+ld%+ld]",
5548 cache_info->filename,nexus_info->region.width,nexus_info->region.height,
5549 nexus_info->region.x,nexus_info->region.y);
5550 return(MagickTrue);
5551}