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-2008 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/workarea/stern/camera_raw/dng_sdk/source/dng_utils.h#16 $ */ 00010 /* $DateTime: 2009/03/18 15:20:27 $ */ 00011 /* $Change: 548456 $ */ 00012 /* $Author: stern $ */ 00013 00014 /*****************************************************************************/ 00015 00016 #ifndef __dng_utils__ 00017 #define __dng_utils__ 00018 00019 /*****************************************************************************/ 00020 00021 #include "dng_classes.h" 00022 #include "dng_flags.h" 00023 #include "dng_types.h" 00024 00025 /*****************************************************************************/ 00026 00027 inline uint32 Abs_int32 (int32 x) 00028 { 00029 00030 #if 0 00031 00032 // Reference version. 00033 00034 return (uint32) (x < 0 ? -x : x); 00035 00036 #else 00037 00038 // Branchless version. 00039 00040 uint32 mask = x >> 31; 00041 00042 return (uint32) ((x + mask) ^ mask); 00043 00044 #endif 00045 00046 } 00047 00048 inline int32 Min_int32 (int32 x, int32 y) 00049 { 00050 00051 return (x <= y ? x : y); 00052 00053 } 00054 00055 inline int32 Max_int32 (int32 x, int32 y) 00056 { 00057 00058 return (x >= y ? x : y); 00059 00060 } 00061 00062 inline int32 Pin_int32 (int32 min, int32 x, int32 max) 00063 { 00064 00065 return Max_int32 (min, Min_int32 (x, max)); 00066 00067 } 00068 00069 inline int32 Pin_int32_between (int32 a, int32 x, int32 b) 00070 { 00071 00072 int32 min, max; 00073 if (a < b) { min = a; max = b; } 00074 else { min = b; max = a; } 00075 00076 return Pin_int32 (min, x, max); 00077 00078 } 00079 00080 /*****************************************************************************/ 00081 00082 inline uint16 Min_uint16 (uint16 x, uint16 y) 00083 { 00084 00085 return (x <= y ? x : y); 00086 00087 } 00088 00089 inline uint16 Max_uint16 (uint16 x, uint16 y) 00090 { 00091 00092 return (x >= y ? x : y); 00093 00094 } 00095 00096 inline int16 Pin_int16 (int32 x) 00097 { 00098 00099 x = Pin_int32 (-32768, x, 32767); 00100 00101 return (int16) x; 00102 00103 } 00104 00105 /*****************************************************************************/ 00106 00107 inline uint32 Min_uint32 (uint32 x, uint32 y) 00108 { 00109 00110 return (x <= y ? x : y); 00111 00112 } 00113 00114 inline uint32 Min_uint32 (uint32 x, uint32 y, uint32 z) 00115 { 00116 00117 return Min_uint32 (x, Min_uint32 (y, z)); 00118 00119 } 00120 00121 inline uint32 Max_uint32 (uint32 x, uint32 y) 00122 { 00123 00124 return (x >= y ? x : y); 00125 00126 } 00127 00128 inline uint32 Max_uint32 (uint32 x, uint32 y, uint32 z) 00129 { 00130 00131 return Max_uint32 (x, Max_uint32 (y, z)); 00132 00133 } 00134 00135 inline uint32 Pin_uint32 (uint32 min, uint32 x, uint32 max) 00136 { 00137 00138 return Max_uint32 (min, Min_uint32 (x, max)); 00139 00140 } 00141 00142 /*****************************************************************************/ 00143 00144 inline uint16 Pin_uint16 (int32 x) 00145 { 00146 00147 #if 0 00148 00149 // Reference version. 00150 00151 x = Pin_int32 (0, x, 0x0FFFF); 00152 00153 #else 00154 00155 // Single branch version. 00156 00157 if (x & ~65535) 00158 { 00159 00160 x = ~x >> 31; 00161 00162 } 00163 00164 #endif 00165 00166 return (uint16) x; 00167 00168 } 00169 00170 /*****************************************************************************/ 00171 00172 inline uint32 RoundUp2 (uint32 x) 00173 { 00174 00175 return (x + 1) & ~1; 00176 00177 } 00178 00179 inline uint32 RoundUp4 (uint32 x) 00180 { 00181 00182 return (x + 3) & ~3; 00183 00184 } 00185 00186 inline uint32 RoundUp8 (uint32 x) 00187 { 00188 00189 return (x + 7) & ~7; 00190 00191 } 00192 00193 inline uint32 RoundUp16 (uint32 x) 00194 { 00195 00196 return (x + 15) & ~15; 00197 00198 } 00199 00200 inline uint32 RoundUp4096 (uint32 x) 00201 { 00202 00203 return (x + 4095) & ~4095; 00204 00205 } 00206 00207 /******************************************************************************/ 00208 00209 inline uint32 RoundDown2 (uint32 x) 00210 { 00211 00212 return x & ~1; 00213 00214 } 00215 00216 inline uint32 RoundDown4 (uint32 x) 00217 { 00218 00219 return x & ~3; 00220 00221 } 00222 00223 inline uint32 RoundDown8 (uint32 x) 00224 { 00225 00226 return x & ~7; 00227 00228 } 00229 00230 inline uint32 RoundDown16 (uint32 x) 00231 { 00232 00233 return x & ~15; 00234 00235 } 00236 00237 /******************************************************************************/ 00238 00239 inline uint32 RoundUpForPixelSize (uint32 x, uint32 pixelSize) 00240 { 00241 00242 switch (pixelSize) 00243 { 00244 00245 case 1: 00246 return RoundUp16 (x); 00247 00248 case 2: 00249 return RoundUp8 (x); 00250 00251 case 4: 00252 return RoundUp4 (x); 00253 00254 case 8: 00255 return RoundUp2 (x); 00256 00257 default: 00258 return RoundUp16 (x); 00259 00260 } 00261 00262 } 00263 00264 /******************************************************************************/ 00265 00266 inline uint64 Abs_int64 (int64 x) 00267 { 00268 00269 return (uint64) (x < 0 ? -x : x); 00270 00271 } 00272 00273 inline int64 Min_int64 (int64 x, int64 y) 00274 { 00275 00276 return (x <= y ? x : y); 00277 00278 } 00279 00280 inline int64 Max_int64 (int64 x, int64 y) 00281 { 00282 00283 return (x >= y ? x : y); 00284 00285 } 00286 00287 inline int64 Pin_int64 (int64 min, int64 x, int64 max) 00288 { 00289 00290 return Max_int64 (min, Min_int64 (x, max)); 00291 00292 } 00293 00294 /******************************************************************************/ 00295 00296 inline uint64 Min_uint64 (uint64 x, uint64 y) 00297 { 00298 00299 return (x <= y ? x : y); 00300 00301 } 00302 00303 inline uint64 Max_uint64 (uint64 x, uint64 y) 00304 { 00305 00306 return (x >= y ? x : y); 00307 00308 } 00309 00310 inline uint64 Pin_uint64 (uint64 min, uint64 x, uint64 max) 00311 { 00312 00313 return Max_uint64 (min, Min_uint64 (x, max)); 00314 00315 } 00316 00317 /*****************************************************************************/ 00318 00319 inline real32 Abs_real32 (real32 x) 00320 { 00321 00322 return (x < 0.0f ? -x : x); 00323 00324 } 00325 00326 inline real32 Min_real32 (real32 x, real32 y) 00327 { 00328 00329 return (x < y ? x : y); 00330 00331 } 00332 00333 inline real32 Max_real32 (real32 x, real32 y) 00334 { 00335 00336 return (x > y ? x : y); 00337 00338 } 00339 00340 inline real32 Pin_real32 (real32 min, real32 x, real32 max) 00341 { 00342 00343 return Max_real32 (min, Min_real32 (x, max)); 00344 00345 } 00346 00347 inline real32 Pin_real32 (real32 x) 00348 { 00349 00350 return Pin_real32 (0.0f, x, 1.0f); 00351 00352 } 00353 00354 inline real32 Lerp_real32 (real32 a, real32 b, real32 t) 00355 { 00356 00357 return a + t * (b - a); 00358 00359 } 00360 00361 /*****************************************************************************/ 00362 00363 inline real64 Abs_real64 (real64 x) 00364 { 00365 00366 return (x < 0.0 ? -x : x); 00367 00368 } 00369 00370 inline real64 Min_real64 (real64 x, real64 y) 00371 { 00372 00373 return (x < y ? x : y); 00374 00375 } 00376 00377 inline real64 Max_real64 (real64 x, real64 y) 00378 { 00379 00380 return (x > y ? x : y); 00381 00382 } 00383 00384 inline real64 Pin_real64 (real64 min, real64 x, real64 max) 00385 { 00386 00387 return Max_real64 (min, Min_real64 (x, max)); 00388 00389 } 00390 00391 inline real64 Pin_real64 (real64 x) 00392 { 00393 00394 return Pin_real64 (0.0, x, 1.0); 00395 00396 } 00397 00398 inline real64 Lerp_real64 (real64 a, real64 b, real64 t) 00399 { 00400 00401 return a + t * (b - a); 00402 00403 } 00404 00405 /*****************************************************************************/ 00406 00407 inline int32 Round_int32 (real32 x) 00408 { 00409 00410 return (int32) (x > 0.0f ? x + 0.5f : x - 0.5f); 00411 00412 } 00413 00414 inline int32 Round_int32 (real64 x) 00415 { 00416 00417 return (int32) (x > 0.0 ? x + 0.5 : x - 0.5); 00418 00419 } 00420 00421 inline uint32 Floor_uint32 (real32 x) 00422 { 00423 00424 return (uint32) Max_real32 (0.0f, x); 00425 00426 } 00427 00428 inline uint32 Floor_uint32 (real64 x) 00429 { 00430 00431 return (uint32) Max_real64 (0.0, x); 00432 00433 } 00434 00435 inline uint32 Round_uint32 (real32 x) 00436 { 00437 00438 return Floor_uint32 (x + 0.5f); 00439 00440 } 00441 00442 inline uint32 Round_uint32 (real64 x) 00443 { 00444 00445 return Floor_uint32 (x + 0.5); 00446 00447 } 00448 00449 /******************************************************************************/ 00450 00451 inline int64 Round_int64 (real64 x) 00452 { 00453 00454 return (int64) (x >= 0.0 ? x + 0.5 : x - 0.5); 00455 00456 } 00457 00458 /*****************************************************************************/ 00459 00460 const int64 kFixed64_One = (((int64) 1) << 32); 00461 const int64 kFixed64_Half = (((int64) 1) << 31); 00462 00463 /******************************************************************************/ 00464 00465 inline int64 Real64ToFixed64 (real64 x) 00466 { 00467 00468 return Round_int64 (x * (real64) kFixed64_One); 00469 00470 } 00471 00472 /******************************************************************************/ 00473 00474 inline real64 Fixed64ToReal64 (int64 x) 00475 { 00476 00477 return x * (1.0 / (real64) kFixed64_One); 00478 00479 } 00480 00481 /*****************************************************************************/ 00482 00483 inline char ForceUppercase (char c) 00484 { 00485 00486 if (c >= 'a' && c <= 'z') 00487 { 00488 00489 c -= 'a' - 'A'; 00490 00491 } 00492 00493 return c; 00494 00495 } 00496 00497 /*****************************************************************************/ 00498 00499 inline uint16 SwapBytes16 (uint16 x) 00500 { 00501 00502 return (x << 8) | 00503 (x >> 8); 00504 00505 } 00506 00507 inline uint32 SwapBytes32 (uint32 x) 00508 { 00509 00510 return (x << 24) + 00511 ((x << 8) & 0x00FF0000) + 00512 ((x >> 8) & 0x0000FF00) + 00513 (x >> 24); 00514 00515 } 00516 00517 /*****************************************************************************/ 00518 00519 inline bool IsAligned16 (const void *p) 00520 { 00521 00522 return (((uintptr) p) & 1) == 0; 00523 00524 } 00525 00526 inline bool IsAligned32 (const void *p) 00527 { 00528 00529 return (((uintptr) p) & 3) == 0; 00530 00531 } 00532 00533 inline bool IsAligned64 (const void *p) 00534 { 00535 00536 return (((uintptr) p) & 7) == 0; 00537 00538 } 00539 00540 inline bool IsAligned128 (const void *p) 00541 { 00542 00543 return (((uintptr) p) & 15) == 0; 00544 00545 } 00546 00547 /******************************************************************************/ 00548 00549 // Converts from RGB values (range 0.0 to 1.0) to HSV values (range 0.0 to 00550 // 6.0 for hue, and 0.0 to 1.0 for saturation and value). 00551 00552 inline void DNG_RGBtoHSV (real32 r, 00553 real32 g, 00554 real32 b, 00555 real32 &h, 00556 real32 &s, 00557 real32 &v) 00558 { 00559 00560 v = Max_real32 (r, Max_real32 (g, b)); 00561 00562 real32 gap = v - Min_real32 (r, Min_real32 (g, b)); 00563 00564 if (gap > 0.0f) 00565 { 00566 00567 if (r == v) 00568 { 00569 00570 h = (g - b) / gap; 00571 00572 if (h < 0.0f) 00573 { 00574 h += 6.0f; 00575 } 00576 00577 } 00578 00579 else if (g == v) 00580 { 00581 h = 2.0f + (b - r) / gap; 00582 } 00583 00584 else 00585 { 00586 h = 4.0f + (r - g) / gap; 00587 } 00588 00589 s = gap / v; 00590 00591 } 00592 00593 else 00594 { 00595 h = 0.0f; 00596 s = 0.0f; 00597 } 00598 00599 } 00600 00601 /*****************************************************************************/ 00602 00603 // Converts from HSV values (range 0.0 to 6.0 for hue, and 0.0 to 1.0 for 00604 // saturation and value) to RGB values (range 0.0 to 1.0). 00605 00606 inline void DNG_HSVtoRGB (real32 h, 00607 real32 s, 00608 real32 v, 00609 real32 &r, 00610 real32 &g, 00611 real32 &b) 00612 { 00613 00614 if (s > 0.0f) 00615 { 00616 00617 if (h < 0.0f) 00618 h += 6.0f; 00619 00620 if (h >= 6.0f) 00621 h -= 6.0f; 00622 00623 int32 i = (int32) h; 00624 real32 f = h - (real32) i; 00625 00626 real32 p = v * (1.0f - s); 00627 00628 #define q (v * (1.0f - s * f)) 00629 #define t (v * (1.0f - s * (1.0f - f))) 00630 00631 switch (i) 00632 { 00633 case 0: r = v; g = t; b = p; break; 00634 case 1: r = q; g = v; b = p; break; 00635 case 2: r = p; g = v; b = t; break; 00636 case 3: r = p; g = q; b = v; break; 00637 case 4: r = t; g = p; b = v; break; 00638 case 5: r = v; g = p; b = q; break; 00639 } 00640 00641 #undef q 00642 #undef t 00643 00644 } 00645 00646 else 00647 { 00648 r = v; 00649 g = v; 00650 b = v; 00651 } 00652 00653 } 00654 00655 /******************************************************************************/ 00656 00657 // High resolution timer, for code profiling. 00658 00659 real64 TickTimeInSeconds (); 00660 00661 // Lower resolution timer, but more stable. 00662 00663 real64 TickCountInSeconds (); 00664 00665 /******************************************************************************/ 00666 00667 class dng_timer 00668 { 00669 00670 public: 00671 00672 dng_timer (const char *message); 00673 00674 ~dng_timer (); 00675 00676 private: 00677 00678 // Hidden copy constructor and assignment operator. 00679 00680 dng_timer (const dng_timer &timer); 00681 00682 dng_timer & operator= (const dng_timer &timer); 00683 00684 private: 00685 00686 const char *fMessage; 00687 00688 real64 fStartTime; 00689 00690 }; 00691 00692 /*****************************************************************************/ 00693 00694 // Returns the maximum squared Euclidean distance from the specified point to the 00695 // specified rectangle rect. 00696 00697 real64 MaxSquaredDistancePointToRect (const dng_point_real64 &point, 00698 const dng_rect_real64 &rect); 00699 00700 /*****************************************************************************/ 00701 00702 // Returns the maximum Euclidean distance from the specified point to the specified 00703 // rectangle rect. 00704 00705 real64 MaxDistancePointToRect (const dng_point_real64 &point, 00706 const dng_rect_real64 &rect); 00707 00708 /*****************************************************************************/ 00709 00710 #endif 00711 00712 /*****************************************************************************/ |