1 /** \file 2 * \brief Utilities 3 * 4 * See Copyright Notice in im_lib.d 5 */ 6 7 module im.util; 8 9 extern (C) @safe nothrow: 10 11 12 /** \defgroup util Utilities 13 * \par 14 * See \ref im_util.h 15 * @{ 16 */ 17 18 pragma(inline, true) { 19 20 auto IM_MIN(T, U)(T _a, U _b) { 21 return _a < _b ? _a : _b; 22 } 23 24 auto IM_MAX(T, U)(T _a, U _b) { 25 return _a > _b ? _a : _b; 26 } 27 28 } 29 30 /** @} */ 31 32 33 /** \defgroup str String Utilities 34 * \par 35 * See \ref im_util.h 36 * \ingroup util */ 37 38 /** Check if the two strings are equal. 39 * \ingroup str */ 40 int imStrEqual(const(char) * str1, const(char) * str2); 41 42 /** Calculate the size of the string but limited to max_len. 43 * \ingroup str */ 44 int imStrNLen(const(char) * str, int max_len); 45 46 /** Check if the data is a string. 47 * \ingroup str */ 48 int imStrCheck(const(void) * data, int count); 49 50 51 52 /** \defgroup imageutil Raw Data Utilities 53 * \par 54 * See \ref im_util.h 55 * \ingroup imagerep */ 56 57 /** Returns the size of the data buffer. 58 * 59 * \verbatim im.ImageDataSize(width: number, height: number, color_mode: number, data_type: number) -> datasize: number [in Lua 5] \endverbatim 60 * \ingroup imageutil */ 61 int imImageDataSize(int width, int height, int color_mode, int data_type); 62 63 /** Returns the size of one line of the data buffer. \n 64 * This depends if the components are packed. If packed includes all components, if not includes only one. 65 * 66 * \verbatim im.ImageLineSize(width: number, color_mode: number, data_type: number) -> linesize: number [in Lua 5] \endverbatim 67 * \ingroup imageutil */ 68 int imImageLineSize(int width, int color_mode, int data_type); 69 70 /** Returns the number of elements of one line of the data buffer. \n 71 * This depends if the components are packed. If packed includes all components, if not includes only one. 72 * 73 * \verbatim im.ImageLineCount(width: number, color_mode: number) -> linecount: number [in Lua 5] \endverbatim 74 * \ingroup imageutil */ 75 int imImageLineCount(int width, int color_mode); 76 77 /** Check if the combination color_mode+data_type is valid. 78 * 79 * \verbatim im.ImageCheckFormat(color_mode: number, data_type: number) -> check: boolean [in Lua 5] \endverbatim 80 * \ingroup imageutil */ 81 int imImageCheckFormat(int color_mode, int data_type); 82 83 84 85 /** \defgroup colorutl Color Utilities 86 * \par 87 * See \ref im_util.h 88 * \ingroup util */ 89 90 /** Encode RGB components in a long for palette usage. \n 91 * "long" definition is compatible with the CD library definition. 92 * 93 * \verbatim im.ColorEncode(red: number, green: number, blue: number) -> color: lightuserdata [in Lua 5] \endverbatim 94 * \ingroup colorutl */ 95 long imColorEncode(ubyte red, ubyte green, ubyte blue); 96 97 /** Decode RGB components from a long for palette usage. \n 98 * "long" definition is compatible with the CD library definition. 99 * 100 * \verbatim im.ColorDecode(color: lightuserdata) -> red: number, green: number, blue: number [in Lua 5] \endverbatim 101 * \ingroup colorutl */ 102 void imColorDecode(ubyte *red, ubyte *green, ubyte *blue, long color); 103 104 105 106 /** \defgroup colormodeutl Color Mode Utilities 107 * \par 108 * See \ref im_util.h 109 * \ingroup imagerep */ 110 111 /** Returns the color mode name. 112 * 113 * \verbatim im.ColorModeSpaceName(color_mode: number) -> name: string [in Lua 5] \endverbatim 114 * \ingroup colormodeutl */ 115 const(char) * imColorModeSpaceName(int color_mode); 116 117 /** Returns the number of components of the color space including alpha. 118 * 119 * \verbatim im.ColorModeDepth(color_mode: number) -> depth: number [in Lua 5] \endverbatim 120 * \ingroup colormodeutl */ 121 int imColorModeDepth(int color_mode); 122 123 pragma(inline, true) { 124 /** Returns the color space of the color mode. 125 * 126 * \verbatim im.ColorModeSpace(color_mode: number) -> color_space: number [in Lua 5] \endverbatim 127 * \ingroup colormodeutl */ 128 auto imColorModeSpace(T)(T _cm) { 129 return _cm & 0xFF; 130 } 131 132 /** Check if the two color modes match. Only the color space is compared. 133 * 134 * \verbatim im.ColorModeMatch(color_mode1: number, color_mode2: number) -> match: boolean [in Lua 5] \endverbatim 135 * \ingroup colormodeutl */ 136 auto imColorModeMatch(T, U)(T _cm1, U _cm2) { 137 return imColorModeSpace(_cm1) == imColorModeSpace(_cm2); 138 } 139 140 /** Check if the color mode has an alpha channel. 141 * 142 * \verbatim im.ColorModeHasAlpha(color_mode: number) -> has_alpha: boolean [in Lua 5] \endverbatim 143 * \ingroup colormodeutl */ 144 auto imColorModeHasAlpha(T)(T _cm) { 145 return _cm & IM_ALPHA; 146 } 147 148 /** Check if the color mode components are packed in one plane. 149 * 150 * \verbatim im.ColorModeIsPacked(color_mode: number) -> is_packed: boolean [in Lua 5] \endverbatim 151 * \ingroup colormodeutl */ 152 auto imColorModeIsPacked(T)(T _cm) { 153 return _cm & IM_PACKED; 154 } 155 156 /** Check if the color mode orients the image from top down to bottom. 157 * 158 * \verbatim im.ColorModeIsTopDown(color_mode: number) -> is_top_down: boolean [in Lua 5] \endverbatim 159 * \ingroup colormodeutl */ 160 auto imColorModeIsTopDown(T)(T _cm) { 161 return _cm & IM_TOPDOWN; 162 } 163 164 } 165 166 /** Returns the color space of the equivalent display bitmap image. \n 167 * Original packing and alpha are ignored. Returns IM_RGB, IM_GRAY, IM_MAP or IM_BINARY. 168 * 169 * \verbatim im.ColorModeToBitmap(color_mode: number) -> color_space: number [in Lua 5] \endverbatim 170 * \ingroup colormodeutl */ 171 int imColorModeToBitmap(int color_mode); 172 173 /** Check if the color mode and data_type defines a display bitmap image. 174 * 175 * \verbatim im.ColorModeIsBitmap(color_mode: number, data_type: number) -> is_bitmap: boolean [in Lua 5] \endverbatim 176 * \ingroup colormodeutl */ 177 int imColorModeIsBitmap(int color_mode, int data_type); 178 179 /** Max depth is 4+1 (cmyk+alpha) 180 * \ingroup colormodeutl */ 181 enum IM_MAXDEPTH = 5; 182 183 184 185 /** \defgroup datatypeutl Data Type Utilities 186 * \par 187 * See \ref im_util.h 188 * \ingroup util 189 * @{ 190 */ 191 192 alias imbyte = ubyte; 193 alias imushort = ushort; 194 195 pragma(inline, true) { 196 197 auto IM_BYTECROP(T)(T _v) { return (_v < 0? 0: _v > 255? 255: _v); } 198 auto IM_FLOATCROP(T)(T _v) { return (_v < 0? 0: _v > 1.0f? 1.0f: _v); } 199 auto IM_CROPMAX(T, U)(T _v, U _max) { return (_v < 0? 0: _v > _max? _max: _v); } 200 auto IM_CROPMINMAX(T, U, V)(T _v, U _min, V _max) { return (_v < _min? _min: _v > _max? _max: _v); } 201 202 } 203 204 /** @} */ 205 206 /** Returns the size in bytes of a specified numeric data type. 207 * 208 * \verbatim im.DataTypeSize(data_type: number) -> size: number [in Lua 5] \endverbatim 209 * \ingroup datatypeutl */ 210 int imDataTypeSize(int data_type); 211 212 /** Returns the numeric data type name given its identifier. 213 * 214 * \verbatim im.DataTypeName(data_type: number) -> name: string [in Lua 5] \endverbatim 215 * \ingroup datatypeutl */ 216 const(char) * imDataTypeName(int data_type); 217 218 /** Returns the maximum value of an integer data type. For floating point returns 0. 219 * 220 * \verbatim im.DataTypeIntMax(data_type: number) -> int_max: number [in Lua 5] \endverbatim 221 * \ingroup datatypeutl */ 222 ulong imDataTypeIntMax(int data_type); 223 224 /** Returns the minimum value of an integer data type. For floating point returns 0. 225 * 226 * \verbatim im.DataTypeIntMin(data_type: number) -> int_min: number [in Lua 5] \endverbatim 227 * \ingroup datatypeutl */ 228 long imDataTypeIntMin(int data_type); 229 230 231 232 /** \defgroup bin Binary Data Utilities 233 * \par 234 * See \ref im_util.h 235 * \ingroup util */ 236 237 /** CPU Byte Orders. 238 * \ingroup bin */ 239 enum imByteOrder 240 { 241 IM_LITTLEENDIAN, /**< Little Endian - The most significant byte is on the right end of a word. Used by Intel processors. */ 242 IM_BIGENDIAN /**< Big Endian - The most significant byte is on the left end of a word. Used by Motorola processors, also is the network standard byte order. */ 243 }; 244 245 /** Returns the current CPU byte order. 246 * \ingroup bin */ 247 int imBinCPUByteOrder(); 248 249 /** Changes the byte order of an array of 2, 4 or 8 byte values. 250 * \ingroup bin */ 251 void imBinSwapBytes(void *data, int count, int size); 252 253 /** Changes the byte order of an array of 2 byte values. 254 * \ingroup bin */ 255 void imBinSwapBytes2(void *data, int count); 256 257 /** Inverts the byte order of the 4 byte values 258 * \ingroup bin */ 259 void imBinSwapBytes4(void *data, int count); 260 261 /** Inverts the byte order of the 8 byte values 262 * \ingroup bin */ 263 void imBinSwapBytes8(void *data, int count); 264 265 266 267 /** \defgroup compress Data Compression Utilities 268 * \par 269 * Deflate compression support uses zlib version 1.2.5. \n 270 * http://www.zlib.org/ \n 271 * Copyright (C) 1995-2011 Jean-loup Gailly and Mark Adler 272 * \par 273 * LZF compression support uses libLZF version 3.5. \n 274 * http://software.schmorp.de/pkg/liblzf \n 275 * Copyright (C) 2000-2009 Marc Alexander Lehmann 276 * \par 277 * LZO compression support uses mini-libLZO version 2.07. \n 278 * http://www.oberhumer.com/opensource/lzo/ \n 279 * Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer \n 280 * But its License is GPL, so we kept it in a separate library 281 * called "im_lzo" that is also GPL. 282 * 283 * See \ref im_util.h 284 * \ingroup util */ 285 286 /** Compresses the data using the ZLIB Deflate compression. \n 287 * The destination buffer must be at least 0.1% larger than source_size plus 12 bytes. \n 288 * It compresses raw byte data. zip_quality can be 1 to 9. \n 289 * Returns the size of the compressed buffer or zero if failed. 290 * \ingroup compress */ 291 int imCompressDataZ(const(void) * src_data, int src_size, void* dst_data, int dst_size, int zip_quality); 292 293 /** Uncompresses the data compressed with the ZLIB Deflate compression. \n 294 * Returns zero if failed. 295 * \ingroup compress */ 296 int imCompressDataUnZ(const(void) * src_data, int src_size, void* dst_data, int dst_size); 297 298 /** Compresses the data using the libLZF compression. \n 299 * Returns the size of the compressed buffer or zero if failed. 300 * \ingroup compress */ 301 int imCompressDataLZF(const(void) * src_data, int src_size, void* dst_data, int dst_size); 302 303 /** Uncompresses the data compressed with the libLZF compression. \n 304 * Returns zero if failed. 305 * \ingroup compress */ 306 int imCompressDataUnLZF(const(void) * src_data, int src_size, void* dst_data, int dst_size); 307 308 /** Compresses the data using the libLZO compression. (Since 3.9) \n 309 * Returns the size of the compressed buffer or zero if failed. \n 310 * Available in a separate library called "im_lzo" which license is GPL. 311 * \ingroup compress */ 312 int imCompressDataLZO(const(void) * src_data, int src_size, void* dst_data, int dst_size); 313 314 /** Uncompresses the data compressed with the libLZO compression. (Since 3.9) \n 315 * Returns zero if failed. \n 316 * Available in a separate library called "im_lzo" which license is GPL. 317 * \ingroup compress */ 318 int imCompressDataUnLZO(const(void) * src_data, int src_size, void* dst_data, int dst_size);