MagickCore 7.1.2-36
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 y;
1792
1793 /*
1794 Allocate splice image.
1795 */
1796 assert(image != (const Image *) NULL);
1797 assert(image->signature == MagickCoreSignature);
1798 if (IsEventLogging() != MagickFalse)
1799 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1800 assert(geometry != (const RectangleInfo *) NULL);
1801 assert(exception != (ExceptionInfo *) NULL);
1802 assert(exception->signature == MagickCoreSignature);
1803 splice_geometry=(*geometry);
1804 splice_image=CloneImage(image,image->columns+splice_geometry.width,
1805 image->rows+splice_geometry.height,MagickTrue,exception);
1806 if (splice_image == (Image *) NULL)
1807 return((Image *) NULL);
1808 if (SetImageStorageClass(splice_image,DirectClass,exception) == MagickFalse)
1809 {
1810 splice_image=DestroyImage(splice_image);
1811 return((Image *) NULL);
1812 }
1813 if ((IsPixelInfoGray(&splice_image->background_color) == MagickFalse) &&
1814 (IsGrayColorspace(splice_image->colorspace) != MagickFalse))
1815 (void) SetImageColorspace(splice_image,sRGBColorspace,exception);
1816 if ((splice_image->background_color.alpha_trait != UndefinedPixelTrait) &&
1817 (splice_image->alpha_trait == UndefinedPixelTrait))
1818 (void) SetImageAlpha(splice_image,OpaqueAlpha,exception);
1819 (void) SetImageBackgroundColor(splice_image,exception);
1820 /*
1821 Respect image geometry.
1822 */
1823 switch (image->gravity)
1824 {
1825 default:
1826 case UndefinedGravity:
1827 case NorthWestGravity:
1828 break;
1829 case NorthGravity:
1830 {
1831 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1832 break;
1833 }
1834 case NorthEastGravity:
1835 {
1836 splice_geometry.x+=(ssize_t) splice_geometry.width;
1837 break;
1838 }
1839 case WestGravity:
1840 {
1841 splice_geometry.y+=(ssize_t) splice_geometry.width/2;
1842 break;
1843 }
1844 case CenterGravity:
1845 {
1846 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1847 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1848 break;
1849 }
1850 case EastGravity:
1851 {
1852 splice_geometry.x+=(ssize_t) splice_geometry.width;
1853 splice_geometry.y+=(ssize_t) splice_geometry.height/2;
1854 break;
1855 }
1856 case SouthWestGravity:
1857 {
1858 splice_geometry.y+=(ssize_t) splice_geometry.height;
1859 break;
1860 }
1861 case SouthGravity:
1862 {
1863 splice_geometry.x+=(ssize_t) splice_geometry.width/2;
1864 splice_geometry.y+=(ssize_t) splice_geometry.height;
1865 break;
1866 }
1867 case SouthEastGravity:
1868 {
1869 splice_geometry.x+=(ssize_t) splice_geometry.width;
1870 splice_geometry.y+=(ssize_t) splice_geometry.height;
1871 break;
1872 }
1873 }
1874 /*
1875 Splice image.
1876 */
1877 status=MagickTrue;
1878 progress=0;
1879 columns=MagickMin(splice_geometry.x,(ssize_t) splice_image->columns);
1880 image_view=AcquireVirtualCacheView(image,exception);
1881 splice_view=AcquireAuthenticCacheView(splice_image,exception);
1882#if defined(MAGICKCORE_OPENMP_SUPPORT)
1883 #pragma omp parallel for schedule(static) shared(progress,status) \
1884 magick_number_threads(image,splice_image,(size_t) splice_geometry.y,2)
1885#endif
1886 for (y=0; y < MagickMin(splice_geometry.y,(ssize_t) splice_image->rows); y++)
1887 {
1888 const Quantum
1889 *magick_restrict p;
1890
1891 ssize_t
1892 x;
1893
1894 Quantum
1895 *magick_restrict q;
1896
1897 if (status == MagickFalse)
1898 continue;
1899 p=GetCacheViewVirtualPixels(image_view,0,y,splice_image->columns,1,
1900 exception);
1901 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1902 exception);
1903 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1904 {
1905 status=MagickFalse;
1906 continue;
1907 }
1908 for (x=0; x < columns; x++)
1909 {
1910 ssize_t
1911 i;
1912
1913 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1914 {
1915 PixelChannel channel = GetPixelChannelChannel(image,i);
1916 PixelTrait traits = GetPixelChannelTraits(image,channel);
1917 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
1918 if ((traits == UndefinedPixelTrait) ||
1919 (splice_traits == UndefinedPixelTrait))
1920 continue;
1921 SetPixelChannel(splice_image,channel,p[i],q);
1922 }
1923 SetPixelRed(splice_image,GetPixelRed(image,p),q);
1924 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
1925 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
1926 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
1927 p+=(ptrdiff_t) GetPixelChannels(image);
1928 q+=(ptrdiff_t) GetPixelChannels(splice_image);
1929 }
1930 for ( ; x < (splice_geometry.x+(ssize_t) splice_geometry.width); x++)
1931 q+=(ptrdiff_t) GetPixelChannels(splice_image);
1932 for ( ; x < (ssize_t) splice_image->columns; x++)
1933 {
1934 ssize_t
1935 i;
1936
1937 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1938 {
1939 PixelChannel channel = GetPixelChannelChannel(image,i);
1940 PixelTrait traits = GetPixelChannelTraits(image,channel);
1941 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
1942 if ((traits == UndefinedPixelTrait) ||
1943 (splice_traits == UndefinedPixelTrait))
1944 continue;
1945 SetPixelChannel(splice_image,channel,p[i],q);
1946 }
1947 SetPixelRed(splice_image,GetPixelRed(image,p),q);
1948 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
1949 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
1950 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
1951 p+=(ptrdiff_t) GetPixelChannels(image);
1952 q+=(ptrdiff_t) GetPixelChannels(splice_image);
1953 }
1954 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
1955 status=MagickFalse;
1956 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1957 {
1958 MagickBooleanType
1959 proceed;
1960
1961#if defined(MAGICKCORE_OPENMP_SUPPORT)
1962 #pragma omp atomic
1963#endif
1964 progress++;
1965 proceed=SetImageProgress(image,SpliceImageTag,progress,
1966 splice_image->rows);
1967 if (proceed == MagickFalse)
1968 status=MagickFalse;
1969 }
1970 }
1971#if defined(MAGICKCORE_OPENMP_SUPPORT)
1972 #pragma omp parallel for schedule(static) shared(progress,status) \
1973 magick_number_threads(image,splice_image,splice_image->rows,2)
1974#endif
1975 for (y=splice_geometry.y+(ssize_t) splice_geometry.height; y < (ssize_t) splice_image->rows; y++)
1976 {
1977 const Quantum
1978 *magick_restrict p;
1979
1980 ssize_t
1981 x;
1982
1983 Quantum
1984 *magick_restrict q;
1985
1986 if (status == MagickFalse)
1987 continue;
1988 if ((y < 0) || (y >= (ssize_t) splice_image->rows))
1989 continue;
1990 p=GetCacheViewVirtualPixels(image_view,0,y-(ssize_t) splice_geometry.height,
1991 splice_image->columns,1,exception);
1992 q=QueueCacheViewAuthenticPixels(splice_view,0,y,splice_image->columns,1,
1993 exception);
1994 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1995 {
1996 status=MagickFalse;
1997 continue;
1998 }
1999 for (x=0; x < columns; x++)
2000 {
2001 ssize_t
2002 i;
2003
2004 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2005 {
2006 PixelChannel channel = GetPixelChannelChannel(image,i);
2007 PixelTrait traits = GetPixelChannelTraits(image,channel);
2008 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
2009 if ((traits == UndefinedPixelTrait) ||
2010 (splice_traits == UndefinedPixelTrait))
2011 continue;
2012 SetPixelChannel(splice_image,channel,p[i],q);
2013 }
2014 SetPixelRed(splice_image,GetPixelRed(image,p),q);
2015 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
2016 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
2017 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
2018 p+=(ptrdiff_t) GetPixelChannels(image);
2019 q+=(ptrdiff_t) GetPixelChannels(splice_image);
2020 }
2021 for ( ; x < (splice_geometry.x+(ssize_t) splice_geometry.width); x++)
2022 q+=(ptrdiff_t) GetPixelChannels(splice_image);
2023 for ( ; x < (ssize_t) splice_image->columns; x++)
2024 {
2025 ssize_t
2026 i;
2027
2028 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2029 {
2030 PixelChannel channel = GetPixelChannelChannel(image,i);
2031 PixelTrait traits = GetPixelChannelTraits(image,channel);
2032 PixelTrait splice_traits=GetPixelChannelTraits(splice_image,channel);
2033 if ((traits == UndefinedPixelTrait) ||
2034 (splice_traits == UndefinedPixelTrait))
2035 continue;
2036 SetPixelChannel(splice_image,channel,p[i],q);
2037 }
2038 SetPixelRed(splice_image,GetPixelRed(image,p),q);
2039 SetPixelGreen(splice_image,GetPixelGreen(image,p),q);
2040 SetPixelBlue(splice_image,GetPixelBlue(image,p),q);
2041 SetPixelAlpha(splice_image,GetPixelAlpha(image,p),q);
2042 p+=(ptrdiff_t) GetPixelChannels(image);
2043 q+=(ptrdiff_t) GetPixelChannels(splice_image);
2044 }
2045 if (SyncCacheViewAuthenticPixels(splice_view,exception) == MagickFalse)
2046 status=MagickFalse;
2047 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2048 {
2049 MagickBooleanType
2050 proceed;
2051
2052#if defined(MAGICKCORE_OPENMP_SUPPORT)
2053 #pragma omp atomic
2054#endif
2055 progress++;
2056 proceed=SetImageProgress(image,SpliceImageTag,progress,
2057 splice_image->rows);
2058 if (proceed == MagickFalse)
2059 status=MagickFalse;
2060 }
2061 }
2062 splice_view=DestroyCacheView(splice_view);
2063 image_view=DestroyCacheView(image_view);
2064 if (status == MagickFalse)
2065 splice_image=DestroyImage(splice_image);
2066 return(splice_image);
2067}
2068␌
2069/*
2070%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2071% %
2072% %
2073% %
2074% T r a n s f o r m I m a g e %
2075% %
2076% %
2077% %
2078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2079%
2080% TransformImage() is a convenience method that behaves like ResizeImage() or
2081% CropImage() but accepts scaling and/or cropping information as a region
2082% geometry specification. If the operation fails, the original image handle
2083% is left as is.
2084%
2085% This should only be used for single images.
2086%
2087% This function destroys what it assumes to be a single image list.
2088% If the input image is part of a larger list, all other images in that list
2089% will be simply 'lost', not destroyed.
2090%
2091% Also if the crop generates a list of images only the first image is resized.
2092% And finally if the crop succeeds and the resize failed, you will get a
2093% cropped image, as well as a 'false' or 'failed' report.
2094%
2095% This function and should probably be deprecated in favor of direct calls
2096% to CropImageToTiles() or ResizeImage(), as appropriate.
2097%
2098% The format of the TransformImage method is:
2099%
2100% MagickBooleanType TransformImage(Image **image,const char *crop_geometry,
2101% const char *image_geometry,ExceptionInfo *exception)
2102%
2103% A description of each parameter follows:
2104%
2105% o image: the image The transformed image is returned as this parameter.
2106%
2107% o crop_geometry: A crop geometry string. This geometry defines a
2108% subregion of the image to crop.
2109%
2110% o image_geometry: An image geometry string. This geometry defines the
2111% final size of the image.
2112%
2113% o exception: return any errors or warnings in this structure.
2114%
2115*/
2116MagickPrivate MagickBooleanType TransformImage(Image **image,
2117 const char *crop_geometry,const char *image_geometry,ExceptionInfo *exception)
2118{
2119 Image
2120 *resize_image,
2121 *transform_image;
2122
2123 RectangleInfo
2124 geometry;
2125
2126 assert(image != (Image **) NULL);
2127 assert((*image)->signature == MagickCoreSignature);
2128 if (IsEventLogging() != MagickFalse)
2129 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",(*image)->filename);
2130 transform_image=(*image);
2131 if (crop_geometry != (const char *) NULL)
2132 {
2133 Image
2134 *crop_image;
2135
2136 /*
2137 Crop image to a user specified size.
2138 */
2139 crop_image=CropImageToTiles(*image,crop_geometry,exception);
2140 if (crop_image == (Image *) NULL)
2141 transform_image=CloneImage(*image,0,0,MagickTrue,exception);
2142 else
2143 {
2144 transform_image=DestroyImage(transform_image);
2145 transform_image=GetFirstImageInList(crop_image);
2146 }
2147 *image=transform_image;
2148 }
2149 if (transform_image == (Image *) NULL)
2150 return(MagickFalse);
2151 /*
2152 Scale image to a user specified size.
2153 */
2154 if (image_geometry == (const char *) NULL)
2155 return(MagickTrue);
2156 (void) ParseRegionGeometry(transform_image,image_geometry,&geometry,
2157 exception);
2158 if ((transform_image->columns == geometry.width) &&
2159 (transform_image->rows == geometry.height))
2160 return(MagickTrue);
2161 resize_image=ResizeImage(transform_image,geometry.width,geometry.height,
2162 transform_image->filter,exception);
2163 if (resize_image == (Image *) NULL)
2164 return(MagickFalse);
2165 transform_image=DestroyImage(transform_image);
2166 transform_image=resize_image;
2167 *image=transform_image;
2168 return(MagickTrue);
2169}
2170␌
2171/*
2172%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2173% %
2174% %
2175% %
2176% T r a n s p o s e I m a g e %
2177% %
2178% %
2179% %
2180%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2181%
2182% TransposeImage() creates a horizontal mirror image by reflecting the pixels
2183% around the central y-axis while rotating them by 90 degrees.
2184%
2185% The format of the TransposeImage method is:
2186%
2187% Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2188%
2189% A description of each parameter follows:
2190%
2191% o image: the image.
2192%
2193% o exception: return any errors or warnings in this structure.
2194%
2195*/
2196MagickExport Image *TransposeImage(const Image *image,ExceptionInfo *exception)
2197{
2198#define TransposeImageTag "Transpose/Image"
2199
2200 CacheView
2201 *image_view,
2202 *transpose_view;
2203
2204 Image
2205 *transpose_image;
2206
2207 MagickBooleanType
2208 status;
2209
2210 MagickOffsetType
2211 progress;
2212
2213 RectangleInfo
2214 page;
2215
2216 ssize_t
2217 y;
2218
2219 assert(image != (const Image *) NULL);
2220 assert(image->signature == MagickCoreSignature);
2221 assert(exception != (ExceptionInfo *) NULL);
2222 assert(exception->signature == MagickCoreSignature);
2223 if (IsEventLogging() != MagickFalse)
2224 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2225 transpose_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2226 exception);
2227 if (transpose_image == (Image *) NULL)
2228 return((Image *) NULL);
2229 /*
2230 Transpose image.
2231 */
2232 status=MagickTrue;
2233 progress=0;
2234 image_view=AcquireVirtualCacheView(image,exception);
2235 transpose_view=AcquireAuthenticCacheView(transpose_image,exception);
2236#if defined(MAGICKCORE_OPENMP_SUPPORT)
2237 #pragma omp parallel for schedule(static) shared(progress,status) \
2238 magick_number_threads(image,transpose_image,image->rows,2)
2239#endif
2240 for (y=0; y < (ssize_t) image->rows; y++)
2241 {
2242 const Quantum
2243 *magick_restrict p;
2244
2245 Quantum
2246 *magick_restrict q;
2247
2248 ssize_t
2249 x;
2250
2251 if (status == MagickFalse)
2252 continue;
2253 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-y-1,
2254 image->columns,1,exception);
2255 q=QueueCacheViewAuthenticPixels(transpose_view,(ssize_t) image->rows-y-1,
2256 0,1,transpose_image->rows,exception);
2257 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2258 {
2259 status=MagickFalse;
2260 continue;
2261 }
2262 for (x=0; x < (ssize_t) image->columns; x++)
2263 {
2264 ssize_t
2265 i;
2266
2267 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2268 {
2269 PixelChannel channel = GetPixelChannelChannel(image,i);
2270 PixelTrait traits = GetPixelChannelTraits(image,channel);
2271 PixelTrait transpose_traits=GetPixelChannelTraits(transpose_image,
2272 channel);
2273 if ((traits == UndefinedPixelTrait) ||
2274 (transpose_traits == UndefinedPixelTrait))
2275 continue;
2276 SetPixelChannel(transpose_image,channel,p[i],q);
2277 }
2278 p+=(ptrdiff_t) GetPixelChannels(image);
2279 q+=(ptrdiff_t) GetPixelChannels(transpose_image);
2280 }
2281 if (SyncCacheViewAuthenticPixels(transpose_view,exception) == MagickFalse)
2282 status=MagickFalse;
2283 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2284 {
2285 MagickBooleanType
2286 proceed;
2287
2288#if defined(MAGICKCORE_OPENMP_SUPPORT)
2289 #pragma omp atomic
2290#endif
2291 progress++;
2292 proceed=SetImageProgress(image,TransposeImageTag,progress,image->rows);
2293 if (proceed == MagickFalse)
2294 status=MagickFalse;
2295 }
2296 }
2297 transpose_view=DestroyCacheView(transpose_view);
2298 image_view=DestroyCacheView(image_view);
2299 transpose_image->type=image->type;
2300 page=transpose_image->page;
2301 Swap(page.width,page.height);
2302 Swap(page.x,page.y);
2303 transpose_image->page=page;
2304 if (status != MagickFalse)
2305 {
2306 char
2307 transform[MagickPathExtent];
2308
2309 (void) FormatLocaleString(transform,MagickPathExtent,
2310 "transpose %.17gx%.17g",(double) image->columns,
2311 (double) image->rows);
2312 AppendImageProfileProperty(transpose_image,"hdrgm","hdrgm:Transform",
2313 transform,exception);
2314 }
2315 if (status == MagickFalse)
2316 transpose_image=DestroyImage(transpose_image);
2317 return(transpose_image);
2318}
2319␌
2320/*
2321%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2322% %
2323% %
2324% %
2325% T r a n s v e r s e I m a g e %
2326% %
2327% %
2328% %
2329%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2330%
2331% TransverseImage() creates a vertical mirror image by reflecting the pixels
2332% around the central x-axis while rotating them by 270 degrees.
2333%
2334% The format of the TransverseImage method is:
2335%
2336% Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2337%
2338% A description of each parameter follows:
2339%
2340% o image: the image.
2341%
2342% o exception: return any errors or warnings in this structure.
2343%
2344*/
2345MagickExport Image *TransverseImage(const Image *image,ExceptionInfo *exception)
2346{
2347#define TransverseImageTag "Transverse/Image"
2348
2349 CacheView
2350 *image_view,
2351 *transverse_view;
2352
2353 Image
2354 *transverse_image;
2355
2356 MagickBooleanType
2357 status;
2358
2359 MagickOffsetType
2360 progress;
2361
2362 RectangleInfo
2363 page;
2364
2365 ssize_t
2366 y;
2367
2368 assert(image != (const Image *) NULL);
2369 assert(image->signature == MagickCoreSignature);
2370 assert(exception != (ExceptionInfo *) NULL);
2371 assert(exception->signature == MagickCoreSignature);
2372 if (IsEventLogging() != MagickFalse)
2373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2374 transverse_image=CloneImage(image,image->rows,image->columns,MagickTrue,
2375 exception);
2376 if (transverse_image == (Image *) NULL)
2377 return((Image *) NULL);
2378 /*
2379 Transverse image.
2380 */
2381 status=MagickTrue;
2382 progress=0;
2383 image_view=AcquireVirtualCacheView(image,exception);
2384 transverse_view=AcquireAuthenticCacheView(transverse_image,exception);
2385#if defined(MAGICKCORE_OPENMP_SUPPORT)
2386 #pragma omp parallel for schedule(static) shared(progress,status) \
2387 magick_number_threads(image,transverse_image,image->rows,2)
2388#endif
2389 for (y=0; y < (ssize_t) image->rows; y++)
2390 {
2391 MagickBooleanType
2392 sync;
2393
2394 const Quantum
2395 *magick_restrict p;
2396
2397 Quantum
2398 *magick_restrict q;
2399
2400 ssize_t
2401 x;
2402
2403 if (status == MagickFalse)
2404 continue;
2405 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2406 q=QueueCacheViewAuthenticPixels(transverse_view,(ssize_t) image->rows-y-1,
2407 0,1,transverse_image->rows,exception);
2408 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2409 {
2410 status=MagickFalse;
2411 continue;
2412 }
2413 q+=(ptrdiff_t) GetPixelChannels(transverse_image)*image->columns;
2414 for (x=0; x < (ssize_t) image->columns; x++)
2415 {
2416 ssize_t
2417 i;
2418
2419 q-=GetPixelChannels(transverse_image);
2420 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2421 {
2422 PixelChannel channel = GetPixelChannelChannel(image,i);
2423 PixelTrait traits = GetPixelChannelTraits(image,channel);
2424 PixelTrait transverse_traits=GetPixelChannelTraits(transverse_image,
2425 channel);
2426 if ((traits == UndefinedPixelTrait) ||
2427 (transverse_traits == UndefinedPixelTrait))
2428 continue;
2429 SetPixelChannel(transverse_image,channel,p[i],q);
2430 }
2431 p+=(ptrdiff_t) GetPixelChannels(image);
2432 }
2433 sync=SyncCacheViewAuthenticPixels(transverse_view,exception);
2434 if (sync == MagickFalse)
2435 status=MagickFalse;
2436 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2437 {
2438 MagickBooleanType
2439 proceed;
2440
2441#if defined(MAGICKCORE_OPENMP_SUPPORT)
2442 #pragma omp atomic
2443#endif
2444 progress++;
2445 proceed=SetImageProgress(image,TransverseImageTag,progress,image->rows);
2446 if (proceed == MagickFalse)
2447 status=MagickFalse;
2448 }
2449 }
2450 transverse_view=DestroyCacheView(transverse_view);
2451 image_view=DestroyCacheView(image_view);
2452 transverse_image->type=image->type;
2453 page=transverse_image->page;
2454 Swap(page.width,page.height);
2455 Swap(page.x,page.y);
2456 if (page.width != 0)
2457 page.x=(ssize_t) page.width-(ssize_t) transverse_image->columns-page.x;
2458 if (page.height != 0)
2459 page.y=(ssize_t) page.height-(ssize_t) transverse_image->rows-page.y;
2460 transverse_image->page=page;
2461 if (status != MagickFalse)
2462 {
2463 char
2464 transform[MagickPathExtent];
2465
2466 (void) FormatLocaleString(transform,MagickPathExtent,
2467 "transverse %.17gx%.17g",(double) image->columns,
2468 (double) image->rows);
2469 AppendImageProfileProperty(transverse_image,"hdrgm","hdrgm:Transform",
2470 transform,exception);
2471 }
2472 if (status == MagickFalse)
2473 transverse_image=DestroyImage(transverse_image);
2474 return(transverse_image);
2475}
2476␌
2477/*
2478%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2479% %
2480% %
2481% %
2482% T r i m I m a g e %
2483% %
2484% %
2485% %
2486%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2487%
2488% TrimImage() trims pixels from the image edges. It allocates the memory
2489% necessary for the new Image structure and returns a pointer to the new
2490% image.
2491%
2492% The format of the TrimImage method is:
2493%
2494% Image *TrimImage(const Image *image,ExceptionInfo *exception)
2495%
2496% A description of each parameter follows:
2497%
2498% o image: the image.
2499%
2500% o exception: return any errors or warnings in this structure.
2501%
2502*/
2503MagickExport Image *TrimImage(const Image *image,ExceptionInfo *exception)
2504{
2505 const char
2506 *artifact;
2507
2508 Image
2509 *trim_image;
2510
2511 RectangleInfo
2512 geometry;
2513
2514 assert(image != (const Image *) NULL);
2515 assert(image->signature == MagickCoreSignature);
2516 if (IsEventLogging() != MagickFalse)
2517 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2518 geometry=GetImageBoundingBox(image,exception);
2519 if ((geometry.width == 0) || (geometry.height == 0))
2520 {
2521 /*
2522 The image is empty: return a minimal 1x1 transparent placeholder.
2523 */
2524 trim_image=CloneImage(image,1,1,MagickTrue,exception);
2525 if (trim_image != (Image *) NULL)
2526 {
2527 trim_image->background_color.alpha_trait=BlendPixelTrait;
2528 trim_image->background_color.alpha=(MagickRealType) TransparentAlpha;
2529 (void) SetImageBackgroundColor(trim_image,exception);
2530 trim_image->page.width=image->columns;
2531 trim_image->page.height=image->rows;
2532 trim_image->page.x=(-1);
2533 trim_image->page.y=(-1);
2534 }
2535 return(trim_image);
2536 }
2537 artifact=GetImageArtifact(image,"trim:minSize");
2538 if (artifact != (const char *) NULL)
2539 {
2540 RectangleInfo
2541 minSize;
2542
2543 /*
2544 Ensure trim is not less than min size dimensions relative to gravity.
2545 */
2546 minSize=geometry;
2547 (void) ParseAbsoluteGeometry(artifact,&minSize);
2548 if ((geometry.width < minSize.width) ||
2549 (geometry.height < minSize.height))
2550 {
2551 minSize.x=(geometry.width < minSize.width) ? (ssize_t)
2552 (minSize.width-geometry.width) : 0;
2553 minSize.y=(geometry.height < minSize.height) ? (ssize_t)
2554 (minSize.height-geometry.height) : 0;
2555 switch (image->gravity)
2556 {
2557 case NorthWestGravity:
2558 {
2559 geometry.x-=minSize.x;
2560 geometry.y-=minSize.y;
2561 break;
2562 }
2563 case NorthGravity:
2564 {
2565 geometry.x-=minSize.x/2;
2566 geometry.y-=minSize.y;
2567 break;
2568 }
2569 case NorthEastGravity:
2570 {
2571 geometry.y-=minSize.y;
2572 break;
2573 }
2574 case WestGravity:
2575 {
2576 geometry.x-=minSize.x;
2577 geometry.y-=minSize.y/2;
2578 break;
2579 }
2580 case EastGravity:
2581 {
2582 geometry.y-=minSize.y/2;
2583 break;
2584 }
2585 case SouthWestGravity:
2586 {
2587 geometry.x-=minSize.x;
2588 break;
2589 }
2590 case SouthGravity:
2591 {
2592 geometry.x-=minSize.x/2;
2593 break;
2594 }
2595 case CenterGravity:
2596 {
2597 geometry.x-=minSize.x/2;
2598 geometry.y-=minSize.y/2;
2599 break;
2600 }
2601 case UndefinedGravity:
2602 case SouthEastGravity:
2603 default:
2604 break;
2605 }
2606 if (geometry.width < minSize.width)
2607 geometry.width=minSize.width;
2608 if (geometry.height < minSize.height)
2609 geometry.height=minSize.height;
2610 if (geometry.width > image->columns)
2611 geometry.width=image->columns;
2612 if (geometry.height > image->rows)
2613 geometry.height=image->rows;
2614 if (geometry.x < 0)
2615 geometry.x=0;
2616 if ((geometry.x+(ssize_t) geometry.width) > (ssize_t) image->columns)
2617 geometry.x=(ssize_t) image->columns-(ssize_t) geometry.width;
2618 if (geometry.y < 0)
2619 geometry.y=0;
2620 if ((geometry.y+(ssize_t) geometry.height) > (ssize_t) image->rows)
2621 geometry.y=(ssize_t) image->rows-(ssize_t) geometry.height;
2622 }
2623 }
2624 geometry.x+=image->page.x;
2625 geometry.y+=image->page.y;
2626 trim_image=CropImage(image,&geometry,exception);
2627 if (trim_image != (Image *) NULL)
2628 {
2629 trim_image->page.width=image->columns;
2630 trim_image->page.height=image->rows;
2631 if (trim_image->page.x == 0)
2632 trim_image->page.x=geometry.x;
2633 if (trim_image->page.y == 0)
2634 trim_image->page.y=geometry.y;
2635 }
2636 return(trim_image);
2637}