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-2012 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=AcquireVirtualCacheView(image,exception);
161 chop_view=AcquireAuthenticCacheView(chop_image,exception);
162 #if defined(MAGICKCORE_OPENMP_SUPPORT)
163 #pragma omp parallel for schedule(static) shared(progress,status)
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)))
193 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
202 channel=GetPixelChannelMapChannel(image,i);
203 traits=GetPixelChannelMapTraits(image,channel);
204 chop_traits=GetPixelChannelMapTraits(chop_image,channel);
205 if ((traits == UndefinedPixelTrait) ||
206 (chop_traits == UndefinedPixelTrait))
208 SetPixelChannel(chop_image,channel,p[i],q);
210 q+=GetPixelChannels(chop_image);
212 p+=GetPixelChannels(image);
214 if (SyncCacheViewAuthenticPixels(chop_view,exception) == MagickFalse)
216 if (image->progress_monitor != (MagickProgressMonitor) NULL)
221 #if defined(MAGICKCORE_OPENMP_SUPPORT)
222 #pragma omp critical (MagickCore_ChopImage)
224 proceed=SetImageProgress(image,ChopImageTag,progress++,image->rows);
225 if (proceed == MagickFalse)
232 #if defined(MAGICKCORE_OPENMP_SUPPORT)
233 #pragma omp parallel for schedule(static) shared(progress,status)
235 for (y=0; y < (ssize_t) (image->rows-(extent.y+extent.height)); y++)
237 register const Quantum
246 if (status == MagickFalse)
248 p=GetCacheViewVirtualPixels(image_view,0,extent.y+extent.height+y,
249 image->columns,1,exception);
250 q=QueueCacheViewAuthenticPixels(chop_view,0,extent.y+y,chop_image->columns,
252 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
257 for (x=0; x < (ssize_t) image->columns; x++)
259 if ((x < extent.x) || (x >= (ssize_t) (extent.x+extent.width)))
264 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
273 channel=GetPixelChannelMapChannel(image,i);
274 traits=GetPixelChannelMapTraits(image,channel);
275 chop_traits=GetPixelChannelMapTraits(chop_image,channel);
276 if ((traits == UndefinedPixelTrait) ||
277 (chop_traits == UndefinedPixelTrait))
279 SetPixelChannel(chop_image,channel,p[i],q);
281 q+=GetPixelChannels(chop_image);
283 p+=GetPixelChannels(image);
285 if (SyncCacheViewAuthenticPixels(chop_view,exception) == MagickFalse)
287 if (image->progress_monitor != (MagickProgressMonitor) NULL)
292 #if defined(MAGICKCORE_OPENMP_SUPPORT)
293 #pragma omp critical (MagickCore_ChopImage)
295 proceed=SetImageProgress(image,ChopImageTag,progress++,image->rows);
296 if (proceed == MagickFalse)
300 chop_view=DestroyCacheView(chop_view);
301 image_view=DestroyCacheView(image_view);
302 chop_image->type=image->type;
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 + C o n s o l i d a t e C M Y K I m a g e %
315 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 % ConsolidateCMYKImage() consolidates separate C, M, Y, and K planes into a
320 % The format of the ConsolidateCMYKImage method is:
322 % Image *ConsolidateCMYKImage(const Image *image,ExceptionInfo *exception)
324 % A description of each parameter follows:
326 % o image: the image sequence.
328 % o exception: return any errors or warnings in this structure.
331 MagickExport Image *ConsolidateCMYKImages(const Image *images,
332 ExceptionInfo *exception)
349 Consolidate separate C, M, Y, and K planes into a single image.
351 assert(images != (Image *) NULL);
352 assert(images->signature == MagickSignature);
353 if (images->debug != MagickFalse)
354 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
355 assert(exception != (ExceptionInfo *) NULL);
356 assert(exception->signature == MagickSignature);
357 cmyk_images=NewImageList();
358 for (j=0; j < (ssize_t) GetImageListLength(images); j+=4)
363 cmyk_image=CloneImage(images,images->columns,images->rows,MagickTrue,
365 if (cmyk_image == (Image *) NULL)
367 if (SetImageStorageClass(cmyk_image,DirectClass,exception) == MagickFalse)
369 (void) SetImageColorspace(cmyk_image,CMYKColorspace,exception);
370 for (i=0; i < 4; i++)
372 image_view=AcquireVirtualCacheView(images,exception);
373 cmyk_view=AcquireAuthenticCacheView(cmyk_image,exception);
374 for (y=0; y < (ssize_t) images->rows; y++)
376 register const Quantum
385 p=GetCacheViewVirtualPixels(image_view,0,y,images->columns,1,exception);
386 q=QueueCacheViewAuthenticPixels(cmyk_view,0,y,cmyk_image->columns,1,
388 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
390 for (x=0; x < (ssize_t) images->columns; x++)
395 pixel=QuantumRange-GetPixelIntensity(images,p);
398 case 0: SetPixelCyan(cmyk_image,pixel,q); break;
399 case 1: SetPixelMagenta(cmyk_image,pixel,q); break;
400 case 2: SetPixelYellow(cmyk_image,pixel,q); break;
401 case 3: SetPixelBlack(cmyk_image,pixel,q); break;
404 p+=GetPixelChannels(images);
405 q+=GetPixelChannels(cmyk_image);
407 if (SyncCacheViewAuthenticPixels(cmyk_view,exception) == MagickFalse)
410 cmyk_view=DestroyCacheView(cmyk_view);
411 image_view=DestroyCacheView(image_view);
412 images=GetNextImageInList(images);
413 if (images == (Image *) NULL)
416 AppendImageToList(&cmyk_images,cmyk_image);
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 % C r o p I m a g e %
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432 % CropImage() extracts a region of the image starting at the offset defined
433 % by geometry. Region must be fully defined, and no special handling of
434 % geometry flags is performed.
436 % The format of the CropImage method is:
438 % Image *CropImage(const Image *image,const RectangleInfo *geometry,
439 % ExceptionInfo *exception)
441 % A description of each parameter follows:
443 % o image: the image.
445 % o geometry: Define the region of the image to crop with members
446 % x, y, width, and height.
448 % o exception: return any errors or warnings in this structure.
451 MagickExport Image *CropImage(const Image *image,const RectangleInfo *geometry,
452 ExceptionInfo *exception)
454 #define CropImageTag "Crop/Image"
482 assert(image != (const Image *) NULL);
483 assert(image->signature == MagickSignature);
484 if (image->debug != MagickFalse)
485 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
486 assert(geometry != (const RectangleInfo *) NULL);
487 assert(exception != (ExceptionInfo *) NULL);
488 assert(exception->signature == MagickSignature);
489 bounding_box=image->page;
490 if ((bounding_box.width == 0) || (bounding_box.height == 0))
492 bounding_box.width=image->columns;
493 bounding_box.height=image->rows;
497 page.width=bounding_box.width;
498 if (page.height == 0)
499 page.height=bounding_box.height;
500 if (((bounding_box.x-page.x) >= (ssize_t) page.width) ||
501 ((bounding_box.y-page.y) >= (ssize_t) page.height) ||
502 ((page.x-bounding_box.x) > (ssize_t) image->columns) ||
503 ((page.y-bounding_box.y) > (ssize_t) image->rows))
506 Crop is not within virtual canvas, return 1 pixel transparent image.
508 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
509 "GeometryDoesNotContainImage","'%s'",image->filename);
510 crop_image=CloneImage(image,1,1,MagickTrue,exception);
511 if (crop_image == (Image *) NULL)
512 return((Image *) NULL);
513 crop_image->background_color.alpha=(Quantum) TransparentAlpha;
514 (void) SetImageBackgroundColor(crop_image,exception);
515 crop_image->page=bounding_box;
516 crop_image->page.x=(-1);
517 crop_image->page.y=(-1);
518 if (crop_image->dispose == BackgroundDispose)
519 crop_image->dispose=NoneDispose;
522 if ((page.x < 0) && (bounding_box.x >= 0))
524 page.width+=page.x-bounding_box.x;
529 page.width-=bounding_box.x-page.x;
530 page.x-=bounding_box.x;
534 if ((page.y < 0) && (bounding_box.y >= 0))
536 page.height+=page.y-bounding_box.y;
541 page.height-=bounding_box.y-page.y;
542 page.y-=bounding_box.y;
546 if ((size_t) (page.x+page.width) > image->columns)
547 page.width=image->columns-page.x;
548 if ((geometry->width != 0) && (page.width > geometry->width))
549 page.width=geometry->width;
550 if ((size_t) (page.y+page.height) > image->rows)
551 page.height=image->rows-page.y;
552 if ((geometry->height != 0) && (page.height > geometry->height))
553 page.height=geometry->height;
554 bounding_box.x+=page.x;
555 bounding_box.y+=page.y;
556 if ((page.width == 0) || (page.height == 0))
558 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
559 "GeometryDoesNotContainImage","'%s'",image->filename);
560 return((Image *) NULL);
563 Initialize crop image attributes.
565 crop_image=CloneImage(image,page.width,page.height,MagickTrue,exception);
566 if (crop_image == (Image *) NULL)
567 return((Image *) NULL);
568 crop_image->page.width=image->page.width;
569 crop_image->page.height=image->page.height;
570 offset.x=(ssize_t) (bounding_box.x+bounding_box.width);
571 offset.y=(ssize_t) (bounding_box.y+bounding_box.height);
572 if ((offset.x > (ssize_t) image->page.width) ||
573 (offset.y > (ssize_t) image->page.height))
575 crop_image->page.width=bounding_box.width;
576 crop_image->page.height=bounding_box.height;
578 crop_image->page.x=bounding_box.x;
579 crop_image->page.y=bounding_box.y;
585 image_view=AcquireVirtualCacheView(image,exception);
586 crop_view=AcquireAuthenticCacheView(crop_image,exception);
587 #if defined(MAGICKCORE_OPENMP_SUPPORT)
588 #pragma omp parallel for schedule(static) shared(progress,status)
590 for (y=0; y < (ssize_t) crop_image->rows; y++)
592 register const Quantum
601 if (status == MagickFalse)
603 p=GetCacheViewVirtualPixels(image_view,page.x,page.y+y,crop_image->columns,
605 q=QueueCacheViewAuthenticPixels(crop_view,0,y,crop_image->columns,1,
607 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
612 for (x=0; x < (ssize_t) crop_image->columns; x++)
617 if (GetPixelMask(image,p) != 0)
619 p+=GetPixelChannels(image);
620 q+=GetPixelChannels(crop_image);
623 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
632 channel=GetPixelChannelMapChannel(image,i);
633 traits=GetPixelChannelMapTraits(image,channel);
634 crop_traits=GetPixelChannelMapTraits(crop_image,channel);
635 if ((traits == UndefinedPixelTrait) ||
636 (crop_traits == UndefinedPixelTrait))
638 SetPixelChannel(crop_image,channel,p[i],q);
640 p+=GetPixelChannels(image);
641 q+=GetPixelChannels(crop_image);
643 if (SyncCacheViewAuthenticPixels(crop_view,exception) == MagickFalse)
645 if (image->progress_monitor != (MagickProgressMonitor) NULL)
650 #if defined(MAGICKCORE_OPENMP_SUPPORT)
651 #pragma omp critical (MagickCore_CropImage)
653 proceed=SetImageProgress(image,CropImageTag,progress++,image->rows);
654 if (proceed == MagickFalse)
658 crop_view=DestroyCacheView(crop_view);
659 image_view=DestroyCacheView(image_view);
660 crop_image->type=image->type;
661 if (status == MagickFalse)
662 crop_image=DestroyImage(crop_image);
667 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671 % C r o p I m a g e T o T i l e s %
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 % CropImageToTiles() crops a single image, into a possible list of tiles.
678 % This may include a single sub-region of the image. This basically applies
679 % all the normal geometry flags for Crop.
681 % Image *CropImageToTiles(const Image *image,
682 % const RectangleInfo *crop_geometry, ExceptionInfo *exception)
684 % A description of each parameter follows:
686 % o image: the image The transformed image is returned as this parameter.
688 % o crop_geometry: A crop geometry string.
690 % o exception: return any errors or warnings in this structure.
694 static inline ssize_t MagickRound(MagickRealType x)
697 Round the fraction to nearest integer.
700 return((ssize_t) (x+0.5));
701 return((ssize_t) (x-0.5));
704 MagickExport Image *CropImageToTiles(const Image *image,
705 const char *crop_geometry,ExceptionInfo *exception)
717 assert(image != (Image *) NULL);
718 assert(image->signature == MagickSignature);
719 if (image->debug != MagickFalse)
720 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
721 crop_image=NewImageList();
723 flags=ParseGravityGeometry(image,crop_geometry,&geometry,exception);
724 if ((flags & AreaValue) != 0)
738 Crop into NxM tiles (@ flag).
740 width=image->columns;
742 if (geometry.width == 0)
744 if (geometry.height == 0)
746 if ((flags & AspectValue) == 0)
748 width-=(geometry.x < 0 ? -1 : 1)*geometry.x;
749 height-=(geometry.y < 0 ? -1 : 1)*geometry.y;
753 width+=(geometry.x < 0 ? -1 : 1)*geometry.x;
754 height+=(geometry.y < 0 ? -1 : 1)*geometry.y;
756 delta.x=(double) width/geometry.width;
757 delta.y=(double) height/geometry.height;
762 for (offset.y=0; offset.y < (double) height; )
764 if ((flags & AspectValue) == 0)
766 crop.y=(ssize_t) MagickRound((MagickRealType) (offset.y-
767 (geometry.y > 0 ? 0 : geometry.y)));
768 offset.y+=delta.y; /* increment now to find width */
769 crop.height=(size_t) MagickRound((MagickRealType) (offset.y+
770 (geometry.y < 0 ? 0 : geometry.y)));
774 crop.y=(ssize_t) MagickRound((MagickRealType) (offset.y-
775 (geometry.y > 0 ? geometry.y : 0)));
776 offset.y+=delta.y; /* increment now to find width */
777 crop.height=(size_t) MagickRound((MagickRealType)
778 (offset.y+(geometry.y < -1 ? geometry.y : 0)));
781 crop.y+=image->page.y;
782 for (offset.x=0; offset.x < (double) width; )
784 if ((flags & AspectValue) == 0)
786 crop.x=(ssize_t) MagickRound((MagickRealType) (offset.x-
787 (geometry.x > 0 ? 0 : geometry.x)));
788 offset.x+=delta.x; /* increment now to find height */
789 crop.width=(size_t) MagickRound((MagickRealType) (offset.x+
790 (geometry.x < 0 ? 0 : geometry.x)));
794 crop.x=(ssize_t) MagickRound((MagickRealType) (offset.x-
795 (geometry.x > 0 ? geometry.x : 0)));
796 offset.x+=delta.x; /* increment now to find height */
797 crop.width=(size_t) MagickRound((MagickRealType) (offset.x+
798 (geometry.x < 0 ? geometry.x : 0)));
801 crop.x+=image->page.x;
802 next=CropImage(image,&crop,exception);
803 if (next == (Image *) NULL)
805 AppendImageToList(&crop_image,next);
807 if (next == (Image *) NULL)
810 ClearMagickException(exception);
813 if (((geometry.width == 0) && (geometry.height == 0)) ||
814 ((flags & XValue) != 0) || ((flags & YValue) != 0))
817 Crop a single region at +X+Y.
819 crop_image=CropImage(image,&geometry,exception);
820 if ((crop_image != (Image *) NULL) && ((flags & AspectValue) != 0))
822 crop_image->page.width=geometry.width;
823 crop_image->page.height=geometry.height;
824 crop_image->page.x-=geometry.x;
825 crop_image->page.y-=geometry.y;
829 if ((image->columns > geometry.width) || (image->rows > geometry.height))
843 Crop into tiles of fixed size WxH.
847 page.width=image->columns;
848 if (page.height == 0)
849 page.height=image->rows;
850 width=geometry.width;
853 height=geometry.height;
857 for (y=0; y < (ssize_t) page.height; y+=(ssize_t) height)
859 for (x=0; x < (ssize_t) page.width; x+=(ssize_t) width)
861 geometry.width=width;
862 geometry.height=height;
865 next=CropImage(image,&geometry,exception);
866 if (next == (Image *) NULL)
868 AppendImageToList(&crop_image,next);
870 if (next == (Image *) NULL)
875 return(CloneImage(image,0,0,MagickTrue,exception));
879 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
883 % E x c e r p t I m a g e %
887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889 % ExcerptImage() returns a excerpt of the image as defined by the geometry.
891 % The format of the ExcerptImage method is:
893 % Image *ExcerptImage(const Image *image,const RectangleInfo *geometry,
894 % ExceptionInfo *exception)
896 % A description of each parameter follows:
898 % o image: the image.
900 % o geometry: Define the region of the image to extend with members
901 % x, y, width, and height.
903 % o exception: return any errors or warnings in this structure.
906 MagickExport Image *ExcerptImage(const Image *image,
907 const RectangleInfo *geometry,ExceptionInfo *exception)
909 #define ExcerptImageTag "Excerpt/Image"
928 Allocate excerpt image.
930 assert(image != (const Image *) NULL);
931 assert(image->signature == MagickSignature);
932 if (image->debug != MagickFalse)
933 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
934 assert(geometry != (const RectangleInfo *) NULL);
935 assert(exception != (ExceptionInfo *) NULL);
936 assert(exception->signature == MagickSignature);
937 excerpt_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
939 if (excerpt_image == (Image *) NULL)
940 return((Image *) NULL);
946 image_view=AcquireVirtualCacheView(image,exception);
947 excerpt_view=AcquireAuthenticCacheView(excerpt_image,exception);
948 #if defined(MAGICKCORE_OPENMP_SUPPORT)
949 #pragma omp parallel for schedule(static,4) shared(progress,status)
951 for (y=0; y < (ssize_t) excerpt_image->rows; y++)
953 register const Quantum
962 if (status == MagickFalse)
964 p=GetCacheViewVirtualPixels(image_view,geometry->x,geometry->y+y,
965 geometry->width,1,exception);
966 q=GetCacheViewAuthenticPixels(excerpt_view,0,y,excerpt_image->columns,1,
968 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
973 for (x=0; x < (ssize_t) excerpt_image->columns; x++)
978 if (GetPixelMask(image,p) != 0)
980 p+=GetPixelChannels(image);
981 q+=GetPixelChannels(excerpt_image);
984 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
993 channel=GetPixelChannelMapChannel(image,i);
994 traits=GetPixelChannelMapTraits(image,channel);
995 excerpt_traits=GetPixelChannelMapTraits(excerpt_image,channel);
996 if ((traits == UndefinedPixelTrait) ||
997 (excerpt_traits == UndefinedPixelTrait))
999 SetPixelChannel(excerpt_image,channel,p[i],q);
1001 p+=GetPixelChannels(image);
1002 q+=GetPixelChannels(excerpt_image);
1004 if (SyncCacheViewAuthenticPixels(excerpt_view,exception) == MagickFalse)
1006 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1011 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1012 #pragma omp critical (MagickCore_ExcerptImage)
1014 proceed=SetImageProgress(image,ExcerptImageTag,progress++,image->rows);
1015 if (proceed == MagickFalse)
1019 excerpt_view=DestroyCacheView(excerpt_view);
1020 image_view=DestroyCacheView(image_view);
1021 excerpt_image->type=image->type;
1022 if (status == MagickFalse)
1023 excerpt_image=DestroyImage(excerpt_image);
1024 return(excerpt_image);
1028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1032 % E x t e n t I m a g e %
1036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038 % ExtentImage() extends the image as defined by the geometry, gravity, and
1039 % image background color. Set the (x,y) offset of the geometry to move the
1040 % original image relative to the extended image.
1042 % The format of the ExtentImage method is:
1044 % Image *ExtentImage(const Image *image,const RectangleInfo *geometry,
1045 % ExceptionInfo *exception)
1047 % A description of each parameter follows:
1049 % o image: the image.
1051 % o geometry: Define the region of the image to extend with members
1052 % x, y, width, and height.
1054 % o exception: return any errors or warnings in this structure.
1057 MagickExport Image *ExtentImage(const Image *image,
1058 const RectangleInfo *geometry,ExceptionInfo *exception)
1064 Allocate extent image.
1066 assert(image != (const Image *) NULL);
1067 assert(image->signature == MagickSignature);
1068 if (image->debug != MagickFalse)
1069 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1070 assert(geometry != (const RectangleInfo *) NULL);
1071 assert(exception != (ExceptionInfo *) NULL);
1072 assert(exception->signature == MagickSignature);
1073 extent_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
1075 if (extent_image == (Image *) NULL)
1076 return((Image *) NULL);
1077 if (SetImageStorageClass(extent_image,DirectClass,exception) == MagickFalse)
1079 extent_image=DestroyImage(extent_image);
1080 return((Image *) NULL);
1082 if (extent_image->background_color.alpha != OpaqueAlpha)
1083 extent_image->matte=MagickTrue;
1084 (void) SetImageBackgroundColor(extent_image,exception);
1085 (void) CompositeImage(extent_image,image,image->compose,MagickTrue,
1086 -geometry->x,-geometry->y,exception);
1087 return(extent_image);
1091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1095 % F l i p I m a g e %
1099 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1101 % FlipImage() creates a vertical mirror image by reflecting the pixels
1102 % around the central x-axis.
1104 % The format of the FlipImage method is:
1106 % Image *FlipImage(const Image *image,ExceptionInfo *exception)
1108 % A description of each parameter follows:
1110 % o image: the image.
1112 % o exception: return any errors or warnings in this structure.
1115 MagickExport Image *FlipImage(const Image *image,ExceptionInfo *exception)
1117 #define FlipImageTag "Flip/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(exception != (ExceptionInfo *) NULL);
1143 assert(exception->signature == MagickSignature);
1144 flip_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1145 if (flip_image == (Image *) NULL)
1146 return((Image *) NULL);
1153 image_view=AcquireVirtualCacheView(image,exception);
1154 flip_view=AcquireAuthenticCacheView(flip_image,exception);
1155 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1156 #pragma omp parallel for schedule(static) shared(progress,status)
1158 for (y=0; y < (ssize_t) flip_image->rows; y++)
1160 register const Quantum
1169 if (status == MagickFalse)
1171 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1172 q=QueueCacheViewAuthenticPixels(flip_view,0,(ssize_t) (flip_image->rows-y-
1173 1),flip_image->columns,1,exception);
1174 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1179 for (x=0; x < (ssize_t) flip_image->columns; x++)
1184 if (GetPixelMask(image,p) != 0)
1186 p+=GetPixelChannels(image);
1187 q+=GetPixelChannels(flip_image);
1190 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1199 channel=GetPixelChannelMapChannel(image,i);
1200 traits=GetPixelChannelMapTraits(image,channel);
1201 flip_traits=GetPixelChannelMapTraits(flip_image,channel);
1202 if ((traits == UndefinedPixelTrait) ||
1203 (flip_traits == UndefinedPixelTrait))
1205 SetPixelChannel(flip_image,channel,p[i],q);
1207 p+=GetPixelChannels(image);
1208 q+=GetPixelChannels(flip_image);
1210 if (SyncCacheViewAuthenticPixels(flip_view,exception) == MagickFalse)
1212 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1217 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1218 #pragma omp critical (MagickCore_FlipImage)
1220 proceed=SetImageProgress(image,FlipImageTag,progress++,image->rows);
1221 if (proceed == MagickFalse)
1225 flip_view=DestroyCacheView(flip_view);
1226 image_view=DestroyCacheView(image_view);
1227 flip_image->type=image->type;
1228 if (page.height != 0)
1229 page.y=(ssize_t) (page.height-flip_image->rows-page.y);
1230 flip_image->page=page;
1231 if (status == MagickFalse)
1232 flip_image=DestroyImage(flip_image);
1237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1241 % F l o p I m a g e %
1245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1247 % FlopImage() creates a horizontal mirror image by reflecting the pixels
1248 % around the central y-axis.
1250 % The format of the FlopImage method is:
1252 % Image *FlopImage(const Image *image,ExceptionInfo *exception)
1254 % A description of each parameter follows:
1256 % o image: the image.
1258 % o exception: return any errors or warnings in this structure.
1261 MagickExport Image *FlopImage(const Image *image,ExceptionInfo *exception)
1263 #define FlopImageTag "Flop/Image"
1284 assert(image != (const Image *) NULL);
1285 assert(image->signature == MagickSignature);
1286 if (image->debug != MagickFalse)
1287 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1288 assert(exception != (ExceptionInfo *) NULL);
1289 assert(exception->signature == MagickSignature);
1290 flop_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1291 if (flop_image == (Image *) NULL)
1292 return((Image *) NULL);
1299 image_view=AcquireVirtualCacheView(image,exception);
1300 flop_view=AcquireAuthenticCacheView(flop_image,exception);
1301 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1302 #pragma omp parallel for schedule(static) shared(progress,status)
1304 for (y=0; y < (ssize_t) flop_image->rows; y++)
1306 register const Quantum
1315 if (status == MagickFalse)
1317 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1318 q=QueueCacheViewAuthenticPixels(flop_view,0,y,flop_image->columns,1,
1320 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1325 q+=GetPixelChannels(flop_image)*flop_image->columns;
1326 for (x=0; x < (ssize_t) flop_image->columns; x++)
1331 q-=GetPixelChannels(flop_image);
1332 if (GetPixelMask(image,p) != 0)
1334 p+=GetPixelChannels(image);
1337 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1346 channel=GetPixelChannelMapChannel(image,i);
1347 traits=GetPixelChannelMapTraits(image,channel);
1348 flop_traits=GetPixelChannelMapTraits(flop_image,channel);
1349 if ((traits == UndefinedPixelTrait) ||
1350 (flop_traits == UndefinedPixelTrait))
1352 SetPixelChannel(flop_image,channel,p[i],q);
1354 p+=GetPixelChannels(image);
1356 if (SyncCacheViewAuthenticPixels(flop_view,exception) == MagickFalse)
1358 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1363 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1364 #pragma omp critical (MagickCore_FlopImage)
1366 proceed=SetImageProgress(image,FlopImageTag,progress++,image->rows);
1367 if (proceed == MagickFalse)
1371 flop_view=DestroyCacheView(flop_view);
1372 image_view=DestroyCacheView(image_view);
1373 flop_image->type=image->type;
1374 if (page.width != 0)
1375 page.x=(ssize_t) (page.width-flop_image->columns-page.x);
1376 flop_image->page=page;
1377 if (status == MagickFalse)
1378 flop_image=DestroyImage(flop_image);
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 % R o l l I m a g e %
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 % RollImage() offsets an image as defined by x_offset and y_offset.
1395 % The format of the RollImage method is:
1397 % Image *RollImage(const Image *image,const ssize_t x_offset,
1398 % const ssize_t y_offset,ExceptionInfo *exception)
1400 % A description of each parameter follows:
1402 % o image: the image.
1404 % o x_offset: the number of columns to roll in the horizontal direction.
1406 % o y_offset: the number of rows to roll in the vertical direction.
1408 % o exception: return any errors or warnings in this structure.
1412 static inline MagickBooleanType CopyImageRegion(Image *destination,
1413 const Image *source,const size_t columns,const size_t rows,
1414 const ssize_t sx,const ssize_t sy,const ssize_t dx,const ssize_t dy,
1415 ExceptionInfo *exception)
1428 source_view=AcquireVirtualCacheView(source,exception);
1429 destination_view=AcquireAuthenticCacheView(destination,exception);
1430 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1431 #pragma omp parallel for schedule(static) shared(status)
1433 for (y=0; y < (ssize_t) rows; y++)
1438 register const Quantum
1450 if (status == MagickFalse)
1452 p=GetCacheViewVirtualPixels(source_view,sx,sy+y,columns,1,exception);
1453 q=GetCacheViewAuthenticPixels(destination_view,dx,dy+y,columns,1,exception);
1454 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1459 for (x=0; x < (ssize_t) columns; x++)
1464 if (GetPixelMask(source,p) != 0)
1466 p+=GetPixelChannels(source);
1467 q+=GetPixelChannels(destination);
1470 for (i=0; i < (ssize_t) GetPixelChannels(source); i++)
1479 channel=GetPixelChannelMapChannel(source,i);
1480 source_traits=GetPixelChannelMapTraits(source,channel);
1481 destination_traits=GetPixelChannelMapTraits(destination,channel);
1482 if ((source_traits == UndefinedPixelTrait) ||
1483 (destination_traits == UndefinedPixelTrait))
1485 SetPixelChannel(destination,channel,p[i],q);
1487 p+=GetPixelChannels(source);
1488 q+=GetPixelChannels(destination);
1490 sync=SyncCacheViewAuthenticPixels(destination_view,exception);
1491 if (sync == MagickFalse)
1494 destination_view=DestroyCacheView(destination_view);
1495 source_view=DestroyCacheView(source_view);
1499 MagickExport Image *RollImage(const Image *image,const ssize_t x_offset,
1500 const ssize_t y_offset,ExceptionInfo *exception)
1502 #define RollImageTag "Roll/Image"
1514 Initialize roll image attributes.
1516 assert(image != (const Image *) NULL);
1517 assert(image->signature == MagickSignature);
1518 if (image->debug != MagickFalse)
1519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1520 assert(exception != (ExceptionInfo *) NULL);
1521 assert(exception->signature == MagickSignature);
1522 roll_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1523 if (roll_image == (Image *) NULL)
1524 return((Image *) NULL);
1527 while (offset.x < 0)
1528 offset.x+=(ssize_t) image->columns;
1529 while (offset.x >= (ssize_t) image->columns)
1530 offset.x-=(ssize_t) image->columns;
1531 while (offset.y < 0)
1532 offset.y+=(ssize_t) image->rows;
1533 while (offset.y >= (ssize_t) image->rows)
1534 offset.y-=(ssize_t) image->rows;
1538 status=CopyImageRegion(roll_image,image,(size_t) offset.x,
1539 (size_t) offset.y,(ssize_t) image->columns-offset.x,(ssize_t) image->rows-
1540 offset.y,0,0,exception);
1541 (void) SetImageProgress(image,RollImageTag,0,3);
1542 status|=CopyImageRegion(roll_image,image,image->columns-offset.x,
1543 (size_t) offset.y,0,(ssize_t) image->rows-offset.y,offset.x,0,
1545 (void) SetImageProgress(image,RollImageTag,1,3);
1546 status|=CopyImageRegion(roll_image,image,(size_t) offset.x,image->rows-
1547 offset.y,(ssize_t) image->columns-offset.x,0,0,offset.y,exception);
1548 (void) SetImageProgress(image,RollImageTag,2,3);
1549 status|=CopyImageRegion(roll_image,image,image->columns-offset.x,image->rows-
1550 offset.y,0,0,offset.x,offset.y,exception);
1551 (void) SetImageProgress(image,RollImageTag,3,3);
1552 roll_image->type=image->type;
1553 if (status == MagickFalse)
1554 roll_image=DestroyImage(roll_image);
1559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1563 % S h a v e I m a g e %
1567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1569 % ShaveImage() shaves pixels from the image edges. It allocates the memory
1570 % necessary for the new Image structure and returns a pointer to the new
1573 % The format of the ShaveImage method is:
1575 % Image *ShaveImage(const Image *image,const RectangleInfo *shave_info,
1576 % ExceptionInfo *exception)
1578 % A description of each parameter follows:
1580 % o shave_image: Method ShaveImage returns a pointer to the shaved
1581 % image. A null image is returned if there is a memory shortage or
1582 % if the image width or height is zero.
1584 % o image: the image.
1586 % o shave_info: Specifies a pointer to a RectangleInfo which defines the
1587 % region of the image to crop.
1589 % o exception: return any errors or warnings in this structure.
1592 MagickExport Image *ShaveImage(const Image *image,
1593 const RectangleInfo *shave_info,ExceptionInfo *exception)
1601 assert(image != (const Image *) NULL);
1602 assert(image->signature == MagickSignature);
1603 if (image->debug != MagickFalse)
1604 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1605 if (((2*shave_info->width) >= image->columns) ||
1606 ((2*shave_info->height) >= image->rows))
1607 ThrowImageException(OptionWarning,"GeometryDoesNotContainImage");
1608 SetGeometry(image,&geometry);
1609 geometry.width-=2*shave_info->width;
1610 geometry.height-=2*shave_info->height;
1611 geometry.x=(ssize_t) shave_info->width+image->page.x;
1612 geometry.y=(ssize_t) shave_info->height+image->page.y;
1613 shave_image=CropImage(image,&geometry,exception);
1614 if (shave_image == (Image *) NULL)
1615 return((Image *) NULL);
1616 shave_image->page.width-=2*shave_info->width;
1617 shave_image->page.height-=2*shave_info->height;
1618 shave_image->page.x-=(ssize_t) shave_info->width;
1619 shave_image->page.y-=(ssize_t) shave_info->height;
1620 return(shave_image);
1624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1628 % S p l i c e I m a g e %
1632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1634 % SpliceImage() splices a solid color into the image as defined by the
1637 % The format of the SpliceImage method is:
1639 % Image *SpliceImage(const Image *image,const RectangleInfo *geometry,
1640 % ExceptionInfo *exception)
1642 % A description of each parameter follows:
1644 % o image: the image.
1646 % o geometry: Define the region of the image to splice with members
1647 % x, y, width, and height.
1649 % o exception: return any errors or warnings in this structure.
1652 MagickExport Image *SpliceImage(const Image *image,
1653 const RectangleInfo *geometry,ExceptionInfo *exception)
1655 #define SpliceImageTag "Splice/Image"
1677 Allocate splice image.
1679 assert(image != (const Image *) NULL);
1680 assert(image->signature == MagickSignature);
1681 if (image->debug != MagickFalse)
1682 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1683 assert(geometry != (const RectangleInfo *) NULL);
1684 assert(exception != (ExceptionInfo *) NULL);
1685 assert(exception->signature == MagickSignature);
1686 splice_geometry=(*geometry);
1687 splice_image=CloneImage(image,image->columns+splice_geometry.width,
1688 image->rows+splice_geometry.height,MagickTrue,exception);
1689 if (splice_image == (Image *) NULL)
1690 return((Image *) NULL);
1691 if (SetImageStorageClass(splice_image,DirectClass,exception) == MagickFalse)
1693 splice_image=DestroyImage(splice_image);
1694 return((Image *) NULL);
1696 (void) SetImageBackgroundColor(splice_image,exception);
1698 Respect image geometry.
1700 switch (image->gravity)
1703 case UndefinedGravity:
1704 case NorthWestGravity:
1708 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1711 case NorthEastGravity:
1713 splice_geometry.x+=(ssize_t) splice_geometry.width;
1718 splice_geometry.y+=(ssize_t) splice_geometry.width/2;
1724 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1725 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1730 splice_geometry.x+=(ssize_t) splice_geometry.width;
1731 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1734 case SouthWestGravity:
1736 splice_geometry.y+=(ssize_t) splice_geometry.height;
1741 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1742 splice_geometry.y+=(ssize_t) splice_geometry.height;
1745 case SouthEastGravity:
1747 splice_geometry.x+=(ssize_t) splice_geometry.width;
1748 splice_geometry.y+=(ssize_t) splice_geometry.height;
1757 image_view=AcquireVirtualCacheView(image,exception);
1758 splice_view=AcquireAuthenticCacheView(splice_image,exception);
1759 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1760 #pragma omp parallel for schedule(static,4) shared(progress,status)
1762 for (y=0; y < (ssize_t) splice_geometry.y; y++)
1764 register const Quantum
1773 if (status == MagickFalse)
1775 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1776 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1778 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1783 for (x=0; x < splice_geometry.x; x++)
1788 if (GetPixelMask(image,p) != 0)
1790 p+=GetPixelChannels(image);
1791 q+=GetPixelChannels(splice_image);
1794 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1803 channel=GetPixelChannelMapChannel(image,i);
1804 traits=GetPixelChannelMapTraits(image,channel);
1805 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1806 if ((traits == UndefinedPixelTrait) ||
1807 (splice_traits == UndefinedPixelTrait))
1809 SetPixelChannel(splice_image,channel,p[i],q);
1811 p+=GetPixelChannels(image);
1812 q+=GetPixelChannels(splice_image);
1814 for ( ; x < (ssize_t) (splice_geometry.x+splice_geometry.width); x++)
1815 q+=GetPixelChannels(splice_image);
1816 for ( ; x < (ssize_t) splice_image->columns; x++)
1821 if (GetPixelMask(image,p) != 0)
1823 p+=GetPixelChannels(image);
1824 q+=GetPixelChannels(splice_image);
1827 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1836 channel=GetPixelChannelMapChannel(image,i);
1837 traits=GetPixelChannelMapTraits(image,channel);
1838 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1839 if ((traits == UndefinedPixelTrait) ||
1840 (splice_traits == UndefinedPixelTrait))
1842 SetPixelChannel(splice_image,channel,p[i],q);
1844 p+=GetPixelChannels(image);
1845 q+=GetPixelChannels(splice_image);
1847 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
1849 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1854 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1855 #pragma omp critical (MagickCore_TransposeImage)
1857 proceed=SetImageProgress(image,SpliceImageTag,progress++,
1858 splice_image->rows);
1859 if (proceed == MagickFalse)
1863 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1864 #pragma omp parallel for schedule(static,4) shared(progress,status)
1866 for (y=(ssize_t) (splice_geometry.y+splice_geometry.height);
1867 y < (ssize_t) splice_image->rows; y++)
1869 register const Quantum
1878 if (status == MagickFalse)
1880 p=GetCacheViewVirtualPixels(image_view,0,y-(ssize_t) splice_geometry.height,
1881 image->columns,1,exception);
1882 if ((y < 0) || (y >= (ssize_t) splice_image->rows))
1884 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1886 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1891 for (x=0; x < splice_geometry.x; x++)
1896 if (GetPixelMask(image,q) != 0)
1898 p+=GetPixelChannels(image);
1899 q+=GetPixelChannels(splice_image);
1902 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1911 channel=GetPixelChannelMapChannel(image,i);
1912 traits=GetPixelChannelMapTraits(image,channel);
1913 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1914 if ((traits == UndefinedPixelTrait) ||
1915 (splice_traits == UndefinedPixelTrait))
1917 SetPixelChannel(splice_image,channel,p[i],q);
1919 p+=GetPixelChannels(image);
1920 q+=GetPixelChannels(splice_image);
1922 for ( ; x < (ssize_t) (splice_geometry.x+splice_geometry.width); x++)
1923 q+=GetPixelChannels(splice_image);
1924 for ( ; x < (ssize_t) splice_image->columns; x++)
1929 if (GetPixelMask(image,q) != 0)
1931 p+=GetPixelChannels(image);
1932 q+=GetPixelChannels(splice_image);
1935 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1944 channel=GetPixelChannelMapChannel(image,i);
1945 traits=GetPixelChannelMapTraits(image,channel);
1946 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1947 if ((traits == UndefinedPixelTrait) ||
1948 (splice_traits == UndefinedPixelTrait))
1950 SetPixelChannel(splice_image,channel,p[i],q);
1952 p+=GetPixelChannels(image);
1953 q+=GetPixelChannels(splice_image);
1955 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
1957 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1962 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1963 #pragma omp critical (MagickCore_TransposeImage)
1965 proceed=SetImageProgress(image,SpliceImageTag,progress++,
1966 splice_image->rows);
1967 if (proceed == MagickFalse)
1971 splice_view=DestroyCacheView(splice_view);
1972 image_view=DestroyCacheView(image_view);
1973 if (status == MagickFalse)
1974 splice_image=DestroyImage(splice_image);
1975 return(splice_image);
1979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1983 % T r a n s f o r m I m a g e %
1987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1989 % TransformImage() is a convenience method that behaves like ResizeImage() or
1990 % CropImage() but accepts scaling and/or cropping information as a region
1991 % geometry specification. If the operation fails, the original image handle
1994 % This should only be used for single images.
1996 % This function destroys what it assumes to be a single image list.
1997 % If the input image is part of a larger list, all other images in that list
1998 % will be simply 'lost', not destroyed.
2000 % Also if the crop generates a list of images only the first image is resized.
2001 % And finally if the crop succeeds and the resize failed, you will get a
2002 % cropped image, as well as a 'false' or 'failed' report.
2004 % This function and should probably be depreciated in favor of direct calls
2005 % to CropImageToTiles() or ResizeImage(), as appropriate.
2007 % The format of the TransformImage method is:
2009 % MagickBooleanType TransformImage(Image **image,const char *crop_geometry,
2010 % const char *image_geometry,ExceptionInfo *exception)
2012 % A description of each parameter follows:
2014 % o image: the image The transformed image is returned as this parameter.
2016 % o crop_geometry: A crop geometry string. This geometry defines a
2017 % subregion of the image to crop.
2019 % o image_geometry: An image geometry string. This geometry defines the
2020 % final size of the image.
2022 % o exception: return any errors or warnings in this structure.
2025 MagickExport MagickBooleanType TransformImage(Image **image,
2026 const char *crop_geometry,const char *image_geometry,ExceptionInfo *exception)
2038 assert(image != (Image **) NULL);
2039 assert((*image)->signature == MagickSignature);
2040 if ((*image)->debug != MagickFalse)
2041 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
2042 transform_image=(*image);
2043 if (crop_geometry != (const char *) NULL)
2049 Crop image to a user specified size.
2051 crop_image=CropImageToTiles(*image,crop_geometry,exception);
2052 if (crop_image == (Image *) NULL)
2053 transform_image=CloneImage(*image,0,0,MagickTrue,exception);
2056 transform_image=DestroyImage(transform_image);
2057 transform_image=GetFirstImageInList(crop_image);
2059 *image=transform_image;
2061 if (image_geometry == (const char *) NULL)
2065 Scale image to a user specified size.
2067 flags=ParseRegionGeometry(transform_image,image_geometry,&geometry,exception);
2069 if ((transform_image->columns == geometry.width) &&
2070 (transform_image->rows == geometry.height))
2072 resize_image=ResizeImage(transform_image,geometry.width,geometry.height,
2073 transform_image->filter,exception);
2074 if (resize_image == (Image *) NULL)
2075 return(MagickFalse);
2076 transform_image=DestroyImage(transform_image);
2077 transform_image=resize_image;
2078 *image=transform_image;
2083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2087 % T r a n s f o r m I m a g e s %
2091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2093 % TransformImages() calls TransformImage() on each image of a sequence.
2095 % The format of the TransformImage method is:
2097 % MagickBooleanType TransformImages(Image **image,
2098 % const char *crop_geometry,const char *image_geometry,
2099 % ExceptionInfo *exception)
2101 % A description of each parameter follows:
2103 % o image: the image The transformed image is returned as this parameter.
2105 % o crop_geometry: A crop geometry string. This geometry defines a
2106 % subregion of the image to crop.
2108 % o image_geometry: An image geometry string. This geometry defines the
2109 % final size of the image.
2111 % o exception: return any errors or warnings in this structure.
2114 MagickExport MagickBooleanType TransformImages(Image **images,
2115 const char *crop_geometry,const char *image_geometry,ExceptionInfo *exception)
2128 assert(images != (Image **) NULL);
2129 assert((*images)->signature == MagickSignature);
2130 if ((*images)->debug != MagickFalse)
2131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2132 (*images)->filename);
2133 image_list=ImageListToArray(*images,exception);
2134 if (image_list == (Image **) NULL)
2135 return(MagickFalse);
2137 transform_images=NewImageList();
2138 for (i=0; image_list[i] != (Image *) NULL; i++)
2140 image=image_list[i];
2141 status|=TransformImage(&image,crop_geometry,image_geometry,exception);
2142 AppendImageToList(&transform_images,image);
2144 *images=transform_images;
2145 image_list=(Image **) RelinquishMagickMemory(image_list);
2146 return(status != 0 ? MagickTrue : MagickFalse);
2150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2154 % T r a n s p o s e I m a g e %
2158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2160 % TransposeImage() creates a horizontal mirror image by reflecting the pixels
2161 % around the central y-axis while rotating them by 90 degrees.
2163 % The format of the TransposeImage method is:
2165 % Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2167 % A description of each parameter follows:
2169 % o image: the image.
2171 % o exception: return any errors or warnings in this structure.
2174 MagickExport Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2176 #define TransposeImageTag "Transpose/Image"
2197 assert(image != (const Image *) NULL);
2198 assert(image->signature == MagickSignature);
2199 if (image->debug != MagickFalse)
2200 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2201 assert(exception != (ExceptionInfo *) NULL);
2202 assert(exception->signature == MagickSignature);
2203 transpose_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2205 if (transpose_image == (Image *) NULL)
2206 return((Image *) NULL);
2212 image_view=AcquireVirtualCacheView(image,exception);
2213 transpose_view=AcquireAuthenticCacheView(transpose_image,exception);
2214 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2215 #pragma omp parallel for schedule(static,4) shared(progress,status)
2217 for (y=0; y < (ssize_t) image->rows; y++)
2219 register const Quantum
2228 if (status == MagickFalse)
2230 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-y-1,
2231 image->columns,1,exception);
2232 q=QueueCacheViewAuthenticPixels(transpose_view,(ssize_t) (image->rows-y-1),
2233 0,1,transpose_image->rows,exception);
2234 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2239 for (x=0; x < (ssize_t) image->columns; x++)
2244 if (GetPixelMask(image,q) != 0)
2246 p+=GetPixelChannels(image);
2247 q+=GetPixelChannels(transpose_image);
2250 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2259 channel=GetPixelChannelMapChannel(image,i);
2260 traits=GetPixelChannelMapTraits(image,channel);
2261 transpose_traits=GetPixelChannelMapTraits(transpose_image,channel);
2262 if ((traits == UndefinedPixelTrait) ||
2263 (transpose_traits == UndefinedPixelTrait))
2265 SetPixelChannel(transpose_image,channel,p[i],q);
2267 p+=GetPixelChannels(image);
2268 q+=GetPixelChannels(transpose_image);
2270 if (SyncCacheViewAuthenticPixels(transpose_view,exception) == MagickFalse)
2272 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2277 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2278 #pragma omp critical (MagickCore_TransposeImage)
2280 proceed=SetImageProgress(image,TransposeImageTag,progress++,
2282 if (proceed == MagickFalse)
2286 transpose_view=DestroyCacheView(transpose_view);
2287 image_view=DestroyCacheView(image_view);
2288 transpose_image->type=image->type;
2289 page=transpose_image->page;
2290 Swap(page.width,page.height);
2291 Swap(page.x,page.y);
2292 transpose_image->page=page;
2293 if (status == MagickFalse)
2294 transpose_image=DestroyImage(transpose_image);
2295 return(transpose_image);
2299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2303 % T r a n s v e r s e I m a g e %
2307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2309 % TransverseImage() creates a vertical mirror image by reflecting the pixels
2310 % around the central x-axis while rotating them by 270 degrees.
2312 % The format of the TransverseImage method is:
2314 % Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2316 % A description of each parameter follows:
2318 % o image: the image.
2320 % o exception: return any errors or warnings in this structure.
2323 MagickExport Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2325 #define TransverseImageTag "Transverse/Image"
2346 assert(image != (const Image *) NULL);
2347 assert(image->signature == MagickSignature);
2348 if (image->debug != MagickFalse)
2349 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2350 assert(exception != (ExceptionInfo *) NULL);
2351 assert(exception->signature == MagickSignature);
2352 transverse_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2354 if (transverse_image == (Image *) NULL)
2355 return((Image *) NULL);
2361 image_view=AcquireVirtualCacheView(image,exception);
2362 transverse_view=AcquireAuthenticCacheView(transverse_image,exception);
2363 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2364 #pragma omp parallel for schedule(static,4) shared(progress,status)
2366 for (y=0; y < (ssize_t) image->rows; y++)
2371 register const Quantum
2380 if (status == MagickFalse)
2382 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2383 q=QueueCacheViewAuthenticPixels(transverse_view,(ssize_t) (image->rows-y-1),
2384 0,1,transverse_image->rows,exception);
2385 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2390 q+=GetPixelChannels(transverse_image)*image->columns;
2391 for (x=0; x < (ssize_t) image->columns; x++)
2396 q-=GetPixelChannels(transverse_image);
2397 if (GetPixelMask(image,p) != 0)
2399 p+=GetPixelChannels(image);
2402 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2411 channel=GetPixelChannelMapChannel(image,i);
2412 traits=GetPixelChannelMapTraits(image,channel);
2413 transverse_traits=GetPixelChannelMapTraits(transverse_image,channel);
2414 if ((traits == UndefinedPixelTrait) ||
2415 (transverse_traits == UndefinedPixelTrait))
2417 SetPixelChannel(transverse_image,channel,p[i],q);
2419 p+=GetPixelChannels(image);
2421 sync=SyncCacheViewAuthenticPixels(transverse_view,exception);
2422 if (sync == MagickFalse)
2424 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2429 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2430 #pragma omp critical (MagickCore_TransverseImage)
2432 proceed=SetImageProgress(image,TransverseImageTag,progress++,
2434 if (proceed == MagickFalse)
2438 transverse_view=DestroyCacheView(transverse_view);
2439 image_view=DestroyCacheView(image_view);
2440 transverse_image->type=image->type;
2441 page=transverse_image->page;
2442 Swap(page.width,page.height);
2443 Swap(page.x,page.y);
2444 if (page.width != 0)
2445 page.x=(ssize_t) (page.width-transverse_image->columns-page.x);
2446 if (page.height != 0)
2447 page.y=(ssize_t) (page.height-transverse_image->rows-page.y);
2448 transverse_image->page=page;
2449 if (status == MagickFalse)
2450 transverse_image=DestroyImage(transverse_image);
2451 return(transverse_image);
2455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2459 % T r i m I m a g e %
2463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2465 % TrimImage() trims pixels from the image edges. It allocates the memory
2466 % necessary for the new Image structure and returns a pointer to the new
2469 % The format of the TrimImage method is:
2471 % Image *TrimImage(const Image *image,ExceptionInfo *exception)
2473 % A description of each parameter follows:
2475 % o image: the image.
2477 % o exception: return any errors or warnings in this structure.
2480 MagickExport Image *TrimImage(const Image *image,ExceptionInfo *exception)
2485 assert(image != (const Image *) NULL);
2486 assert(image->signature == MagickSignature);
2487 if (image->debug != MagickFalse)
2488 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2489 geometry=GetImageBoundingBox(image,exception);
2490 if ((geometry.width == 0) || (geometry.height == 0))
2495 crop_image=CloneImage(image,1,1,MagickTrue,exception);
2496 if (crop_image == (Image *) NULL)
2497 return((Image *) NULL);
2498 crop_image->background_color.alpha=(Quantum) TransparentAlpha;
2499 (void) SetImageBackgroundColor(crop_image,exception);
2500 crop_image->page=image->page;
2501 crop_image->page.x=(-1);
2502 crop_image->page.y=(-1);
2505 geometry.x+=image->page.x;
2506 geometry.y+=image->page.y;
2507 return(CropImage(image,&geometry,exception));