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 void InitializeQuantumState(const QuantumInfo *quantum_info,
00092   const EndianType endian,QuantumState *quantum_state)
00093 {
00094   static const unsigned long mask[32] =
00095   {
00096     0x00000000UL, 0x00000001UL, 0x00000003UL, 0x00000007UL, 0x0000000fUL,
00097     0x0000001fUL, 0x0000003fUL, 0x0000007fUL, 0x000000ffUL, 0x000001ffUL,
00098     0x000003ffUL, 0x000007ffUL, 0x00000fffUL, 0x00001fffUL, 0x00003fffUL,
00099     0x00007fffUL, 0x0000ffffUL, 0x0001ffffUL, 0x0003ffffUL, 0x0007ffffUL,
00100     0x000fffffUL, 0x001fffffUL, 0x003fffffUL, 0x007fffffUL, 0x00ffffffUL,
00101     0x01ffffffUL, 0x03ffffffUL, 0x07ffffffUL, 0x0fffffffUL, 0x1fffffffUL,
00102     0x3fffffffUL, 0x7fffffffUL
00103   };
00104 
00105   (void) ResetMagickMemory(quantum_state,0,sizeof(&quantum_state));
00106   quantum_state->endian=endian;
00107   quantum_state->minimum=quantum_info->minimum;
00108   quantum_state->scale=quantum_info->scale;
00109   quantum_state->inverse_scale=0.0;
00110   if (quantum_state->scale != 0.0)
00111     quantum_state->inverse_scale=1.0/quantum_state->scale;
00112   quantum_state->bits=0;
00113   quantum_state->mask=mask;
00114 }
00115 
00116 static inline unsigned char *PopCharPixel(const unsigned char pixel,
00117   unsigned char *pixels)
00118 {
00119   *pixels++=pixel;
00120   return(pixels);
00121 }
00122 
00123 static inline unsigned char *PopLongPixel(const EndianType endian,
00124   const unsigned long pixel,unsigned char *pixels)
00125 {
00126   register unsigned int
00127     quantum;
00128 
00129   quantum=(unsigned int) pixel;
00130   if (endian != LSBEndian)
00131     {
00132       *pixels++=(unsigned char) (quantum >> 24);
00133       *pixels++=(unsigned char) (quantum >> 16);
00134       *pixels++=(unsigned char) (quantum >> 8);
00135       *pixels++=(unsigned char) (quantum);
00136       return(pixels);
00137     }
00138   *pixels++=(unsigned char) (quantum);
00139   *pixels++=(unsigned char) (quantum >> 8);
00140   *pixels++=(unsigned char) (quantum >> 16);
00141   *pixels++=(unsigned char) (quantum >> 24);
00142   return(pixels);
00143 }
00144 
00145 static inline unsigned char *PopShortPixel(const EndianType endian,
00146   const unsigned short pixel,unsigned char *pixels)
00147 {
00148   register unsigned int
00149     quantum;
00150 
00151   quantum=pixel;
00152   if (endian != LSBEndian)
00153     {
00154       *pixels++=(unsigned char) (quantum >> 8);
00155       *pixels++=(unsigned char) (quantum);
00156       return(pixels);
00157     }
00158   *pixels++=(unsigned char) (quantum);
00159   *pixels++=(unsigned char) (quantum >> 8);
00160   return(pixels);
00161 }
00162 
00163 static inline const unsigned char *PushCharPixel(const unsigned char *pixels,
00164   unsigned char *pixel)
00165 {
00166   *pixel=(*pixels++);
00167   return(pixels);
00168 }
00169 
00170 static inline const unsigned char *PushLongPixel(const EndianType endian,
00171   const unsigned char *pixels,unsigned long *pixel)
00172 {
00173   register unsigned int
00174     quantum;
00175 
00176   if (endian != LSBEndian)
00177     {
00178       quantum=(unsigned int) (*pixels++ << 24);
00179       quantum|=(unsigned int) (*pixels++ << 16);
00180       quantum|=(unsigned int) (*pixels++ << 8);
00181       quantum|=(unsigned int) (*pixels++);
00182     }
00183   else
00184     {
00185       quantum=(unsigned int) (*pixels++);
00186       quantum|=(unsigned int) (*pixels++ << 8);
00187       quantum|=(unsigned int) (*pixels++ << 16);
00188       quantum|=(unsigned int) (*pixels++ << 24);
00189     }
00190   *pixel=(unsigned long) (quantum & 0xffffffff);
00191   return(pixels);
00192 }
00193 
00194 static inline const unsigned char *PushShortPixel(const EndianType endian,
00195   const unsigned char *pixels,unsigned short *pixel)
00196 {
00197   register unsigned int
00198     quantum;
00199 
00200   if (endian != LSBEndian)
00201     {
00202       quantum=(unsigned int) (*pixels++ << 8);
00203       quantum|=(unsigned int) *pixels++;
00204     }
00205   else
00206     {
00207       quantum=(unsigned int) *pixels++;
00208       quantum|=(unsigned int) (*pixels++ << 8);
00209     }
00210   *pixel=(unsigned short) (quantum & 0xffff);
00211   return(pixels);
00212 }
00213 
00214 static inline Quantum ScaleAnyToQuantum(const QuantumAny quantum,
00215   const QuantumAny range)
00216 {
00217   return((Quantum) (((MagickRealType) QuantumRange*quantum)/range+0.5));
00218 }
00219 
00220 static inline QuantumAny ScaleQuantumToAny(const Quantum quantum,
00221   const QuantumAny range)
00222 {
00223   return((QuantumAny) (((MagickRealType) range*quantum)/QuantumRange+0.5));
00224 }
00225 
00226 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
00227 static inline Quantum ScaleCharToQuantum(const unsigned char value)
00228 {
00229   return((Quantum) value);
00230 }
00231 
00232 static inline Quantum ScaleLongToQuantum(const unsigned long value)
00233 {
00234 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00235   return((Quantum) ((value+8421504UL)/16843009UL));
00236 #else
00237   return((Quantum) (value/16843009.0));
00238 #endif
00239 }
00240 
00241 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
00242 {
00243 #if defined(MAGICKCORE_HDRI_SUPPORT)
00244   return((Quantum) value);
00245 #else
00246   if (value <= 0.0)
00247     return(0);
00248   if ((value+0.5) >= MaxMap)
00249     return((Quantum) QuantumRange);
00250   return((Quantum) (value+0.5));
00251 #endif
00252 }
00253 
00254 static inline unsigned long ScaleQuantumToLong(const Quantum quantum)
00255 {
00256 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00257   return((unsigned long) (16843009UL*quantum));
00258 #else
00259   if (quantum <= 0.0)
00260     return(0UL);
00261   if ((16843009.0*quantum) >= 4294967295.0)
00262     return(4294967295UL);
00263   return((unsigned long) (16843009.0*quantum+0.5));
00264 #endif
00265 }
00266 
00267 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
00268 {
00269   if (quantum >= (Quantum) MaxMap)
00270     return((unsigned long) MaxMap);
00271 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00272   return((unsigned long) quantum);
00273 #else
00274   if (quantum < 0.0)
00275     return(0UL);
00276   return((unsigned long) (quantum+0.5));
00277 #endif
00278 }
00279 
00280 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
00281 {
00282 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00283   return((unsigned short) (257UL*quantum));
00284 #else
00285   if (quantum <= 0.0)
00286     return(0);
00287   if ((257.0*quantum) >= 65535.0)
00288     return(65535);
00289   return((unsigned short) (257.0*quantum+0.5));
00290 #endif
00291 }
00292 
00293 static inline Quantum ScaleShortToQuantum(const unsigned short value)
00294 {
00295 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00296   return((Quantum) ((value+128UL)/257UL));
00297 #else
00298   return((Quantum) (value/257.0));
00299 #endif
00300 }
00301 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
00302 static inline Quantum ScaleCharToQuantum(const unsigned char value)
00303 {
00304 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00305   return((Quantum) (257UL*value));
00306 #else
00307   return((Quantum) (257.0*value));
00308 #endif
00309 }
00310 
00311 static inline Quantum ScaleLongToQuantum(const unsigned long value)
00312 {
00313 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00314   return((Quantum) ((value+MagickULLConstant(32768))/
00315     MagickULLConstant(65537)));
00316 #else
00317   return((Quantum) (value/65537.0));
00318 #endif
00319 }
00320 
00321 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
00322 {
00323 #if defined(MAGICKCORE_HDRI_SUPPORT)
00324   return((Quantum) value);
00325 #else
00326   if (value <= 0.0)
00327     return(0);
00328   if ((value+0.5) >= MaxMap)
00329     return((Quantum) QuantumRange);
00330   return((Quantum) (value+0.5));
00331 #endif
00332 }
00333 
00334 static inline unsigned long ScaleQuantumToLong(const Quantum quantum)
00335 {
00336 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00337   return((unsigned long) (65537UL*quantum));
00338 #else
00339   if (quantum <= 0.0)
00340     return(0UL);
00341   if ((65537.0*quantum) >= 4294967295.0)
00342     return(4294967295UL);
00343   return((unsigned long) (65537.0*quantum+0.5));
00344 #endif
00345 }
00346 
00347 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
00348 {
00349   if (quantum >= (Quantum) MaxMap)
00350     return((unsigned long) MaxMap);
00351 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00352   return((unsigned long) quantum);
00353 #else
00354   if (quantum < 0.0)
00355     return(0UL);
00356   return((unsigned long) (quantum+0.5));
00357 #endif
00358 }
00359 
00360 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
00361 {
00362 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00363   return((unsigned short) quantum);
00364 #else
00365   if (quantum <= 0.0)
00366     return(0);
00367   if (quantum >= 65535.0)
00368     return(65535);
00369   return((unsigned short) (quantum+0.5));
00370 #endif
00371 }
00372 
00373 static inline Quantum ScaleShortToQuantum(const unsigned short value)
00374 {
00375   return((Quantum) value);
00376 }
00377 #elif (MAGICKCORE_QUANTUM_DEPTH == 32)
00378 static inline Quantum ScaleCharToQuantum(const unsigned char value)
00379 {
00380 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00381   return((Quantum) (16843009UL*value));
00382 #else
00383   return((Quantum) (16843009.0*value));
00384 #endif
00385 }
00386 
00387 static inline Quantum ScaleLongToQuantum(const unsigned long value)
00388 {
00389   return((Quantum) value);
00390 }
00391 
00392 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
00393 {
00394 #if defined(MAGICKCORE_HDRI_SUPPORT)
00395   return((Quantum) (65537.0*value));
00396 #else
00397   if (value <= 0.0)
00398     return(0);
00399   if ((value+0.5) >= MaxMap)
00400     return(QuantumRange);
00401   return((Quantum) (65537UL*value));
00402 #endif
00403 }
00404 
00405 static inline unsigned long ScaleQuantumToLong(const Quantum quantum)
00406 {
00407   return((unsigned long) quantum);
00408 }
00409 
00410 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
00411 {
00412   if ((quantum/65537) >= MaxMap)
00413     return((unsigned long) MaxMap);
00414 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00415   return((unsigned long) ((quantum+MagickULLConstant(32768))/
00416     MagickULLConstant(65537)));
00417 #else
00418   if (quantum < 0.0)
00419     return(0UL);
00420   return((unsigned long) (quantum/65537.0)+0.5);
00421 #endif
00422 }
00423 
00424 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
00425 {
00426 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00427   return((unsigned short) ((quantum+MagickULLConstant(32768))/
00428     MagickULLConstant(65537)));
00429 #else
00430   if (quantum <= 0.0)
00431     return(0);
00432   if ((quantum/65537.0) >= 65535.0)
00433     return(65535);
00434   return((unsigned short) (quantum/65537.0+0.5));
00435 #endif
00436 }
00437 
00438 static inline Quantum ScaleShortToQuantum(const unsigned short value)
00439 {
00440 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00441   return((Quantum) (65537UL*value));
00442 #else
00443   return((Quantum) (65537.0*value));
00444 #endif
00445 }
00446 #elif (MAGICKCORE_QUANTUM_DEPTH == 64)
00447 static inline Quantum ScaleCharToQuantum(const unsigned char value)
00448 {
00449 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00450   return((Quantum) (MagickULLConstant(71777214294589695)*value));
00451 #else
00452   return((Quantum) (71777214294589695.0*value));
00453 #endif
00454 }
00455 
00456 static inline Quantum ScaleLongToQuantum(const unsigned long value)
00457 {
00458 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00459   return((Quantum) (4294967295UL*value));
00460 #else
00461   return((Quantum) (4294967295.0*value));
00462 #endif
00463 }
00464 
00465 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
00466 {
00467 #if defined(MAGICKCORE_HDRI_SUPPORT)
00468   return((Quantum) (281479271612415.0*value));
00469 #else
00470   if (value <= 0.0)
00471     return(0);
00472   if ((value+0.5) >= MaxMap)
00473     return(QuantumRange);
00474   return((Quantum) (MagickULLConstant(281479271612415)*value));
00475 #endif
00476 }
00477 
00478 static inline unsigned long ScaleQuantumToLong(const Quantum quantum)
00479 {
00480 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00481   return((unsigned long) ((quantum+2147483648.0)/4294967297.0));
00482 #else
00483   return((unsigned long) (quantum/4294967297.0+0.5));
00484 #endif
00485 }
00486 
00487 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
00488 {
00489   if ((quantum/281479271612415.0) >= MaxMap)
00490     return((unsigned long) MaxMap);
00491 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00492   return((unsigned long) ((quantum+2147450879.0)/281479271612415.0));
00493 #else
00494   if (quantum < 0.0)
00495     return(0UL);
00496   return((unsigned long) (quantum/281479271612415.0)+0.5);
00497 #endif
00498 }
00499 
00500 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
00501 {
00502 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00503   return((unsigned short) ((quantum+2147450879.0)/281479271612415.0));
00504 #else
00505   return((unsigned short) (quantum/281479271612415.0+0.5));
00506 #endif
00507 }
00508 
00509 static inline Quantum ScaleShortToQuantum(const unsigned short value)
00510 {
00511 #if !defined(MAGICKCORE_HDRI_SUPPORT)
00512   return((Quantum) (MagickULLConstant(281479271612415)*value));
00513 #else
00514   return((Quantum) (281479271612415.0*value));
00515 #endif
00516 }
00517 #endif
00518 
00519 #if defined(__cplusplus) || defined(c_plusplus)
00520 }
00521 #endif
00522 
00523 #endif

Generated on Thu Jul 2 12:03:21 2009 for MagickCore by  doxygen 1.5.8