MagickCore 7.1.2-37
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
transform.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
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 %
11% %
12% %
13% MagickCore Image Transform Methods %
14% %
15% Software Design %
16% Cristy %
17% July 1992 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/license/ %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38␌
39/*
40 Include declarations.
41*/
42#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/artifact.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/cache-view.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colorspace-private.h"
50#include "MagickCore/composite.h"
51#include "MagickCore/composite-private.h"
52#include "MagickCore/distort.h"
53#include "MagickCore/draw.h"
54#include "MagickCore/effect.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/exception-private.h"
57#include "MagickCore/geometry.h"
58#include "MagickCore/image.h"
59#include "MagickCore/memory_.h"
60#include "MagickCore/layer.h"
61#include "MagickCore/list.h"
62#include "MagickCore/monitor.h"
63#include "MagickCore/monitor-private.h"
64#include "MagickCore/pixel-accessor.h"
65#include "MagickCore/profile-private.h"
66#include "MagickCore/property.h"
67#include "MagickCore/resource_.h"
68#include "MagickCore/resize.h"
69#include "MagickCore/statistic.h"
70#include "MagickCore/string_.h"
71#include "MagickCore/thread-private.h"
72#include "MagickCore/transform.h"
73#include "MagickCore/transform-private.h"
74␌
75/*
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77% %
78% %
79% %
80% A u t o O r i e n t I m a g e %
81% %
82% %
83% %
84%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85%
86% AutoOrientImage() adjusts an image so that its orientation is suitable for
87% viewing (i.e. top-left orientation).
88%
89% The format of the AutoOrientImage method is:
90%
91% Image *AutoOrientImage(const Image *image,
92% const OrientationType orientation,ExceptionInfo *exception)
93%
94% A description of each parameter follows:
95%
96% o image: The image.
97%
98% o orientation: Current image orientation.
99%
100% o exception: Return any errors or warnings in this structure.
101%
102*/
103MagickExport Image *AutoOrientImage(const Image *image,
104 const OrientationType orientation,ExceptionInfo *exception)
105{
106 Image
107 *orient_image;
108
109 assert(image != (const Image *) NULL);
110 assert(image->signature == MagickCoreSignature);
111 assert(exception != (ExceptionInfo *) NULL);
112 assert(exception->signature == MagickCoreSignature);
113 orient_image=(Image *) NULL;
114 switch (orientation)
115 {
116 case UndefinedOrientation:
117 case TopLeftOrientation:
118 default:
119 {
120 orient_image=CloneImage(image,0,0,MagickTrue,exception);
121 break;
122 }
123 case TopRightOrientation:
124 {
125 orient_image=FlopImage(image,exception);
126 break;
127 }
128 case BottomRightOrientation:
129 {
130 orient_image=RotateImage(image,180.0,exception);
131 break;
132 }
133 case BottomLeftOrientation:
134 {
135 orient_image=FlipImage(image,exception);
136 break;
137 }
138 case LeftTopOrientation:
139 {
140 orient_image=TransposeImage(image,exception);
141 break;
142 }
143 case RightTopOrientation:
144 {
145 orient_image=RotateImage(image,90.0,exception);
146 break;
147 }
148 case RightBottomOrientation:
149 {
150 orient_image=TransverseImage(image,exception);
151 break;
152 }
153 case LeftBottomOrientation:
154 {
155 orient_image=RotateImage(image,270.0,exception);
156 break;
157 }
158 }
159 if (orient_image != (Image *) NULL)
160 orient_image->orientation=TopLeftOrientation;
161 return(orient_image);
162}
163␌
164/*
165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166% %
167% %
168% %
169% C h o p I m a g e %
170% %
171% %
172% %
173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174%
175% ChopImage() removes a region of an image and collapses the image to occupy
176% the removed portion.
177%
178% The format of the ChopImage method is:
179%
180% Image *ChopImage(const Image *image,const RectangleInfo *chop_info,
181% ExceptionInfo *exception)
182%
183% A description of each parameter follows:
184%
185% o image: the image.
186%
187% o chop_info: Define the region of the image to chop.
188%
189% o exception: return any errors or warnings in this structure.
190%
191*/
192MagickExport Image *ChopImage(const Image *image,const RectangleInfo *chop_info,
193 ExceptionInfo *exception)
194{
195#define ChopImageTag "Chop/Image"
196
197 CacheView
198 *chop_view,
199 *image_view;
200
201 Image
202 *chop_image;
203
204 MagickBooleanType
205 status;
206
207 MagickOffsetType
208 progress;
209
210 RectangleInfo
211 extent;
212
213 ssize_t
214 y;
215
216 /*
217 Check chop geometry.
218 */
219 assert(image != (const Image *) NULL);
220 assert(image->signature == MagickCoreSignature);
221 assert(exception != (ExceptionInfo *) NULL);
222 assert(exception->signature == MagickCoreSignature);
223 assert(chop_info != (RectangleInfo *) NULL);
224 if (IsEventLogging() != MagickFalse)
225 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
226 if (((chop_info->x+(ssize_t) chop_info->width) < 0) ||
227 ((chop_info->y+(ssize_t) chop_info->height) < 0) ||
228 (chop_info->x > (ssize_t) image->columns) ||
229 (chop_info->y > (ssize_t) image->rows))
230 ThrowImageException(OptionWarning,"GeometryDoesNotContainImage");
231 extent=(*chop_info);
232 if ((extent.x+(ssize_t) extent.width) > (ssize_t) image->columns)
233 extent.width=(size_t) ((ssize_t) image->columns-extent.x);
234 if ((extent.y+(ssize_t) extent.height) > (ssize_t) image->rows)
235 extent.height=(size_t) ((ssize_t) image->rows-extent.y);
236 if (extent.x < 0)
237 {
238 extent.width-=(size_t) (-extent.x);
239 extent.x=0;
240 }
241 if (extent.y < 0)
242 {
243 extent.height-=(size_t) (-extent.y);
244 extent.y=0;
245 }
246 if ((extent.width >= image->columns) || (extent.height >= image->rows))
247 ThrowImageException(OptionWarning,"GeometryDoesNotContainImage");
248 chop_image=CloneImage(image,image->columns-extent.width,image->rows-
249 extent.height,MagickTrue,exception);
250 if (chop_image == (Image *) NULL)
251 return((Image *) NULL);
252 /*
253 Extract chop image.
254 */
255 status=MagickTrue;
256 progress=0;
257 image_view=AcquireVirtualCacheView(image,exception);
258 chop_view=AcquireAuthenticCacheView(chop_image,exception);
259#if defined(MAGICKCORE_OPENMP_SUPPORT)
260 #pragma omp parallel for schedule(static) shared(status) \
261 magick_number_threads(image,chop_image,(size_t) extent.y,2)
262#endif
263 for (y=0; y < (ssize_t) extent.y; y++)
264 {
265 const Quantum
266 *magick_restrict p;
267
268 ssize_t
269 x;
270
271 Quantum
272 *magick_restrict q;
273
274 if (status == MagickFalse)
275 continue;
276 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
277 q=QueueCacheViewAuthenticPixels(chop_view,0,y,chop_image->columns,1,
278 exception);
279 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
280 {
281 status=MagickFalse;
282 continue;
283 }
284 for (x=0; x < (ssize_t) image->columns; x++)
285 {
286 if ((x < extent.x) || (x >= (extent.x+(ssize_t) extent.width)))
287 {
288 ssize_t
289 i;
290
291 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
292 {
293 PixelChannel channel = GetPixelChannelChannel(image,i);
294 PixelTrait traits = GetPixelChannelTraits(image,channel);
295 PixelTrait chop_traits=GetPixelChannelTraits(chop_image,channel);
296 if ((traits == UndefinedPixelTrait) ||
297 (chop_traits == UndefinedPixelTrait))
298 continue;
299 SetPixelChannel(chop_image,channel,p[i],q);
300 }
301 q+=(ptrdiff_t) GetPixelChannels(chop_image);
302 }
303 p+=(ptrdiff_t) GetPixelChannels(image);
304 }
305 if (SyncCacheViewAuthenticPixels(chop_view,exception) == MagickFalse)
306 status=MagickFalse;
307 if (image->progress_monitor != (MagickProgressMonitor) NULL)
308 {
309 MagickBooleanType
310 proceed;
311
312#if defined(MAGICKCORE_OPENMP_SUPPORT)
313 #pragma omp atomic
314#endif
315 progress++;
316 proceed=SetImageProgress(image,ChopImageTag,progress,image->rows);
317 if (proceed == MagickFalse)
318 status=MagickFalse;
319 }
320 }
321 /*
322 Extract chop image.
323 */
324#if defined(MAGICKCORE_OPENMP_SUPPORT)
325 #pragma omp parallel for schedule(static) shared(progress,status) \
326 magick_number_threads(image,chop_image,image->rows-((size_t) extent.y+extent.height),2)
327#endif
328 for (y=0; y < (ssize_t) (image->rows-((size_t) extent.y+extent.height)); y++)
329 {
330 const Quantum
331 *magick_restrict p;
332
333 ssize_t
334 x;
335
336 Quantum
337 *magick_restrict q;
338
339 if (status == MagickFalse)
340 continue;
341 p=GetCacheViewVirtualPixels(image_view,0,extent.y+(ssize_t) extent.height+y,
342 image->columns,1,exception);
343 q=QueueCacheViewAuthenticPixels(chop_view,0,extent.y+y,chop_image->columns,
344 1,exception);
345 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
346 {
347 status=MagickFalse;
348 continue;
349 }
350 for (x=0; x < (ssize_t) image->columns; x++)
351 {
352 if ((x < extent.x) || (x >= (extent.x+(ssize_t) extent.width)))
353 {
354 ssize_t
355 i;
356
357 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
358 {
359 PixelChannel channel = GetPixelChannelChannel(image,i);
360 PixelTrait traits = GetPixelChannelTraits(image,channel);
361 PixelTrait chop_traits=GetPixelChannelTraits(chop_image,channel);
362 if ((traits == UndefinedPixelTrait) ||
363 (chop_traits == UndefinedPixelTrait))
364 continue;
365 SetPixelChannel(chop_image,channel,p[i],q);
366 }
367 q+=(ptrdiff_t) GetPixelChannels(chop_image);
368 }
369 p+=(ptrdiff_t) GetPixelChannels(image);
370 }
371 if (SyncCacheViewAuthenticPixels(chop_view,exception) == MagickFalse)
372 status=MagickFalse;
373 if (image->progress_monitor != (MagickProgressMonitor) NULL)
374 {
375 MagickBooleanType
376 proceed;
377
378#if defined(MAGICKCORE_OPENMP_SUPPORT)
379 #pragma omp atomic
380#endif
381 progress++;
382 proceed=SetImageProgress(image,ChopImageTag,progress,image->rows);
383 if (proceed == MagickFalse)
384 status=MagickFalse;
385 }
386 }
387 chop_view=DestroyCacheView(chop_view);
388 image_view=DestroyCacheView(image_view);
389 chop_image->type=image->type;
390 if (status == MagickFalse)
391 chop_image=DestroyImage(chop_image);
392 return(chop_image);
393}
394␌
395/*
396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397% %
398% %
399% %
400+ C o n s o l i d a t e C M Y K I m a g e %
401% %
402% %
403% %
404%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405%
406% ConsolidateCMYKImage() consolidates separate C, M, Y, and K planes into a
407% single image.
408%
409% The format of the ConsolidateCMYKImage method is:
410%
411% Image *ConsolidateCMYKImage(const Image *image,ExceptionInfo *exception)
412%
413% A description of each parameter follows:
414%
415% o image: the image sequence.
416%
417% o exception: return any errors or warnings in this structure.
418%
419*/
420MagickExport Image *ConsolidateCMYKImages(const Image *images,
421 ExceptionInfo *exception)
422{
423 CacheView
424 *cmyk_view,
425 *image_view;
426
427 Image
428 *cmyk_image,
429 *cmyk_images,
430 *p_image;
431
432 ssize_t
433 j,
434 y;
435
436 /*
437 Consolidate separate C, M, Y, and K planes into a single image.
438 */
439 assert(images != (Image *) NULL);
440 assert(images->signature == MagickCoreSignature);
441 assert(exception != (ExceptionInfo *) NULL);
442 assert(exception->signature == MagickCoreSignature);
443 if (IsEventLogging() != MagickFalse)
444 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
445 cmyk_images=NewImageList();
446 p_image=GetFirstImageInList(images);
447 for (j=0; j < (ssize_t) GetImageListLength(p_image); j+=4)
448 {
449 const Image
450 *plane_images[4],
451 *q_image = p_image;
452
453 ssize_t
454 i;
455
456 /*
457 Gather and validate the next 4 planes.
458 */
459 for (i=0; i < 4; i++)
460 {
461 if (q_image == (const Image *) NULL)
462 break;
463 plane_images[i]=q_image;
464 q_image=GetNextImageInList(q_image);
465 }
466 if (i < 4)
467 {
468 ThrowMagickException(exception,GetMagickModule(),OptionError,
469 "ImagesAreNotTheSameSize","`%s'",p_image->filename);
470 break;
471 }
472 if ((plane_images[1]->columns != plane_images[0]->columns) ||
473 (plane_images[1]->rows != plane_images[0]->rows) ||
474 (plane_images[2]->columns != plane_images[0]->columns) ||
475 (plane_images[2]->rows != plane_images[0]->rows) ||
476 (plane_images[3]->columns != plane_images[0]->columns) ||
477 (plane_images[3]->rows != plane_images[0]->rows))
478 {
479 ThrowMagickException(exception,GetMagickModule(),OptionError,
480 "ImagesAreNotTheSameSize","`%s'",p_image->filename);
481 break;
482 }
483 cmyk_image=CloneImage(plane_images[0],0,0,MagickTrue,exception);
484 if (cmyk_image == (Image *) NULL)
485 break;
486 if (SetImageStorageClass(cmyk_image,DirectClass,exception) == MagickFalse)
487 {
488 cmyk_image=DestroyImage(cmyk_image);
489 break;
490 }
491 (void) SetImageColorspace(cmyk_image,CMYKColorspace,exception);
492 cmyk_view=AcquireAuthenticCacheView(cmyk_image,exception);
493 for (i=0; i < 4; i++)
494 {
495 image_view=AcquireVirtualCacheView(plane_images[i],exception);
496 for (y=0; y < (ssize_t) plane_images[i]->rows; y++)
497 {
498 const Quantum
499 *magick_restrict p;
500
501 Quantum
502 *magick_restrict q;
503
504 ssize_t
505 x;
506
507 p=GetCacheViewVirtualPixels(image_view,0,y,plane_images[i]->columns,1,
508 exception);
509 q=QueueCacheViewAuthenticPixels(cmyk_view,0,y,cmyk_image->columns,1,
510 exception);
511 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
512 break;
513 for (x=0; x < (ssize_t) plane_images[i]->columns; x++)
514 {
515 Quantum
516 pixel;
517
518 pixel=ClampToQuantum((double) QuantumRange-
519 GetPixelIntensity(plane_images[i],p));
520 switch (i)
521 {
522 case 0: SetPixelCyan(cmyk_image,pixel,q); break;
523 case 1: SetPixelMagenta(cmyk_image,pixel,q); break;
524 case 2: SetPixelYellow(cmyk_image,pixel,q); break;
525 case 3: SetPixelBlack(cmyk_image,pixel,q); break;
526 default: break;
527 }
528 p+=(ptrdiff_t) GetPixelChannels(plane_images[i]);
529 q+=(ptrdiff_t) GetPixelChannels(cmyk_image);
530 }
531 }
532 image_view=DestroyCacheView(image_view);
533 }
534 if (SyncCacheViewAuthenticPixels(cmyk_view,exception) == MagickFalse)
535 {
536 cmyk_view=DestroyCacheView(cmyk_view);
537 cmyk_image=DestroyImage(cmyk_image);
538 break;
539 }
540 cmyk_view=DestroyCacheView(cmyk_view);
541 AppendImageToList(&cmyk_images,cmyk_image);
542 for (i=0; (i < 4) && (p_image != (Image *) NULL); i++)
543 p_image=GetNextImageInList(p_image);
544 }
545 if (j < (ssize_t) GetImageListLength(images))
546 cmyk_images=DestroyImageList(cmyk_images);
547 return(cmyk_images);
548}
549␌
550/*
551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552% %
553% %
554% %
555% C r o p I m a g e %
556% %
557% %
558% %
559%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560%
561% CropImage() extracts a region of the image starting at the offset defined
562% by geometry. Region must be fully defined, and no special handling of
563% geometry flags is performed.
564%
565% The format of the CropImage method is:
566%
567% Image *CropImage(const Image *image,const RectangleInfo *geometry,
568% ExceptionInfo *exception)
569%
570% A description of each parameter follows:
571%
572% o image: the image.
573%
574% o geometry: Define the region of the image to crop with members
575% x, y, width, and height.
576%
577% o exception: return any errors or warnings in this structure.
578%
579*/
580MagickExport Image *CropImage(const Image *image,const RectangleInfo *geometry,
581 ExceptionInfo *exception)
582{
583#define CropImageTag "Crop/Image"
584
585 CacheView
586 *crop_view,
587 *image_view;
588
589 Image
590 *crop_image;
591
592 MagickBooleanType
593 status;
594
595 MagickOffsetType
596 progress;
597
598 OffsetInfo
599 offset;
600
601 RectangleInfo
602 bounding_box,
603 page;
604
605 ssize_t
606 y;
607
608 /*
609 Check crop geometry.
610 */
611 assert(image != (const Image *) NULL);
612 assert(image->signature == MagickCoreSignature);
613 assert(geometry != (const RectangleInfo *) NULL);
614 assert(exception != (ExceptionInfo *) NULL);
615 assert(exception->signature == MagickCoreSignature);
616 if (IsEventLogging() != MagickFalse)
617 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
618 bounding_box=image->page;
619 if ((bounding_box.width == 0) || (bounding_box.height == 0))
620 {
621 bounding_box.width=image->columns;
622 bounding_box.height=image->rows;
623 }
624 page=(*geometry);
625 if (page.width == 0)
626 page.width=bounding_box.width;
627 if (page.height == 0)
628 page.height=bounding_box.height;
629 if ((((double) bounding_box.x-page.x) >= (double) page.width) ||
630 (((double) bounding_box.y-page.y) >= (double) page.height) ||
631 (((double) page.x-bounding_box.x) > (double) image->columns) ||
632 (((double) page.y-bounding_box.y) > (double) image->rows))
633 {
634 /*
635 Crop is not within virtual canvas, return 1 pixel transparent image.
636 */
637 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
638 "GeometryDoesNotContainImage","(\"%.17gx%.17g%+.20g%+.20g\") `%s'",
639 (double) geometry->width,(double) geometry->height,
640 (double) geometry->x,(double) geometry->y,image->filename);
641 crop_image=CloneImage(image,1,1,MagickTrue,exception);
642 if (crop_image == (Image *) NULL)
643 return((Image *) NULL);
644 crop_image->background_color.alpha_trait=BlendPixelTrait;
645 crop_image->background_color.alpha=(MagickRealType) TransparentAlpha;
646 (void) SetImageBackgroundColor(crop_image,exception);
647 crop_image->page=bounding_box;
648 crop_image->page.x=(-1);
649 crop_image->page.y=(-1);
650 if (crop_image->dispose == BackgroundDispose)
651 crop_image->dispose=NoneDispose;
652 return(crop_image);
653 }
654 if ((page.x < 0) && (bounding_box.x >= 0))
655 {
656 page.width=CastDoubleToSizeT((double) page.width+page.x-bounding_box.x);
657 page.x=0;
658 }
659 else
660 {
661 page.width=CastDoubleToSizeT((double) page.width-(bounding_box.x-page.x));
662 page.x-=bounding_box.x;
663 if (page.x < 0)
664 page.x=0;
665 }
666 if ((page.y < 0) && (bounding_box.y >= 0))
667 {
668 page.height=CastDoubleToSizeT((double) page.height+page.y-bounding_box.y);
669 page.y=0;
670 }
671 else
672 {
673 page.height=CastDoubleToSizeT((double) page.height-(bounding_box.y-
674 page.y));
675 page.y-=bounding_box.y;
676 if (page.y < 0)
677 page.y=0;
678 }
679 if ((page.x+(ssize_t) page.width) > (ssize_t) image->columns)
680 page.width=(size_t) ((ssize_t) image->columns-page.x);
681 if ((geometry->width != 0) && (page.width > geometry->width))
682 page.width=geometry->width;
683 if ((page.y+(ssize_t) page.height) > (ssize_t) image->rows)
684 page.height=CastDoubleToSizeT((double) image->rows-page.y);
685 if ((geometry->height != 0) && (page.height > geometry->height))
686 page.height=geometry->height;
687 bounding_box.x+=page.x;
688 bounding_box.y+=page.y;
689 if ((page.width == 0) || (page.height == 0))
690 {
691 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
692 "GeometryDoesNotContainImage","`%s'",image->filename);
693 return((Image *) NULL);
694 }
695 /*
696 Initialize crop image attributes.
697 */
698 crop_image=CloneImage(image,page.width,page.height,MagickTrue,exception);
699 if (crop_image == (Image *) NULL)
700 return((Image *) NULL);
701 crop_image->page.width=image->page.width;
702 crop_image->page.height=image->page.height;
703 offset.x=bounding_box.x+(ssize_t) bounding_box.width;
704 offset.y=bounding_box.y+(ssize_t) bounding_box.height;
705 if ((offset.x > (ssize_t) image->page.width) ||
706 (offset.y > (ssize_t) image->page.height))
707 {
708 crop_image->page.width=bounding_box.width;
709 crop_image->page.height=bounding_box.height;
710 }
711 crop_image->page.x=bounding_box.x;
712 crop_image->page.y=bounding_box.y;
713 /*
714 Crop image.
715 */
716 status=MagickTrue;
717 progress=0;
718 image_view=AcquireVirtualCacheView(image,exception);
719 crop_view=AcquireAuthenticCacheView(crop_image,exception);
720#if defined(MAGICKCORE_OPENMP_SUPPORT)
721 #pragma omp parallel for schedule(static) shared(status) \
722 magick_number_threads(image,crop_image,crop_image->rows,2)
723#endif
724 for (y=0; y < (ssize_t) crop_image->rows; y++)
725 {
726 const Quantum
727 *magick_restrict p;
728
729 Quantum
730 *magick_restrict q;
731
732 ssize_t
733 x;
734
735 if (status == MagickFalse)
736 continue;
737 p=GetCacheViewVirtualPixels(image_view,page.x,page.y+y,crop_image->columns,
738 1,exception);
739 q=QueueCacheViewAuthenticPixels(crop_view,0,y,crop_image->columns,1,
740 exception);
741 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
742 {
743 status=MagickFalse;
744 continue;
745 }
746 for (x=0; x < (ssize_t) crop_image->columns; x++)
747 {
748 ssize_t
749 i;
750
751 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
752 {
753 PixelChannel channel = GetPixelChannelChannel(image,i);
754 PixelTrait traits = GetPixelChannelTraits(image,channel);
755 PixelTrait crop_traits=GetPixelChannelTraits(crop_image,channel);
756 if ((traits == UndefinedPixelTrait) ||
757 (crop_traits == UndefinedPixelTrait))
758 continue;
759 SetPixelChannel(crop_image,channel,p[i],q);
760 }
761 p+=(ptrdiff_t) GetPixelChannels(image);
762 q+=(ptrdiff_t) GetPixelChannels(crop_image);
763 }
764 if (SyncCacheViewAuthenticPixels(crop_view,exception) == MagickFalse)
765 status=MagickFalse;
766 if (image->progress_monitor != (MagickProgressMonitor) NULL)
767 {
768 MagickBooleanType
769 proceed;
770
771#if defined(MAGICKCORE_OPENMP_SUPPORT)
772 #pragma omp atomic
773#endif
774 progress++;
775 proceed=SetImageProgress(image,CropImageTag,progress,image->rows);
776 if (proceed == MagickFalse)
777 status=MagickFalse;
778 }
779 }
780 crop_view=DestroyCacheView(crop_view);
781 image_view=DestroyCacheView(image_view);
782 crop_image->type=image->type;
783 if (status != MagickFalse)
784 {
785 char
786 transform[MagickPathExtent];
787
788 (void) FormatLocaleString(transform,MagickPathExtent,
789 "crop %.17gx%.17g %.17gx%.17g%+.20g%+.20g",
790 (double) image->columns,(double) image->rows,(double) page.width,
791 (double) page.height,(double) page.x,(double) page.y);
792 AppendImageProfileProperty(crop_image,"hdrgm","hdrgm:Transform",
793 transform,exception);
794 }
795 if (status == MagickFalse)
796 crop_image=DestroyImage(crop_image);
797 return(crop_image);
798}
799␌
800/*
801%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802% %
803% %
804% %
805% C r o p I m a g e T o T i l e s %
806% %
807% %
808% %
809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810%
811% CropImageToTiles() crops a single image, into a possible list of tiles.
812% This may include a single sub-region of the image. This basically applies
813% all the normal geometry flags for Crop.
814%
815% Image *CropImageToTiles(const Image *image,
816% const RectangleInfo *crop_geometry, ExceptionInfo *exception)
817%
818% A description of each parameter follows:
819%
820% o image: the image The transformed image is returned as this parameter.
821%
822% o crop_geometry: A crop geometry string.
823%
824% o exception: return any errors or warnings in this structure.
825%
826*/
827
828static inline ssize_t PixelRoundOffset(double x)
829{
830 /*
831 Round the fraction to nearest integer.
832 */
833 if ((x-floor(x)) < (ceil(x)-x))
834 return(CastDoubleToSsizeT(floor(x)));
835 return(CastDoubleToSsizeT(ceil(x)));
836}
837
838MagickExport Image *CropImageToTiles(const Image *image,
839 const char *crop_geometry,ExceptionInfo *exception)
840{
841 Image
842 *next,
843 *crop_image;
844
845 MagickStatusType
846 flags;
847
848 RectangleInfo
849 geometry;
850
851 assert(image != (Image *) NULL);
852 assert(image->signature == MagickCoreSignature);
853 if (IsEventLogging() != MagickFalse)
854 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
855 flags=ParseGravityGeometry(image,crop_geometry,&geometry,exception);
856 if ((flags & AreaValue) != 0)
857 {
858 PointInfo
859 delta,
860 offset;
861
862 RectangleInfo
863 crop;
864
865 size_t
866 height,
867 width;
868
869 /*
870 Crop into NxM tiles (@ flag).
871 */
872 crop_image=NewImageList();
873 width=image->columns;
874 height=image->rows;
875 if (geometry.width == 0)
876 geometry.width=1;
877 if (geometry.height == 0)
878 geometry.height=1;
879 if ((flags & AspectValue) == 0)
880 {
881 width=(size_t) ((ssize_t) width-(geometry.x < 0 ? -1 : 1)*geometry.x);
882 height=(size_t) ((ssize_t) height-(geometry.y < 0 ? -1 : 1)*
883 geometry.y);
884 }
885 else
886 {
887 width=(size_t) ((ssize_t) width+(geometry.x < 0 ? -1 : 1)*geometry.x);
888 height=(size_t) ((ssize_t) height+(geometry.y < 0 ? -1 : 1)*
889 geometry.y);
890 }
891 delta.x=(double) width/geometry.width;
892 delta.y=(double) height/geometry.height;
893 if (delta.x < 1.0)
894 delta.x=1.0;
895 if (delta.y < 1.0)
896 delta.y=1.0;
897 for (offset.y=0; offset.y < (double) height; )
898 {
899 if ((flags & AspectValue) == 0)
900 {
901 crop.y=PixelRoundOffset((double) (offset.y-
902 (geometry.y > 0 ? 0 : geometry.y)));
903 offset.y+=delta.y; /* increment now to find width */
904 crop.height=(size_t) PixelRoundOffset((double) (offset.y+
905 (geometry.y < 0 ? 0 : geometry.y)));
906 }
907 else
908 {
909 crop.y=PixelRoundOffset((double) (offset.y-
910 (geometry.y > 0 ? geometry.y : 0)));
911 offset.y+=delta.y; /* increment now to find width */
912 crop.height=(size_t) PixelRoundOffset((double)
913 (offset.y+(geometry.y < -1 ? geometry.y : 0)));
914 }
915 crop.height=(size_t) ((ssize_t) crop.height-crop.y);
916 crop.y+=image->page.y;
917 for (offset.x=0; offset.x < (double) width; )
918 {
919 if ((flags & AspectValue) == 0)
920 {
921 crop.x=PixelRoundOffset((double) (offset.x-
922 (geometry.x > 0 ? 0 : geometry.x)));
923 offset.x+=delta.x; /* increment now to find height */
924 crop.width=(size_t) PixelRoundOffset((double) (offset.x+
925 (geometry.x < 0 ? 0 : geometry.x)));
926 }
927 else
928 {
929 crop.x=PixelRoundOffset((double) (offset.x-
930 (geometry.x > 0 ? geometry.x : 0)));
931 offset.x+=delta.x; /* increment now to find height */
932 crop.width=(size_t) PixelRoundOffset((double) (offset.x+
933 (geometry.x < 0 ? geometry.x : 0)));
934 }
935 crop.width=(size_t) ((ssize_t) crop.width-crop.x);
936 crop.x+=image->page.x;
937 next=CropImage(image,&crop,exception);
938 if (next != (Image *) NULL)
939 AppendImageToList(&crop_image,next);
940 }
941 }
942 ClearMagickException(exception);
943 return(crop_image);
944 }
945 if (((geometry.width == 0) && (geometry.height == 0)) ||
946 ((flags & XValue) != 0) || ((flags & YValue) != 0))
947 {
948 /*
949 Crop a single region at +X+Y.
950 */
951 crop_image=CropImage(image,&geometry,exception);
952 if ((crop_image != (Image *) NULL) && ((flags & AspectValue) != 0))
953 {
954 crop_image->page.width=geometry.width;
955 crop_image->page.height=geometry.height;
956 crop_image->page.x-=geometry.x;
957 crop_image->page.y-=geometry.y;
958 }
959 return(crop_image);
960 }
961 if ((image->columns > geometry.width) || (image->rows > geometry.height))
962 {
963 RectangleInfo
964 page;
965
966 size_t
967 height,
968 width;
969
970 ssize_t
971 x,
972 y;
973
974 /*
975 Crop into tiles of fixed size WxH.
976 */
977 page=image->page;
978 if (page.width == 0)
979 page.width=image->columns;
980 if (page.height == 0)
981 page.height=image->rows;
982 width=geometry.width;
983 if (width == 0)
984 width=page.width;
985 height=geometry.height;
986 if (height == 0)
987 height=page.height;
988 next=(Image *) NULL;
989 crop_image=NewImageList();
990 for (y=0; y < (ssize_t) page.height; y+=(ssize_t) height)
991 {
992 for (x=0; x < (ssize_t) page.width; x+=(ssize_t) width)
993 {
994 geometry.width=width;
995 geometry.height=height;
996 geometry.x=x;
997 geometry.y=y;
998 next=CropImage(image,&geometry,exception);
999 if (next == (Image *) NULL)
1000 break;
1001 AppendImageToList(&crop_image,next);
1002 }
1003 if (next == (Image *) NULL)
1004 break;
1005 }
1006 return(crop_image);
1007 }
1008 return(CloneImage(image,0,0,MagickTrue,exception));
1009}
1010␌
1011/*
1012%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1013% %
1014% %
1015% %
1016% E x c e r p t I m a g e %
1017% %
1018% %
1019% %
1020%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1021%
1022% ExcerptImage() returns an excerpt of the image as defined by the geometry.
1023%
1024% The format of the ExcerptImage method is:
1025%
1026% Image *ExcerptImage(const Image *image,const RectangleInfo *geometry,
1027% ExceptionInfo *exception)
1028%
1029% A description of each parameter follows:
1030%
1031% o image: the image.
1032%
1033% o geometry: Define the region of the image to extend with members
1034% x, y, width, and height.
1035%
1036% o exception: return any errors or warnings in this structure.
1037%
1038*/
1039MagickExport Image *ExcerptImage(const Image *image,
1040 const RectangleInfo *geometry,ExceptionInfo *exception)
1041{
1042#define ExcerptImageTag "Excerpt/Image"
1043
1044 CacheView
1045 *excerpt_view,
1046 *image_view;
1047
1048 Image
1049 *excerpt_image;
1050
1051 MagickBooleanType
1052 status;
1053
1054 MagickOffsetType
1055 progress;
1056
1057 ssize_t
1058 y;
1059
1060 /*
1061 Allocate excerpt image.
1062 */
1063 assert(image != (const Image *) NULL);
1064 assert(image->signature == MagickCoreSignature);
1065 assert(geometry != (const RectangleInfo *) NULL);
1066 assert(exception != (ExceptionInfo *) NULL);
1067 assert(exception->signature == MagickCoreSignature);
1068 if (IsEventLogging() != MagickFalse)
1069 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1070 excerpt_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
1071 exception);
1072 if (excerpt_image == (Image *) NULL)
1073 return((Image *) NULL);
1074 /*
1075 Excerpt each row.
1076 */
1077 status=MagickTrue;
1078 progress=0;
1079 image_view=AcquireVirtualCacheView(image,exception);
1080 excerpt_view=AcquireAuthenticCacheView(excerpt_image,exception);
1081#if defined(MAGICKCORE_OPENMP_SUPPORT)
1082 #pragma omp parallel for schedule(static) shared(progress,status) \
1083 magick_number_threads(image,excerpt_image,excerpt_image->rows,2)
1084#endif
1085 for (y=0; y < (ssize_t) excerpt_image->rows; y++)
1086 {
1087 const Quantum
1088 *magick_restrict p;
1089
1090 Quantum
1091 *magick_restrict q;
1092
1093 ssize_t
1094 x;
1095
1096 if (status == MagickFalse)
1097 continue;
1098 p=GetCacheViewVirtualPixels(image_view,geometry->x,geometry->y+y,
1099 geometry->width,1,exception);
1100 q=GetCacheViewAuthenticPixels(excerpt_view,0,y,excerpt_image->columns,1,
1101 exception);
1102 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1103 {
1104 status=MagickFalse;
1105 continue;
1106 }
1107 for (x=0; x < (ssize_t) excerpt_image->columns; x++)
1108 {
1109 ssize_t
1110 i;
1111
1112 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1113 {
1114 PixelChannel channel = GetPixelChannelChannel(image,i);
1115 PixelTrait traits = GetPixelChannelTraits(image,channel);
1116 PixelTrait excerpt_traits=GetPixelChannelTraits(excerpt_image,channel);
1117 if ((traits == UndefinedPixelTrait) ||
1118 (excerpt_traits == UndefinedPixelTrait))
1119 continue;
1120 SetPixelChannel(excerpt_image,channel,p[i],q);
1121 }
1122 p+=(ptrdiff_t) GetPixelChannels(image);
1123 q+=(ptrdiff_t) GetPixelChannels(excerpt_image);
1124 }
1125 if (SyncCacheViewAuthenticPixels(excerpt_view,exception) == MagickFalse)
1126 status=MagickFalse;
1127 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1128 {
1129 MagickBooleanType
1130 proceed;
1131
1132#if defined(MAGICKCORE_OPENMP_SUPPORT)
1133 #pragma omp atomic
1134#endif
1135 progress++;
1136 proceed=SetImageProgress(image,ExcerptImageTag,progress,image->rows);
1137 if (proceed == MagickFalse)
1138 status=MagickFalse;
1139 }
1140 }
1141 excerpt_view=DestroyCacheView(excerpt_view);
1142 image_view=DestroyCacheView(image_view);
1143 excerpt_image->type=image->type;
1144 if (status == MagickFalse)
1145 excerpt_image=DestroyImage(excerpt_image);
1146 return(excerpt_image);
1147}
1148␌
1149/*
1150%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1151% %
1152% %
1153% %
1154% E x t e n t I m a g e %
1155% %
1156% %
1157% %
1158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1159%
1160% ExtentImage() extends the image as defined by the geometry, gravity, and
1161% image background color. Set the (x,y) offset of the geometry to move the
1162% original image relative to the extended image.
1163%
1164% The format of the ExtentImage method is:
1165%
1166% Image *ExtentImage(const Image *image,const RectangleInfo *geometry,
1167% ExceptionInfo *exception)
1168%
1169% A description of each parameter follows:
1170%
1171% o image: the image.
1172%
1173% o geometry: Define the region of the image to extend with members
1174% x, y, width, and height.
1175%
1176% o exception: return any errors or warnings in this structure.
1177%
1178*/
1179MagickExport Image *ExtentImage(const Image *image,
1180 const RectangleInfo *geometry,ExceptionInfo *exception)
1181{
1182 Image
1183 *extent_image;
1184
1185 MagickBooleanType
1186 status;
1187
1188 /*
1189 Allocate extent image.
1190 */
1191 assert(image != (const Image *) NULL);
1192 assert(image->signature == MagickCoreSignature);
1193 assert(geometry != (const RectangleInfo *) NULL);
1194 assert(exception != (ExceptionInfo *) NULL);
1195 assert(exception->signature == MagickCoreSignature);
1196 if (IsEventLogging() != MagickFalse)
1197 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1198 extent_image=CloneImage(image,geometry->width,geometry->height,MagickTrue,
1199 exception);
1200 if (extent_image == (Image *) NULL)
1201 return((Image *) NULL);
1202 status=SetImageBackgroundColor(extent_image,exception);
1203 if (status == MagickFalse)
1204 {
1205 extent_image=DestroyImage(extent_image);
1206 return((Image *) NULL);
1207 }
1208 DisableCompositeClampUnlessSpecified(extent_image);
1209 status=CompositeImage(extent_image,image,image->compose,MagickTrue,
1210 -geometry->x,-geometry->y,exception);
1211 if (status != MagickFalse)
1212 Update8BIMClipPath(extent_image,image->columns,image->rows,geometry);
1213 return(extent_image);
1214}
1215␌
1216/*
1217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1218% %
1219% %
1220% %
1221% F l i p I m a g e %
1222% %
1223% %
1224% %
1225%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1226%
1227% FlipImage() creates a vertical mirror image by reflecting the pixels
1228% around the central x-axis.
1229%
1230% The format of the FlipImage method is:
1231%
1232% Image *FlipImage(const Image *image,ExceptionInfo *exception)
1233%
1234% A description of each parameter follows:
1235%
1236% o image: the image.
1237%
1238% o exception: return any errors or warnings in this structure.
1239%
1240*/
1241MagickExport Image *FlipImage(const Image *image,ExceptionInfo *exception)
1242{
1243#define FlipImageTag "Flip/Image"
1244
1245 CacheView
1246 *flip_view,
1247 *image_view;
1248
1249 Image
1250 *flip_image;
1251
1252 MagickBooleanType
1253 status;
1254
1255 MagickOffsetType
1256 progress;
1257
1258 RectangleInfo
1259 page;
1260
1261 ssize_t
1262 y;
1263
1264 assert(image != (const Image *) NULL);
1265 assert(image->signature == MagickCoreSignature);
1266 assert(exception != (ExceptionInfo *) NULL);
1267 assert(exception->signature == MagickCoreSignature);
1268 if (IsEventLogging() != MagickFalse)
1269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1270 flip_image=CloneImage(image,0,0,MagickTrue,exception);
1271 if (flip_image == (Image *) NULL)
1272 return((Image *) NULL);
1273 /*
1274 Flip image.
1275 */
1276 status=MagickTrue;
1277 progress=0;
1278 page=image->page;
1279 image_view=AcquireVirtualCacheView(image,exception);
1280 flip_view=AcquireAuthenticCacheView(flip_image,exception);
1281#if defined(MAGICKCORE_OPENMP_SUPPORT)
1282 #pragma omp parallel for schedule(static) shared(status) \
1283 magick_number_threads(image,flip_image,flip_image->rows,2)
1284#endif
1285 for (y=0; y < (ssize_t) flip_image->rows; y++)
1286 {
1287 const Quantum
1288 *magick_restrict p;
1289
1290 Quantum
1291 *magick_restrict q;
1292
1293 ssize_t
1294 x;
1295
1296 if (status == MagickFalse)
1297 continue;
1298 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1299 q=QueueCacheViewAuthenticPixels(flip_view,0,((ssize_t) flip_image->rows-y-
1300 1),flip_image->columns,1,exception);
1301 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1302 {
1303 status=MagickFalse;
1304 continue;
1305 }
1306 for (x=0; x < (ssize_t) flip_image->columns; x++)
1307 {
1308 ssize_t
1309 i;
1310
1311 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1312 {
1313 PixelChannel channel = GetPixelChannelChannel(image,i);
1314 PixelTrait traits = GetPixelChannelTraits(image,channel);
1315 PixelTrait flip_traits = GetPixelChannelTraits(flip_image,channel);
1316 if ((traits == UndefinedPixelTrait) ||
1317 (flip_traits == UndefinedPixelTrait))
1318 continue;
1319 SetPixelChannel(flip_image,channel,p[i],q);
1320 }
1321 p+=(ptrdiff_t) GetPixelChannels(image);
1322 q+=(ptrdiff_t) GetPixelChannels(flip_image);
1323 }
1324 if (SyncCacheViewAuthenticPixels(flip_view,exception) == MagickFalse)
1325 status=MagickFalse;
1326 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1327 {
1328 MagickBooleanType
1329 proceed;
1330
1331#if defined(MAGICKCORE_OPENMP_SUPPORT)
1332 #pragma omp atomic
1333#endif
1334 progress++;
1335 proceed=SetImageProgress(image,FlipImageTag,progress,image->rows);
1336 if (proceed == MagickFalse)
1337 status=MagickFalse;
1338 }
1339 }
1340 flip_view=DestroyCacheView(flip_view);
1341 image_view=DestroyCacheView(image_view);
1342 flip_image->type=image->type;
1343 if (page.height != 0)
1344 page.y=((ssize_t) page.height-(ssize_t) flip_image->rows-page.y);
1345 flip_image->page=page;
1346 if (status != MagickFalse)
1347 {
1348 char
1349 transform[MagickPathExtent];
1350
1351 (void) FormatLocaleString(transform,MagickPathExtent,
1352 "flip %.17gx%.17g",(double) image->columns,(double) image->rows);
1353 AppendImageProfileProperty(flip_image,"hdrgm","hdrgm:Transform",
1354 transform,exception);
1355 }
1356 if (status == MagickFalse)
1357 flip_image=DestroyImage(flip_image);
1358 return(flip_image);
1359}
1360␌
1361/*
1362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1363% %
1364% %
1365% %
1366% F l o p I m a g e %
1367% %
1368% %
1369% %
1370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371%
1372% FlopImage() creates a horizontal mirror image by reflecting the pixels
1373% around the central y-axis.
1374%
1375% The format of the FlopImage method is:
1376%
1377% Image *FlopImage(const Image *image,ExceptionInfo *exception)
1378%
1379% A description of each parameter follows:
1380%
1381% o image: the image.
1382%
1383% o exception: return any errors or warnings in this structure.
1384%
1385*/
1386MagickExport Image *FlopImage(const Image *image,ExceptionInfo *exception)
1387{
1388#define FlopImageTag "Flop/Image"
1389
1390 CacheView
1391 *flop_view,
1392 *image_view;
1393
1394 Image
1395 *flop_image;
1396
1397 MagickBooleanType
1398 status;
1399
1400 MagickOffsetType
1401 progress;
1402
1403 RectangleInfo
1404 page;
1405
1406 ssize_t
1407 y;
1408
1409 assert(image != (const Image *) NULL);
1410 assert(image->signature == MagickCoreSignature);
1411 assert(exception != (ExceptionInfo *) NULL);
1412 assert(exception->signature == MagickCoreSignature);
1413 if (IsEventLogging() != MagickFalse)
1414 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1415 flop_image=CloneImage(image,0,0,MagickTrue,exception);
1416 if (flop_image == (Image *) NULL)
1417 return((Image *) NULL);
1418 /*
1419 Flop each row.
1420 */
1421 status=MagickTrue;
1422 progress=0;
1423 page=image->page;
1424 image_view=AcquireVirtualCacheView(image,exception);
1425 flop_view=AcquireAuthenticCacheView(flop_image,exception);
1426#if defined(MAGICKCORE_OPENMP_SUPPORT)
1427 #pragma omp parallel for schedule(static) shared(status) \
1428 magick_number_threads(image,flop_image,flop_image->rows,2)
1429#endif
1430 for (y=0; y < (ssize_t) flop_image->rows; y++)
1431 {
1432 const Quantum
1433 *magick_restrict p;
1434
1435 ssize_t
1436 x;
1437
1438 Quantum
1439 *magick_restrict q;
1440
1441 if (status == MagickFalse)
1442 continue;
1443 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1444 q=QueueCacheViewAuthenticPixels(flop_view,0,y,flop_image->columns,1,
1445 exception);
1446 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1447 {
1448 status=MagickFalse;
1449 continue;
1450 }
1451 q+=(ptrdiff_t) GetPixelChannels(flop_image)*flop_image->columns;
1452 for (x=0; x < (ssize_t) flop_image->columns; x++)
1453 {
1454 ssize_t
1455 i;
1456
1457 q-=GetPixelChannels(flop_image);
1458 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1459 {
1460 PixelChannel channel = GetPixelChannelChannel(image,i);
1461 PixelTrait traits = GetPixelChannelTraits(image,channel);
1462 PixelTrait flop_traits=GetPixelChannelTraits(flop_image,channel);
1463 if ((traits == UndefinedPixelTrait) ||
1464 (flop_traits == UndefinedPixelTrait))
1465 continue;
1466 SetPixelChannel(flop_image,channel,p[i],q);
1467 }
1468 p+=(ptrdiff_t) GetPixelChannels(image);
1469 }
1470 if (SyncCacheViewAuthenticPixels(flop_view,exception) == MagickFalse)
1471 status=MagickFalse;
1472 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1473 {
1474 MagickBooleanType
1475 proceed;
1476
1477#if defined(MAGICKCORE_OPENMP_SUPPORT)
1478 #pragma omp atomic
1479#endif
1480 progress++;
1481 proceed=SetImageProgress(image,FlopImageTag,progress,image->rows);
1482 if (proceed == MagickFalse)
1483 status=MagickFalse;
1484 }
1485 }
1486 flop_view=DestroyCacheView(flop_view);
1487 image_view=DestroyCacheView(image_view);
1488 flop_image->type=image->type;
1489 if (page.width != 0)
1490 page.x=((ssize_t) page.width-(ssize_t) flop_image->columns-page.x);
1491 flop_image->page=page;
1492 if (status != MagickFalse)
1493 {
1494 char
1495 transform[MagickPathExtent];
1496
1497 (void) FormatLocaleString(transform,MagickPathExtent,
1498 "flop %.17gx%.17g",(double) image->columns,(double) image->rows);
1499 AppendImageProfileProperty(flop_image,"hdrgm","hdrgm:Transform",
1500 transform,exception);
1501 }
1502 if (status == MagickFalse)
1503 flop_image=DestroyImage(flop_image);
1504 return(flop_image);
1505}
1506␌
1507/*
1508%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1509% %
1510% %
1511% %
1512% R o l l I m a g e %
1513% %
1514% %
1515% %
1516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1517%
1518% RollImage() offsets an image as defined by x_offset and y_offset.
1519%
1520% The format of the RollImage method is:
1521%
1522% Image *RollImage(const Image *image,const ssize_t x_offset,
1523% const ssize_t y_offset,ExceptionInfo *exception)
1524%
1525% A description of each parameter follows:
1526%
1527% o image: the image.
1528%
1529% o x_offset: the number of columns to roll in the horizontal direction.
1530%
1531% o y_offset: the number of rows to roll in the vertical direction.
1532%
1533% o exception: return any errors or warnings in this structure.
1534%
1535*/
1536
1537static MagickBooleanType CopyImageRegion(Image *destination,const Image *source, const size_t columns,const size_t rows,const ssize_t sx,const ssize_t sy,
1538 const ssize_t dx,const ssize_t dy,ExceptionInfo *exception)
1539{
1540 CacheView
1541 *source_view,
1542 *destination_view;
1543
1544 MagickBooleanType
1545 status;
1546
1547 ssize_t
1548 y;
1549
1550 if (columns == 0)
1551 return(MagickTrue);
1552 status=MagickTrue;
1553 source_view=AcquireVirtualCacheView(source,exception);
1554 destination_view=AcquireAuthenticCacheView(destination,exception);
1555#if defined(MAGICKCORE_OPENMP_SUPPORT)
1556 #pragma omp parallel for schedule(static) shared(status) \
1557 magick_number_threads(source,destination,rows,2)
1558#endif
1559 for (y=0; y < (ssize_t) rows; y++)
1560 {
1561 MagickBooleanType
1562 sync;
1563
1564 const Quantum
1565 *magick_restrict p;
1566
1567 Quantum
1568 *magick_restrict q;
1569
1570 ssize_t
1571 x;
1572
1573 /*
1574 Transfer scanline.
1575 */
1576 if (status == MagickFalse)
1577 continue;
1578 p=GetCacheViewVirtualPixels(source_view,sx,sy+y,columns,1,exception);
1579 q=GetCacheViewAuthenticPixels(destination_view,dx,dy+y,columns,1,exception);
1580 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1581 {
1582 status=MagickFalse;
1583 continue;
1584 }
1585 for (x=0; x < (ssize_t) columns; x++)
1586 {
1587 ssize_t
1588 i;
1589
1590 for (i=0; i < (ssize_t) GetPixelChannels(source); i++)
1591 {
1592 PixelChannel channel = GetPixelChannelChannel(source,i);
1593 PixelTrait source_traits=GetPixelChannelTraits(source,channel);
1594 PixelTrait destination_traits=GetPixelChannelTraits(destination,
1595 channel);
1596 if ((source_traits == UndefinedPixelTrait) ||
1597 (destination_traits == UndefinedPixelTrait))
1598 continue;
1599 SetPixelChannel(destination,channel,p[i],q);
1600 }
1601 p+=(ptrdiff_t) GetPixelChannels(source);
1602 q+=(ptrdiff_t) GetPixelChannels(destination);
1603 }
1604 sync=SyncCacheViewAuthenticPixels(destination_view,exception);
1605 if (sync == MagickFalse)
1606 status=MagickFalse;
1607 }
1608 destination_view=DestroyCacheView(destination_view);
1609 source_view=DestroyCacheView(source_view);
1610 return(status);
1611}
1612
1613MagickExport Image *RollImage(const Image *image,const ssize_t x_offset,
1614 const ssize_t y_offset,ExceptionInfo *exception)
1615{
1616#define RollImageTag "Roll/Image"
1617
1618 Image
1619 *roll_image;
1620
1621 MagickStatusType
1622 status;
1623
1624 RectangleInfo
1625 offset;
1626
1627 /*
1628 Initialize roll image attributes.
1629 */
1630 assert(image != (const Image *) NULL);
1631 assert(image->signature == MagickCoreSignature);
1632 assert(exception != (ExceptionInfo *) NULL);
1633 assert(exception->signature == MagickCoreSignature);
1634 if (IsEventLogging() != MagickFalse)
1635 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1636 roll_image=CloneImage(image,0,0,MagickTrue,exception);
1637 if (roll_image == (Image *) NULL)
1638 return((Image *) NULL);
1639 offset.x=x_offset;
1640 offset.y=y_offset;
1641 while (offset.x < 0)
1642 offset.x+=(ssize_t) image->columns;
1643 while (offset.x >= (ssize_t) image->columns)
1644 offset.x-=(ssize_t) image->columns;
1645 while (offset.y < 0)
1646 offset.y+=(ssize_t) image->rows;
1647 while (offset.y >= (ssize_t) image->rows)
1648 offset.y-=(ssize_t) image->rows;
1649 /*
1650 Roll image.
1651 */
1652 status=CopyImageRegion(roll_image,image,(size_t) offset.x,
1653 (size_t) offset.y,(ssize_t) image->columns-offset.x,(ssize_t) image->rows-
1654 offset.y,0,0,exception);
1655 (void) SetImageProgress(image,RollImageTag,0,3);
1656 status&=(MagickStatusType) CopyImageRegion(roll_image,image,(size_t)
1657 ((ssize_t) image->columns-offset.x),(size_t) offset.y,0,(ssize_t)
1658 image->rows-offset.y,offset.x,0,exception);
1659 (void) SetImageProgress(image,RollImageTag,1,3);
1660 status&=(MagickStatusType) CopyImageRegion(roll_image,image,(size_t)
1661 offset.x,(size_t) ((ssize_t) image->rows-offset.y),(ssize_t)
1662 image->columns-offset.x,0,0,offset.y,exception);
1663 (void) SetImageProgress(image,RollImageTag,2,3);
1664 status&=(MagickStatusType) CopyImageRegion(roll_image,image,(size_t)
1665 ((ssize_t) image->columns-offset.x),(size_t) ((ssize_t) image->rows-
1666 offset.y),0,0,offset.x,offset.y,exception);
1667 (void) SetImageProgress(image,RollImageTag,3,3);
1668 roll_image->type=image->type;
1669 if (status == MagickFalse)
1670 roll_image=DestroyImage(roll_image);
1671 return(roll_image);
1672}
1673␌
1674/*
1675%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1676% %
1677% %
1678% %
1679% S h a v e I m a g e %
1680% %
1681% %
1682% %
1683%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1684%
1685% ShaveImage() shaves pixels from the image edges. It allocates the memory
1686% necessary for the new Image structure and returns a pointer to the new
1687% image.
1688%
1689% The format of the ShaveImage method is:
1690%
1691% Image *ShaveImage(const Image *image,const RectangleInfo *shave_info,
1692% ExceptionInfo *exception)
1693%
1694% A description of each parameter follows:
1695%
1696% o shave_image: Method ShaveImage returns a pointer to the shaved
1697% image. A null image is returned if there is a memory shortage or
1698% if the image width or height is zero.
1699%
1700% o image: the image.
1701%
1702% o shave_info: Specifies a pointer to a RectangleInfo which defines the
1703% region of the image to crop.
1704%
1705% o exception: return any errors or warnings in this structure.
1706%
1707*/
1708MagickExport Image *ShaveImage(const Image *image,
1709 const RectangleInfo *shave_info,ExceptionInfo *exception)
1710{
1711 Image
1712 *shave_image;
1713
1714 RectangleInfo
1715 geometry;
1716
1717 assert(image != (const Image *) NULL);
1718 assert(image->signature == MagickCoreSignature);
1719 if (IsEventLogging() != MagickFalse)
1720 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1721 if (((2*shave_info->width) >= image->columns) ||
1722 ((2*shave_info->height) >= image->rows))
1723 ThrowImageException(OptionWarning,"GeometryDoesNotContainImage");
1724 SetGeometry(image,&geometry);
1725 geometry.width-=2*shave_info->width;
1726 geometry.height-=2*shave_info->height;
1727 geometry.x=(ssize_t) shave_info->width+image->page.x;
1728 geometry.y=(ssize_t) shave_info->height+image->page.y;
1729 shave_image=CropImage(image,&geometry,exception);
1730 if (shave_image == (Image *) NULL)
1731 return((Image *) NULL);
1732 shave_image->page.width-=2*shave_info->width;
1733 shave_image->page.height-=2*shave_info->height;
1734 shave_image->page.x-=(ssize_t) shave_info->width;
1735 shave_image->page.y-=(ssize_t) shave_info->height;
1736 return(shave_image);
1737}
1738␌
1739/*
1740%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1741% %
1742% %
1743% %
1744% S p l i c e I m a g e %
1745% %
1746% %
1747% %
1748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1749%
1750% SpliceImage() splices a solid color into the image as defined by the
1751% geometry.
1752%
1753% The format of the SpliceImage method is:
1754%
1755% Image *SpliceImage(const Image *image,const RectangleInfo *geometry,
1756% ExceptionInfo *exception)
1757%
1758% A description of each parameter follows:
1759%
1760% o image: the image.
1761%
1762% o geometry: Define the region of the image to splice with members
1763% x, y, width, and height.
1764%
1765% o exception: return any errors or warnings in this structure.
1766%
1767*/
1768MagickExport Image *SpliceImage(const Image *image,
1769 const RectangleInfo *geometry,ExceptionInfo *exception)
1770{
1771#define SpliceImageTag "Splice/Image"
1772
1773 CacheView
1774 *image_view,
1775 *splice_view;
1776
1777 Image
1778 *splice_image;
1779
1780 MagickBooleanType
1781 status;
1782
1783 MagickOffsetType
1784 progress;
1785
1786 RectangleInfo
1787 splice_geometry;
1788
1789 ssize_t
1790 columns,
1791 rows,
1792 y;
1793
1794 /*
1795 Allocate splice image.
1796 */
1797 assert(image != (const Image *) NULL);
1798 assert(image->signature == MagickCoreSignature);
1799 if (IsEventLogging() != MagickFalse)
1800 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1801 assert(geometry != (const RectangleInfo *) NULL);
1802 assert(exception != (ExceptionInfo *) NULL);
1803 assert(exception->signature == MagickCoreSignature);
1804 splice_geometry=(*geometry);
1805 splice_image=CloneImage(image,image->columns+splice_geometry.width,
1806 image->rows+splice_geometry.height,MagickTrue,exception);
1807 if (splice_image == (Image *) NULL)
1808 return((Image *) NULL);
1809 if (SetImageStorageClass(splice_image,DirectClass,exception) == MagickFalse)
1810 {
1811 splice_image=DestroyImage(splice_image);
1812 return((Image *) NULL);
1813 }
1814 if ((IsPixelInfoGray(&splice_image->background_color) == MagickFalse) &&
1815 (IsGrayColorspace(splice_image->colorspace) != MagickFalse))
1816 (void) SetImageColorspace(splice_image,sRGBColorspace,exception);
1817 if ((splice_image->background_color.alpha_trait != UndefinedPixelTrait) &&
1818 (splice_image->alpha_trait == UndefinedPixelTrait))
1819 (void) SetImageAlpha(splice_image,OpaqueAlpha,exception);
1820 (void) SetImageBackgroundColor(splice_image,exception);
1821 /*
1822 Respect image geometry.
1823 */
1824 switch (image->gravity)
1825 {
1826 default:
1827 case UndefinedGravity:
1828 case NorthWestGravity:
1829 break;
1830 case NorthGravity:
1831 {
1832 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1833 break;
1834 }
1835 case NorthEastGravity:
1836 {
1837 splice_geometry.x+=(ssize_t) splice_geometry.width;
1838 break;
1839 }
1840 case WestGravity:
1841 {
1842 splice_geometry.y+=(ssize_t) splice_geometry.width/2;
1843 break;
1844 }
1845 case CenterGravity:
1846 {
1847 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1848 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1849 break;
1850 }
1851 case EastGravity:
1852 {
1853 splice_geometry.x+=(ssize_t) splice_geometry.width;
1854 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1855 break;
1856 }
1857 case SouthWestGravity:
1858 {
1859 splice_geometry.y+=(ssize_t) splice_geometry.height;
1860 break;
1861 }
1862 case SouthGravity:
1863 {
1864 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1865 splice_geometry.y+=(ssize_t) splice_geometry.height;
1866 break;
1867 }
1868 case SouthEastGravity:
1869 {
1870 splice_geometry.x+=(ssize_t) splice_geometry.width;
1871 splice_geometry.y+=(ssize_t) splice_geometry.height;
1872 break;
1873 }
1874 }
1875 /*
1876 Splice image.
1877 */
1878 status=MagickTrue;
1879 progress=0;
1880 columns=MagickMin(splice_geometry.x,(ssize_t) splice_image->columns);
1881 rows=MagickMin(splice_geometry.y,(ssize_t) splice_image->rows);
1882 image_view=AcquireVirtualCacheView(image,exception);
1883 splice_view=AcquireAuthenticCacheView(splice_image,exception);
1884#if defined(MAGICKCORE_OPENMP_SUPPORT)
1885 #pragma omp parallel for schedule(static) shared(progress,status) \
1886 magick_number_threads(image,splice_image,rows,2)
1887#endif
1888 for (y=0; y < rows; y++)
1889 {
1890 const Quantum
1891 *magick_restrict p;
1892
1893 ssize_t
1894 x;
1895
1896 Quantum
1897 *magick_restrict q;
1898
1899 if (status == MagickFalse)
1900 continue;
1901 p=GetCacheViewVirtualPixels(image_view,0,y,splice_image->columns,1,
1902 exception);
1903 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1904 exception);
1905 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1906 {
1907 status=MagickFalse;
1908 continue;
1909 }
1910 for (x=0; x < columns; x++)
1911 {
1912 ssize_t
1913 i;
1914
1915 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1916 {
1917 PixelChannel channel = GetPixelChannelChannel(image,i);
1918 PixelTrait traits = GetPixelChannelTraits(image,channel);
1919 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
1920 if ((traits == UndefinedPixelTrait) ||
1921 (splice_traits == UndefinedPixelTrait))
1922 continue;
1923 SetPixelChannel(splice_image,channel,p[i],q);
1924 }
1925 SetPixelRed(splice_image,GetPixelRed(image,p),q);
1926 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
1927 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
1928 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
1929 p+=(ptrdiff_t) GetPixelChannels(image);
1930 q+=(ptrdiff_t) GetPixelChannels(splice_image);
1931 }
1932 for ( ; x < (splice_geometry.x+(ssize_t) splice_geometry.width); x++)
1933 q+=(ptrdiff_t) GetPixelChannels(splice_image);
1934 for ( ; x < (ssize_t) splice_image->columns; x++)
1935 {
1936 ssize_t
1937 i;
1938
1939 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1940 {
1941 PixelChannel channel = GetPixelChannelChannel(image,i);
1942 PixelTrait traits = GetPixelChannelTraits(image,channel);
1943 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
1944 if ((traits == UndefinedPixelTrait) ||
1945 (splice_traits == UndefinedPixelTrait))
1946 continue;
1947 SetPixelChannel(splice_image,channel,p[i],q);
1948 }
1949 SetPixelRed(splice_image,GetPixelRed(image,p),q);
1950 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
1951 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
1952 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
1953 p+=(ptrdiff_t) GetPixelChannels(image);
1954 q+=(ptrdiff_t) GetPixelChannels(splice_image);
1955 }
1956 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
1957 status=MagickFalse;
1958 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1959 {
1960 MagickBooleanType
1961 proceed;
1962
1963#if defined(MAGICKCORE_OPENMP_SUPPORT)
1964 #pragma omp atomic
1965#endif
1966 progress++;
1967 proceed=SetImageProgress(image,SpliceImageTag,progress,
1968 splice_image->rows);
1969 if (proceed == MagickFalse)
1970 status=MagickFalse;
1971 }
1972 }
1973#if defined(MAGICKCORE_OPENMP_SUPPORT)
1974 #pragma omp parallel for schedule(static) shared(progress,status) \
1975 magick_number_threads(image,splice_image,splice_image->rows,2)
1976#endif
1977 for (y=splice_geometry.y+(ssize_t) splice_geometry.height; y < (ssize_t) splice_image->rows; y++)
1978 {
1979 const Quantum
1980 *magick_restrict p;
1981
1982 ssize_t
1983 x;
1984
1985 Quantum
1986 *magick_restrict q;
1987
1988 if (status == MagickFalse)
1989 continue;
1990 if ((y < 0) || (y >= (ssize_t) splice_image->rows))
1991 continue;
1992 p=GetCacheViewVirtualPixels(image_view,0,y-(ssize_t) splice_geometry.height,
1993 splice_image->columns,1,exception);
1994 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1995 exception);
1996 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1997 {
1998 status=MagickFalse;
1999 continue;
2000 }
2001 for (x=0; x < columns; x++)
2002 {
2003 ssize_t
2004 i;
2005
2006 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2007 {
2008 PixelChannel channel = GetPixelChannelChannel(image,i);
2009 PixelTrait traits = GetPixelChannelTraits(image,channel);
2010 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
2011 if ((traits == UndefinedPixelTrait) ||
2012 (splice_traits == UndefinedPixelTrait))
2013 continue;
2014 SetPixelChannel(splice_image,channel,p[i],q);
2015 }
2016 SetPixelRed(splice_image,GetPixelRed(image,p),q);
2017 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
2018 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
2019 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
2020 p+=(ptrdiff_t) GetPixelChannels(image);
2021 q+=(ptrdiff_t) GetPixelChannels(splice_image);
2022 }
2023 for ( ; x < (splice_geometry.x+(ssize_t) splice_geometry.width); x++)
2024 q+=(ptrdiff_t) GetPixelChannels(splice_image);
2025 for ( ; x < (ssize_t) splice_image->columns; x++)
2026 {
2027 ssize_t
2028 i;
2029
2030 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2031 {
2032 PixelChannel channel = GetPixelChannelChannel(image,i);
2033 PixelTrait traits = GetPixelChannelTraits(image,channel);
2034 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
2035 if ((traits == UndefinedPixelTrait) ||
2036 (splice_traits == UndefinedPixelTrait))
2037 continue;
2038 SetPixelChannel(splice_image,channel,p[i],q);
2039 }
2040 SetPixelRed(splice_image,GetPixelRed(image,p),q);
2041 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
2042 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
2043 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
2044 p+=(ptrdiff_t) GetPixelChannels(image);
2045 q+=(ptrdiff_t) GetPixelChannels(splice_image);
2046 }
2047 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
2048 status=MagickFalse;
2049 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2050 {
2051 MagickBooleanType
2052 proceed;
2053
2054#if defined(MAGICKCORE_OPENMP_SUPPORT)
2055 #pragma omp atomic
2056#endif
2057 progress++;
2058 proceed=SetImageProgress(image,SpliceImageTag,progress,
2059 splice_image->rows);
2060 if (proceed == MagickFalse)
2061 status=MagickFalse;
2062 }
2063 }
2064 splice_view=DestroyCacheView(splice_view);
2065 image_view=DestroyCacheView(image_view);
2066 if (status == MagickFalse)
2067 splice_image=DestroyImage(splice_image);
2068 return(splice_image);
2069}
2070␌
2071/*
2072%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2073% %
2074% %
2075% %
2076% T r a n s f o r m I m a g e %
2077% %
2078% %
2079% %
2080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2081%
2082% TransformImage() is a convenience method that behaves like ResizeImage() or
2083% CropImage() but accepts scaling and/or cropping information as a region
2084% geometry specification. If the operation fails, the original image handle
2085% is left as is.
2086%
2087% This should only be used for single images.
2088%
2089% This function destroys what it assumes to be a single image list.
2090% If the input image is part of a larger list, all other images in that list
2091% will be simply 'lost', not destroyed.
2092%
2093% Also if the crop generates a list of images only the first image is resized.
2094% And finally if the crop succeeds and the resize failed, you will get a
2095% cropped image, as well as a 'false' or 'failed' report.
2096%
2097% This function and should probably be deprecated in favor of direct calls
2098% to CropImageToTiles() or ResizeImage(), as appropriate.
2099%
2100% The format of the TransformImage method is:
2101%
2102% MagickBooleanType TransformImage(Image **image,const char *crop_geometry,
2103% const char *image_geometry,ExceptionInfo *exception)
2104%
2105% A description of each parameter follows:
2106%
2107% o image: the image The transformed image is returned as this parameter.
2108%
2109% o crop_geometry: A crop geometry string. This geometry defines a
2110% subregion of the image to crop.
2111%
2112% o image_geometry: An image geometry string. This geometry defines the
2113% final size of the image.
2114%
2115% o exception: return any errors or warnings in this structure.
2116%
2117*/
2118MagickPrivate MagickBooleanType TransformImage(Image **image,
2119 const char *crop_geometry,const char *image_geometry,ExceptionInfo *exception)
2120{
2121 Image
2122 *resize_image,
2123 *transform_image;
2124
2125 RectangleInfo
2126 geometry;
2127
2128 assert(image != (Image **) NULL);
2129 assert((*image)->signature == MagickCoreSignature);
2130 if (IsEventLogging() != MagickFalse)
2131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
2132 transform_image=(*image);
2133 if (crop_geometry != (const char *) NULL)
2134 {
2135 Image
2136 *crop_image;
2137
2138 /*
2139 Crop image to a user specified size.
2140 */
2141 crop_image=CropImageToTiles(*image,crop_geometry,exception);
2142 if (crop_image == (Image *) NULL)
2143 transform_image=CloneImage(*image,0,0,MagickTrue,exception);
2144 else
2145 {
2146 transform_image=DestroyImage(transform_image);
2147 transform_image=GetFirstImageInList(crop_image);
2148 }
2149 *image=transform_image;
2150 }
2151 if (transform_image == (Image *) NULL)
2152 return(MagickFalse);
2153 /*
2154 Scale image to a user specified size.
2155 */
2156 if (image_geometry == (const char *) NULL)
2157 return(MagickTrue);
2158 (void) ParseRegionGeometry(transform_image,image_geometry,&geometry,
2159 exception);
2160 if ((transform_image->columns == geometry.width) &&
2161 (transform_image->rows == geometry.height))
2162 return(MagickTrue);
2163 resize_image=ResizeImage(transform_image,geometry.width,geometry.height,
2164 transform_image->filter,exception);
2165 if (resize_image == (Image *) NULL)
2166 return(MagickFalse);
2167 transform_image=DestroyImage(transform_image);
2168 transform_image=resize_image;
2169 *image=transform_image;
2170 return(MagickTrue);
2171}
2172␌
2173/*
2174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2175% %
2176% %
2177% %
2178% T r a n s p o s e I m a g e %
2179% %
2180% %
2181% %
2182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2183%
2184% TransposeImage() creates a horizontal mirror image by reflecting the pixels
2185% around the central y-axis while rotating them by 90 degrees.
2186%
2187% The format of the TransposeImage method is:
2188%
2189% Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2190%
2191% A description of each parameter follows:
2192%
2193% o image: the image.
2194%
2195% o exception: return any errors or warnings in this structure.
2196%
2197*/
2198MagickExport Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2199{
2200#define TransposeImageTag "Transpose/Image"
2201
2202 CacheView
2203 *image_view,
2204 *transpose_view;
2205
2206 Image
2207 *transpose_image;
2208
2209 MagickBooleanType
2210 status;
2211
2212 MagickOffsetType
2213 progress;
2214
2215 RectangleInfo
2216 page;
2217
2218 ssize_t
2219 y;
2220
2221 assert(image != (const Image *) NULL);
2222 assert(image->signature == MagickCoreSignature);
2223 assert(exception != (ExceptionInfo *) NULL);
2224 assert(exception->signature == MagickCoreSignature);
2225 if (IsEventLogging() != MagickFalse)
2226 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2227 transpose_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2228 exception);
2229 if (transpose_image == (Image *) NULL)
2230 return((Image *) NULL);
2231 /*
2232 Transpose image.
2233 */
2234 status=MagickTrue;
2235 progress=0;
2236 image_view=AcquireVirtualCacheView(image,exception);
2237 transpose_view=AcquireAuthenticCacheView(transpose_image,exception);
2238#if defined(MAGICKCORE_OPENMP_SUPPORT)
2239 #pragma omp parallel for schedule(static) shared(progress,status) \
2240 magick_number_threads(image,transpose_image,image->rows,2)
2241#endif
2242 for (y=0; y < (ssize_t) image->rows; y++)
2243 {
2244 const Quantum
2245 *magick_restrict p;
2246
2247 Quantum
2248 *magick_restrict q;
2249
2250 ssize_t
2251 x;
2252
2253 if (status == MagickFalse)
2254 continue;
2255 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-y-1,
2256 image->columns,1,exception);
2257 q=QueueCacheViewAuthenticPixels(transpose_view,(ssize_t) image->rows-y-1,
2258 0,1,transpose_image->rows,exception);
2259 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2260 {
2261 status=MagickFalse;
2262 continue;
2263 }
2264 for (x=0; x < (ssize_t) image->columns; x++)
2265 {
2266 ssize_t
2267 i;
2268
2269 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2270 {
2271 PixelChannel channel = GetPixelChannelChannel(image,i);
2272 PixelTrait traits = GetPixelChannelTraits(image,channel);
2273 PixelTrait transpose_traits=GetPixelChannelTraits(transpose_image,
2274 channel);
2275 if ((traits == UndefinedPixelTrait) ||
2276 (transpose_traits == UndefinedPixelTrait))
2277 continue;
2278 SetPixelChannel(transpose_image,channel,p[i],q);
2279 }
2280 p+=(ptrdiff_t) GetPixelChannels(image);
2281 q+=(ptrdiff_t) GetPixelChannels(transpose_image);
2282 }
2283 if (SyncCacheViewAuthenticPixels(transpose_view,exception) == MagickFalse)
2284 status=MagickFalse;
2285 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2286 {
2287 MagickBooleanType
2288 proceed;
2289
2290#if defined(MAGICKCORE_OPENMP_SUPPORT)
2291 #pragma omp atomic
2292#endif
2293 progress++;
2294 proceed=SetImageProgress(image,TransposeImageTag,progress,image->rows);
2295 if (proceed == MagickFalse)
2296 status=MagickFalse;
2297 }
2298 }
2299 transpose_view=DestroyCacheView(transpose_view);
2300 image_view=DestroyCacheView(image_view);
2301 transpose_image->type=image->type;
2302 page=transpose_image->page;
2303 Swap(page.width,page.height);
2304 Swap(page.x,page.y);
2305 transpose_image->page=page;
2306 if (status != MagickFalse)
2307 {
2308 char
2309 transform[MagickPathExtent];
2310
2311 (void) FormatLocaleString(transform,MagickPathExtent,
2312 "transpose %.17gx%.17g",(double) image->columns,
2313 (double) image->rows);
2314 AppendImageProfileProperty(transpose_image,"hdrgm","hdrgm:Transform",
2315 transform,exception);
2316 }
2317 if (status == MagickFalse)
2318 transpose_image=DestroyImage(transpose_image);
2319 return(transpose_image);
2320}
2321␌
2322/*
2323%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2324% %
2325% %
2326% %
2327% T r a n s v e r s e I m a g e %
2328% %
2329% %
2330% %
2331%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2332%
2333% TransverseImage() creates a vertical mirror image by reflecting the pixels
2334% around the central x-axis while rotating them by 270 degrees.
2335%
2336% The format of the TransverseImage method is:
2337%
2338% Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2339%
2340% A description of each parameter follows:
2341%
2342% o image: the image.
2343%
2344% o exception: return any errors or warnings in this structure.
2345%
2346*/
2347MagickExport Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2348{
2349#define TransverseImageTag "Transverse/Image"
2350
2351 CacheView
2352 *image_view,
2353 *transverse_view;
2354
2355 Image
2356 *transverse_image;
2357
2358 MagickBooleanType
2359 status;
2360
2361 MagickOffsetType
2362 progress;
2363
2364 RectangleInfo
2365 page;
2366
2367 ssize_t
2368 y;
2369
2370 assert(image != (const Image *) NULL);
2371 assert(image->signature == MagickCoreSignature);
2372 assert(exception != (ExceptionInfo *) NULL);
2373 assert(exception->signature == MagickCoreSignature);
2374 if (IsEventLogging() != MagickFalse)
2375 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2376 transverse_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2377 exception);
2378 if (transverse_image == (Image *) NULL)
2379 return((Image *) NULL);
2380 /*
2381 Transverse image.
2382 */
2383 status=MagickTrue;
2384 progress=0;
2385 image_view=AcquireVirtualCacheView(image,exception);
2386 transverse_view=AcquireAuthenticCacheView(transverse_image,exception);
2387#if defined(MAGICKCORE_OPENMP_SUPPORT)
2388 #pragma omp parallel for schedule(static) shared(progress,status) \
2389 magick_number_threads(image,transverse_image,image->rows,2)
2390#endif
2391 for (y=0; y < (ssize_t) image->rows; y++)
2392 {
2393 MagickBooleanType
2394 sync;
2395
2396 const Quantum
2397 *magick_restrict p;
2398
2399 Quantum
2400 *magick_restrict q;
2401
2402 ssize_t
2403 x;
2404
2405 if (status == MagickFalse)
2406 continue;
2407 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2408 q=QueueCacheViewAuthenticPixels(transverse_view,(ssize_t) image->rows-y-1,
2409 0,1,transverse_image->rows,exception);
2410 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2411 {
2412 status=MagickFalse;
2413 continue;
2414 }
2415 q+=(ptrdiff_t) GetPixelChannels(transverse_image)*image->columns;
2416 for (x=0; x < (ssize_t) image->columns; x++)
2417 {
2418 ssize_t
2419 i;
2420
2421 q-=GetPixelChannels(transverse_image);
2422 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2423 {
2424 PixelChannel channel = GetPixelChannelChannel(image,i);
2425 PixelTrait traits = GetPixelChannelTraits(image,channel);
2426 PixelTrait transverse_traits=GetPixelChannelTraits(transverse_image,
2427 channel);
2428 if ((traits == UndefinedPixelTrait) ||
2429 (transverse_traits == UndefinedPixelTrait))
2430 continue;
2431 SetPixelChannel(transverse_image,channel,p[i],q);
2432 }
2433 p+=(ptrdiff_t) GetPixelChannels(image);
2434 }
2435 sync=SyncCacheViewAuthenticPixels(transverse_view,exception);
2436 if (sync == MagickFalse)
2437 status=MagickFalse;
2438 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2439 {
2440 MagickBooleanType
2441 proceed;
2442
2443#if defined(MAGICKCORE_OPENMP_SUPPORT)
2444 #pragma omp atomic
2445#endif
2446 progress++;
2447 proceed=SetImageProgress(image,TransverseImageTag,progress,image->rows);
2448 if (proceed == MagickFalse)
2449 status=MagickFalse;
2450 }
2451 }
2452 transverse_view=DestroyCacheView(transverse_view);
2453 image_view=DestroyCacheView(image_view);
2454 transverse_image->type=image->type;
2455 page=transverse_image->page;
2456 Swap(page.width,page.height);
2457 Swap(page.x,page.y);
2458 if (page.width != 0)
2459 page.x=(ssize_t) page.width-(ssize_t) transverse_image->columns-page.x;
2460 if (page.height != 0)
2461 page.y=(ssize_t) page.height-(ssize_t) transverse_image->rows-page.y;
2462 transverse_image->page=page;
2463 if (status != MagickFalse)
2464 {
2465 char
2466 transform[MagickPathExtent];
2467
2468 (void) FormatLocaleString(transform,MagickPathExtent,
2469 "transverse %.17gx%.17g",(double) image->columns,
2470 (double) image->rows);
2471 AppendImageProfileProperty(transverse_image,"hdrgm","hdrgm:Transform",
2472 transform,exception);
2473 }
2474 if (status == MagickFalse)
2475 transverse_image=DestroyImage(transverse_image);
2476 return(transverse_image);
2477}
2478␌
2479/*
2480%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2481% %
2482% %
2483% %
2484% T r i m I m a g e %
2485% %
2486% %
2487% %
2488%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2489%
2490% TrimImage() trims pixels from the image edges. It allocates the memory
2491% necessary for the new Image structure and returns a pointer to the new
2492% image.
2493%
2494% The format of the TrimImage method is:
2495%
2496% Image *TrimImage(const Image *image,ExceptionInfo *exception)
2497%
2498% A description of each parameter follows:
2499%
2500% o image: the image.
2501%
2502% o exception: return any errors or warnings in this structure.
2503%
2504*/
2505MagickExport Image *TrimImage(const Image *image,ExceptionInfo *exception)
2506{
2507 const char
2508 *artifact;
2509
2510 Image
2511 *trim_image;
2512
2513 RectangleInfo
2514 geometry;
2515
2516 assert(image != (const Image *) NULL);
2517 assert(image->signature == MagickCoreSignature);
2518 if (IsEventLogging() != MagickFalse)
2519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2520 geometry=GetImageBoundingBox(image,exception);
2521 if ((geometry.width == 0) || (geometry.height == 0))
2522 {
2523 /*
2524 The image is empty: return a minimal 1x1 transparent placeholder.
2525 */
2526 trim_image=CloneImage(image,1,1,MagickTrue,exception);
2527 if (trim_image != (Image *) NULL)
2528 {
2529 trim_image->background_color.alpha_trait=BlendPixelTrait;
2530 trim_image->background_color.alpha=(MagickRealType) TransparentAlpha;
2531 (void) SetImageBackgroundColor(trim_image,exception);
2532 trim_image->page.width=image->columns;
2533 trim_image->page.height=image->rows;
2534 trim_image->page.x=(-1);
2535 trim_image->page.y=(-1);
2536 }
2537 return(trim_image);
2538 }
2539 artifact=GetImageArtifact(image,"trim:minSize");
2540 if (artifact != (const char *) NULL)
2541 {
2542 RectangleInfo
2543 minSize;
2544
2545 /*
2546 Ensure trim is not less than min size dimensions relative to gravity.
2547 */
2548 minSize=geometry;
2549 (void) ParseAbsoluteGeometry(artifact,&minSize);
2550 if ((geometry.width < minSize.width) ||
2551 (geometry.height < minSize.height))
2552 {
2553 minSize.x=(geometry.width < minSize.width) ? (ssize_t)
2554 (minSize.width-geometry.width) : 0;
2555 minSize.y=(geometry.height < minSize.height) ? (ssize_t)
2556 (minSize.height-geometry.height) : 0;
2557 switch (image->gravity)
2558 {
2559 case NorthWestGravity:
2560 {
2561 geometry.x-=minSize.x;
2562 geometry.y-=minSize.y;
2563 break;
2564 }
2565 case NorthGravity:
2566 {
2567 geometry.x-=minSize.x/2;
2568 geometry.y-=minSize.y;
2569 break;
2570 }
2571 case NorthEastGravity:
2572 {
2573 geometry.y-=minSize.y;
2574 break;
2575 }
2576 case WestGravity:
2577 {
2578 geometry.x-=minSize.x;
2579 geometry.y-=minSize.y/2;
2580 break;
2581 }
2582 case EastGravity:
2583 {
2584 geometry.y-=minSize.y/2;
2585 break;
2586 }
2587 case SouthWestGravity:
2588 {
2589 geometry.x-=minSize.x;
2590 break;
2591 }
2592 case SouthGravity:
2593 {
2594 geometry.x-=minSize.x/2;
2595 break;
2596 }
2597 case CenterGravity:
2598 {
2599 geometry.x-=minSize.x/2;
2600 geometry.y-=minSize.y/2;
2601 break;
2602 }
2603 case UndefinedGravity:
2604 case SouthEastGravity:
2605 default:
2606 break;
2607 }
2608 if (geometry.width < minSize.width)
2609 geometry.width=minSize.width;
2610 if (geometry.height < minSize.height)
2611 geometry.height=minSize.height;
2612 if (geometry.width > image->columns)
2613 geometry.width=image->columns;
2614 if (geometry.height > image->rows)
2615 geometry.height=image->rows;
2616 if (geometry.x < 0)
2617 geometry.x=0;
2618 if ((geometry.x+(ssize_t) geometry.width) > (ssize_t) image->columns)
2619 geometry.x=(ssize_t) image->columns-(ssize_t) geometry.width;
2620 if (geometry.y < 0)
2621 geometry.y=0;
2622 if ((geometry.y+(ssize_t) geometry.height) > (ssize_t) image->rows)
2623 geometry.y=(ssize_t) image->rows-(ssize_t) geometry.height;
2624 }
2625 }
2626 geometry.x+=image->page.x;
2627 geometry.y+=image->page.y;
2628 trim_image=CropImage(image,&geometry,exception);
2629 if (trim_image != (Image *) NULL)
2630 {
2631 trim_image->page.width=image->columns;
2632 trim_image->page.height=image->rows;
2633 if (trim_image->page.x == 0)
2634 trim_image->page.x=geometry.x;
2635 if (trim_image->page.y == 0)
2636 trim_image->page.y=geometry.y;
2637 }
2638 return(trim_image);
2639}