blob: d595ced2cb184db241714d89506cae15c7d43196 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/attribute.h"
45#include "MagickCore/blob.h"
46#include "MagickCore/blob-private.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/cache-view.h"
49#include "MagickCore/client.h"
50#include "MagickCore/color.h"
51#include "MagickCore/color-private.h"
52#include "MagickCore/colormap.h"
53#include "MagickCore/colormap-private.h"
54#include "MagickCore/colorspace.h"
cristycca1bfa2011-09-12 12:39:53 +000055#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000056#include "MagickCore/composite.h"
57#include "MagickCore/composite-private.h"
58#include "MagickCore/constitute.h"
59#include "MagickCore/draw.h"
60#include "MagickCore/draw-private.h"
61#include "MagickCore/effect.h"
62#include "MagickCore/enhance.h"
63#include "MagickCore/exception.h"
64#include "MagickCore/exception-private.h"
65#include "MagickCore/geometry.h"
66#include "MagickCore/histogram.h"
67#include "MagickCore/identify.h"
68#include "MagickCore/image.h"
69#include "MagickCore/image-private.h"
70#include "MagickCore/list.h"
71#include "MagickCore/log.h"
72#include "MagickCore/memory_.h"
73#include "MagickCore/magick.h"
74#include "MagickCore/monitor.h"
75#include "MagickCore/monitor-private.h"
76#include "MagickCore/paint.h"
77#include "MagickCore/pixel.h"
78#include "MagickCore/pixel-accessor.h"
79#include "MagickCore/property.h"
80#include "MagickCore/quantize.h"
81#include "MagickCore/quantum-private.h"
82#include "MagickCore/random_.h"
83#include "MagickCore/resource_.h"
84#include "MagickCore/semaphore.h"
85#include "MagickCore/segment.h"
86#include "MagickCore/splay-tree.h"
87#include "MagickCore/string_.h"
88#include "MagickCore/thread-private.h"
89#include "MagickCore/threshold.h"
90#include "MagickCore/transform.h"
91#include "MagickCore/utility.h"
cristyda06ed12009-10-14 18:36:54 +000092
93/*
94%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95% %
96% %
97% %
98+ G e t I m a g e B o u n d i n g B o x %
99% %
100% %
101% %
102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103%
104% GetImageBoundingBox() returns the bounding box of an image canvas.
105%
106% The format of the GetImageBoundingBox method is:
107%
108% RectangleInfo GetImageBoundingBox(const Image *image,
109% ExceptionInfo *exception)
110%
111% A description of each parameter follows:
112%
113% o bounds: Method GetImageBoundingBox returns the bounding box of an
114% image canvas.
115%
116% o image: the image.
117%
118% o exception: return any errors or warnings in this structure.
119%
120*/
121MagickExport RectangleInfo GetImageBoundingBox(const Image *image,
122 ExceptionInfo *exception)
123{
cristyc4c8d132010-01-07 01:58:38 +0000124 CacheView
125 *image_view;
126
cristyda06ed12009-10-14 18:36:54 +0000127 MagickBooleanType
128 status;
129
cristy4c08aed2011-07-01 19:47:50 +0000130 PixelInfo
cristyda06ed12009-10-14 18:36:54 +0000131 target[3],
132 zero;
133
134 RectangleInfo
135 bounds;
136
cristy4c08aed2011-07-01 19:47:50 +0000137 register const Quantum
cristyda06ed12009-10-14 18:36:54 +0000138 *p;
139
cristy9d314ff2011-03-09 01:30:28 +0000140 ssize_t
141 y;
142
cristyda06ed12009-10-14 18:36:54 +0000143 assert(image != (Image *) NULL);
144 assert(image->signature == MagickSignature);
145 if (image->debug != MagickFalse)
146 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
147 bounds.width=0;
148 bounds.height=0;
cristybb503372010-05-27 20:51:26 +0000149 bounds.x=(ssize_t) image->columns;
150 bounds.y=(ssize_t) image->rows;
cristy4c08aed2011-07-01 19:47:50 +0000151 GetPixelInfo(image,&target[0]);
cristyda06ed12009-10-14 18:36:54 +0000152 image_view=AcquireCacheView(image);
153 p=GetCacheViewVirtualPixels(image_view,0,0,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000154 if (p == (const Quantum *) NULL)
cristyda06ed12009-10-14 18:36:54 +0000155 {
156 image_view=DestroyCacheView(image_view);
157 return(bounds);
158 }
cristy803640d2011-11-17 02:11:32 +0000159 GetPixelInfoPixel(image,p,&target[0]);
cristy4c08aed2011-07-01 19:47:50 +0000160 GetPixelInfo(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);
cristy803640d2011-11-17 02:11:32 +0000163 GetPixelInfoPixel(image,p,&target[1]);
cristy4c08aed2011-07-01 19:47:50 +0000164 GetPixelInfo(image,&target[2]);
cristy4cb39ab2010-06-07 13:59:16 +0000165 p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-1,1,1,
166 exception);
cristy803640d2011-11-17 02:11:32 +0000167 GetPixelInfoPixel(image,p,&target[2]);
cristyda06ed12009-10-14 18:36:54 +0000168 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000169 GetPixelInfo(image,&zero);
cristyb5d5f722009-11-04 03:03:49 +0000170#if defined(MAGICKCORE_OPENMP_SUPPORT)
171 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000172#endif
cristybb503372010-05-27 20:51:26 +0000173 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000174 {
cristy4c08aed2011-07-01 19:47:50 +0000175 PixelInfo
cristyda06ed12009-10-14 18:36:54 +0000176 pixel;
177
178 RectangleInfo
179 bounding_box;
180
cristy4c08aed2011-07-01 19:47:50 +0000181 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000182 *restrict p;
cristyda06ed12009-10-14 18:36:54 +0000183
cristybb503372010-05-27 20:51:26 +0000184 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000185 x;
186
187 if (status == MagickFalse)
188 continue;
cristyb5d5f722009-11-04 03:03:49 +0000189#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyda06ed12009-10-14 18:36:54 +0000190# pragma omp critical (MagickCore_GetImageBoundingBox)
191#endif
192 bounding_box=bounds;
193 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000194 if (p == (const Quantum *) NULL)
cristyda06ed12009-10-14 18:36:54 +0000195 {
196 status=MagickFalse;
197 continue;
198 }
cristyda06ed12009-10-14 18:36:54 +0000199 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000200 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000201 {
cristy803640d2011-11-17 02:11:32 +0000202 GetPixelInfoPixel(image,p,&pixel);
cristyda06ed12009-10-14 18:36:54 +0000203 if ((x < bounding_box.x) &&
cristy4c08aed2011-07-01 19:47:50 +0000204 (IsFuzzyEquivalencePixelInfo(&pixel,&target[0]) == MagickFalse))
cristyda06ed12009-10-14 18:36:54 +0000205 bounding_box.x=x;
cristybb503372010-05-27 20:51:26 +0000206 if ((x > (ssize_t) bounding_box.width) &&
cristy4c08aed2011-07-01 19:47:50 +0000207 (IsFuzzyEquivalencePixelInfo(&pixel,&target[1]) == MagickFalse))
cristybb503372010-05-27 20:51:26 +0000208 bounding_box.width=(size_t) x;
cristyda06ed12009-10-14 18:36:54 +0000209 if ((y < bounding_box.y) &&
cristy4c08aed2011-07-01 19:47:50 +0000210 (IsFuzzyEquivalencePixelInfo(&pixel,&target[0]) == MagickFalse))
cristyda06ed12009-10-14 18:36:54 +0000211 bounding_box.y=y;
cristybb503372010-05-27 20:51:26 +0000212 if ((y > (ssize_t) bounding_box.height) &&
cristy4c08aed2011-07-01 19:47:50 +0000213 (IsFuzzyEquivalencePixelInfo(&pixel,&target[2]) == MagickFalse))
cristybb503372010-05-27 20:51:26 +0000214 bounding_box.height=(size_t) y;
cristyed231572011-07-14 02:18:59 +0000215 p+=GetPixelChannels(image);
cristyda06ed12009-10-14 18:36:54 +0000216 }
cristyb5d5f722009-11-04 03:03:49 +0000217#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyda06ed12009-10-14 18:36:54 +0000218# pragma omp critical (MagickCore_GetImageBoundingBox)
219#endif
220 {
221 if (bounding_box.x < bounds.x)
222 bounds.x=bounding_box.x;
223 if (bounding_box.y < bounds.y)
224 bounds.y=bounding_box.y;
225 if (bounding_box.width > bounds.width)
226 bounds.width=bounding_box.width;
227 if (bounding_box.height > bounds.height)
228 bounds.height=bounding_box.height;
229 }
230 }
231 image_view=DestroyCacheView(image_view);
232 if ((bounds.width == 0) || (bounds.height == 0))
233 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
234 "GeometryDoesNotContainImage","`%s'",image->filename);
235 else
236 {
237 bounds.width-=(bounds.x-1);
238 bounds.height-=(bounds.y-1);
239 }
240 return(bounds);
241}
242
243/*
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245% %
246% %
247% %
cristyfefab1b2011-07-05 00:33:22 +0000248% G e t I m a g e D e p t h %
cristyda06ed12009-10-14 18:36:54 +0000249% %
250% %
251% %
252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253%
cristyfefab1b2011-07-05 00:33:22 +0000254% GetImageDepth() returns the depth of a particular image channel.
cristyda06ed12009-10-14 18:36:54 +0000255%
cristyfefab1b2011-07-05 00:33:22 +0000256% The format of the GetImageDepth method is:
cristyda06ed12009-10-14 18:36:54 +0000257%
cristybb503372010-05-27 20:51:26 +0000258% size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
cristyda06ed12009-10-14 18:36:54 +0000259%
260% A description of each parameter follows:
261%
262% o image: the image.
263%
cristyda06ed12009-10-14 18:36:54 +0000264% o exception: return any errors or warnings in this structure.
265%
266*/
cristyfefab1b2011-07-05 00:33:22 +0000267MagickExport size_t GetImageDepth(const Image *image,
268 ExceptionInfo *exception)
cristyda06ed12009-10-14 18:36:54 +0000269{
cristyc4c8d132010-01-07 01:58:38 +0000270 CacheView
271 *image_view;
272
cristyda06ed12009-10-14 18:36:54 +0000273 MagickBooleanType
274 status;
275
cristybb503372010-05-27 20:51:26 +0000276 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000277 id;
278
cristybb503372010-05-27 20:51:26 +0000279 size_t
cristyda06ed12009-10-14 18:36:54 +0000280 *current_depth,
281 depth,
282 number_threads;
283
cristy9d314ff2011-03-09 01:30:28 +0000284 ssize_t
285 y;
286
cristyda06ed12009-10-14 18:36:54 +0000287 /*
288 Compute image depth.
289 */
290 assert(image != (Image *) NULL);
291 assert(image->signature == MagickSignature);
292 if (image->debug != MagickFalse)
293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
294 number_threads=GetOpenMPMaximumThreads();
cristybb503372010-05-27 20:51:26 +0000295 current_depth=(size_t *) AcquireQuantumMemory(number_threads,
cristyda06ed12009-10-14 18:36:54 +0000296 sizeof(*current_depth));
cristybb503372010-05-27 20:51:26 +0000297 if (current_depth == (size_t *) NULL)
cristyda06ed12009-10-14 18:36:54 +0000298 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
299 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +0000300 for (id=0; id < (ssize_t) number_threads; id++)
cristyda06ed12009-10-14 18:36:54 +0000301 current_depth[id]=1;
302 if ((image->storage_class == PseudoClass) && (image->matte == MagickFalse))
303 {
cristy101ab702011-10-13 13:06:32 +0000304 register const PixelInfo
cristyc47d1f82009-11-26 01:44:43 +0000305 *restrict p;
cristyda06ed12009-10-14 18:36:54 +0000306
cristybb503372010-05-27 20:51:26 +0000307 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000308 i;
309
310 p=image->colormap;
cristyb5d5f722009-11-04 03:03:49 +0000311#if defined(MAGICKCORE_OPENMP_SUPPORT)
312 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000313#endif
cristybb503372010-05-27 20:51:26 +0000314 for (i=0; i < (ssize_t) image->colors; i++)
cristyda06ed12009-10-14 18:36:54 +0000315 {
cristy5c9e6f22010-09-17 17:31:01 +0000316 const int
317 id = GetOpenMPThreadId();
318
cristyda06ed12009-10-14 18:36:54 +0000319 if (status == MagickFalse)
320 continue;
cristyda06ed12009-10-14 18:36:54 +0000321 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
322 {
323 MagickStatusType
324 status;
325
326 QuantumAny
cristyb6d6f7c2010-06-09 13:17:57 +0000327 range;
cristyda06ed12009-10-14 18:36:54 +0000328
329 status=0;
cristyb6d6f7c2010-06-09 13:17:57 +0000330 range=GetQuantumRange(current_depth[id]);
cristyed231572011-07-14 02:18:59 +0000331 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000332 status|=p->red != ScaleAnyToQuantum(ScaleQuantumToAny(p->red,
cristyb6d6f7c2010-06-09 13:17:57 +0000333 range),range);
cristyed231572011-07-14 02:18:59 +0000334 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000335 status|=p->green != ScaleAnyToQuantum(ScaleQuantumToAny(p->green,
cristyb6d6f7c2010-06-09 13:17:57 +0000336 range),range);
cristyed231572011-07-14 02:18:59 +0000337 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000338 status|=p->blue != ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,
cristyb6d6f7c2010-06-09 13:17:57 +0000339 range),range);
cristyda06ed12009-10-14 18:36:54 +0000340 if (status == 0)
341 break;
342 current_depth[id]++;
343 }
344 p++;
345 }
346 depth=current_depth[0];
cristybb503372010-05-27 20:51:26 +0000347 for (id=1; id < (ssize_t) number_threads; id++)
cristyda06ed12009-10-14 18:36:54 +0000348 if (depth < current_depth[id])
349 depth=current_depth[id];
cristybb503372010-05-27 20:51:26 +0000350 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
cristyda06ed12009-10-14 18:36:54 +0000351 return(depth);
352 }
353 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000354#if defined(MAGICKCORE_OPENMP_SUPPORT)
355 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000356#endif
cristybb503372010-05-27 20:51:26 +0000357 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000358 {
cristy5c9e6f22010-09-17 17:31:01 +0000359 const int
360 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000361
cristy4c08aed2011-07-01 19:47:50 +0000362 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000363 *restrict p;
cristyda06ed12009-10-14 18:36:54 +0000364
cristybb503372010-05-27 20:51:26 +0000365 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000366 x;
367
368 if (status == MagickFalse)
369 continue;
cristyda06ed12009-10-14 18:36:54 +0000370 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000371 if (p == (const Quantum *) NULL)
cristyda06ed12009-10-14 18:36:54 +0000372 continue;
cristybb503372010-05-27 20:51:26 +0000373 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000374 {
cristy8a11cb12011-10-19 23:53:34 +0000375 register ssize_t
376 i;
377
378 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristyda06ed12009-10-14 18:36:54 +0000379 {
cristyabace412011-12-11 15:56:53 +0000380 PixelChannel
381 channel;
382
cristy8a11cb12011-10-19 23:53:34 +0000383 PixelTrait
384 traits;
cristyda06ed12009-10-14 18:36:54 +0000385
cristyabace412011-12-11 15:56:53 +0000386 channel=GetPixelChannelMapChannel(image,i);
387 traits=GetPixelChannelMapTraits(image,channel);
cristy8a11cb12011-10-19 23:53:34 +0000388 if (traits == UndefinedPixelTrait)
389 continue;
390 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
391 {
392 MagickStatusType
393 status;
cristyda06ed12009-10-14 18:36:54 +0000394
cristy8a11cb12011-10-19 23:53:34 +0000395 QuantumAny
396 range;
397
398 status=0;
399 range=GetQuantumRange(current_depth[id]);
400 status|=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
401 range);
402 if (status == 0)
403 break;
404 current_depth[id]++;
405 }
cristyda06ed12009-10-14 18:36:54 +0000406 }
cristyed231572011-07-14 02:18:59 +0000407 p+=GetPixelChannels(image);
cristyda06ed12009-10-14 18:36:54 +0000408 }
409 if (current_depth[id] == MAGICKCORE_QUANTUM_DEPTH)
410 status=MagickFalse;
411 }
412 image_view=DestroyCacheView(image_view);
413 depth=current_depth[0];
cristybb503372010-05-27 20:51:26 +0000414 for (id=1; id < (ssize_t) number_threads; id++)
cristyda06ed12009-10-14 18:36:54 +0000415 if (depth < current_depth[id])
416 depth=current_depth[id];
cristybb503372010-05-27 20:51:26 +0000417 current_depth=(size_t *) RelinquishMagickMemory(current_depth);
cristyda06ed12009-10-14 18:36:54 +0000418 return(depth);
419}
420
421/*
422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423% %
424% %
425% %
426% G e t I m a g e Q u a n t u m D e p t h %
427% %
428% %
429% %
430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431%
432% GetImageQuantumDepth() returns the depth of the image rounded to a legal
433% quantum depth: 8, 16, or 32.
434%
435% The format of the GetImageQuantumDepth method is:
436%
cristybb503372010-05-27 20:51:26 +0000437% size_t GetImageQuantumDepth(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000438% const MagickBooleanType constrain)
439%
440% A description of each parameter follows:
441%
442% o image: the image.
443%
444% o constrain: A value other than MagickFalse, constrains the depth to
445% a maximum of MAGICKCORE_QUANTUM_DEPTH.
446%
447*/
448
449static inline double MagickMin(const double x,const double y)
450{
451 if (x < y)
452 return(x);
453 return(y);
454}
455
cristybb503372010-05-27 20:51:26 +0000456MagickExport size_t GetImageQuantumDepth(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000457 const MagickBooleanType constrain)
458{
cristybb503372010-05-27 20:51:26 +0000459 size_t
cristyda06ed12009-10-14 18:36:54 +0000460 depth;
461
462 depth=image->depth;
463 if (depth <= 8)
464 depth=8;
465 else
466 if (depth <= 16)
467 depth=16;
468 else
469 if (depth <= 32)
470 depth=32;
471 else
472 if (depth <= 64)
473 depth=64;
474 if (constrain != MagickFalse)
cristy9e940dd2011-12-13 01:22:25 +0000475 depth=(size_t) MagickMin((double) depth,(double) MAGICKCORE_QUANTUM_DEPTH);
cristyda06ed12009-10-14 18:36:54 +0000476 return(depth);
477}
478
479/*
480%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481% %
482% %
483% %
484% G e t I m a g e T y p e %
485% %
486% %
487% %
488%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
489%
490% GetImageType() returns the potential type of image:
491%
492% Bilevel Grayscale GrayscaleMatte
493% Palette PaletteMatte TrueColor
494% TrueColorMatte ColorSeparation ColorSeparationMatte
495%
496% To ensure the image type matches its potential, use SetImageType():
497%
498% (void) SetImageType(image,GetImageType(image));
499%
500% The format of the GetImageType method is:
501%
502% ImageType GetImageType(const Image *image,ExceptionInfo *exception)
503%
504% A description of each parameter follows:
505%
506% o image: the image.
507%
508% o exception: return any errors or warnings in this structure.
509%
510*/
511MagickExport ImageType GetImageType(const Image *image,ExceptionInfo *exception)
512{
513 assert(image != (Image *) NULL);
514 assert(image->signature == MagickSignature);
515 if (image->debug != MagickFalse)
516 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
517 if (image->colorspace == CMYKColorspace)
518 {
519 if (image->matte == MagickFalse)
520 return(ColorSeparationType);
521 return(ColorSeparationMatteType);
522 }
cristy4c08aed2011-07-01 19:47:50 +0000523 if (IsImageMonochrome(image,exception) != MagickFalse)
cristyda06ed12009-10-14 18:36:54 +0000524 return(BilevelType);
cristy4c08aed2011-07-01 19:47:50 +0000525 if (IsImageGray(image,exception) != MagickFalse)
cristyda06ed12009-10-14 18:36:54 +0000526 {
527 if (image->matte != MagickFalse)
528 return(GrayscaleMatteType);
529 return(GrayscaleType);
530 }
531 if (IsPaletteImage(image,exception) != MagickFalse)
532 {
533 if (image->matte != MagickFalse)
534 return(PaletteMatteType);
535 return(PaletteType);
536 }
537 if (image->matte != MagickFalse)
538 return(TrueColorMatteType);
539 return(TrueColorType);
540}
541
542/*
543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544% %
545% %
546% %
cristy4c08aed2011-07-01 19:47:50 +0000547% I s I m a g e G r a y %
cristyda06ed12009-10-14 18:36:54 +0000548% %
549% %
550% %
551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552%
cristy4c08aed2011-07-01 19:47:50 +0000553% IsImageGray() returns MagickTrue if all the pixels in the image have the
cristyda06ed12009-10-14 18:36:54 +0000554% same red, green, and blue intensities.
555%
cristy4c08aed2011-07-01 19:47:50 +0000556% The format of the IsImageGray method is:
cristyda06ed12009-10-14 18:36:54 +0000557%
cristy4c08aed2011-07-01 19:47:50 +0000558% MagickBooleanType IsImageGray(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000559% ExceptionInfo *exception)
560%
561% A description of each parameter follows:
562%
563% o image: the image.
564%
565% o exception: return any errors or warnings in this structure.
566%
567*/
cristy4c08aed2011-07-01 19:47:50 +0000568MagickExport MagickBooleanType IsImageGray(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000569 ExceptionInfo *exception)
570{
cristy0910f242010-04-01 18:55:09 +0000571 CacheView
572 *image_view;
573
cristyda06ed12009-10-14 18:36:54 +0000574 ImageType
575 type;
576
cristy4c08aed2011-07-01 19:47:50 +0000577 register const Quantum
cristyda06ed12009-10-14 18:36:54 +0000578 *p;
579
cristybb503372010-05-27 20:51:26 +0000580 register ssize_t
cristy0910f242010-04-01 18:55:09 +0000581 x;
582
cristy95802a72010-09-05 19:07:17 +0000583 ssize_t
584 y;
585
cristyda06ed12009-10-14 18:36:54 +0000586 assert(image != (Image *) NULL);
587 assert(image->signature == MagickSignature);
588 if (image->debug != MagickFalse)
589 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
590 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
591 (image->type == GrayscaleMatteType))
592 return(MagickTrue);
cristycca1bfa2011-09-12 12:39:53 +0000593 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristyda06ed12009-10-14 18:36:54 +0000594 return(MagickFalse);
595 type=BilevelType;
cristy0910f242010-04-01 18:55:09 +0000596 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000597 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000598 {
cristy0910f242010-04-01 18:55:09 +0000599 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000600 if (p == (const Quantum *) NULL)
cristy0910f242010-04-01 18:55:09 +0000601 break;
cristybb503372010-05-27 20:51:26 +0000602 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000603 {
cristy4c08aed2011-07-01 19:47:50 +0000604 if (IsPixelGray(image,p) == MagickFalse)
cristyda06ed12009-10-14 18:36:54 +0000605 {
cristy5f1c1ff2010-12-23 21:38:06 +0000606 type=UndefinedType;
cristyda06ed12009-10-14 18:36:54 +0000607 break;
cristy0910f242010-04-01 18:55:09 +0000608 }
cristybd5a96c2011-08-21 00:04:26 +0000609 if ((type == BilevelType) &&
610 (IsPixelMonochrome(image,p) == MagickFalse))
cristy0910f242010-04-01 18:55:09 +0000611 type=GrayscaleType;
cristyed231572011-07-14 02:18:59 +0000612 p+=GetPixelChannels(image);
cristyda06ed12009-10-14 18:36:54 +0000613 }
cristy5f1c1ff2010-12-23 21:38:06 +0000614 if (type == UndefinedType)
cristyda06ed12009-10-14 18:36:54 +0000615 break;
cristyda06ed12009-10-14 18:36:54 +0000616 }
cristy0910f242010-04-01 18:55:09 +0000617 image_view=DestroyCacheView(image_view);
cristy5f1c1ff2010-12-23 21:38:06 +0000618 if (type == UndefinedType)
cristyda06ed12009-10-14 18:36:54 +0000619 return(MagickFalse);
620 ((Image *) image)->type=type;
621 if ((type == GrayscaleType) && (image->matte != MagickFalse))
622 ((Image *) image)->type=GrayscaleMatteType;
623 return(MagickTrue);
624}
625
626/*
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628% %
629% %
630% %
cristy4c08aed2011-07-01 19:47:50 +0000631% I s I m a g e M o n o c h r o m e %
cristyda06ed12009-10-14 18:36:54 +0000632% %
633% %
634% %
635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636%
cristy4c08aed2011-07-01 19:47:50 +0000637% IsImageMonochrome() returns MagickTrue if all the pixels in the image have
cristyda06ed12009-10-14 18:36:54 +0000638% the same red, green, and blue intensities and the intensity is either
639% 0 or QuantumRange.
640%
cristy4c08aed2011-07-01 19:47:50 +0000641% The format of the IsImageMonochrome method is:
cristyda06ed12009-10-14 18:36:54 +0000642%
cristy4c08aed2011-07-01 19:47:50 +0000643% MagickBooleanType IsImageMonochrome(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000644% ExceptionInfo *exception)
645%
646% A description of each parameter follows:
647%
648% o image: the image.
649%
650% o exception: return any errors or warnings in this structure.
651%
652*/
cristy4c08aed2011-07-01 19:47:50 +0000653MagickExport MagickBooleanType IsImageMonochrome(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000654 ExceptionInfo *exception)
655{
cristy0910f242010-04-01 18:55:09 +0000656 CacheView
657 *image_view;
658
cristyda06ed12009-10-14 18:36:54 +0000659 ImageType
660 type;
661
cristybb503372010-05-27 20:51:26 +0000662 register ssize_t
cristy0910f242010-04-01 18:55:09 +0000663 x;
664
cristy4c08aed2011-07-01 19:47:50 +0000665 register const Quantum
cristyda06ed12009-10-14 18:36:54 +0000666 *p;
667
cristy9d314ff2011-03-09 01:30:28 +0000668 ssize_t
669 y;
670
cristyda06ed12009-10-14 18:36:54 +0000671 assert(image != (Image *) NULL);
672 assert(image->signature == MagickSignature);
673 if (image->debug != MagickFalse)
674 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
675 if (image->type == BilevelType)
676 return(MagickTrue);
cristycca1bfa2011-09-12 12:39:53 +0000677 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristyda06ed12009-10-14 18:36:54 +0000678 return(MagickFalse);
679 type=BilevelType;
cristy0910f242010-04-01 18:55:09 +0000680 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000681 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000682 {
cristy0910f242010-04-01 18:55:09 +0000683 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000684 if (p == (const Quantum *) NULL)
cristy0910f242010-04-01 18:55:09 +0000685 break;
cristybb503372010-05-27 20:51:26 +0000686 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000687 {
cristy4c08aed2011-07-01 19:47:50 +0000688 if (IsPixelMonochrome(image,p) == MagickFalse)
cristyda06ed12009-10-14 18:36:54 +0000689 {
cristy5f1c1ff2010-12-23 21:38:06 +0000690 type=UndefinedType;
cristyda06ed12009-10-14 18:36:54 +0000691 break;
cristy0910f242010-04-01 18:55:09 +0000692 }
cristyed231572011-07-14 02:18:59 +0000693 p+=GetPixelChannels(image);
cristyda06ed12009-10-14 18:36:54 +0000694 }
cristy5f1c1ff2010-12-23 21:38:06 +0000695 if (type == UndefinedType)
cristyda06ed12009-10-14 18:36:54 +0000696 break;
cristyda06ed12009-10-14 18:36:54 +0000697 }
cristy0910f242010-04-01 18:55:09 +0000698 image_view=DestroyCacheView(image_view);
cristy5f1c1ff2010-12-23 21:38:06 +0000699 if (type == UndefinedType)
cristyda06ed12009-10-14 18:36:54 +0000700 return(MagickFalse);
701 ((Image *) image)->type=type;
702 return(MagickTrue);
703}
704
705/*
706%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707% %
708% %
709% %
cristy4c08aed2011-07-01 19:47:50 +0000710% I s I m a g e O p a q u e %
cristyda06ed12009-10-14 18:36:54 +0000711% %
712% %
713% %
714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715%
cristy4c08aed2011-07-01 19:47:50 +0000716% IsImageOpaque() returns MagickTrue if none of the pixels in the image have
cristyda06ed12009-10-14 18:36:54 +0000717% an opacity value other than opaque (0).
718%
cristy4c08aed2011-07-01 19:47:50 +0000719% The format of the IsImageOpaque method is:
cristyda06ed12009-10-14 18:36:54 +0000720%
cristy4c08aed2011-07-01 19:47:50 +0000721% MagickBooleanType IsImageOpaque(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000722% ExceptionInfo *exception)
723%
724% A description of each parameter follows:
725%
726% o image: the image.
727%
728% o exception: return any errors or warnings in this structure.
729%
730*/
cristy4c08aed2011-07-01 19:47:50 +0000731MagickExport MagickBooleanType IsImageOpaque(const Image *image,
cristyda06ed12009-10-14 18:36:54 +0000732 ExceptionInfo *exception)
733{
cristyc4c8d132010-01-07 01:58:38 +0000734 CacheView
735 *image_view;
736
cristy4c08aed2011-07-01 19:47:50 +0000737 register const Quantum
cristyda06ed12009-10-14 18:36:54 +0000738 *p;
739
cristybb503372010-05-27 20:51:26 +0000740 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000741 x;
742
cristy9d314ff2011-03-09 01:30:28 +0000743 ssize_t
744 y;
745
cristyda06ed12009-10-14 18:36:54 +0000746 /*
747 Determine if image is opaque.
748 */
749 assert(image != (Image *) NULL);
750 assert(image->signature == MagickSignature);
751 if (image->debug != MagickFalse)
752 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
753 if (image->matte == MagickFalse)
754 return(MagickTrue);
755 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000756 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000757 {
758 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000759 if (p == (const Quantum *) NULL)
cristyda06ed12009-10-14 18:36:54 +0000760 break;
cristybb503372010-05-27 20:51:26 +0000761 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000762 {
cristy4c08aed2011-07-01 19:47:50 +0000763 if (GetPixelAlpha(image,p) != OpaqueAlpha)
cristyda06ed12009-10-14 18:36:54 +0000764 break;
cristyed231572011-07-14 02:18:59 +0000765 p+=GetPixelChannels(image);
cristyda06ed12009-10-14 18:36:54 +0000766 }
cristybb503372010-05-27 20:51:26 +0000767 if (x < (ssize_t) image->columns)
cristyda06ed12009-10-14 18:36:54 +0000768 break;
769 }
770 image_view=DestroyCacheView(image_view);
cristybb503372010-05-27 20:51:26 +0000771 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
cristyda06ed12009-10-14 18:36:54 +0000772}
773
774/*
775%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776% %
777% %
778% %
cristyfefab1b2011-07-05 00:33:22 +0000779% S e t I m a g e D e p t h %
cristyda06ed12009-10-14 18:36:54 +0000780% %
781% %
782% %
783%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784%
cristyfefab1b2011-07-05 00:33:22 +0000785% SetImageDepth() sets the depth of the image.
cristyda06ed12009-10-14 18:36:54 +0000786%
cristyfefab1b2011-07-05 00:33:22 +0000787% The format of the SetImageDepth method is:
cristyda06ed12009-10-14 18:36:54 +0000788%
cristy8a11cb12011-10-19 23:53:34 +0000789% MagickBooleanType SetImageDepth(Image *image,const size_t depth,
790% ExceptionInfo *exception)
cristyda06ed12009-10-14 18:36:54 +0000791%
792% A description of each parameter follows:
793%
794% o image: the image.
795%
796% o channel: the channel.
797%
798% o depth: the image depth.
799%
cristy8a11cb12011-10-19 23:53:34 +0000800% o exception: return any errors or warnings in this structure.
801%
cristyda06ed12009-10-14 18:36:54 +0000802*/
cristyda06ed12009-10-14 18:36:54 +0000803MagickExport MagickBooleanType SetImageDepth(Image *image,
cristy8a11cb12011-10-19 23:53:34 +0000804 const size_t depth,ExceptionInfo *exception)
cristyda06ed12009-10-14 18:36:54 +0000805{
cristyc4c8d132010-01-07 01:58:38 +0000806 CacheView
807 *image_view;
808
cristyda06ed12009-10-14 18:36:54 +0000809 MagickBooleanType
810 status;
811
812 QuantumAny
cristyb6d6f7c2010-06-09 13:17:57 +0000813 range;
cristyda06ed12009-10-14 18:36:54 +0000814
cristy9d314ff2011-03-09 01:30:28 +0000815 ssize_t
816 y;
817
cristyda06ed12009-10-14 18:36:54 +0000818 assert(image != (Image *) NULL);
819 if (image->debug != MagickFalse)
820 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
821 assert(image->signature == MagickSignature);
cristy8a11cb12011-10-19 23:53:34 +0000822 if (GetImageDepth(image,exception) <= (size_t)
cristyda06ed12009-10-14 18:36:54 +0000823 MagickMin((double) depth,(double) MAGICKCORE_QUANTUM_DEPTH))
824 {
825 image->depth=depth;
826 return(MagickTrue);
827 }
828 /*
829 Scale pixels to desired depth.
830 */
831 status=MagickTrue;
cristyb6d6f7c2010-06-09 13:17:57 +0000832 range=GetQuantumRange(depth);
cristyda06ed12009-10-14 18:36:54 +0000833 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000834#if defined(MAGICKCORE_OPENMP_SUPPORT)
835 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000836#endif
cristybb503372010-05-27 20:51:26 +0000837 for (y=0; y < (ssize_t) image->rows; y++)
cristyda06ed12009-10-14 18:36:54 +0000838 {
cristybb503372010-05-27 20:51:26 +0000839 register ssize_t
cristyda06ed12009-10-14 18:36:54 +0000840 x;
841
cristy4c08aed2011-07-01 19:47:50 +0000842 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000843 *restrict q;
cristyda06ed12009-10-14 18:36:54 +0000844
845 if (status == MagickFalse)
846 continue;
cristy8a11cb12011-10-19 23:53:34 +0000847 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000848 if (q == (Quantum *) NULL)
cristyda06ed12009-10-14 18:36:54 +0000849 {
850 status=MagickFalse;
851 continue;
852 }
cristybb503372010-05-27 20:51:26 +0000853 for (x=0; x < (ssize_t) image->columns; x++)
cristyda06ed12009-10-14 18:36:54 +0000854 {
cristy8a11cb12011-10-19 23:53:34 +0000855 register ssize_t
856 i;
857
858 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
859 {
cristyabace412011-12-11 15:56:53 +0000860 PixelChannel
861 channel;
862
cristy8a11cb12011-10-19 23:53:34 +0000863 PixelTrait
864 traits;
865
cristyabace412011-12-11 15:56:53 +0000866 channel=GetPixelChannelMapChannel(image,i);
867 traits=GetPixelChannelMapTraits(image,channel);
cristy8a11cb12011-10-19 23:53:34 +0000868 if (traits == UndefinedPixelTrait)
869 continue;
870 q[i]=ScaleAnyToQuantum(ScaleQuantumToAny(q[i],range),range);
871 }
cristyed231572011-07-14 02:18:59 +0000872 q+=GetPixelChannels(image);
cristyda06ed12009-10-14 18:36:54 +0000873 }
874 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
875 {
876 status=MagickFalse;
877 continue;
878 }
879 }
880 image_view=DestroyCacheView(image_view);
881 if (image->storage_class == PseudoClass)
882 {
cristy101ab702011-10-13 13:06:32 +0000883 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +0000884 *restrict p;
cristyda06ed12009-10-14 18:36:54 +0000885
cristy1c300ae2011-10-09 22:27:43 +0000886 register ssize_t
887 i;
888
cristyda06ed12009-10-14 18:36:54 +0000889 p=image->colormap;
cristyb5d5f722009-11-04 03:03:49 +0000890#if defined(MAGICKCORE_OPENMP_SUPPORT)
891 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristyda06ed12009-10-14 18:36:54 +0000892#endif
cristybb503372010-05-27 20:51:26 +0000893 for (i=0; i < (ssize_t) image->colors; i++)
cristyda06ed12009-10-14 18:36:54 +0000894 {
cristyed231572011-07-14 02:18:59 +0000895 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy9384eed2011-06-13 14:16:32 +0000896 p->red=ScaleAnyToQuantum(ScaleQuantumToAny(p->red,range),range);
cristyed231572011-07-14 02:18:59 +0000897 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy9384eed2011-06-13 14:16:32 +0000898 p->green=ScaleAnyToQuantum(ScaleQuantumToAny(p->green,range),range);
cristyed231572011-07-14 02:18:59 +0000899 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy9384eed2011-06-13 14:16:32 +0000900 p->blue=ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,range),range);
cristyed231572011-07-14 02:18:59 +0000901 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000902 p->alpha=ScaleAnyToQuantum(ScaleQuantumToAny(p->alpha,range),range);
cristyda06ed12009-10-14 18:36:54 +0000903 p++;
904 }
905 }
906 image->depth=depth;
907 return(status);
908}