MagickCore 7.1.1
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
attribute.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% AAA TTTTT TTTTT RRRR IIIII BBBB U U TTTTT EEEEE %
7% A A T T R R I B B U U T E %
8% AAAAA T T RRRR I BBBB U U T EEE %
9% A A T T R R I B B U U T E %
10% A A T T R R IIIII BBBB UUU T EEEEE %
11% %
12% %
13% MagickCore Get / Set Image Attributes %
14% %
15% Software Design %
16% Cristy %
17% October 2002 %
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/script/license.php %
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/*
41 Include declarations.
42*/
43#include "MagickCore/studio.h"
44#include "MagickCore/artifact.h"
45#include "MagickCore/attribute.h"
46#include "MagickCore/blob.h"
47#include "MagickCore/blob-private.h"
48#include "MagickCore/cache.h"
49#include "MagickCore/cache-private.h"
50#include "MagickCore/cache-view.h"
51#include "MagickCore/channel.h"
52#include "MagickCore/client.h"
53#include "MagickCore/color.h"
54#include "MagickCore/color-private.h"
55#include "MagickCore/colormap.h"
56#include "MagickCore/colormap-private.h"
57#include "MagickCore/colorspace.h"
58#include "MagickCore/colorspace-private.h"
59#include "MagickCore/composite.h"
60#include "MagickCore/composite-private.h"
61#include "MagickCore/constitute.h"
62#include "MagickCore/draw.h"
63#include "MagickCore/draw-private.h"
64#include "MagickCore/effect.h"
65#include "MagickCore/enhance.h"
66#include "MagickCore/exception.h"
67#include "MagickCore/exception-private.h"
68#include "MagickCore/geometry.h"
69#include "MagickCore/histogram.h"
70#include "MagickCore/identify.h"
71#include "MagickCore/image.h"
72#include "MagickCore/image-private.h"
73#include "MagickCore/list.h"
74#include "MagickCore/log.h"
75#include "MagickCore/memory_.h"
76#include "MagickCore/magick.h"
77#include "MagickCore/monitor.h"
78#include "MagickCore/monitor-private.h"
79#include "MagickCore/option.h"
80#include "MagickCore/paint.h"
81#include "MagickCore/pixel.h"
82#include "MagickCore/pixel-accessor.h"
83#include "MagickCore/property.h"
84#include "MagickCore/quantize.h"
85#include "MagickCore/quantum-private.h"
86#include "MagickCore/random_.h"
87#include "MagickCore/resource_.h"
88#include "MagickCore/semaphore.h"
89#include "MagickCore/segment.h"
90#include "MagickCore/splay-tree.h"
91#include "MagickCore/string_.h"
92#include "MagickCore/string-private.h"
93#include "MagickCore/thread-private.h"
94#include "MagickCore/threshold.h"
95#include "MagickCore/transform.h"
96#include "MagickCore/utility.h"
97
98/*
99%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100% %
101% %
102% %
103+ G e t I m a g e B o u n d i n g B o x %
104% %
105% %
106% %
107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108%
109% GetImageBoundingBox() returns the bounding box of an image canvas.
110%
111% The format of the GetImageBoundingBox method is:
112%
113% RectangleInfo GetImageBoundingBox(const Image *image,
114% ExceptionInfo *exception)
115%
116% A description of each parameter follows:
117%
118% o bounds: Method GetImageBoundingBox returns the bounding box of an
119% image canvas.
120%
121% o image: the image.
122%
123% o exception: return any errors or warnings in this structure.
124%
125*/
126
127typedef struct _CensusInfo
128{
129 double
130 left,
131 right,
132 top,
133 bottom;
134} CensusInfo;
135
136static double GetEdgeBackgroundCensus(const Image *image,
137 const CacheView *image_view,const GravityType gravity,const size_t width,
138 const size_t height,const ssize_t x_offset,const ssize_t y_offset,
139 ExceptionInfo *exception)
140{
142 *edge_view;
143
144 const char
145 *artifact;
146
147 const Quantum
148 *p;
149
150 double
151 census;
152
153 Image
154 *edge_image;
155
157 background,
158 pixel;
159
161 edge_geometry;
162
163 ssize_t
164 y;
165
166 /*
167 Determine the percent of image background for this edge.
168 */
169 switch (gravity)
170 {
171 case NorthWestGravity:
172 case NorthGravity:
173 default:
174 {
175 p=GetCacheViewVirtualPixels(image_view,0,0,1,1,exception);
176 break;
177 }
178 case NorthEastGravity:
179 case EastGravity:
180 {
181 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,0,1,1,
182 exception);
183 break;
184 }
185 case SouthEastGravity:
186 case SouthGravity:
187 {
188 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,
189 (ssize_t) image->rows-1,1,1,exception);
190 break;
191 }
192 case SouthWestGravity:
193 case WestGravity:
194 {
195 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-1,1,1,
196 exception);
197 break;
198 }
199 }
200 if (p == (const Quantum *) NULL)
201 return(0.0);
202 GetPixelInfoPixel(image,p,&background);
203 artifact=GetImageArtifact(image,"background");
204 if (artifact != (const char *) NULL)
205 (void) QueryColorCompliance(artifact,AllCompliance,&background,exception);
206 artifact=GetImageArtifact(image,"trim:background-color");
207 if (artifact != (const char *) NULL)
208 (void) QueryColorCompliance(artifact,AllCompliance,&background,exception);
209 edge_geometry.width=width;
210 edge_geometry.height=height;
211 edge_geometry.x=x_offset;
212 edge_geometry.y=y_offset;
213 GravityAdjustGeometry(image->columns,image->rows,gravity,&edge_geometry);
214 edge_image=CropImage(image,&edge_geometry,exception);
215 if (edge_image == (Image *) NULL)
216 return(0.0);
217 census=0.0;
218 edge_view=AcquireVirtualCacheView(edge_image,exception);
219 for (y=0; y < (ssize_t) edge_image->rows; y++)
220 {
221 ssize_t
222 x;
223
224 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
225 if (p == (const Quantum *) NULL)
226 break;
227 for (x=0; x < (ssize_t) edge_image->columns; x++)
228 {
229 GetPixelInfoPixel(edge_image,p,&pixel);
230 if (IsFuzzyEquivalencePixelInfo(&pixel,&background) == MagickFalse)
231 census++;
232 p+=GetPixelChannels(edge_image);
233 }
234 }
235 census/=((double) edge_image->columns*edge_image->rows);
236 edge_view=DestroyCacheView(edge_view);
237 edge_image=DestroyImage(edge_image);
238 return(census);
239}
240
241static inline double GetMinEdgeBackgroundCensus(const CensusInfo *edge)
242{
243 double
244 census;
245
246 census=MagickMin(MagickMin(MagickMin(edge->left,edge->right),edge->top),
247 edge->bottom);
248 return(census);
249}
250
251static RectangleInfo GetEdgeBoundingBox(const Image *image,
252 ExceptionInfo *exception)
253{
255 *edge_view;
256
258 edge,
259 vertex;
260
261 const char
262 *artifact;
263
264 double
265 background_census,
266 percent_background;
267
268 Image
269 *edge_image;
270
272 bounds;
273
274 /*
275 Get the image bounding box.
276 */
277 assert(image != (Image *) NULL);
278 assert(image->signature == MagickCoreSignature);
279 if (IsEventLogging() != MagickFalse)
280 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
281 SetGeometry(image,&bounds);
282 edge_image=CloneImage(image,0,0,MagickTrue,exception);
283 if (edge_image == (Image *) NULL)
284 return(bounds);
285 (void) ParseAbsoluteGeometry("0x0+0+0",&edge_image->page);
286 (void) memset(&vertex,0,sizeof(vertex));
287 edge_view=AcquireVirtualCacheView(edge_image,exception);
288 edge.left=GetEdgeBackgroundCensus(edge_image,edge_view,WestGravity,
289 1,0,0,0,exception);
290 edge.right=GetEdgeBackgroundCensus(edge_image,edge_view,EastGravity,
291 1,0,0,0,exception);
292 edge.top=GetEdgeBackgroundCensus(edge_image,edge_view,NorthGravity,
293 0,1,0,0,exception);
294 edge.bottom=GetEdgeBackgroundCensus(edge_image,edge_view,SouthGravity,
295 0,1,0,0,exception);
296 percent_background=1.0;
297 artifact=GetImageArtifact(edge_image,"trim:percent-background");
298 if (artifact != (const char *) NULL)
299 percent_background=StringToDouble(artifact,(char **) NULL)/100.0;
300 percent_background=MagickMin(MagickMax(1.0-percent_background,MagickEpsilon),
301 1.0);
302 background_census=GetMinEdgeBackgroundCensus(&edge);
303 for ( ; background_census < percent_background;
304 background_census=GetMinEdgeBackgroundCensus(&edge))
305 {
306 if ((bounds.width == 0) || (bounds.height == 0))
307 break;
308 if (fabs(edge.left-background_census) < MagickEpsilon)
309 {
310 /*
311 Trim left edge.
312 */
313 vertex.left++;
314 bounds.width--;
315 edge.left=GetEdgeBackgroundCensus(edge_image,edge_view,
316 NorthWestGravity,1,bounds.height,(ssize_t) vertex.left,(ssize_t)
317 vertex.top,exception);
318 edge.top=GetEdgeBackgroundCensus(edge_image,edge_view,
319 NorthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
320 vertex.top,exception);
321 edge.bottom=GetEdgeBackgroundCensus(edge_image,edge_view,
322 SouthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
323 vertex.bottom,exception);
324 continue;
325 }
326 if (fabs(edge.right-background_census) < MagickEpsilon)
327 {
328 /*
329 Trim right edge.
330 */
331 vertex.right++;
332 bounds.width--;
333 edge.right=GetEdgeBackgroundCensus(edge_image,edge_view,
334 NorthEastGravity,1,bounds.height,(ssize_t) vertex.right,(ssize_t)
335 vertex.top,exception);
336 edge.top=GetEdgeBackgroundCensus(edge_image,edge_view,
337 NorthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
338 vertex.top,exception);
339 edge.bottom=GetEdgeBackgroundCensus(edge_image,edge_view,
340 SouthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
341 vertex.bottom,exception);
342 continue;
343 }
344 if (fabs(edge.top-background_census) < MagickEpsilon)
345 {
346 /*
347 Trim top edge.
348 */
349 vertex.top++;
350 bounds.height--;
351 edge.left=GetEdgeBackgroundCensus(edge_image,edge_view,
352 NorthWestGravity,1,bounds.height,(ssize_t) vertex.left,(ssize_t)
353 vertex.top,exception);
354 edge.right=GetEdgeBackgroundCensus(edge_image,edge_view,
355 NorthEastGravity,1,bounds.height,(ssize_t) vertex.right,(ssize_t)
356 vertex.top,exception);
357 edge.top=GetEdgeBackgroundCensus(edge_image,edge_view,
358 NorthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
359 vertex.top,exception);
360 continue;
361 }
362 if (fabs(edge.bottom-background_census) < MagickEpsilon)
363 {
364 /*
365 Trim bottom edge.
366 */
367 vertex.bottom++;
368 bounds.height--;
369 edge.left=GetEdgeBackgroundCensus(edge_image,edge_view,
370 NorthWestGravity,1,bounds.height,(ssize_t) vertex.left,(ssize_t)
371 vertex.top,exception);
372 edge.right=GetEdgeBackgroundCensus(edge_image,edge_view,
373 NorthEastGravity,1,bounds.height,(ssize_t) vertex.right,(ssize_t)
374 vertex.top,exception);
375 edge.bottom=GetEdgeBackgroundCensus(edge_image,edge_view,
376 SouthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
377 vertex.bottom,exception);
378 continue;
379 }
380 }
381 edge_view=DestroyCacheView(edge_view);
382 edge_image=DestroyImage(edge_image);
383 bounds.x=(ssize_t) vertex.left;
384 bounds.y=(ssize_t) vertex.top;
385 if ((bounds.width == 0) || (bounds.height == 0))
386 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
387 "GeometryDoesNotContainImage","`%s'",image->filename);
388 return(bounds);
389}
390
391MagickExport RectangleInfo GetImageBoundingBox(const Image *image,
392 ExceptionInfo *exception)
393{
395 *image_view;
396
397 const char
398 *artifact;
399
400 const Quantum
401 *p;
402
403 MagickBooleanType
404 status;
405
407 target[4],
408 zero;
409
411 bounds;
412
413 ssize_t
414 y;
415
416 assert(image != (Image *) NULL);
417 assert(image->signature == MagickCoreSignature);
418 if (IsEventLogging() != MagickFalse)
419 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
420 artifact=GetImageArtifact(image,"trim:percent-background");
421 if (artifact != (const char *) NULL)
422 return(GetEdgeBoundingBox(image,exception));
423 artifact=GetImageArtifact(image,"trim:edges");
424 if (artifact == (const char *) NULL)
425 {
426 bounds.width=(size_t) (image->columns == 1 ? 1 : 0);
427 bounds.height=(size_t) (image->rows == 1 ? 1 : 0);
428 bounds.x=(ssize_t) image->columns;
429 bounds.y=(ssize_t) image->rows;
430 }
431 else
432 {
433 char
434 *edges,
435 *q,
436 *r;
437
438 bounds.width=(size_t) image->columns;
439 bounds.height=(size_t) image->rows;
440 bounds.x=0;
441 bounds.y=0;
442 edges=AcquireString(artifact);
443 r=edges;
444 while ((q=StringToken(",",&r)) != (char *) NULL)
445 {
446 if (LocaleCompare(q,"north") == 0)
447 bounds.y=(ssize_t) image->rows;
448 if (LocaleCompare(q,"east") == 0)
449 bounds.width=0;
450 if (LocaleCompare(q,"south") == 0)
451 bounds.height=0;
452 if (LocaleCompare(q,"west") == 0)
453 bounds.x=(ssize_t) image->columns;
454 }
455 edges=DestroyString(edges);
456 }
457 GetPixelInfo(image,&target[0]);
458 image_view=AcquireVirtualCacheView(image,exception);
459 p=GetCacheViewVirtualPixels(image_view,0,0,1,1,exception);
460 if (p == (const Quantum *) NULL)
461 {
462 image_view=DestroyCacheView(image_view);
463 return(bounds);
464 }
465 GetPixelInfoPixel(image,p,&target[0]);
466 GetPixelInfo(image,&target[1]);
467 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,0,1,1,
468 exception);
469 if (p != (const Quantum *) NULL)
470 GetPixelInfoPixel(image,p,&target[1]);
471 GetPixelInfo(image,&target[2]);
472 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-1,1,1,
473 exception);
474 if (p != (const Quantum *) NULL)
475 GetPixelInfoPixel(image,p,&target[2]);
476 GetPixelInfo(image,&target[3]);
477 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,(ssize_t)
478 image->rows-1,1,1,exception);
479 if (p != (const Quantum *) NULL)
480 GetPixelInfoPixel(image,p,&target[3]);
481 status=MagickTrue;
482 GetPixelInfo(image,&zero);
483#if defined(MAGICKCORE_OPENMP_SUPPORT)
484 #pragma omp parallel for schedule(static) shared(status) \
485 magick_number_threads(image,image,image->rows,2)
486#endif
487 for (y=0; y < (ssize_t) image->rows; y++)
488 {
489 const Quantum
490 *magick_restrict q;
491
493 pixel;
494
496 bounding_box;
497
498 ssize_t
499 x;
500
501 if (status == MagickFalse)
502 continue;
503#if defined(MAGICKCORE_OPENMP_SUPPORT)
504# pragma omp critical (MagickCore_GetImageBoundingBox)
505#endif
506 bounding_box=bounds;
507 q=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
508 if (q == (const Quantum *) NULL)
509 {
510 status=MagickFalse;
511 continue;
512 }
513 pixel=zero;
514 for (x=0; x < (ssize_t) image->columns; x++)
515 {
516 GetPixelInfoPixel(image,q,&pixel);
517 if ((x < bounding_box.x) &&
518 (IsFuzzyEquivalencePixelInfo(&pixel,&target[0]) == MagickFalse))
519 bounding_box.x=x;
520 if ((x > (ssize_t) bounding_box.width) &&
521 (IsFuzzyEquivalencePixelInfo(&pixel,&target[1]) == MagickFalse))
522 bounding_box.width=(size_t) x;
523 if ((y < bounding_box.y) &&
524 (IsFuzzyEquivalencePixelInfo(&pixel,&target[0]) == MagickFalse))
525 bounding_box.y=y;
526 if ((y > (ssize_t) bounding_box.height) &&
527 (IsFuzzyEquivalencePixelInfo(&pixel,&target[2]) == MagickFalse))
528 bounding_box.height=(size_t) y;
529 if ((x < (ssize_t) bounding_box.width) &&
530 (y > (ssize_t) bounding_box.height) &&
531 (IsFuzzyEquivalencePixelInfo(&pixel,&target[3]) == MagickFalse))
532 {
533 bounding_box.width=(size_t) x;
534 bounding_box.height=(size_t) y;
535 }
536 q+=GetPixelChannels(image);
537 }
538#if defined(MAGICKCORE_OPENMP_SUPPORT)
539# pragma omp critical (MagickCore_GetImageBoundingBox)
540#endif
541 {
542 if (bounding_box.x < bounds.x)
543 bounds.x=bounding_box.x;
544 if (bounding_box.y < bounds.y)
545 bounds.y=bounding_box.y;
546 if (bounding_box.width > bounds.width)
547 bounds.width=bounding_box.width;
548 if (bounding_box.height > bounds.height)
549 bounds.height=bounding_box.height;
550 }
551 }
552 image_view=DestroyCacheView(image_view);
553 if ((bounds.width == 0) || (bounds.height == 0))
554 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
555 "GeometryDoesNotContainImage","`%s'",image->filename);
556 else
557 {
558 bounds.width-=(size_t) (bounds.x-1);
559 bounds.height-=(size_t) (bounds.y-1);
560 }
561 return(bounds);
562}
563
564/*
565%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566% %
567% %
568% %
569% G e t I m a g e C o n v e x H u l l %
570% %
571% %
572% %
573%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574%
575% GetImageConvexHull() returns the convex hull points of an image canvas.
576%
577% The format of the GetImageConvexHull method is:
578%
579% PointInfo *GetImageConvexHull(const Image *image,
580% size_t number_vertices,ExceptionInfo *exception)
581%
582% A description of each parameter follows:
583%
584% o image: the image.
585%
586% o number_vertices: the number of vertices in the convex hull.
587%
588% o exception: return any errors or warnings in this structure.
589%
590*/
591
592static double LexicographicalOrder(PointInfo *a,PointInfo *b,PointInfo *c)
593{
594 /*
595 Order by x-coordinate, and in case of a tie, by y-coordinate.
596 */
597 return((b->x-a->x)*(c->y-a->y)-(b->y-a->y)*(c->x-a->x));
598}
599
600static PixelInfo GetEdgeBackgroundColor(const Image *image,
601 const CacheView *image_view,ExceptionInfo *exception)
602{
603 const char
604 *artifact;
605
606 double
607 census[4],
608 edge_census;
609
611 background[4],
612 edge_background;
613
614 ssize_t
615 i;
616
617 /*
618 Most dominant color of edges/corners is the background color of the image.
619 */
620 memset(&edge_background,0,sizeof(edge_background));
621 artifact=GetImageArtifact(image,"convex-hull:background-color");
622 if (artifact == (const char *) NULL)
623 artifact=GetImageArtifact(image,"background");
624#if defined(MAGICKCORE_OPENMP_SUPPORT)
625 #pragma omp parallel for schedule(static)
626#endif
627 for (i=0; i < 4; i++)
628 {
630 *edge_view;
631
632 GravityType
633 gravity;
634
635 Image
636 *edge_image;
637
639 pixel;
640
642 edge_geometry;
643
644 const Quantum
645 *p;
646
647 ssize_t
648 y;
649
650 census[i]=0.0;
651 (void) memset(&edge_geometry,0,sizeof(edge_geometry));
652 switch (i)
653 {
654 case 0:
655 default:
656 {
657 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-1,1,1,
658 exception);
659 gravity=WestGravity;
660 edge_geometry.width=1;
661 edge_geometry.height=0;
662 break;
663 }
664 case 1:
665 {
666 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,0,1,1,
667 exception);
668 gravity=EastGravity;
669 edge_geometry.width=1;
670 edge_geometry.height=0;
671 break;
672 }
673 case 2:
674 {
675 p=GetCacheViewVirtualPixels(image_view,0,0,1,1,exception);
676 gravity=NorthGravity;
677 edge_geometry.width=0;
678 edge_geometry.height=1;
679 break;
680 }
681 case 3:
682 {
683 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,
684 (ssize_t) image->rows-1,1,1,exception);
685 gravity=SouthGravity;
686 edge_geometry.width=0;
687 edge_geometry.height=1;
688 break;
689 }
690 }
691 GetPixelInfoPixel(image,p,background+i);
692 if (artifact != (const char *) NULL)
693 (void) QueryColorCompliance(artifact,AllCompliance,background+i,
694 exception);
695 GravityAdjustGeometry(image->columns,image->rows,gravity,&edge_geometry);
696 edge_image=CropImage(image,&edge_geometry,exception);
697 if (edge_image == (Image *) NULL)
698 continue;
699 edge_view=AcquireVirtualCacheView(edge_image,exception);
700 for (y=0; y < (ssize_t) edge_image->rows; y++)
701 {
702 ssize_t
703 x;
704
705 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,
706 exception);
707 if (p == (const Quantum *) NULL)
708 break;
709 for (x=0; x < (ssize_t) edge_image->columns; x++)
710 {
711 GetPixelInfoPixel(edge_image,p,&pixel);
712 if (IsFuzzyEquivalencePixelInfo(&pixel,background+i) == MagickFalse)
713 census[i]++;
714 p+=GetPixelChannels(edge_image);
715 }
716 }
717 edge_view=DestroyCacheView(edge_view);
718 edge_image=DestroyImage(edge_image);
719 }
720 edge_census=(-1.0);
721 for (i=0; i < 4; i++)
722 if (census[i] > edge_census)
723 {
724 edge_background=background[i];
725 edge_census=census[i];
726 }
727 return(edge_background);
728}
729
730void TraceConvexHull(PointInfo *vertices,size_t number_vertices,
731 PointInfo ***monotone_chain,size_t *chain_length)
732{
734 **chain;
735
736 size_t
737 demark,
738 n;
739
740 ssize_t
741 i;
742
743 /*
744 Construct the upper and lower hulls: rightmost to leftmost counterclockwise.
745 */
746 chain=(*monotone_chain);
747 n=0;
748 for (i=0; i < (ssize_t) number_vertices; i++)
749 {
750 while ((n >= 2) &&
751 (LexicographicalOrder(chain[n-2],chain[n-1],&vertices[i]) <= 0.0))
752 n--;
753 chain[n++]=(&vertices[i]);
754 }
755 demark=n+1;
756 for (i=(ssize_t) number_vertices-2; i >= 0; i--)
757 {
758 while ((n >= demark) &&
759 (LexicographicalOrder(chain[n-2],chain[n-1],&vertices[i]) <= 0.0))
760 n--;
761 chain[n++]=(&vertices[i]);
762 }
763 *chain_length=n;
764}
765
766MagickExport PointInfo *GetImageConvexHull(const Image *image,
767 size_t *number_vertices,ExceptionInfo *exception)
768{
770 *image_view;
771
772 MagickBooleanType
773 status;
774
776 *monotone_info,
777 *vertices_info;
778
780 background;
781
783 *convex_hull,
784 **monotone_chain,
785 *vertices;
786
787 size_t
788 n;
789
790 ssize_t
791 y;
792
793 /*
794 Identify convex hull vertices of image foreground object(s).
795 */
796 assert(image != (Image *) NULL);
797 assert(image->signature == MagickCoreSignature);
798 if (IsEventLogging() != MagickFalse)
799 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
800 *number_vertices=0;
801 vertices_info=AcquireVirtualMemory(image->columns,image->rows*
802 sizeof(*vertices));
803 monotone_info=AcquireVirtualMemory(2*image->columns,2*
804 image->rows*sizeof(*monotone_chain));
805 if ((vertices_info == (MemoryInfo *) NULL) ||
806 (monotone_info == (MemoryInfo *) NULL))
807 {
808 if (monotone_info != (MemoryInfo *) NULL)
809 monotone_info=(MemoryInfo *) RelinquishVirtualMemory(monotone_info);
810 if (vertices_info != (MemoryInfo *) NULL)
811 vertices_info=RelinquishVirtualMemory(vertices_info);
812 return((PointInfo *) NULL);
813 }
814 vertices=(PointInfo *) GetVirtualMemoryBlob(vertices_info);
815 monotone_chain=(PointInfo **) GetVirtualMemoryBlob(monotone_info);
816 image_view=AcquireVirtualCacheView(image,exception);
817 background=GetEdgeBackgroundColor(image,image_view,exception);
818 status=MagickTrue;
819 n=0;
820 for (y=0; y < (ssize_t) image->rows; y++)
821 {
822 const Quantum
823 *p;
824
825 ssize_t
826 x;
827
828 if (status == MagickFalse)
829 continue;
830 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
831 if (p == (const Quantum *) NULL)
832 {
833 status=MagickFalse;
834 continue;
835 }
836 for (x=0; x < (ssize_t) image->columns; x++)
837 {
839 pixel;
840
841 GetPixelInfoPixel(image,p,&pixel);
842 if (IsFuzzyEquivalencePixelInfo(&pixel,&background) == MagickFalse)
843 {
844 vertices[n].x=(double) x;
845 vertices[n].y=(double) y;
846 n++;
847 }
848 p+=GetPixelChannels(image);
849 }
850 }
851 image_view=DestroyCacheView(image_view);
852 /*
853 Return the convex hull of the image foreground object(s).
854 */
855 TraceConvexHull(vertices,n,&monotone_chain,number_vertices);
856 convex_hull=(PointInfo *) AcquireQuantumMemory(*number_vertices,
857 sizeof(*convex_hull));
858 if (convex_hull != (PointInfo *) NULL)
859 for (n=0; n < *number_vertices; n++)
860 convex_hull[n]=(*monotone_chain[n]);
861 monotone_info=RelinquishVirtualMemory(monotone_info);
862 vertices_info=RelinquishVirtualMemory(vertices_info);
863 return(convex_hull);
864}
865
866/*
867%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
868% %
869% %
870% %
871% G e t I m a g e D e p t h %
872% %
873% %
874% %
875%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876%
877% GetImageDepth() returns the depth of a particular image channel.
878%
879% The format of the GetImageDepth method is:
880%
881% size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
882%
883% A description of each parameter follows:
884%
885% o image: the image.
886%
887% o exception: return any errors or warnings in this structure.
888%
889*/
890MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
891{
893 *image_view;
894
895 MagickBooleanType
896 status;
897
898 ssize_t
899 i;
900
901 size_t
902 *current_depth,
903 depth,
904 number_threads;
905
906 ssize_t
907 y;
908
909 /*
910 Compute image depth.
911 */
912 assert(image != (Image *) NULL);
913 assert(image->signature == MagickCoreSignature);
914 if (IsEventLogging() != MagickFalse)
915 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
916 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
917 current_depth=(size_t *) AcquireQuantumMemory(number_threads,
918 sizeof(*current_depth));
919 if (current_depth == (size_t *) NULL)
920 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
921 status=MagickTrue;
922 for (i=0; i < (ssize_t) number_threads; i++)
923 current_depth[i]=1;
924 if ((image->storage_class == PseudoClass) &&
925 ((image->alpha_trait & BlendPixelTrait) == 0))
926 {
927 for (i=0; i < (ssize_t) image->colors; i++)
928 {
929 const int
930 id = GetOpenMPThreadId();
931
932 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
933 {
934 MagickBooleanType
935 atDepth;
936
937 QuantumAny
938 range;
939
940 atDepth=MagickTrue;
941 range=GetQuantumRange(current_depth[id]);
942 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
943 if (IsPixelAtDepth(ClampToQuantum(image->colormap[i].red),range) == MagickFalse)
944 atDepth=MagickFalse;
945 if ((atDepth != MagickFalse) &&
946 (GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
947 if (IsPixelAtDepth(ClampToQuantum(image->colormap[i].green),range) == MagickFalse)
948 atDepth=MagickFalse;
949 if ((atDepth != MagickFalse) &&
950 (GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
951 if (IsPixelAtDepth(ClampToQuantum(image->colormap[i].blue),range) == MagickFalse)
952 atDepth=MagickFalse;
953 if ((atDepth != MagickFalse))
954 break;
955 current_depth[id]++;
956 }
957 }
958 depth=current_depth[0];
959 for (i=1; i < (ssize_t) number_threads; i++)
960 if (depth < current_depth[i])
961 depth=current_depth[i];
962 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
963 return(depth);
964 }
965 image_view=AcquireVirtualCacheView(image,exception);
966#if !defined(MAGICKCORE_HDRI_SUPPORT)
967 DisableMSCWarning(4127)
968 if ((1UL*QuantumRange) <= MaxMap)
969 RestoreMSCWarning
970 {
971 size_t
972 *depth_map;
973
974 /*
975 Scale pixels to desired (optimized with depth map).
976 */
977 depth_map=(size_t *) AcquireQuantumMemory(MaxMap+1,sizeof(*depth_map));
978 if (depth_map == (size_t *) NULL)
979 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
980 for (i=0; i <= (ssize_t) MaxMap; i++)
981 {
982 for (depth=1; depth < (size_t) MAGICKCORE_QUANTUM_DEPTH; depth++)
983 {
984 Quantum
985 pixel;
986
987 QuantumAny
988 range;
989
990 range=GetQuantumRange(depth);
991 pixel=(Quantum) i;
992 if (pixel == ScaleAnyToQuantum(ScaleQuantumToAny(pixel,range),range))
993 break;
994 }
995 depth_map[i]=depth;
996 }
997#if defined(MAGICKCORE_OPENMP_SUPPORT)
998 #pragma omp parallel for schedule(static) shared(status) \
999 magick_number_threads(image,image,image->rows,1)
1000#endif
1001 for (y=0; y < (ssize_t) image->rows; y++)
1002 {
1003 const int
1004 id = GetOpenMPThreadId();
1005
1006 const Quantum
1007 *magick_restrict p;
1008
1009 ssize_t
1010 x;
1011
1012 if (status == MagickFalse)
1013 continue;
1014 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1015 if (p == (const Quantum *) NULL)
1016 continue;
1017 for (x=0; x < (ssize_t) image->columns; x++)
1018 {
1019 ssize_t
1020 j;
1021
1022 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1023 {
1024 PixelChannel channel = GetPixelChannelChannel(image,j);
1025 PixelTrait traits = GetPixelChannelTraits(image,channel);
1026 if ((traits & UpdatePixelTrait) == 0)
1027 continue;
1028 if (depth_map[ScaleQuantumToMap(p[j])] > current_depth[id])
1029 current_depth[id]=depth_map[ScaleQuantumToMap(p[j])];
1030 }
1031 p+=GetPixelChannels(image);
1032 }
1033 if (current_depth[id] == MAGICKCORE_QUANTUM_DEPTH)
1034 status=MagickFalse;
1035 }
1036 image_view=DestroyCacheView(image_view);
1037 depth=current_depth[0];
1038 for (i=1; i < (ssize_t) number_threads; i++)
1039 if (depth < current_depth[i])
1040 depth=current_depth[i];
1041 depth_map=(size_t *) RelinquishMagickMemory(depth_map);
1042 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
1043 return(depth);
1044 }
1045#endif
1046 /*
1047 Compute pixel depth.
1048 */
1049#if defined(MAGICKCORE_OPENMP_SUPPORT)
1050 #pragma omp parallel for schedule(static) shared(status) \
1051 magick_number_threads(image,image,image->rows,1)
1052#endif
1053 for (y=0; y < (ssize_t) image->rows; y++)
1054 {
1055 const int
1056 id = GetOpenMPThreadId();
1057
1058 const Quantum
1059 *magick_restrict p;
1060
1061 ssize_t
1062 x;
1063
1064 if (status == MagickFalse)
1065 continue;
1066 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1067 if (p == (const Quantum *) NULL)
1068 continue;
1069 for (x=0; x < (ssize_t) image->columns; x++)
1070 {
1071 ssize_t
1072 j;
1073
1074 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1075 {
1076 PixelChannel
1077 channel;
1078
1079 PixelTrait
1080 traits;
1081
1082 channel=GetPixelChannelChannel(image,j);
1083 traits=GetPixelChannelTraits(image,channel);
1084 if ((traits & UpdatePixelTrait) == 0)
1085 continue;
1086 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
1087 {
1088 QuantumAny
1089 range;
1090
1091 range=GetQuantumRange(current_depth[id]);
1092 if (p[j] == ScaleAnyToQuantum(ScaleQuantumToAny(p[j],range),range))
1093 break;
1094 current_depth[id]++;
1095 }
1096 }
1097 p+=GetPixelChannels(image);
1098 }
1099 if (current_depth[id] == MAGICKCORE_QUANTUM_DEPTH)
1100 status=MagickFalse;
1101 }
1102 image_view=DestroyCacheView(image_view);
1103 depth=current_depth[0];
1104 for (i=1; i < (ssize_t) number_threads; i++)
1105 if (depth < current_depth[i])
1106 depth=current_depth[i];
1107 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
1108 return(depth);
1109}
1110
1111/*
1112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1113% %
1114% %
1115% %
1116% G e t I m a g e M i n i m u m B o u n d i n g B o x %
1117% %
1118% %
1119% %
1120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1121%
1122% GetImageMinimumBoundingBox() returns the points that form the minimum
1123% bounding box around the image foreground objects with the "Rotating
1124% Calipers" algorithm. The method also returns these properties:
1125% minimum-bounding-box:area, minimum-bounding-box:width,
1126% minimum-bounding-box:height, and minimum-bounding-box:angle.
1127%
1128% The format of the GetImageMinimumBoundingBox method is:
1129%
1130% PointInfo *GetImageMinimumBoundingBox(Image *image,
1131% size_t number_vertices,ExceptionInfo *exception)
1132%
1133% A description of each parameter follows:
1134%
1135% o image: the image.
1136%
1137% o number_vertices: the number of vertices in the bounding box.
1138%
1139% o exception: return any errors or warnings in this structure.
1140%
1141*/
1142
1143typedef struct _CaliperInfo
1144{
1145 double
1146 area,
1147 width,
1148 height,
1149 projection;
1150
1151 ssize_t
1152 p,
1153 q,
1154 v;
1155} CaliperInfo;
1156
1157static inline double getAngle(PointInfo *p,PointInfo *q)
1158{
1159 /*
1160 Get the angle between line (p,q) and horizontal axis, in degrees.
1161 */
1162 return(RadiansToDegrees(atan2(q->y-p->y,q->x-p->x)));
1163}
1164
1165static inline double getDistance(PointInfo *p,PointInfo *q)
1166{
1167 double
1168 distance;
1169
1170 distance=hypot(p->x-q->x,p->y-q->y);
1171 return(distance*distance);
1172}
1173
1174static inline double getProjection(PointInfo *p,PointInfo *q,PointInfo *v)
1175{
1176 double
1177 distance;
1178
1179 /*
1180 Projection of vector (x,y) - p into a line passing through p and q.
1181 */
1182 distance=getDistance(p,q);
1183 if (distance < MagickEpsilon)
1184 return(INFINITY);
1185 return((q->x-p->x)*(v->x-p->x)+(v->y-p->y)*(q->y-p->y))/sqrt(distance);
1186}
1187
1188static inline double getFeretDiameter(PointInfo *p,PointInfo *q,PointInfo *v)
1189{
1190 double
1191 distance;
1192
1193 /*
1194 Distance from a point (x,y) to a line passing through p and q.
1195 */
1196 distance=getDistance(p,q);
1197 if (distance < MagickEpsilon)
1198 return(INFINITY);
1199 return((q->x-p->x)*(v->y-p->y)-(v->x-p->x)*(q->y-p->y))/sqrt(distance);
1200}
1201
1202MagickExport PointInfo *GetImageMinimumBoundingBox(Image *image,
1203 size_t *number_vertices,ExceptionInfo *exception)
1204{
1206 caliper_info;
1207
1208 const char
1209 *artifact;
1210
1211 double
1212 angle,
1213 diameter,
1214 distance;
1215
1216 PointInfo
1217 *bounding_box,
1218 *vertices;
1219
1220 size_t
1221 number_hull_vertices;
1222
1223 ssize_t
1224 i;
1225
1226 /*
1227 Generate the minimum bounding box with the "Rotating Calipers" algorithm.
1228 */
1229 assert(image != (Image *) NULL);
1230 assert(image->signature == MagickCoreSignature);
1231 if (IsEventLogging() != MagickFalse)
1232 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1233 *number_vertices=0;
1234 vertices=GetImageConvexHull(image,&number_hull_vertices,exception);
1235 if (vertices == (PointInfo *) NULL)
1236 return((PointInfo *) NULL);
1237 *number_vertices=4;
1238 bounding_box=(PointInfo *) AcquireQuantumMemory(*number_vertices,
1239 sizeof(*bounding_box));
1240 if (bounding_box == (PointInfo *) NULL)
1241 {
1242 vertices=(PointInfo *) RelinquishMagickMemory(vertices);
1243 return((PointInfo *) NULL);
1244 }
1245 caliper_info.area=2.0*image->columns*image->rows;
1246 caliper_info.width=(double) image->columns+image->rows;
1247 caliper_info.height=0.0;
1248 caliper_info.projection=0.0;
1249 caliper_info.p=(-1);
1250 caliper_info.q=(-1);
1251 caliper_info.v=(-1);
1252 for (i=0; i < (ssize_t) number_hull_vertices; i++)
1253 {
1254 double
1255 area = 0.0,
1256 max_projection = 0.0,
1257 min_diameter = -1.0,
1258 min_projection = 0.0;
1259
1260 ssize_t
1261 j,
1262 k;
1263
1264 ssize_t
1265 p = -1,
1266 q = -1,
1267 v = -1;
1268
1269 for (j=0; j < (ssize_t) number_hull_vertices; j++)
1270 {
1271 diameter=fabs(getFeretDiameter(&vertices[i],
1272 &vertices[(i+1) % (ssize_t) number_hull_vertices],&vertices[j]));
1273 if (min_diameter < diameter)
1274 {
1275 min_diameter=diameter;
1276 p=i;
1277 q=(i+1) % (ssize_t) number_hull_vertices;
1278 v=j;
1279 }
1280 }
1281 for (k=0; k < (ssize_t) number_hull_vertices; k++)
1282 {
1283 double
1284 projection;
1285
1286 /*
1287 Rotating calipers.
1288 */
1289 projection=getProjection(&vertices[p],&vertices[q],&vertices[k]);
1290 min_projection=MagickMin(min_projection,projection);
1291 max_projection=MagickMax(max_projection,projection);
1292 }
1293 area=min_diameter*(max_projection-min_projection);
1294 if (caliper_info.area > area)
1295 {
1296 caliper_info.area=area;
1297 caliper_info.width=min_diameter;
1298 caliper_info.height=max_projection-min_projection;
1299 caliper_info.projection=max_projection;
1300 caliper_info.p=p;
1301 caliper_info.q=q;
1302 caliper_info.v=v;
1303 }
1304 }
1305 /*
1306 Initialize minimum bounding box.
1307 */
1308 diameter=getFeretDiameter(&vertices[caliper_info.p],
1309 &vertices[caliper_info.q],&vertices[caliper_info.v]);
1310 angle=atan2(vertices[caliper_info.q].y-vertices[caliper_info.p].y,
1311 vertices[caliper_info.q].x-vertices[caliper_info.p].x);
1312 bounding_box[0].x=vertices[caliper_info.p].x+cos(angle)*
1313 caliper_info.projection;
1314 bounding_box[0].y=vertices[caliper_info.p].y+sin(angle)*
1315 caliper_info.projection;
1316 bounding_box[1].x=floor(bounding_box[0].x+cos(angle+MagickPI/2.0)*diameter+
1317 0.5);
1318 bounding_box[1].y=floor(bounding_box[0].y+sin(angle+MagickPI/2.0)*diameter+
1319 0.5);
1320 bounding_box[2].x=floor(bounding_box[1].x+cos(angle)*(-caliper_info.height)+
1321 0.5);
1322 bounding_box[2].y=floor(bounding_box[1].y+sin(angle)*(-caliper_info.height)+
1323 0.5);
1324 bounding_box[3].x=floor(bounding_box[2].x+cos(angle+MagickPI/2.0)*(-diameter)+
1325 0.5);
1326 bounding_box[3].y=floor(bounding_box[2].y+sin(angle+MagickPI/2.0)*(-diameter)+
1327 0.5);
1328 /*
1329 Export minimum bounding box properties.
1330 */
1331 (void) FormatImageProperty(image,"minimum-bounding-box:area","%.*g",
1332 GetMagickPrecision(),caliper_info.area);
1333 (void) FormatImageProperty(image,"minimum-bounding-box:width","%.*g",
1334 GetMagickPrecision(),caliper_info.width);
1335 (void) FormatImageProperty(image,"minimum-bounding-box:height","%.*g",
1336 GetMagickPrecision(),caliper_info.height);
1337 (void) FormatImageProperty(image,"minimum-bounding-box:_p","%.*g,%.*g",
1338 GetMagickPrecision(),vertices[caliper_info.p].x,
1339 GetMagickPrecision(),vertices[caliper_info.p].y);
1340 (void) FormatImageProperty(image,"minimum-bounding-box:_q","%.*g,%.*g",
1341 GetMagickPrecision(),vertices[caliper_info.q].x,
1342 GetMagickPrecision(),vertices[caliper_info.q].y);
1343 (void) FormatImageProperty(image,"minimum-bounding-box:_v","%.*g,%.*g",
1344 GetMagickPrecision(),vertices[caliper_info.v].x,
1345 GetMagickPrecision(),vertices[caliper_info.v].y);
1346 /*
1347 Find smallest angle to origin.
1348 */
1349 distance=hypot(bounding_box[0].x,bounding_box[0].y);
1350 angle=getAngle(&bounding_box[0],&bounding_box[1]);
1351 for (i=1; i < 4; i++)
1352 {
1353 double d = hypot(bounding_box[i].x,bounding_box[i].y);
1354 if (d < distance)
1355 {
1356 distance=d;
1357 angle=getAngle(&bounding_box[i],&bounding_box[(i+1) % 4]);
1358 }
1359 }
1360 artifact=GetImageArtifact(image,"minimum-bounding-box:orientation");
1361 if (artifact != (const char *) NULL)
1362 {
1363 double
1364 length,
1365 q_length,
1366 p_length;
1367
1368 PointInfo
1369 delta,
1370 point;
1371
1372 /*
1373 Find smallest perpendicular distance from edge to origin.
1374 */
1375 point=bounding_box[0];
1376 for (i=1; i < 4; i++)
1377 {
1378 if (bounding_box[i].x < point.x)
1379 point.x=bounding_box[i].x;
1380 if (bounding_box[i].y < point.y)
1381 point.y=bounding_box[i].y;
1382 }
1383 for (i=0; i < 4; i++)
1384 {
1385 bounding_box[i].x-=point.x;
1386 bounding_box[i].y-=point.y;
1387 }
1388 for (i=0; i < 4; i++)
1389 {
1390 double
1391 d,
1392 intercept,
1393 slope;
1394
1395 delta.x=bounding_box[(i+1) % 4].x-bounding_box[i].x;
1396 delta.y=bounding_box[(i+1) % 4].y-bounding_box[i].y;
1397 slope=delta.y*PerceptibleReciprocal(delta.x);
1398 intercept=bounding_box[(i+1) % 4].y-slope*bounding_box[i].x;
1399 d=fabs((slope*bounding_box[i].x-bounding_box[i].y+intercept)*
1400 PerceptibleReciprocal(sqrt(slope*slope+1.0)));
1401 if ((i == 0) || (d < distance))
1402 {
1403 distance=d;
1404 point=delta;
1405 }
1406 }
1407 angle=RadiansToDegrees(atan(point.y*PerceptibleReciprocal(point.x)));
1408 length=hypot(point.x,point.y);
1409 p_length=fabs((double) MagickMax(caliper_info.width,caliper_info.height)-
1410 length);
1411 q_length=fabs(length-(double) MagickMin(caliper_info.width,
1412 caliper_info.height));
1413 if (LocaleCompare(artifact,"landscape") == 0)
1414 {
1415 if (p_length > q_length)
1416 angle+=(angle < 0.0) ? 90.0 : -90.0;
1417 }
1418 else
1419 if (LocaleCompare(artifact,"portrait") == 0)
1420 {
1421 if (p_length < q_length)
1422 angle+=(angle >= 0.0) ? 90.0 : -90.0;
1423 }
1424 }
1425 (void) FormatImageProperty(image,"minimum-bounding-box:angle","%.*g",
1426 GetMagickPrecision(),angle);
1427 (void) FormatImageProperty(image,"minimum-bounding-box:unrotate","%.*g",
1428 GetMagickPrecision(),-angle);
1429 vertices=(PointInfo *) RelinquishMagickMemory(vertices);
1430 return(bounding_box);
1431}
1432
1433/*
1434%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1435% %
1436% %
1437% %
1438% G e t I m a g e Q u a n t u m D e p t h %
1439% %
1440% %
1441% %
1442%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1443%
1444% GetImageQuantumDepth() returns the depth of the image rounded to a legal
1445% quantum depth: 8, 16, or 32.
1446%
1447% The format of the GetImageQuantumDepth method is:
1448%
1449% size_t GetImageQuantumDepth(const Image *image,
1450% const MagickBooleanType constrain)
1451%
1452% A description of each parameter follows:
1453%
1454% o image: the image.
1455%
1456% o constrain: A value other than MagickFalse, constrains the depth to
1457% a maximum of MAGICKCORE_QUANTUM_DEPTH.
1458%
1459*/
1460MagickExport size_t GetImageQuantumDepth(const Image *image,
1461 const MagickBooleanType constrain)
1462{
1463 size_t
1464 depth;
1465
1466 depth=image->depth;
1467 if (depth <= 8)
1468 depth=8;
1469 else
1470 if (depth <= 16)
1471 depth=16;
1472 else
1473 if (depth <= 32)
1474 depth=32;
1475 else
1476 if (depth <= 64)
1477 depth=64;
1478 if (constrain != MagickFalse)
1479 depth=(size_t) MagickMin((double) depth,(double) MAGICKCORE_QUANTUM_DEPTH);
1480 return(depth);
1481}
1482
1483/*
1484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485% %
1486% %
1487% %
1488% G e t I m a g e T y p e %
1489% %
1490% %
1491% %
1492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1493%
1494% GetImageType() returns the type of image:
1495%
1496% Bilevel Grayscale GrayscaleMatte
1497% Palette PaletteMatte TrueColor
1498% TrueColorMatte ColorSeparation ColorSeparationMatte
1499%
1500% The format of the GetImageType method is:
1501%
1502% ImageType GetImageType(const Image *image)
1503%
1504% A description of each parameter follows:
1505%
1506% o image: the image.
1507%
1508*/
1509MagickExport ImageType GetImageType(const Image *image)
1510{
1511 assert(image != (Image *) NULL);
1512 assert(image->signature == MagickCoreSignature);
1513 if (image->colorspace == CMYKColorspace)
1514 {
1515 if ((image->alpha_trait & BlendPixelTrait) == 0)
1516 return(ColorSeparationType);
1517 return(ColorSeparationAlphaType);
1518 }
1519 if (IsImageMonochrome(image) != MagickFalse)
1520 return(BilevelType);
1521 if (IsImageGray(image) != MagickFalse)
1522 {
1523 if (image->alpha_trait != UndefinedPixelTrait)
1524 return(GrayscaleAlphaType);
1525 return(GrayscaleType);
1526 }
1527 if (IsPaletteImage(image) != MagickFalse)
1528 {
1529 if (image->alpha_trait != UndefinedPixelTrait)
1530 return(PaletteAlphaType);
1531 return(PaletteType);
1532 }
1533 if (image->alpha_trait != UndefinedPixelTrait)
1534 return(TrueColorAlphaType);
1535 return(TrueColorType);
1536}
1537
1538/*
1539%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1540% %
1541% %
1542% %
1543% I d e n t i f y I m a g e G r a y %
1544% %
1545% %
1546% %
1547%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1548%
1549% IdentifyImageGray() returns grayscale if all the pixels in the image have
1550% the same red, green, and blue intensities, and bi-level is the intensity is
1551% either 0 or QuantumRange. Otherwise undefined is returned.
1552%
1553% The format of the IdentifyImageGray method is:
1554%
1555% ImageType IdentifyImageGray(const Image *image,ExceptionInfo *exception)
1556%
1557% A description of each parameter follows:
1558%
1559% o image: the image.
1560%
1561% o exception: return any errors or warnings in this structure.
1562%
1563*/
1564MagickExport ImageType IdentifyImageGray(const Image *image,
1565 ExceptionInfo *exception)
1566{
1567 CacheView
1568 *image_view;
1569
1570 ImageType
1571 type = BilevelType;
1572
1573 ssize_t
1574 y;
1575
1576 assert(image != (Image *) NULL);
1577 assert(image->signature == MagickCoreSignature);
1578 if (IsEventLogging() != MagickFalse)
1579 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1580 if (IsImageGray(image) != MagickFalse)
1581 return(image->type);
1582 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1583 return(UndefinedType);
1584 image_view=AcquireVirtualCacheView(image,exception);
1585#if defined(MAGICKCORE_OPENMP_SUPPORT)
1586 #pragma omp parallel for schedule(static) shared(type) \
1587 magick_number_threads(image,image,image->rows,2)
1588#endif
1589 for (y=0; y < (ssize_t) image->rows; y++)
1590 {
1591 const Quantum
1592 *p;
1593
1594 ssize_t
1595 x;
1596
1597 if (type == UndefinedType)
1598 continue;
1599 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1600 if (p == (const Quantum *) NULL)
1601 {
1602 type=UndefinedType;
1603 continue;
1604 }
1605 for (x=0; x < (ssize_t) image->columns; x++)
1606 {
1607 if (IsPixelGray(image,p) == MagickFalse)
1608 {
1609 type=UndefinedType;
1610 break;
1611 }
1612 if ((type == BilevelType) && (IsPixelMonochrome(image,p) == MagickFalse))
1613 type=GrayscaleType;
1614 p+=GetPixelChannels(image);
1615 }
1616 }
1617 image_view=DestroyCacheView(image_view);
1618 if ((type == GrayscaleType) && (image->alpha_trait != UndefinedPixelTrait))
1619 type=GrayscaleAlphaType;
1620 return(type);
1621}
1622
1623/*
1624%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1625% %
1626% %
1627% %
1628% I d e n t i f y I m a g e M o n o c h r o m e %
1629% %
1630% %
1631% %
1632%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1633%
1634% IdentifyImageMonochrome() returns MagickTrue if all the pixels in the image
1635% have the same red, green, and blue intensities and the intensity is either
1636% 0 or QuantumRange.
1637%
1638% The format of the IdentifyImageMonochrome method is:
1639%
1640% MagickBooleanType IdentifyImageMonochrome(const Image *image,
1641% ExceptionInfo *exception)
1642%
1643% A description of each parameter follows:
1644%
1645% o image: the image.
1646%
1647% o exception: return any errors or warnings in this structure.
1648%
1649*/
1650MagickExport MagickBooleanType IdentifyImageMonochrome(const Image *image,
1651 ExceptionInfo *exception)
1652{
1653 CacheView
1654 *image_view;
1655
1656 ImageType
1657 type = BilevelType;
1658
1659 ssize_t
1660 y;
1661
1662 assert(image != (Image *) NULL);
1663 assert(image->signature == MagickCoreSignature);
1664 if (IsEventLogging() != MagickFalse)
1665 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1666 if (image->type == BilevelType)
1667 return(MagickTrue);
1668 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1669 return(MagickFalse);
1670 image_view=AcquireVirtualCacheView(image,exception);
1671#if defined(MAGICKCORE_OPENMP_SUPPORT)
1672 #pragma omp parallel for schedule(static) shared(type) \
1673 magick_number_threads(image,image,image->rows,2)
1674#endif
1675 for (y=0; y < (ssize_t) image->rows; y++)
1676 {
1677 const Quantum
1678 *p;
1679
1680 ssize_t
1681 x;
1682
1683 if (type == UndefinedType)
1684 continue;
1685 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1686 if (p == (const Quantum *) NULL)
1687 {
1688 type=UndefinedType;
1689 continue;
1690 }
1691 for (x=0; x < (ssize_t) image->columns; x++)
1692 {
1693 if (IsPixelMonochrome(image,p) == MagickFalse)
1694 {
1695 type=UndefinedType;
1696 break;
1697 }
1698 p+=GetPixelChannels(image);
1699 }
1700 }
1701 image_view=DestroyCacheView(image_view);
1702 return(type == BilevelType ? MagickTrue : MagickFalse);
1703}
1704
1705/*
1706%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1707% %
1708% %
1709% %
1710% I d e n t i f y I m a g e T y p e %
1711% %
1712% %
1713% %
1714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1715%
1716% IdentifyImageType() returns the potential type of image:
1717%
1718% Bilevel Grayscale GrayscaleMatte
1719% Palette PaletteMatte TrueColor
1720% TrueColorMatte ColorSeparation ColorSeparationMatte
1721%
1722% To ensure the image type matches its potential, use SetImageType():
1723%
1724% (void) SetImageType(image,IdentifyImageType(image,exception),exception);
1725%
1726% The format of the IdentifyImageType method is:
1727%
1728% ImageType IdentifyImageType(const Image *image,ExceptionInfo *exception)
1729%
1730% A description of each parameter follows:
1731%
1732% o image: the image.
1733%
1734% o exception: return any errors or warnings in this structure.
1735%
1736*/
1737MagickExport ImageType IdentifyImageType(const Image *image,
1738 ExceptionInfo *exception)
1739{
1740 ImageType
1741 type;
1742
1743 assert(image != (Image *) NULL);
1744 assert(image->signature == MagickCoreSignature);
1745 if (IsEventLogging() != MagickFalse)
1746 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1747 if (image->colorspace == CMYKColorspace)
1748 {
1749 if ((image->alpha_trait & BlendPixelTrait) == 0)
1750 return(ColorSeparationType);
1751 return(ColorSeparationAlphaType);
1752 }
1753 type=IdentifyImageGray(image,exception);
1754 if (IsGrayImageType(type))
1755 return(type);
1756 if (IdentifyPaletteImage(image,exception) != MagickFalse)
1757 {
1758 if (image->alpha_trait != UndefinedPixelTrait)
1759 return(PaletteAlphaType);
1760 return(PaletteType);
1761 }
1762 if (image->alpha_trait != UndefinedPixelTrait)
1763 return(TrueColorAlphaType);
1764 return(TrueColorType);
1765}
1766
1767/*
1768%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1769% %
1770% %
1771% %
1772% I s I m a g e G r a y %
1773% %
1774% %
1775% %
1776%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1777%
1778% IsImageGray() returns MagickTrue if the type of the image is grayscale or
1779% bi-level.
1780%
1781% The format of the IsImageGray method is:
1782%
1783% MagickBooleanType IsImageGray(const Image *image)
1784%
1785% A description of each parameter follows:
1786%
1787% o image: the image.
1788%
1789*/
1790MagickExport MagickBooleanType IsImageGray(const Image *image)
1791{
1792 assert(image != (Image *) NULL);
1793 assert(image->signature == MagickCoreSignature);
1794 if (IsGrayImageType(image->type) != MagickFalse)
1795 return(MagickTrue);
1796 return(MagickFalse);
1797}
1798
1799/*
1800%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1801% %
1802% %
1803% %
1804% I s I m a g e M o n o c h r o m e %
1805% %
1806% %
1807% %
1808%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1809%
1810% IsImageMonochrome() returns MagickTrue if type of the image is bi-level.
1811%
1812% The format of the IsImageMonochrome method is:
1813%
1814% MagickBooleanType IsImageMonochrome(const Image *image)
1815%
1816% A description of each parameter follows:
1817%
1818% o image: the image.
1819%
1820*/
1821MagickExport MagickBooleanType IsImageMonochrome(const Image *image)
1822{
1823 assert(image != (Image *) NULL);
1824 assert(image->signature == MagickCoreSignature);
1825 if (image->type == BilevelType)
1826 return(MagickTrue);
1827 return(MagickFalse);
1828}
1829
1830/*
1831%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1832% %
1833% %
1834% %
1835% I s I m a g e O p a q u e %
1836% %
1837% %
1838% %
1839%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1840%
1841% IsImageOpaque() returns MagickTrue if none of the pixels in the image have
1842% an alpha value other than OpaqueAlpha (QuantumRange).
1843%
1844% Will return true immediately is alpha channel is not available.
1845%
1846% The format of the IsImageOpaque method is:
1847%
1848% MagickBooleanType IsImageOpaque(const Image *image,
1849% ExceptionInfo *exception)
1850%
1851% A description of each parameter follows:
1852%
1853% o image: the image.
1854%
1855% o exception: return any errors or warnings in this structure.
1856%
1857*/
1858MagickExport MagickBooleanType IsImageOpaque(const Image *image,
1859 ExceptionInfo *exception)
1860{
1861 CacheView
1862 *image_view;
1863
1864 MagickBooleanType
1865 opaque = MagickTrue;
1866
1867 ssize_t
1868 y;
1869
1870 /*
1871 Determine if image is opaque.
1872 */
1873 assert(image != (Image *) NULL);
1874 assert(image->signature == MagickCoreSignature);
1875 if (IsEventLogging() != MagickFalse)
1876 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1877 if ((image->alpha_trait & BlendPixelTrait) == 0)
1878 return(MagickTrue);
1879 image_view=AcquireVirtualCacheView(image,exception);
1880#if defined(MAGICKCORE_OPENMP_SUPPORT)
1881 #pragma omp parallel for schedule(static) shared(opaque) \
1882 magick_number_threads(image,image,image->rows,2)
1883#endif
1884 for (y=0; y < (ssize_t) image->rows; y++)
1885 {
1886 const Quantum
1887 *p;
1888
1889 ssize_t
1890 x;
1891
1892 if (opaque == MagickFalse)
1893 continue;
1894 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1895 if (p == (const Quantum *) NULL)
1896 {
1897 opaque=MagickFalse;
1898 continue;
1899 }
1900 for (x=0; x < (ssize_t) image->columns; x++)
1901 {
1902 if (GetPixelAlpha(image,p) != OpaqueAlpha)
1903 {
1904 opaque=MagickFalse;
1905 break;
1906 }
1907 p+=GetPixelChannels(image);
1908 }
1909 }
1910 image_view=DestroyCacheView(image_view);
1911 return(opaque);
1912}
1913
1914/*
1915%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1916% %
1917% %
1918% %
1919% S e t I m a g e D e p t h %
1920% %
1921% %
1922% %
1923%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1924%
1925% SetImageDepth() sets the depth of the image.
1926%
1927% The format of the SetImageDepth method is:
1928%
1929% MagickBooleanType SetImageDepth(Image *image,const size_t depth,
1930% ExceptionInfo *exception)
1931%
1932% A description of each parameter follows:
1933%
1934% o image: the image.
1935%
1936% o channel: the channel.
1937%
1938% o depth: the image depth.
1939%
1940% o exception: return any errors or warnings in this structure.
1941%
1942*/
1943MagickExport MagickBooleanType SetImageDepth(Image *image,
1944 const size_t depth,ExceptionInfo *exception)
1945{
1946 CacheView
1947 *image_view;
1948
1949 MagickBooleanType
1950 status;
1951
1952 QuantumAny
1953 range;
1954
1955 ssize_t
1956 y;
1957
1958 assert(image != (Image *) NULL);
1959 if (IsEventLogging() != MagickFalse)
1960 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1961 assert(image->signature == MagickCoreSignature);
1962 if (depth >= MAGICKCORE_QUANTUM_DEPTH)
1963 {
1964 image->depth=depth;
1965 return(MagickTrue);
1966 }
1967 range=GetQuantumRange(depth);
1968 if (image->storage_class == PseudoClass)
1969 {
1970 ssize_t
1971 i;
1972
1973#if defined(MAGICKCORE_OPENMP_SUPPORT)
1974 #pragma omp parallel for schedule(static) shared(status) \
1975 magick_number_threads(image,image,image->colors,1)
1976#endif
1977 for (i=0; i < (ssize_t) image->colors; i++)
1978 {
1979 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1980 image->colormap[i].red=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
1981 ClampPixel(image->colormap[i].red),range),range);
1982 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1983 image->colormap[i].green=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
1984 ClampPixel(image->colormap[i].green),range),range);
1985 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1986 image->colormap[i].blue=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
1987 ClampPixel(image->colormap[i].blue),range),range);
1988 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1989 image->colormap[i].alpha=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
1990 ClampPixel(image->colormap[i].alpha),range),range);
1991 }
1992 }
1993 status=MagickTrue;
1994 image_view=AcquireAuthenticCacheView(image,exception);
1995#if !defined(MAGICKCORE_HDRI_SUPPORT)
1996 DisableMSCWarning(4127)
1997 if ((1UL*QuantumRange) <= MaxMap)
1998 RestoreMSCWarning
1999 {
2000 Quantum
2001 *depth_map;
2002
2003 ssize_t
2004 i;
2005
2006 /*
2007 Scale pixels to desired (optimized with depth map).
2008 */
2009 depth_map=(Quantum *) AcquireQuantumMemory(MaxMap+1,sizeof(*depth_map));
2010 if (depth_map == (Quantum *) NULL)
2011 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2012 for (i=0; i <= (ssize_t) MaxMap; i++)
2013 depth_map[i]=ScaleAnyToQuantum(ScaleQuantumToAny((Quantum) i,range),
2014 range);
2015#if defined(MAGICKCORE_OPENMP_SUPPORT)
2016 #pragma omp parallel for schedule(static) shared(status) \
2017 magick_number_threads(image,image,image->rows,2)
2018#endif
2019 for (y=0; y < (ssize_t) image->rows; y++)
2020 {
2021 ssize_t
2022 x;
2023
2024 Quantum
2025 *magick_restrict q;
2026
2027 if (status == MagickFalse)
2028 continue;
2029 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
2030 exception);
2031 if (q == (Quantum *) NULL)
2032 {
2033 status=MagickFalse;
2034 continue;
2035 }
2036 for (x=0; x < (ssize_t) image->columns; x++)
2037 {
2038 ssize_t
2039 j;
2040
2041 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2042 {
2043 PixelChannel
2044 channel;
2045
2046 PixelTrait
2047 traits;
2048
2049 channel=GetPixelChannelChannel(image,j);
2050 traits=GetPixelChannelTraits(image,channel);
2051 if ((traits & UpdatePixelTrait) == 0)
2052 continue;
2053 q[j]=depth_map[ScaleQuantumToMap(q[j])];
2054 }
2055 q+=GetPixelChannels(image);
2056 }
2057 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2058 {
2059 status=MagickFalse;
2060 continue;
2061 }
2062 }
2063 image_view=DestroyCacheView(image_view);
2064 depth_map=(Quantum *) RelinquishMagickMemory(depth_map);
2065 if (status != MagickFalse)
2066 image->depth=depth;
2067 return(status);
2068 }
2069#endif
2070 /*
2071 Scale pixels to desired depth.
2072 */
2073#if defined(MAGICKCORE_OPENMP_SUPPORT)
2074 #pragma omp parallel for schedule(static) shared(status) \
2075 magick_number_threads(image,image,image->rows,2)
2076#endif
2077 for (y=0; y < (ssize_t) image->rows; y++)
2078 {
2079 ssize_t
2080 x;
2081
2082 Quantum
2083 *magick_restrict q;
2084
2085 if (status == MagickFalse)
2086 continue;
2087 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2088 if (q == (Quantum *) NULL)
2089 {
2090 status=MagickFalse;
2091 continue;
2092 }
2093 for (x=0; x < (ssize_t) image->columns; x++)
2094 {
2095 ssize_t
2096 i;
2097
2098 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2099 {
2100 PixelChannel
2101 channel;
2102
2103 PixelTrait
2104 traits;
2105
2106 channel=GetPixelChannelChannel(image,i);
2107 traits=GetPixelChannelTraits(image,channel);
2108 if ((traits & UpdatePixelTrait) == 0)
2109 continue;
2110 q[i]=ScaleAnyToQuantum(ScaleQuantumToAny(ClampPixel((MagickRealType)
2111 q[i]),range),range);
2112 }
2113 q+=GetPixelChannels(image);
2114 }
2115 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2116 {
2117 status=MagickFalse;
2118 continue;
2119 }
2120 }
2121 image_view=DestroyCacheView(image_view);
2122 if (status != MagickFalse)
2123 image->depth=depth;
2124 return(status);
2125}
2126
2127/*
2128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2129% %
2130% %
2131% %
2132% S e t I m a g e T y p e %
2133% %
2134% %
2135% %
2136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2137%
2138% SetImageType() sets the type of image. Choose from these types:
2139%
2140% Bilevel Grayscale GrayscaleMatte
2141% Palette PaletteMatte TrueColor
2142% TrueColorMatte ColorSeparation ColorSeparationMatte
2143% OptimizeType
2144%
2145% The format of the SetImageType method is:
2146%
2147% MagickBooleanType SetImageType(Image *image,const ImageType type,
2148% ExceptionInfo *exception)
2149%
2150% A description of each parameter follows:
2151%
2152% o image: the image.
2153%
2154% o type: Image type.
2155%
2156% o exception: return any errors or warnings in this structure.
2157%
2158*/
2159MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
2160 ExceptionInfo *exception)
2161{
2162 const char
2163 *artifact;
2164
2165 ImageInfo
2166 *image_info;
2167
2168 MagickBooleanType
2169 status;
2170
2172 *quantize_info;
2173
2174 assert(image != (Image *) NULL);
2175 if (IsEventLogging() != MagickFalse)
2176 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2177 assert(image->signature == MagickCoreSignature);
2178 status=MagickTrue;
2179 image_info=AcquireImageInfo();
2180 image_info->dither=image->dither;
2181 artifact=GetImageArtifact(image,"dither");
2182 if (artifact != (const char *) NULL)
2183 (void) SetImageOption(image_info,"dither",artifact);
2184 switch (type)
2185 {
2186 case BilevelType:
2187 {
2188 if (IsGrayImageType(image->type) == MagickFalse)
2189 status=TransformImageColorspace(image,GRAYColorspace,exception);
2190 (void) NormalizeImage(image,exception);
2191 (void) BilevelImage(image,(double) QuantumRange/2.0,exception);
2192 quantize_info=AcquireQuantizeInfo(image_info);
2193 quantize_info->number_colors=2;
2194 quantize_info->colorspace=GRAYColorspace;
2195 status=QuantizeImage(quantize_info,image,exception);
2196 quantize_info=DestroyQuantizeInfo(quantize_info);
2197 image->alpha_trait=UndefinedPixelTrait;
2198 break;
2199 }
2200 case GrayscaleType:
2201 {
2202 if (IsGrayImageType(image->type) == MagickFalse)
2203 status=TransformImageColorspace(image,GRAYColorspace,exception);
2204 image->alpha_trait=UndefinedPixelTrait;
2205 break;
2206 }
2207 case GrayscaleAlphaType:
2208 {
2209 if (IsGrayImageType(image->type) == MagickFalse)
2210 status=TransformImageColorspace(image,GRAYColorspace,exception);
2211 if ((image->alpha_trait & BlendPixelTrait) == 0)
2212 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2213 break;
2214 }
2215 case PaletteType:
2216 {
2217 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2218 status=TransformImageColorspace(image,sRGBColorspace,exception);
2219 if ((image->storage_class == DirectClass) || (image->colors > 256))
2220 {
2221 quantize_info=AcquireQuantizeInfo(image_info);
2222 quantize_info->number_colors=256;
2223 status=QuantizeImage(quantize_info,image,exception);
2224 quantize_info=DestroyQuantizeInfo(quantize_info);
2225 }
2226 image->alpha_trait=UndefinedPixelTrait;
2227 break;
2228 }
2229 case PaletteBilevelAlphaType:
2230 {
2231 ChannelType
2232 channel_mask;
2233
2234 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2235 status=TransformImageColorspace(image,sRGBColorspace,exception);
2236 if ((image->alpha_trait & BlendPixelTrait) == 0)
2237 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2238 channel_mask=SetImageChannelMask(image,AlphaChannel);
2239 (void) BilevelImage(image,(double) QuantumRange/2.0,exception);
2240 (void) SetImageChannelMask(image,channel_mask);
2241 quantize_info=AcquireQuantizeInfo(image_info);
2242 status=QuantizeImage(quantize_info,image,exception);
2243 quantize_info=DestroyQuantizeInfo(quantize_info);
2244 break;
2245 }
2246 case PaletteAlphaType:
2247 {
2248 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2249 status=TransformImageColorspace(image,sRGBColorspace,exception);
2250 if ((image->alpha_trait & BlendPixelTrait) == 0)
2251 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2252 quantize_info=AcquireQuantizeInfo(image_info);
2253 quantize_info->colorspace=TransparentColorspace;
2254 status=QuantizeImage(quantize_info,image,exception);
2255 quantize_info=DestroyQuantizeInfo(quantize_info);
2256 break;
2257 }
2258 case TrueColorType:
2259 {
2260 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2261 status=TransformImageColorspace(image,sRGBColorspace,exception);
2262 if (image->storage_class != DirectClass)
2263 status=SetImageStorageClass(image,DirectClass,exception);
2264 image->alpha_trait=UndefinedPixelTrait;
2265 break;
2266 }
2267 case TrueColorAlphaType:
2268 {
2269 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2270 status=TransformImageColorspace(image,sRGBColorspace,exception);
2271 if (image->storage_class != DirectClass)
2272 status=SetImageStorageClass(image,DirectClass,exception);
2273 if ((image->alpha_trait & BlendPixelTrait) == 0)
2274 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2275 break;
2276 }
2277 case ColorSeparationType:
2278 {
2279 if (image->colorspace != CMYKColorspace)
2280 status=TransformImageColorspace(image,CMYKColorspace,exception);
2281 if (image->storage_class != DirectClass)
2282 status=SetImageStorageClass(image,DirectClass,exception);
2283 image->alpha_trait=UndefinedPixelTrait;
2284 break;
2285 }
2286 case ColorSeparationAlphaType:
2287 {
2288 if (image->colorspace != CMYKColorspace)
2289 status=TransformImageColorspace(image,CMYKColorspace,exception);
2290 if (image->storage_class != DirectClass)
2291 status=SetImageStorageClass(image,DirectClass,exception);
2292 if ((image->alpha_trait & BlendPixelTrait) == 0)
2293 status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2294 break;
2295 }
2296 case OptimizeType:
2297 case UndefinedType:
2298 break;
2299 }
2300 image_info=DestroyImageInfo(image_info);
2301 if (status == MagickFalse)
2302 return(status);
2303 image->type=type;
2304 return(MagickTrue);
2305}