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-2011 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/draw.h"
51 #include "MagickCore/effect.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/layer.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/resource_.h"
63 #include "MagickCore/resize.h"
64 #include "MagickCore/statistic.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/thread-private.h"
67 #include "MagickCore/transform.h"
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % ChopImage() removes a region of an image and collapses the image to occupy
81 % the removed portion.
83 % The format of the ChopImage method is:
85 % Image *ChopImage(const Image *image,const RectangleInfo *chop_info)
86 % ExceptionInfo *exception)
88 % A description of each parameter follows:
92 % o chop_info: Define the region of the image to chop.
94 % o exception: return any errors or warnings in this structure.
97 MagickExport Image *ChopImage(const Image *image,const RectangleInfo *chop_info,
98 ExceptionInfo *exception)
100 #define ChopImageTag "Chop/Image"
124 assert(image != (const Image *) NULL);
125 assert(image->signature == MagickSignature);
126 if (image->debug != MagickFalse)
127 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
128 assert(exception != (ExceptionInfo *) NULL);
129 assert(exception->signature == MagickSignature);
130 assert(chop_info != (RectangleInfo *) NULL);
131 if (((chop_info->x+(ssize_t) chop_info->width) < 0) ||
132 ((chop_info->y+(ssize_t) chop_info->height) < 0) ||
133 (chop_info->x > (ssize_t) image->columns) ||
134 (chop_info->y > (ssize_t) image->rows))
135 ThrowImageException(OptionWarning,"GeometryDoesNotContainImage");
137 if ((extent.x+(ssize_t) extent.width) > (ssize_t) image->columns)
138 extent.width=(size_t) ((ssize_t) image->columns-extent.x);
139 if ((extent.y+(ssize_t) extent.height) > (ssize_t) image->rows)
140 extent.height=(size_t) ((ssize_t) image->rows-extent.y);
143 extent.width-=(size_t) (-extent.x);
148 extent.height-=(size_t) (-extent.y);
151 chop_image=CloneImage(image,image->columns-extent.width,image->rows-
152 extent.height,MagickTrue,exception);
153 if (chop_image == (Image *) NULL)
154 return((Image *) NULL);
160 image_view=AcquireCacheView(image);
161 chop_view=AcquireCacheView(chop_image);
162 #if defined(MAGICKCORE_OPENMP_SUPPORT)
163 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
165 for (y=0; y < (ssize_t) extent.y; y++)
167 register const Quantum
176 if (status == MagickFalse)
178 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
179 q=QueueCacheViewAuthenticPixels(chop_view,0,y,chop_image->columns,1,
181 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
186 for (x=0; x < (ssize_t) image->columns; x++)
188 if ((x < extent.x) || (x >= (ssize_t) (extent.x+extent.width)))
190 SetPixelRed(chop_image,GetPixelRed(image,p),q);
191 SetPixelGreen(chop_image,GetPixelGreen(image,p),q);
192 SetPixelBlue(chop_image,GetPixelBlue(image,p),q);
193 if (image->colorspace == CMYKColorspace)
194 SetPixelBlack(chop_image,GetPixelBlack(image,p),q);
195 q+=GetPixelChannels(chop_image);
197 p+=GetPixelChannels(image);
199 if (SyncCacheViewAuthenticPixels(chop_view,exception) == MagickFalse)
201 if (image->progress_monitor != (MagickProgressMonitor) NULL)
206 #if defined(MAGICKCORE_OPENMP_SUPPORT)
207 #pragma omp critical (MagickCore_ChopImage)
209 proceed=SetImageProgress(image,ChopImageTag,progress++,image->rows);
210 if (proceed == MagickFalse)
217 #if defined(MAGICKCORE_OPENMP_SUPPORT)
218 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
220 for (y=0; y < (ssize_t) (image->rows-(extent.y+extent.height)); y++)
222 register const Quantum
231 if (status == MagickFalse)
233 p=GetCacheViewVirtualPixels(image_view,0,extent.y+extent.height+y,
234 image->columns,1,exception);
235 q=QueueCacheViewAuthenticPixels(chop_view,0,extent.y+y,chop_image->columns,
237 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
242 for (x=0; x < (ssize_t) image->columns; x++)
244 if ((x < extent.x) || (x >= (ssize_t) (extent.x+extent.width)))
246 SetPixelRed(chop_image,GetPixelRed(image,p),q);
247 SetPixelGreen(chop_image,GetPixelGreen(image,p),q);
248 SetPixelBlue(chop_image,GetPixelBlue(image,p),q);
249 p+=GetPixelChannels(image);
250 q+=GetPixelChannels(chop_image);
252 p+=GetPixelChannels(image);
254 if (SyncCacheViewAuthenticPixels(chop_view,exception) == MagickFalse)
256 if (image->progress_monitor != (MagickProgressMonitor) NULL)
261 #if defined(MAGICKCORE_OPENMP_SUPPORT)
262 #pragma omp critical (MagickCore_ChopImage)
264 proceed=SetImageProgress(image,ChopImageTag,progress++,image->rows);
265 if (proceed == MagickFalse)
269 chop_view=DestroyCacheView(chop_view);
270 image_view=DestroyCacheView(image_view);
271 chop_image->type=image->type;
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 + C o n s o l i d a t e C M Y K I m a g e %
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 % ConsolidateCMYKImage() consolidates separate C, M, Y, and K planes into a
289 % The format of the ConsolidateCMYKImage method is:
291 % Image *ConsolidateCMYKImage(const Image *image,ExceptionInfo *exception)
293 % A description of each parameter follows:
295 % o image: the image sequence.
297 % o exception: return any errors or warnings in this structure.
300 MagickExport Image *ConsolidateCMYKImages(const Image *images,
301 ExceptionInfo *exception)
318 Consolidate separate C, M, Y, and K planes into a single image.
320 assert(images != (Image *) NULL);
321 assert(images->signature == MagickSignature);
322 if (images->debug != MagickFalse)
323 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
324 assert(exception != (ExceptionInfo *) NULL);
325 assert(exception->signature == MagickSignature);
326 cmyk_images=NewImageList();
327 for (i=0; i < (ssize_t) GetImageListLength(images); i+=4)
329 cmyk_image=CloneImage(images,images->columns,images->rows,MagickTrue,
331 if (cmyk_image == (Image *) NULL)
333 if (SetImageStorageClass(cmyk_image,DirectClass,exception) == MagickFalse)
335 (void) SetImageColorspace(cmyk_image,CMYKColorspace,exception);
336 image_view=AcquireCacheView(images);
337 cmyk_view=AcquireCacheView(cmyk_image);
338 for (y=0; y < (ssize_t) images->rows; y++)
340 register const Quantum
349 p=GetCacheViewVirtualPixels(image_view,0,y,images->columns,1,exception);
350 q=QueueCacheViewAuthenticPixels(cmyk_view,0,y,cmyk_image->columns,1,
352 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
354 for (x=0; x < (ssize_t) images->columns; x++)
356 SetPixelRed(cmyk_image,QuantumRange-GetPixelIntensity(images,p),q);
357 p+=GetPixelChannels(images);
358 q+=GetPixelChannels(cmyk_image);
360 if (SyncCacheViewAuthenticPixels(cmyk_view,exception) == MagickFalse)
363 cmyk_view=DestroyCacheView(cmyk_view);
364 image_view=DestroyCacheView(image_view);
365 images=GetNextImageInList(images);
366 if (images == (Image *) NULL)
368 image_view=AcquireCacheView(images);
369 cmyk_view=AcquireCacheView(cmyk_image);
370 for (y=0; y < (ssize_t) images->rows; y++)
372 register const Quantum
381 p=GetCacheViewVirtualPixels(image_view,0,y,images->columns,1,exception);
382 q=GetCacheViewAuthenticPixels(cmyk_view,0,y,cmyk_image->columns,1,
384 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
386 for (x=0; x < (ssize_t) images->columns; x++)
388 SetPixelGreen(cmyk_image,QuantumRange-GetPixelIntensity(images,p),q);
389 p+=GetPixelChannels(images);
390 q+=GetPixelChannels(cmyk_image);
392 if (SyncCacheViewAuthenticPixels(cmyk_view,exception) == MagickFalse)
395 cmyk_view=DestroyCacheView(cmyk_view);
396 image_view=DestroyCacheView(image_view);
397 images=GetNextImageInList(images);
398 if (images == (Image *) NULL)
400 image_view=AcquireCacheView(images);
401 cmyk_view=AcquireCacheView(cmyk_image);
402 for (y=0; y < (ssize_t) images->rows; y++)
404 register const Quantum
413 p=GetCacheViewVirtualPixels(image_view,0,y,images->columns,1,exception);
414 q=GetCacheViewAuthenticPixels(cmyk_view,0,y,cmyk_image->columns,1,
416 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
418 for (x=0; x < (ssize_t) images->columns; x++)
420 SetPixelBlue(cmyk_image,QuantumRange-GetPixelIntensity(images,p),q);
421 p+=GetPixelChannels(images);
422 q+=GetPixelChannels(cmyk_image);
424 if (SyncCacheViewAuthenticPixels(cmyk_view,exception) == MagickFalse)
427 cmyk_view=DestroyCacheView(cmyk_view);
428 image_view=DestroyCacheView(image_view);
429 images=GetNextImageInList(images);
430 if (images == (Image *) NULL)
432 image_view=AcquireCacheView(images);
433 cmyk_view=AcquireCacheView(cmyk_image);
434 for (y=0; y < (ssize_t) images->rows; y++)
436 register const Quantum
445 p=GetCacheViewVirtualPixels(image_view,0,y,images->columns,1,exception);
446 q=GetCacheViewAuthenticPixels(cmyk_view,0,y,cmyk_image->columns,1,
448 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
450 for (x=0; x < (ssize_t) images->columns; x++)
452 SetPixelBlack(cmyk_image,QuantumRange-GetPixelIntensity(images,p),q);
453 p+=GetPixelChannels(images);
454 q+=GetPixelChannels(cmyk_image);
456 if (SyncCacheViewAuthenticPixels(cmyk_view,exception) == MagickFalse)
459 cmyk_view=DestroyCacheView(cmyk_view);
460 image_view=DestroyCacheView(image_view);
461 AppendImageToList(&cmyk_images,cmyk_image);
462 images=GetNextImageInList(images);
463 if (images == (Image *) NULL)
470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 % C r o p I m a g e %
478 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
480 % CropImage() extracts a region of the image starting at the offset defined
481 % by geometry. Region must be fully defined, and no special handling of
482 % geometry flags is performed.
484 % The format of the CropImage method is:
486 % Image *CropImage(const Image *image,const RectangleInfo *geometry,
487 % ExceptionInfo *exception)
489 % A description of each parameter follows:
491 % o image: the image.
493 % o geometry: Define the region of the image to crop with members
494 % x, y, width, and height.
496 % o exception: return any errors or warnings in this structure.
499 MagickExport Image *CropImage(const Image *image,const RectangleInfo *geometry,
500 ExceptionInfo *exception)
502 #define CropImageTag "Crop/Image"
530 assert(image != (const Image *) NULL);
531 assert(image->signature == MagickSignature);
532 if (image->debug != MagickFalse)
533 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
534 assert(geometry != (const RectangleInfo *) NULL);
535 assert(exception != (ExceptionInfo *) NULL);
536 assert(exception->signature == MagickSignature);
537 bounding_box=image->page;
538 if ((bounding_box.width == 0) || (bounding_box.height == 0))
540 bounding_box.width=image->columns;
541 bounding_box.height=image->rows;
545 page.width=bounding_box.width;
546 if (page.height == 0)
547 page.height=bounding_box.height;
548 if (((bounding_box.x-page.x) >= (ssize_t) page.width) ||
549 ((bounding_box.y-page.y) >= (ssize_t) page.height) ||
550 ((page.x-bounding_box.x) > (ssize_t) image->columns) ||
551 ((page.y-bounding_box.y) > (ssize_t) image->rows))
554 Crop is not within virtual canvas, return 1 pixel transparent image.
556 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
557 "GeometryDoesNotContainImage","`%s'",image->filename);
558 crop_image=CloneImage(image,1,1,MagickTrue,exception);
559 if (crop_image == (Image *) NULL)
560 return((Image *) NULL);
561 crop_image->background_color.alpha=(Quantum) TransparentAlpha;
562 (void) SetImageBackgroundColor(crop_image);
563 crop_image->page=bounding_box;
564 crop_image->page.x=(-1);
565 crop_image->page.y=(-1);
566 if (crop_image->dispose == BackgroundDispose)
567 crop_image->dispose=NoneDispose;
570 if ((page.x < 0) && (bounding_box.x >= 0))
572 page.width+=page.x-bounding_box.x;
577 page.width-=bounding_box.x-page.x;
578 page.x-=bounding_box.x;
582 if ((page.y < 0) && (bounding_box.y >= 0))
584 page.height+=page.y-bounding_box.y;
589 page.height-=bounding_box.y-page.y;
590 page.y-=bounding_box.y;
594 if ((size_t) (page.x+page.width) > image->columns)
595 page.width=image->columns-page.x;
596 if ((geometry->width != 0) && (page.width > geometry->width))
597 page.width=geometry->width;
598 if ((size_t) (page.y+page.height) > image->rows)
599 page.height=image->rows-page.y;
600 if ((geometry->height != 0) && (page.height > geometry->height))
601 page.height=geometry->height;
602 bounding_box.x+=page.x;
603 bounding_box.y+=page.y;
604 if ((page.width == 0) || (page.height == 0))
606 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
607 "GeometryDoesNotContainImage","`%s'",image->filename);
608 return((Image *) NULL);
611 Initialize crop image attributes.
613 crop_image=CloneImage(image,page.width,page.height,MagickTrue,exception);
614 if (crop_image == (Image *) NULL)
615 return((Image *) NULL);
616 crop_image->page.width=image->page.width;
617 crop_image->page.height=image->page.height;
618 offset.x=(ssize_t) (bounding_box.x+bounding_box.width);
619 offset.y=(ssize_t) (bounding_box.y+bounding_box.height);
620 if ((offset.x > (ssize_t) image->page.width) ||
621 (offset.y > (ssize_t) image->page.height))
623 crop_image->page.width=bounding_box.width;
624 crop_image->page.height=bounding_box.height;
626 crop_image->page.x=bounding_box.x;
627 crop_image->page.y=bounding_box.y;
633 image_view=AcquireCacheView(image);
634 crop_view=AcquireCacheView(crop_image);
635 #if defined(MAGICKCORE_OPENMP_SUPPORT)
636 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
638 for (y=0; y < (ssize_t) crop_image->rows; y++)
640 register const Quantum
649 if (status == MagickFalse)
651 p=GetCacheViewVirtualPixels(image_view,page.x,page.y+y,crop_image->columns,
653 q=QueueCacheViewAuthenticPixels(crop_view,0,y,crop_image->columns,1,
655 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
660 for (x=0; x < (ssize_t) crop_image->columns; x++)
665 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
674 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
675 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
676 crop_traits=GetPixelChannelMapTraits(crop_image,channel);
677 if ((traits == UndefinedPixelTrait) ||
678 (crop_traits == UndefinedPixelTrait))
682 p+=GetPixelChannels(image);
683 q+=GetPixelChannels(crop_image);
685 if (SyncCacheViewAuthenticPixels(crop_view,exception) == MagickFalse)
687 if (image->progress_monitor != (MagickProgressMonitor) NULL)
692 #if defined(MAGICKCORE_OPENMP_SUPPORT)
693 #pragma omp critical (MagickCore_CropImage)
695 proceed=SetImageProgress(image,CropImageTag,progress++,image->rows);
696 if (proceed == MagickFalse)
700 crop_view=DestroyCacheView(crop_view);
701 image_view=DestroyCacheView(image_view);
702 crop_image->type=image->type;
703 if (status == MagickFalse)
704 crop_image=DestroyImage(crop_image);
709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 % C r o p I m a g e T o T i l e s %
717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 % CropImageToTiles() will crop a single image, into a possible list of tiles.
720 % This may include a single sub-region of the image. This basically applies
721 % all the normal geometry flags for Crop.
723 % Image *CropImageToTiles(const Image *image,const RectangleInfo
724 % *crop_geometry, ExceptionInfo *exception)
726 % A description of each parameter follows:
728 % o image: the image The transformed image is returned as this parameter.
730 % o crop_geometry: A crop geometry string.
732 % o exception: return any errors or warnings in this structure.
736 static inline ssize_t MagickRound(MagickRealType x)
739 Round the fraction to nearest integer.
742 return((ssize_t) (x+0.5));
743 return((ssize_t) (x-0.5));
746 MagickExport Image *CropImageToTiles(const Image *image,
747 const char *crop_geometry, ExceptionInfo *exception)
759 assert(image != (Image *) NULL);
760 assert(image->signature == MagickSignature);
761 if (image->debug != MagickFalse)
762 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
763 crop_image=NewImageList();
765 flags=ParseGravityGeometry(image,crop_geometry,&geometry,exception);
766 if ((flags & AreaValue) != 0)
780 Crop into NxM tiles (@ flag).
782 width=image->columns;
784 if (geometry.width == 0)
786 if (geometry.height == 0)
788 if ((flags & AspectValue) == 0)
790 width-=(geometry.x < 0 ? -1 : 1)*geometry.x;
791 height-=(geometry.y < 0 ? -1 : 1)*geometry.y;
795 width+=(geometry.x < 0 ? -1 : 1)*geometry.x;
796 height+=(geometry.y < 0 ? -1 : 1)*geometry.y;
798 delta.x=(double) width/geometry.width;
799 delta.y=(double) height/geometry.height;
800 for (offset.y=0; offset.y < (double) height; )
802 if ((flags & AspectValue) == 0)
804 crop.y=(ssize_t) MagickRound((MagickRealType) (offset.y-
805 (geometry.y > 0 ? 0 : geometry.y)));
806 offset.y+=delta.y; /* increment now to find width */
807 crop.height=(size_t) MagickRound((MagickRealType) (offset.y+
808 (geometry.y < 0 ? 0 : geometry.y)));
812 crop.y=(ssize_t) MagickRound((MagickRealType) (offset.y-
813 (geometry.y > 0 ? geometry.y : 0)));
814 offset.y+=delta.y; /* increment now to find width */
815 crop.height=(size_t) MagickRound((MagickRealType)
816 (offset.y+(geometry.y < -1 ? geometry.y : 0)));
819 crop.y+=image->page.y;
820 for (offset.x=0; offset.x < (double) width; )
822 if ((flags & AspectValue) == 0)
824 crop.x=(ssize_t) MagickRound((MagickRealType) (offset.x-
825 (geometry.x > 0 ? 0 : geometry.x)));
826 offset.x+=delta.x; /* increment now to find height */
827 crop.width=(size_t) MagickRound((MagickRealType) (offset.x+
828 (geometry.x < 0 ? 0 : geometry.x)));
832 crop.x=(ssize_t) MagickRound((MagickRealType) (offset.x-
833 (geometry.x > 0 ? geometry.x : 0)));
834 offset.x+=delta.x; /* increment now to find height */
835 crop.width=(size_t) MagickRound((MagickRealType) (offset.x+
836 (geometry.x < 0 ? geometry.x : 0)));
839 crop.x+=image->page.x;
840 next=CropImage(image,&crop,exception);
841 if (next == (Image *) NULL)
843 AppendImageToList(&crop_image,next);
845 if (next == (Image *) NULL)
848 ClearMagickException(exception);
852 if (((geometry.width == 0) && (geometry.height == 0)) ||
853 ((flags & XValue) != 0) || ((flags & YValue) != 0))
856 Crop a single region at +X+Y.
858 crop_image=CropImage(image,&geometry,exception);
859 if ((crop_image != (Image *) NULL) && ((flags & AspectValue) != 0))
861 crop_image->page.width=geometry.width;
862 crop_image->page.height=geometry.height;
863 crop_image->page.x-=geometry.x;
864 crop_image->page.y-=geometry.y;
868 if ((image->columns > geometry.width) || (image->rows > geometry.height))
882 Crop into tiles of fixed size WxH.
886 page.width=image->columns;
887 if (page.height == 0)
888 page.height=image->rows;
889 width=geometry.width;
892 height=geometry.height;
896 for (y=0; y < (ssize_t) page.height; y+=(ssize_t) height)
898 for (x=0; x < (ssize_t) page.width; x+=(ssize_t) width)
900 geometry.width=width;
901 geometry.height=height;
904 next=CropImage(image,&geometry,exception);
905 if (next == (Image *) NULL)
907 AppendImageToList(&crop_image,next);
909 if (next == (Image *) NULL)
914 return(CloneImage(image,0,0,MagickTrue,exception));
918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
922 % E x c e r p t I m a g e %
926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
928 % ExcerptImage() returns a excerpt of the image as defined by the geometry.
930 % The format of the ExcerptImage method is:
932 % Image *ExcerptImage(const Image *image,const RectangleInfo *geometry,
933 % ExceptionInfo *exception)
935 % A description of each parameter follows:
937 % o image: the image.
939 % o geometry: Define the region of the image to extend with members
940 % x, y, width, and height.
942 % o exception: return any errors or warnings in this structure.
945 MagickExport Image *ExcerptImage(const Image *image,
946 const RectangleInfo *geometry,ExceptionInfo *exception)
948 #define ExcerptImageTag "Excerpt/Image"
967 Allocate excerpt image.
969 assert(image != (const Image *) NULL);
970 assert(image->signature == MagickSignature);
971 if (image->debug != MagickFalse)
972 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
973 assert(geometry != (const RectangleInfo *) NULL);
974 assert(exception != (ExceptionInfo *) NULL);
975 assert(exception->signature == MagickSignature);
976 excerpt_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
978 if (excerpt_image == (Image *) NULL)
979 return((Image *) NULL);
985 image_view=AcquireCacheView(image);
986 excerpt_view=AcquireCacheView(excerpt_image);
987 #if defined(MAGICKCORE_OPENMP_SUPPORT)
988 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
990 for (y=0; y < (ssize_t) excerpt_image->rows; y++)
992 register const Quantum
1001 if (status == MagickFalse)
1003 p=GetCacheViewVirtualPixels(image_view,geometry->x,geometry->y+y,
1004 geometry->width,1,exception);
1005 q=GetCacheViewAuthenticPixels(excerpt_view,0,y,excerpt_image->columns,1,
1007 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1012 for (x=0; x < (ssize_t) excerpt_image->columns; x++)
1017 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1026 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1027 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1028 excerpt_traits=GetPixelChannelMapTraits(excerpt_image,channel);
1029 if ((traits == UndefinedPixelTrait) ||
1030 (excerpt_traits == UndefinedPixelTrait))
1034 p+=GetPixelChannels(image);
1035 q+=GetPixelChannels(excerpt_image);
1037 if (SyncCacheViewAuthenticPixels(excerpt_view,exception) == MagickFalse)
1039 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1044 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1045 #pragma omp critical (MagickCore_ExcerptImage)
1047 proceed=SetImageProgress(image,ExcerptImageTag,progress++,image->rows);
1048 if (proceed == MagickFalse)
1052 excerpt_view=DestroyCacheView(excerpt_view);
1053 image_view=DestroyCacheView(image_view);
1054 excerpt_image->type=image->type;
1055 if (status == MagickFalse)
1056 excerpt_image=DestroyImage(excerpt_image);
1057 return(excerpt_image);
1061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1065 % E x t e n t I m a g e %
1069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1071 % ExtentImage() extends the image as defined by the geometry, gravity, and
1072 % image background color. Set the (x,y) offset of the geometry to move the
1073 % original image relative to the extended image.
1075 % The format of the ExtentImage method is:
1077 % Image *ExtentImage(const Image *image,const RectangleInfo *geometry,
1078 % ExceptionInfo *exception)
1080 % A description of each parameter follows:
1082 % o image: the image.
1084 % o geometry: Define the region of the image to extend with members
1085 % x, y, width, and height.
1087 % o exception: return any errors or warnings in this structure.
1090 MagickExport Image *ExtentImage(const Image *image,
1091 const RectangleInfo *geometry,ExceptionInfo *exception)
1097 Allocate extent image.
1099 assert(image != (const Image *) NULL);
1100 assert(image->signature == MagickSignature);
1101 if (image->debug != MagickFalse)
1102 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1103 assert(geometry != (const RectangleInfo *) NULL);
1104 assert(exception != (ExceptionInfo *) NULL);
1105 assert(exception->signature == MagickSignature);
1106 extent_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
1108 if (extent_image == (Image *) NULL)
1109 return((Image *) NULL);
1110 if (SetImageStorageClass(extent_image,DirectClass,exception) == MagickFalse)
1112 extent_image=DestroyImage(extent_image);
1113 return((Image *) NULL);
1115 if (extent_image->background_color.alpha != OpaqueAlpha)
1116 extent_image->matte=MagickTrue;
1117 (void) SetImageBackgroundColor(extent_image);
1118 (void) CompositeImage(extent_image,image->compose,image,-geometry->x,
1120 return(extent_image);
1124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1128 % F l i p I m a g e %
1132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1134 % FlipImage() creates a vertical mirror image by reflecting the pixels
1135 % around the central x-axis.
1137 % The format of the FlipImage method is:
1139 % Image *FlipImage(const Image *image,ExceptionInfo *exception)
1141 % A description of each parameter follows:
1143 % o image: the image.
1145 % o exception: return any errors or warnings in this structure.
1148 MagickExport Image *FlipImage(const Image *image,ExceptionInfo *exception)
1150 #define FlipImageTag "Flip/Image"
1171 assert(image != (const Image *) NULL);
1172 assert(image->signature == MagickSignature);
1173 if (image->debug != MagickFalse)
1174 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1175 assert(exception != (ExceptionInfo *) NULL);
1176 assert(exception->signature == MagickSignature);
1177 flip_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1178 if (flip_image == (Image *) NULL)
1179 return((Image *) NULL);
1186 image_view=AcquireCacheView(image);
1187 flip_view=AcquireCacheView(flip_image);
1188 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1189 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
1191 for (y=0; y < (ssize_t) flip_image->rows; y++)
1193 register const Quantum
1202 if (status == MagickFalse)
1204 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1205 q=QueueCacheViewAuthenticPixels(flip_view,0,(ssize_t) (flip_image->rows-y-
1206 1),flip_image->columns,1,exception);
1207 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1212 for (x=0; x < (ssize_t) flip_image->columns; x++)
1217 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1226 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1227 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1228 flip_traits=GetPixelChannelMapTraits(flip_image,channel);
1229 if ((traits == UndefinedPixelTrait) ||
1230 (flip_traits == UndefinedPixelTrait))
1234 p+=GetPixelChannels(image);
1235 q+=GetPixelChannels(flip_image);
1237 if (SyncCacheViewAuthenticPixels(flip_view,exception) == MagickFalse)
1239 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1244 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1245 #pragma omp critical (MagickCore_FlipImage)
1247 proceed=SetImageProgress(image,FlipImageTag,progress++,image->rows);
1248 if (proceed == MagickFalse)
1252 flip_view=DestroyCacheView(flip_view);
1253 image_view=DestroyCacheView(image_view);
1254 flip_image->type=image->type;
1255 if (page.height != 0)
1256 page.y=(ssize_t) (page.height-flip_image->rows-page.y);
1257 flip_image->page=page;
1258 if (status == MagickFalse)
1259 flip_image=DestroyImage(flip_image);
1264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268 % F l o p I m a g e %
1272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1274 % FlopImage() creates a horizontal mirror image by reflecting the pixels
1275 % around the central y-axis.
1277 % The format of the FlopImage method is:
1279 % Image *FlopImage(const Image *image,ExceptionInfo *exception)
1281 % A description of each parameter follows:
1283 % o image: the image.
1285 % o exception: return any errors or warnings in this structure.
1288 MagickExport Image *FlopImage(const Image *image,ExceptionInfo *exception)
1290 #define FlopImageTag "Flop/Image"
1311 assert(image != (const Image *) NULL);
1312 assert(image->signature == MagickSignature);
1313 if (image->debug != MagickFalse)
1314 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1315 assert(exception != (ExceptionInfo *) NULL);
1316 assert(exception->signature == MagickSignature);
1317 flop_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1318 if (flop_image == (Image *) NULL)
1319 return((Image *) NULL);
1326 image_view=AcquireCacheView(image);
1327 flop_view=AcquireCacheView(flop_image);
1328 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1329 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
1331 for (y=0; y < (ssize_t) flop_image->rows; y++)
1333 register const Quantum
1342 if (status == MagickFalse)
1344 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1345 q=QueueCacheViewAuthenticPixels(flop_view,0,y,flop_image->columns,1,
1347 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1352 q+=GetPixelChannels(flop_image)*flop_image->columns;
1353 for (x=0; x < (ssize_t) flop_image->columns; x++)
1358 q-=GetPixelChannels(flop_image);
1359 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1368 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1369 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1370 flop_traits=GetPixelChannelMapTraits(flop_image,channel);
1371 if ((traits == UndefinedPixelTrait) ||
1372 (flop_traits == UndefinedPixelTrait))
1376 p+=GetPixelChannels(image);
1378 if (SyncCacheViewAuthenticPixels(flop_view,exception) == MagickFalse)
1380 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1385 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1386 #pragma omp critical (MagickCore_FlopImage)
1388 proceed=SetImageProgress(image,FlopImageTag,progress++,image->rows);
1389 if (proceed == MagickFalse)
1393 flop_view=DestroyCacheView(flop_view);
1394 image_view=DestroyCacheView(image_view);
1395 flop_image->type=image->type;
1396 if (page.width != 0)
1397 page.x=(ssize_t) (page.width-flop_image->columns-page.x);
1398 flop_image->page=page;
1399 if (status == MagickFalse)
1400 flop_image=DestroyImage(flop_image);
1405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409 % R o l l I m a g e %
1413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1415 % RollImage() offsets an image as defined by x_offset and y_offset.
1417 % The format of the RollImage method is:
1419 % Image *RollImage(const Image *image,const ssize_t x_offset,
1420 % const ssize_t y_offset,ExceptionInfo *exception)
1422 % A description of each parameter follows:
1424 % o image: the image.
1426 % o x_offset: the number of columns to roll in the horizontal direction.
1428 % o y_offset: the number of rows to roll in the vertical direction.
1430 % o exception: return any errors or warnings in this structure.
1434 static inline MagickBooleanType CopyImageRegion(Image *destination,
1435 const Image *source,const size_t columns,const size_t rows,
1436 const ssize_t sx,const ssize_t sy,const ssize_t dx,const ssize_t dy,
1437 ExceptionInfo *exception)
1450 source_view=AcquireCacheView(source);
1451 destination_view=AcquireCacheView(destination);
1452 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1453 #pragma omp parallel for schedule(dynamic,4) shared(status)
1455 for (y=0; y < (ssize_t) rows; y++)
1460 register const Quantum
1472 if (status == MagickFalse)
1474 p=GetCacheViewVirtualPixels(source_view,sx,sy+y,columns,1,exception);
1475 q=GetCacheViewAuthenticPixels(destination_view,dx,dy+y,columns,1,exception);
1476 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1481 for (x=0; x < (ssize_t) columns; x++)
1486 for (i=0; i < (ssize_t) GetPixelChannels(source); i++)
1495 source_traits=GetPixelChannelMapTraits(source,(PixelChannel) i);
1496 channel=GetPixelChannelMapChannel(source,(PixelChannel) i);
1497 destination_traits=GetPixelChannelMapTraits(destination,channel);
1498 if ((source_traits == UndefinedPixelTrait) ||
1499 (destination_traits == UndefinedPixelTrait))
1503 p+=GetPixelChannels(source);
1504 q+=GetPixelChannels(destination);
1506 sync=SyncCacheViewAuthenticPixels(destination_view,exception);
1507 if (sync == MagickFalse)
1510 destination_view=DestroyCacheView(destination_view);
1511 source_view=DestroyCacheView(source_view);
1515 MagickExport Image *RollImage(const Image *image,const ssize_t x_offset,
1516 const ssize_t y_offset,ExceptionInfo *exception)
1518 #define RollImageTag "Roll/Image"
1530 Initialize roll image attributes.
1532 assert(image != (const Image *) NULL);
1533 assert(image->signature == MagickSignature);
1534 if (image->debug != MagickFalse)
1535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1536 assert(exception != (ExceptionInfo *) NULL);
1537 assert(exception->signature == MagickSignature);
1538 roll_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1539 if (roll_image == (Image *) NULL)
1540 return((Image *) NULL);
1543 while (offset.x < 0)
1544 offset.x+=(ssize_t) image->columns;
1545 while (offset.x >= (ssize_t) image->columns)
1546 offset.x-=(ssize_t) image->columns;
1547 while (offset.y < 0)
1548 offset.y+=(ssize_t) image->rows;
1549 while (offset.y >= (ssize_t) image->rows)
1550 offset.y-=(ssize_t) image->rows;
1554 status=CopyImageRegion(roll_image,image,(size_t) offset.x,
1555 (size_t) offset.y,(ssize_t) image->columns-offset.x,(ssize_t) image->rows-
1556 offset.y,0,0,exception);
1557 (void) SetImageProgress(image,RollImageTag,0,3);
1558 status|=CopyImageRegion(roll_image,image,image->columns-offset.x,
1559 (size_t) offset.y,0,(ssize_t) image->rows-offset.y,offset.x,0,
1561 (void) SetImageProgress(image,RollImageTag,1,3);
1562 status|=CopyImageRegion(roll_image,image,(size_t) offset.x,image->rows-
1563 offset.y,(ssize_t) image->columns-offset.x,0,0,offset.y,exception);
1564 (void) SetImageProgress(image,RollImageTag,2,3);
1565 status|=CopyImageRegion(roll_image,image,image->columns-offset.x,image->rows-
1566 offset.y,0,0,offset.x,offset.y,exception);
1567 (void) SetImageProgress(image,RollImageTag,3,3);
1568 roll_image->type=image->type;
1569 if (status == MagickFalse)
1570 roll_image=DestroyImage(roll_image);
1575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1579 % S h a v e I m a g e %
1583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1585 % ShaveImage() shaves pixels from the image edges. It allocates the memory
1586 % necessary for the new Image structure and returns a pointer to the new
1589 % The format of the ShaveImage method is:
1591 % Image *ShaveImage(const Image *image,const RectangleInfo *shave_info,
1592 % ExceptionInfo *exception)
1594 % A description of each parameter follows:
1596 % o shave_image: Method ShaveImage returns a pointer to the shaved
1597 % image. A null image is returned if there is a memory shortage or
1598 % if the image width or height is zero.
1600 % o image: the image.
1602 % o shave_info: Specifies a pointer to a RectangleInfo which defines the
1603 % region of the image to crop.
1605 % o exception: return any errors or warnings in this structure.
1608 MagickExport Image *ShaveImage(const Image *image,
1609 const RectangleInfo *shave_info,ExceptionInfo *exception)
1617 assert(image != (const Image *) NULL);
1618 assert(image->signature == MagickSignature);
1619 if (image->debug != MagickFalse)
1620 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1621 if (((2*shave_info->width) >= image->columns) ||
1622 ((2*shave_info->height) >= image->rows))
1623 ThrowImageException(OptionWarning,"GeometryDoesNotContainImage");
1624 SetGeometry(image,&geometry);
1625 geometry.width-=2*shave_info->width;
1626 geometry.height-=2*shave_info->height;
1627 geometry.x=(ssize_t) shave_info->width+image->page.x;
1628 geometry.y=(ssize_t) shave_info->height+image->page.y;
1629 shave_image=CropImage(image,&geometry,exception);
1630 if (shave_image == (Image *) NULL)
1631 return((Image *) NULL);
1632 shave_image->page.width-=2*shave_info->width;
1633 shave_image->page.height-=2*shave_info->height;
1634 shave_image->page.x-=(ssize_t) shave_info->width;
1635 shave_image->page.y-=(ssize_t) shave_info->height;
1636 return(shave_image);
1640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1644 % S p l i c e I m a g e %
1648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1650 % SpliceImage() splices a solid color into the image as defined by the
1653 % The format of the SpliceImage method is:
1655 % Image *SpliceImage(const Image *image,const RectangleInfo *geometry,
1656 % ExceptionInfo *exception)
1658 % A description of each parameter follows:
1660 % o image: the image.
1662 % o geometry: Define the region of the image to splice with members
1663 % x, y, width, and height.
1665 % o exception: return any errors or warnings in this structure.
1668 MagickExport Image *SpliceImage(const Image *image,
1669 const RectangleInfo *geometry,ExceptionInfo *exception)
1671 #define SpliceImageTag "Splice/Image"
1693 Allocate splice image.
1695 assert(image != (const Image *) NULL);
1696 assert(image->signature == MagickSignature);
1697 if (image->debug != MagickFalse)
1698 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1699 assert(geometry != (const RectangleInfo *) NULL);
1700 assert(exception != (ExceptionInfo *) NULL);
1701 assert(exception->signature == MagickSignature);
1702 splice_geometry=(*geometry);
1703 splice_image=CloneImage(image,image->columns+splice_geometry.width,
1704 image->rows+splice_geometry.height,MagickTrue,exception);
1705 if (splice_image == (Image *) NULL)
1706 return((Image *) NULL);
1707 if (SetImageStorageClass(splice_image,DirectClass,exception) == MagickFalse)
1709 splice_image=DestroyImage(splice_image);
1710 return((Image *) NULL);
1712 (void) SetImageBackgroundColor(splice_image);
1714 Respect image geometry.
1716 switch (image->gravity)
1719 case UndefinedGravity:
1720 case NorthWestGravity:
1724 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1727 case NorthEastGravity:
1729 splice_geometry.x+=(ssize_t) splice_geometry.width;
1734 splice_geometry.y+=(ssize_t) splice_geometry.width/2;
1740 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1741 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1746 splice_geometry.x+=(ssize_t) splice_geometry.width;
1747 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1750 case SouthWestGravity:
1752 splice_geometry.y+=(ssize_t) splice_geometry.height;
1757 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1758 splice_geometry.y+=(ssize_t) splice_geometry.height;
1761 case SouthEastGravity:
1763 splice_geometry.x+=(ssize_t) splice_geometry.width;
1764 splice_geometry.y+=(ssize_t) splice_geometry.height;
1773 image_view=AcquireCacheView(image);
1774 splice_view=AcquireCacheView(splice_image);
1775 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1776 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1778 for (y=0; y < (ssize_t) splice_geometry.y; y++)
1780 register const Quantum
1789 if (status == MagickFalse)
1791 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1792 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1794 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1799 for (x=0; x < splice_geometry.x; x++)
1804 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1813 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1814 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1815 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1816 if ((traits == UndefinedPixelTrait) ||
1817 (splice_traits == UndefinedPixelTrait))
1821 p+=GetPixelChannels(image);
1822 q+=GetPixelChannels(splice_image);
1824 for ( ; x < (ssize_t) (splice_geometry.x+splice_geometry.width); x++)
1825 q+=GetPixelChannels(splice_image);
1826 for ( ; x < (ssize_t) splice_image->columns; x++)
1831 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1840 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1841 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1842 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1843 if ((traits == UndefinedPixelTrait) ||
1844 (splice_traits == UndefinedPixelTrait))
1848 p+=GetPixelChannels(image);
1849 q+=GetPixelChannels(splice_image);
1851 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
1853 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1858 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1859 #pragma omp critical (MagickCore_TransposeImage)
1861 proceed=SetImageProgress(image,SpliceImageTag,progress++,
1862 splice_image->rows);
1863 if (proceed == MagickFalse)
1867 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1868 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1870 for (y=(ssize_t) (splice_geometry.y+splice_geometry.height);
1871 y < (ssize_t) splice_image->rows; y++)
1873 register const Quantum
1882 if (status == MagickFalse)
1884 p=GetCacheViewVirtualPixels(image_view,0,y-(ssize_t) splice_geometry.height,
1885 image->columns,1,exception);
1886 if ((y < 0) || (y >= (ssize_t) splice_image->rows))
1888 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1890 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1895 for (x=0; x < splice_geometry.x; x++)
1900 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1909 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1910 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1911 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1912 if ((traits == UndefinedPixelTrait) ||
1913 (splice_traits == UndefinedPixelTrait))
1917 p+=GetPixelChannels(image);
1918 q+=GetPixelChannels(splice_image);
1920 for ( ; x < (ssize_t) (splice_geometry.x+splice_geometry.width); x++)
1921 q+=GetPixelChannels(splice_image);
1922 for ( ; x < (ssize_t) splice_image->columns; x++)
1927 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1936 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1937 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1938 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1939 if ((traits == UndefinedPixelTrait) ||
1940 (splice_traits == UndefinedPixelTrait))
1944 p+=GetPixelChannels(image);
1945 q+=GetPixelChannels(splice_image);
1947 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
1949 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1954 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1955 #pragma omp critical (MagickCore_TransposeImage)
1957 proceed=SetImageProgress(image,SpliceImageTag,progress++,
1958 splice_image->rows);
1959 if (proceed == MagickFalse)
1963 splice_view=DestroyCacheView(splice_view);
1964 image_view=DestroyCacheView(image_view);
1965 if (status == MagickFalse)
1966 splice_image=DestroyImage(splice_image);
1967 return(splice_image);
1971 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1975 % T r a n s f o r m I m a g e %
1979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1981 % TransformImage() is a convenience method that behaves like ResizeImage() or
1982 % CropImage() but accepts scaling and/or cropping information as a region
1983 % geometry specification. If the operation fails, the original image handle
1986 % This should only be used for single images.
1988 % This function destroys what it assumes to be a single image list.
1989 % If the input image is part of a larger list, all other images in that list
1990 % will be simply 'lost', not destroyed.
1992 % Also if the crop generates a list of images only the first image is resized.
1993 % And finally if the crop succeeds and the resize failed, you will get a
1994 % cropped image, as well as a 'false' or 'failed' report.
1996 % This function and should probably be depreciated in favor of direct calls
1997 % to CropImageToTiles() or ResizeImage(), as appropriate.
1999 % The format of the TransformImage method is:
2001 % MagickBooleanType TransformImage(Image **image,const char *crop_geometry,
2002 % const char *image_geometry)
2004 % A description of each parameter follows:
2006 % o image: the image The transformed image is returned as this parameter.
2008 % o crop_geometry: A crop geometry string. This geometry defines a
2009 % subregion of the image to crop.
2011 % o image_geometry: An image geometry string. This geometry defines the
2012 % final size of the image.
2015 MagickExport MagickBooleanType TransformImage(Image **image,
2016 const char *crop_geometry,const char *image_geometry)
2028 assert(image != (Image **) NULL);
2029 assert((*image)->signature == MagickSignature);
2030 if ((*image)->debug != MagickFalse)
2031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
2032 transform_image=(*image);
2033 if (crop_geometry != (const char *) NULL)
2039 Crop image to a user specified size.
2041 crop_image=CropImageToTiles(*image,crop_geometry,&(*image)->exception);
2042 if (crop_image == (Image *) NULL)
2043 transform_image=CloneImage(*image,0,0,MagickTrue,&(*image)->exception);
2046 transform_image=DestroyImage(transform_image);
2047 transform_image=GetFirstImageInList(crop_image);
2049 *image=transform_image;
2051 if (image_geometry == (const char *) NULL)
2055 Scale image to a user specified size.
2057 flags=ParseRegionGeometry(transform_image,image_geometry,&geometry,
2058 &(*image)->exception);
2060 if ((transform_image->columns == geometry.width) &&
2061 (transform_image->rows == geometry.height))
2063 resize_image=ResizeImage(transform_image,geometry.width,geometry.height,
2064 transform_image->filter,transform_image->blur,&(*image)->exception);
2065 if (resize_image == (Image *) NULL)
2066 return(MagickFalse);
2067 transform_image=DestroyImage(transform_image);
2068 transform_image=resize_image;
2069 *image=transform_image;
2074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2078 % T r a n s f o r m I m a g e s %
2082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2084 % TransformImages() calls TransformImage() on each image of a sequence.
2086 % The format of the TransformImage method is:
2088 % MagickBooleanType TransformImages(Image **image,
2089 % const char *crop_geometry,const char *image_geometry)
2091 % A description of each parameter follows:
2093 % o image: the image The transformed image is returned as this parameter.
2095 % o crop_geometry: A crop geometry string. This geometry defines a
2096 % subregion of the image to crop.
2098 % o image_geometry: An image geometry string. This geometry defines the
2099 % final size of the image.
2102 MagickExport MagickBooleanType TransformImages(Image **images,
2103 const char *crop_geometry,const char *image_geometry)
2116 assert(images != (Image **) NULL);
2117 assert((*images)->signature == MagickSignature);
2118 if ((*images)->debug != MagickFalse)
2119 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2120 (*images)->filename);
2121 image_list=ImageListToArray(*images,&(*images)->exception);
2122 if (image_list == (Image **) NULL)
2123 return(MagickFalse);
2125 transform_images=NewImageList();
2126 for (i=0; image_list[i] != (Image *) NULL; i++)
2128 image=image_list[i];
2129 status|=TransformImage(&image,crop_geometry,image_geometry);
2130 AppendImageToList(&transform_images,image);
2132 *images=transform_images;
2133 image_list=(Image **) RelinquishMagickMemory(image_list);
2134 return(status != 0 ? MagickTrue : MagickFalse);
2138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2142 % T r a n s p o s e I m a g e %
2146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2148 % TransposeImage() creates a horizontal mirror image by reflecting the pixels
2149 % around the central y-axis while rotating them by 90 degrees.
2151 % The format of the TransposeImage method is:
2153 % Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2155 % A description of each parameter follows:
2157 % o image: the image.
2159 % o exception: return any errors or warnings in this structure.
2162 MagickExport Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2164 #define TransposeImageTag "Transpose/Image"
2185 assert(image != (const Image *) NULL);
2186 assert(image->signature == MagickSignature);
2187 if (image->debug != MagickFalse)
2188 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2189 assert(exception != (ExceptionInfo *) NULL);
2190 assert(exception->signature == MagickSignature);
2191 transpose_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2193 if (transpose_image == (Image *) NULL)
2194 return((Image *) NULL);
2200 image_view=AcquireCacheView(image);
2201 transpose_view=AcquireCacheView(transpose_image);
2202 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2203 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2205 for (y=0; y < (ssize_t) image->rows; y++)
2207 register const Quantum
2216 if (status == MagickFalse)
2218 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-y-1,
2219 image->columns,1,exception);
2220 q=QueueCacheViewAuthenticPixels(transpose_view,(ssize_t) (image->rows-y-1),
2221 0,1,transpose_image->rows,exception);
2222 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2227 for (x=0; x < (ssize_t) image->columns; x++)
2232 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2241 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2242 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
2243 transpose_traits=GetPixelChannelMapTraits(transpose_image,channel);
2244 if ((traits == UndefinedPixelTrait) ||
2245 (transpose_traits == UndefinedPixelTrait))
2249 p+=GetPixelChannels(image);
2250 q+=GetPixelChannels(transpose_image);
2252 if (SyncCacheViewAuthenticPixels(transpose_view,exception) == MagickFalse)
2254 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2259 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2260 #pragma omp critical (MagickCore_TransposeImage)
2262 proceed=SetImageProgress(image,TransposeImageTag,progress++,
2264 if (proceed == MagickFalse)
2268 transpose_view=DestroyCacheView(transpose_view);
2269 image_view=DestroyCacheView(image_view);
2270 transpose_image->type=image->type;
2271 page=transpose_image->page;
2272 Swap(page.width,page.height);
2273 Swap(page.x,page.y);
2274 transpose_image->page=page;
2275 if (status == MagickFalse)
2276 transpose_image=DestroyImage(transpose_image);
2277 return(transpose_image);
2281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2285 % T r a n s v e r s e I m a g e %
2289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2291 % TransverseImage() creates a vertical mirror image by reflecting the pixels
2292 % around the central x-axis while rotating them by 270 degrees.
2294 % The format of the TransverseImage method is:
2296 % Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2298 % A description of each parameter follows:
2300 % o image: the image.
2302 % o exception: return any errors or warnings in this structure.
2305 MagickExport Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2307 #define TransverseImageTag "Transverse/Image"
2328 assert(image != (const Image *) NULL);
2329 assert(image->signature == MagickSignature);
2330 if (image->debug != MagickFalse)
2331 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2332 assert(exception != (ExceptionInfo *) NULL);
2333 assert(exception->signature == MagickSignature);
2334 transverse_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2336 if (transverse_image == (Image *) NULL)
2337 return((Image *) NULL);
2343 image_view=AcquireCacheView(image);
2344 transverse_view=AcquireCacheView(transverse_image);
2345 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2346 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2348 for (y=0; y < (ssize_t) image->rows; y++)
2353 register const Quantum
2362 if (status == MagickFalse)
2364 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2365 q=QueueCacheViewAuthenticPixels(transverse_view,(ssize_t) (image->rows-y-1),
2366 0,1,transverse_image->rows,exception);
2367 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2372 q+=GetPixelChannels(transverse_image)*image->columns;
2373 for (x=0; x < (ssize_t) image->columns; x++)
2378 q-=GetPixelChannels(transverse_image);
2379 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2388 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2389 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
2390 transverse_traits=GetPixelChannelMapTraits(transverse_image,channel);
2391 if ((traits == UndefinedPixelTrait) ||
2392 (transverse_traits == UndefinedPixelTrait))
2396 p+=GetPixelChannels(image);
2398 sync=SyncCacheViewAuthenticPixels(transverse_view,exception);
2399 if (sync == MagickFalse)
2401 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2406 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2407 #pragma omp critical (MagickCore_TransverseImage)
2409 proceed=SetImageProgress(image,TransverseImageTag,progress++,
2411 if (proceed == MagickFalse)
2415 transverse_view=DestroyCacheView(transverse_view);
2416 image_view=DestroyCacheView(image_view);
2417 transverse_image->type=image->type;
2418 page=transverse_image->page;
2419 Swap(page.width,page.height);
2420 Swap(page.x,page.y);
2421 if (page.width != 0)
2422 page.x=(ssize_t) (page.width-transverse_image->columns-page.x);
2423 if (page.height != 0)
2424 page.y=(ssize_t) (page.height-transverse_image->rows-page.y);
2425 transverse_image->page=page;
2426 if (status == MagickFalse)
2427 transverse_image=DestroyImage(transverse_image);
2428 return(transverse_image);
2432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2436 % T r i m I m a g e %
2440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2442 % TrimImage() trims pixels from the image edges. It allocates the memory
2443 % necessary for the new Image structure and returns a pointer to the new
2446 % The format of the TrimImage method is:
2448 % Image *TrimImage(const Image *image,ExceptionInfo *exception)
2450 % A description of each parameter follows:
2452 % o image: the image.
2454 % o exception: return any errors or warnings in this structure.
2457 MagickExport Image *TrimImage(const Image *image,ExceptionInfo *exception)
2462 assert(image != (const Image *) NULL);
2463 assert(image->signature == MagickSignature);
2464 if (image->debug != MagickFalse)
2465 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2466 geometry=GetImageBoundingBox(image,exception);
2467 if ((geometry.width == 0) || (geometry.height == 0))
2472 crop_image=CloneImage(image,1,1,MagickTrue,exception);
2473 if (crop_image == (Image *) NULL)
2474 return((Image *) NULL);
2475 crop_image->background_color.alpha=(Quantum) TransparentAlpha;
2476 (void) SetImageBackgroundColor(crop_image);
2477 crop_image->page=image->page;
2478 crop_image->page.x=(-1);
2479 crop_image->page.y=(-1);
2482 geometry.x+=image->page.x;
2483 geometry.y+=image->page.y;
2484 return(CropImage(image,&geometry,exception));