]> granicus.if.org Git - imagemagick/blob - MagickCore/quantum.c
(no commit message)
[imagemagick] / MagickCore / quantum.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                QQQ   U   U   AAA   N   N  TTTTT  U   U  M   M               %
7 %               Q   Q  U   U  A   A  NN  N    T    U   U  MM MM               %
8 %               Q   Q  U   U  AAAAA  N N N    T    U   U  M M M               %
9 %               Q  QQ  U   U  A   A  N  NN    T    U   U  M   M               %
10 %                QQQQ   UUU   A   A  N   N    T     UUU   M   M               %
11 %                                                                             %
12 %             MagicCore Methods to Acquire / Destroy Quantum Pixels           %
13 %                                                                             %
14 %                             Software Design                                 %
15 %                               John Cristy                                   %
16 %                               October 1998                                  %
17 %                                                                             %
18 %                                                                             %
19 %  Copyright 1999-2008 ImageMagick Studio LLC, a non-profit organization      %
20 %  dedicated to making software imaging solutions freely available.           %
21 %                                                                             %
22 %  You may not use this file except in compliance with the License.  You may  %
23 %  obtain a copy of the License at                                            %
24 %                                                                             %
25 %    http://www.imagemagick.org/script/license.php                            %
26 %                                                                             %
27 %  Unless required by applicable law or agreed to in writing, software        %
28 %  distributed under the License is distributed on an "AS IS" BASIS,          %
29 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
30 %  See the License for the specific language governing permissions and        %
31 %  limitations under the License.                                             %
32 %                                                                             %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %
35 %
36 */
37 \f
38 /*
39   Include declarations.
40 */
41 #include "MagickCore/studio.h"
42 #include "MagickCore/attribute.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/color-private.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/constitute.h"
50 #include "MagickCore/delegate.h"
51 #include "MagickCore/geometry.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/option.h"
57 #include "MagickCore/pixel.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/property.h"
60 #include "MagickCore/quantum.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/resource_.h"
63 #include "MagickCore/semaphore.h"
64 #include "MagickCore/statistic.h"
65 #include "MagickCore/stream.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/string-private.h"
68 #include "MagickCore/thread-private.h"
69 #include "MagickCore/utility.h"
70 \f
71 /*
72   Define declarations.
73 */
74 #define QuantumSignature  0xab
75 \f
76 /*
77   Forward declarations.
78 */
79 static void
80   DestroyQuantumPixels(QuantumInfo *);
81 \f
82 /*
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %                                                                             %
85 %                                                                             %
86 %                                                                             %
87 %   A c q u i r e Q u a n t u m I n f o                                       %
88 %                                                                             %
89 %                                                                             %
90 %                                                                             %
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 %
93 %  AcquireQuantumInfo() allocates the QuantumInfo structure.
94 %
95 %  The format of the AcquireQuantumInfo method is:
96 %
97 %      QuantumInfo *AcquireQuantumInfo(const ImageInfo *image_info,Image *image)
98 %
99 %  A description of each parameter follows:
100 %
101 %    o image_info: the image info.
102 %
103 %    o image: the image.
104 %
105 */
106
107 static inline size_t MagickMax(const size_t x,
108   const size_t y)
109 {
110   if (x > y)
111     return(x);
112   return(y);
113 }
114
115 MagickExport QuantumInfo *AcquireQuantumInfo(const ImageInfo *image_info,
116   Image *image)
117 {
118   MagickBooleanType
119     status;
120
121   QuantumInfo
122     *quantum_info;
123
124   quantum_info=(QuantumInfo *) AcquireMagickMemory(sizeof(*quantum_info));
125   if (quantum_info == (QuantumInfo *) NULL)
126     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
127   quantum_info->signature=MagickSignature;
128   GetQuantumInfo(image_info,quantum_info);
129   if (image == (const Image *) NULL)
130     return(quantum_info);
131   status=SetQuantumDepth(image,quantum_info,image->depth);
132   if (status == MagickFalse)
133     quantum_info=DestroyQuantumInfo(quantum_info);
134   quantum_info->endian=image->endian;
135   return(quantum_info);
136 }
137 \f
138 /*
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 %                                                                             %
141 %                                                                             %
142 %                                                                             %
143 +   A c q u i r e Q u a n t u m P i x e l s                                   %
144 %                                                                             %
145 %                                                                             %
146 %                                                                             %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 %
149 %  AcquireQuantumPixels() allocates the unsigned char structure.
150 %
151 %  The format of the AcquireQuantumPixels method is:
152 %
153 %      MagickBooleanType AcquireQuantumPixels(QuantumInfo *quantum_info,
154 %        const size_t extent)
155 %
156 %  A description of each parameter follows:
157 %
158 %    o quantum_info: the quantum info.
159 %
160 %    o extent: the quantum info.
161 %
162 */
163 static MagickBooleanType AcquireQuantumPixels(QuantumInfo *quantum_info,
164   const size_t extent)
165 {
166   register ssize_t
167     i;
168
169   assert(quantum_info != (QuantumInfo *) NULL);
170   assert(quantum_info->signature == MagickSignature);
171   quantum_info->number_threads=GetOpenMPMaximumThreads();
172   quantum_info->pixels=(unsigned char **) AcquireQuantumMemory(
173     quantum_info->number_threads,sizeof(*quantum_info->pixels));
174   if (quantum_info->pixels == (unsigned char **) NULL)
175     return(MagickFalse);
176   quantum_info->extent=extent;
177   (void) ResetMagickMemory(quantum_info->pixels,0,quantum_info->number_threads*
178     sizeof(*quantum_info->pixels));
179   for (i=0; i < (ssize_t) quantum_info->number_threads; i++)
180   {
181     quantum_info->pixels[i]=(unsigned char *) AcquireQuantumMemory(extent+1,
182       sizeof(**quantum_info->pixels));
183     if (quantum_info->pixels[i] == (unsigned char *) NULL)
184       return(MagickFalse);
185     (void) ResetMagickMemory(quantum_info->pixels[i],0,(extent+1)*
186       sizeof(**quantum_info->pixels));
187     quantum_info->pixels[i][extent]=QuantumSignature;
188   }
189   return(MagickTrue);
190 }
191 \f
192 /*
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 %                                                                             %
195 %                                                                             %
196 %                                                                             %
197 %   D e s t r o y Q u a n t u m I n f o                                       %
198 %                                                                             %
199 %                                                                             %
200 %                                                                             %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 %
203 %  DestroyQuantumInfo() deallocates memory associated with the QuantumInfo
204 %  structure.
205 %
206 %  The format of the DestroyQuantumInfo method is:
207 %
208 %      QuantumInfo *DestroyQuantumInfo(QuantumInfo *quantum_info)
209 %
210 %  A description of each parameter follows:
211 %
212 %    o quantum_info: the quantum info.
213 %
214 */
215 MagickExport QuantumInfo *DestroyQuantumInfo(QuantumInfo *quantum_info)
216 {
217   assert(quantum_info != (QuantumInfo *) NULL);
218   assert(quantum_info->signature == MagickSignature);
219   if (quantum_info->pixels != (unsigned char **) NULL)
220     DestroyQuantumPixels(quantum_info);
221   if (quantum_info->semaphore != (SemaphoreInfo *) NULL)
222     DestroySemaphoreInfo(&quantum_info->semaphore);
223   quantum_info->signature=(~MagickSignature);
224   quantum_info=(QuantumInfo *) RelinquishMagickMemory(quantum_info);
225   return(quantum_info);
226 }
227 \f
228 /*
229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 %                                                                             %
231 %                                                                             %
232 %                                                                             %
233 +   D e s t r o y Q u a n t u m P i x e l s                                   %
234 %                                                                             %
235 %                                                                             %
236 %                                                                             %
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 %
239 %  DestroyQuantumPixels() destroys the quantum pixels.
240 %
241 %  The format of the DestroyQuantumPixels() method is:
242 %
243 %      void DestroyQuantumPixels(QuantumInfo *quantum_info)
244 %
245 %  A description of each parameter follows:
246 %
247 %    o quantum_info: the quantum info.
248 %
249 */
250 static void DestroyQuantumPixels(QuantumInfo *quantum_info)
251 {
252   register ssize_t
253     i;
254
255   ssize_t
256     extent;
257
258   assert(quantum_info != (QuantumInfo *) NULL);
259   assert(quantum_info->signature == MagickSignature);
260   assert(quantum_info->pixels != (unsigned char **) NULL);
261   extent=(ssize_t) quantum_info->extent;
262   for (i=0; i < (ssize_t) quantum_info->number_threads; i++)
263     if (quantum_info->pixels[i] != (unsigned char *) NULL)
264       {
265         /*
266           Did we overrun our quantum buffer?
267         */
268         assert(quantum_info->pixels[i][extent] == QuantumSignature);
269         quantum_info->pixels[i]=(unsigned char *) RelinquishMagickMemory(
270           quantum_info->pixels[i]);
271       }
272   quantum_info->pixels=(unsigned char **) RelinquishMagickMemory(
273     quantum_info->pixels);
274 }
275 \f
276 /*
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 %                                                                             %
279 %                                                                             %
280 %                                                                             %
281 %   G e t Q u a n t u m E x t e n t                                           %
282 %                                                                             %
283 %                                                                             %
284 %                                                                             %
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 %
287 %  GetQuantumExtent() returns the quantum pixel buffer extent.
288 %
289 %  The format of the GetQuantumExtent method is:
290 %
291 %      size_t GetQuantumExtent(Image *image,const QuantumInfo *quantum_info,
292 %        const QuantumType quantum_type)
293 %
294 %  A description of each parameter follows:
295 %
296 %    o image: the image.
297 %
298 %    o quantum_info: the quantum info.
299 %
300 %    o quantum_type: Declare which pixel components to transfer (red, green,
301 %      blue, opacity, RGB, or RGBA).
302 %
303 */
304 MagickExport size_t GetQuantumExtent(const Image *image,
305   const QuantumInfo *quantum_info,const QuantumType quantum_type)
306 {
307   size_t
308     packet_size;
309
310   assert(quantum_info != (QuantumInfo *) NULL);
311   assert(quantum_info->signature == MagickSignature);
312   packet_size=1;
313   switch (quantum_type)
314   {
315     case GrayAlphaQuantum: packet_size=2; break;
316     case IndexAlphaQuantum: packet_size=2; break;
317     case RGBQuantum: packet_size=3; break;
318     case BGRQuantum: packet_size=3; break;
319     case RGBAQuantum: packet_size=4; break;
320     case RGBOQuantum: packet_size=4; break;
321     case BGRAQuantum: packet_size=4; break;
322     case CMYKQuantum: packet_size=4; break;
323     case CMYKAQuantum: packet_size=5; break;
324     default: break;
325   }
326   if (quantum_info->pack == MagickFalse)
327     return((size_t) (packet_size*image->columns*((quantum_info->depth+7)/8)));
328   return((size_t) ((packet_size*image->columns*quantum_info->depth+7)/8));
329 }
330 \f
331 /*
332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 %                                                                             %
334 %                                                                             %
335 %                                                                             %
336 %   G e t Q u a n t u m F o r m a t                                           %
337 %                                                                             %
338 %                                                                             %
339 %                                                                             %
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 %
342 %  GetQuantumFormat() returns the quantum format of the image.
343 %
344 %  The format of the GetQuantumFormat method is:
345 %
346 %      QuantumFormatType GetQuantumFormat(const QuantumInfo *quantum_info)
347 %
348 %  A description of each parameter follows:
349 %
350 %    o quantum_info: the quantum info.
351 %
352 */
353 MagickExport QuantumFormatType GetQuantumFormat(const QuantumInfo *quantum_info)
354 {
355   assert(quantum_info != (QuantumInfo *) NULL);
356   assert(quantum_info->signature == MagickSignature);
357   return(quantum_info->format);
358 }
359 \f
360 /*
361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362 %                                                                             %
363 %                                                                             %
364 %                                                                             %
365 %   G e t Q u a n t u m I n f o                                               %
366 %                                                                             %
367 %                                                                             %
368 %                                                                             %
369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 %
371 %  GetQuantumInfo() initializes the QuantumInfo structure to default values.
372 %
373 %  The format of the GetQuantumInfo method is:
374 %
375 %      GetQuantumInfo(const ImageInfo *image_info,QuantumInfo *quantum_info)
376 %
377 %  A description of each parameter follows:
378 %
379 %    o image_info: the image info.
380 %
381 %    o quantum_info: the quantum info.
382 %
383 */
384 MagickExport void GetQuantumInfo(const ImageInfo *image_info,
385   QuantumInfo *quantum_info)
386 {
387   const char
388     *option;
389
390   assert(quantum_info != (QuantumInfo *) NULL);
391   (void) ResetMagickMemory(quantum_info,0,sizeof(*quantum_info));
392   quantum_info->quantum=8;
393   quantum_info->maximum=1.0;
394   quantum_info->scale=QuantumRange;
395   quantum_info->pack=MagickTrue;
396   quantum_info->semaphore=AllocateSemaphoreInfo();
397   quantum_info->signature=MagickSignature;
398   if (image_info == (const ImageInfo *) NULL)
399     return;
400   option=GetImageOption(image_info,"quantum:format");
401   if (option != (char *) NULL)
402     quantum_info->format=(QuantumFormatType) ParseCommandOption(
403       MagickQuantumFormatOptions,MagickFalse,option);
404   option=GetImageOption(image_info,"quantum:minimum");
405   if (option != (char *) NULL)
406     quantum_info->minimum=StringToDouble(option,(char **) NULL);
407   option=GetImageOption(image_info,"quantum:maximum");
408   if (option != (char *) NULL)
409     quantum_info->maximum=StringToDouble(option,(char **) NULL);
410   if ((quantum_info->minimum == 0.0) && (quantum_info->maximum == 0.0))
411     quantum_info->scale=0.0;
412   else
413     if (quantum_info->minimum == quantum_info->maximum)
414       {
415         quantum_info->scale=(MagickRealType) QuantumRange/quantum_info->minimum;
416         quantum_info->minimum=0.0;
417       }
418     else
419       quantum_info->scale=(MagickRealType) QuantumRange/(quantum_info->maximum-
420         quantum_info->minimum);
421   option=GetImageOption(image_info,"quantum:scale");
422   if (option != (char *) NULL)
423     quantum_info->scale=StringToDouble(option,(char **) NULL);
424   option=GetImageOption(image_info,"quantum:polarity");
425   if (option != (char *) NULL)
426     quantum_info->min_is_white=LocaleCompare(option,"min-is-white") == 0 ?
427       MagickTrue : MagickFalse;
428   quantum_info->endian=image_info->endian;
429   ResetQuantumState(quantum_info);
430 }
431 \f
432 /*
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 %                                                                             %
435 %                                                                             %
436 %                                                                             %
437 %   G e t Q u a n t u m P i x e l s                                           %
438 %                                                                             %
439 %                                                                             %
440 %                                                                             %
441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 %
443 %  GetQuantumPixels() returns the quantum pixels.
444 %
445 %  The format of the GetQuantumPixels method is:
446 %
447 %      unsigned char *QuantumPixels GetQuantumPixels(
448 %        const QuantumInfo *quantum_info)
449 %
450 %  A description of each parameter follows:
451 %
452 %    o image: the image.
453 %
454 */
455 MagickExport unsigned char *GetQuantumPixels(const QuantumInfo *quantum_info)
456 {
457   const int
458     id = GetOpenMPThreadId();
459
460   assert(quantum_info != (QuantumInfo *) NULL);
461   assert(quantum_info->signature == MagickSignature);
462   return(quantum_info->pixels[id]);
463 }
464 \f
465 /*
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 %                                                                             %
468 %                                                                             %
469 %                                                                             %
470 %   G e t Q u a n t u m T y p e                                               %
471 %                                                                             %
472 %                                                                             %
473 %                                                                             %
474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475 %
476 %  GetQuantumType() returns the quantum type of the image.
477 %
478 %  The format of the GetQuantumType method is:
479 %
480 %      QuantumType GetQuantumType(Image *image,ExceptionInfo *exception)
481 %
482 %  A description of each parameter follows:
483 %
484 %    o image: the image.
485 %
486 */
487 MagickExport QuantumType GetQuantumType(Image *image,ExceptionInfo *exception)
488 {
489   QuantumType
490     quantum_type;
491
492   assert(image != (Image *) NULL);
493   assert(image->signature == MagickSignature);
494   if (image->debug != MagickFalse)
495     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
496   quantum_type=RGBQuantum;
497   if (image->matte != MagickFalse)
498     quantum_type=RGBAQuantum;
499   if (image->colorspace == CMYKColorspace)
500     {
501       quantum_type=CMYKQuantum;
502       if (image->matte != MagickFalse)
503         quantum_type=CMYKAQuantum;
504     }
505   if (IsImageGray(image,exception) != MagickFalse)
506     {
507       quantum_type=GrayQuantum;
508       if (image->matte != MagickFalse)
509         quantum_type=GrayAlphaQuantum;
510     }
511   else
512     if (image->storage_class == PseudoClass)
513       {
514         quantum_type=IndexQuantum;
515         if (image->matte != MagickFalse)
516           quantum_type=IndexAlphaQuantum;
517       }
518   return(quantum_type);
519 }
520 \f
521 /*
522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523 %                                                                             %
524 %                                                                             %
525 %                                                                             %
526 +   R e s e t Q u a n t u m S t a t e                                         %
527 %                                                                             %
528 %                                                                             %
529 %                                                                             %
530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
531 %
532 %  ResetQuantumState() resets the quantum state.
533 %
534 %  The format of the ResetQuantumState method is:
535 %
536 %      void ResetQuantumState(QuantumInfo *quantum_info)
537 %
538 %  A description of each parameter follows:
539 %
540 %    o quantum_info: the quantum info.
541 %
542 */
543 MagickPrivate void ResetQuantumState(QuantumInfo *quantum_info)
544 {
545   static const unsigned int mask[32] =
546   {
547     0x00000000U, 0x00000001U, 0x00000003U, 0x00000007U, 0x0000000fU,
548     0x0000001fU, 0x0000003fU, 0x0000007fU, 0x000000ffU, 0x000001ffU,
549     0x000003ffU, 0x000007ffU, 0x00000fffU, 0x00001fffU, 0x00003fffU,
550     0x00007fffU, 0x0000ffffU, 0x0001ffffU, 0x0003ffffU, 0x0007ffffU,
551     0x000fffffU, 0x001fffffU, 0x003fffffU, 0x007fffffU, 0x00ffffffU,
552     0x01ffffffU, 0x03ffffffU, 0x07ffffffU, 0x0fffffffU, 0x1fffffffU,
553     0x3fffffffU, 0x7fffffffU
554   };
555
556   assert(quantum_info != (QuantumInfo *) NULL);
557   assert(quantum_info->signature == MagickSignature);
558   quantum_info->state.inverse_scale=1.0;
559   if (fabs(quantum_info->scale) >= MagickEpsilon)
560     quantum_info->state.inverse_scale/=quantum_info->scale;
561   quantum_info->state.pixel=0U;
562   quantum_info->state.bits=0U;
563   quantum_info->state.mask=mask;
564 }
565 \f
566 /*
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568 %                                                                             %
569 %                                                                             %
570 %                                                                             %
571 %   S e t Q u a n t u m F o r m a t                                           %
572 %                                                                             %
573 %                                                                             %
574 %                                                                             %
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 %
577 %  SetQuantumAlphaType() sets the quantum format.
578 %
579 %  The format of the SetQuantumAlphaType method is:
580 %
581 %      void SetQuantumAlphaType(QuantumInfo *quantum_info,
582 %        const QuantumAlphaType type)
583 %
584 %  A description of each parameter follows:
585 %
586 %    o quantum_info: the quantum info.
587 %
588 %    o type: the alpha type (e.g. associate).
589 %
590 */
591 MagickExport void SetQuantumAlphaType(QuantumInfo *quantum_info,
592   const QuantumAlphaType type)
593 {
594   assert(quantum_info != (QuantumInfo *) NULL);
595   assert(quantum_info->signature == MagickSignature);
596   quantum_info->alpha_type=type;
597 }
598 \f
599 /*
600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601 %                                                                             %
602 %                                                                             %
603 %                                                                             %
604 %   S e t Q u a n t u m D e p t h                                             %
605 %                                                                             %
606 %                                                                             %
607 %                                                                             %
608 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
609 %
610 %  SetQuantumDepth() sets the quantum depth.
611 %
612 %  The format of the SetQuantumDepth method is:
613 %
614 %      MagickBooleanType SetQuantumDepth(const Image *image,
615 %        QuantumInfo *quantum_info,const size_t depth)
616 %
617 %  A description of each parameter follows:
618 %
619 %    o image: the image.
620 %
621 %    o quantum_info: the quantum info.
622 %
623 %    o depth: the quantum depth.
624 %
625 */
626 MagickExport MagickBooleanType SetQuantumDepth(const Image *image,
627   QuantumInfo *quantum_info,const size_t depth)
628 {
629   MagickBooleanType
630     status;
631
632   /*
633     Allocate the quantum pixel buffer.
634   */
635   assert(image != (Image *) NULL);
636   assert(image->signature == MagickSignature);
637   if (image->debug != MagickFalse)
638     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
639   assert(quantum_info != (QuantumInfo *) NULL);
640   assert(quantum_info->signature == MagickSignature);
641   quantum_info->depth=depth;
642   if (quantum_info->format == FloatingPointQuantumFormat)
643     {
644       if (quantum_info->depth > 32)
645         quantum_info->depth=64;
646       else
647         if (quantum_info->depth > 16)
648           quantum_info->depth=32;
649         else
650           quantum_info->depth=16;
651     }
652   if (quantum_info->pixels != (unsigned char **) NULL)
653     DestroyQuantumPixels(quantum_info);
654   status=AcquireQuantumPixels(quantum_info,(6+quantum_info->pad)*image->columns*
655     ((quantum_info->depth+7)/8));  /* allow for CMYKA + RLE byte + pad */
656   return(status);
657 }
658 \f
659 /*
660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
661 %                                                                             %
662 %                                                                             %
663 %                                                                             %
664 %   S e t Q u a n t u m F o r m a t                                           %
665 %                                                                             %
666 %                                                                             %
667 %                                                                             %
668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
669 %
670 %  SetQuantumFormat() sets the quantum format.
671 %
672 %  The format of the SetQuantumFormat method is:
673 %
674 %      MagickBooleanType SetQuantumFormat(const Image *image,
675 %        QuantumInfo *quantum_info,const QuantumFormatType format)
676 %
677 %  A description of each parameter follows:
678 %
679 %    o image: the image.
680 %
681 %    o quantum_info: the quantum info.
682 %
683 %    o format: the quantum format.
684 %
685 */
686 MagickExport MagickBooleanType SetQuantumFormat(const Image *image,
687   QuantumInfo *quantum_info,const QuantumFormatType format)
688 {
689   assert(image != (Image *) NULL);
690   assert(image->signature == MagickSignature);
691   if (image->debug != MagickFalse)
692     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
693   assert(quantum_info != (QuantumInfo *) NULL);
694   assert(quantum_info->signature == MagickSignature);
695   quantum_info->format=format;
696   return(SetQuantumDepth(image,quantum_info,quantum_info->depth));
697 }
698 \f
699 /*
700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
701 %                                                                             %
702 %                                                                             %
703 %                                                                             %
704 %   S e t Q u a n t u m I m a g e T y p e                                     %
705 %                                                                             %
706 %                                                                             %
707 %                                                                             %
708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709 %
710 %  SetQuantumImageType() sets the image type based on the quantum type.
711 %
712 %  The format of the SetQuantumImageType method is:
713 %
714 %      void ImageType SetQuantumImageType(Image *image,
715 %        const QuantumType quantum_type)
716 %
717 %  A description of each parameter follows:
718 %
719 %    o image: the image.
720 %
721 %    o quantum_type: Declare which pixel components to transfer (red, green,
722 %      blue, opacity, RGB, or RGBA).
723 %
724 */
725 MagickExport void SetQuantumImageType(Image *image,
726   const QuantumType quantum_type)
727 {
728   assert(image != (Image *) NULL);
729   assert(image->signature == MagickSignature);
730   if (image->debug != MagickFalse)
731     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
732   switch (quantum_type)
733   {
734     case IndexQuantum:
735     case IndexAlphaQuantum:
736     {
737       image->type=PaletteType;
738       break;
739     }
740     case GrayQuantum:
741     case GrayAlphaQuantum:
742     {
743       image->type=GrayscaleType;
744       if (image->depth == 1)
745         image->type=BilevelType;
746       break;
747     }
748     case CyanQuantum:
749     case MagentaQuantum:
750     case YellowQuantum:
751     case BlackQuantum:
752     case CMYKQuantum:
753     case CMYKAQuantum:
754     {
755       image->type=ColorSeparationType;
756       break;
757     }
758     default:
759     {
760       image->type=TrueColorType;
761       break;
762     }
763   }
764 }
765 \f
766 /*
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768 %                                                                             %
769 %                                                                             %
770 %                                                                             %
771 %   S e t Q u a n t u m P a c k                                               %
772 %                                                                             %
773 %                                                                             %
774 %                                                                             %
775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 %
777 %  SetQuantumPack() sets the quantum pack flag.
778 %
779 %  The format of the SetQuantumPack method is:
780 %
781 %      void SetQuantumPack(QuantumInfo *quantum_info,
782 %        const MagickBooleanType pack)
783 %
784 %  A description of each parameter follows:
785 %
786 %    o quantum_info: the quantum info.
787 %
788 %    o pack: the pack flag.
789 %
790 */
791 MagickExport void SetQuantumPack(QuantumInfo *quantum_info,
792   const MagickBooleanType pack)
793 {
794   assert(quantum_info != (QuantumInfo *) NULL);
795   assert(quantum_info->signature == MagickSignature);
796   quantum_info->pack=pack;
797 }
798 \f
799 \f
800 /*
801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802 %                                                                             %
803 %                                                                             %
804 %                                                                             %
805 %   S e t Q u a n t u m P a d                                                 %
806 %                                                                             %
807 %                                                                             %
808 %                                                                             %
809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810 %
811 %  SetQuantumPad() sets the quantum pad.
812 %
813 %  The format of the SetQuantumPad method is:
814 %
815 %      MagickBooleanType SetQuantumPad(const Image *image,
816 %        QuantumInfo *quantum_info,const size_t pad)
817 %
818 %  A description of each parameter follows:
819 %
820 %    o image: the image.
821 %
822 %    o quantum_info: the quantum info.
823 %
824 %    o pad: the quantum pad.
825 %
826 */
827 MagickExport MagickBooleanType SetQuantumPad(const Image *image,
828   QuantumInfo *quantum_info,const size_t pad)
829 {
830   assert(image != (Image *) NULL);
831   assert(image->signature == MagickSignature);
832   if (image->debug != MagickFalse)
833     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
834   assert(quantum_info != (QuantumInfo *) NULL);
835   assert(quantum_info->signature == MagickSignature);
836   quantum_info->pad=pad;
837   return(SetQuantumDepth(image,quantum_info,quantum_info->depth));
838 }
839 \f
840 /*
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
842 %                                                                             %
843 %                                                                             %
844 %                                                                             %
845 %   S e t Q u a n t u m M i n I s W h i t e                                   %
846 %                                                                             %
847 %                                                                             %
848 %                                                                             %
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850 %
851 %  SetQuantumMinIsWhite() sets the quantum min-is-white flag.
852 %
853 %  The format of the SetQuantumMinIsWhite method is:
854 %
855 %      void SetQuantumMinIsWhite(QuantumInfo *quantum_info,
856 %        const MagickBooleanType min_is_white)
857 %
858 %  A description of each parameter follows:
859 %
860 %    o quantum_info: the quantum info.
861 %
862 %    o min_is_white: the min-is-white flag.
863 %
864 */
865 MagickExport void SetQuantumMinIsWhite(QuantumInfo *quantum_info,
866   const MagickBooleanType min_is_white)
867 {
868   assert(quantum_info != (QuantumInfo *) NULL);
869   assert(quantum_info->signature == MagickSignature);
870   quantum_info->min_is_white=min_is_white;
871 }
872 \f
873 /*
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
875 %                                                                             %
876 %                                                                             %
877 %                                                                             %
878 %   S e t Q u a n t u m Q u a n t u m                                         %
879 %                                                                             %
880 %                                                                             %
881 %                                                                             %
882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
883 %
884 %  SetQuantumQuantum() sets the quantum quantum.
885 %
886 %  The format of the SetQuantumQuantum method is:
887 %
888 %      void SetQuantumQuantum(QuantumInfo *quantum_info,
889 %        const size_t quantum)
890 %
891 %  A description of each parameter follows:
892 %
893 %    o quantum_info: the quantum info.
894 %
895 %    o quantum: the quantum quantum.
896 %
897 */
898 MagickExport void SetQuantumQuantum(QuantumInfo *quantum_info,
899   const size_t quantum)
900 {
901   assert(quantum_info != (QuantumInfo *) NULL);
902   assert(quantum_info->signature == MagickSignature);
903   quantum_info->quantum=quantum;
904 }
905 \f
906 /*
907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
908 %                                                                             %
909 %                                                                             %
910 %                                                                             %
911 %   S e t Q u a n t u m S c a l e                                             %
912 %                                                                             %
913 %                                                                             %
914 %                                                                             %
915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
916 %
917 %  SetQuantumScale() sets the quantum scale.
918 %
919 %  The format of the SetQuantumScale method is:
920 %
921 %      void SetQuantumScale(QuantumInfo *quantum_info,const double scale)
922 %
923 %  A description of each parameter follows:
924 %
925 %    o quantum_info: the quantum info.
926 %
927 %    o scale: the quantum scale.
928 %
929 */
930 MagickExport void SetQuantumScale(QuantumInfo *quantum_info,const double scale)
931 {
932   assert(quantum_info != (QuantumInfo *) NULL);
933   assert(quantum_info->signature == MagickSignature);
934   quantum_info->scale=scale;
935 }