]> granicus.if.org Git - imagemagick/blob - MagickWand/wand-view.c
434a4d9c79fc78848f6f3a1ea84ddaed7c25e727
[imagemagick] / MagickWand / wand-view.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                        W   W   AAA   N   N  DDDD                            %
6 %                        W   W  A   A  NN  N  D   D                           %
7 %                        W W W  AAAAA  N N N  D   D                           %
8 %                        WW WW  A   A  N  NN  D   D                           %
9 %                        W   W  A   A  N   N  DDDD                            %
10 %                                                                             %
11 %                        V   V  IIIII  EEEEE  W   W                           %
12 %                        V   V    I    E      W   W                           %
13 %                        V   V    I    EEE    W W W                           %
14 %                         V V     I    E      WW WW                           %
15 %                          V    IIIII  EEEEE  W   W                           %
16 %                                                                             %
17 %                                                                             %
18 %                        MagickWand Wand View Methods                         %
19 %                                                                             %
20 %                              Software Design                                %
21 %                                John Cristy                                  %
22 %                                March 2003                                   %
23 %                                                                             %
24 %                                                                             %
25 %  Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization      %
26 %  dedicated to making software imaging solutions freely available.           %
27 %                                                                             %
28 %  You may not use this file except in compliance with the License.  You may  %
29 %  obtain a copy of the License at                                            %
30 %                                                                             %
31 %    http://www.imagemagick.org/script/license.php                            %
32 %                                                                             %
33 %  Unless required by applicable law or agreed to in writing, software        %
34 %  distributed under the License is distributed on an "AS IS" BASIS,          %
35 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
36 %  See the License for the specific language governing permissions and        %
37 %  limitations under the License.                                             %
38 %                                                                             %
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %
41 %
42 %
43 */
44 \f
45 /*
46   Include declarations.
47 */
48 #include "MagickWand/studio.h"
49 #include "MagickWand/MagickWand.h"
50 #include "MagickWand/magick-wand-private.h"
51 #include "MagickWand/wand.h"
52 #include "MagickCore/monitor-private.h"
53 #include "MagickCore/thread-private.h"
54 /*
55  Define declarations.
56 */
57 #define WandViewId  "WandView"
58 \f
59 /*
60   Typedef declarations.
61 */
62 struct _WandView
63 {
64   size_t
65     id;
66
67   char
68     name[MaxTextExtent],
69     *description;
70
71   RectangleInfo
72     extent;
73
74   MagickWand
75     *wand;
76
77   Image
78     *image;
79
80   CacheView
81     *view;
82
83   PixelWand
84     ***pixel_wands;
85
86   ExceptionInfo
87     *exception;
88
89   MagickBooleanType
90     debug;
91
92   size_t
93     signature;
94 };
95 \f
96 /*
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 %                                                                             %
99 %                                                                             %
100 %                                                                             %
101 %   C l o n e W a n d V i e w                                                 %
102 %                                                                             %
103 %                                                                             %
104 %                                                                             %
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %
107 %  CloneWandView() makes a copy of the specified wand view.
108 %
109 %  The format of the CloneWandView method is:
110 %
111 %      WandView *CloneWandView(const WandView *wand_view)
112 %
113 %  A description of each parameter follows:
114 %
115 %    o wand_view: the wand view.
116 %
117 */
118 WandExport WandView *CloneWandView(const WandView *wand_view)
119 {
120   WandView
121     *clone_view;
122
123   register ssize_t
124     i;
125
126   assert(wand_view != (WandView *) NULL);
127   assert(wand_view->signature == WandSignature);
128   if (wand_view->debug != MagickFalse)
129     (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand_view->name);
130   clone_view=(WandView *) AcquireMagickMemory(sizeof(*clone_view));
131   if (clone_view == (WandView *) NULL)
132     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
133       wand_view->name);
134   (void) ResetMagickMemory(clone_view,0,sizeof(*clone_view));
135   clone_view->id=AcquireWandId();
136   (void) FormatLocaleString(clone_view->name,MaxTextExtent,"%s-%.20g",
137     WandViewId,(double) clone_view->id);
138   clone_view->description=ConstantString(wand_view->description);
139   clone_view->image=CloneImage(wand_view->image,0,0,MagickTrue,
140     wand_view->exception);
141   clone_view->view=CloneCacheView(wand_view->view);
142   clone_view->extent=wand_view->extent;
143   clone_view->exception=AcquireExceptionInfo();
144   InheritException(clone_view->exception,wand_view->exception);
145   for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
146     clone_view->pixel_wands[i]=ClonePixelWands((const PixelWand **)
147       wand_view->pixel_wands[i],wand_view->extent.width);
148   clone_view->debug=wand_view->debug;
149   if (clone_view->debug != MagickFalse)
150     (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",clone_view->name);
151   clone_view->signature=WandSignature;
152   return(clone_view);
153 }
154 \f
155 /*
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %                                                                             %
158 %                                                                             %
159 %                                                                             %
160 %   D e s t r o y W a n d V i e w                                             %
161 %                                                                             %
162 %                                                                             %
163 %                                                                             %
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 %
166 %  DestroyWandView() deallocates memory associated with a wand view.
167 %
168 %  The format of the DestroyWandView method is:
169 %
170 %      WandView *DestroyWandView(WandView *wand_view)
171 %
172 %  A description of each parameter follows:
173 %
174 %    o wand_view: the wand view.
175 %
176 */
177
178 static PixelWand ***DestroyPixelsThreadSet(PixelWand ***pixel_wands,
179   const size_t number_wands)
180 {
181   register ssize_t
182     i;
183
184   assert(pixel_wands != (PixelWand ***) NULL);
185   for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
186     if (pixel_wands[i] != (PixelWand **) NULL)
187       pixel_wands[i]=DestroyPixelWands(pixel_wands[i],number_wands);
188   pixel_wands=(PixelWand ***) RelinquishMagickMemory(pixel_wands);
189   return(pixel_wands);
190 }
191
192 WandExport WandView *DestroyWandView(WandView *wand_view)
193 {
194   assert(wand_view != (WandView *) NULL);
195   assert(wand_view->signature == WandSignature);
196   wand_view->pixel_wands=DestroyPixelsThreadSet(wand_view->pixel_wands,
197     wand_view->extent.width);
198   wand_view->image=DestroyImage(wand_view->image);
199   wand_view->view=DestroyCacheView(wand_view->view);
200   wand_view->exception=DestroyExceptionInfo(wand_view->exception);
201   wand_view->signature=(~WandSignature);
202   RelinquishWandId(wand_view->id);
203   wand_view=(WandView *) RelinquishMagickMemory(wand_view);
204   return(wand_view);
205 }
206 \f
207 /*
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 %                                                                             %
210 %                                                                             %
211 %                                                                             %
212 %   D u p l e x T r a n s f e r W a n d V i e w I t e r a t o r               %
213 %                                                                             %
214 %                                                                             %
215 %                                                                             %
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 %
218 %  DuplexTransferWandViewIterator() iterates over three wand views in
219 %  parallel and calls your transfer method for each scanline of the view.  The
220 %  source and duplex pixel extent is not confined to the image canvas-- that is
221 %  you can include negative offsets or widths or heights that exceed the image
222 %  dimension.  However, the destination wand view is confined to the image
223 %  canvas-- that is no negative offsets or widths or heights that exceed the
224 %  image dimension are permitted.
225 %
226 %  The callback signature is:
227 %
228 %      MagickBooleanType DuplexTransferImageViewMethod(const WandView *source,
229 %        const WandView *duplex,WandView *destination,const ssize_t y,
230 %        const int thread_id,void *context)
231 %
232 %  Use this pragma if the view is not single threaded:
233 %
234 %    #pragma omp critical
235 %
236 %  to define a section of code in your callback transfer method that must be
237 %  executed by a single thread at a time.
238 %
239 %  The format of the DuplexTransferWandViewIterator method is:
240 %
241 %      MagickBooleanType DuplexTransferWandViewIterator(WandView *source,
242 %        WandView *duplex,WandView *destination,
243 %        DuplexTransferWandViewMethod transfer,void *context)
244 %
245 %  A description of each parameter follows:
246 %
247 %    o source: the source wand view.
248 %
249 %    o duplex: the duplex wand view.
250 %
251 %    o destination: the destination wand view.
252 %
253 %    o transfer: the transfer callback method.
254 %
255 %    o context: the user defined context.
256 %
257 */
258 WandExport MagickBooleanType DuplexTransferWandViewIterator(WandView *source,
259   WandView *duplex,WandView *destination,DuplexTransferWandViewMethod transfer,
260   void *context)
261 {
262   Image
263     *destination_image,
264     *source_image;
265
266   MagickBooleanType
267     status;
268
269   MagickOffsetType
270     progress;
271
272   size_t
273     height,
274     width;
275
276   ssize_t
277     y;
278
279   assert(source != (WandView *) NULL);
280   assert(source->signature == WandSignature);
281   if (transfer == (DuplexTransferWandViewMethod) NULL)
282     return(MagickFalse);
283   source_image=source->wand->images;
284   destination_image=destination->wand->images;
285   status=SetImageStorageClass(destination_image,DirectClass,
286     destination->exception);
287   if (status == MagickFalse)
288     return(MagickFalse);
289   status=MagickTrue;
290   progress=0;
291   height=source->extent.height-source->extent.y;
292   width=source->extent.width-source->extent.x;
293 #if defined(MAGICKCORE_OPENMP_SUPPORT)
294   #pragma omp parallel for schedule(static) shared(progress,status) \
295     if ((height*width) > 8192) \
296       num_threads(GetMagickResourceLimit(ThreadResource))
297 #endif
298   for (y=source->extent.y; y < (ssize_t) source->extent.height; y++)
299   {
300     const int
301       id = GetOpenMPThreadId();
302
303     MagickBooleanType
304       sync;
305
306     register const Quantum
307       *restrict duplex_pixels,
308       *restrict pixels;
309
310     register ssize_t
311       x;
312
313     register Quantum
314       *restrict destination_pixels;
315
316     if (status == MagickFalse)
317       continue;
318     pixels=GetCacheViewVirtualPixels(source->view,source->extent.x,y,
319       source->extent.width,1,source->exception);
320     if (pixels == (const Quantum *) NULL)
321       {
322         status=MagickFalse;
323         continue;
324       }
325     for (x=0; x < (ssize_t) source->extent.width; x++)
326     {
327       PixelSetQuantumPixel(source->image,pixels,source->pixel_wands[id][x]);
328       pixels+=GetPixelChannels(source->image);
329     }
330     duplex_pixels=GetCacheViewVirtualPixels(duplex->view,duplex->extent.x,y,
331       duplex->extent.width,1,duplex->exception);
332     if (duplex_pixels == (const Quantum *) NULL)
333       {
334         status=MagickFalse;
335         continue;
336       }
337     for (x=0; x < (ssize_t) duplex->extent.width; x++)
338     {
339       PixelSetQuantumPixel(duplex->image,duplex_pixels,
340         duplex->pixel_wands[id][x]);
341       duplex_pixels+=GetPixelChannels(duplex->image);
342     }
343     destination_pixels=GetCacheViewAuthenticPixels(destination->view,
344       destination->extent.x,y,destination->extent.width,1,
345       destination->exception);
346     if (destination_pixels == (Quantum *) NULL)
347       {
348         status=MagickFalse;
349         continue;
350       }
351     for (x=0; x < (ssize_t) destination->extent.width; x++)
352     {
353       PixelSetQuantumPixel(destination->image,destination_pixels,
354         destination->pixel_wands[id][x]);
355       destination_pixels+=GetPixelChannels(destination->image);
356     }
357     if (transfer(source,duplex,destination,y,id,context) == MagickFalse)
358       status=MagickFalse;
359     destination_pixels=GetCacheViewAuthenticPixels(destination->view,
360       destination->extent.x,y,destination->extent.width,1,
361       destination->exception);
362     for (x=0; x < (ssize_t) destination->extent.width; x++)
363     {
364       PixelGetQuantumPixel(destination->image,destination->pixel_wands[id][x],
365         destination_pixels);
366       destination_pixels+=GetPixelChannels(destination->image);
367     }
368     sync=SyncCacheViewAuthenticPixels(destination->view,destination->exception);
369     if (sync == MagickFalse)
370       status=MagickFalse;
371     if (source_image->progress_monitor != (MagickProgressMonitor) NULL)
372       {
373         MagickBooleanType
374           proceed;
375
376 #if defined(MAGICKCORE_OPENMP_SUPPORT)
377         #pragma omp critical (MagickWand_DuplexTransferWandViewIterator)
378 #endif
379         proceed=SetImageProgress(source_image,source->description,progress++,
380           source->extent.height);
381         if (proceed == MagickFalse)
382           status=MagickFalse;
383       }
384   }
385   return(status);
386 }
387 \f
388 /*
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 %                                                                             %
391 %                                                                             %
392 %                                                                             %
393 %   G e t W a n d V i e w E x c e p t i o n                                   %
394 %                                                                             %
395 %                                                                             %
396 %                                                                             %
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 %
399 %  GetWandViewException() returns the severity, reason, and description of any
400 %  error that occurs when utilizing a wand view.
401 %
402 %  The format of the GetWandViewException method is:
403 %
404 %      char *GetWandViewException(const WandView *wand_view,
405 %        ExceptionType *severity)
406 %
407 %  A description of each parameter follows:
408 %
409 %    o wand_view: the pixel wand_view.
410 %
411 %    o severity: the severity of the error is returned here.
412 %
413 */
414 WandExport char *GetWandViewException(const WandView *wand_view,
415   ExceptionType *severity)
416 {
417   char
418     *description;
419
420   assert(wand_view != (const WandView *) NULL);
421   assert(wand_view->signature == WandSignature);
422   if (wand_view->debug != MagickFalse)
423     (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand_view->name);
424   assert(severity != (ExceptionType *) NULL);
425   *severity=wand_view->exception->severity;
426   description=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
427     sizeof(*description));
428   if (description == (char *) NULL)
429     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
430       wand_view->name);
431   *description='\0';
432   if (wand_view->exception->reason != (char *) NULL)
433     (void) CopyMagickString(description,GetLocaleExceptionMessage(
434       wand_view->exception->severity,wand_view->exception->reason),
435         MaxTextExtent);
436   if (wand_view->exception->description != (char *) NULL)
437     {
438       (void) ConcatenateMagickString(description," (",MaxTextExtent);
439       (void) ConcatenateMagickString(description,GetLocaleExceptionMessage(
440         wand_view->exception->severity,wand_view->exception->description),
441         MaxTextExtent);
442       (void) ConcatenateMagickString(description,")",MaxTextExtent);
443     }
444   return(description);
445 }
446 \f
447 /*
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449 %                                                                             %
450 %                                                                             %
451 %                                                                             %
452 %   G e t W a n d V i e w E x t e n t                                         %
453 %                                                                             %
454 %                                                                             %
455 %                                                                             %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 %
458 %  GetWandViewExtent() returns the wand view extent.
459 %
460 %  The format of the GetWandViewExtent method is:
461 %
462 %      RectangleInfo GetWandViewExtent(const WandView *wand_view)
463 %
464 %  A description of each parameter follows:
465 %
466 %    o wand_view: the wand view.
467 %
468 */
469 WandExport RectangleInfo GetWandViewExtent(const WandView *wand_view)
470 {
471   assert(wand_view != (WandView *) NULL);
472   assert(wand_view->signature == WandSignature);
473   return(wand_view->extent);
474 }
475 \f
476 /*
477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 %                                                                             %
479 %                                                                             %
480 %                                                                             %
481 %   G e t W a n d V i e w I t e r a t o r                                     %
482 %                                                                             %
483 %                                                                             %
484 %                                                                             %
485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 %
487 %  GetWandViewIterator() iterates over the wand view in parallel and calls
488 %  your get method for each scanline of the view.  The pixel extent is
489 %  not confined to the image canvas-- that is you can include negative offsets
490 %  or widths or heights that exceed the image dimension.  Any updates to
491 %  the pixels in your callback are ignored.
492 %
493 %  The callback signature is:
494 %
495 %      MagickBooleanType GetImageViewMethod(const WandView *source,
496 %        const ssize_t y,const int thread_id,void *context)
497 %
498 %  Use this pragma if the view is not single threaded:
499 %
500 %    #pragma omp critical
501 %
502 %  to define a section of code in your callback get method that must be
503 %  executed by a single thread at a time.
504 %
505 %  The format of the GetWandViewIterator method is:
506 %
507 %      MagickBooleanType GetWandViewIterator(WandView *source,
508 %        GetWandViewMethod get,void *context)
509 %
510 %  A description of each parameter follows:
511 %
512 %    o source: the source wand view.
513 %
514 %    o get: the get callback method.
515 %
516 %    o context: the user defined context.
517 %
518 */
519 WandExport MagickBooleanType GetWandViewIterator(WandView *source,
520   GetWandViewMethod get,void *context)
521 {
522   Image
523     *source_image;
524
525   MagickBooleanType
526     status;
527
528   MagickOffsetType
529     progress;
530
531   size_t
532     height,
533     width;
534
535   ssize_t
536     y;
537
538   assert(source != (WandView *) NULL);
539   assert(source->signature == WandSignature);
540   if (get == (GetWandViewMethod) NULL)
541     return(MagickFalse);
542   source_image=source->wand->images;
543   status=MagickTrue;
544   progress=0;
545   height=source->extent.height-source->extent.y;
546   width=source->extent.width-source->extent.x;
547 #if defined(MAGICKCORE_OPENMP_SUPPORT)
548   #pragma omp parallel for schedule(static) shared(progress,status) \
549     if ((height*width) > 8192) \
550       num_threads(GetMagickResourceLimit(ThreadResource))
551 #endif
552   for (y=source->extent.y; y < (ssize_t) source->extent.height; y++)
553   {
554     const int
555       id = GetOpenMPThreadId();
556
557     register const Quantum
558       *pixels;
559
560     register ssize_t
561       x;
562
563     if (status == MagickFalse)
564       continue;
565     pixels=GetCacheViewVirtualPixels(source->view,source->extent.x,y,
566       source->extent.width,1,source->exception);
567     if (pixels == (const Quantum *) NULL)
568       {
569         status=MagickFalse;
570         continue;
571       }
572     for (x=0; x < (ssize_t) source->extent.width; x++)
573     {
574       PixelSetQuantumPixel(source->image,pixels,source->pixel_wands[id][x]);
575       pixels+=GetPixelChannels(source->image);
576     }
577     if (get(source,y,id,context) == MagickFalse)
578       status=MagickFalse;
579     if (source_image->progress_monitor != (MagickProgressMonitor) NULL)
580       {
581         MagickBooleanType
582           proceed;
583
584 #if defined(MAGICKCORE_OPENMP_SUPPORT)
585         #pragma omp critical (MagickWand_GetWandViewIterator)
586 #endif
587         proceed=SetImageProgress(source_image,source->description,progress++,
588           source->extent.height);
589         if (proceed == MagickFalse)
590           status=MagickFalse;
591       }
592   }
593   return(status);
594 }
595 \f
596 /*
597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598 %                                                                             %
599 %                                                                             %
600 %                                                                             %
601 %   G e t W a n d V i e w P i x e l s                                         %
602 %                                                                             %
603 %                                                                             %
604 %                                                                             %
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 %
607 %  GetWandViewPixels() returns the wand view pixel_wands.
608 %
609 %  The format of the GetWandViewPixels method is:
610 %
611 %      PixelWand *GetWandViewPixels(const WandView *wand_view)
612 %
613 %  A description of each parameter follows:
614 %
615 %    o wand_view: the wand view.
616 %
617 */
618 WandExport PixelWand **GetWandViewPixels(const WandView *wand_view)
619 {
620   const int
621     id = GetOpenMPThreadId();
622
623   assert(wand_view != (WandView *) NULL);
624   assert(wand_view->signature == WandSignature);
625   return(wand_view->pixel_wands[id]);
626 }
627 \f
628 /*
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 %                                                                             %
631 %                                                                             %
632 %                                                                             %
633 %   G e t W a n d V i e w W a n d                                             %
634 %                                                                             %
635 %                                                                             %
636 %                                                                             %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 %
639 %  GetWandViewWand() returns the magick wand associated with the wand view.
640 %
641 %  The format of the GetWandViewWand method is:
642 %
643 %      MagickWand *GetWandViewWand(const WandView *wand_view)
644 %
645 %  A description of each parameter follows:
646 %
647 %    o wand_view: the wand view.
648 %
649 */
650 WandExport MagickWand *GetWandViewWand(const WandView *wand_view)
651 {
652   assert(wand_view != (WandView *) NULL);
653   assert(wand_view->signature == WandSignature);
654   return(wand_view->wand);
655 }
656 \f
657 /*
658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659 %                                                                             %
660 %                                                                             %
661 %                                                                             %
662 %   I s W a n d V i e w                                                       %
663 %                                                                             %
664 %                                                                             %
665 %                                                                             %
666 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667 %
668 %  IsWandView() returns MagickTrue if the the parameter is verified as a wand
669 %  view object.
670 %
671 %  The format of the IsWandView method is:
672 %
673 %      MagickBooleanType IsWandView(const WandView *wand_view)
674 %
675 %  A description of each parameter follows:
676 %
677 %    o wand_view: the wand view.
678 %
679 */
680 WandExport MagickBooleanType IsWandView(const WandView *wand_view)
681 {
682   size_t
683     length;
684
685   if (wand_view == (const WandView *) NULL)
686     return(MagickFalse);
687   if (wand_view->signature != WandSignature)
688     return(MagickFalse);
689   length=strlen(WandViewId);
690   if (LocaleNCompare(wand_view->name,WandViewId,length) != 0)
691     return(MagickFalse);
692   return(MagickTrue);
693 }
694 \f
695 /*
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
697 %                                                                             %
698 %                                                                             %
699 %                                                                             %
700 %   N e w W a n d V i e w                                                     %
701 %                                                                             %
702 %                                                                             %
703 %                                                                             %
704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
705 %
706 %  NewWandView() returns a wand view required for all other methods in the
707 %  Wand View API.
708 %
709 %  The format of the NewWandView method is:
710 %
711 %      WandView *NewWandView(MagickWand *wand)
712 %
713 %  A description of each parameter follows:
714 %
715 %    o wand: the wand.
716 %
717 */
718
719 static PixelWand ***AcquirePixelsThreadSet(const size_t number_wands)
720 {
721   PixelWand
722     ***pixel_wands;
723
724   register ssize_t
725     i;
726
727   size_t
728     number_threads;
729
730   number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
731   pixel_wands=(PixelWand ***) AcquireQuantumMemory(number_threads,
732     sizeof(*pixel_wands));
733   if (pixel_wands == (PixelWand ***) NULL)
734     return((PixelWand ***) NULL);
735   (void) ResetMagickMemory(pixel_wands,0,number_threads*sizeof(*pixel_wands));
736   for (i=0; i < (ssize_t) number_threads; i++)
737   {
738     pixel_wands[i]=NewPixelWands(number_wands);
739     if (pixel_wands[i] == (PixelWand **) NULL)
740       return(DestroyPixelsThreadSet(pixel_wands,number_wands));
741   }
742   return(pixel_wands);
743 }
744
745 WandExport WandView *NewWandView(MagickWand *wand)
746 {
747   ExceptionInfo
748     *exception;
749
750   WandView
751     *wand_view;
752
753   assert(wand != (MagickWand *) NULL);
754   assert(wand->signature == WandSignature);
755   wand_view=(WandView *) AcquireMagickMemory(sizeof(*wand_view));
756   if (wand_view == (WandView *) NULL)
757     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
758       GetExceptionMessage(errno));
759   (void) ResetMagickMemory(wand_view,0,sizeof(*wand_view));
760   wand_view->id=AcquireWandId();
761   (void) FormatLocaleString(wand_view->name,MaxTextExtent,"%s-%.20g",
762     WandViewId,(double) wand_view->id);
763   wand_view->description=ConstantString("WandView");
764   wand_view->wand=wand;
765   exception=AcquireExceptionInfo();
766   wand_view->view=AcquireVirtualCacheView(wand_view->wand->images,exception);
767   wand_view->extent.width=wand->images->columns;
768   wand_view->extent.height=wand->images->rows;
769   wand_view->pixel_wands=AcquirePixelsThreadSet(wand_view->extent.width);
770   wand_view->exception=exception;
771   if (wand_view->pixel_wands == (PixelWand ***) NULL)
772     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
773       GetExceptionMessage(errno));
774   wand_view->debug=IsEventLogging();
775   wand_view->signature=WandSignature;
776   return(wand_view);
777 }
778 \f
779 /*
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
781 %                                                                             %
782 %                                                                             %
783 %                                                                             %
784 %   N e w W a n d V i e w E x t e n t                                         %
785 %                                                                             %
786 %                                                                             %
787 %                                                                             %
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
789 %
790 %  NewWandViewExtent() returns a wand view required for all other methods
791 %  in the Wand View API.
792 %
793 %  The format of the NewWandViewExtent method is:
794 %
795 %      WandView *NewWandViewExtent(MagickWand *wand,const ssize_t x,
796 %        const ssize_t y,const size_t width,const size_t height)
797 %
798 %  A description of each parameter follows:
799 %
800 %    o wand: the magick wand.
801 %
802 %    o x,y,columns,rows:  These values define the perimeter of a extent of
803 %      pixel_wands view.
804 %
805 */
806 WandExport WandView *NewWandViewExtent(MagickWand *wand,const ssize_t x,
807   const ssize_t y,const size_t width,const size_t height)
808 {
809   ExceptionInfo
810     *exception;
811
812   WandView
813     *wand_view;
814
815   assert(wand != (MagickWand *) NULL);
816   assert(wand->signature == WandSignature);
817   wand_view=(WandView *) AcquireMagickMemory(sizeof(*wand_view));
818   if (wand_view == (WandView *) NULL)
819     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
820       GetExceptionMessage(errno));
821   (void) ResetMagickMemory(wand_view,0,sizeof(*wand_view));
822   wand_view->id=AcquireWandId();
823   (void) FormatLocaleString(wand_view->name,MaxTextExtent,"%s-%.20g",
824     WandViewId,(double) wand_view->id);
825   wand_view->description=ConstantString("WandView");
826   exception=AcquireExceptionInfo();
827   wand_view->view=AcquireVirtualCacheView(wand_view->wand->images,exception);
828   wand_view->wand=wand;
829   wand_view->extent.width=width;
830   wand_view->extent.height=height;
831   wand_view->extent.x=x;
832   wand_view->extent.y=y;
833   wand_view->exception=exception;
834   wand_view->pixel_wands=AcquirePixelsThreadSet(wand_view->extent.width);
835   if (wand_view->pixel_wands == (PixelWand ***) NULL)
836     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
837       GetExceptionMessage(errno));
838   wand_view->debug=IsEventLogging();
839   wand_view->signature=WandSignature;
840   return(wand_view);
841 }
842 \f
843 /*
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845 %                                                                             %
846 %                                                                             %
847 %                                                                             %
848 %   S e t W a n d V i e w D e s c r i p t i o n                               %
849 %                                                                             %
850 %                                                                             %
851 %                                                                             %
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 %
854 %  SetWandViewDescription() associates a description with an image view.
855 %
856 %  The format of the SetWandViewDescription method is:
857 %
858 %      void SetWandViewDescription(WandView *image_view,const char *description)
859 %
860 %  A description of each parameter follows:
861 %
862 %    o wand_view: the wand view.
863 %
864 %    o description: the wand view description.
865 %
866 */
867 MagickExport void SetWandViewDescription(WandView *wand_view,
868   const char *description)
869 {
870   assert(wand_view != (WandView *) NULL);
871   assert(wand_view->signature == WandSignature);
872   wand_view->description=ConstantString(description);
873 }
874 \f
875 /*
876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
877 %                                                                             %
878 %                                                                             %
879 %                                                                             %
880 %   S e t W a n d V i e w I t e r a t o r                                     %
881 %                                                                             %
882 %                                                                             %
883 %                                                                             %
884 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
885 %
886 %  SetWandViewIterator() iterates over the wand view in parallel and calls
887 %  your set method for each scanline of the view.  The pixel extent is
888 %  confined to the image canvas-- that is no negative offsets or widths or
889 %  heights that exceed the image dimension.  The pixels are initiallly
890 %  undefined and any settings you make in the callback method are automagically
891 %  synced back to your image.
892 %
893 %  The callback signature is:
894 %
895 %      MagickBooleanType SetImageViewMethod(ImageView *destination,
896 %        const ssize_t y,const int thread_id,void *context)
897 %
898 %  Use this pragma if the view is not single threaded:
899 %
900 %    #pragma omp critical
901 %
902 %  to define a section of code in your callback set method that must be
903 %  executed by a single thread at a time.
904 %
905 %  The format of the SetWandViewIterator method is:
906 %
907 %      MagickBooleanType SetWandViewIterator(WandView *destination,
908 %        SetWandViewMethod set,void *context)
909 %
910 %  A description of each parameter follows:
911 %
912 %    o destination: the wand view.
913 %
914 %    o set: the set callback method.
915 %
916 %    o context: the user defined context.
917 %
918 */
919 WandExport MagickBooleanType SetWandViewIterator(WandView *destination,
920   SetWandViewMethod set,void *context)
921 {
922   Image
923     *destination_image;
924
925   MagickBooleanType
926     status;
927
928   MagickOffsetType
929     progress;
930
931   size_t
932     height,
933     width;
934
935   ssize_t
936     y;
937
938   assert(destination != (WandView *) NULL);
939   assert(destination->signature == WandSignature);
940   if (set == (SetWandViewMethod) NULL)
941     return(MagickFalse);
942   destination_image=destination->wand->images;
943   status=SetImageStorageClass(destination_image,DirectClass,
944     destination->exception);
945   if (status == MagickFalse)
946     return(MagickFalse);
947   status=MagickTrue;
948   progress=0;
949   height=destination->extent.height-destination->extent.y;
950   width=destination->extent.width-destination->extent.x;
951 #if defined(MAGICKCORE_OPENMP_SUPPORT)
952   #pragma omp parallel for schedule(static) shared(progress,status) \
953     if ((height*width) > 8192) \
954       num_threads(GetMagickResourceLimit(ThreadResource))
955 #endif
956   for (y=destination->extent.y; y < (ssize_t) destination->extent.height; y++)
957   {
958     const int
959       id = GetOpenMPThreadId();
960
961     MagickBooleanType
962       sync;
963
964     register ssize_t
965       x;
966
967     register Quantum
968       *restrict pixels;
969
970     if (status == MagickFalse)
971       continue;
972     pixels=GetCacheViewAuthenticPixels(destination->view,destination->extent.x,
973       y,destination->extent.width,1,destination->exception);
974     if (pixels == (Quantum *) NULL)
975       {
976         status=MagickFalse;
977         continue;
978       }
979     if (set(destination,y,id,context) == MagickFalse)
980       status=MagickFalse;
981     for (x=0; x < (ssize_t) destination->extent.width; x++)
982     {
983       PixelGetQuantumPixel(destination->image,destination->pixel_wands[id][x],
984         pixels);
985       pixels+=GetPixelChannels(destination->image);
986     }
987     sync=SyncCacheViewAuthenticPixels(destination->view,destination->exception);
988     if (sync == MagickFalse)
989       status=MagickFalse;
990     if (destination_image->progress_monitor != (MagickProgressMonitor) NULL)
991       {
992         MagickBooleanType
993           proceed;
994
995 #if defined(MAGICKCORE_OPENMP_SUPPORT)
996         #pragma omp critical (MagickWand_SetWandViewIterator)
997 #endif
998         proceed=SetImageProgress(destination_image,destination->description,
999           progress++,destination->extent.height);
1000         if (proceed == MagickFalse)
1001           status=MagickFalse;
1002       }
1003   }
1004   return(status);
1005 }
1006 \f
1007 /*
1008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1009 %                                                                             %
1010 %                                                                             %
1011 %                                                                             %
1012 %   T r a n s f e r W a n d V i e w I t e r a t o r                           %
1013 %                                                                             %
1014 %                                                                             %
1015 %                                                                             %
1016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1017 %
1018 %  TransferWandViewIterator() iterates over two wand views in parallel and
1019 %  calls your transfer method for each scanline of the view.  The source pixel
1020 %  extent is not confined to the image canvas-- that is you can include
1021 %  negative offsets or widths or heights that exceed the image dimension.
1022 %  However, the destination wand view is confined to the image canvas-- that
1023 %  is no negative offsets or widths or heights that exceed the image dimension
1024 %  are permitted.
1025 %
1026 %  The callback signature is:
1027 %
1028 %      MagickBooleanType TransferImageViewMethod(const WandView *source,
1029 %        WandView *destination,const ssize_t y,const int thread_id,
1030 %        void *context)
1031 %
1032 %  Use this pragma if the view is not single threaded:
1033 %
1034 %    #pragma omp critical
1035 %
1036 %  to define a section of code in your callback transfer method that must be
1037 %  executed by a single thread at a time.
1038 %
1039 %  The format of the TransferWandViewIterator method is:
1040 %
1041 %      MagickBooleanType TransferWandViewIterator(WandView *source,
1042 %        WandView *destination,TransferWandViewMethod transfer,void *context)
1043 %
1044 %  A description of each parameter follows:
1045 %
1046 %    o source: the source wand view.
1047 %
1048 %    o destination: the destination wand view.
1049 %
1050 %    o transfer: the transfer callback method.
1051 %
1052 %    o context: the user defined context.
1053 %
1054 */
1055 WandExport MagickBooleanType TransferWandViewIterator(WandView *source,
1056   WandView *destination,TransferWandViewMethod transfer,void *context)
1057 {
1058   Image
1059     *destination_image,
1060     *source_image;
1061
1062   MagickBooleanType
1063     status;
1064
1065   MagickOffsetType
1066     progress;
1067
1068   size_t
1069     height,
1070     width;
1071
1072   ssize_t
1073     y;
1074
1075   assert(source != (WandView *) NULL);
1076   assert(source->signature == WandSignature);
1077   if (transfer == (TransferWandViewMethod) NULL)
1078     return(MagickFalse);
1079   source_image=source->wand->images;
1080   destination_image=destination->wand->images;
1081   status=SetImageStorageClass(destination_image,DirectClass,
1082     destination->exception);
1083   if (status == MagickFalse)
1084     return(MagickFalse);
1085   status=MagickTrue;
1086   progress=0;
1087   height=source->extent.height-source->extent.y;
1088   width=source->extent.width-source->extent.x;
1089 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1090   #pragma omp parallel for schedule(static) shared(progress,status) \
1091     if ((height*width) > 8192) \
1092       num_threads(GetMagickResourceLimit(ThreadResource))
1093 #endif
1094   for (y=source->extent.y; y < (ssize_t) source->extent.height; y++)
1095   {
1096     const int
1097       id = GetOpenMPThreadId();
1098
1099     MagickBooleanType
1100       sync;
1101
1102     register const Quantum
1103       *restrict pixels;
1104
1105     register ssize_t
1106       x;
1107
1108     register Quantum
1109       *restrict destination_pixels;
1110
1111     if (status == MagickFalse)
1112       continue;
1113     pixels=GetCacheViewVirtualPixels(source->view,source->extent.x,y,
1114       source->extent.width,1,source->exception);
1115     if (pixels == (const Quantum *) NULL)
1116       {
1117         status=MagickFalse;
1118         continue;
1119       }
1120     for (x=0; x < (ssize_t) source->extent.width; x++)
1121     {
1122       PixelSetQuantumPixel(source->image,pixels,source->pixel_wands[id][x]);
1123       pixels+=GetPixelChannels(source->image);
1124     }
1125     destination_pixels=GetCacheViewAuthenticPixels(destination->view,
1126       destination->extent.x,y,destination->extent.width,1,
1127       destination->exception);
1128     if (destination_pixels == (Quantum *) NULL)
1129       {
1130         status=MagickFalse;
1131         continue;
1132       }
1133     for (x=0; x < (ssize_t) destination->extent.width; x++)
1134     {
1135       PixelSetQuantumPixel(destination->image,destination_pixels,
1136         destination->pixel_wands[id][x]);
1137       destination_pixels+=GetPixelChannels(destination->image);
1138     }
1139     if (transfer(source,destination,y,id,context) == MagickFalse)
1140       status=MagickFalse;
1141     destination_pixels=GetCacheViewAuthenticPixels(destination->view,
1142       destination->extent.x,y,destination->extent.width,1,
1143       destination->exception);
1144     for (x=0; x < (ssize_t) destination->extent.width; x++)
1145     {
1146       PixelGetQuantumPixel(destination->image,destination->pixel_wands[id][x],
1147         destination_pixels);
1148       destination_pixels+=GetPixelChannels(destination->image);
1149     }
1150     sync=SyncCacheViewAuthenticPixels(destination->view,destination->exception);
1151     if (sync == MagickFalse)
1152       status=MagickFalse;
1153     if (source_image->progress_monitor != (MagickProgressMonitor) NULL)
1154       {
1155         MagickBooleanType
1156           proceed;
1157
1158 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1159         #pragma omp critical (MagickWand_TransferWandViewIterator)
1160 #endif
1161         proceed=SetImageProgress(source_image,source->description,progress++,
1162           source->extent.height);
1163         if (proceed == MagickFalse)
1164           status=MagickFalse;
1165       }
1166   }
1167   return(status);
1168 }
1169 \f
1170 /*
1171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 %                                                                             %
1173 %                                                                             %
1174 %                                                                             %
1175 %   U p d a t e W a n d V i e w I t e r a t o r                               %
1176 %                                                                             %
1177 %                                                                             %
1178 %                                                                             %
1179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180 %
1181 %  UpdateWandViewIterator() iterates over the wand view in parallel and calls
1182 %  your update method for each scanline of the view.  The pixel extent is
1183 %  confined to the image canvas-- that is no negative offsets or widths or
1184 %  heights that exceed the image dimension are permitted.  Updates to pixels
1185 %  in your callback are automagically synced back to the image.
1186 %
1187 %  The callback signature is:
1188 %
1189 %      MagickBooleanType UpdateImageViewMethod(WandView *source,const ssize_t y,
1190 %        const int thread_id,void *context)
1191 %
1192 %  Use this pragma if the view is not single threaded:
1193 %
1194 %    #pragma omp critical
1195 %
1196 %  to define a section of code in your callback update method that must be
1197 %  executed by a single thread at a time.
1198 %
1199 %  The format of the UpdateWandViewIterator method is:
1200 %
1201 %      MagickBooleanType UpdateWandViewIterator(WandView *source,
1202 %        UpdateWandViewMethod update,void *context)
1203 %
1204 %  A description of each parameter follows:
1205 %
1206 %    o source: the source wand view.
1207 %
1208 %    o update: the update callback method.
1209 %
1210 %    o context: the user defined context.
1211 %
1212 */
1213 WandExport MagickBooleanType UpdateWandViewIterator(WandView *source,
1214   UpdateWandViewMethod update,void *context)
1215 {
1216   Image
1217     *source_image;
1218
1219   MagickBooleanType
1220     status;
1221
1222   MagickOffsetType
1223     progress;
1224
1225   size_t
1226     height,
1227     width;
1228
1229   ssize_t
1230     y;
1231
1232   assert(source != (WandView *) NULL);
1233   assert(source->signature == WandSignature);
1234   if (update == (UpdateWandViewMethod) NULL)
1235     return(MagickFalse);
1236   source_image=source->wand->images;
1237   status=SetImageStorageClass(source_image,DirectClass,source->exception);
1238   if (status == MagickFalse)
1239     return(MagickFalse);
1240   status=MagickTrue;
1241   progress=0;
1242   height=source->extent.height-source->extent.y;
1243   width=source->extent.width-source->extent.x;
1244 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1245   #pragma omp parallel for schedule(static) shared(progress,status) \
1246     if ((height*width) > 8192) \
1247       num_threads(GetMagickResourceLimit(ThreadResource))
1248 #endif
1249   for (y=source->extent.y; y < (ssize_t) source->extent.height; y++)
1250   {
1251     const int
1252       id = GetOpenMPThreadId();
1253
1254     MagickBooleanType
1255       sync;
1256
1257     register ssize_t
1258       x;
1259
1260     register Quantum
1261       *restrict pixels;
1262
1263     if (status == MagickFalse)
1264       continue;
1265     pixels=GetCacheViewAuthenticPixels(source->view,source->extent.x,y,
1266       source->extent.width,1,source->exception);
1267     if (pixels == (Quantum *) NULL)
1268       {
1269         status=MagickFalse;
1270         continue;
1271       }
1272     for (x=0; x < (ssize_t) source->extent.width; x++)
1273     {
1274       PixelSetQuantumPixel(source->image,pixels,source->pixel_wands[id][x]);
1275       pixels+=GetPixelChannels(source->image);
1276     }
1277     if (update(source,y,id,context) == MagickFalse)
1278       status=MagickFalse;
1279     for (x=0; x < (ssize_t) source->extent.width; x++)
1280     {
1281       PixelGetQuantumPixel(source->image,source->pixel_wands[id][x],pixels);
1282       pixels+=GetPixelChannels(source->image);
1283     }
1284     sync=SyncCacheViewAuthenticPixels(source->view,source->exception);
1285     if (sync == MagickFalse)
1286       status=MagickFalse;
1287     if (source_image->progress_monitor != (MagickProgressMonitor) NULL)
1288       {
1289         MagickBooleanType
1290           proceed;
1291
1292 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1293         #pragma omp critical (MagickWand_UpdateWandViewIterator)
1294 #endif
1295         proceed=SetImageProgress(source_image,source->description,progress++,
1296           source->extent.height);
1297         if (proceed == MagickFalse)
1298           status=MagickFalse;
1299       }
1300   }
1301   return(status);
1302 }