DocumentationOverviewBuilding ASL Documentation Library Wiki Docs Indices Browse Perforce More InfoRelease NotesWiki Site Search License Success Stories Contributors MediaDownloadPerforce Depots SupportASL SourceForge HomeMailing Lists Discussion Forums Report Bugs Suggest Features Contribute to ASL RSSShort-text newsFull-text news File releases Other Adobe ProjectsAdobe AirAdobe GIL Adobe Labs Adobe Media Gallery Adobe XMP Tamarin project (Mozilla Foundation) Other ResourcesBoostRIAForge SGI STL |
dng_utils.h00001 /*****************************************************************************/ 00002 // Copyright 2006-2007 Adobe Systems Incorporated 00003 // All Rights Reserved. 00004 // 00005 // NOTICE: Adobe permits you to use, modify, and distribute this file in 00006 // accordance with the terms of the Adobe license agreement accompanying it. 00007 /*****************************************************************************/ 00008 00009 /* $Id: //mondo/dng_sdk_1_2/dng_sdk/source/dng_utils.h#1 $ */ 00010 /* $DateTime: 2008/03/09 14:29:54 $ */ 00011 /* $Change: 431850 $ */ 00012 /* $Author: tknoll $ */ 00013 00014 /*****************************************************************************/ 00015 00016 #ifndef __dng_utils__ 00017 #define __dng_utils__ 00018 00019 /*****************************************************************************/ 00020 00021 #include "dng_flags.h" 00022 #include "dng_types.h" 00023 00024 /*****************************************************************************/ 00025 00026 inline uint32 Abs_int32 (int32 x) 00027 { 00028 00029 #if 0 00030 00031 // Reference version. 00032 00033 return (uint32) (x < 0 ? -x : x); 00034 00035 #else 00036 00037 // Branchless version. 00038 00039 uint32 mask = x >> 31; 00040 00041 return (uint32) ((x + mask) ^ mask); 00042 00043 #endif 00044 00045 } 00046 00047 inline int32 Min_int32 (int32 x, int32 y) 00048 { 00049 00050 return (x <= y ? x : y); 00051 00052 } 00053 00054 inline int32 Max_int32 (int32 x, int32 y) 00055 { 00056 00057 return (x >= y ? x : y); 00058 00059 } 00060 00061 inline int32 Pin_int32 (int32 min, int32 x, int32 max) 00062 { 00063 00064 return Max_int32 (min, Min_int32 (x, max)); 00065 00066 } 00067 00068 /*****************************************************************************/ 00069 00070 inline int16 Pin_int16 (int32 x) 00071 { 00072 00073 x = Pin_int32 (-32768, x, 32767); 00074 00075 return (int16) x; 00076 00077 } 00078 00079 /*****************************************************************************/ 00080 00081 inline uint32 Min_uint32 (uint32 x, uint32 y) 00082 { 00083 00084 return (x <= y ? x : y); 00085 00086 } 00087 00088 inline uint32 Min_uint32 (uint32 x, uint32 y, uint32 z) 00089 { 00090 00091 return Min_uint32 (x, Min_uint32 (y, z)); 00092 00093 } 00094 00095 inline uint32 Max_uint32 (uint32 x, uint32 y) 00096 { 00097 00098 return (x >= y ? x : y); 00099 00100 } 00101 00102 inline uint32 Max_uint32 (uint32 x, uint32 y, uint32 z) 00103 { 00104 00105 return Max_uint32 (x, Max_uint32 (y, z)); 00106 00107 } 00108 00109 inline uint32 Pin_uint32 (uint32 min, uint32 x, uint32 max) 00110 { 00111 00112 return Max_uint32 (min, Min_uint32 (x, max)); 00113 00114 } 00115 00116 /*****************************************************************************/ 00117 00118 inline uint16 Pin_uint16 (int32 x) 00119 { 00120 00121 #if 0 00122 00123 // Reference version. 00124 00125 x = Pin_int32 (0, x, 0x0FFFF); 00126 00127 #else 00128 00129 // Single branch version. 00130 00131 if (x & ~65535) 00132 { 00133 00134 x = ~x >> 31; 00135 00136 } 00137 00138 #endif 00139 00140 return (uint16) x; 00141 00142 } 00143 00144 /*****************************************************************************/ 00145 00146 inline uint32 RoundUp2 (uint32 x) 00147 { 00148 00149 return (x + 1) & ~1; 00150 00151 } 00152 00153 inline uint32 RoundUp4 (uint32 x) 00154 { 00155 00156 return (x + 3) & ~3; 00157 00158 } 00159 00160 inline uint32 RoundUp8 (uint32 x) 00161 { 00162 00163 return (x + 7) & ~7; 00164 00165 } 00166 00167 inline uint32 RoundUp16 (uint32 x) 00168 { 00169 00170 return (x + 15) & ~15; 00171 00172 } 00173 00174 inline uint32 RoundUp4096 (uint32 x) 00175 { 00176 00177 return (x + 4095) & ~4095; 00178 00179 } 00180 00181 /******************************************************************************/ 00182 00183 inline uint32 RoundDown2 (uint32 x) 00184 { 00185 00186 return x & ~1; 00187 00188 } 00189 00190 inline uint32 RoundDown4 (uint32 x) 00191 { 00192 00193 return x & ~3; 00194 00195 } 00196 00197 inline uint32 RoundDown8 (uint32 x) 00198 { 00199 00200 return x & ~7; 00201 00202 } 00203 00204 inline uint32 RoundDown16 (uint32 x) 00205 { 00206 00207 return x & ~15; 00208 00209 } 00210 00211 /******************************************************************************/ 00212 00213 inline uint64 Abs_int64 (int64 x) 00214 { 00215 00216 return (uint64) (x < 0 ? -x : x); 00217 00218 } 00219 00220 inline int64 Min_int64 (int64 x, int64 y) 00221 { 00222 00223 return (x <= y ? x : y); 00224 00225 } 00226 00227 inline int64 Max_int64 (int64 x, int64 y) 00228 { 00229 00230 return (x >= y ? x : y); 00231 00232 } 00233 00234 inline int64 Pin_int64 (int64 min, int64 x, int64 max) 00235 { 00236 00237 return Max_int64 (min, Min_int64 (x, max)); 00238 00239 } 00240 00241 /******************************************************************************/ 00242 00243 inline uint64 Min_uint64 (uint64 x, uint64 y) 00244 { 00245 00246 return (x <= y ? x : y); 00247 00248 } 00249 00250 inline uint64 Max_uint64 (uint64 x, uint64 y) 00251 { 00252 00253 return (x >= y ? x : y); 00254 00255 } 00256 00257 inline uint64 Pin_uint64 (uint64 min, uint64 x, uint64 max) 00258 { 00259 00260 return Max_uint64 (min, Min_uint64 (x, max)); 00261 00262 } 00263 00264 /*****************************************************************************/ 00265 00266 inline real32 Abs_real32 (real32 x) 00267 { 00268 00269 return (x < 0.0f ? -x : x); 00270 00271 } 00272 00273 inline real32 Min_real32 (real32 x, real32 y) 00274 { 00275 00276 return (x < y ? x : y); 00277 00278 } 00279 00280 inline real32 Max_real32 (real32 x, real32 y) 00281 { 00282 00283 return (x > y ? x : y); 00284 00285 } 00286 00287 inline real32 Pin_real32 (real32 min, real32 x, real32 max) 00288 { 00289 00290 return Max_real32 (min, Min_real32 (x, max)); 00291 00292 } 00293 00294 /*****************************************************************************/ 00295 00296 inline real64 Abs_real64 (real64 x) 00297 { 00298 00299 return (x < 0.0 ? -x : x); 00300 00301 } 00302 00303 inline real64 Min_real64 (real64 x, real64 y) 00304 { 00305 00306 return (x < y ? x : y); 00307 00308 } 00309 00310 inline real64 Max_real64 (real64 x, real64 y) 00311 { 00312 00313 return (x > y ? x : y); 00314 00315 } 00316 00317 inline real64 Pin_real64 (real64 min, real64 x, real64 max) 00318 { 00319 00320 return Max_real64 (min, Min_real64 (x, max)); 00321 00322 } 00323 00324 /*****************************************************************************/ 00325 00326 inline int32 Round_int32 (real32 x) 00327 { 00328 00329 return (int32) (x > 0.0f ? x + 0.5f : x - 0.5f); 00330 00331 } 00332 00333 inline int32 Round_int32 (real64 x) 00334 { 00335 00336 return (int32) (x > 0.0 ? x + 0.5 : x - 0.5); 00337 00338 } 00339 00340 inline uint32 Floor_uint32 (real32 x) 00341 { 00342 00343 return (uint32) Max_real32 (0.0f, x); 00344 00345 } 00346 00347 inline uint32 Floor_uint32 (real64 x) 00348 { 00349 00350 return (uint32) Max_real64 (0.0, x); 00351 00352 } 00353 00354 inline uint32 Round_uint32 (real32 x) 00355 { 00356 00357 return Floor_uint32 (x + 0.5f); 00358 00359 } 00360 00361 inline uint32 Round_uint32 (real64 x) 00362 { 00363 00364 return Floor_uint32 (x + 0.5); 00365 00366 } 00367 00368 /******************************************************************************/ 00369 00370 inline int64 Round_int64 (real64 x) 00371 { 00372 00373 return (int64) (x >= 0.0 ? x + 0.5 : x - 0.5); 00374 00375 } 00376 00377 /*****************************************************************************/ 00378 00379 inline char ForceUppercase (char c) 00380 { 00381 00382 if (c >= 'a' && c <= 'z') 00383 { 00384 00385 c -= 'a' - 'A'; 00386 00387 } 00388 00389 return c; 00390 00391 } 00392 00393 /*****************************************************************************/ 00394 00395 inline uint16 SwapBytes16 (uint16 x) 00396 { 00397 00398 return (x << 8) | 00399 (x >> 8); 00400 00401 } 00402 00403 inline uint32 SwapBytes32 (uint32 x) 00404 { 00405 00406 return (x << 24) + 00407 ((x << 8) & 0x00FF0000) + 00408 ((x >> 8) & 0x0000FF00) + 00409 (x >> 24); 00410 00411 } 00412 00413 /******************************************************************************/ 00414 00415 // Converts from RGB values (range 0.0 to 1.0) to HSV values (range 0.0 to 00416 // 6.0 for hue, and 0.0 to 1.0 for saturation and value). 00417 00418 inline void DNG_RGBtoHSV (real32 r, 00419 real32 g, 00420 real32 b, 00421 real32 &h, 00422 real32 &s, 00423 real32 &v) 00424 { 00425 00426 v = Max_real32 (r, Max_real32 (g, b)); 00427 00428 real32 gap = v - Min_real32 (r, Min_real32 (g, b)); 00429 00430 if (gap > 0.0f) 00431 { 00432 00433 if (r == v) 00434 { 00435 00436 h = (g - b) / gap; 00437 00438 if (h < 0.0f) 00439 { 00440 h += 6.0f; 00441 } 00442 00443 } 00444 00445 else if (g == v) 00446 { 00447 h = 2.0f + (b - r) / gap; 00448 } 00449 00450 else 00451 { 00452 h = 4.0f + (r - g) / gap; 00453 } 00454 00455 s = gap / v; 00456 00457 } 00458 00459 else 00460 { 00461 h = 0.0f; 00462 s = 0.0f; 00463 } 00464 00465 } 00466 00467 /*****************************************************************************/ 00468 00469 // Converts from HSV values (range 0.0 to 6.0 for hue, and 0.0 to 1.0 for 00470 // saturation and value) to RGB values (range 0.0 to 1.0). 00471 00472 inline void DNG_HSVtoRGB (real32 h, 00473 real32 s, 00474 real32 v, 00475 real32 &r, 00476 real32 &g, 00477 real32 &b) 00478 { 00479 00480 if (s > 0.0f) 00481 { 00482 00483 if (h < 0.0f) 00484 h += 6.0f; 00485 00486 if (h >= 6.0f) 00487 h -= 6.0f; 00488 00489 int32 i = (int32) h; 00490 real32 f = h - (real32) i; 00491 00492 real32 p = v * (1.0f - s); 00493 00494 #define q (v * (1.0f - s * f)) 00495 #define t (v * (1.0f - s * (1.0f - f))) 00496 00497 switch (i) 00498 { 00499 case 0: r = v; g = t; b = p; break; 00500 case 1: r = q; g = v; b = p; break; 00501 case 2: r = p; g = v; b = t; break; 00502 case 3: r = p; g = q; b = v; break; 00503 case 4: r = t; g = p; b = v; break; 00504 case 5: r = v; g = p; b = q; break; 00505 } 00506 00507 #undef q 00508 #undef t 00509 00510 } 00511 00512 else 00513 { 00514 r = v; 00515 g = v; 00516 b = v; 00517 } 00518 00519 } 00520 00521 /******************************************************************************/ 00522 00523 real64 TickTimeInSeconds (); 00524 00525 /******************************************************************************/ 00526 00527 class dng_timer 00528 { 00529 00530 public: 00531 00532 dng_timer (const char *message); 00533 00534 ~dng_timer (); 00535 00536 private: 00537 00538 // Hidden copy constructor and assignment operator. 00539 00540 dng_timer (const dng_timer &timer); 00541 00542 dng_timer & operator= (const dng_timer &timer); 00543 00544 private: 00545 00546 const char *fMessage; 00547 00548 real64 fStartTime; 00549 00550 }; 00551 00552 /*****************************************************************************/ 00553 00554 #endif 00555 00556 /*****************************************************************************/ | |||
