quantum-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2008 ImageMagick Studio LLC, a non-profit organization
00003   dedicated to making software imaging solutions freely available.
00004 
00005   You may not use this file except in compliance with the License.
00006   obtain a copy of the License at
00007 
00008     http://www.imagemagick.org/script/license.php
00009 
00010   Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013   See the License for the specific language governing permissions and
00014   limitations under the License.
00015 
00016   MagickCore quantum inline methods.
00017 */
00018 #ifndef _MAGICKCORE_QUANTUM_PRIVATE_H
00019 #define _MAGICKCORE_QUANTUM_PRIVATE_H
00020 
00021 #if defined(__cplusplus) || defined(c_plusplus)
00022 extern "C" {
00023 #endif
00024 
00025 #include "magick/cache.h"
00026 
00027 typedef struct _QuantumState
00028 {
00029   EndianType
00030     endian;
00031 
00032   double
00033     minimum,
00034     scale,
00035     inverse_scale;
00036 
00037   unsigned long
00038     pixel,
00039     bits;
00040 
00041   const unsigned long
00042     *mask;
00043 } QuantumState;
00044 
00045 struct _QuantumInfo
00046 {
00047   unsigned long
00048     depth,
00049     quantum;
00050 
00051   QuantumFormatType
00052     format;
00053 
00054   double
00055     minimum,
00056     maximum,
00057     scale;
00058 
00059   size_t
00060     pad;
00061 
00062   MagickBooleanType
00063     min_is_white,
00064     pack;
00065 
00066   QuantumAlphaType
00067     alpha_type;
00068 
00069   unsigned long
00070     number_threads;
00071 
00072   unsigned char
00073     **pixels;
00074 
00075   size_t
00076     extent;
00077 
00078   SemaphoreInfo
00079     *semaphore;
00080 
00081   unsigned long
00082     signature;
00083 };
00084 
00085 static inline MagickSizeType GetQuantumRange(const unsigned long depth)
00086 {
00087   return((MagickSizeType) ((MagickULLConstant(1) << (depth-1))+
00088     ((MagickULLConstant(1) << (depth-1))-1)));
00089 }
00090 
00091 static inline QuantumAny GetQuantumScale(const unsigned long depth)
00092 {
00093   if ((depth == 0) || (depth == MAGICKCORE_QUANTUM_DEPTH))
00094     return(1);
00095   if (depth < MAGICKCORE_QUANTUM_DEPTH)
00096     return((QuantumAny) (QuantumRange/((MagickSizeType) QuantumRange >>
00097       (MAGICKCORE_QUANTUM_DEPTH-depth))));
00098   return((QuantumAny) ((MagickSizeType) ((MagickULLConstant(1) << (depth-1))+
00099     ((MagickULLConstant(1) << (depth-1))-1))/QuantumRange));
00100 }
00101 
00102 static inline void InitializeQuantumState(const QuantumInfo *quantum_info,
00103   const EndianType endian,QuantumState *quantum_state)
00104 {
00105   static const unsigned long mask[32] =
00106   {
00107     0x00000000UL, 0x00000001UL, 0x00000003UL, 0x00000007UL, 0x0000000fUL,
00108     0x0000001fUL, 0x0000003fUL, 0x0000007fUL, 0x000000ffUL, 0x000001ffUL,
00109     0x000003ffUL, 0x000007ffUL, 0x00000fffUL, 0x00001fffUL, 0x00003fffUL,
00110     0x00007fffUL, 0x0000ffffUL, 0x0001ffffUL, 0x0003ffffUL, 0x0007ffffUL,
00111     0x000fffffUL, 0x001fffffUL, 0x003fffffUL, 0x007fffffUL, 0x00ffffffUL,
00112     0x01ffffffUL, 0x03ffffffUL, 0x07ffffffUL, 0x0fffffffUL, 0x1fffffffUL,
00113     0x3fffffffUL, 0x7fffffffUL
00114   };
00115 
00116   (void) ResetMagickMemory(quantum_state,0,sizeof(&quantum_state));
00117   quantum_state->endian=endian;
00118   quantum_state->minimum=quantum_info->minimum;
00119   quantum_state->scale=quantum_info->scale;
00120   quantum_state->inverse_scale=0.0;
00121   if (quantum_state->scale != 0.0)
00122     quantum_state->inverse_scale=1.0/quantum_state->scale;
00123   quantum_state->bits=0;
00124   quantum_state->mask=mask;
00125 }
00126 
00127 static inline unsigned char *PopCharPixel(const unsigned char pixel,
00128   unsigned char *pixels)
00129 {
00130   *pixels++=pixel;
00131   return(pixels);
00132 }
00133 
00134 static inline unsigned char *PopLongPixel(const EndianType endian,
00135   const unsigned long pixel,unsigned char *pixels)
00136 {
00137   register unsigned int
00138     quantum;
00139 
00140   quantum=(unsigned int) pixel;
00141   if (endian != LSBEndian)
00142     {
00143       *pixels++=(unsigned char) (quantum >> 24);
00144       *pixels++=(unsigned char) (quantum >> 16);
00145       *pixels++=(unsigned char) (quantum >> 8);
00146       *pixels++=(unsigned char) (quantum);
00147       return(pixels);
00148     }
00149   *pixels++=(unsigned char) (quantum);
00150   *pixels++=(unsigned char) (quantum >> 8);
00151   *pixels++=(unsigned char) (quantum >> 16);
00152   *pixels++=(unsigned char) (quantum >> 24);
00153   return(pixels);
00154 }
00155 
00156 static inline unsigned char *PopShortPixel(const EndianType endian,
00157   const unsigned short pixel,unsigned char *pixels)
00158 {
00159   register unsigned int
00160     quantum;
00161 
00162   quantum=pixel;
00163   if (endian != LSBEndian)
00164     {
00165       *pixels++=(unsigned char) (quantum >> 8);
00166       *pixels++=(unsigned char) (quantum);
00167       return(pixels);
00168     }
00169   *pixels++=(unsigned char) (quantum);
00170   *pixels++=(unsigned char) (quantum >> 8);
00171   return(pixels);
00172 }
00173 
00174 static inline const unsigned char *PushCharPixel(const unsigned char *pixels,
00175   unsigned char *pixel)
00176 {
00177   *pixel=(*pixels++);
00178   return(pixels);
00179 }
00180 
00181 static inline const unsigned char *PushLongPixel(const EndianType endian,
00182   const unsigned char *pixels,unsigned long *pixel)
00183 {
00184   register unsigned int
00185     quantum;
00186 
00187   if (endian != LSBEndian)
00188     {
00189       quantum=(unsigned int) (*pixels++ << 24);
00190       quantum|=(unsigned int) (*pixels++ << 16);
00191       quantum|=(unsigned int) (*pixels++ << 8);
00192       quantum|=(unsigned int) (*pixels++);
00193     }
00194   else
00195     {
00196       quantum=(unsigned int) (*pixels++);
00197       quantum|=(unsigned int) (*pixels++ << 8);
00198       quantum|=(unsigned int) (*pixels++ << 16);
00199       quantum|=(unsigned int) (*pixels++ << 24);
00200     }
00201   *pixel=(unsigned long) (quantum & 0xffffffff);
00202   return(pixels);
00203 }
00204 
00205 static inline const unsigned char *PushShortPixel(const EndianType endian,
00206   const unsigned char *pixels,unsigned short *pixel)
00207 {
00208   register unsigned int
00209     quantum;
00210 
00211   if (endian != LSBEndian)
00212     {
00213       quantum=(unsigned int) (*pixels++ << 8);
00214       quantum|=(unsigned int) *pixels++;
00215     }
00216   else
00217     {
00218       quantum=(unsigned int) *pixels++;
00219       quantum|=(unsigned int) (*pixels++ << 8);
00220     }
00221   *pixel=(unsigned short) (quantum & 0xffff);
00222   return(pixels);
00223 }
00224 
00225 static inline Quantum ScaleAnyToQuantum(const QuantumAny quantum,
00226   const unsigned long depth,const QuantumAny scale)
00227 {
00228   if (depth == MAGICKCORE_QUANTUM_DEPTH)
00229     return((Quantum) quantum);
00230   if (depth > MAGICKCORE_QUANTUM_DEPTH)
00231     return((Quantum) (quantum/scale));
00232   return((Quantum) (scale*quantum));
00233 }
00234 
00235 static inline QuantumAny ScaleQuantumToAny(const Quantum quantum,
00236   const unsigned long depth,const QuantumAny scale)
00237 {
00238   if (depth == MAGICKCORE_QUANTUM_DEPTH)
00239     return((QuantumAny) quantum);
00240   if (depth < MAGICKCORE_QUANTUM_DEPTH)
00241     return((QuantumAny) (quantum/scale));
00242   return((QuantumAny) (scale*quantum));
00243 }
00244 
00245 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
00246 static inline Quantum ScaleCharToQuantum(const unsigned char value)
00247 {
00248   return((Quantum) value);
00249 }
00250 
00251 static inline Quantum ScaleLongToQuantum(const unsigned long value)
00252 {
00253 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00254   return((Quantum) ((value+8421504UL)/16843009UL));
00255 #else
00256   return((Quantum) (value/16843009.0));
00257 #endif
00258 }
00259 
00260 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
00261 {
00262 #if defined(MAGICKCORE_HDRI_SUPPORT)
00263   return((Quantum) value);
00264 #else
00265   if (value <= 0.0)
00266     return(0);
00267   if ((value+0.5) >= MaxMap)
00268     return((Quantum) QuantumRange);
00269   return((Quantum) (value+0.5));
00270 #endif
00271 }
00272 
00273 static inline unsigned long ScaleQuantumToLong(const Quantum quantum)
00274 {
00275 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00276   return((unsigned long) (16843009UL*quantum));
00277 #else
00278   if (quantum <= 0.0)
00279     return(0UL);
00280   if ((16843009.0*quantum) >= 4294967295.0)
00281     return(4294967295UL);
00282   return((unsigned long) (16843009.0*quantum+0.5));
00283 #endif
00284 }
00285 
00286 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
00287 {
00288   if (quantum >= (Quantum) MaxMap)
00289     return((unsigned long) MaxMap);
00290 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00291   return((unsigned long) quantum);
00292 #else
00293   if (quantum < 0.0)
00294     return(0UL);
00295   return((unsigned long) (quantum+0.5));
00296 #endif
00297 }
00298 
00299 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
00300 {
00301 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00302   return((unsigned short) (257UL*quantum));
00303 #else
00304   if (quantum <= 0.0)
00305     return(0);
00306   if ((257.0*quantum) >= 65535.0)
00307     return(65535);
00308   return((unsigned short) (257.0*quantum+0.5));
00309 #endif
00310 }
00311 
00312 static inline Quantum ScaleShortToQuantum(const unsigned short value)
00313 {
00314 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00315   return((Quantum) ((value+128UL)/257UL));
00316 #else
00317   return((Quantum) (value/257.0));
00318 #endif
00319 }
00320 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
00321 static inline Quantum ScaleCharToQuantum(const unsigned char value)
00322 {
00323 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00324   return((Quantum) (257UL*value));
00325 #else
00326   return((Quantum) (257.0*value));
00327 #endif
00328 }
00329 
00330 static inline Quantum ScaleLongToQuantum(const unsigned long value)
00331 {
00332 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00333   return((Quantum) ((value+MagickULLConstant(32768))/
00334     MagickULLConstant(65537)));
00335 #else
00336   return((Quantum) (value/65537.0));
00337 #endif
00338 }
00339 
00340 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
00341 {
00342 #if defined(MAGICKCORE_HDRI_SUPPORT)
00343   return((Quantum) value);
00344 #else
00345   if (value <= 0.0)
00346     return(0);
00347   if ((value+0.5) >= MaxMap)
00348     return((Quantum) QuantumRange);
00349   return((Quantum) (value+0.5));
00350 #endif
00351 }
00352 
00353 static inline unsigned long ScaleQuantumToLong(const Quantum quantum)
00354 {
00355 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00356   return((unsigned long) (65537UL*quantum));
00357 #else
00358   if (quantum <= 0.0)
00359     return(0UL);
00360   if ((65537.0*quantum) >= 4294967295.0)
00361     return(4294967295UL);
00362   return((unsigned long) (65537.0*quantum+0.5));
00363 #endif
00364 }
00365 
00366 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
00367 {
00368   if (quantum >= (Quantum) MaxMap)
00369     return((unsigned long) MaxMap);
00370 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00371   return((unsigned long) quantum);
00372 #else
00373   if (quantum < 0.0)
00374     return(0UL);
00375   return((unsigned long) (quantum+0.5));
00376 #endif
00377 }
00378 
00379 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
00380 {
00381 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00382   return((unsigned short) quantum);
00383 #else
00384   if (quantum <= 0.0)
00385     return(0);
00386   if (quantum >= 65535.0)
00387     return(65535);
00388   return((unsigned short) (quantum+0.5));
00389 #endif
00390 }
00391 
00392 static inline Quantum ScaleShortToQuantum(const unsigned short value)
00393 {
00394   return((Quantum) value);
00395 }
00396 #elif (MAGICKCORE_QUANTUM_DEPTH == 32)
00397 static inline Quantum ScaleCharToQuantum(const unsigned char value)
00398 {
00399 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00400   return((Quantum) (16843009UL*value));
00401 #else
00402   return((Quantum) (16843009.0*value));
00403 #endif
00404 }
00405 
00406 static inline Quantum ScaleLongToQuantum(const unsigned long value)
00407 {
00408   return((Quantum) value);
00409 }
00410 
00411 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
00412 {
00413 #if defined(MAGICKCORE_HDRI_SUPPORT)
00414   return((Quantum) (65537.0*value));
00415 #else
00416   if (value <= 0.0)
00417     return(0);
00418   if ((value+0.5) >= MaxMap)
00419     return(QuantumRange);
00420   return((Quantum) (65537UL*value));
00421 #endif
00422 }
00423 
00424 static inline unsigned long ScaleQuantumToLong(const Quantum quantum)
00425 {
00426   return((unsigned long) quantum);
00427 }
00428 
00429 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
00430 {
00431   if ((quantum/65537) >= MaxMap)
00432     return((unsigned long) MaxMap);
00433 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00434   return((unsigned long) ((quantum+MagickULLConstant(32768))/
00435     MagickULLConstant(65537)));
00436 #else
00437   if (quantum < 0.0)
00438     return(0UL);
00439   return((unsigned long) (quantum/65537.0)+0.5);
00440 #endif
00441 }
00442 
00443 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
00444 {
00445 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00446   return((unsigned short) ((quantum+MagickULLConstant(32768))/
00447     MagickULLConstant(65537)));
00448 #else
00449   if (quantum <= 0.0)
00450     return(0);
00451   if ((quantum/65537.0) >= 65535.0)
00452     return(65535);
00453   return((unsigned short) (quantum/65537.0+0.5));
00454 #endif
00455 }
00456 
00457 static inline Quantum ScaleShortToQuantum(const unsigned short value)
00458 {
00459 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00460   return((Quantum) (65537UL*value));
00461 #else
00462   return((Quantum) (65537.0*value));
00463 #endif
00464 }
00465 #elif (MAGICKCORE_QUANTUM_DEPTH == 64)
00466 static inline Quantum ScaleCharToQuantum(const unsigned char value)
00467 {
00468 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00469   return((Quantum) (MagickULLConstant(71777214294589695)*value));
00470 #else
00471   return((Quantum) (71777214294589695.0*value));
00472 #endif
00473 }
00474 
00475 static inline Quantum ScaleLongToQuantum(const unsigned long value)
00476 {
00477 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00478   return((Quantum) (4294967295UL*value));
00479 #else
00480   return((Quantum) (4294967295.0*value));
00481 #endif
00482 }
00483 
00484 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
00485 {
00486 #if defined(MAGICKCORE_HDRI_SUPPORT)
00487   return((Quantum) (281479271612415.0*value));
00488 #else
00489   if (value <= 0.0)
00490     return(0);
00491   if ((value+0.5) >= MaxMap)
00492     return(QuantumRange);
00493   return((Quantum) (MagickULLConstant(281479271612415)*value));
00494 #endif
00495 }
00496 
00497 static inline unsigned long ScaleQuantumToLong(const Quantum quantum)
00498 {
00499 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00500   return((unsigned long) ((quantum+2147483648.0)/4294967297.0));
00501 #else
00502   return((unsigned long) (quantum/4294967297.0+0.5));
00503 #endif
00504 }
00505 
00506 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
00507 {
00508   if ((quantum/281479271612415.0) >= MaxMap)
00509     return((unsigned long) MaxMap);
00510 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00511   return((unsigned long) ((quantum+2147450879.0)/281479271612415.0));
00512 #else
00513   if (quantum < 0.0)
00514     return(0UL);
00515   return((unsigned long) (quantum/281479271612415.0)+0.5);
00516 #endif
00517 }
00518 
00519 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
00520 {
00521 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00522   return((unsigned short) ((quantum+2147450879.0)/281479271612415.0));
00523 #else
00524   return((unsigned short) (quantum/281479271612415.0+0.5));
00525 #endif
00526 }
00527 
00528 static inline Quantum ScaleShortToQuantum(const unsigned short value)
00529 {
00530 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00531   return((Quantum) (MagickULLConstant(281479271612415)*value));
00532 #else
00533   return((Quantum) (281479271612415.0*value));
00534 #endif
00535 }
00536 #endif
00537 
00538 #if defined(__cplusplus) || defined(c_plusplus)
00539 }
00540 #endif
00541 
00542 #endif

Generated on Tue Dec 30 16:17:45 2008 for MagickCore by  doxygen 1.5.7.1