MagickCore 6.9.12-98
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 "magick/studio.h"
44#include "magick/artifact.h"
45#include "magick/attribute.h"
46#include "magick/blob.h"
47#include "magick/blob-private.h"
48#include "magick/cache.h"
49#include "magick/cache-private.h"
50#include "magick/cache-view.h"
51#include "magick/client.h"
52#include "magick/channel.h"
53#include "magick/color.h"
54#include "magick/color-private.h"
55#include "magick/colormap.h"
56#include "magick/colormap-private.h"
57#include "magick/colorspace.h"
58#include "magick/colorspace-private.h"
59#include "magick/composite.h"
60#include "magick/composite-private.h"
61#include "magick/constitute.h"
62#include "magick/deprecate.h"
63#include "magick/draw.h"
64#include "magick/draw-private.h"
65#include "magick/effect.h"
66#include "magick/enhance.h"
67#include "magick/exception.h"
68#include "magick/exception-private.h"
69#include "magick/geometry.h"
70#include "magick/histogram.h"
71#include "magick/identify.h"
72#include "magick/image.h"
73#include "magick/image-private.h"
74#include "magick/list.h"
75#include "magick/log.h"
76#include "magick/memory_.h"
77#include "magick/magick.h"
78#include "magick/monitor.h"
79#include "magick/monitor-private.h"
80#include "magick/option.h"
81#include "magick/paint.h"
82#include "magick/pixel.h"
83#include "magick/pixel-private.h"
84#include "magick/property.h"
85#include "magick/quantize.h"
86#include "magick/random_.h"
87#include "magick/resource_.h"
88#include "magick/semaphore.h"
89#include "magick/segment.h"
90#include "magick/splay-tree.h"
91#include "magick/string_.h"
92#include "magick/string-private.h"
93#include "magick/thread-private.h"
94#include "magick/threshold.h"
95#include "magick/transform.h"
96#include "magick/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 _EdgeInfo
128{
129 double
130 left,
131 right,
132 top,
133 bottom;
134} EdgeInfo;
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 PixelPacket
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 PixelPacket *) NULL)
201 return(0.0);
202 GetMagickPixelPacket(image,&background);
203 SetMagickPixelPacket(image,p,(IndexPacket *) NULL,&background);
204 artifact=GetImageArtifact(image,"background");
205 if (artifact != (const char *) NULL)
206 (void) QueryMagickColor(artifact,&background,exception);
207 artifact=GetImageArtifact(image,"trim:background-color");
208 if (artifact != (const char *) NULL)
209 (void) QueryMagickColor(artifact,&background,exception);
210 edge_geometry.width=width;
211 edge_geometry.height=height;
212 edge_geometry.x=x_offset;
213 edge_geometry.y=y_offset;
214 GravityAdjustGeometry(image->columns,image->rows,gravity,&edge_geometry);
215 edge_image=CropImage(image,&edge_geometry,exception);
216 if (edge_image == (Image *) NULL)
217 return(0.0);
218 census=0.0;
219 GetMagickPixelPacket(edge_image,&pixel);
220 edge_view=AcquireVirtualCacheView(edge_image,exception);
221 for (y=0; y < (ssize_t) edge_image->rows; y++)
222 {
223 ssize_t
224 x;
225
226 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
227 if (p == (const PixelPacket *) NULL)
228 break;
229 for (x=0; x < (ssize_t) edge_image->columns; x++)
230 {
231 SetMagickPixelPacket(edge_image,p,(IndexPacket *) NULL,&pixel);
232 if (IsMagickColorSimilar(&pixel,&background) == MagickFalse)
233 census++;
234 p++;
235 }
236 }
237 census/=((double) edge_image->columns*edge_image->rows);
238 edge_view=DestroyCacheView(edge_view);
239 edge_image=DestroyImage(edge_image);
240 return(census);
241}
242
243static inline double GetMinEdgeBackgroundCensus(const EdgeInfo *edge)
244{
245 double
246 census;
247
248 census=MagickMin(MagickMin(MagickMin(edge->left,edge->right),edge->top),
249 edge->bottom);
250 return(census);
251}
252
253static RectangleInfo GetEdgeBoundingBox(const Image *image,
254 ExceptionInfo *exception)
255{
257 *edge_view;
258
259 const char
260 *artifact;
261
262 double
263 background_census,
264 percent_background;
265
267 edge,
268 vertex;
269
270 Image
271 *edge_image;
272
274 bounds;
275
276 /*
277 Get the image bounding box.
278 */
279 assert(image != (Image *) NULL);
280 assert(image->signature == MagickCoreSignature);
281 if (IsEventLogging() != MagickFalse)
282 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
283 SetGeometry(image,&bounds);
284 edge_image=CloneImage(image,0,0,MagickTrue,exception);
285 if (edge_image == (Image *) NULL)
286 return(bounds);
287 (void) ParseAbsoluteGeometry("0x0+0+0",&edge_image->page);
288 (void) memset(&vertex,0,sizeof(vertex));
289 edge_view=AcquireVirtualCacheView(edge_image,exception);
290 edge.left=GetEdgeBackgroundCensus(edge_image,edge_view,WestGravity,
291 1,0,0,0,exception);
292 edge.right=GetEdgeBackgroundCensus(edge_image,edge_view,EastGravity,
293 1,0,0,0,exception);
294 edge.top=GetEdgeBackgroundCensus(edge_image,edge_view,NorthGravity,
295 0,1,0,0,exception);
296 edge.bottom=GetEdgeBackgroundCensus(edge_image,edge_view,SouthGravity,
297 0,1,0,0,exception);
298 percent_background=1.0;
299 artifact=GetImageArtifact(edge_image,"trim:percent-background");
300 if (artifact != (const char *) NULL)
301 percent_background=StringToDouble(artifact,(char **) NULL)/100.0;
302 percent_background=MagickMin(MagickMax(1.0-percent_background,MagickEpsilon),
303 1.0);
304 background_census=GetMinEdgeBackgroundCensus(&edge);
305 for ( ; background_census < percent_background;
306 background_census=GetMinEdgeBackgroundCensus(&edge))
307 {
308 if ((bounds.width == 0) || (bounds.height == 0))
309 break;
310 if (fabs(edge.left-background_census) < MagickEpsilon)
311 {
312 /*
313 Trim left edge.
314 */
315 vertex.left++;
316 bounds.width--;
317 edge.left=GetEdgeBackgroundCensus(edge_image,edge_view,
318 NorthWestGravity,1,bounds.height,(ssize_t) vertex.left,(ssize_t)
319 vertex.top,exception);
320 edge.top=GetEdgeBackgroundCensus(edge_image,edge_view,
321 NorthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
322 vertex.top,exception);
323 edge.bottom=GetEdgeBackgroundCensus(edge_image,edge_view,
324 SouthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
325 vertex.bottom,exception);
326 continue;
327 }
328 if (fabs(edge.right-background_census) < MagickEpsilon)
329 {
330 /*
331 Trim right edge.
332 */
333 vertex.right++;
334 bounds.width--;
335 edge.right=GetEdgeBackgroundCensus(edge_image,edge_view,
336 NorthEastGravity,1,bounds.height,(ssize_t) vertex.right,(ssize_t)
337 vertex.top,exception);
338 edge.top=GetEdgeBackgroundCensus(edge_image,edge_view,
339 NorthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
340 vertex.top,exception);
341 edge.bottom=GetEdgeBackgroundCensus(edge_image,edge_view,
342 SouthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
343 vertex.bottom,exception);
344 continue;
345 }
346 if (fabs(edge.top-background_census) < MagickEpsilon)
347 {
348 /*
349 Trim top edge.
350 */
351 vertex.top++;
352 bounds.height--;
353 edge.left=GetEdgeBackgroundCensus(edge_image,edge_view,
354 NorthWestGravity,1,bounds.height,(ssize_t) vertex.left,(ssize_t)
355 vertex.top,exception);
356 edge.right=GetEdgeBackgroundCensus(edge_image,edge_view,
357 NorthEastGravity,1,bounds.height,(ssize_t) vertex.right,(ssize_t)
358 vertex.top,exception);
359 edge.top=GetEdgeBackgroundCensus(edge_image,edge_view,
360 NorthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
361 vertex.top,exception);
362 continue;
363 }
364 if (fabs(edge.bottom-background_census) < MagickEpsilon)
365 {
366 /*
367 Trim bottom edge.
368 */
369 vertex.bottom++;
370 bounds.height--;
371 edge.left=GetEdgeBackgroundCensus(edge_image,edge_view,
372 NorthWestGravity,1,bounds.height,(ssize_t) vertex.left,(ssize_t)
373 vertex.top,exception);
374 edge.right=GetEdgeBackgroundCensus(edge_image,edge_view,
375 NorthEastGravity,1,bounds.height,(ssize_t) vertex.right,(ssize_t)
376 vertex.top,exception);
377 edge.bottom=GetEdgeBackgroundCensus(edge_image,edge_view,
378 SouthWestGravity,bounds.width,1,(ssize_t) vertex.left,(ssize_t)
379 vertex.bottom,exception);
380 continue;
381 }
382 }
383 edge_view=DestroyCacheView(edge_view);
384 edge_image=DestroyImage(edge_image);
385 bounds.x=(ssize_t) vertex.left;
386 bounds.y=(ssize_t) vertex.top;
387 if ((bounds.width == 0) || (bounds.height == 0))
388 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
389 "GeometryDoesNotContainImage","`%s'",image->filename);
390 return(bounds);
391}
392
393MagickExport RectangleInfo GetImageBoundingBox(const Image *image,
394 ExceptionInfo *exception)
395{
397 *image_view;
398
399 const char
400 *artifact;
401
402 MagickBooleanType
403 status;
404
406 target[4],
407 zero;
408
410 bounds;
411
412 const PixelPacket
413 *p;
414
415 ssize_t
416 y;
417
418 assert(image != (Image *) NULL);
419 assert(image->signature == MagickCoreSignature);
420 if (IsEventLogging() != MagickFalse)
421 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
422 artifact=GetImageArtifact(image,"trim:percent-background");
423 if (artifact != (const char *) NULL)
424 return(GetEdgeBoundingBox(image,exception));
425 bounds.width=image->columns == 1 ? 1 : 0;
426 bounds.height=image->rows == 1 ? 1 : 0;
427 bounds.x=(ssize_t) image->columns;
428 bounds.y=(ssize_t) image->rows;
429 GetMagickPixelPacket(image,&target[0]);
430 image_view=AcquireVirtualCacheView(image,exception);
431 p=GetCacheViewVirtualPixels(image_view,0,0,1,1,exception);
432 if (p == (const PixelPacket *) NULL)
433 {
434 image_view=DestroyCacheView(image_view);
435 return(bounds);
436 }
437 SetMagickPixelPacket(image,p,GetCacheViewVirtualIndexQueue(image_view),
438 &target[0]);
439 GetMagickPixelPacket(image,&target[1]);
440 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,0,1,1,
441 exception);
442 if (p != (const PixelPacket *) NULL)
443 SetMagickPixelPacket(image,p,GetCacheViewVirtualIndexQueue(image_view),
444 &target[1]);
445 GetMagickPixelPacket(image,&target[2]);
446 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-1,1,1,
447 exception);
448 if (p != (const PixelPacket *) NULL)
449 SetMagickPixelPacket(image,p,GetCacheViewVirtualIndexQueue(image_view),
450 &target[2]);
451 GetMagickPixelPacket(image,&target[3]);
452 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,
453 (ssize_t) image->rows-1,1,1,exception);
454 if (p != (const PixelPacket *) NULL)
455 SetMagickPixelPacket(image,p,GetCacheViewVirtualIndexQueue(image_view),
456 &target[3]);
457 status=MagickTrue;
458 GetMagickPixelPacket(image,&zero);
459#if defined(MAGICKCORE_OPENMP_SUPPORT)
460 #pragma omp parallel for schedule(static) shared(status) \
461 magick_number_threads(image,image,image->rows,1)
462#endif
463 for (y=0; y < (ssize_t) image->rows; y++)
464 {
466 pixel;
467
469 bounding_box;
470
471 const IndexPacket
472 *magick_restrict indexes;
473
474 const PixelPacket
475 *magick_restrict p;
476
477 ssize_t
478 x;
479
480 if (status == MagickFalse)
481 continue;
482#if defined(MAGICKCORE_OPENMP_SUPPORT)
483# pragma omp critical (MagickCore_GetImageBoundingBox)
484#endif
485 bounding_box=bounds;
486 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
487 if (p == (const PixelPacket *) NULL)
488 {
489 status=MagickFalse;
490 continue;
491 }
492 indexes=GetCacheViewVirtualIndexQueue(image_view);
493 pixel=zero;
494 for (x=0; x < (ssize_t) image->columns; x++)
495 {
496 SetMagickPixelPacket(image,p,indexes+x,&pixel);
497 if ((x < bounding_box.x) &&
498 (IsMagickColorSimilar(&pixel,&target[0]) == MagickFalse))
499 bounding_box.x=x;
500 if ((x > (ssize_t) bounding_box.width) &&
501 (IsMagickColorSimilar(&pixel,&target[1]) == MagickFalse))
502 bounding_box.width=(size_t) x;
503 if ((y < bounding_box.y) &&
504 (IsMagickColorSimilar(&pixel,&target[0]) == MagickFalse))
505 bounding_box.y=y;
506 if ((y > (ssize_t) bounding_box.height) &&
507 (IsMagickColorSimilar(&pixel,&target[2]) == MagickFalse))
508 bounding_box.height=(size_t) y;
509 if ((x < (ssize_t) bounding_box.width) &&
510 (y > (ssize_t) bounding_box.height) &&
511 (IsMagickColorSimilar(&pixel,&target[3]) == MagickFalse))
512 {
513 bounding_box.width=(size_t) x;
514 bounding_box.height=(size_t) y;
515 }
516 p++;
517 }
518#if defined(MAGICKCORE_OPENMP_SUPPORT)
519# pragma omp critical (MagickCore_GetImageBoundingBox)
520#endif
521 {
522 if (bounding_box.x < bounds.x)
523 bounds.x=bounding_box.x;
524 if (bounding_box.y < bounds.y)
525 bounds.y=bounding_box.y;
526 if (bounding_box.width > bounds.width)
527 bounds.width=bounding_box.width;
528 if (bounding_box.height > bounds.height)
529 bounds.height=bounding_box.height;
530 }
531 }
532 image_view=DestroyCacheView(image_view);
533 if ((bounds.width == 0) || (bounds.height == 0))
534 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
535 "GeometryDoesNotContainImage","`%s'",image->filename);
536 else
537 {
538 bounds.width-=(size_t) (bounds.x-1);
539 bounds.height-=(size_t) (bounds.y-1);
540 }
541 return(bounds);
542}
543
544/*
545%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
546% %
547% %
548% %
549% G e t I m a g e C h a n n e l D e p t h %
550% %
551% %
552% %
553%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554%
555% GetImageChannelDepth() returns the depth of a particular image channel.
556%
557% The format of the GetImageChannelDepth method is:
558%
559% size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
560% size_t GetImageChannelDepth(const Image *image,
561% const ChannelType channel,ExceptionInfo *exception)
562%
563% A description of each parameter follows:
564%
565% o image: the image.
566%
567% o channel: the channel.
568%
569% o exception: return any errors or warnings in this structure.
570%
571*/
572MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
573{
574 return(GetImageChannelDepth(image,CompositeChannels,exception));
575}
576
577MagickExport size_t GetImageChannelDepth(const Image *image,
578 const ChannelType channel,ExceptionInfo *exception)
579{
581 *image_view;
582
583 MagickBooleanType
584 status;
585
586 ssize_t
587 i;
588
589 size_t
590 *current_depth,
591 depth,
592 number_threads;
593
594 ssize_t
595 y;
596
597 /*
598 Compute image depth.
599 */
600 assert(image != (Image *) NULL);
601
602 assert(image->signature == MagickCoreSignature);
603 if (IsEventLogging() != MagickFalse)
604 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
605 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
606 current_depth=(size_t *) AcquireQuantumMemory(number_threads,
607 sizeof(*current_depth));
608 if (current_depth == (size_t *) NULL)
609 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
610 status=MagickTrue;
611 for (i=0; i < (ssize_t) number_threads; i++)
612 current_depth[i]=1;
613 if ((image->storage_class == PseudoClass) && (image->matte == MagickFalse))
614 {
615#if defined(MAGICKCORE_OPENMP_SUPPORT)
616 #pragma omp parallel for schedule(static) shared(status) \
617 magick_number_threads(image,image,image->colors,1)
618#endif
619 for (i=0; i < (ssize_t) image->colors; i++)
620 {
621 const int
622 id = GetOpenMPThreadId();
623
624 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
625 {
626 MagickBooleanType
627 atDepth;
628
629 QuantumAny
630 range;
631
632 atDepth=MagickTrue;
633 range=GetQuantumRange(current_depth[id]);
634 if ((channel & RedChannel) != 0)
635 if (IsPixelAtDepth(image->colormap[i].red,range) == MagickFalse)
636 atDepth=MagickFalse;
637 if ((atDepth != MagickFalse) && ((channel & GreenChannel) != 0))
638 if (IsPixelAtDepth(image->colormap[i].green,range) == MagickFalse)
639 atDepth=MagickFalse;
640 if ((atDepth != MagickFalse) && ((channel & BlueChannel) != 0))
641 if (IsPixelAtDepth(image->colormap[i].blue,range) == MagickFalse)
642 atDepth=MagickFalse;
643 if ((atDepth != MagickFalse))
644 break;
645 current_depth[id]++;
646 }
647 }
648 depth=current_depth[0];
649 for (i=1; i < (ssize_t) number_threads; i++)
650 if (depth < current_depth[i])
651 depth=current_depth[i];
652 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
653 return(depth);
654 }
655 image_view=AcquireVirtualCacheView(image,exception);
656#if !defined(MAGICKCORE_HDRI_SUPPORT)
657DisableMSCWarning(4127)
658 if (1UL*QuantumRange <= MaxMap)
659RestoreMSCWarning
660 {
661 size_t
662 *depth_map;
663
664 /*
665 Scale pixels to desired (optimized with depth map).
666 */
667 depth_map=(size_t *) AcquireQuantumMemory(MaxMap+1,sizeof(*depth_map));
668 if (depth_map == (size_t *) NULL)
669 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
670 for (i=0; i <= (ssize_t) MaxMap; i++)
671 {
672 unsigned int
673 depth;
674
675 for (depth=1; depth < MAGICKCORE_QUANTUM_DEPTH; depth++)
676 {
677 Quantum
678 pixel;
679
680 QuantumAny
681 range;
682
683 range=GetQuantumRange(depth);
684 pixel=(Quantum) i;
685 if (pixel == ScaleAnyToQuantum(ScaleQuantumToAny(pixel,range),range))
686 break;
687 }
688 depth_map[i]=depth;
689 }
690#if defined(MAGICKCORE_OPENMP_SUPPORT)
691 #pragma omp parallel for schedule(static) shared(status) \
692 magick_number_threads(image,image,image->rows,1)
693#endif
694 for (y=0; y < (ssize_t) image->rows; y++)
695 {
696 const int
697 id = GetOpenMPThreadId();
698
699 const IndexPacket
700 *magick_restrict indexes;
701
702 const PixelPacket
703 *magick_restrict p;
704
705 ssize_t
706 x;
707
708 if (status == MagickFalse)
709 continue;
710 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
711 if (p == (const PixelPacket *) NULL)
712 continue;
713 indexes=GetCacheViewVirtualIndexQueue(image_view);
714 for (x=0; x < (ssize_t) image->columns; x++)
715 {
716 Quantum
717 pixel;
718
719 if ((channel & RedChannel) != 0)
720 {
721 pixel=GetPixelRed(p);
722 if (depth_map[ScaleQuantumToMap(pixel)] > current_depth[id])
723 current_depth[id]=depth_map[ScaleQuantumToMap(pixel)];
724 }
725 if ((channel & GreenChannel) != 0)
726 {
727 pixel=GetPixelGreen(p);
728 if (depth_map[ScaleQuantumToMap(pixel)] > current_depth[id])
729 current_depth[id]=depth_map[ScaleQuantumToMap(pixel)];
730 }
731 if ((channel & BlueChannel) != 0)
732 {
733 pixel=GetPixelBlue(p);
734 if (depth_map[ScaleQuantumToMap(pixel)] > current_depth[id])
735 current_depth[id]=depth_map[ScaleQuantumToMap(pixel)];
736 }
737 if (((channel & OpacityChannel) != 0) &&
738 (image->matte != MagickFalse))
739 {
740 pixel=GetPixelOpacity(p);
741 if (depth_map[ScaleQuantumToMap(pixel)] > current_depth[id])
742 current_depth[id]=depth_map[ScaleQuantumToMap(pixel)];
743 }
744 if (((channel & IndexChannel) != 0) &&
745 (image->colorspace == CMYKColorspace))
746 {
747 pixel=GetPixelIndex(indexes+x);
748 if (depth_map[ScaleQuantumToMap(pixel)] > current_depth[id])
749 current_depth[id]=depth_map[ScaleQuantumToMap(pixel)];
750 }
751 p++;
752 }
753 if (current_depth[id] == MAGICKCORE_QUANTUM_DEPTH)
754 status=MagickFalse;
755 }
756 image_view=DestroyCacheView(image_view);
757 depth=current_depth[0];
758 for (i=1; i < (ssize_t) number_threads; i++)
759 if (depth < current_depth[i])
760 depth=current_depth[i];
761 depth_map=(size_t *) RelinquishMagickMemory(depth_map);
762 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
763 return(depth);
764 }
765#endif
766#if defined(MAGICKCORE_OPENMP_SUPPORT)
767 #pragma omp parallel for schedule(static) shared(status) \
768 magick_number_threads(image,image,image->rows,1)
769#endif
770 for (y=0; y < (ssize_t) image->rows; y++)
771 {
772 const int
773 id = GetOpenMPThreadId();
774
775 const IndexPacket
776 *magick_restrict indexes;
777
778 const PixelPacket
779 *magick_restrict p;
780
781 ssize_t
782 x;
783
784 if (status == MagickFalse)
785 continue;
786 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
787 if (p == (const PixelPacket *) NULL)
788 continue;
789 indexes=GetCacheViewVirtualIndexQueue(image_view);
790 for (x=0; x < (ssize_t) image->columns; x++)
791 {
792 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
793 {
794 MagickBooleanType
795 atDepth;
796
797 QuantumAny
798 range;
799
800 atDepth=MagickTrue;
801 range=GetQuantumRange(current_depth[id]);
802 if ((atDepth != MagickFalse) && ((channel & RedChannel) != 0))
803 if (IsPixelAtDepth(GetPixelRed(p),range) == MagickFalse)
804 atDepth=MagickFalse;
805 if ((atDepth != MagickFalse) && ((channel & GreenChannel) != 0))
806 if (IsPixelAtDepth(GetPixelGreen(p),range) == MagickFalse)
807 atDepth=MagickFalse;
808 if ((atDepth != MagickFalse) && ((channel & BlueChannel) != 0))
809 if (IsPixelAtDepth(GetPixelBlue(p),range) == MagickFalse)
810 atDepth=MagickFalse;
811 if ((atDepth != MagickFalse) && ((channel & OpacityChannel) != 0) &&
812 (image->matte != MagickFalse))
813 if (IsPixelAtDepth(GetPixelOpacity(p),range) == MagickFalse)
814 atDepth=MagickTrue;
815 if ((atDepth != MagickFalse) && ((channel & IndexChannel) != 0) &&
816 (image->colorspace == CMYKColorspace))
817 if (IsPixelAtDepth(GetPixelIndex(indexes+x),range) == MagickFalse)
818 atDepth=MagickFalse;
819 if ((atDepth != MagickFalse))
820 break;
821 current_depth[id]++;
822 }
823 p++;
824 }
825 if (current_depth[id] == MAGICKCORE_QUANTUM_DEPTH)
826 status=MagickFalse;
827 }
828 image_view=DestroyCacheView(image_view);
829 depth=current_depth[0];
830 for (i=1; i < (ssize_t) number_threads; i++)
831 if (depth < current_depth[i])
832 depth=current_depth[i];
833 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
834 return(depth);
835}
836
837/*
838%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839% %
840% %
841% %
842% G e t I m a g e Q u a n t u m D e p t h %
843% %
844% %
845% %
846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847%
848% GetImageQuantumDepth() returns the depth of the image rounded to a legal
849% quantum depth: 8, 16, or 32.
850%
851% The format of the GetImageQuantumDepth method is:
852%
853% size_t GetImageQuantumDepth(const Image *image,
854% const MagickBooleanType constrain)
855%
856% A description of each parameter follows:
857%
858% o image: the image.
859%
860% o constrain: A value other than MagickFalse, constrains the depth to
861% a maximum of MAGICKCORE_QUANTUM_DEPTH.
862%
863*/
864MagickExport size_t GetImageQuantumDepth(const Image *image,
865 const MagickBooleanType constrain)
866{
867 size_t
868 depth;
869
870 depth=image->depth;
871 if (depth <= 8)
872 depth=8;
873 else
874 if (depth <= 16)
875 depth=16;
876 else
877 if (depth <= 32)
878 depth=32;
879 else
880 if (depth <= 64)
881 depth=64;
882 if (constrain != MagickFalse)
883 depth=(size_t) MagickMin((double) depth,(double) MAGICKCORE_QUANTUM_DEPTH);
884 return(depth);
885}
886
887/*
888%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889% %
890% %
891% %
892% G e t I m a g e T y p e %
893% %
894% %
895% %
896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897%
898% GetImageType() returns the potential type of image:
899%
900% Bilevel Grayscale GrayscaleMatte
901% Palette PaletteMatte TrueColor
902% TrueColorMatte ColorSeparation ColorSeparationMatte
903%
904% To ensure the image type matches its potential, use SetImageType():
905%
906% (void) SetImageType(image,GetImageType(image));
907%
908% The format of the GetImageType method is:
909%
910% ImageType GetImageType(const Image *image,ExceptionInfo *exception)
911%
912% A description of each parameter follows:
913%
914% o image: the image.
915%
916% o exception: return any errors or warnings in this structure.
917%
918*/
919MagickExport ImageType GetImageType(const Image *image,ExceptionInfo *exception)
920{
921 assert(image != (Image *) NULL);
922 assert(image->signature == MagickCoreSignature);
923 if (IsEventLogging() != MagickFalse)
924 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
925 if (image->colorspace == CMYKColorspace)
926 {
927 if (image->matte == MagickFalse)
928 return(ColorSeparationType);
929 return(ColorSeparationMatteType);
930 }
931 if (IsMonochromeImage(image,exception) != MagickFalse)
932 return(BilevelType);
933 if (IsGrayImage(image,exception) != MagickFalse)
934 {
935 if (image->matte != MagickFalse)
936 return(GrayscaleMatteType);
937 return(GrayscaleType);
938 }
939 if (IsPaletteImage(image,exception) != MagickFalse)
940 {
941 if (image->matte != MagickFalse)
942 return(PaletteMatteType);
943 return(PaletteType);
944 }
945 if (image->matte != MagickFalse)
946 return(TrueColorMatteType);
947 return(TrueColorType);
948}
949
950/*
951%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
952% %
953% %
954% %
955% I d e n t i f y I m a g e G r a y %
956% %
957% %
958% %
959%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
960%
961% either 0 or QuantumRange. Otherwise undefined is returned.
962%
963% The format of the IdentifyImageGray method is:
964%
965% ImageType IdentifyImageGray(const Image *image,ExceptionInfo *exception)
966%
967% A description of each parameter follows:
968%
969% o image: the image.
970%
971% o exception: return any errors or warnings in this structure.
972%
973*/
974MagickExport ImageType IdentifyImageGray(const Image *image,
975 ExceptionInfo *exception)
976{
978 *image_view;
979
980 ImageType
981 type;
982
983 const PixelPacket
984 *p;
985
986 ssize_t
987 x;
988
989 ssize_t
990 y;
991
992 assert(image != (Image *) NULL);
993 assert(image->signature == MagickCoreSignature);
994 if (IsEventLogging() != MagickFalse)
995 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
996 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
997 (image->type == GrayscaleMatteType))
998 return(image->type);
999 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1000 return(UndefinedType);
1001 type=BilevelType;
1002 image_view=AcquireVirtualCacheView(image,exception);
1003 for (y=0; y < (ssize_t) image->rows; y++)
1004 {
1005 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1006 if (p == (const PixelPacket *) NULL)
1007 break;
1008 for (x=0; x < (ssize_t) image->columns; x++)
1009 {
1010 if (IsPixelGray(p) == MagickFalse)
1011 {
1012 type=UndefinedType;
1013 break;
1014 }
1015 if ((type == BilevelType) && (IsPixelMonochrome(p) == MagickFalse))
1016 type=GrayscaleType;
1017 p++;
1018 }
1019 if (type == UndefinedType)
1020 break;
1021 }
1022 image_view=DestroyCacheView(image_view);
1023 if ((type == GrayscaleType) && (image->matte != MagickFalse))
1024 type=GrayscaleMatteType;
1025 return(type);
1026}
1027
1028/*
1029%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1030% %
1031% %
1032% %
1033% I d e n t i f y I m a g e M o n o c h r o m e %
1034% %
1035% %
1036% %
1037%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038%
1039% IdentifyImageMonochrome() returns MagickTrue if all the pixels in the image
1040% have the same red, green, and blue intensities and the intensity is either
1041% 0 or QuantumRange.
1042%
1043% The format of the IdentifyImageMonochrome method is:
1044%
1045% MagickBooleanType IdentifyImageMonochrome(const Image *image,
1046% ExceptionInfo *exception)
1047%
1048% A description of each parameter follows:
1049%
1050% o image: the image.
1051%
1052% o exception: return any errors or warnings in this structure.
1053%
1054*/
1055MagickExport MagickBooleanType IdentifyImageMonochrome(const Image *image,
1056 ExceptionInfo *exception)
1057{
1058 CacheView
1059 *image_view;
1060
1061 ImageType
1062 type;
1063
1064 ssize_t
1065 x;
1066
1067 const PixelPacket
1068 *p;
1069
1070 ssize_t
1071 y;
1072
1073 assert(image != (Image *) NULL);
1074 assert(image->signature == MagickCoreSignature);
1075 if (IsEventLogging() != MagickFalse)
1076 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1077 if (image->type == BilevelType)
1078 return(MagickTrue);
1079 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1080 return(MagickFalse);
1081 type=BilevelType;
1082 image_view=AcquireVirtualCacheView(image,exception);
1083 for (y=0; y < (ssize_t) image->rows; y++)
1084 {
1085 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1086 if (p == (const PixelPacket *) NULL)
1087 break;
1088 for (x=0; x < (ssize_t) image->columns; x++)
1089 {
1090 if (IsPixelMonochrome(p) == MagickFalse)
1091 {
1092 type=UndefinedType;
1093 break;
1094 }
1095 p++;
1096 }
1097 if (type == UndefinedType)
1098 break;
1099 }
1100 image_view=DestroyCacheView(image_view);
1101 if (type == BilevelType)
1102 return(MagickTrue);
1103 return(MagickFalse);
1104}
1105
1106/*
1107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1108% %
1109% %
1110% %
1111% I d e n t i f y I m a g e T y p e %
1112% %
1113% %
1114% %
1115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1116%
1117% IdentifyImageType() returns the potential type of image:
1118%
1119% Bilevel Grayscale GrayscaleMatte
1120% Palette PaletteMatte TrueColor
1121% TrueColorMatte ColorSeparation ColorSeparationMatte
1122%
1123% To ensure the image type matches its potential, use SetImageType():
1124%
1125% (void) SetImageType(image,IdentifyImageType(image,exception),exception);
1126%
1127% The format of the IdentifyImageType method is:
1128%
1129% ImageType IdentifyImageType(const Image *image,ExceptionInfo *exception)
1130%
1131% A description of each parameter follows:
1132%
1133% o image: the image.
1134%
1135% o exception: return any errors or warnings in this structure.
1136%
1137*/
1138MagickExport ImageType IdentifyImageType(const Image *image,
1139 ExceptionInfo *exception)
1140{
1141 assert(image != (Image *) NULL);
1142 assert(image->signature == MagickCoreSignature);
1143 if (IsEventLogging() != MagickFalse)
1144 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1145 if (image->colorspace == CMYKColorspace)
1146 {
1147 if (image->matte == MagickFalse)
1148 return(ColorSeparationType);
1149 return(ColorSeparationMatteType);
1150 }
1151 if (IdentifyImageMonochrome(image,exception) != MagickFalse)
1152 return(BilevelType);
1153 if (IdentifyImageGray(image,exception) != UndefinedType)
1154 {
1155 if (image->matte != MagickFalse)
1156 return(GrayscaleMatteType);
1157 return(GrayscaleType);
1158 }
1159 if (IdentifyPaletteImage(image,exception) != MagickFalse)
1160 {
1161 if (image->matte != MagickFalse)
1162 return(PaletteMatteType);
1163 return(PaletteType);
1164 }
1165 if (image->matte != MagickFalse)
1166 return(TrueColorMatteType);
1167 return(TrueColorType);
1168}
1169
1170/*
1171%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172% %
1173% %
1174% %
1175% I s G r a y I m a g e %
1176% %
1177% %
1178% %
1179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180%
1181% IsGrayImage() returns MagickTrue if the type of the image is grayscale or
1182% bi-level.
1183%
1184% The format of the IsGrayImage method is:
1185%
1186% MagickBooleanType IsGrayImage(const Image *image,
1187% ExceptionInfo *exception)
1188%
1189% A description of each parameter follows:
1190%
1191% o image: the image.
1192%
1193% o exception: return any errors or warnings in this structure.
1194%
1195*/
1196MagickExport MagickBooleanType IsGrayImage(const Image *image,
1197 ExceptionInfo *magick_unused(exception))
1198{
1199 assert(image != (Image *) NULL);
1200 assert(image->signature == MagickCoreSignature);
1201 magick_unreferenced(exception);
1202 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
1203 (image->type == GrayscaleMatteType))
1204 return(MagickTrue);
1205 return(MagickFalse);
1206}
1207
1208/*
1209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1210% %
1211% %
1212% %
1213% I s M o n o c h r o m e I m a g e %
1214% %
1215% %
1216% %
1217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1218%
1219% IsMonochromeImage() returns MagickTrue if type of the image is bi-level.
1220%
1221% The format of the IsMonochromeImage method is:
1222%
1223% MagickBooleanType IsMonochromeImage(const Image *image,
1224% ExceptionInfo *exception)
1225%
1226% A description of each parameter follows:
1227%
1228% o image: the image.
1229%
1230% o exception: return any errors or warnings in this structure.
1231%
1232*/
1233MagickExport MagickBooleanType IsMonochromeImage(const Image *image,
1234 ExceptionInfo *magick_unused(exception))
1235{
1236 assert(image != (Image *) NULL);
1237 assert(image->signature == MagickCoreSignature);
1238 magick_unreferenced(exception);
1239 if (image->type == BilevelType)
1240 return(MagickTrue);
1241 return(MagickFalse);
1242}
1243
1244/*
1245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1246% %
1247% %
1248% %
1249% I s O p a q u e I m a g e %
1250% %
1251% %
1252% %
1253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1254%
1255% IsOpaqueImage() returns MagickTrue if none of the pixels in the image have
1256% an opacity value other than opaque (0).
1257%
1258% The format of the IsOpaqueImage method is:
1259%
1260% MagickBooleanType IsOpaqueImage(const Image *image,
1261% ExceptionInfo *exception)
1262%
1263% A description of each parameter follows:
1264%
1265% o image: the image.
1266%
1267% o exception: return any errors or warnings in this structure.
1268%
1269*/
1270MagickExport MagickBooleanType IsOpaqueImage(const Image *image,
1271 ExceptionInfo *exception)
1272{
1273 CacheView
1274 *image_view;
1275
1276 const PixelPacket
1277 *p;
1278
1279 ssize_t
1280 x;
1281
1282 ssize_t
1283 y;
1284
1285 /*
1286 Determine if image is opaque.
1287 */
1288 assert(image != (Image *) NULL);
1289 assert(image->signature == MagickCoreSignature);
1290 if (IsEventLogging() != MagickFalse)
1291 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1292 if (image->matte == MagickFalse)
1293 return(MagickTrue);
1294 image_view=AcquireVirtualCacheView(image,exception);
1295 for (y=0; y < (ssize_t) image->rows; y++)
1296 {
1297 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1298 if (p == (const PixelPacket *) NULL)
1299 break;
1300 for (x=0; x < (ssize_t) image->columns; x++)
1301 {
1302 if (GetPixelOpacity(p) != OpaqueOpacity)
1303 break;
1304 p++;
1305 }
1306 if (x < (ssize_t) image->columns)
1307 break;
1308 }
1309 image_view=DestroyCacheView(image_view);
1310 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
1311}
1312
1313/*
1314%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1315% %
1316% %
1317% %
1318% S e t I m a g e C h a n n e l D e p t h %
1319% %
1320% %
1321% %
1322%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1323%
1324% SetImageChannelDepth() sets the depth of the image.
1325%
1326% The format of the SetImageChannelDepth method is:
1327%
1328% MagickBooleanType SetImageDepth(Image *image,const size_t depth)
1329% MagickBooleanType SetImageChannelDepth(Image *image,
1330% const ChannelType channel,const size_t depth)
1331%
1332% A description of each parameter follows:
1333%
1334% o image: the image.
1335%
1336% o channel: the channel.
1337%
1338% o depth: the image depth.
1339%
1340*/
1341
1342MagickExport MagickBooleanType SetImageDepth(Image *image,
1343 const size_t depth)
1344{
1345 return(SetImageChannelDepth(image,CompositeChannels,depth));
1346}
1347
1348MagickExport MagickBooleanType SetImageChannelDepth(Image *image,
1349 const ChannelType channel,const size_t depth)
1350{
1351 CacheView
1352 *image_view;
1353
1355 *exception;
1356
1357 MagickBooleanType
1358 status;
1359
1360 QuantumAny
1361 range;
1362
1363 ssize_t
1364 y;
1365
1366 assert(image != (Image *) NULL);
1367 if (IsEventLogging() != MagickFalse)
1368 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1369 assert(image->signature == MagickCoreSignature);
1370 if (depth >= MAGICKCORE_QUANTUM_DEPTH)
1371 {
1372 image->depth=depth;
1373 return(MagickTrue);
1374 }
1375 range=GetQuantumRange(depth);
1376 if (image->storage_class == PseudoClass)
1377 {
1378 ssize_t
1379 i;
1380
1381#if defined(MAGICKCORE_OPENMP_SUPPORT)
1382 #pragma omp parallel for schedule(static) shared(status) \
1383 magick_number_threads(image,image,image->rows,1)
1384#endif
1385 for (i=0; i < (ssize_t) image->colors; i++)
1386 {
1387 if ((channel & RedChannel) != 0)
1388 image->colormap[i].red=ScaleAnyToQuantum(ScaleQuantumToAny(
1389 ClampPixel((MagickRealType) image->colormap[i].red),range),range);
1390 if ((channel & GreenChannel) != 0)
1391 image->colormap[i].green=ScaleAnyToQuantum(ScaleQuantumToAny(
1392 ClampPixel((MagickRealType) image->colormap[i].green),range),range);
1393 if ((channel & BlueChannel) != 0)
1394 image->colormap[i].blue=ScaleAnyToQuantum(ScaleQuantumToAny(
1395 ClampPixel((MagickRealType) image->colormap[i].blue),range),range);
1396 if ((channel & OpacityChannel) != 0)
1397 image->colormap[i].opacity=ScaleAnyToQuantum(ScaleQuantumToAny(
1398 ClampPixel((MagickRealType) image->colormap[i].opacity),range),
1399 range);
1400 }
1401 }
1402 status=MagickTrue;
1403 exception=(&image->exception);
1404 image_view=AcquireAuthenticCacheView(image,exception);
1405#if !defined(MAGICKCORE_HDRI_SUPPORT)
1406DisableMSCWarning(4127)
1407 if (1UL*QuantumRange <= MaxMap)
1408RestoreMSCWarning
1409 {
1410 Quantum
1411 *depth_map;
1412
1413 ssize_t
1414 i;
1415
1416 /*
1417 Scale pixels to desired (optimized with depth map).
1418 */
1419 depth_map=(Quantum *) AcquireQuantumMemory(MaxMap+1,sizeof(*depth_map));
1420 if (depth_map == (Quantum *) NULL)
1421 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1422 for (i=0; i <= (ssize_t) MaxMap; i++)
1423 depth_map[i]=ScaleAnyToQuantum(ScaleQuantumToAny((Quantum) i,range),
1424 range);
1425#if defined(MAGICKCORE_OPENMP_SUPPORT)
1426 #pragma omp parallel for schedule(static) shared(status) \
1427 magick_number_threads(image,image,image->rows,1)
1428#endif
1429 for (y=0; y < (ssize_t) image->rows; y++)
1430 {
1432 *magick_restrict q;
1433
1434 ssize_t
1435 x;
1436
1437 if (status == MagickFalse)
1438 continue;
1439 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1440 exception);
1441 if (q == (PixelPacket *) NULL)
1442 {
1443 status=MagickFalse;
1444 continue;
1445 }
1446 for (x=0; x < (ssize_t) image->columns; x++)
1447 {
1448 if ((channel & RedChannel) != 0)
1449 SetPixelRed(q,depth_map[ScaleQuantumToMap(GetPixelRed(q))]);
1450 if ((channel & GreenChannel) != 0)
1451 SetPixelGreen(q,depth_map[ScaleQuantumToMap(GetPixelGreen(q))]);
1452 if ((channel & BlueChannel) != 0)
1453 SetPixelBlue(q,depth_map[ScaleQuantumToMap(GetPixelBlue(q))]);
1454 if (((channel & OpacityChannel) != 0) &&
1455 (image->matte != MagickFalse))
1456 SetPixelOpacity(q,depth_map[ScaleQuantumToMap(GetPixelOpacity(q))]);
1457 q++;
1458 }
1459 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1460 {
1461 status=MagickFalse;
1462 continue;
1463 }
1464 }
1465 image_view=DestroyCacheView(image_view);
1466 depth_map=(Quantum *) RelinquishMagickMemory(depth_map);
1467 if (status != MagickFalse)
1468 image->depth=depth;
1469 return(status);
1470 }
1471#endif
1472 /*
1473 Scale pixels to desired depth.
1474 */
1475#if defined(MAGICKCORE_OPENMP_SUPPORT)
1476 #pragma omp parallel for schedule(static) shared(status) \
1477 magick_number_threads(image,image,image->rows,1)
1478#endif
1479 for (y=0; y < (ssize_t) image->rows; y++)
1480 {
1482 *magick_restrict q;
1483
1484 ssize_t
1485 x;
1486
1487 if (status == MagickFalse)
1488 continue;
1489 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1490 if (q == (PixelPacket *) NULL)
1491 {
1492 status=MagickFalse;
1493 continue;
1494 }
1495 for (x=0; x < (ssize_t) image->columns; x++)
1496 {
1497 if ((channel & RedChannel) != 0)
1498 SetPixelRed(q,ScaleAnyToQuantum(ScaleQuantumToAny(ClampPixel(
1499 (MagickRealType) GetPixelRed(q)),range),range));
1500 if ((channel & GreenChannel) != 0)
1501 SetPixelGreen(q,ScaleAnyToQuantum(ScaleQuantumToAny(ClampPixel(
1502 (MagickRealType) GetPixelGreen(q)),range),range));
1503 if ((channel & BlueChannel) != 0)
1504 SetPixelBlue(q,ScaleAnyToQuantum(ScaleQuantumToAny(ClampPixel(
1505 (MagickRealType) GetPixelBlue(q)),range),range));
1506 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
1507 SetPixelOpacity(q,ScaleAnyToQuantum(ScaleQuantumToAny(ClampPixel(
1508 (MagickRealType) GetPixelOpacity(q)),range),range));
1509 q++;
1510 }
1511 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1512 {
1513 status=MagickFalse;
1514 continue;
1515 }
1516 }
1517 image_view=DestroyCacheView(image_view);
1518 if (status != MagickFalse)
1519 image->depth=depth;
1520 return(status);
1521}
1522
1523/*
1524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525% %
1526% %
1527% %
1528% S e t I m a g e T y p e %
1529% %
1530% %
1531% %
1532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1533%
1534% SetImageType() sets the type of image. Choose from these types:
1535%
1536% BilevelType, GrayscaleType, GrayscaleMatteType, PaletteType,
1537% PaletteMatteType, TrueColorType, TrueColorMatteType,
1538% ColorSeparationType, ColorSeparationMatteType, OptimizeType
1539%
1540% The format of the SetImageType method is:
1541%
1542% MagickBooleanType SetImageType(Image *image,const ImageType type)
1543%
1544% A description of each parameter follows:
1545%
1546% o image: the image.
1547%
1548% o type: Image type.
1549%
1550*/
1551MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type)
1552{
1553 const char
1554 *artifact;
1555
1556 ImageInfo
1557 *image_info;
1558
1559 MagickBooleanType
1560 status;
1561
1563 *quantize_info;
1564
1565 assert(image != (Image *) NULL);
1566 if (IsEventLogging() != MagickFalse)
1567 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1568 assert(image->signature == MagickCoreSignature);
1569 status=MagickTrue;
1570 image_info=AcquireImageInfo();
1571 image_info->dither=image->dither;
1572 artifact=GetImageArtifact(image,"dither");
1573 if (artifact != (const char *) NULL)
1574 (void) SetImageOption(image_info,"dither",artifact);
1575 switch (type)
1576 {
1577 case BilevelType:
1578 {
1579 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1580 status=TransformImageColorspace(image,GRAYColorspace);
1581 (void) NormalizeImage(image);
1582 (void) BilevelImage(image,(double) QuantumRange/2.0);
1583 quantize_info=AcquireQuantizeInfo(image_info);
1584 quantize_info->number_colors=2;
1585 quantize_info->colorspace=GRAYColorspace;
1586 status=QuantizeImage(quantize_info,image);
1587 quantize_info=DestroyQuantizeInfo(quantize_info);
1588 image->matte=MagickFalse;
1589 break;
1590 }
1591 case GrayscaleType:
1592 {
1593 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1594 status=TransformImageColorspace(image,GRAYColorspace);
1595 image->matte=MagickFalse;
1596 break;
1597 }
1598 case GrayscaleMatteType:
1599 {
1600 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1601 status=TransformImageColorspace(image,GRAYColorspace);
1602 if (image->matte == MagickFalse)
1603 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1604 break;
1605 }
1606 case PaletteType:
1607 {
1608 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1609 status=TransformImageColorspace(image,sRGBColorspace);
1610 if ((image->storage_class == DirectClass) || (image->colors > 256))
1611 {
1612 quantize_info=AcquireQuantizeInfo(image_info);
1613 quantize_info->number_colors=256;
1614 status=QuantizeImage(quantize_info,image);
1615 quantize_info=DestroyQuantizeInfo(quantize_info);
1616 }
1617 image->matte=MagickFalse;
1618 break;
1619 }
1620 case PaletteBilevelMatteType:
1621 {
1622 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1623 status=TransformImageColorspace(image,sRGBColorspace);
1624 if (image->matte == MagickFalse)
1625 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1626 (void) BilevelImageChannel(image,AlphaChannel,(double) QuantumRange/2.0);
1627 quantize_info=AcquireQuantizeInfo(image_info);
1628 status=QuantizeImage(quantize_info,image);
1629 quantize_info=DestroyQuantizeInfo(quantize_info);
1630 break;
1631 }
1632 case PaletteMatteType:
1633 {
1634 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1635 status=TransformImageColorspace(image,sRGBColorspace);
1636 if (image->matte == MagickFalse)
1637 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1638 quantize_info=AcquireQuantizeInfo(image_info);
1639 quantize_info->colorspace=TransparentColorspace;
1640 status=QuantizeImage(quantize_info,image);
1641 quantize_info=DestroyQuantizeInfo(quantize_info);
1642 break;
1643 }
1644 case TrueColorType:
1645 {
1646 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1647 status=TransformImageColorspace(image,sRGBColorspace);
1648 if (image->storage_class != DirectClass)
1649 status=SetImageStorageClass(image,DirectClass);
1650 image->matte=MagickFalse;
1651 break;
1652 }
1653 case TrueColorMatteType:
1654 {
1655 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1656 status=TransformImageColorspace(image,sRGBColorspace);
1657 if (image->storage_class != DirectClass)
1658 status=SetImageStorageClass(image,DirectClass);
1659 if (image->matte == MagickFalse)
1660 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1661 break;
1662 }
1663 case ColorSeparationType:
1664 {
1665 if (image->colorspace != CMYKColorspace)
1666 status=TransformImageColorspace(image,CMYKColorspace);
1667 if (image->storage_class != DirectClass)
1668 status=SetImageStorageClass(image,DirectClass);
1669 image->matte=MagickFalse;
1670 break;
1671 }
1672 case ColorSeparationMatteType:
1673 {
1674 if (image->colorspace != CMYKColorspace)
1675 status=TransformImageColorspace(image,CMYKColorspace);
1676 if (image->storage_class != DirectClass)
1677 status=SetImageStorageClass(image,DirectClass);
1678 if (image->matte == MagickFalse)
1679 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1680 break;
1681 }
1682 case OptimizeType:
1683 case UndefinedType:
1684 break;
1685 }
1686 image_info=DestroyImageInfo(image_info);
1687 if (status == MagickFalse)
1688 return(MagickFalse);
1689 image->type=type;
1690 return(MagickTrue);
1691}