2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT RRRR AAA N N SSSSS FFFFF OOO RRRR M M %
7 % T R R A A NN N SS F O O R R MM MM %
8 % T RRRR AAAAA N N N SSS FFF O O RRRR M M M %
9 % T R R A A N NN SS F O O R R M M %
10 % T R R A A N N SSSSS F OOO R R M M %
13 % MagickCore Image Transform Methods %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/cache.h"
45 #include "MagickCore/cache-view.h"
46 #include "MagickCore/color.h"
47 #include "MagickCore/color-private.h"
48 #include "MagickCore/colorspace-private.h"
49 #include "MagickCore/composite.h"
50 #include "MagickCore/distort.h"
51 #include "MagickCore/draw.h"
52 #include "MagickCore/effect.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/geometry.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/layer.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/pixel-accessor.h"
63 #include "MagickCore/resource_.h"
64 #include "MagickCore/resize.h"
65 #include "MagickCore/statistic.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/thread-private.h"
68 #include "MagickCore/transform.h"
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % A u t o O r i e n t I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % AutoOrientImage() adjusts an image so that its orientation is suitable for
82 % viewing (i.e. top-left orientation).
84 % The format of the AutoOrientImage method is:
86 % Image *AutoOrientImage(const Image *image,
87 % const OrientationType orientation,ExceptionInfo *exception)
89 % A description of each parameter follows:
93 % o orientation: Current image orientation.
95 % o exception: Return any errors or warnings in this structure.
98 MagickExport Image *AutoOrientImage(const Image *image,
99 const OrientationType orientation,ExceptionInfo *exception)
104 assert(image != (const Image *) NULL);
105 assert(image->signature == MagickSignature);
106 assert(exception != (ExceptionInfo *) NULL);
107 assert(exception->signature == MagickSignature);
108 orient_image=(Image *) NULL;
111 case UndefinedOrientation:
112 case TopLeftOrientation:
115 orient_image=CloneImage(image,0,0,MagickTrue,exception);
118 case TopRightOrientation:
120 orient_image=FlopImage(image,exception);
123 case BottomRightOrientation:
125 orient_image=RotateImage(image,180.0,exception);
128 case BottomLeftOrientation:
130 orient_image=FlipImage(image,exception);
133 case LeftTopOrientation:
135 orient_image=TransposeImage(image,exception);
138 case RightTopOrientation:
140 orient_image=RotateImage(image,90.0,exception);
143 case RightBottomOrientation:
145 orient_image=TransverseImage(image,exception);
148 case LeftBottomOrientation:
150 orient_image=RotateImage(image,270.0,exception);
154 if (orient_image != (Image *) NULL)
155 orient_image->orientation=TopLeftOrientation;
156 return(orient_image);
160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 % C h o p I m a g e %
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 % ChopImage() removes a region of an image and collapses the image to occupy
171 % the removed portion.
173 % The format of the ChopImage method is:
175 % Image *ChopImage(const Image *image,const RectangleInfo *chop_info)
176 % ExceptionInfo *exception)
178 % A description of each parameter follows:
180 % o image: the image.
182 % o chop_info: Define the region of the image to chop.
184 % o exception: return any errors or warnings in this structure.
187 MagickExport Image *ChopImage(const Image *image,const RectangleInfo *chop_info,
188 ExceptionInfo *exception)
190 #define ChopImageTag "Chop/Image"
214 assert(image != (const Image *) NULL);
215 assert(image->signature == MagickSignature);
216 if (image->debug != MagickFalse)
217 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
218 assert(exception != (ExceptionInfo *) NULL);
219 assert(exception->signature == MagickSignature);
220 assert(chop_info != (RectangleInfo *) NULL);
221 if (((chop_info->x+(ssize_t) chop_info->width) < 0) ||
222 ((chop_info->y+(ssize_t) chop_info->height) < 0) ||
223 (chop_info->x > (ssize_t) image->columns) ||
224 (chop_info->y > (ssize_t) image->rows))
225 ThrowImageException(OptionWarning,"GeometryDoesNotContainImage");
227 if ((extent.x+(ssize_t) extent.width) > (ssize_t) image->columns)
228 extent.width=(size_t) ((ssize_t) image->columns-extent.x);
229 if ((extent.y+(ssize_t) extent.height) > (ssize_t) image->rows)
230 extent.height=(size_t) ((ssize_t) image->rows-extent.y);
233 extent.width-=(size_t) (-extent.x);
238 extent.height-=(size_t) (-extent.y);
241 chop_image=CloneImage(image,image->columns-extent.width,image->rows-
242 extent.height,MagickTrue,exception);
243 if (chop_image == (Image *) NULL)
244 return((Image *) NULL);
250 image_view=AcquireVirtualCacheView(image,exception);
251 chop_view=AcquireAuthenticCacheView(chop_image,exception);
252 #if defined(MAGICKCORE_OPENMP_SUPPORT)
253 #pragma omp parallel for schedule(static,4) shared(status) \
254 magick_threads(image,chop_image,1,1)
256 for (y=0; y < (ssize_t) extent.y; y++)
258 register const Quantum
267 if (status == MagickFalse)
269 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
270 q=QueueCacheViewAuthenticPixels(chop_view,0,y,chop_image->columns,1,
272 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
277 for (x=0; x < (ssize_t) image->columns; x++)
279 if ((x < extent.x) || (x >= (ssize_t) (extent.x+extent.width)))
284 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
286 PixelChannel channel=GetPixelChannelChannel(image,i);
287 PixelTrait traits=GetPixelChannelTraits(image,channel);
288 PixelTrait chop_traits=GetPixelChannelTraits(chop_image,channel);
289 if ((traits == UndefinedPixelTrait) ||
290 (chop_traits == UndefinedPixelTrait))
292 SetPixelChannel(chop_image,channel,p[i],q);
294 q+=GetPixelChannels(chop_image);
296 p+=GetPixelChannels(image);
298 if (SyncCacheViewAuthenticPixels(chop_view,exception) == MagickFalse)
300 if (image->progress_monitor != (MagickProgressMonitor) NULL)
305 #if defined(MAGICKCORE_OPENMP_SUPPORT)
306 #pragma omp critical (MagickCore_ChopImage)
308 proceed=SetImageProgress(image,ChopImageTag,progress++,image->rows);
309 if (proceed == MagickFalse)
316 #if defined(MAGICKCORE_OPENMP_SUPPORT)
317 #pragma omp parallel for schedule(static,4) shared(progress,status) \
318 magick_threads(image,chop_image,1,1)
320 for (y=0; y < (ssize_t) (image->rows-(extent.y+extent.height)); y++)
322 register const Quantum
331 if (status == MagickFalse)
333 p=GetCacheViewVirtualPixels(image_view,0,extent.y+extent.height+y,
334 image->columns,1,exception);
335 q=QueueCacheViewAuthenticPixels(chop_view,0,extent.y+y,chop_image->columns,
337 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
342 for (x=0; x < (ssize_t) image->columns; x++)
344 if ((x < extent.x) || (x >= (ssize_t) (extent.x+extent.width)))
349 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
351 PixelChannel channel=GetPixelChannelChannel(image,i);
352 PixelTrait traits=GetPixelChannelTraits(image,channel);
353 PixelTrait chop_traits=GetPixelChannelTraits(chop_image,channel);
354 if ((traits == UndefinedPixelTrait) ||
355 (chop_traits == UndefinedPixelTrait))
357 SetPixelChannel(chop_image,channel,p[i],q);
359 q+=GetPixelChannels(chop_image);
361 p+=GetPixelChannels(image);
363 if (SyncCacheViewAuthenticPixels(chop_view,exception) == MagickFalse)
365 if (image->progress_monitor != (MagickProgressMonitor) NULL)
370 #if defined(MAGICKCORE_OPENMP_SUPPORT)
371 #pragma omp critical (MagickCore_ChopImage)
373 proceed=SetImageProgress(image,ChopImageTag,progress++,image->rows);
374 if (proceed == MagickFalse)
378 chop_view=DestroyCacheView(chop_view);
379 image_view=DestroyCacheView(image_view);
380 chop_image->type=image->type;
381 if (status == MagickFalse)
382 chop_image=DestroyImage(chop_image);
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 + C o n s o l i d a t e C M Y K I m a g e %
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 % ConsolidateCMYKImage() consolidates separate C, M, Y, and K planes into a
400 % The format of the ConsolidateCMYKImage method is:
402 % Image *ConsolidateCMYKImage(const Image *image,ExceptionInfo *exception)
404 % A description of each parameter follows:
406 % o image: the image sequence.
408 % o exception: return any errors or warnings in this structure.
411 MagickExport Image *ConsolidateCMYKImages(const Image *images,
412 ExceptionInfo *exception)
429 Consolidate separate C, M, Y, and K planes into a single image.
431 assert(images != (Image *) NULL);
432 assert(images->signature == MagickSignature);
433 if (images->debug != MagickFalse)
434 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
435 assert(exception != (ExceptionInfo *) NULL);
436 assert(exception->signature == MagickSignature);
437 cmyk_images=NewImageList();
438 for (j=0; j < (ssize_t) GetImageListLength(images); j+=4)
443 assert(images != (Image *) NULL);
444 cmyk_image=CloneImage(images,images->columns,images->rows,MagickTrue,
446 if (cmyk_image == (Image *) NULL)
448 if (SetImageStorageClass(cmyk_image,DirectClass,exception) == MagickFalse)
450 (void) SetImageColorspace(cmyk_image,CMYKColorspace,exception);
451 for (i=0; i < 4; i++)
453 image_view=AcquireVirtualCacheView(images,exception);
454 cmyk_view=AcquireAuthenticCacheView(cmyk_image,exception);
455 for (y=0; y < (ssize_t) images->rows; y++)
457 register const Quantum
466 p=GetCacheViewVirtualPixels(image_view,0,y,images->columns,1,exception);
467 q=QueueCacheViewAuthenticPixels(cmyk_view,0,y,cmyk_image->columns,1,
469 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
471 for (x=0; x < (ssize_t) images->columns; x++)
476 pixel=QuantumRange-GetPixelIntensity(images,p);
479 case 0: SetPixelCyan(cmyk_image,pixel,q); break;
480 case 1: SetPixelMagenta(cmyk_image,pixel,q); break;
481 case 2: SetPixelYellow(cmyk_image,pixel,q); break;
482 case 3: SetPixelBlack(cmyk_image,pixel,q); break;
485 p+=GetPixelChannels(images);
486 q+=GetPixelChannels(cmyk_image);
488 if (SyncCacheViewAuthenticPixels(cmyk_view,exception) == MagickFalse)
491 cmyk_view=DestroyCacheView(cmyk_view);
492 image_view=DestroyCacheView(image_view);
493 images=GetNextImageInList(images);
494 if (images == (Image *) NULL)
497 AppendImageToList(&cmyk_images,cmyk_image);
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
507 % C r o p I m a g e %
511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 % CropImage() extracts a region of the image starting at the offset defined
514 % by geometry. Region must be fully defined, and no special handling of
515 % geometry flags is performed.
517 % The format of the CropImage method is:
519 % Image *CropImage(const Image *image,const RectangleInfo *geometry,
520 % ExceptionInfo *exception)
522 % A description of each parameter follows:
524 % o image: the image.
526 % o geometry: Define the region of the image to crop with members
527 % x, y, width, and height.
529 % o exception: return any errors or warnings in this structure.
532 MagickExport Image *CropImage(const Image *image,const RectangleInfo *geometry,
533 ExceptionInfo *exception)
535 #define CropImageTag "Crop/Image"
563 assert(image != (const Image *) NULL);
564 assert(image->signature == MagickSignature);
565 if (image->debug != MagickFalse)
566 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
567 assert(geometry != (const RectangleInfo *) NULL);
568 assert(exception != (ExceptionInfo *) NULL);
569 assert(exception->signature == MagickSignature);
570 bounding_box=image->page;
571 if ((bounding_box.width == 0) || (bounding_box.height == 0))
573 bounding_box.width=image->columns;
574 bounding_box.height=image->rows;
578 page.width=bounding_box.width;
579 if (page.height == 0)
580 page.height=bounding_box.height;
581 if (((bounding_box.x-page.x) >= (ssize_t) page.width) ||
582 ((bounding_box.y-page.y) >= (ssize_t) page.height) ||
583 ((page.x-bounding_box.x) > (ssize_t) image->columns) ||
584 ((page.y-bounding_box.y) > (ssize_t) image->rows))
587 Crop is not within virtual canvas, return 1 pixel transparent image.
589 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
590 "GeometryDoesNotContainImage","`%s'",image->filename);
591 crop_image=CloneImage(image,1,1,MagickTrue,exception);
592 if (crop_image == (Image *) NULL)
593 return((Image *) NULL);
594 crop_image->background_color.alpha=(Quantum) TransparentAlpha;
595 crop_image->alpha_trait=BlendPixelTrait;
596 (void) SetImageBackgroundColor(crop_image,exception);
597 crop_image->page=bounding_box;
598 crop_image->page.x=(-1);
599 crop_image->page.y=(-1);
600 if (crop_image->dispose == BackgroundDispose)
601 crop_image->dispose=NoneDispose;
604 if ((page.x < 0) && (bounding_box.x >= 0))
606 page.width+=page.x-bounding_box.x;
611 page.width-=bounding_box.x-page.x;
612 page.x-=bounding_box.x;
616 if ((page.y < 0) && (bounding_box.y >= 0))
618 page.height+=page.y-bounding_box.y;
623 page.height-=bounding_box.y-page.y;
624 page.y-=bounding_box.y;
628 if ((page.x+(ssize_t) page.width) > (ssize_t) image->columns)
629 page.width=image->columns-page.x;
630 if ((geometry->width != 0) && (page.width > geometry->width))
631 page.width=geometry->width;
632 if ((page.y+(ssize_t) page.height) > (ssize_t) image->rows)
633 page.height=image->rows-page.y;
634 if ((geometry->height != 0) && (page.height > geometry->height))
635 page.height=geometry->height;
636 bounding_box.x+=page.x;
637 bounding_box.y+=page.y;
638 if ((page.width == 0) || (page.height == 0))
640 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
641 "GeometryDoesNotContainImage","`%s'",image->filename);
642 return((Image *) NULL);
645 Initialize crop image attributes.
647 crop_image=CloneImage(image,page.width,page.height,MagickTrue,exception);
648 if (crop_image == (Image *) NULL)
649 return((Image *) NULL);
650 crop_image->page.width=image->page.width;
651 crop_image->page.height=image->page.height;
652 offset.x=(ssize_t) (bounding_box.x+bounding_box.width);
653 offset.y=(ssize_t) (bounding_box.y+bounding_box.height);
654 if ((offset.x > (ssize_t) image->page.width) ||
655 (offset.y > (ssize_t) image->page.height))
657 crop_image->page.width=bounding_box.width;
658 crop_image->page.height=bounding_box.height;
660 crop_image->page.x=bounding_box.x;
661 crop_image->page.y=bounding_box.y;
667 image_view=AcquireVirtualCacheView(image,exception);
668 crop_view=AcquireAuthenticCacheView(crop_image,exception);
669 #if defined(MAGICKCORE_OPENMP_SUPPORT)
670 #pragma omp parallel for schedule(static,4) shared(status) \
671 magick_threads(image,crop_image,1,1)
673 for (y=0; y < (ssize_t) crop_image->rows; y++)
675 register const Quantum
684 if (status == MagickFalse)
686 p=GetCacheViewVirtualPixels(image_view,page.x,page.y+y,crop_image->columns,
688 q=QueueCacheViewAuthenticPixels(crop_view,0,y,crop_image->columns,1,
690 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
695 for (x=0; x < (ssize_t) crop_image->columns; x++)
700 if (GetPixelReadMask(image,p) == 0)
702 SetPixelBackgoundColor(crop_image,q);
703 p+=GetPixelChannels(image);
704 q+=GetPixelChannels(crop_image);
707 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
709 PixelChannel channel=GetPixelChannelChannel(image,i);
710 PixelTrait traits=GetPixelChannelTraits(image,channel);
711 PixelTrait crop_traits=GetPixelChannelTraits(crop_image,channel);
712 if ((traits == UndefinedPixelTrait) ||
713 (crop_traits == UndefinedPixelTrait))
715 SetPixelChannel(crop_image,channel,p[i],q);
717 p+=GetPixelChannels(image);
718 q+=GetPixelChannels(crop_image);
720 if (SyncCacheViewAuthenticPixels(crop_view,exception) == MagickFalse)
722 if (image->progress_monitor != (MagickProgressMonitor) NULL)
727 #if defined(MAGICKCORE_OPENMP_SUPPORT)
728 #pragma omp critical (MagickCore_CropImage)
730 proceed=SetImageProgress(image,CropImageTag,progress++,image->rows);
731 if (proceed == MagickFalse)
735 crop_view=DestroyCacheView(crop_view);
736 image_view=DestroyCacheView(image_view);
737 crop_image->type=image->type;
738 if (status == MagickFalse)
739 crop_image=DestroyImage(crop_image);
744 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
748 % C r o p I m a g e T o T i l e s %
752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 % CropImageToTiles() crops a single image, into a possible list of tiles.
755 % This may include a single sub-region of the image. This basically applies
756 % all the normal geometry flags for Crop.
758 % Image *CropImageToTiles(const Image *image,
759 % const RectangleInfo *crop_geometry, ExceptionInfo *exception)
761 % A description of each parameter follows:
763 % o image: the image The transformed image is returned as this parameter.
765 % o crop_geometry: A crop geometry string.
767 % o exception: return any errors or warnings in this structure.
771 static inline double MagickRound(double x)
774 Round the fraction to nearest integer.
776 if ((x-floor(x)) < (ceil(x)-x))
781 MagickExport Image *CropImageToTiles(const Image *image,
782 const char *crop_geometry,ExceptionInfo *exception)
794 assert(image != (Image *) NULL);
795 assert(image->signature == MagickSignature);
796 if (image->debug != MagickFalse)
797 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
798 crop_image=NewImageList();
800 flags=ParseGravityGeometry(image,crop_geometry,&geometry,exception);
801 if ((flags & AreaValue) != 0)
815 Crop into NxM tiles (@ flag).
817 width=image->columns;
819 if (geometry.width == 0)
821 if (geometry.height == 0)
823 if ((flags & AspectValue) == 0)
825 width-=(geometry.x < 0 ? -1 : 1)*geometry.x;
826 height-=(geometry.y < 0 ? -1 : 1)*geometry.y;
830 width+=(geometry.x < 0 ? -1 : 1)*geometry.x;
831 height+=(geometry.y < 0 ? -1 : 1)*geometry.y;
833 delta.x=(double) width/geometry.width;
834 delta.y=(double) height/geometry.height;
839 for (offset.y=0; offset.y < (double) height; )
841 if ((flags & AspectValue) == 0)
843 crop.y=(ssize_t) MagickRound((double) (offset.y-
844 (geometry.y > 0 ? 0 : geometry.y)));
845 offset.y+=delta.y; /* increment now to find width */
846 crop.height=(size_t) MagickRound((double) (offset.y+
847 (geometry.y < 0 ? 0 : geometry.y)));
851 crop.y=(ssize_t) MagickRound((double) (offset.y-
852 (geometry.y > 0 ? geometry.y : 0)));
853 offset.y+=delta.y; /* increment now to find width */
854 crop.height=(size_t) MagickRound((double)
855 (offset.y+(geometry.y < -1 ? geometry.y : 0)));
858 crop.y+=image->page.y;
859 for (offset.x=0; offset.x < (double) width; )
861 if ((flags & AspectValue) == 0)
863 crop.x=(ssize_t) MagickRound((double) (offset.x-
864 (geometry.x > 0 ? 0 : geometry.x)));
865 offset.x+=delta.x; /* increment now to find height */
866 crop.width=(size_t) MagickRound((double) (offset.x+
867 (geometry.x < 0 ? 0 : geometry.x)));
871 crop.x=(ssize_t) MagickRound((double) (offset.x-
872 (geometry.x > 0 ? geometry.x : 0)));
873 offset.x+=delta.x; /* increment now to find height */
874 crop.width=(size_t) MagickRound((double) (offset.x+
875 (geometry.x < 0 ? geometry.x : 0)));
878 crop.x+=image->page.x;
879 next=CropImage(image,&crop,exception);
880 if (next == (Image *) NULL)
882 AppendImageToList(&crop_image,next);
884 if (next == (Image *) NULL)
887 ClearMagickException(exception);
890 if (((geometry.width == 0) && (geometry.height == 0)) ||
891 ((flags & XValue) != 0) || ((flags & YValue) != 0))
894 Crop a single region at +X+Y.
896 crop_image=CropImage(image,&geometry,exception);
897 if ((crop_image != (Image *) NULL) && ((flags & AspectValue) != 0))
899 crop_image->page.width=geometry.width;
900 crop_image->page.height=geometry.height;
901 crop_image->page.x-=geometry.x;
902 crop_image->page.y-=geometry.y;
906 if ((image->columns > geometry.width) || (image->rows > geometry.height))
920 Crop into tiles of fixed size WxH.
924 page.width=image->columns;
925 if (page.height == 0)
926 page.height=image->rows;
927 width=geometry.width;
930 height=geometry.height;
934 for (y=0; y < (ssize_t) page.height; y+=(ssize_t) height)
936 for (x=0; x < (ssize_t) page.width; x+=(ssize_t) width)
938 geometry.width=width;
939 geometry.height=height;
942 next=CropImage(image,&geometry,exception);
943 if (next == (Image *) NULL)
945 AppendImageToList(&crop_image,next);
947 if (next == (Image *) NULL)
952 return(CloneImage(image,0,0,MagickTrue,exception));
956 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
960 % E x c e r p t I m a g e %
964 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966 % ExcerptImage() returns a excerpt of the image as defined by the geometry.
968 % The format of the ExcerptImage method is:
970 % Image *ExcerptImage(const Image *image,const RectangleInfo *geometry,
971 % ExceptionInfo *exception)
973 % A description of each parameter follows:
975 % o image: the image.
977 % o geometry: Define the region of the image to extend with members
978 % x, y, width, and height.
980 % o exception: return any errors or warnings in this structure.
983 MagickExport Image *ExcerptImage(const Image *image,
984 const RectangleInfo *geometry,ExceptionInfo *exception)
986 #define ExcerptImageTag "Excerpt/Image"
1005 Allocate excerpt image.
1007 assert(image != (const Image *) NULL);
1008 assert(image->signature == MagickSignature);
1009 if (image->debug != MagickFalse)
1010 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1011 assert(geometry != (const RectangleInfo *) NULL);
1012 assert(exception != (ExceptionInfo *) NULL);
1013 assert(exception->signature == MagickSignature);
1014 excerpt_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
1016 if (excerpt_image == (Image *) NULL)
1017 return((Image *) NULL);
1023 image_view=AcquireVirtualCacheView(image,exception);
1024 excerpt_view=AcquireAuthenticCacheView(excerpt_image,exception);
1025 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1026 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1027 magick_threads(image,excerpt_image,excerpt_image->rows,1)
1029 for (y=0; y < (ssize_t) excerpt_image->rows; y++)
1031 register const Quantum
1040 if (status == MagickFalse)
1042 p=GetCacheViewVirtualPixels(image_view,geometry->x,geometry->y+y,
1043 geometry->width,1,exception);
1044 q=GetCacheViewAuthenticPixels(excerpt_view,0,y,excerpt_image->columns,1,
1046 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1051 for (x=0; x < (ssize_t) excerpt_image->columns; x++)
1056 if (GetPixelReadMask(image,p) == 0)
1058 SetPixelBackgoundColor(excerpt_image,q);
1059 p+=GetPixelChannels(image);
1060 q+=GetPixelChannels(excerpt_image);
1063 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1065 PixelChannel channel=GetPixelChannelChannel(image,i);
1066 PixelTrait traits=GetPixelChannelTraits(image,channel);
1067 PixelTrait excerpt_traits=GetPixelChannelTraits(excerpt_image,channel);
1068 if ((traits == UndefinedPixelTrait) ||
1069 (excerpt_traits == UndefinedPixelTrait))
1071 SetPixelChannel(excerpt_image,channel,p[i],q);
1073 p+=GetPixelChannels(image);
1074 q+=GetPixelChannels(excerpt_image);
1076 if (SyncCacheViewAuthenticPixels(excerpt_view,exception) == MagickFalse)
1078 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1083 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1084 #pragma omp critical (MagickCore_ExcerptImage)
1086 proceed=SetImageProgress(image,ExcerptImageTag,progress++,image->rows);
1087 if (proceed == MagickFalse)
1091 excerpt_view=DestroyCacheView(excerpt_view);
1092 image_view=DestroyCacheView(image_view);
1093 excerpt_image->type=image->type;
1094 if (status == MagickFalse)
1095 excerpt_image=DestroyImage(excerpt_image);
1096 return(excerpt_image);
1100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1104 % E x t e n t I m a g e %
1108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1110 % ExtentImage() extends the image as defined by the geometry, gravity, and
1111 % image background color. Set the (x,y) offset of the geometry to move the
1112 % original image relative to the extended image.
1114 % The format of the ExtentImage method is:
1116 % Image *ExtentImage(const Image *image,const RectangleInfo *geometry,
1117 % ExceptionInfo *exception)
1119 % A description of each parameter follows:
1121 % o image: the image.
1123 % o geometry: Define the region of the image to extend with members
1124 % x, y, width, and height.
1126 % o exception: return any errors or warnings in this structure.
1129 MagickExport Image *ExtentImage(const Image *image,
1130 const RectangleInfo *geometry,ExceptionInfo *exception)
1136 Allocate extent image.
1138 assert(image != (const Image *) NULL);
1139 assert(image->signature == MagickSignature);
1140 if (image->debug != MagickFalse)
1141 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1142 assert(geometry != (const RectangleInfo *) NULL);
1143 assert(exception != (ExceptionInfo *) NULL);
1144 assert(exception->signature == MagickSignature);
1145 extent_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
1147 if (extent_image == (Image *) NULL)
1148 return((Image *) NULL);
1149 (void) SetImageBackgroundColor(extent_image,exception);
1150 (void) CompositeImage(extent_image,image,image->compose,MagickTrue,
1151 -geometry->x,-geometry->y,exception);
1152 return(extent_image);
1156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1160 % F l i p I m a g e %
1164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1166 % FlipImage() creates a vertical mirror image by reflecting the pixels
1167 % around the central x-axis.
1169 % The format of the FlipImage method is:
1171 % Image *FlipImage(const Image *image,ExceptionInfo *exception)
1173 % A description of each parameter follows:
1175 % o image: the image.
1177 % o exception: return any errors or warnings in this structure.
1180 MagickExport Image *FlipImage(const Image *image,ExceptionInfo *exception)
1182 #define FlipImageTag "Flip/Image"
1203 assert(image != (const Image *) NULL);
1204 assert(image->signature == MagickSignature);
1205 if (image->debug != MagickFalse)
1206 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1207 assert(exception != (ExceptionInfo *) NULL);
1208 assert(exception->signature == MagickSignature);
1209 flip_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1210 if (flip_image == (Image *) NULL)
1211 return((Image *) NULL);
1218 image_view=AcquireVirtualCacheView(image,exception);
1219 flip_view=AcquireAuthenticCacheView(flip_image,exception);
1220 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1221 #pragma omp parallel for schedule(static,4) shared(status) \
1222 magick_threads(image,flip_image,1,1)
1224 for (y=0; y < (ssize_t) flip_image->rows; y++)
1226 register const Quantum
1235 if (status == MagickFalse)
1237 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1238 q=QueueCacheViewAuthenticPixels(flip_view,0,(ssize_t) (flip_image->rows-y-
1239 1),flip_image->columns,1,exception);
1240 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1245 for (x=0; x < (ssize_t) flip_image->columns; x++)
1250 if (GetPixelReadMask(image,p) == 0)
1252 SetPixelBackgoundColor(flip_image,q);
1253 p+=GetPixelChannels(image);
1254 q+=GetPixelChannels(flip_image);
1257 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1259 PixelChannel channel=GetPixelChannelChannel(image,i);
1260 PixelTrait traits=GetPixelChannelTraits(image,channel);
1261 PixelTrait flip_traits=GetPixelChannelTraits(flip_image,channel);
1262 if ((traits == UndefinedPixelTrait) ||
1263 (flip_traits == UndefinedPixelTrait))
1265 SetPixelChannel(flip_image,channel,p[i],q);
1267 p+=GetPixelChannels(image);
1268 q+=GetPixelChannels(flip_image);
1270 if (SyncCacheViewAuthenticPixels(flip_view,exception) == MagickFalse)
1272 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1277 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1278 #pragma omp critical (MagickCore_FlipImage)
1280 proceed=SetImageProgress(image,FlipImageTag,progress++,image->rows);
1281 if (proceed == MagickFalse)
1285 flip_view=DestroyCacheView(flip_view);
1286 image_view=DestroyCacheView(image_view);
1287 flip_image->type=image->type;
1288 if (page.height != 0)
1289 page.y=(ssize_t) (page.height-flip_image->rows-page.y);
1290 flip_image->page=page;
1291 if (status == MagickFalse)
1292 flip_image=DestroyImage(flip_image);
1297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301 % F l o p I m a g e %
1305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1307 % FlopImage() creates a horizontal mirror image by reflecting the pixels
1308 % around the central y-axis.
1310 % The format of the FlopImage method is:
1312 % Image *FlopImage(const Image *image,ExceptionInfo *exception)
1314 % A description of each parameter follows:
1316 % o image: the image.
1318 % o exception: return any errors or warnings in this structure.
1321 MagickExport Image *FlopImage(const Image *image,ExceptionInfo *exception)
1323 #define FlopImageTag "Flop/Image"
1344 assert(image != (const Image *) NULL);
1345 assert(image->signature == MagickSignature);
1346 if (image->debug != MagickFalse)
1347 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1348 assert(exception != (ExceptionInfo *) NULL);
1349 assert(exception->signature == MagickSignature);
1350 flop_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1351 if (flop_image == (Image *) NULL)
1352 return((Image *) NULL);
1359 image_view=AcquireVirtualCacheView(image,exception);
1360 flop_view=AcquireAuthenticCacheView(flop_image,exception);
1361 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1362 #pragma omp parallel for schedule(static,4) shared(status) \
1363 magick_threads(image,flop_image,1,1)
1365 for (y=0; y < (ssize_t) flop_image->rows; y++)
1367 register const Quantum
1376 if (status == MagickFalse)
1378 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1379 q=QueueCacheViewAuthenticPixels(flop_view,0,y,flop_image->columns,1,
1381 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1386 q+=GetPixelChannels(flop_image)*flop_image->columns;
1387 for (x=0; x < (ssize_t) flop_image->columns; x++)
1392 q-=GetPixelChannels(flop_image);
1393 if (GetPixelReadMask(image,p) == 0)
1395 p+=GetPixelChannels(image);
1398 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1400 PixelChannel channel=GetPixelChannelChannel(image,i);
1401 PixelTrait traits=GetPixelChannelTraits(image,channel);
1402 PixelTrait flop_traits=GetPixelChannelTraits(flop_image,channel);
1403 if ((traits == UndefinedPixelTrait) ||
1404 (flop_traits == UndefinedPixelTrait))
1406 SetPixelChannel(flop_image,channel,p[i],q);
1408 p+=GetPixelChannels(image);
1410 if (SyncCacheViewAuthenticPixels(flop_view,exception) == MagickFalse)
1412 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1417 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1418 #pragma omp critical (MagickCore_FlopImage)
1420 proceed=SetImageProgress(image,FlopImageTag,progress++,image->rows);
1421 if (proceed == MagickFalse)
1425 flop_view=DestroyCacheView(flop_view);
1426 image_view=DestroyCacheView(image_view);
1427 flop_image->type=image->type;
1428 if (page.width != 0)
1429 page.x=(ssize_t) (page.width-flop_image->columns-page.x);
1430 flop_image->page=page;
1431 if (status == MagickFalse)
1432 flop_image=DestroyImage(flop_image);
1437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1441 % R o l l I m a g e %
1445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 % RollImage() offsets an image as defined by x_offset and y_offset.
1449 % The format of the RollImage method is:
1451 % Image *RollImage(const Image *image,const ssize_t x_offset,
1452 % const ssize_t y_offset,ExceptionInfo *exception)
1454 % A description of each parameter follows:
1456 % o image: the image.
1458 % o x_offset: the number of columns to roll in the horizontal direction.
1460 % o y_offset: the number of rows to roll in the vertical direction.
1462 % o exception: return any errors or warnings in this structure.
1466 static inline MagickBooleanType CopyImageRegion(Image *destination,
1467 const Image *source,const size_t columns,const size_t rows,
1468 const ssize_t sx,const ssize_t sy,const ssize_t dx,const ssize_t dy,
1469 ExceptionInfo *exception)
1484 source_view=AcquireVirtualCacheView(source,exception);
1485 destination_view=AcquireAuthenticCacheView(destination,exception);
1486 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1487 #pragma omp parallel for schedule(static,4) shared(status) \
1488 magick_threads(source,destination,rows,1)
1490 for (y=0; y < (ssize_t) rows; y++)
1495 register const Quantum
1507 if (status == MagickFalse)
1509 p=GetCacheViewVirtualPixels(source_view,sx,sy+y,columns,1,exception);
1510 q=GetCacheViewAuthenticPixels(destination_view,dx,dy+y,columns,1,exception);
1511 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1516 for (x=0; x < (ssize_t) columns; x++)
1521 if (GetPixelReadMask(source,p) == 0)
1523 SetPixelBackgoundColor(destination,q);
1524 p+=GetPixelChannels(source);
1525 q+=GetPixelChannels(destination);
1528 for (i=0; i < (ssize_t) GetPixelChannels(source); i++)
1530 PixelChannel channel=GetPixelChannelChannel(source,i);
1531 PixelTrait source_traits=GetPixelChannelTraits(source,channel);
1532 PixelTrait destination_traits=GetPixelChannelTraits(destination,
1534 if ((source_traits == UndefinedPixelTrait) ||
1535 (destination_traits == UndefinedPixelTrait))
1537 SetPixelChannel(destination,channel,p[i],q);
1539 p+=GetPixelChannels(source);
1540 q+=GetPixelChannels(destination);
1542 sync=SyncCacheViewAuthenticPixels(destination_view,exception);
1543 if (sync == MagickFalse)
1546 destination_view=DestroyCacheView(destination_view);
1547 source_view=DestroyCacheView(source_view);
1551 MagickExport Image *RollImage(const Image *image,const ssize_t x_offset,
1552 const ssize_t y_offset,ExceptionInfo *exception)
1554 #define RollImageTag "Roll/Image"
1566 Initialize roll image attributes.
1568 assert(image != (const Image *) NULL);
1569 assert(image->signature == MagickSignature);
1570 if (image->debug != MagickFalse)
1571 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1572 assert(exception != (ExceptionInfo *) NULL);
1573 assert(exception->signature == MagickSignature);
1574 roll_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1575 if (roll_image == (Image *) NULL)
1576 return((Image *) NULL);
1579 while (offset.x < 0)
1580 offset.x+=(ssize_t) image->columns;
1581 while (offset.x >= (ssize_t) image->columns)
1582 offset.x-=(ssize_t) image->columns;
1583 while (offset.y < 0)
1584 offset.y+=(ssize_t) image->rows;
1585 while (offset.y >= (ssize_t) image->rows)
1586 offset.y-=(ssize_t) image->rows;
1590 status=CopyImageRegion(roll_image,image,(size_t) offset.x,
1591 (size_t) offset.y,(ssize_t) image->columns-offset.x,(ssize_t) image->rows-
1592 offset.y,0,0,exception);
1593 (void) SetImageProgress(image,RollImageTag,0,3);
1594 status&=CopyImageRegion(roll_image,image,image->columns-offset.x,
1595 (size_t) offset.y,0,(ssize_t) image->rows-offset.y,offset.x,0,
1597 (void) SetImageProgress(image,RollImageTag,1,3);
1598 status&=CopyImageRegion(roll_image,image,(size_t) offset.x,image->rows-
1599 offset.y,(ssize_t) image->columns-offset.x,0,0,offset.y,exception);
1600 (void) SetImageProgress(image,RollImageTag,2,3);
1601 status&=CopyImageRegion(roll_image,image,image->columns-offset.x,image->rows-
1602 offset.y,0,0,offset.x,offset.y,exception);
1603 (void) SetImageProgress(image,RollImageTag,3,3);
1604 roll_image->type=image->type;
1605 if (status == MagickFalse)
1606 roll_image=DestroyImage(roll_image);
1611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1615 % S h a v e I m a g e %
1619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 % ShaveImage() shaves pixels from the image edges. It allocates the memory
1622 % necessary for the new Image structure and returns a pointer to the new
1625 % The format of the ShaveImage method is:
1627 % Image *ShaveImage(const Image *image,const RectangleInfo *shave_info,
1628 % ExceptionInfo *exception)
1630 % A description of each parameter follows:
1632 % o shave_image: Method ShaveImage returns a pointer to the shaved
1633 % image. A null image is returned if there is a memory shortage or
1634 % if the image width or height is zero.
1636 % o image: the image.
1638 % o shave_info: Specifies a pointer to a RectangleInfo which defines the
1639 % region of the image to crop.
1641 % o exception: return any errors or warnings in this structure.
1644 MagickExport Image *ShaveImage(const Image *image,
1645 const RectangleInfo *shave_info,ExceptionInfo *exception)
1653 assert(image != (const Image *) NULL);
1654 assert(image->signature == MagickSignature);
1655 if (image->debug != MagickFalse)
1656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1657 if (((2*shave_info->width) >= image->columns) ||
1658 ((2*shave_info->height) >= image->rows))
1659 ThrowImageException(OptionWarning,"GeometryDoesNotContainImage");
1660 SetGeometry(image,&geometry);
1661 geometry.width-=2*shave_info->width;
1662 geometry.height-=2*shave_info->height;
1663 geometry.x=(ssize_t) shave_info->width+image->page.x;
1664 geometry.y=(ssize_t) shave_info->height+image->page.y;
1665 shave_image=CropImage(image,&geometry,exception);
1666 if (shave_image == (Image *) NULL)
1667 return((Image *) NULL);
1668 shave_image->page.width-=2*shave_info->width;
1669 shave_image->page.height-=2*shave_info->height;
1670 shave_image->page.x-=(ssize_t) shave_info->width;
1671 shave_image->page.y-=(ssize_t) shave_info->height;
1672 return(shave_image);
1676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1680 % S p l i c e I m a g e %
1684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1686 % SpliceImage() splices a solid color into the image as defined by the
1689 % The format of the SpliceImage method is:
1691 % Image *SpliceImage(const Image *image,const RectangleInfo *geometry,
1692 % ExceptionInfo *exception)
1694 % A description of each parameter follows:
1696 % o image: the image.
1698 % o geometry: Define the region of the image to splice with members
1699 % x, y, width, and height.
1701 % o exception: return any errors or warnings in this structure.
1704 MagickExport Image *SpliceImage(const Image *image,
1705 const RectangleInfo *geometry,ExceptionInfo *exception)
1707 #define SpliceImageTag "Splice/Image"
1729 Allocate splice image.
1731 assert(image != (const Image *) NULL);
1732 assert(image->signature == MagickSignature);
1733 if (image->debug != MagickFalse)
1734 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1735 assert(geometry != (const RectangleInfo *) NULL);
1736 assert(exception != (ExceptionInfo *) NULL);
1737 assert(exception->signature == MagickSignature);
1738 splice_geometry=(*geometry);
1739 splice_image=CloneImage(image,image->columns+splice_geometry.width,
1740 image->rows+splice_geometry.height,MagickTrue,exception);
1741 if (splice_image == (Image *) NULL)
1742 return((Image *) NULL);
1743 if (SetImageStorageClass(splice_image,DirectClass,exception) == MagickFalse)
1745 splice_image=DestroyImage(splice_image);
1746 return((Image *) NULL);
1748 if ((IsPixelInfoGray(&splice_image->background_color) == MagickFalse) &&
1749 (IsGrayColorspace(splice_image->colorspace) != MagickFalse))
1750 (void) SetImageColorspace(splice_image,sRGBColorspace,exception);
1751 if ((splice_image->background_color.alpha_trait == BlendPixelTrait) &&
1752 (splice_image->alpha_trait != BlendPixelTrait))
1753 (void) SetImageAlpha(splice_image,OpaqueAlpha,exception);
1754 (void) SetImageBackgroundColor(splice_image,exception);
1756 Respect image geometry.
1758 switch (image->gravity)
1761 case UndefinedGravity:
1762 case NorthWestGravity:
1766 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1769 case NorthEastGravity:
1771 splice_geometry.x+=(ssize_t) splice_geometry.width;
1776 splice_geometry.y+=(ssize_t) splice_geometry.width/2;
1781 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1782 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1787 splice_geometry.x+=(ssize_t) splice_geometry.width;
1788 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1791 case SouthWestGravity:
1793 splice_geometry.y+=(ssize_t) splice_geometry.height;
1798 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1799 splice_geometry.y+=(ssize_t) splice_geometry.height;
1802 case SouthEastGravity:
1804 splice_geometry.x+=(ssize_t) splice_geometry.width;
1805 splice_geometry.y+=(ssize_t) splice_geometry.height;
1814 image_view=AcquireVirtualCacheView(image,exception);
1815 splice_view=AcquireAuthenticCacheView(splice_image,exception);
1816 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1817 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1818 magick_threads(image,splice_image,1,1)
1820 for (y=0; y < (ssize_t) splice_geometry.y; y++)
1822 register const Quantum
1831 if (status == MagickFalse)
1833 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1834 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1836 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1841 for (x=0; x < splice_geometry.x; x++)
1846 if (GetPixelReadMask(image,p) == 0)
1848 SetPixelBackgoundColor(splice_image,q);
1849 p+=GetPixelChannels(image);
1850 q+=GetPixelChannels(splice_image);
1853 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1855 PixelChannel channel=GetPixelChannelChannel(image,i);
1856 PixelTrait traits=GetPixelChannelTraits(image,channel);
1857 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
1858 if ((traits == UndefinedPixelTrait) ||
1859 (splice_traits == UndefinedPixelTrait))
1861 SetPixelChannel(splice_image,channel,p[i],q);
1863 SetPixelRed(splice_image,GetPixelRed(image,p),q);
1864 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
1865 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
1866 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
1867 p+=GetPixelChannels(image);
1868 q+=GetPixelChannels(splice_image);
1870 for ( ; x < (ssize_t) (splice_geometry.x+splice_geometry.width); x++)
1871 q+=GetPixelChannels(splice_image);
1872 for ( ; x < (ssize_t) splice_image->columns; x++)
1877 if (GetPixelReadMask(image,p) == 0)
1879 SetPixelBackgoundColor(splice_image,q);
1880 p+=GetPixelChannels(image);
1881 q+=GetPixelChannels(splice_image);
1884 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1886 PixelChannel channel=GetPixelChannelChannel(image,i);
1887 PixelTrait traits=GetPixelChannelTraits(image,channel);
1888 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
1889 if ((traits == UndefinedPixelTrait) ||
1890 (splice_traits == UndefinedPixelTrait))
1892 SetPixelChannel(splice_image,channel,p[i],q);
1894 SetPixelRed(splice_image,GetPixelRed(image,p),q);
1895 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
1896 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
1897 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
1898 p+=GetPixelChannels(image);
1899 q+=GetPixelChannels(splice_image);
1901 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
1903 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1908 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1909 #pragma omp critical (MagickCore_TransposeImage)
1911 proceed=SetImageProgress(image,SpliceImageTag,progress++,
1912 splice_image->rows);
1913 if (proceed == MagickFalse)
1917 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1918 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1919 magick_threads(image,splice_image,1,1)
1921 for (y=(ssize_t) (splice_geometry.y+splice_geometry.height);
1922 y < (ssize_t) splice_image->rows; y++)
1924 register const Quantum
1933 if (status == MagickFalse)
1935 p=GetCacheViewVirtualPixels(image_view,0,y-(ssize_t) splice_geometry.height,
1936 image->columns,1,exception);
1937 if ((y < 0) || (y >= (ssize_t) splice_image->rows))
1939 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1941 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1946 for (x=0; x < splice_geometry.x; x++)
1951 if (GetPixelReadMask(image,q) == 0)
1953 SetPixelBackgoundColor(splice_image,q);
1954 p+=GetPixelChannels(image);
1955 q+=GetPixelChannels(splice_image);
1958 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1960 PixelChannel channel=GetPixelChannelChannel(image,i);
1961 PixelTrait traits=GetPixelChannelTraits(image,channel);
1962 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
1963 if ((traits == UndefinedPixelTrait) ||
1964 (splice_traits == UndefinedPixelTrait))
1966 SetPixelChannel(splice_image,channel,p[i],q);
1968 SetPixelRed(splice_image,GetPixelRed(image,p),q);
1969 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
1970 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
1971 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
1972 p+=GetPixelChannels(image);
1973 q+=GetPixelChannels(splice_image);
1975 for ( ; x < (ssize_t) (splice_geometry.x+splice_geometry.width); x++)
1976 q+=GetPixelChannels(splice_image);
1977 for ( ; x < (ssize_t) splice_image->columns; x++)
1982 if (GetPixelReadMask(image,q) == 0)
1984 SetPixelBackgoundColor(splice_image,q);
1985 p+=GetPixelChannels(image);
1986 q+=GetPixelChannels(splice_image);
1989 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1991 PixelChannel channel=GetPixelChannelChannel(image,i);
1992 PixelTrait traits=GetPixelChannelTraits(image,channel);
1993 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
1994 if ((traits == UndefinedPixelTrait) ||
1995 (splice_traits == UndefinedPixelTrait))
1997 SetPixelChannel(splice_image,channel,p[i],q);
1999 SetPixelRed(splice_image,GetPixelRed(image,p),q);
2000 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
2001 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
2002 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
2003 p+=GetPixelChannels(image);
2004 q+=GetPixelChannels(splice_image);
2006 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
2008 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2013 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2014 #pragma omp critical (MagickCore_TransposeImage)
2016 proceed=SetImageProgress(image,SpliceImageTag,progress++,
2017 splice_image->rows);
2018 if (proceed == MagickFalse)
2022 splice_view=DestroyCacheView(splice_view);
2023 image_view=DestroyCacheView(image_view);
2024 if (status == MagickFalse)
2025 splice_image=DestroyImage(splice_image);
2026 return(splice_image);
2030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2034 % T r a n s f o r m I m a g e %
2038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2040 % TransformImage() is a convenience method that behaves like ResizeImage() or
2041 % CropImage() but accepts scaling and/or cropping information as a region
2042 % geometry specification. If the operation fails, the original image handle
2045 % This should only be used for single images.
2047 % This function destroys what it assumes to be a single image list.
2048 % If the input image is part of a larger list, all other images in that list
2049 % will be simply 'lost', not destroyed.
2051 % Also if the crop generates a list of images only the first image is resized.
2052 % And finally if the crop succeeds and the resize failed, you will get a
2053 % cropped image, as well as a 'false' or 'failed' report.
2055 % This function and should probably be deprecated in favor of direct calls
2056 % to CropImageToTiles() or ResizeImage(), as appropriate.
2058 % The format of the TransformImage method is:
2060 % MagickBooleanType TransformImage(Image **image,const char *crop_geometry,
2061 % const char *image_geometry,ExceptionInfo *exception)
2063 % A description of each parameter follows:
2065 % o image: the image The transformed image is returned as this parameter.
2067 % o crop_geometry: A crop geometry string. This geometry defines a
2068 % subregion of the image to crop.
2070 % o image_geometry: An image geometry string. This geometry defines the
2071 % final size of the image.
2073 % o exception: return any errors or warnings in this structure.
2076 MagickExport MagickBooleanType TransformImage(Image **image,
2077 const char *crop_geometry,const char *image_geometry,ExceptionInfo *exception)
2089 assert(image != (Image **) NULL);
2090 assert((*image)->signature == MagickSignature);
2091 if ((*image)->debug != MagickFalse)
2092 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
2093 transform_image=(*image);
2094 if (crop_geometry != (const char *) NULL)
2100 Crop image to a user specified size.
2102 crop_image=CropImageToTiles(*image,crop_geometry,exception);
2103 if (crop_image == (Image *) NULL)
2104 transform_image=CloneImage(*image,0,0,MagickTrue,exception);
2107 transform_image=DestroyImage(transform_image);
2108 transform_image=GetFirstImageInList(crop_image);
2110 *image=transform_image;
2112 if (image_geometry == (const char *) NULL)
2116 Scale image to a user specified size.
2118 flags=ParseRegionGeometry(transform_image,image_geometry,&geometry,exception);
2120 if ((transform_image->columns == geometry.width) &&
2121 (transform_image->rows == geometry.height))
2123 resize_image=ResizeImage(transform_image,geometry.width,geometry.height,
2124 transform_image->filter,exception);
2125 if (resize_image == (Image *) NULL)
2126 return(MagickFalse);
2127 transform_image=DestroyImage(transform_image);
2128 transform_image=resize_image;
2129 *image=transform_image;
2134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2138 % T r a n s f o r m I m a g e s %
2142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2144 % TransformImages() calls TransformImage() on each image of a sequence.
2146 % The format of the TransformImage method is:
2148 % MagickBooleanType TransformImages(Image **image,
2149 % const char *crop_geometry,const char *image_geometry,
2150 % ExceptionInfo *exception)
2152 % A description of each parameter follows:
2154 % o image: the image The transformed image is returned as this parameter.
2156 % o crop_geometry: A crop geometry string. This geometry defines a
2157 % subregion of the image to crop.
2159 % o image_geometry: An image geometry string. This geometry defines the
2160 % final size of the image.
2162 % o exception: return any errors or warnings in this structure.
2165 MagickExport MagickBooleanType TransformImages(Image **images,
2166 const char *crop_geometry,const char *image_geometry,ExceptionInfo *exception)
2179 assert(images != (Image **) NULL);
2180 assert((*images)->signature == MagickSignature);
2181 if ((*images)->debug != MagickFalse)
2182 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2183 (*images)->filename);
2184 image_list=ImageListToArray(*images,exception);
2185 if (image_list == (Image **) NULL)
2186 return(MagickFalse);
2188 transform_images=NewImageList();
2189 for (i=0; image_list[i] != (Image *) NULL; i++)
2191 image=image_list[i];
2192 status&=TransformImage(&image,crop_geometry,image_geometry,exception);
2193 AppendImageToList(&transform_images,image);
2195 *images=transform_images;
2196 image_list=(Image **) RelinquishMagickMemory(image_list);
2197 return(status != 0 ? MagickTrue : MagickFalse);
2201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2205 % T r a n s p o s e I m a g e %
2209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2211 % TransposeImage() creates a horizontal mirror image by reflecting the pixels
2212 % around the central y-axis while rotating them by 90 degrees.
2214 % The format of the TransposeImage method is:
2216 % Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2218 % A description of each parameter follows:
2220 % o image: the image.
2222 % o exception: return any errors or warnings in this structure.
2225 MagickExport Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2227 #define TransposeImageTag "Transpose/Image"
2248 assert(image != (const Image *) NULL);
2249 assert(image->signature == MagickSignature);
2250 if (image->debug != MagickFalse)
2251 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2252 assert(exception != (ExceptionInfo *) NULL);
2253 assert(exception->signature == MagickSignature);
2254 transpose_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2256 if (transpose_image == (Image *) NULL)
2257 return((Image *) NULL);
2263 image_view=AcquireVirtualCacheView(image,exception);
2264 transpose_view=AcquireAuthenticCacheView(transpose_image,exception);
2265 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2266 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2267 magick_threads(image,transpose_image,image->rows,1)
2269 for (y=0; y < (ssize_t) image->rows; y++)
2271 register const Quantum
2280 if (status == MagickFalse)
2282 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-y-1,
2283 image->columns,1,exception);
2284 q=QueueCacheViewAuthenticPixels(transpose_view,(ssize_t) (image->rows-y-1),
2285 0,1,transpose_image->rows,exception);
2286 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2291 for (x=0; x < (ssize_t) image->columns; x++)
2296 if (GetPixelReadMask(image,q) == 0)
2298 SetPixelBackgoundColor(transpose_image,q);
2299 p+=GetPixelChannels(image);
2300 q+=GetPixelChannels(transpose_image);
2303 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2305 PixelChannel channel=GetPixelChannelChannel(image,i);
2306 PixelTrait traits=GetPixelChannelTraits(image,channel);
2307 PixelTrait transpose_traits=GetPixelChannelTraits(transpose_image,
2309 if ((traits == UndefinedPixelTrait) ||
2310 (transpose_traits == UndefinedPixelTrait))
2312 SetPixelChannel(transpose_image,channel,p[i],q);
2314 p+=GetPixelChannels(image);
2315 q+=GetPixelChannels(transpose_image);
2317 if (SyncCacheViewAuthenticPixels(transpose_view,exception) == MagickFalse)
2319 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2324 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2325 #pragma omp critical (MagickCore_TransposeImage)
2327 proceed=SetImageProgress(image,TransposeImageTag,progress++,
2329 if (proceed == MagickFalse)
2333 transpose_view=DestroyCacheView(transpose_view);
2334 image_view=DestroyCacheView(image_view);
2335 transpose_image->type=image->type;
2336 page=transpose_image->page;
2337 Swap(page.width,page.height);
2338 Swap(page.x,page.y);
2339 transpose_image->page=page;
2340 if (status == MagickFalse)
2341 transpose_image=DestroyImage(transpose_image);
2342 return(transpose_image);
2346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2350 % T r a n s v e r s e I m a g e %
2354 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2356 % TransverseImage() creates a vertical mirror image by reflecting the pixels
2357 % around the central x-axis while rotating them by 270 degrees.
2359 % The format of the TransverseImage method is:
2361 % Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2363 % A description of each parameter follows:
2365 % o image: the image.
2367 % o exception: return any errors or warnings in this structure.
2370 MagickExport Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2372 #define TransverseImageTag "Transverse/Image"
2393 assert(image != (const Image *) NULL);
2394 assert(image->signature == MagickSignature);
2395 if (image->debug != MagickFalse)
2396 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2397 assert(exception != (ExceptionInfo *) NULL);
2398 assert(exception->signature == MagickSignature);
2399 transverse_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2401 if (transverse_image == (Image *) NULL)
2402 return((Image *) NULL);
2408 image_view=AcquireVirtualCacheView(image,exception);
2409 transverse_view=AcquireAuthenticCacheView(transverse_image,exception);
2410 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2411 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2412 magick_threads(image,transverse_image,image->rows,1)
2414 for (y=0; y < (ssize_t) image->rows; y++)
2419 register const Quantum
2428 if (status == MagickFalse)
2430 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2431 q=QueueCacheViewAuthenticPixels(transverse_view,(ssize_t) (image->rows-y-1),
2432 0,1,transverse_image->rows,exception);
2433 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2438 q+=GetPixelChannels(transverse_image)*image->columns;
2439 for (x=0; x < (ssize_t) image->columns; x++)
2444 q-=GetPixelChannels(transverse_image);
2445 if (GetPixelReadMask(image,p) == 0)
2447 p+=GetPixelChannels(image);
2450 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2452 PixelChannel channel=GetPixelChannelChannel(image,i);
2453 PixelTrait traits=GetPixelChannelTraits(image,channel);
2454 PixelTrait transverse_traits=GetPixelChannelTraits(transverse_image,
2456 if ((traits == UndefinedPixelTrait) ||
2457 (transverse_traits == UndefinedPixelTrait))
2459 SetPixelChannel(transverse_image,channel,p[i],q);
2461 p+=GetPixelChannels(image);
2463 sync=SyncCacheViewAuthenticPixels(transverse_view,exception);
2464 if (sync == MagickFalse)
2466 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2471 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2472 #pragma omp critical (MagickCore_TransverseImage)
2474 proceed=SetImageProgress(image,TransverseImageTag,progress++,
2476 if (proceed == MagickFalse)
2480 transverse_view=DestroyCacheView(transverse_view);
2481 image_view=DestroyCacheView(image_view);
2482 transverse_image->type=image->type;
2483 page=transverse_image->page;
2484 Swap(page.width,page.height);
2485 Swap(page.x,page.y);
2486 if (page.width != 0)
2487 page.x=(ssize_t) (page.width-transverse_image->columns-page.x);
2488 if (page.height != 0)
2489 page.y=(ssize_t) (page.height-transverse_image->rows-page.y);
2490 transverse_image->page=page;
2491 if (status == MagickFalse)
2492 transverse_image=DestroyImage(transverse_image);
2493 return(transverse_image);
2497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2501 % T r i m I m a g e %
2505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2507 % TrimImage() trims pixels from the image edges. It allocates the memory
2508 % necessary for the new Image structure and returns a pointer to the new
2511 % The format of the TrimImage method is:
2513 % Image *TrimImage(const Image *image,ExceptionInfo *exception)
2515 % A description of each parameter follows:
2517 % o image: the image.
2519 % o exception: return any errors or warnings in this structure.
2522 MagickExport Image *TrimImage(const Image *image,ExceptionInfo *exception)
2527 assert(image != (const Image *) NULL);
2528 assert(image->signature == MagickSignature);
2529 if (image->debug != MagickFalse)
2530 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2531 geometry=GetImageBoundingBox(image,exception);
2532 if ((geometry.width == 0) || (geometry.height == 0))
2537 crop_image=CloneImage(image,1,1,MagickTrue,exception);
2538 if (crop_image == (Image *) NULL)
2539 return((Image *) NULL);
2540 crop_image->background_color.alpha=(Quantum) TransparentAlpha;
2541 crop_image->alpha_trait=BlendPixelTrait;
2542 (void) SetImageBackgroundColor(crop_image,exception);
2543 crop_image->page=image->page;
2544 crop_image->page.x=(-1);
2545 crop_image->page.y=(-1);
2548 geometry.x+=image->page.x;
2549 geometry.y+=image->page.y;
2550 return(CropImage(image,&geometry,exception));