1 /* 2 Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization 3 dedicated to making software imaging solutions freely available. 4 5 You may not use this file except in compliance with the License. You may 6 obtain a copy of the License at 7 8 https://imagemagick.org/script/license.php 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 16 MagickCore quantum inline methods. 17 */ 18 #ifndef MAGICKCORE_QUANTUM_H 19 #define MAGICKCORE_QUANTUM_H 20 21 #include "MagickCore/image.h" 22 #include "MagickCore/semaphore.h" 23 24 #if defined(__cplusplus) || defined(c_plusplus) 25 extern "C" { 26 #endif 27 28 typedef enum 29 { 30 UndefinedEndian, 31 LSBEndian, 32 MSBEndian 33 } EndianType; 34 35 typedef enum 36 { 37 UndefinedQuantumAlpha, 38 AssociatedQuantumAlpha, 39 DisassociatedQuantumAlpha 40 } QuantumAlphaType; 41 42 typedef enum 43 { 44 UndefinedQuantumFormat, 45 FloatingPointQuantumFormat, 46 SignedQuantumFormat, 47 UnsignedQuantumFormat 48 } QuantumFormatType; 49 50 typedef enum 51 { 52 UndefinedQuantum, 53 AlphaQuantum, 54 BGRAQuantum, 55 BGROQuantum, 56 BGRQuantum, 57 BlackQuantum, 58 BlueQuantum, 59 CbYCrAQuantum, 60 CbYCrQuantum, 61 CbYCrYQuantum, 62 CMYKAQuantum, 63 CMYKOQuantum, 64 CMYKQuantum, 65 CyanQuantum, 66 GrayAlphaQuantum, 67 GrayQuantum, 68 GreenQuantum, 69 IndexAlphaQuantum, 70 IndexQuantum, 71 MagentaQuantum, 72 OpacityQuantum, 73 RedQuantum, 74 RGBAQuantum, 75 RGBOQuantum, 76 RGBPadQuantum, 77 RGBQuantum, 78 YellowQuantum 79 } QuantumType; 80 81 typedef struct _QuantumInfo 82 QuantumInfo; 83 84 static inline Quantum ClampToQuantum(const MagickRealType value) 85 { 86 #if defined(MAGICKCORE_HDRI_SUPPORT) 87 return((Quantum) value); 88 #else 89 if (value <= 0.0f) 90 return((Quantum) 0); 91 if (value >= (MagickRealType) QuantumRange) 92 return(QuantumRange); 93 return((Quantum) (value+0.5f)); 94 #endif 95 } 96 97 #if (MAGICKCORE_QUANTUM_DEPTH == 8) 98 static inline unsigned char ScaleQuantumToChar(const Quantum quantum) 99 { 100 #if !defined(MAGICKCORE_HDRI_SUPPORT) 101 return((unsigned char) quantum); 102 #else 103 if (quantum <= 0.0) 104 return(0); 105 if (quantum >= 255.0) 106 return(255); 107 return((unsigned char) (quantum+0.5)); 108 #endif 109 } 110 #elif (MAGICKCORE_QUANTUM_DEPTH == 16) 111 static inline unsigned char ScaleQuantumToChar(const Quantum quantum) 112 { 113 #if !defined(MAGICKCORE_HDRI_SUPPORT) 114 return((unsigned char) (((quantum+128UL)-((quantum+128UL) >> 8)) >> 8)); 115 #else 116 if (quantum <= 0.0) 117 return(0); 118 if ((quantum/257.0) >= 255.0) 119 return(255); 120 return((unsigned char) (quantum/257.0+0.5)); 121 #endif 122 } 123 #elif (MAGICKCORE_QUANTUM_DEPTH == 32) 124 static inline unsigned char ScaleQuantumToChar(const Quantum quantum) 125 { 126 #if !defined(MAGICKCORE_HDRI_SUPPORT) 127 return((unsigned char) ((quantum+MagickULLConstant(8421504))/ 128 MagickULLConstant(16843009))); 129 #else 130 if (quantum <= 0.0) 131 return(0); 132 if ((quantum/16843009.0) >= 255.0) 133 return(255); 134 return((unsigned char) (quantum/16843009.0+0.5)); 135 #endif 136 } 137 #elif (MAGICKCORE_QUANTUM_DEPTH == 64) 138 static inline unsigned char ScaleQuantumToChar(const Quantum quantum) 139 { 140 #if !defined(MAGICKCORE_HDRI_SUPPORT) 141 return((unsigned char) (quantum/72340172838076673.0+0.5)); 142 #else 143 if (quantum <= 0.0) 144 return(0); 145 if ((quantum/72340172838076673.0) >= 255.0) 146 return(255); 147 return((unsigned char) (quantum/72340172838076673.0+0.5)); 148 #endif 149 } 150 #endif 151 152 extern MagickExport EndianType 153 GetQuantumEndian(const QuantumInfo *); 154 155 extern MagickExport MagickBooleanType 156 SetQuantumDepth(const Image *,QuantumInfo *,const size_t), 157 SetQuantumEndian(const Image *,QuantumInfo *,const EndianType), 158 SetQuantumFormat(const Image *,QuantumInfo *,const QuantumFormatType), 159 SetQuantumPad(const Image *,QuantumInfo *,const size_t); 160 161 extern MagickExport QuantumFormatType 162 GetQuantumFormat(const QuantumInfo *); 163 164 extern MagickExport QuantumInfo 165 *AcquireQuantumInfo(const ImageInfo *,Image *), 166 *DestroyQuantumInfo(QuantumInfo *); 167 168 extern MagickExport QuantumType 169 GetQuantumType(Image *,ExceptionInfo *); 170 171 extern MagickExport size_t 172 ExportQuantumPixels(const Image *,CacheView *,QuantumInfo *,const QuantumType, 173 unsigned char *magick_restrict,ExceptionInfo *), 174 GetQuantumExtent(const Image *,const QuantumInfo *,const QuantumType), 175 ImportQuantumPixels(const Image *,CacheView *,QuantumInfo *,const QuantumType, 176 const unsigned char *magick_restrict,ExceptionInfo *); 177 178 extern MagickExport unsigned char 179 *GetQuantumPixels(const QuantumInfo *); 180 181 extern MagickExport void 182 GetQuantumInfo(const ImageInfo *,QuantumInfo *), 183 SetQuantumAlphaType(QuantumInfo *,const QuantumAlphaType), 184 SetQuantumImageType(Image *,const QuantumType), 185 SetQuantumMinIsWhite(QuantumInfo *,const MagickBooleanType), 186 SetQuantumPack(QuantumInfo *,const MagickBooleanType), 187 SetQuantumQuantum(QuantumInfo *,const size_t), 188 SetQuantumScale(QuantumInfo *,const double); 189 190 #if defined(__cplusplus) || defined(c_plusplus) 191 } 192 #endif 193 194 #endif 195