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=AcquireCacheView(image);
161 chop_view=AcquireCacheView(chop_image);
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 p+=GetPixelChannels(chop_image);
282 q+=GetPixelChannels(chop_image);
284 p+=GetPixelChannels(image);
286 if (SyncCacheViewAuthenticPixels(chop_view,exception) == MagickFalse)
288 if (image->progress_monitor != (MagickProgressMonitor) NULL)
293 #if defined(MAGICKCORE_OPENMP_SUPPORT)
294 #pragma omp critical (MagickCore_ChopImage)
296 proceed=SetImageProgress(image,ChopImageTag,progress++,image->rows);
297 if (proceed == MagickFalse)
301 chop_view=DestroyCacheView(chop_view);
302 image_view=DestroyCacheView(image_view);
303 chop_image->type=image->type;
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 + C o n s o l i d a t e C M Y K I m a g e %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 % ConsolidateCMYKImage() consolidates separate C, M, Y, and K planes into a
321 % The format of the ConsolidateCMYKImage method is:
323 % Image *ConsolidateCMYKImage(const Image *image,ExceptionInfo *exception)
325 % A description of each parameter follows:
327 % o image: the image sequence.
329 % o exception: return any errors or warnings in this structure.
332 MagickExport Image *ConsolidateCMYKImages(const Image *images,
333 ExceptionInfo *exception)
350 Consolidate separate C, M, Y, and K planes into a single image.
352 assert(images != (Image *) NULL);
353 assert(images->signature == MagickSignature);
354 if (images->debug != MagickFalse)
355 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
356 assert(exception != (ExceptionInfo *) NULL);
357 assert(exception->signature == MagickSignature);
358 cmyk_images=NewImageList();
359 for (j=0; j < (ssize_t) GetImageListLength(images); j+=4)
364 cmyk_image=CloneImage(images,images->columns,images->rows,MagickTrue,
366 if (cmyk_image == (Image *) NULL)
368 if (SetImageStorageClass(cmyk_image,DirectClass,exception) == MagickFalse)
370 (void) SetImageColorspace(cmyk_image,CMYKColorspace,exception);
371 for (i=0; i < 4; i++)
373 image_view=AcquireCacheView(images);
374 cmyk_view=AcquireCacheView(cmyk_image);
375 for (y=0; y < (ssize_t) images->rows; y++)
377 register const Quantum
386 p=GetCacheViewVirtualPixels(image_view,0,y,images->columns,1,exception);
387 q=QueueCacheViewAuthenticPixels(cmyk_view,0,y,cmyk_image->columns,1,
389 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
391 for (x=0; x < (ssize_t) images->columns; x++)
396 pixel=QuantumRange-GetPixelIntensity(images,p);
399 case 0: SetPixelCyan(cmyk_image,pixel,q); break;
400 case 1: SetPixelMagenta(cmyk_image,pixel,q); break;
401 case 2: SetPixelYellow(cmyk_image,pixel,q); break;
402 case 3: SetPixelBlack(cmyk_image,pixel,q); break;
405 p+=GetPixelChannels(images);
406 q+=GetPixelChannels(cmyk_image);
408 if (SyncCacheViewAuthenticPixels(cmyk_view,exception) == MagickFalse)
411 cmyk_view=DestroyCacheView(cmyk_view);
412 image_view=DestroyCacheView(image_view);
413 images=GetNextImageInList(images);
414 if (images == (Image *) NULL)
417 AppendImageToList(&cmyk_images,cmyk_image);
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 % C r o p I m a g e %
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
433 % CropImage() extracts a region of the image starting at the offset defined
434 % by geometry. Region must be fully defined, and no special handling of
435 % geometry flags is performed.
437 % The format of the CropImage method is:
439 % Image *CropImage(const Image *image,const RectangleInfo *geometry,
440 % ExceptionInfo *exception)
442 % A description of each parameter follows:
444 % o image: the image.
446 % o geometry: Define the region of the image to crop with members
447 % x, y, width, and height.
449 % o exception: return any errors or warnings in this structure.
452 MagickExport Image *CropImage(const Image *image,const RectangleInfo *geometry,
453 ExceptionInfo *exception)
455 #define CropImageTag "Crop/Image"
483 assert(image != (const Image *) NULL);
484 assert(image->signature == MagickSignature);
485 if (image->debug != MagickFalse)
486 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
487 assert(geometry != (const RectangleInfo *) NULL);
488 assert(exception != (ExceptionInfo *) NULL);
489 assert(exception->signature == MagickSignature);
490 bounding_box=image->page;
491 if ((bounding_box.width == 0) || (bounding_box.height == 0))
493 bounding_box.width=image->columns;
494 bounding_box.height=image->rows;
498 page.width=bounding_box.width;
499 if (page.height == 0)
500 page.height=bounding_box.height;
501 if (((bounding_box.x-page.x) >= (ssize_t) page.width) ||
502 ((bounding_box.y-page.y) >= (ssize_t) page.height) ||
503 ((page.x-bounding_box.x) > (ssize_t) image->columns) ||
504 ((page.y-bounding_box.y) > (ssize_t) image->rows))
507 Crop is not within virtual canvas, return 1 pixel transparent image.
509 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
510 "GeometryDoesNotContainImage","`%s'",image->filename);
511 crop_image=CloneImage(image,1,1,MagickTrue,exception);
512 if (crop_image == (Image *) NULL)
513 return((Image *) NULL);
514 crop_image->background_color.alpha=(Quantum) TransparentAlpha;
515 (void) SetImageBackgroundColor(crop_image,exception);
516 crop_image->page=bounding_box;
517 crop_image->page.x=(-1);
518 crop_image->page.y=(-1);
519 if (crop_image->dispose == BackgroundDispose)
520 crop_image->dispose=NoneDispose;
523 if ((page.x < 0) && (bounding_box.x >= 0))
525 page.width+=page.x-bounding_box.x;
530 page.width-=bounding_box.x-page.x;
531 page.x-=bounding_box.x;
535 if ((page.y < 0) && (bounding_box.y >= 0))
537 page.height+=page.y-bounding_box.y;
542 page.height-=bounding_box.y-page.y;
543 page.y-=bounding_box.y;
547 if ((size_t) (page.x+page.width) > image->columns)
548 page.width=image->columns-page.x;
549 if ((geometry->width != 0) && (page.width > geometry->width))
550 page.width=geometry->width;
551 if ((size_t) (page.y+page.height) > image->rows)
552 page.height=image->rows-page.y;
553 if ((geometry->height != 0) && (page.height > geometry->height))
554 page.height=geometry->height;
555 bounding_box.x+=page.x;
556 bounding_box.y+=page.y;
557 if ((page.width == 0) || (page.height == 0))
559 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
560 "GeometryDoesNotContainImage","`%s'",image->filename);
561 return((Image *) NULL);
564 Initialize crop image attributes.
566 crop_image=CloneImage(image,page.width,page.height,MagickTrue,exception);
567 if (crop_image == (Image *) NULL)
568 return((Image *) NULL);
569 crop_image->page.width=image->page.width;
570 crop_image->page.height=image->page.height;
571 offset.x=(ssize_t) (bounding_box.x+bounding_box.width);
572 offset.y=(ssize_t) (bounding_box.y+bounding_box.height);
573 if ((offset.x > (ssize_t) image->page.width) ||
574 (offset.y > (ssize_t) image->page.height))
576 crop_image->page.width=bounding_box.width;
577 crop_image->page.height=bounding_box.height;
579 crop_image->page.x=bounding_box.x;
580 crop_image->page.y=bounding_box.y;
586 image_view=AcquireCacheView(image);
587 crop_view=AcquireCacheView(crop_image);
588 #if defined(MAGICKCORE_OPENMP_SUPPORT)
589 #pragma omp parallel for schedule(static) shared(progress,status)
591 for (y=0; y < (ssize_t) crop_image->rows; y++)
593 register const Quantum
602 if (status == MagickFalse)
604 p=GetCacheViewVirtualPixels(image_view,page.x,page.y+y,crop_image->columns,
606 q=QueueCacheViewAuthenticPixels(crop_view,0,y,crop_image->columns,1,
608 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
613 for (x=0; x < (ssize_t) crop_image->columns; x++)
618 if (GetPixelMask(image,p) != 0)
620 p+=GetPixelChannels(image);
621 q+=GetPixelChannels(crop_image);
624 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
633 channel=GetPixelChannelMapChannel(image,i);
634 traits=GetPixelChannelMapTraits(image,channel);
635 crop_traits=GetPixelChannelMapTraits(crop_image,channel);
636 if ((traits == UndefinedPixelTrait) ||
637 (crop_traits == UndefinedPixelTrait))
639 SetPixelChannel(crop_image,channel,p[i],q);
641 p+=GetPixelChannels(image);
642 q+=GetPixelChannels(crop_image);
644 if (SyncCacheViewAuthenticPixels(crop_view,exception) == MagickFalse)
646 if (image->progress_monitor != (MagickProgressMonitor) NULL)
651 #if defined(MAGICKCORE_OPENMP_SUPPORT)
652 #pragma omp critical (MagickCore_CropImage)
654 proceed=SetImageProgress(image,CropImageTag,progress++,image->rows);
655 if (proceed == MagickFalse)
659 crop_view=DestroyCacheView(crop_view);
660 image_view=DestroyCacheView(image_view);
661 crop_image->type=image->type;
662 if (status == MagickFalse)
663 crop_image=DestroyImage(crop_image);
668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672 % C r o p I m a g e T o T i l e s %
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678 % CropImageToTiles() crops a single image, into a possible list of tiles.
679 % This may include a single sub-region of the image. This basically applies
680 % all the normal geometry flags for Crop.
682 % Image *CropImageToTiles(const Image *image,
683 % const RectangleInfo *crop_geometry, ExceptionInfo *exception)
685 % A description of each parameter follows:
687 % o image: the image The transformed image is returned as this parameter.
689 % o crop_geometry: A crop geometry string.
691 % o exception: return any errors or warnings in this structure.
695 static inline ssize_t MagickRound(MagickRealType x)
698 Round the fraction to nearest integer.
701 return((ssize_t) (x+0.5));
702 return((ssize_t) (x-0.5));
705 MagickExport Image *CropImageToTiles(const Image *image,
706 const char *crop_geometry,ExceptionInfo *exception)
718 assert(image != (Image *) NULL);
719 assert(image->signature == MagickSignature);
720 if (image->debug != MagickFalse)
721 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
722 crop_image=NewImageList();
724 flags=ParseGravityGeometry(image,crop_geometry,&geometry,exception);
725 if ((flags & AreaValue) != 0)
739 Crop into NxM tiles (@ flag).
741 width=image->columns;
743 if (geometry.width == 0)
745 if (geometry.height == 0)
747 if ((flags & AspectValue) == 0)
749 width-=(geometry.x < 0 ? -1 : 1)*geometry.x;
750 height-=(geometry.y < 0 ? -1 : 1)*geometry.y;
754 width+=(geometry.x < 0 ? -1 : 1)*geometry.x;
755 height+=(geometry.y < 0 ? -1 : 1)*geometry.y;
757 delta.x=(double) width/geometry.width;
758 delta.y=(double) height/geometry.height;
763 for (offset.y=0; offset.y < (double) height; )
765 if ((flags & AspectValue) == 0)
767 crop.y=(ssize_t) MagickRound((MagickRealType) (offset.y-
768 (geometry.y > 0 ? 0 : geometry.y)));
769 offset.y+=delta.y; /* increment now to find width */
770 crop.height=(size_t) MagickRound((MagickRealType) (offset.y+
771 (geometry.y < 0 ? 0 : geometry.y)));
775 crop.y=(ssize_t) MagickRound((MagickRealType) (offset.y-
776 (geometry.y > 0 ? geometry.y : 0)));
777 offset.y+=delta.y; /* increment now to find width */
778 crop.height=(size_t) MagickRound((MagickRealType)
779 (offset.y+(geometry.y < -1 ? geometry.y : 0)));
782 crop.y+=image->page.y;
783 for (offset.x=0; offset.x < (double) width; )
785 if ((flags & AspectValue) == 0)
787 crop.x=(ssize_t) MagickRound((MagickRealType) (offset.x-
788 (geometry.x > 0 ? 0 : geometry.x)));
789 offset.x+=delta.x; /* increment now to find height */
790 crop.width=(size_t) MagickRound((MagickRealType) (offset.x+
791 (geometry.x < 0 ? 0 : geometry.x)));
795 crop.x=(ssize_t) MagickRound((MagickRealType) (offset.x-
796 (geometry.x > 0 ? geometry.x : 0)));
797 offset.x+=delta.x; /* increment now to find height */
798 crop.width=(size_t) MagickRound((MagickRealType) (offset.x+
799 (geometry.x < 0 ? geometry.x : 0)));
802 crop.x+=image->page.x;
803 next=CropImage(image,&crop,exception);
804 if (next == (Image *) NULL)
806 AppendImageToList(&crop_image,next);
808 if (next == (Image *) NULL)
811 ClearMagickException(exception);
814 if (((geometry.width == 0) && (geometry.height == 0)) ||
815 ((flags & XValue) != 0) || ((flags & YValue) != 0))
818 Crop a single region at +X+Y.
820 crop_image=CropImage(image,&geometry,exception);
821 if ((crop_image != (Image *) NULL) && ((flags & AspectValue) != 0))
823 crop_image->page.width=geometry.width;
824 crop_image->page.height=geometry.height;
825 crop_image->page.x-=geometry.x;
826 crop_image->page.y-=geometry.y;
830 if ((image->columns > geometry.width) || (image->rows > geometry.height))
844 Crop into tiles of fixed size WxH.
848 page.width=image->columns;
849 if (page.height == 0)
850 page.height=image->rows;
851 width=geometry.width;
854 height=geometry.height;
858 for (y=0; y < (ssize_t) page.height; y+=(ssize_t) height)
860 for (x=0; x < (ssize_t) page.width; x+=(ssize_t) width)
862 geometry.width=width;
863 geometry.height=height;
866 next=CropImage(image,&geometry,exception);
867 if (next == (Image *) NULL)
869 AppendImageToList(&crop_image,next);
871 if (next == (Image *) NULL)
876 return(CloneImage(image,0,0,MagickTrue,exception));
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 % E x c e r p t I m a g e %
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 % ExcerptImage() returns a excerpt of the image as defined by the geometry.
892 % The format of the ExcerptImage method is:
894 % Image *ExcerptImage(const Image *image,const RectangleInfo *geometry,
895 % ExceptionInfo *exception)
897 % A description of each parameter follows:
899 % o image: the image.
901 % o geometry: Define the region of the image to extend with members
902 % x, y, width, and height.
904 % o exception: return any errors or warnings in this structure.
907 MagickExport Image *ExcerptImage(const Image *image,
908 const RectangleInfo *geometry,ExceptionInfo *exception)
910 #define ExcerptImageTag "Excerpt/Image"
929 Allocate excerpt image.
931 assert(image != (const Image *) NULL);
932 assert(image->signature == MagickSignature);
933 if (image->debug != MagickFalse)
934 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
935 assert(geometry != (const RectangleInfo *) NULL);
936 assert(exception != (ExceptionInfo *) NULL);
937 assert(exception->signature == MagickSignature);
938 excerpt_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
940 if (excerpt_image == (Image *) NULL)
941 return((Image *) NULL);
947 image_view=AcquireCacheView(image);
948 excerpt_view=AcquireCacheView(excerpt_image);
949 #if defined(MAGICKCORE_OPENMP_SUPPORT)
950 #pragma omp parallel for schedule(static,4) shared(progress,status)
952 for (y=0; y < (ssize_t) excerpt_image->rows; y++)
954 register const Quantum
963 if (status == MagickFalse)
965 p=GetCacheViewVirtualPixels(image_view,geometry->x,geometry->y+y,
966 geometry->width,1,exception);
967 q=GetCacheViewAuthenticPixels(excerpt_view,0,y,excerpt_image->columns,1,
969 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
974 for (x=0; x < (ssize_t) excerpt_image->columns; x++)
979 if (GetPixelMask(image,p) != 0)
981 p+=GetPixelChannels(image);
982 q+=GetPixelChannels(excerpt_image);
985 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
994 channel=GetPixelChannelMapChannel(image,i);
995 traits=GetPixelChannelMapTraits(image,channel);
996 excerpt_traits=GetPixelChannelMapTraits(excerpt_image,channel);
997 if ((traits == UndefinedPixelTrait) ||
998 (excerpt_traits == UndefinedPixelTrait))
1000 SetPixelChannel(excerpt_image,channel,p[i],q);
1002 p+=GetPixelChannels(image);
1003 q+=GetPixelChannels(excerpt_image);
1005 if (SyncCacheViewAuthenticPixels(excerpt_view,exception) == MagickFalse)
1007 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1012 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1013 #pragma omp critical (MagickCore_ExcerptImage)
1015 proceed=SetImageProgress(image,ExcerptImageTag,progress++,image->rows);
1016 if (proceed == MagickFalse)
1020 excerpt_view=DestroyCacheView(excerpt_view);
1021 image_view=DestroyCacheView(image_view);
1022 excerpt_image->type=image->type;
1023 if (status == MagickFalse)
1024 excerpt_image=DestroyImage(excerpt_image);
1025 return(excerpt_image);
1029 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1033 % E x t e n t I m a g e %
1037 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1039 % ExtentImage() extends the image as defined by the geometry, gravity, and
1040 % image background color. Set the (x,y) offset of the geometry to move the
1041 % original image relative to the extended image.
1043 % The format of the ExtentImage method is:
1045 % Image *ExtentImage(const Image *image,const RectangleInfo *geometry,
1046 % ExceptionInfo *exception)
1048 % A description of each parameter follows:
1050 % o image: the image.
1052 % o geometry: Define the region of the image to extend with members
1053 % x, y, width, and height.
1055 % o exception: return any errors or warnings in this structure.
1058 MagickExport Image *ExtentImage(const Image *image,
1059 const RectangleInfo *geometry,ExceptionInfo *exception)
1065 Allocate extent image.
1067 assert(image != (const Image *) NULL);
1068 assert(image->signature == MagickSignature);
1069 if (image->debug != MagickFalse)
1070 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1071 assert(geometry != (const RectangleInfo *) NULL);
1072 assert(exception != (ExceptionInfo *) NULL);
1073 assert(exception->signature == MagickSignature);
1074 extent_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
1076 if (extent_image == (Image *) NULL)
1077 return((Image *) NULL);
1078 if (SetImageStorageClass(extent_image,DirectClass,exception) == MagickFalse)
1080 extent_image=DestroyImage(extent_image);
1081 return((Image *) NULL);
1083 if (extent_image->background_color.alpha != OpaqueAlpha)
1084 extent_image->matte=MagickTrue;
1085 (void) SetImageBackgroundColor(extent_image,exception);
1086 (void) CompositeImage(extent_image,image,image->compose,MagickFalse,
1087 -geometry->x,-geometry->y,exception);
1088 return(extent_image);
1092 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1096 % F l i p I m a g e %
1100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1102 % FlipImage() creates a vertical mirror image by reflecting the pixels
1103 % around the central x-axis.
1105 % The format of the FlipImage method is:
1107 % Image *FlipImage(const Image *image,ExceptionInfo *exception)
1109 % A description of each parameter follows:
1111 % o image: the image.
1113 % o exception: return any errors or warnings in this structure.
1116 MagickExport Image *FlipImage(const Image *image,ExceptionInfo *exception)
1118 #define FlipImageTag "Flip/Image"
1139 assert(image != (const Image *) NULL);
1140 assert(image->signature == MagickSignature);
1141 if (image->debug != MagickFalse)
1142 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1143 assert(exception != (ExceptionInfo *) NULL);
1144 assert(exception->signature == MagickSignature);
1145 flip_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1146 if (flip_image == (Image *) NULL)
1147 return((Image *) NULL);
1154 image_view=AcquireCacheView(image);
1155 flip_view=AcquireCacheView(flip_image);
1156 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1157 #pragma omp parallel for schedule(static) shared(progress,status)
1159 for (y=0; y < (ssize_t) flip_image->rows; y++)
1161 register const Quantum
1170 if (status == MagickFalse)
1172 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1173 q=QueueCacheViewAuthenticPixels(flip_view,0,(ssize_t) (flip_image->rows-y-
1174 1),flip_image->columns,1,exception);
1175 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1180 for (x=0; x < (ssize_t) flip_image->columns; x++)
1185 if (GetPixelMask(image,p) != 0)
1187 p+=GetPixelChannels(image);
1188 q+=GetPixelChannels(flip_image);
1191 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1200 channel=GetPixelChannelMapChannel(image,i);
1201 traits=GetPixelChannelMapTraits(image,channel);
1202 flip_traits=GetPixelChannelMapTraits(flip_image,channel);
1203 if ((traits == UndefinedPixelTrait) ||
1204 (flip_traits == UndefinedPixelTrait))
1206 SetPixelChannel(flip_image,channel,p[i],q);
1208 p+=GetPixelChannels(image);
1209 q+=GetPixelChannels(flip_image);
1211 if (SyncCacheViewAuthenticPixels(flip_view,exception) == MagickFalse)
1213 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1218 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1219 #pragma omp critical (MagickCore_FlipImage)
1221 proceed=SetImageProgress(image,FlipImageTag,progress++,image->rows);
1222 if (proceed == MagickFalse)
1226 flip_view=DestroyCacheView(flip_view);
1227 image_view=DestroyCacheView(image_view);
1228 flip_image->type=image->type;
1229 if (page.height != 0)
1230 page.y=(ssize_t) (page.height-flip_image->rows-page.y);
1231 flip_image->page=page;
1232 if (status == MagickFalse)
1233 flip_image=DestroyImage(flip_image);
1238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1242 % F l o p I m a g e %
1246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1248 % FlopImage() creates a horizontal mirror image by reflecting the pixels
1249 % around the central y-axis.
1251 % The format of the FlopImage method is:
1253 % Image *FlopImage(const Image *image,ExceptionInfo *exception)
1255 % A description of each parameter follows:
1257 % o image: the image.
1259 % o exception: return any errors or warnings in this structure.
1262 MagickExport Image *FlopImage(const Image *image,ExceptionInfo *exception)
1264 #define FlopImageTag "Flop/Image"
1285 assert(image != (const Image *) NULL);
1286 assert(image->signature == MagickSignature);
1287 if (image->debug != MagickFalse)
1288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1289 assert(exception != (ExceptionInfo *) NULL);
1290 assert(exception->signature == MagickSignature);
1291 flop_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1292 if (flop_image == (Image *) NULL)
1293 return((Image *) NULL);
1300 image_view=AcquireCacheView(image);
1301 flop_view=AcquireCacheView(flop_image);
1302 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1303 #pragma omp parallel for schedule(static) shared(progress,status)
1305 for (y=0; y < (ssize_t) flop_image->rows; y++)
1307 register const Quantum
1316 if (status == MagickFalse)
1318 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1319 q=QueueCacheViewAuthenticPixels(flop_view,0,y,flop_image->columns,1,
1321 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1326 q+=GetPixelChannels(flop_image)*flop_image->columns;
1327 for (x=0; x < (ssize_t) flop_image->columns; x++)
1332 q-=GetPixelChannels(flop_image);
1333 if (GetPixelMask(image,p) != 0)
1335 p+=GetPixelChannels(image);
1338 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1347 channel=GetPixelChannelMapChannel(image,i);
1348 traits=GetPixelChannelMapTraits(image,channel);
1349 flop_traits=GetPixelChannelMapTraits(flop_image,channel);
1350 if ((traits == UndefinedPixelTrait) ||
1351 (flop_traits == UndefinedPixelTrait))
1353 SetPixelChannel(flop_image,channel,p[i],q);
1355 p+=GetPixelChannels(image);
1357 if (SyncCacheViewAuthenticPixels(flop_view,exception) == MagickFalse)
1359 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1364 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1365 #pragma omp critical (MagickCore_FlopImage)
1367 proceed=SetImageProgress(image,FlopImageTag,progress++,image->rows);
1368 if (proceed == MagickFalse)
1372 flop_view=DestroyCacheView(flop_view);
1373 image_view=DestroyCacheView(image_view);
1374 flop_image->type=image->type;
1375 if (page.width != 0)
1376 page.x=(ssize_t) (page.width-flop_image->columns-page.x);
1377 flop_image->page=page;
1378 if (status == MagickFalse)
1379 flop_image=DestroyImage(flop_image);
1384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388 % R o l l I m a g e %
1392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1394 % RollImage() offsets an image as defined by x_offset and y_offset.
1396 % The format of the RollImage method is:
1398 % Image *RollImage(const Image *image,const ssize_t x_offset,
1399 % const ssize_t y_offset,ExceptionInfo *exception)
1401 % A description of each parameter follows:
1403 % o image: the image.
1405 % o x_offset: the number of columns to roll in the horizontal direction.
1407 % o y_offset: the number of rows to roll in the vertical direction.
1409 % o exception: return any errors or warnings in this structure.
1413 static inline MagickBooleanType CopyImageRegion(Image *destination,
1414 const Image *source,const size_t columns,const size_t rows,
1415 const ssize_t sx,const ssize_t sy,const ssize_t dx,const ssize_t dy,
1416 ExceptionInfo *exception)
1429 source_view=AcquireCacheView(source);
1430 destination_view=AcquireCacheView(destination);
1431 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1432 #pragma omp parallel for schedule(static) shared(status)
1434 for (y=0; y < (ssize_t) rows; y++)
1439 register const Quantum
1451 if (status == MagickFalse)
1453 p=GetCacheViewVirtualPixels(source_view,sx,sy+y,columns,1,exception);
1454 q=GetCacheViewAuthenticPixels(destination_view,dx,dy+y,columns,1,exception);
1455 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1460 for (x=0; x < (ssize_t) columns; x++)
1465 if (GetPixelMask(source,p) != 0)
1467 p+=GetPixelChannels(source);
1468 q+=GetPixelChannels(destination);
1471 for (i=0; i < (ssize_t) GetPixelChannels(source); i++)
1480 channel=GetPixelChannelMapChannel(source,i);
1481 source_traits=GetPixelChannelMapTraits(source,channel);
1482 destination_traits=GetPixelChannelMapTraits(destination,channel);
1483 if ((source_traits == UndefinedPixelTrait) ||
1484 (destination_traits == UndefinedPixelTrait))
1486 SetPixelChannel(destination,channel,p[i],q);
1488 p+=GetPixelChannels(source);
1489 q+=GetPixelChannels(destination);
1491 sync=SyncCacheViewAuthenticPixels(destination_view,exception);
1492 if (sync == MagickFalse)
1495 destination_view=DestroyCacheView(destination_view);
1496 source_view=DestroyCacheView(source_view);
1500 MagickExport Image *RollImage(const Image *image,const ssize_t x_offset,
1501 const ssize_t y_offset,ExceptionInfo *exception)
1503 #define RollImageTag "Roll/Image"
1515 Initialize roll image attributes.
1517 assert(image != (const Image *) NULL);
1518 assert(image->signature == MagickSignature);
1519 if (image->debug != MagickFalse)
1520 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1521 assert(exception != (ExceptionInfo *) NULL);
1522 assert(exception->signature == MagickSignature);
1523 roll_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1524 if (roll_image == (Image *) NULL)
1525 return((Image *) NULL);
1528 while (offset.x < 0)
1529 offset.x+=(ssize_t) image->columns;
1530 while (offset.x >= (ssize_t) image->columns)
1531 offset.x-=(ssize_t) image->columns;
1532 while (offset.y < 0)
1533 offset.y+=(ssize_t) image->rows;
1534 while (offset.y >= (ssize_t) image->rows)
1535 offset.y-=(ssize_t) image->rows;
1539 status=CopyImageRegion(roll_image,image,(size_t) offset.x,
1540 (size_t) offset.y,(ssize_t) image->columns-offset.x,(ssize_t) image->rows-
1541 offset.y,0,0,exception);
1542 (void) SetImageProgress(image,RollImageTag,0,3);
1543 status|=CopyImageRegion(roll_image,image,image->columns-offset.x,
1544 (size_t) offset.y,0,(ssize_t) image->rows-offset.y,offset.x,0,
1546 (void) SetImageProgress(image,RollImageTag,1,3);
1547 status|=CopyImageRegion(roll_image,image,(size_t) offset.x,image->rows-
1548 offset.y,(ssize_t) image->columns-offset.x,0,0,offset.y,exception);
1549 (void) SetImageProgress(image,RollImageTag,2,3);
1550 status|=CopyImageRegion(roll_image,image,image->columns-offset.x,image->rows-
1551 offset.y,0,0,offset.x,offset.y,exception);
1552 (void) SetImageProgress(image,RollImageTag,3,3);
1553 roll_image->type=image->type;
1554 if (status == MagickFalse)
1555 roll_image=DestroyImage(roll_image);
1560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1564 % S h a v e I m a g e %
1568 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1570 % ShaveImage() shaves pixels from the image edges. It allocates the memory
1571 % necessary for the new Image structure and returns a pointer to the new
1574 % The format of the ShaveImage method is:
1576 % Image *ShaveImage(const Image *image,const RectangleInfo *shave_info,
1577 % ExceptionInfo *exception)
1579 % A description of each parameter follows:
1581 % o shave_image: Method ShaveImage returns a pointer to the shaved
1582 % image. A null image is returned if there is a memory shortage or
1583 % if the image width or height is zero.
1585 % o image: the image.
1587 % o shave_info: Specifies a pointer to a RectangleInfo which defines the
1588 % region of the image to crop.
1590 % o exception: return any errors or warnings in this structure.
1593 MagickExport Image *ShaveImage(const Image *image,
1594 const RectangleInfo *shave_info,ExceptionInfo *exception)
1602 assert(image != (const Image *) NULL);
1603 assert(image->signature == MagickSignature);
1604 if (image->debug != MagickFalse)
1605 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1606 if (((2*shave_info->width) >= image->columns) ||
1607 ((2*shave_info->height) >= image->rows))
1608 ThrowImageException(OptionWarning,"GeometryDoesNotContainImage");
1609 SetGeometry(image,&geometry);
1610 geometry.width-=2*shave_info->width;
1611 geometry.height-=2*shave_info->height;
1612 geometry.x=(ssize_t) shave_info->width+image->page.x;
1613 geometry.y=(ssize_t) shave_info->height+image->page.y;
1614 shave_image=CropImage(image,&geometry,exception);
1615 if (shave_image == (Image *) NULL)
1616 return((Image *) NULL);
1617 shave_image->page.width-=2*shave_info->width;
1618 shave_image->page.height-=2*shave_info->height;
1619 shave_image->page.x-=(ssize_t) shave_info->width;
1620 shave_image->page.y-=(ssize_t) shave_info->height;
1621 return(shave_image);
1625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1629 % S p l i c e I m a g e %
1633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1635 % SpliceImage() splices a solid color into the image as defined by the
1638 % The format of the SpliceImage method is:
1640 % Image *SpliceImage(const Image *image,const RectangleInfo *geometry,
1641 % ExceptionInfo *exception)
1643 % A description of each parameter follows:
1645 % o image: the image.
1647 % o geometry: Define the region of the image to splice with members
1648 % x, y, width, and height.
1650 % o exception: return any errors or warnings in this structure.
1653 MagickExport Image *SpliceImage(const Image *image,
1654 const RectangleInfo *geometry,ExceptionInfo *exception)
1656 #define SpliceImageTag "Splice/Image"
1678 Allocate splice image.
1680 assert(image != (const Image *) NULL);
1681 assert(image->signature == MagickSignature);
1682 if (image->debug != MagickFalse)
1683 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1684 assert(geometry != (const RectangleInfo *) NULL);
1685 assert(exception != (ExceptionInfo *) NULL);
1686 assert(exception->signature == MagickSignature);
1687 splice_geometry=(*geometry);
1688 splice_image=CloneImage(image,image->columns+splice_geometry.width,
1689 image->rows+splice_geometry.height,MagickTrue,exception);
1690 if (splice_image == (Image *) NULL)
1691 return((Image *) NULL);
1692 if (SetImageStorageClass(splice_image,DirectClass,exception) == MagickFalse)
1694 splice_image=DestroyImage(splice_image);
1695 return((Image *) NULL);
1697 (void) SetImageBackgroundColor(splice_image,exception);
1699 Respect image geometry.
1701 switch (image->gravity)
1704 case UndefinedGravity:
1705 case NorthWestGravity:
1709 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1712 case NorthEastGravity:
1714 splice_geometry.x+=(ssize_t) splice_geometry.width;
1719 splice_geometry.y+=(ssize_t) splice_geometry.width/2;
1725 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1726 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1731 splice_geometry.x+=(ssize_t) splice_geometry.width;
1732 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1735 case SouthWestGravity:
1737 splice_geometry.y+=(ssize_t) splice_geometry.height;
1742 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1743 splice_geometry.y+=(ssize_t) splice_geometry.height;
1746 case SouthEastGravity:
1748 splice_geometry.x+=(ssize_t) splice_geometry.width;
1749 splice_geometry.y+=(ssize_t) splice_geometry.height;
1758 image_view=AcquireCacheView(image);
1759 splice_view=AcquireCacheView(splice_image);
1760 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1761 #pragma omp parallel for schedule(static,4) shared(progress,status)
1763 for (y=0; y < (ssize_t) splice_geometry.y; y++)
1765 register const Quantum
1774 if (status == MagickFalse)
1776 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1777 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1779 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1784 for (x=0; x < splice_geometry.x; x++)
1789 if (GetPixelMask(image,p) != 0)
1791 p+=GetPixelChannels(image);
1792 q+=GetPixelChannels(splice_image);
1795 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1804 channel=GetPixelChannelMapChannel(image,i);
1805 traits=GetPixelChannelMapTraits(image,channel);
1806 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1807 if ((traits == UndefinedPixelTrait) ||
1808 (splice_traits == UndefinedPixelTrait))
1810 SetPixelChannel(splice_image,channel,p[i],q);
1812 p+=GetPixelChannels(image);
1813 q+=GetPixelChannels(splice_image);
1815 for ( ; x < (ssize_t) (splice_geometry.x+splice_geometry.width); x++)
1816 q+=GetPixelChannels(splice_image);
1817 for ( ; x < (ssize_t) splice_image->columns; x++)
1822 if (GetPixelMask(image,p) != 0)
1824 p+=GetPixelChannels(image);
1825 q+=GetPixelChannels(splice_image);
1828 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1837 channel=GetPixelChannelMapChannel(image,i);
1838 traits=GetPixelChannelMapTraits(image,channel);
1839 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1840 if ((traits == UndefinedPixelTrait) ||
1841 (splice_traits == UndefinedPixelTrait))
1843 SetPixelChannel(splice_image,channel,p[i],q);
1845 p+=GetPixelChannels(image);
1846 q+=GetPixelChannels(splice_image);
1848 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
1850 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1855 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1856 #pragma omp critical (MagickCore_TransposeImage)
1858 proceed=SetImageProgress(image,SpliceImageTag,progress++,
1859 splice_image->rows);
1860 if (proceed == MagickFalse)
1864 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1865 #pragma omp parallel for schedule(static,4) shared(progress,status)
1867 for (y=(ssize_t) (splice_geometry.y+splice_geometry.height);
1868 y < (ssize_t) splice_image->rows; y++)
1870 register const Quantum
1879 if (status == MagickFalse)
1881 p=GetCacheViewVirtualPixels(image_view,0,y-(ssize_t) splice_geometry.height,
1882 image->columns,1,exception);
1883 if ((y < 0) || (y >= (ssize_t) splice_image->rows))
1885 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1887 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1892 for (x=0; x < splice_geometry.x; x++)
1897 if (GetPixelMask(image,q) != 0)
1899 p+=GetPixelChannels(image);
1900 q+=GetPixelChannels(splice_image);
1903 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1912 channel=GetPixelChannelMapChannel(image,i);
1913 traits=GetPixelChannelMapTraits(image,channel);
1914 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1915 if ((traits == UndefinedPixelTrait) ||
1916 (splice_traits == UndefinedPixelTrait))
1918 SetPixelChannel(splice_image,channel,p[i],q);
1920 p+=GetPixelChannels(image);
1921 q+=GetPixelChannels(splice_image);
1923 for ( ; x < (ssize_t) (splice_geometry.x+splice_geometry.width); x++)
1924 q+=GetPixelChannels(splice_image);
1925 for ( ; x < (ssize_t) splice_image->columns; x++)
1930 if (GetPixelMask(image,q) != 0)
1932 p+=GetPixelChannels(image);
1933 q+=GetPixelChannels(splice_image);
1936 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1945 channel=GetPixelChannelMapChannel(image,i);
1946 traits=GetPixelChannelMapTraits(image,channel);
1947 splice_traits=GetPixelChannelMapTraits(splice_image,channel);
1948 if ((traits == UndefinedPixelTrait) ||
1949 (splice_traits == UndefinedPixelTrait))
1951 SetPixelChannel(splice_image,channel,p[i],q);
1953 p+=GetPixelChannels(image);
1954 q+=GetPixelChannels(splice_image);
1956 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
1958 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1963 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1964 #pragma omp critical (MagickCore_TransposeImage)
1966 proceed=SetImageProgress(image,SpliceImageTag,progress++,
1967 splice_image->rows);
1968 if (proceed == MagickFalse)
1972 splice_view=DestroyCacheView(splice_view);
1973 image_view=DestroyCacheView(image_view);
1974 if (status == MagickFalse)
1975 splice_image=DestroyImage(splice_image);
1976 return(splice_image);
1980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1984 % T r a n s f o r m I m a g e %
1988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1990 % TransformImage() is a convenience method that behaves like ResizeImage() or
1991 % CropImage() but accepts scaling and/or cropping information as a region
1992 % geometry specification. If the operation fails, the original image handle
1995 % This should only be used for single images.
1997 % This function destroys what it assumes to be a single image list.
1998 % If the input image is part of a larger list, all other images in that list
1999 % will be simply 'lost', not destroyed.
2001 % Also if the crop generates a list of images only the first image is resized.
2002 % And finally if the crop succeeds and the resize failed, you will get a
2003 % cropped image, as well as a 'false' or 'failed' report.
2005 % This function and should probably be depreciated in favor of direct calls
2006 % to CropImageToTiles() or ResizeImage(), as appropriate.
2008 % The format of the TransformImage method is:
2010 % MagickBooleanType TransformImage(Image **image,const char *crop_geometry,
2011 % const char *image_geometry,ExceptionInfo *exception)
2013 % A description of each parameter follows:
2015 % o image: the image The transformed image is returned as this parameter.
2017 % o crop_geometry: A crop geometry string. This geometry defines a
2018 % subregion of the image to crop.
2020 % o image_geometry: An image geometry string. This geometry defines the
2021 % final size of the image.
2023 % o exception: return any errors or warnings in this structure.
2026 MagickExport MagickBooleanType TransformImage(Image **image,
2027 const char *crop_geometry,const char *image_geometry,ExceptionInfo *exception)
2039 assert(image != (Image **) NULL);
2040 assert((*image)->signature == MagickSignature);
2041 if ((*image)->debug != MagickFalse)
2042 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
2043 transform_image=(*image);
2044 if (crop_geometry != (const char *) NULL)
2050 Crop image to a user specified size.
2052 crop_image=CropImageToTiles(*image,crop_geometry,exception);
2053 if (crop_image == (Image *) NULL)
2054 transform_image=CloneImage(*image,0,0,MagickTrue,exception);
2057 transform_image=DestroyImage(transform_image);
2058 transform_image=GetFirstImageInList(crop_image);
2060 *image=transform_image;
2062 if (image_geometry == (const char *) NULL)
2066 Scale image to a user specified size.
2068 flags=ParseRegionGeometry(transform_image,image_geometry,&geometry,exception);
2070 if ((transform_image->columns == geometry.width) &&
2071 (transform_image->rows == geometry.height))
2073 resize_image=ResizeImage(transform_image,geometry.width,geometry.height,
2074 transform_image->filter,exception);
2075 if (resize_image == (Image *) NULL)
2076 return(MagickFalse);
2077 transform_image=DestroyImage(transform_image);
2078 transform_image=resize_image;
2079 *image=transform_image;
2084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2088 % T r a n s f o r m I m a g e s %
2092 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2094 % TransformImages() calls TransformImage() on each image of a sequence.
2096 % The format of the TransformImage method is:
2098 % MagickBooleanType TransformImages(Image **image,
2099 % const char *crop_geometry,const char *image_geometry,
2100 % ExceptionInfo *exception)
2102 % A description of each parameter follows:
2104 % o image: the image The transformed image is returned as this parameter.
2106 % o crop_geometry: A crop geometry string. This geometry defines a
2107 % subregion of the image to crop.
2109 % o image_geometry: An image geometry string. This geometry defines the
2110 % final size of the image.
2112 % o exception: return any errors or warnings in this structure.
2115 MagickExport MagickBooleanType TransformImages(Image **images,
2116 const char *crop_geometry,const char *image_geometry,ExceptionInfo *exception)
2129 assert(images != (Image **) NULL);
2130 assert((*images)->signature == MagickSignature);
2131 if ((*images)->debug != MagickFalse)
2132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
2133 (*images)->filename);
2134 image_list=ImageListToArray(*images,exception);
2135 if (image_list == (Image **) NULL)
2136 return(MagickFalse);
2138 transform_images=NewImageList();
2139 for (i=0; image_list[i] != (Image *) NULL; i++)
2141 image=image_list[i];
2142 status|=TransformImage(&image,crop_geometry,image_geometry,exception);
2143 AppendImageToList(&transform_images,image);
2145 *images=transform_images;
2146 image_list=(Image **) RelinquishMagickMemory(image_list);
2147 return(status != 0 ? MagickTrue : MagickFalse);
2151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2155 % T r a n s p o s e I m a g e %
2159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2161 % TransposeImage() creates a horizontal mirror image by reflecting the pixels
2162 % around the central y-axis while rotating them by 90 degrees.
2164 % The format of the TransposeImage method is:
2166 % Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2168 % A description of each parameter follows:
2170 % o image: the image.
2172 % o exception: return any errors or warnings in this structure.
2175 MagickExport Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2177 #define TransposeImageTag "Transpose/Image"
2198 assert(image != (const Image *) NULL);
2199 assert(image->signature == MagickSignature);
2200 if (image->debug != MagickFalse)
2201 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2202 assert(exception != (ExceptionInfo *) NULL);
2203 assert(exception->signature == MagickSignature);
2204 transpose_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2206 if (transpose_image == (Image *) NULL)
2207 return((Image *) NULL);
2213 image_view=AcquireCacheView(image);
2214 transpose_view=AcquireCacheView(transpose_image);
2215 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2216 #pragma omp parallel for schedule(static,4) shared(progress,status)
2218 for (y=0; y < (ssize_t) image->rows; y++)
2220 register const Quantum
2229 if (status == MagickFalse)
2231 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-y-1,
2232 image->columns,1,exception);
2233 q=QueueCacheViewAuthenticPixels(transpose_view,(ssize_t) (image->rows-y-1),
2234 0,1,transpose_image->rows,exception);
2235 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2240 for (x=0; x < (ssize_t) image->columns; x++)
2245 if (GetPixelMask(image,q) != 0)
2247 p+=GetPixelChannels(image);
2248 q+=GetPixelChannels(transpose_image);
2251 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2260 channel=GetPixelChannelMapChannel(image,i);
2261 traits=GetPixelChannelMapTraits(image,channel);
2262 transpose_traits=GetPixelChannelMapTraits(transpose_image,channel);
2263 if ((traits == UndefinedPixelTrait) ||
2264 (transpose_traits == UndefinedPixelTrait))
2266 SetPixelChannel(transpose_image,channel,p[i],q);
2268 p+=GetPixelChannels(image);
2269 q+=GetPixelChannels(transpose_image);
2271 if (SyncCacheViewAuthenticPixels(transpose_view,exception) == MagickFalse)
2273 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2278 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2279 #pragma omp critical (MagickCore_TransposeImage)
2281 proceed=SetImageProgress(image,TransposeImageTag,progress++,
2283 if (proceed == MagickFalse)
2287 transpose_view=DestroyCacheView(transpose_view);
2288 image_view=DestroyCacheView(image_view);
2289 transpose_image->type=image->type;
2290 page=transpose_image->page;
2291 Swap(page.width,page.height);
2292 Swap(page.x,page.y);
2293 transpose_image->page=page;
2294 if (status == MagickFalse)
2295 transpose_image=DestroyImage(transpose_image);
2296 return(transpose_image);
2300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2304 % T r a n s v e r s e I m a g e %
2308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2310 % TransverseImage() creates a vertical mirror image by reflecting the pixels
2311 % around the central x-axis while rotating them by 270 degrees.
2313 % The format of the TransverseImage method is:
2315 % Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2317 % A description of each parameter follows:
2319 % o image: the image.
2321 % o exception: return any errors or warnings in this structure.
2324 MagickExport Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2326 #define TransverseImageTag "Transverse/Image"
2347 assert(image != (const Image *) NULL);
2348 assert(image->signature == MagickSignature);
2349 if (image->debug != MagickFalse)
2350 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2351 assert(exception != (ExceptionInfo *) NULL);
2352 assert(exception->signature == MagickSignature);
2353 transverse_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2355 if (transverse_image == (Image *) NULL)
2356 return((Image *) NULL);
2362 image_view=AcquireCacheView(image);
2363 transverse_view=AcquireCacheView(transverse_image);
2364 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2365 #pragma omp parallel for schedule(static,4) shared(progress,status)
2367 for (y=0; y < (ssize_t) image->rows; y++)
2372 register const Quantum
2381 if (status == MagickFalse)
2383 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2384 q=QueueCacheViewAuthenticPixels(transverse_view,(ssize_t) (image->rows-y-1),
2385 0,1,transverse_image->rows,exception);
2386 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2391 q+=GetPixelChannels(transverse_image)*image->columns;
2392 for (x=0; x < (ssize_t) image->columns; x++)
2397 q-=GetPixelChannels(transverse_image);
2398 if (GetPixelMask(image,p) != 0)
2400 p+=GetPixelChannels(image);
2403 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2412 channel=GetPixelChannelMapChannel(image,i);
2413 traits=GetPixelChannelMapTraits(image,channel);
2414 transverse_traits=GetPixelChannelMapTraits(transverse_image,channel);
2415 if ((traits == UndefinedPixelTrait) ||
2416 (transverse_traits == UndefinedPixelTrait))
2418 SetPixelChannel(transverse_image,channel,p[i],q);
2420 p+=GetPixelChannels(image);
2422 sync=SyncCacheViewAuthenticPixels(transverse_view,exception);
2423 if (sync == MagickFalse)
2425 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2430 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2431 #pragma omp critical (MagickCore_TransverseImage)
2433 proceed=SetImageProgress(image,TransverseImageTag,progress++,
2435 if (proceed == MagickFalse)
2439 transverse_view=DestroyCacheView(transverse_view);
2440 image_view=DestroyCacheView(image_view);
2441 transverse_image->type=image->type;
2442 page=transverse_image->page;
2443 Swap(page.width,page.height);
2444 Swap(page.x,page.y);
2445 if (page.width != 0)
2446 page.x=(ssize_t) (page.width-transverse_image->columns-page.x);
2447 if (page.height != 0)
2448 page.y=(ssize_t) (page.height-transverse_image->rows-page.y);
2449 transverse_image->page=page;
2450 if (status == MagickFalse)
2451 transverse_image=DestroyImage(transverse_image);
2452 return(transverse_image);
2456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2460 % T r i m I m a g e %
2464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2466 % TrimImage() trims pixels from the image edges. It allocates the memory
2467 % necessary for the new Image structure and returns a pointer to the new
2470 % The format of the TrimImage method is:
2472 % Image *TrimImage(const Image *image,ExceptionInfo *exception)
2474 % A description of each parameter follows:
2476 % o image: the image.
2478 % o exception: return any errors or warnings in this structure.
2481 MagickExport Image *TrimImage(const Image *image,ExceptionInfo *exception)
2486 assert(image != (const Image *) NULL);
2487 assert(image->signature == MagickSignature);
2488 if (image->debug != MagickFalse)
2489 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2490 geometry=GetImageBoundingBox(image,exception);
2491 if ((geometry.width == 0) || (geometry.height == 0))
2496 crop_image=CloneImage(image,1,1,MagickTrue,exception);
2497 if (crop_image == (Image *) NULL)
2498 return((Image *) NULL);
2499 crop_image->background_color.alpha=(Quantum) TransparentAlpha;
2500 (void) SetImageBackgroundColor(crop_image,exception);
2501 crop_image->page=image->page;
2502 crop_image->page.x=(-1);
2503 crop_image->page.y=(-1);
2506 geometry.x+=image->page.x;
2507 geometry.y+=image->page.y;
2508 return(CropImage(image,&geometry,exception));