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