blob: df000519b44374949e1fb43c4b91bda067b7dcaa [file] [log] [blame]
cristyda06ed12009-10-14 18:36:54 +00001/*
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% John Cristy %
17% October 2002 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristyda06ed12009-10-14 18:36:54 +000021% 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% http://www.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/attribute.h"
45#include "magick/blob.h"
46#include "magick/blob-private.h"
47#include "magick/cache.h"
48#include "magick/cache-view.h"
49#include "magick/client.h"
50#include "magick/color.h"
51#include "magick/color-private.h"
52#include "magick/colormap.h"
53#include "magick/colormap-private.h"
54#include "magick/colorspace.h"
55#include "magick/composite.h"
56#include "magick/composite-private.h"
57#include "magick/constitute.h"
58#include "magick/deprecate.h"
59#include "magick/draw.h"
60#include "magick/draw-private.h"
61#include "magick/effect.h"
62#include "magick/enhance.h"
63#include "magick/exception.h"
64#include "magick/exception-private.h"
65#include "magick/geometry.h"
66#include "magick/histogram.h"
67#include "magick/identify.h"
68#include "magick/image.h"
69#include "magick/image-private.h"
70#include "magick/list.h"
71#include "magick/log.h"
72#include "magick/memory_.h"
73#include "magick/magick.h"
74#include "magick/monitor.h"
75#include "magick/monitor-private.h"
76#include "magick/paint.h"
77#include "magick/pixel.h"
78#include "magick/pixel-private.h"
79#include "magick/property.h"
80#include "magick/quantize.h"
81#include "magick/random_.h"
82#include "magick/resource_.h"
83#include "magick/semaphore.h"
84#include "magick/segment.h"
85#include "magick/splay-tree.h"
86#include "magick/string_.h"
87#include "magick/thread-private.h"
88#include "magick/threshold.h"
89#include "magick/transform.h"
90#include "magick/utility.h"
91
92/*
93%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94% %
95% %
96% %
97+ G e t I m a g e B o u n d i n g B o x %
98% %
99% %
100% %
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102%
103% GetImageBoundingBox() returns the bounding box of an image canvas.
104%
105% The format of the GetImageBoundingBox method is:
106%
107% RectangleInfo GetImageBoundingBox(const Image *image,
108% ExceptionInfo *exception)
109%
110% A description of each parameter follows:
111%
112% o bounds: Method GetImageBoundingBox returns the bounding box of an
113% image canvas.
114%
115% o image: the image.
116%
117% o exception: return any errors or warnings in this structure.
118%
119*/
120MagickExport RectangleInfo GetImageBoundingBox(const Image *image,
121 ExceptionInfo *exception)
122{
cristyc4c8d132010-01-07 01:58:38 +0000123 CacheView
124 *image_view;
125
cristyda06ed12009-10-14 18:36:54 +0000126 MagickBooleanType
127 status;
128
129 MagickPixelPacket
130 target[3],
131 zero;
132
133 RectangleInfo
134 bounds;
135
136 register const PixelPacket
137 *p;
138
cristy9d314ff2011-03-09 01:30:28 +0000139 ssize_t
140 y;
141
cristyda06ed12009-10-14 18:36:54 +0000142 assert(image != (Image *) NULL);
143 assert(image->signature == MagickSignature);
144 if (image->debug != MagickFalse)
145 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
146 bounds.width=0;
147 bounds.height=0;
cristybb503372010-05-27 20:51:26 +0000148 bounds.x=(ssize_t) image->columns;
149 bounds.y=(ssize_t) image->rows;
cristyda06ed12009-10-14 18:36:54 +0000150 GetMagickPixelPacket(image,&target[0]);
151 image_view=AcquireCacheView(image);
152 p=GetCacheViewVirtualPixels(image_view,0,0,1,1,exception);
153 if (p == (const PixelPacket *) NULL)
154 {
155 image_view=DestroyCacheView(image_view);
156 return(bounds);
157 }
158 SetMagickPixelPacket(image,p,GetCacheViewAuthenticIndexQueue(image_view),
159 &target[0]);
160 GetMagickPixelPacket(image,&target[1]);
cristybb503372010-05-27 20:51:26 +0000161 p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,0,1,1,
cristyda06ed12009-10-14 18:36:54 +0000162 exception);
163 SetMagickPixelPacket(image,p,GetCacheViewAuthenticIndexQueue(image_view),
164 &target[1]);
165 GetMagickPixelPacket(image,&target[2]);
cristy4cb39ab2010-06-07 13:59:16 +0000166 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-1,1,1,
167 exception);
cristyda06ed12009-10-14 18:36:54 +0000168 SetMagickPixelPacket(image,p,GetCacheViewAuthenticIndexQueue(image_view),
169 &target[2]);
170 status=MagickTrue;
171 GetMagickPixelPacket(image,&zero);
cristyb5d5f722009-11-04 03:03:49 +0000172#if defined(MAGICKCORE_OPENMP_SUPPORT)
173 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000174#endif
cristybb503372010-05-27 20:51:26 +0000175 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000176 {
177 MagickPixelPacket
178 pixel;
179
180 RectangleInfo
181 bounding_box;
182
183 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000184 *restrict indexes;
cristyda06ed12009-10-14 18:36:54 +0000185
186 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000187 *restrict p;
cristyda06ed12009-10-14 18:36:54 +0000188
cristybb503372010-05-27 20:51:26 +0000189 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000190 x;
191
192 if (status == MagickFalse)
193 continue;
cristyb5d5f722009-11-04 03:03:49 +0000194#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyda06ed12009-10-14 18:36:54 +0000195# pragma omp critical (MagickCore_GetImageBoundingBox)
196#endif
197 bounding_box=bounds;
198 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
199 if (p == (const PixelPacket *) NULL)
200 {
201 status=MagickFalse;
202 continue;
203 }
204 indexes=GetCacheViewVirtualIndexQueue(image_view);
205 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000206 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000207 {
208 SetMagickPixelPacket(image,p,indexes+x,&pixel);
209 if ((x < bounding_box.x) &&
210 (IsMagickColorSimilar(&pixel,&target[0]) == MagickFalse))
211 bounding_box.x=x;
cristybb503372010-05-27 20:51:26 +0000212 if ((x > (ssize_t) bounding_box.width) &&
cristyda06ed12009-10-14 18:36:54 +0000213 (IsMagickColorSimilar(&pixel,&target[1]) == MagickFalse))
cristybb503372010-05-27 20:51:26 +0000214 bounding_box.width=(size_t) x;
cristyda06ed12009-10-14 18:36:54 +0000215 if ((y < bounding_box.y) &&
216 (IsMagickColorSimilar(&pixel,&target[0]) == MagickFalse))
217 bounding_box.y=y;
cristybb503372010-05-27 20:51:26 +0000218 if ((y > (ssize_t) bounding_box.height) &&
cristyda06ed12009-10-14 18:36:54 +0000219 (IsMagickColorSimilar(&pixel,&target[2]) == MagickFalse))
cristybb503372010-05-27 20:51:26 +0000220 bounding_box.height=(size_t) y;
cristyda06ed12009-10-14 18:36:54 +0000221 p++;
222 }
cristyb5d5f722009-11-04 03:03:49 +0000223#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyda06ed12009-10-14 18:36:54 +0000224# pragma omp critical (MagickCore_GetImageBoundingBox)
225#endif
226 {
227 if (bounding_box.x < bounds.x)
228 bounds.x=bounding_box.x;
229 if (bounding_box.y < bounds.y)
230 bounds.y=bounding_box.y;
231 if (bounding_box.width > bounds.width)
232 bounds.width=bounding_box.width;
233 if (bounding_box.height > bounds.height)
234 bounds.height=bounding_box.height;
235 }
236 }
237 image_view=DestroyCacheView(image_view);
238 if ((bounds.width == 0) || (bounds.height == 0))
239 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
240 "GeometryDoesNotContainImage","`%s'",image->filename);
241 else
242 {
243 bounds.width-=(bounds.x-1);
244 bounds.height-=(bounds.y-1);
245 }
246 return(bounds);
247}
248
249/*
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251% %
252% %
253% %
254% G e t I m a g e C h a n n e l D e p t h %
255% %
256% %
257% %
258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259%
260% GetImageChannelDepth() returns the depth of a particular image channel.
261%
262% The format of the GetImageChannelDepth method is:
263%
cristybb503372010-05-27 20:51:26 +0000264% size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
265% size_t GetImageChannelDepth(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000266% const ChannelType channel,ExceptionInfo *exception)
267%
268% A description of each parameter follows:
269%
270% o image: the image.
271%
272% o channel: the channel.
273%
274% o exception: return any errors or warnings in this structure.
275%
276*/
cristye8c25f92010-06-03 00:53:06 +0000277MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
cristyda06ed12009-10-14 18:36:54 +0000278{
279 return(GetImageChannelDepth(image,AllChannels,exception));
280}
281
cristybb503372010-05-27 20:51:26 +0000282MagickExport size_t GetImageChannelDepth(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000283 const ChannelType channel,ExceptionInfo *exception)
284{
cristyc4c8d132010-01-07 01:58:38 +0000285 CacheView
286 *image_view;
287
cristyda06ed12009-10-14 18:36:54 +0000288 MagickBooleanType
289 status;
290
cristybb503372010-05-27 20:51:26 +0000291 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000292 id;
293
cristybb503372010-05-27 20:51:26 +0000294 size_t
cristyda06ed12009-10-14 18:36:54 +0000295 *current_depth,
296 depth,
297 number_threads;
298
cristy9d314ff2011-03-09 01:30:28 +0000299 ssize_t
300 y;
301
cristyda06ed12009-10-14 18:36:54 +0000302 /*
303 Compute image depth.
304 */
305 assert(image != (Image *) NULL);
306 assert(image->signature == MagickSignature);
307 if (image->debug != MagickFalse)
308 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
309 number_threads=GetOpenMPMaximumThreads();
cristybb503372010-05-27 20:51:26 +0000310 current_depth=(size_t *) AcquireQuantumMemory(number_threads,
cristyda06ed12009-10-14 18:36:54 +0000311 sizeof(*current_depth));
cristybb503372010-05-27 20:51:26 +0000312 if (current_depth == (size_t *) NULL)
cristyda06ed12009-10-14 18:36:54 +0000313 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
314 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +0000315 for (id=0; id < (ssize_t) number_threads; id++)
cristyda06ed12009-10-14 18:36:54 +0000316 current_depth[id]=1;
317 if ((image->storage_class == PseudoClass) && (image->matte == MagickFalse))
318 {
319 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000320 *restrict p;
cristyda06ed12009-10-14 18:36:54 +0000321
cristybb503372010-05-27 20:51:26 +0000322 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000323 i;
324
325 p=image->colormap;
cristyb5d5f722009-11-04 03:03:49 +0000326#if defined(MAGICKCORE_OPENMP_SUPPORT)
327 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000328#endif
cristybb503372010-05-27 20:51:26 +0000329 for (i=0; i < (ssize_t) image->colors; i++)
cristyda06ed12009-10-14 18:36:54 +0000330 {
cristy5c9e6f22010-09-17 17:31:01 +0000331 const int
332 id = GetOpenMPThreadId();
333
cristyda06ed12009-10-14 18:36:54 +0000334 if (status == MagickFalse)
335 continue;
cristyda06ed12009-10-14 18:36:54 +0000336 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
337 {
338 MagickStatusType
339 status;
340
341 QuantumAny
cristyb6d6f7c2010-06-09 13:17:57 +0000342 range;
cristyda06ed12009-10-14 18:36:54 +0000343
344 status=0;
cristyb6d6f7c2010-06-09 13:17:57 +0000345 range=GetQuantumRange(current_depth[id]);
cristyda06ed12009-10-14 18:36:54 +0000346 if ((channel & RedChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000347 status|=p->red != ScaleAnyToQuantum(ScaleQuantumToAny(p->red,
348 range),range);
cristyda06ed12009-10-14 18:36:54 +0000349 if ((channel & GreenChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000350 status|=p->green != ScaleAnyToQuantum(ScaleQuantumToAny(p->green,
351 range),range);
cristyda06ed12009-10-14 18:36:54 +0000352 if ((channel & BlueChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000353 status|=p->blue != ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,
354 range),range);
cristyda06ed12009-10-14 18:36:54 +0000355 if (status == 0)
356 break;
357 current_depth[id]++;
358 }
359 p++;
360 }
361 depth=current_depth[0];
cristybb503372010-05-27 20:51:26 +0000362 for (id=1; id < (ssize_t) number_threads; id++)
cristyda06ed12009-10-14 18:36:54 +0000363 if (depth < current_depth[id])
364 depth=current_depth[id];
cristybb503372010-05-27 20:51:26 +0000365 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
cristyda06ed12009-10-14 18:36:54 +0000366 return(depth);
367 }
368 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000369#if defined(MAGICKCORE_OPENMP_SUPPORT)
370 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000371#endif
cristybb503372010-05-27 20:51:26 +0000372 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000373 {
cristy5c9e6f22010-09-17 17:31:01 +0000374 const int
375 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000376
cristyda06ed12009-10-14 18:36:54 +0000377 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000378 *restrict indexes;
cristyda06ed12009-10-14 18:36:54 +0000379
380 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000381 *restrict p;
cristyda06ed12009-10-14 18:36:54 +0000382
cristybb503372010-05-27 20:51:26 +0000383 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000384 x;
385
386 if (status == MagickFalse)
387 continue;
cristyda06ed12009-10-14 18:36:54 +0000388 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
389 if (p == (const PixelPacket *) NULL)
390 continue;
391 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristybb503372010-05-27 20:51:26 +0000392 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000393 {
394 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
395 {
396 MagickStatusType
397 status;
398
399 QuantumAny
cristyb6d6f7c2010-06-09 13:17:57 +0000400 range;
cristyda06ed12009-10-14 18:36:54 +0000401
402 status=0;
cristyb6d6f7c2010-06-09 13:17:57 +0000403 range=GetQuantumRange(current_depth[id]);
cristyda06ed12009-10-14 18:36:54 +0000404 if ((channel & RedChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000405 status|=p->red != ScaleAnyToQuantum(ScaleQuantumToAny(p->red,range),
406 range);
cristyda06ed12009-10-14 18:36:54 +0000407 if ((channel & GreenChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000408 status|=p->green != ScaleAnyToQuantum(ScaleQuantumToAny(p->green,
409 range),range);
cristyda06ed12009-10-14 18:36:54 +0000410 if ((channel & BlueChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000411 status|=p->blue != ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,range),
412 range);
cristyda06ed12009-10-14 18:36:54 +0000413 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
cristyb6d6f7c2010-06-09 13:17:57 +0000414 status|=p->opacity != ScaleAnyToQuantum(ScaleQuantumToAny(p->opacity,
415 range),range);
cristyda06ed12009-10-14 18:36:54 +0000416 if (((channel & IndexChannel) != 0) &&
417 (image->colorspace == CMYKColorspace))
cristyb6d6f7c2010-06-09 13:17:57 +0000418 status|=indexes[x] != ScaleAnyToQuantum(ScaleQuantumToAny(indexes[x],
419 range),range);
cristyda06ed12009-10-14 18:36:54 +0000420 if (status == 0)
421 break;
422 current_depth[id]++;
423 }
424 p++;
425 }
426 if (current_depth[id] == MAGICKCORE_QUANTUM_DEPTH)
427 status=MagickFalse;
428 }
429 image_view=DestroyCacheView(image_view);
430 depth=current_depth[0];
cristybb503372010-05-27 20:51:26 +0000431 for (id=1; id < (ssize_t) number_threads; id++)
cristyda06ed12009-10-14 18:36:54 +0000432 if (depth < current_depth[id])
433 depth=current_depth[id];
cristybb503372010-05-27 20:51:26 +0000434 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
cristyda06ed12009-10-14 18:36:54 +0000435 return(depth);
436}
437
438/*
439%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440% %
441% %
442% %
443% G e t I m a g e Q u a n t u m D e p t h %
444% %
445% %
446% %
447%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448%
449% GetImageQuantumDepth() returns the depth of the image rounded to a legal
450% quantum depth: 8, 16, or 32.
451%
452% The format of the GetImageQuantumDepth method is:
453%
cristybb503372010-05-27 20:51:26 +0000454% size_t GetImageQuantumDepth(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000455% const MagickBooleanType constrain)
456%
457% A description of each parameter follows:
458%
459% o image: the image.
460%
461% o constrain: A value other than MagickFalse, constrains the depth to
462% a maximum of MAGICKCORE_QUANTUM_DEPTH.
463%
464*/
465
466static inline double MagickMin(const double x,const double y)
467{
468 if (x < y)
469 return(x);
470 return(y);
471}
472
cristybb503372010-05-27 20:51:26 +0000473MagickExport size_t GetImageQuantumDepth(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000474 const MagickBooleanType constrain)
475{
cristybb503372010-05-27 20:51:26 +0000476 size_t
cristyda06ed12009-10-14 18:36:54 +0000477 depth;
478
479 depth=image->depth;
480 if (depth <= 8)
481 depth=8;
482 else
483 if (depth <= 16)
484 depth=16;
485 else
486 if (depth <= 32)
487 depth=32;
488 else
489 if (depth <= 64)
490 depth=64;
491 if (constrain != MagickFalse)
cristybb503372010-05-27 20:51:26 +0000492 depth=(size_t) MagickMin((double) depth,(double)
cristyda06ed12009-10-14 18:36:54 +0000493 MAGICKCORE_QUANTUM_DEPTH);
494 return(depth);
495}
496
497/*
498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499% %
500% %
501% %
502% G e t I m a g e T y p e %
503% %
504% %
505% %
506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
507%
508% GetImageType() returns the potential type of image:
509%
510% Bilevel Grayscale GrayscaleMatte
511% Palette PaletteMatte TrueColor
512% TrueColorMatte ColorSeparation ColorSeparationMatte
513%
514% To ensure the image type matches its potential, use SetImageType():
515%
516% (void) SetImageType(image,GetImageType(image));
517%
518% The format of the GetImageType method is:
519%
520% ImageType GetImageType(const Image *image,ExceptionInfo *exception)
521%
522% A description of each parameter follows:
523%
524% o image: the image.
525%
526% o exception: return any errors or warnings in this structure.
527%
528*/
529MagickExport ImageType GetImageType(const Image *image,ExceptionInfo *exception)
530{
531 assert(image != (Image *) NULL);
532 assert(image->signature == MagickSignature);
533 if (image->debug != MagickFalse)
534 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
535 if (image->colorspace == CMYKColorspace)
536 {
537 if (image->matte == MagickFalse)
538 return(ColorSeparationType);
539 return(ColorSeparationMatteType);
540 }
541 if (IsMonochromeImage(image,exception) != MagickFalse)
542 return(BilevelType);
543 if (IsGrayImage(image,exception) != MagickFalse)
544 {
545 if (image->matte != MagickFalse)
546 return(GrayscaleMatteType);
547 return(GrayscaleType);
548 }
549 if (IsPaletteImage(image,exception) != MagickFalse)
550 {
551 if (image->matte != MagickFalse)
552 return(PaletteMatteType);
553 return(PaletteType);
554 }
555 if (image->matte != MagickFalse)
556 return(TrueColorMatteType);
557 return(TrueColorType);
558}
559
560/*
561%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562% %
563% %
564% %
565% I s G r a y I m a g e %
566% %
567% %
568% %
569%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570%
571% IsGrayImage() returns MagickTrue if all the pixels in the image have the
572% same red, green, and blue intensities.
573%
574% The format of the IsGrayImage method is:
575%
576% MagickBooleanType IsGrayImage(const Image *image,
577% ExceptionInfo *exception)
578%
579% A description of each parameter follows:
580%
581% o image: the image.
582%
583% o exception: return any errors or warnings in this structure.
584%
585*/
586MagickExport MagickBooleanType IsGrayImage(const Image *image,
587 ExceptionInfo *exception)
588{
cristy0910f242010-04-01 18:55:09 +0000589 CacheView
590 *image_view;
591
cristyda06ed12009-10-14 18:36:54 +0000592 ImageType
593 type;
594
595 register const PixelPacket
596 *p;
597
cristybb503372010-05-27 20:51:26 +0000598 register ssize_t
cristy0910f242010-04-01 18:55:09 +0000599 x;
600
cristy95802a72010-09-05 19:07:17 +0000601 ssize_t
602 y;
603
cristyda06ed12009-10-14 18:36:54 +0000604 assert(image != (Image *) NULL);
605 assert(image->signature == MagickSignature);
606 if (image->debug != MagickFalse)
607 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
608 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
609 (image->type == GrayscaleMatteType))
610 return(MagickTrue);
611 if (image->colorspace == CMYKColorspace)
612 return(MagickFalse);
613 type=BilevelType;
cristy0910f242010-04-01 18:55:09 +0000614 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000615 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000616 {
cristy0910f242010-04-01 18:55:09 +0000617 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
618 if (p == (const PixelPacket *) NULL)
619 break;
cristybb503372010-05-27 20:51:26 +0000620 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000621 {
cristy0910f242010-04-01 18:55:09 +0000622 if (IsGrayPixel(p) == MagickFalse)
cristyda06ed12009-10-14 18:36:54 +0000623 {
cristy5f1c1ff2010-12-23 21:38:06 +0000624 type=UndefinedType;
cristyda06ed12009-10-14 18:36:54 +0000625 break;
cristy0910f242010-04-01 18:55:09 +0000626 }
627 if ((type == BilevelType) && (IsMonochromePixel(p) == MagickFalse))
628 type=GrayscaleType;
629 p++;
cristyda06ed12009-10-14 18:36:54 +0000630 }
cristy5f1c1ff2010-12-23 21:38:06 +0000631 if (type == UndefinedType)
cristyda06ed12009-10-14 18:36:54 +0000632 break;
cristyda06ed12009-10-14 18:36:54 +0000633 }
cristy0910f242010-04-01 18:55:09 +0000634 image_view=DestroyCacheView(image_view);
cristy5f1c1ff2010-12-23 21:38:06 +0000635 if (type == UndefinedType)
cristyda06ed12009-10-14 18:36:54 +0000636 return(MagickFalse);
637 ((Image *) image)->type=type;
638 if ((type == GrayscaleType) && (image->matte != MagickFalse))
639 ((Image *) image)->type=GrayscaleMatteType;
640 return(MagickTrue);
641}
642
643/*
644%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645% %
646% %
647% %
648% I s M o n o c h r o m e I m a g e %
649% %
650% %
651% %
652%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653%
654% IsMonochromeImage() returns MagickTrue if all the pixels in the image have
655% the same red, green, and blue intensities and the intensity is either
656% 0 or QuantumRange.
657%
658% The format of the IsMonochromeImage method is:
659%
660% MagickBooleanType IsMonochromeImage(const Image *image,
661% ExceptionInfo *exception)
662%
663% A description of each parameter follows:
664%
665% o image: the image.
666%
667% o exception: return any errors or warnings in this structure.
668%
669*/
670MagickExport MagickBooleanType IsMonochromeImage(const Image *image,
671 ExceptionInfo *exception)
672{
cristy0910f242010-04-01 18:55:09 +0000673 CacheView
674 *image_view;
675
cristyda06ed12009-10-14 18:36:54 +0000676 ImageType
677 type;
678
cristybb503372010-05-27 20:51:26 +0000679 register ssize_t
cristy0910f242010-04-01 18:55:09 +0000680 x;
681
cristyda06ed12009-10-14 18:36:54 +0000682 register const PixelPacket
683 *p;
684
cristy9d314ff2011-03-09 01:30:28 +0000685 ssize_t
686 y;
687
cristyda06ed12009-10-14 18:36:54 +0000688 assert(image != (Image *) NULL);
689 assert(image->signature == MagickSignature);
690 if (image->debug != MagickFalse)
691 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
692 if (image->type == BilevelType)
693 return(MagickTrue);
694 if (image->colorspace == CMYKColorspace)
695 return(MagickFalse);
696 type=BilevelType;
cristy0910f242010-04-01 18:55:09 +0000697 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000698 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000699 {
cristy0910f242010-04-01 18:55:09 +0000700 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
701 if (p == (const PixelPacket *) NULL)
702 break;
cristybb503372010-05-27 20:51:26 +0000703 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000704 {
cristy0910f242010-04-01 18:55:09 +0000705 if (IsMonochromePixel(p) == MagickFalse)
cristyda06ed12009-10-14 18:36:54 +0000706 {
cristy5f1c1ff2010-12-23 21:38:06 +0000707 type=UndefinedType;
cristyda06ed12009-10-14 18:36:54 +0000708 break;
cristy0910f242010-04-01 18:55:09 +0000709 }
710 p++;
cristyda06ed12009-10-14 18:36:54 +0000711 }
cristy5f1c1ff2010-12-23 21:38:06 +0000712 if (type == UndefinedType)
cristyda06ed12009-10-14 18:36:54 +0000713 break;
cristyda06ed12009-10-14 18:36:54 +0000714 }
cristy0910f242010-04-01 18:55:09 +0000715 image_view=DestroyCacheView(image_view);
cristy5f1c1ff2010-12-23 21:38:06 +0000716 if (type == UndefinedType)
cristyda06ed12009-10-14 18:36:54 +0000717 return(MagickFalse);
718 ((Image *) image)->type=type;
719 return(MagickTrue);
720}
721
722/*
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724% %
725% %
726% %
727% I s O p a q u e I m a g e %
728% %
729% %
730% %
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732%
733% IsOpaqueImage() returns MagickTrue if none of the pixels in the image have
734% an opacity value other than opaque (0).
735%
736% The format of the IsOpaqueImage method is:
737%
738% MagickBooleanType IsOpaqueImage(const Image *image,
739% ExceptionInfo *exception)
740%
741% A description of each parameter follows:
742%
743% o image: the image.
744%
745% o exception: return any errors or warnings in this structure.
746%
747*/
748MagickExport MagickBooleanType IsOpaqueImage(const Image *image,
749 ExceptionInfo *exception)
750{
cristyc4c8d132010-01-07 01:58:38 +0000751 CacheView
752 *image_view;
753
cristyda06ed12009-10-14 18:36:54 +0000754 register const PixelPacket
755 *p;
756
cristybb503372010-05-27 20:51:26 +0000757 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000758 x;
759
cristy9d314ff2011-03-09 01:30:28 +0000760 ssize_t
761 y;
762
cristyda06ed12009-10-14 18:36:54 +0000763 /*
764 Determine if image is opaque.
765 */
766 assert(image != (Image *) NULL);
767 assert(image->signature == MagickSignature);
768 if (image->debug != MagickFalse)
769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
770 if (image->matte == MagickFalse)
771 return(MagickTrue);
772 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000773 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000774 {
775 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
776 if (p == (const PixelPacket *) NULL)
777 break;
cristybb503372010-05-27 20:51:26 +0000778 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000779 {
780 if (p->opacity != OpaqueOpacity)
781 break;
782 p++;
783 }
cristybb503372010-05-27 20:51:26 +0000784 if (x < (ssize_t) image->columns)
cristyda06ed12009-10-14 18:36:54 +0000785 break;
786 }
787 image_view=DestroyCacheView(image_view);
cristybb503372010-05-27 20:51:26 +0000788 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
cristyda06ed12009-10-14 18:36:54 +0000789}
790
791/*
792%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793% %
794% %
795% %
796% S e t I m a g e C h a n n e l D e p t h %
797% %
798% %
799% %
800%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801%
802% SetImageChannelDepth() sets the depth of the image.
803%
804% The format of the SetImageChannelDepth method is:
805%
cristybb503372010-05-27 20:51:26 +0000806% MagickBooleanType SetImageDepth(Image *image,const size_t depth)
cristyda06ed12009-10-14 18:36:54 +0000807% MagickBooleanType SetImageChannelDepth(Image *image,
cristybb503372010-05-27 20:51:26 +0000808% const ChannelType channel,const size_t depth)
cristyda06ed12009-10-14 18:36:54 +0000809%
810% A description of each parameter follows:
811%
812% o image: the image.
813%
814% o channel: the channel.
815%
816% o depth: the image depth.
817%
818*/
cristyda06ed12009-10-14 18:36:54 +0000819MagickExport MagickBooleanType SetImageDepth(Image *image,
cristybb503372010-05-27 20:51:26 +0000820 const size_t depth)
cristyda06ed12009-10-14 18:36:54 +0000821{
822 return(SetImageChannelDepth(image,AllChannels,depth));
823}
824
825MagickExport MagickBooleanType SetImageChannelDepth(Image *image,
cristybb503372010-05-27 20:51:26 +0000826 const ChannelType channel,const size_t depth)
cristyda06ed12009-10-14 18:36:54 +0000827{
cristyc4c8d132010-01-07 01:58:38 +0000828 CacheView
829 *image_view;
830
cristyda06ed12009-10-14 18:36:54 +0000831 ExceptionInfo
832 *exception;
833
cristyda06ed12009-10-14 18:36:54 +0000834 MagickBooleanType
835 status;
836
837 QuantumAny
cristyb6d6f7c2010-06-09 13:17:57 +0000838 range;
cristyda06ed12009-10-14 18:36:54 +0000839
cristy9d314ff2011-03-09 01:30:28 +0000840 ssize_t
841 y;
842
cristyda06ed12009-10-14 18:36:54 +0000843 assert(image != (Image *) NULL);
844 if (image->debug != MagickFalse)
845 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
846 assert(image->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +0000847 if (GetImageDepth(image,&image->exception) <= (size_t)
cristyda06ed12009-10-14 18:36:54 +0000848 MagickMin((double) depth,(double) MAGICKCORE_QUANTUM_DEPTH))
849 {
850 image->depth=depth;
851 return(MagickTrue);
852 }
853 /*
854 Scale pixels to desired depth.
855 */
856 status=MagickTrue;
cristyb6d6f7c2010-06-09 13:17:57 +0000857 range=GetQuantumRange(depth);
cristyda06ed12009-10-14 18:36:54 +0000858 exception=(&image->exception);
859 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000860#if defined(MAGICKCORE_OPENMP_SUPPORT)
861 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000862#endif
cristybb503372010-05-27 20:51:26 +0000863 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000864 {
865 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +0000866 *restrict indexes;
cristyda06ed12009-10-14 18:36:54 +0000867
cristybb503372010-05-27 20:51:26 +0000868 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000869 x;
870
871 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000872 *restrict q;
cristyda06ed12009-10-14 18:36:54 +0000873
874 if (status == MagickFalse)
875 continue;
876 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
877 exception);
878 if (q == (PixelPacket *) NULL)
879 {
880 status=MagickFalse;
881 continue;
882 }
883 indexes=GetCacheViewAuthenticIndexQueue(image_view);
cristybb503372010-05-27 20:51:26 +0000884 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000885 {
886 if ((channel & RedChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000887 q->red=ScaleAnyToQuantum(ScaleQuantumToAny(q->red,range),range);
cristyda06ed12009-10-14 18:36:54 +0000888 if ((channel & GreenChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000889 q->green=ScaleAnyToQuantum(ScaleQuantumToAny(q->green,range),range);
cristyda06ed12009-10-14 18:36:54 +0000890 if ((channel & BlueChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000891 q->blue=ScaleAnyToQuantum(ScaleQuantumToAny(q->blue,range),range);
cristyda06ed12009-10-14 18:36:54 +0000892 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
cristyb6d6f7c2010-06-09 13:17:57 +0000893 q->opacity=ScaleAnyToQuantum(ScaleQuantumToAny(q->opacity,range),range);
cristyda06ed12009-10-14 18:36:54 +0000894 if (((channel & IndexChannel) != 0) &&
895 (image->colorspace == CMYKColorspace))
cristyb6d6f7c2010-06-09 13:17:57 +0000896 indexes[x]=ScaleAnyToQuantum(ScaleQuantumToAny(indexes[x],range),range);
cristyda06ed12009-10-14 18:36:54 +0000897 q++;
898 }
899 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
900 {
901 status=MagickFalse;
902 continue;
903 }
904 }
905 image_view=DestroyCacheView(image_view);
906 if (image->storage_class == PseudoClass)
907 {
cristybb503372010-05-27 20:51:26 +0000908 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000909 i;
910
911 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000912 *restrict p;
cristyda06ed12009-10-14 18:36:54 +0000913
914 p=image->colormap;
cristyb5d5f722009-11-04 03:03:49 +0000915#if defined(MAGICKCORE_OPENMP_SUPPORT)
916 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000917#endif
cristybb503372010-05-27 20:51:26 +0000918 for (i=0; i < (ssize_t) image->colors; i++)
cristyda06ed12009-10-14 18:36:54 +0000919 {
920 if ((channel & RedChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000921 p->red=ScaleAnyToQuantum(ScaleQuantumToAny(p->red,range),range);
cristyda06ed12009-10-14 18:36:54 +0000922 if ((channel & GreenChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000923 p->green=ScaleAnyToQuantum(ScaleQuantumToAny(p->green,range),range);
cristyda06ed12009-10-14 18:36:54 +0000924 if ((channel & BlueChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000925 p->blue=ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,range),range);
cristyda06ed12009-10-14 18:36:54 +0000926 if ((channel & OpacityChannel) != 0)
cristyb6d6f7c2010-06-09 13:17:57 +0000927 p->opacity=ScaleAnyToQuantum(ScaleQuantumToAny(p->opacity,range),
928 range);
cristyda06ed12009-10-14 18:36:54 +0000929 p++;
930 }
931 }
932 image->depth=depth;
933 return(status);
934}