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"
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)
171 case NorthWestGravity:
175 p=GetCacheViewVirtualPixels(image_view,0,0,1,1,exception);
178 case NorthEastGravity:
181 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,0,1,1,
185 case SouthEastGravity:
188 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,
189 (ssize_t) image->rows-1,1,1,exception);
192 case SouthWestGravity:
195 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-1,1,1,
200 if (p == (
const Quantum *) NULL)
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)
218 edge_view=AcquireVirtualCacheView(edge_image,exception);
219 for (y=0; y < (ssize_t) edge_image->rows; y++)
224 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
225 if (p == (
const Quantum *) NULL)
227 for (x=0; x < (ssize_t) edge_image->columns; x++)
229 GetPixelInfoPixel(edge_image,p,&pixel);
230 if (IsFuzzyEquivalencePixelInfo(&pixel,&background) == MagickFalse)
232 p+=(ptrdiff_t) GetPixelChannels(edge_image);
235 census/=((double) edge_image->columns*edge_image->rows);
236 edge_view=DestroyCacheView(edge_view);
237 edge_image=DestroyImage(edge_image);
241static inline double GetMinEdgeBackgroundCensus(
const CensusInfo *edge)
246 census=MagickMin(MagickMin(MagickMin(edge->left,edge->right),edge->top),
251static RectangleInfo GetEdgeBoundingBox(
const Image *image,
252 ExceptionInfo *exception)
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)
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,
290 edge.right=GetEdgeBackgroundCensus(edge_image,edge_view,EastGravity,
292 edge.top=GetEdgeBackgroundCensus(edge_image,edge_view,NorthGravity,
294 edge.bottom=GetEdgeBackgroundCensus(edge_image,edge_view,SouthGravity,
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),
302 background_census=GetMinEdgeBackgroundCensus(&edge);
303 for ( ; background_census < percent_background;
304 background_census=GetMinEdgeBackgroundCensus(&edge))
306 if ((bounds.width == 0) || (bounds.height == 0))
308 if (fabs(edge.left-background_census) < MagickEpsilon)
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);
326 if (fabs(edge.right-background_census) < MagickEpsilon)
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);
344 if (fabs(edge.top-background_census) < MagickEpsilon)
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);
362 if (fabs(edge.bottom-background_census) < MagickEpsilon)
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);
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);
391MagickExport RectangleInfo GetImageBoundingBox(
const Image *image,
392 ExceptionInfo *exception)
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)
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;
438 bounds.width=(size_t) image->columns;
439 bounds.height=(size_t) image->rows;
442 edges=AcquireString(artifact);
444 while ((q=StringToken(
",",&r)) != (
char *) NULL)
446 if (LocaleCompare(q,
"north") == 0)
447 bounds.y=(ssize_t) image->rows;
448 if (LocaleCompare(q,
"east") == 0)
450 if (LocaleCompare(q,
"south") == 0)
452 if (LocaleCompare(q,
"west") == 0)
453 bounds.x=(ssize_t) image->columns;
455 edges=DestroyString(edges);
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)
462 image_view=DestroyCacheView(image_view);
465 GetPixelInfoPixel(image,p,&target[0]);
466 GetPixelInfo(image,&target[1]);
467 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,0,1,1,
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,
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]);
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)
487 for (y=0; y < (ssize_t) image->rows; y++)
501 if (status == MagickFalse)
503#if defined(MAGICKCORE_OPENMP_SUPPORT)
504# pragma omp critical (MagickCore_GetImageBoundingBox)
507 q=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
508 if (q == (
const Quantum *) NULL)
514 for (x=0; x < (ssize_t) image->columns; x++)
516 GetPixelInfoPixel(image,q,&pixel);
517 if ((x < bounding_box.x) &&
518 (IsFuzzyEquivalencePixelInfo(&pixel,&target[0]) == MagickFalse))
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))
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))
533 bounding_box.width=(size_t) x;
534 bounding_box.height=(size_t) y;
536 q+=(ptrdiff_t) GetPixelChannels(image);
538#if defined(MAGICKCORE_OPENMP_SUPPORT)
539# pragma omp critical (MagickCore_GetImageBoundingBox)
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;
552 image_view=DestroyCacheView(image_view);
553 if ((bounds.width == 0) || (bounds.height == 0) ||
554 (bounds.x > (ssize_t) bounds.width) ||
555 (bounds.y > (ssize_t) bounds.height))
556 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
557 "GeometryDoesNotContainImage",
"`%s'",image->filename);
560 bounds.width-=(size_t) (bounds.x-1);
561 bounds.height-=(size_t) (bounds.y-1);
594static double LexicographicalOrder(PointInfo *a,PointInfo *b,PointInfo *c)
599 return((b->x-a->x)*(c->y-a->y)-(b->y-a->y)*(c->x-a->x));
602static PixelInfo GetEdgeBackgroundColor(
const Image *image,
603 const CacheView *image_view,ExceptionInfo *exception)
622 memset(&edge_background,0,
sizeof(edge_background));
623 artifact=GetImageArtifact(image,
"convex-hull:background-color");
624 if (artifact == (
const char *) NULL)
625 artifact=GetImageArtifact(image,
"background");
626#if defined(MAGICKCORE_OPENMP_SUPPORT)
627 #pragma omp parallel for schedule(static)
629 for (i=0; i < 4; i++)
653 (void) memset(&edge_geometry,0,
sizeof(edge_geometry));
659 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-1,1,1,
662 edge_geometry.width=1;
663 edge_geometry.height=0;
668 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,0,1,1,
671 edge_geometry.width=1;
672 edge_geometry.height=0;
677 p=GetCacheViewVirtualPixels(image_view,0,0,1,1,exception);
678 gravity=NorthGravity;
679 edge_geometry.width=0;
680 edge_geometry.height=1;
685 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,
686 (ssize_t) image->rows-1,1,1,exception);
687 gravity=SouthGravity;
688 edge_geometry.width=0;
689 edge_geometry.height=1;
693 GetPixelInfoPixel(image,p,background+i);
694 if (artifact != (
const char *) NULL)
695 (void) QueryColorCompliance(artifact,AllCompliance,background+i,
697 GravityAdjustGeometry(image->columns,image->rows,gravity,&edge_geometry);
698 edge_image=CropImage(image,&edge_geometry,exception);
699 if (edge_image == (Image *) NULL)
701 edge_view=AcquireVirtualCacheView(edge_image,exception);
702 for (y=0; y < (ssize_t) edge_image->rows; y++)
707 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,
709 if (p == (
const Quantum *) NULL)
711 for (x=0; x < (ssize_t) edge_image->columns; x++)
713 GetPixelInfoPixel(edge_image,p,&pixel);
714 if (IsFuzzyEquivalencePixelInfo(&pixel,background+i) == MagickFalse)
716 p+=(ptrdiff_t) GetPixelChannels(edge_image);
719 edge_view=DestroyCacheView(edge_view);
720 edge_image=DestroyImage(edge_image);
723 for (i=0; i < 4; i++)
724 if (census[i] > edge_census)
726 edge_background=background[i];
727 edge_census=census[i];
729 return(edge_background);
732void TraceConvexHull(PointInfo *vertices,
size_t number_vertices,
733 PointInfo ***monotone_chain,
size_t *chain_length)
748 chain=(*monotone_chain);
750 for (i=0; i < (ssize_t) number_vertices; i++)
753 (LexicographicalOrder(chain[n-2],chain[n-1],&vertices[i]) <= 0.0))
755 chain[n++]=(&vertices[i]);
758 for (i=(ssize_t) number_vertices-2; i >= 0; i--)
760 while ((n >= demark) &&
761 (LexicographicalOrder(chain[n-2],chain[n-1],&vertices[i]) <= 0.0))
763 chain[n++]=(&vertices[i]);
768MagickExport PointInfo *GetImageConvexHull(
const Image *image,
769 size_t *number_vertices,ExceptionInfo *exception)
798 assert(image != (Image *) NULL);
799 assert(image->signature == MagickCoreSignature);
800 if (IsEventLogging() != MagickFalse)
801 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
803 vertices_info=AcquireVirtualMemory(image->columns,image->rows*
805 monotone_info=AcquireVirtualMemory(2*image->columns,2*
806 image->rows*
sizeof(*monotone_chain));
807 if ((vertices_info == (MemoryInfo *) NULL) ||
808 (monotone_info == (MemoryInfo *) NULL))
810 if (monotone_info != (MemoryInfo *) NULL)
811 monotone_info=(MemoryInfo *) RelinquishVirtualMemory(monotone_info);
812 if (vertices_info != (MemoryInfo *) NULL)
813 vertices_info=RelinquishVirtualMemory(vertices_info);
814 return((PointInfo *) NULL);
816 vertices=(PointInfo *) GetVirtualMemoryBlob(vertices_info);
817 monotone_chain=(PointInfo **) GetVirtualMemoryBlob(monotone_info);
818 image_view=AcquireVirtualCacheView(image,exception);
819 background=GetEdgeBackgroundColor(image,image_view,exception);
822 for (y=0; y < (ssize_t) image->rows; y++)
830 if (status == MagickFalse)
832 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
833 if (p == (
const Quantum *) NULL)
838 for (x=0; x < (ssize_t) image->columns; x++)
843 GetPixelInfoPixel(image,p,&pixel);
844 if (IsFuzzyEquivalencePixelInfo(&pixel,&background) == MagickFalse)
846 vertices[n].x=(double) x;
847 vertices[n].y=(double) y;
850 p+=(ptrdiff_t) GetPixelChannels(image);
853 image_view=DestroyCacheView(image_view);
857 TraceConvexHull(vertices,n,&monotone_chain,number_vertices);
858 convex_hull=(PointInfo *) AcquireQuantumMemory(*number_vertices,
859 sizeof(*convex_hull));
860 if (convex_hull != (PointInfo *) NULL)
861 for (n=0; n < *number_vertices; n++)
862 convex_hull[n]=(*monotone_chain[n]);
863 monotone_info=RelinquishVirtualMemory(monotone_info);
864 vertices_info=RelinquishVirtualMemory(vertices_info);
892MagickExport
size_t GetImageDepth(
const Image *image,ExceptionInfo *exception)
914 assert(image != (Image *) NULL);
915 assert(image->signature == MagickCoreSignature);
916 if (IsEventLogging() != MagickFalse)
917 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
918 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
919 current_depth=(
size_t *) AcquireQuantumMemory(number_threads,
920 sizeof(*current_depth));
921 if (current_depth == (
size_t *) NULL)
922 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
924 for (i=0; i < (ssize_t) number_threads; i++)
926 if ((image->storage_class == PseudoClass) &&
927 ((image->alpha_trait & BlendPixelTrait) == 0))
929 for (i=0; i < (ssize_t) image->colors; i++)
932 id = GetOpenMPThreadId();
934 while (current_depth[
id] < MAGICKCORE_QUANTUM_DEPTH)
943 range=GetQuantumRange(current_depth[
id]);
944 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
945 if (IsPixelAtDepth(ClampToQuantum(image->colormap[i].red),range) == MagickFalse)
947 if ((atDepth != MagickFalse) &&
948 (GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
949 if (IsPixelAtDepth(ClampToQuantum(image->colormap[i].green),range) == MagickFalse)
951 if ((atDepth != MagickFalse) &&
952 (GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
953 if (IsPixelAtDepth(ClampToQuantum(image->colormap[i].blue),range) == MagickFalse)
955 if ((atDepth != MagickFalse))
960 depth=current_depth[0];
961 for (i=1; i < (ssize_t) number_threads; i++)
962 if (depth < current_depth[i])
963 depth=current_depth[i];
964 current_depth=(
size_t *) RelinquishMagickMemory(current_depth);
967 image_view=AcquireVirtualCacheView(image,exception);
968#if !defined(MAGICKCORE_HDRI_SUPPORT)
969 DisableMSCWarning(4127)
970 if ((1UL*QuantumRange) <= MaxMap)
979 depth_map=(
size_t *) AcquireQuantumMemory(MaxMap+1,
sizeof(*depth_map));
980 if (depth_map == (
size_t *) NULL)
981 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
982 for (i=0; i <= (ssize_t) MaxMap; i++)
984 for (depth=1; depth < (size_t) MAGICKCORE_QUANTUM_DEPTH; depth++)
992 range=GetQuantumRange(depth);
994 if (pixel == ScaleAnyToQuantum(ScaleQuantumToAny(pixel,range),range))
999#if defined(MAGICKCORE_OPENMP_SUPPORT)
1000 #pragma omp parallel for schedule(static) shared(status) \
1001 magick_number_threads(image,image,image->rows,1)
1003 for (y=0; y < (ssize_t) image->rows; y++)
1006 id = GetOpenMPThreadId();
1014 if (status == MagickFalse)
1016 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1017 if (p == (
const Quantum *) NULL)
1019 for (x=0; x < (ssize_t) image->columns; x++)
1024 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1026 PixelChannel channel = GetPixelChannelChannel(image,j);
1027 PixelTrait traits = GetPixelChannelTraits(image,channel);
1028 if ((traits & UpdatePixelTrait) == 0)
1030 if (depth_map[ScaleQuantumToMap(p[j])] > current_depth[
id])
1031 current_depth[id]=depth_map[ScaleQuantumToMap(p[j])];
1033 p+=(ptrdiff_t) GetPixelChannels(image);
1035 if (current_depth[
id] == MAGICKCORE_QUANTUM_DEPTH)
1038 image_view=DestroyCacheView(image_view);
1039 depth=current_depth[0];
1040 for (i=1; i < (ssize_t) number_threads; i++)
1041 if (depth < current_depth[i])
1042 depth=current_depth[i];
1043 depth_map=(
size_t *) RelinquishMagickMemory(depth_map);
1044 current_depth=(
size_t *) RelinquishMagickMemory(current_depth);
1051#if defined(MAGICKCORE_OPENMP_SUPPORT)
1052 #pragma omp parallel for schedule(static) shared(status) \
1053 magick_number_threads(image,image,image->rows,1)
1055 for (y=0; y < (ssize_t) image->rows; y++)
1058 id = GetOpenMPThreadId();
1066 if (status == MagickFalse)
1068 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1069 if (p == (
const Quantum *) NULL)
1071 for (x=0; x < (ssize_t) image->columns; x++)
1076 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1084 channel=GetPixelChannelChannel(image,j);
1085 traits=GetPixelChannelTraits(image,channel);
1086 if ((traits & UpdatePixelTrait) == 0)
1088 while (current_depth[
id] < MAGICKCORE_QUANTUM_DEPTH)
1093 range=GetQuantumRange(current_depth[
id]);
1094 if (p[j] == ScaleAnyToQuantum(ScaleQuantumToAny(p[j],range),range))
1096 current_depth[id]++;
1099 p+=(ptrdiff_t) GetPixelChannels(image);
1101 if (current_depth[
id] == MAGICKCORE_QUANTUM_DEPTH)
1104 image_view=DestroyCacheView(image_view);
1105 depth=current_depth[0];
1106 for (i=1; i < (ssize_t) number_threads; i++)
1107 if (depth < current_depth[i])
1108 depth=current_depth[i];
1109 current_depth=(
size_t *) RelinquishMagickMemory(current_depth);
1159static inline double getAngle(PointInfo *p,PointInfo *q)
1164 return(RadiansToDegrees(atan2(q->y-p->y,q->x-p->x)));
1167static inline double getDistance(PointInfo *p,PointInfo *q)
1172 distance=hypot(p->x-q->x,p->y-q->y);
1173 return(distance*distance);
1176static inline double getProjection(PointInfo *p,PointInfo *q,PointInfo *v)
1184 distance=getDistance(p,q);
1185 if (distance < MagickEpsilon)
1187 return((q->x-p->x)*(v->x-p->x)+(v->y-p->y)*(q->y-p->y))/sqrt(distance);
1190static inline double getFeretDiameter(PointInfo *p,PointInfo *q,PointInfo *v)
1198 distance=getDistance(p,q);
1199 if (distance < MagickEpsilon)
1201 return((q->x-p->x)*(v->y-p->y)-(v->x-p->x)*(q->y-p->y))/sqrt(distance);
1204MagickExport PointInfo *GetImageMinimumBoundingBox(Image *image,
1205 size_t *number_vertices,ExceptionInfo *exception)
1223 number_hull_vertices;
1231 assert(image != (Image *) NULL);
1232 assert(image->signature == MagickCoreSignature);
1233 if (IsEventLogging() != MagickFalse)
1234 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1236 vertices=GetImageConvexHull(image,&number_hull_vertices,exception);
1237 if (vertices == (PointInfo *) NULL)
1238 return((PointInfo *) NULL);
1240 bounding_box=(PointInfo *) AcquireQuantumMemory(*number_vertices,
1241 sizeof(*bounding_box));
1242 if (bounding_box == (PointInfo *) NULL)
1244 vertices=(PointInfo *) RelinquishMagickMemory(vertices);
1245 return((PointInfo *) NULL);
1247 caliper_info.area=2.0*image->columns*image->rows;
1248 caliper_info.width=(double) image->columns+image->rows;
1249 caliper_info.height=0.0;
1250 caliper_info.projection=0.0;
1251 caliper_info.p=(-1);
1252 caliper_info.q=(-1);
1253 caliper_info.v=(-1);
1254 for (i=0; i < (ssize_t) number_hull_vertices; i++)
1258 max_projection = 0.0,
1259 min_diameter = -1.0,
1260 min_projection = 0.0;
1271 for (j=0; j < (ssize_t) number_hull_vertices; j++)
1273 diameter=fabs(getFeretDiameter(&vertices[i],
1274 &vertices[(i+1) % (ssize_t) number_hull_vertices],&vertices[j]));
1275 if (min_diameter < diameter)
1277 min_diameter=diameter;
1279 q=(i+1) % (ssize_t) number_hull_vertices;
1283 for (k=0; k < (ssize_t) number_hull_vertices; k++)
1291 projection=getProjection(&vertices[p],&vertices[q],&vertices[k]);
1292 min_projection=MagickMin(min_projection,projection);
1293 max_projection=MagickMax(max_projection,projection);
1295 area=min_diameter*(max_projection-min_projection);
1296 if (caliper_info.area > area)
1298 caliper_info.area=area;
1299 caliper_info.width=min_diameter;
1300 caliper_info.height=max_projection-min_projection;
1301 caliper_info.projection=max_projection;
1310 diameter=getFeretDiameter(&vertices[caliper_info.p],
1311 &vertices[caliper_info.q],&vertices[caliper_info.v]);
1312 angle=atan2(vertices[caliper_info.q].y-vertices[caliper_info.p].y,
1313 vertices[caliper_info.q].x-vertices[caliper_info.p].x);
1314 bounding_box[0].x=vertices[caliper_info.p].x+cos(angle)*
1315 caliper_info.projection;
1316 bounding_box[0].y=vertices[caliper_info.p].y+sin(angle)*
1317 caliper_info.projection;
1318 bounding_box[1].x=floor(bounding_box[0].x+cos(angle+MagickPI/2.0)*diameter+
1320 bounding_box[1].y=floor(bounding_box[0].y+sin(angle+MagickPI/2.0)*diameter+
1322 bounding_box[2].x=floor(bounding_box[1].x+cos(angle)*(-caliper_info.height)+
1324 bounding_box[2].y=floor(bounding_box[1].y+sin(angle)*(-caliper_info.height)+
1326 bounding_box[3].x=floor(bounding_box[2].x+cos(angle+MagickPI/2.0)*(-diameter)+
1328 bounding_box[3].y=floor(bounding_box[2].y+sin(angle+MagickPI/2.0)*(-diameter)+
1333 (void) FormatImageProperty(image,
"minimum-bounding-box:area",
"%.*g",
1334 GetMagickPrecision(),caliper_info.area);
1335 (void) FormatImageProperty(image,
"minimum-bounding-box:width",
"%.*g",
1336 GetMagickPrecision(),caliper_info.width);
1337 (void) FormatImageProperty(image,
"minimum-bounding-box:height",
"%.*g",
1338 GetMagickPrecision(),caliper_info.height);
1339 (void) FormatImageProperty(image,
"minimum-bounding-box:_p",
"%.*g,%.*g",
1340 GetMagickPrecision(),vertices[caliper_info.p].x,
1341 GetMagickPrecision(),vertices[caliper_info.p].y);
1342 (void) FormatImageProperty(image,
"minimum-bounding-box:_q",
"%.*g,%.*g",
1343 GetMagickPrecision(),vertices[caliper_info.q].x,
1344 GetMagickPrecision(),vertices[caliper_info.q].y);
1345 (void) FormatImageProperty(image,
"minimum-bounding-box:_v",
"%.*g,%.*g",
1346 GetMagickPrecision(),vertices[caliper_info.v].x,
1347 GetMagickPrecision(),vertices[caliper_info.v].y);
1351 distance=hypot(bounding_box[0].x,bounding_box[0].y);
1352 angle=getAngle(&bounding_box[0],&bounding_box[1]);
1353 for (i=1; i < 4; i++)
1355 double d = hypot(bounding_box[i].x,bounding_box[i].y);
1359 angle=getAngle(&bounding_box[i],&bounding_box[(i+1) % 4]);
1362 artifact=GetImageArtifact(image,
"minimum-bounding-box:orientation");
1363 if (artifact != (
const char *) NULL)
1377 point=bounding_box[0];
1378 for (i=1; i < 4; i++)
1380 if (bounding_box[i].x < point.x)
1381 point.x=bounding_box[i].x;
1382 if (bounding_box[i].y < point.y)
1383 point.y=bounding_box[i].y;
1385 for (i=0; i < 4; i++)
1387 bounding_box[i].x-=point.x;
1388 bounding_box[i].y-=point.y;
1390 for (i=0; i < 4; i++)
1397 delta.x=bounding_box[(i+1) % 4].x-bounding_box[i].x;
1398 delta.y=bounding_box[(i+1) % 4].y-bounding_box[i].y;
1399 slope=delta.y*MagickSafeReciprocal(delta.x);
1400 intercept=bounding_box[(i+1) % 4].y-slope*bounding_box[i].x;
1401 d=fabs((slope*bounding_box[i].x-bounding_box[i].y+intercept)*
1402 MagickSafeReciprocal(sqrt(slope*slope+1.0)));
1403 if ((i == 0) || (d < distance))
1409 angle=RadiansToDegrees(atan(point.y*MagickSafeReciprocal(point.x)));
1410 length=hypot(point.x,point.y);
1411 p_length=fabs((
double) MagickMax(caliper_info.width,caliper_info.height)-
1413 q_length=fabs(length-(
double) MagickMin(caliper_info.width,
1414 caliper_info.height));
1415 if (LocaleCompare(artifact,
"landscape") == 0)
1417 if (p_length > q_length)
1418 angle+=(angle < 0.0) ? 90.0 : -90.0;
1421 if (LocaleCompare(artifact,
"portrait") == 0)
1423 if (p_length < q_length)
1424 angle+=(angle >= 0.0) ? 90.0 : -90.0;
1427 (void) FormatImageProperty(image,
"minimum-bounding-box:angle",
"%.*g",
1428 GetMagickPrecision(),angle);
1429 (void) FormatImageProperty(image,
"minimum-bounding-box:unrotate",
"%.*g",
1430 GetMagickPrecision(),-angle);
1431 vertices=(PointInfo *) RelinquishMagickMemory(vertices);
1432 return(bounding_box);
1462MagickExport
size_t GetImageQuantumDepth(
const Image *image,
1463 const MagickBooleanType constrain)
1480 if (constrain != MagickFalse)
1481 depth=(size_t) MagickMin((
double) depth,(
double) MAGICKCORE_QUANTUM_DEPTH);
1511MagickExport ImageType GetImageType(
const Image *image)
1513 assert(image != (Image *) NULL);
1514 assert(image->signature == MagickCoreSignature);
1515 if (image->colorspace == CMYKColorspace)
1517 if ((image->alpha_trait & BlendPixelTrait) == 0)
1518 return(ColorSeparationType);
1519 return(ColorSeparationAlphaType);
1521 if (IsImageMonochrome(image) != MagickFalse)
1522 return(BilevelType);
1523 if (IsImageGray(image) != MagickFalse)
1525 if (image->alpha_trait != UndefinedPixelTrait)
1526 return(GrayscaleAlphaType);
1527 return(GrayscaleType);
1529 if (IsPaletteImage(image) != MagickFalse)
1531 if (image->alpha_trait != UndefinedPixelTrait)
1532 return(PaletteAlphaType);
1533 return(PaletteType);
1535 if (image->alpha_trait != UndefinedPixelTrait)
1536 return(TrueColorAlphaType);
1537 return(TrueColorType);
1566MagickExport ImageType IdentifyImageGray(
const Image *image,
1567 ExceptionInfo *exception)
1576 status = MagickTrue;
1581 assert(image != (Image *) NULL);
1582 assert(image->signature == MagickCoreSignature);
1583 if (IsEventLogging() != MagickFalse)
1584 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1585 if (IsImageGray(image) != MagickFalse)
1586 return(image->type);
1587 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1588 return(UndefinedType);
1589 image_view=AcquireVirtualCacheView(image,exception);
1590#if defined(MAGICKCORE_OPENMP_SUPPORT)
1591 #pragma omp parallel for schedule(static) shared(status,type) \
1592 magick_number_threads(image,image,image->rows,2)
1594 for (y=0; y < (ssize_t) image->rows; y++)
1602 if (status == MagickFalse)
1604 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1605 if (p == (
const Quantum *) NULL)
1610 for (x=0; x < (ssize_t) image->columns; x++)
1612 if (IsPixelGray(image,p) == MagickFalse)
1617 if ((type == BilevelType) && (IsPixelMonochrome(image,p) == MagickFalse))
1619 p+=(ptrdiff_t) GetPixelChannels(image);
1622 image_view=DestroyCacheView(image_view);
1623 if ((type == GrayscaleType) && (image->alpha_trait != UndefinedPixelTrait))
1624 type=GrayscaleAlphaType;
1625 if (status == MagickFalse)
1626 return(UndefinedType);
1657MagickExport MagickBooleanType IdentifyImageMonochrome(
const Image *image,
1658 ExceptionInfo *exception)
1669 assert(image != (Image *) NULL);
1670 assert(image->signature == MagickCoreSignature);
1671 if (IsEventLogging() != MagickFalse)
1672 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1673 if (image->type == BilevelType)
1675 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1676 return(MagickFalse);
1677 image_view=AcquireVirtualCacheView(image,exception);
1678#if defined(MAGICKCORE_OPENMP_SUPPORT)
1679 #pragma omp parallel for schedule(static) shared(type) \
1680 magick_number_threads(image,image,image->rows,2)
1682 for (y=0; y < (ssize_t) image->rows; y++)
1690 if (type == UndefinedType)
1692 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1693 if (p == (
const Quantum *) NULL)
1698 for (x=0; x < (ssize_t) image->columns; x++)
1700 if (IsPixelMonochrome(image,p) == MagickFalse)
1705 p+=(ptrdiff_t) GetPixelChannels(image);
1708 image_view=DestroyCacheView(image_view);
1709 return(type == BilevelType ? MagickTrue : MagickFalse);
1744MagickExport ImageType IdentifyImageType(
const Image *image,
1745 ExceptionInfo *exception)
1750 assert(image != (Image *) NULL);
1751 assert(image->signature == MagickCoreSignature);
1752 if (IsEventLogging() != MagickFalse)
1753 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1754 if (image->colorspace == CMYKColorspace)
1756 if ((image->alpha_trait & BlendPixelTrait) == 0)
1757 return(ColorSeparationType);
1758 return(ColorSeparationAlphaType);
1760 type=IdentifyImageGray(image,exception);
1761 if (IsGrayImageType(type))
1763 if (IdentifyPaletteImage(image,exception) != MagickFalse)
1765 if (image->alpha_trait != UndefinedPixelTrait)
1766 return(PaletteAlphaType);
1767 return(PaletteType);
1769 if (image->alpha_trait != UndefinedPixelTrait)
1770 return(TrueColorAlphaType);
1771 return(TrueColorType);
1797MagickExport MagickBooleanType IsImageGray(
const Image *image)
1799 assert(image != (Image *) NULL);
1800 assert(image->signature == MagickCoreSignature);
1801 if (IsGrayImageType(image->type) != MagickFalse)
1803 return(MagickFalse);
1828MagickExport MagickBooleanType IsImageMonochrome(
const Image *image)
1830 assert(image != (Image *) NULL);
1831 assert(image->signature == MagickCoreSignature);
1832 if (image->type == BilevelType)
1834 return(MagickFalse);
1865MagickExport MagickBooleanType IsImageOpaque(
const Image *image,
1866 ExceptionInfo *exception)
1872 opaque = MagickTrue;
1880 assert(image != (Image *) NULL);
1881 assert(image->signature == MagickCoreSignature);
1882 if (IsEventLogging() != MagickFalse)
1883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1884 if ((image->alpha_trait & BlendPixelTrait) == 0)
1886 image_view=AcquireVirtualCacheView(image,exception);
1887#if defined(MAGICKCORE_OPENMP_SUPPORT)
1888 #pragma omp parallel for schedule(static) shared(opaque) \
1889 magick_number_threads(image,image,image->rows,2)
1891 for (y=0; y < (ssize_t) image->rows; y++)
1899 if (opaque == MagickFalse)
1901 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1902 if (p == (
const Quantum *) NULL)
1907 for (x=0; x < (ssize_t) image->columns; x++)
1909 if (GetPixelAlpha(image,p) != OpaqueAlpha)
1914 p+=(ptrdiff_t) GetPixelChannels(image);
1917 image_view=DestroyCacheView(image_view);
1951static MagickBooleanType FloydSteinbergImageDepth(Image *image,
1952 const size_t depth,ExceptionInfo *exception)
1975 status=SetImageStorageClass(image,DirectClass,exception);
1976 if (status == MagickFalse)
1977 return(MagickFalse);
1978 channels=GetPixelChannels(image);
1979 distortion=(
double *) AcquireQuantumMemory(image->columns,3*channels*
1980 sizeof(*distortion));
1981 if (distortion == (
double *) NULL)
1982 return(MagickFalse);
1983 (void) memset(distortion,0,3*image->columns*channels*
sizeof(*distortion));
1984 range=GetQuantumRange(depth);
1985 image_view=AcquireAuthenticCacheView(image,exception);
1986 for (y=0; y < (ssize_t) image->rows; y++)
1996 if (status == MagickFalse)
1998 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1999 if (q == (Quantum *) NULL)
2007 u=(y % 3)*(ssize_t) (image->columns*channels);
2008 (void) memset(distortion+u,0,image->columns*channels*
sizeof(*distortion));
2009 v=((y+1) % 3)*(ssize_t) (image->columns*channels);
2010 for (x=0; x < (ssize_t) image->columns; x++)
2015 for (i=0; i < (ssize_t) channels; i++)
2030 channel=GetPixelChannelChannel(image,i);
2031 traits=GetPixelChannelTraits(image,channel);
2032 if ((traits & UpdatePixelTrait) == 0)
2038 pixel=(double) q[i]+distortion[u];
2039 q[i]=ScaleAnyToQuantum(ScaleQuantumToAny(ClampPixel((MagickRealType)
2040 pixel),range),range);
2044 error=pixel-(double) q[i];
2045 if ((x+1) < (ssize_t) image->columns)
2046 distortion[u+(ssize_t) channels]+=7.0*error/16.0;
2047 if ((y+1) < (ssize_t) image->rows)
2053 distortion[v-(ssize_t) channels]+=3.0*error/16.0;
2054 distortion[v]+=5.0*error/16.0;
2055 if ((x+1) < (ssize_t) image->columns)
2056 distortion[v+(ssize_t) channels]+=1.0*error/16.0;
2061 q+=(ptrdiff_t) GetPixelChannels(image);
2063 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2069 image_view=DestroyCacheView(image_view);
2070 distortion=(
double *) RelinquishMagickMemory(distortion);
2071 if (status != MagickFalse)
2076MagickExport MagickBooleanType SetImageDepth(Image *image,
2077 const size_t depth,ExceptionInfo *exception)
2094 assert(image != (Image *) NULL);
2095 if (IsEventLogging() != MagickFalse)
2096 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2097 assert(image->signature == MagickCoreSignature);
2098 if (depth >= MAGICKCORE_QUANTUM_DEPTH)
2103 artifact=GetImageArtifact(image,
"dither");
2104 if ((artifact != (
const char *) NULL) &&
2105 (LocaleCompare(artifact,
"FloydSteinberg") == 0))
2106 return(FloydSteinbergImageDepth(image,depth,exception));
2107 range=GetQuantumRange(depth);
2108 if (image->storage_class == PseudoClass)
2113#if defined(MAGICKCORE_OPENMP_SUPPORT)
2114 #pragma omp parallel for schedule(static) shared(status) \
2115 magick_number_threads(image,image,image->colors,1)
2117 for (i=0; i < (ssize_t) image->colors; i++)
2119 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2120 image->colormap[i].red=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
2121 ClampPixel(image->colormap[i].red),range),range);
2122 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2123 image->colormap[i].green=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
2124 ClampPixel(image->colormap[i].green),range),range);
2125 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2126 image->colormap[i].blue=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
2127 ClampPixel(image->colormap[i].blue),range),range);
2128 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2129 image->colormap[i].alpha=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
2130 ClampPixel(image->colormap[i].alpha),range),range);
2134 image_view=AcquireAuthenticCacheView(image,exception);
2135#if !defined(MAGICKCORE_HDRI_SUPPORT)
2136 DisableMSCWarning(4127)
2137 if ((1UL*QuantumRange) <= MaxMap)
2149 depth_map=(Quantum *) AcquireQuantumMemory(MaxMap+1,
sizeof(*depth_map));
2150 if (depth_map == (Quantum *) NULL)
2151 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2152 for (i=0; i <= (ssize_t) MaxMap; i++)
2153 depth_map[i]=ScaleAnyToQuantum(ScaleQuantumToAny((Quantum) i,range),
2155#if defined(MAGICKCORE_OPENMP_SUPPORT)
2156 #pragma omp parallel for schedule(static) shared(status) \
2157 magick_number_threads(image,image,image->rows,2)
2159 for (y=0; y < (ssize_t) image->rows; y++)
2167 if (status == MagickFalse)
2169 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
2171 if (q == (Quantum *) NULL)
2176 for (x=0; x < (ssize_t) image->columns; x++)
2181 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2189 channel=GetPixelChannelChannel(image,j);
2190 traits=GetPixelChannelTraits(image,channel);
2191 if ((traits & UpdatePixelTrait) == 0)
2193 q[j]=depth_map[ScaleQuantumToMap(q[j])];
2195 q+=(ptrdiff_t) GetPixelChannels(image);
2197 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2203 image_view=DestroyCacheView(image_view);
2204 depth_map=(Quantum *) RelinquishMagickMemory(depth_map);
2205 if (status != MagickFalse)
2213#if defined(MAGICKCORE_OPENMP_SUPPORT)
2214 #pragma omp parallel for schedule(static) shared(status) \
2215 magick_number_threads(image,image,image->rows,2)
2217 for (y=0; y < (ssize_t) image->rows; y++)
2225 if (status == MagickFalse)
2227 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2228 if (q == (Quantum *) NULL)
2233 for (x=0; x < (ssize_t) image->columns; x++)
2238 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2246 channel=GetPixelChannelChannel(image,i);
2247 traits=GetPixelChannelTraits(image,channel);
2248 if ((traits & UpdatePixelTrait) == 0)
2250 q[i]=ScaleAnyToQuantum(ScaleQuantumToAny(ClampPixel((MagickRealType)
2251 q[i]),range),range);
2253 q+=(ptrdiff_t) GetPixelChannels(image);
2255 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2261 image_view=DestroyCacheView(image_view);
2262 if (status != MagickFalse)
2299MagickExport MagickBooleanType SetImageType(Image *image,
const ImageType type,
2300 ExceptionInfo *exception)
2314 assert(image != (Image *) NULL);
2315 if (IsEventLogging() != MagickFalse)
2316 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
2317 assert(image->signature == MagickCoreSignature);
2319 image_info=AcquireImageInfo();
2320 image_info->dither=image->dither;
2321 artifact=GetImageArtifact(image,
"dither");
2322 if (artifact != (
const char *) NULL)
2323 (void) SetImageOption(image_info,
"dither",artifact);
2328 if (IsGrayImageType(image->type) == MagickFalse)
2329 status=TransformImageColorspace(image,GRAYColorspace,exception);
2330 (void) NormalizeImage(image,exception);
2331 (void) BilevelImage(image,(
double) QuantumRange/2.0,exception);
2332 quantize_info=AcquireQuantizeInfo(image_info);
2333 quantize_info->number_colors=2;
2334 quantize_info->colorspace=GRAYColorspace;
2335 status=QuantizeImage(quantize_info,image,exception);
2336 quantize_info=DestroyQuantizeInfo(quantize_info);
2337 image->alpha_trait=UndefinedPixelTrait;
2342 if (IsGrayImageType(image->type) == MagickFalse)
2343 status=TransformImageColorspace(image,GRAYColorspace,exception);
2344 image->alpha_trait=UndefinedPixelTrait;
2347 case GrayscaleAlphaType:
2349 if (IsGrayImageType(image->type) == MagickFalse)
2350 status=TransformImageColorspace(image,GRAYColorspace,exception);
2351 if ((image->alpha_trait & BlendPixelTrait) == 0)
2352 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2357 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2358 status=TransformImageColorspace(image,sRGBColorspace,exception);
2359 if ((image->storage_class == DirectClass) || (image->colors > 256))
2361 quantize_info=AcquireQuantizeInfo(image_info);
2362 quantize_info->number_colors=256;
2363 status=QuantizeImage(quantize_info,image,exception);
2364 quantize_info=DestroyQuantizeInfo(quantize_info);
2366 image->alpha_trait=UndefinedPixelTrait;
2369 case PaletteBilevelAlphaType:
2374 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2375 status=TransformImageColorspace(image,sRGBColorspace,exception);
2376 if ((image->alpha_trait & BlendPixelTrait) == 0)
2377 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2378 channel_mask=SetImageChannelMask(image,AlphaChannel);
2379 (void) BilevelImage(image,(
double) QuantumRange/2.0,exception);
2380 (void) SetImageChannelMask(image,channel_mask);
2381 quantize_info=AcquireQuantizeInfo(image_info);
2382 status=QuantizeImage(quantize_info,image,exception);
2383 quantize_info=DestroyQuantizeInfo(quantize_info);
2386 case PaletteAlphaType:
2388 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2389 status=TransformImageColorspace(image,sRGBColorspace,exception);
2390 if ((image->alpha_trait & BlendPixelTrait) == 0)
2391 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2392 quantize_info=AcquireQuantizeInfo(image_info);
2393 status=QuantizeImage(quantize_info,image,exception);
2394 quantize_info=DestroyQuantizeInfo(quantize_info);
2399 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2400 status=TransformImageColorspace(image,sRGBColorspace,exception);
2401 if (image->storage_class != DirectClass)
2402 status=SetImageStorageClass(image,DirectClass,exception);
2403 image->alpha_trait=UndefinedPixelTrait;
2406 case TrueColorAlphaType:
2408 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2409 status=TransformImageColorspace(image,sRGBColorspace,exception);
2410 if (image->storage_class != DirectClass)
2411 status=SetImageStorageClass(image,DirectClass,exception);
2412 if ((image->alpha_trait & BlendPixelTrait) == 0)
2413 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2416 case ColorSeparationType:
2418 if (image->colorspace != CMYKColorspace)
2419 status=TransformImageColorspace(image,CMYKColorspace,exception);
2420 if (image->storage_class != DirectClass)
2421 status=SetImageStorageClass(image,DirectClass,exception);
2422 image->alpha_trait=UndefinedPixelTrait;
2425 case ColorSeparationAlphaType:
2427 if (image->colorspace != CMYKColorspace)
2428 status=TransformImageColorspace(image,CMYKColorspace,exception);
2429 if (image->storage_class != DirectClass)
2430 status=SetImageStorageClass(image,DirectClass,exception);
2431 if ((image->alpha_trait & BlendPixelTrait) == 0)
2432 status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2439 image_info=DestroyImageInfo(image_info);
2440 if (status == MagickFalse)