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