PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/llquantize.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 152 lines | 82 code | 32 blank | 38 comment | 2 complexity | 3b2d0b443cc42d936cc46d432859fb0a MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llquantize.h
  3. * @brief useful routines for quantizing floats to various length ints
  4. * and back out again
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #ifndef LL_LLQUANTIZE_H
  28. #define LL_LLQUANTIZE_H
  29. const U16 U16MAX = 65535;
  30. LL_ALIGN_16( const F32 F_U16MAX_4A[4] ) = { 65535.f, 65535.f, 65535.f, 65535.f };
  31. const F32 OOU16MAX = 1.f/(F32)(U16MAX);
  32. LL_ALIGN_16( const F32 F_OOU16MAX_4A[4] ) = { OOU16MAX, OOU16MAX, OOU16MAX, OOU16MAX };
  33. const U8 U8MAX = 255;
  34. LL_ALIGN_16( const F32 F_U8MAX_4A[4] ) = { 255.f, 255.f, 255.f, 255.f };
  35. const F32 OOU8MAX = 1.f/(F32)(U8MAX);
  36. LL_ALIGN_16( const F32 F_OOU8MAX_4A[4] ) = { OOU8MAX, OOU8MAX, OOU8MAX, OOU8MAX };
  37. const U8 FIRSTVALIDCHAR = 54;
  38. const U8 MAXSTRINGVAL = U8MAX - FIRSTVALIDCHAR; //we don't allow newline or null
  39. inline U16 F32_to_U16_ROUND(F32 val, F32 lower, F32 upper)
  40. {
  41. val = llclamp(val, lower, upper);
  42. // make sure that the value is positive and normalized to <0, 1>
  43. val -= lower;
  44. val /= (upper - lower);
  45. // round the value. Sreturn the U16
  46. return (U16)(llround(val*U16MAX));
  47. }
  48. inline U16 F32_to_U16(F32 val, F32 lower, F32 upper)
  49. {
  50. val = llclamp(val, lower, upper);
  51. // make sure that the value is positive and normalized to <0, 1>
  52. val -= lower;
  53. val /= (upper - lower);
  54. // return the U16
  55. return (U16)(llfloor(val*U16MAX));
  56. }
  57. inline F32 U16_to_F32(U16 ival, F32 lower, F32 upper)
  58. {
  59. F32 val = ival*OOU16MAX;
  60. F32 delta = (upper - lower);
  61. val *= delta;
  62. val += lower;
  63. F32 max_error = delta*OOU16MAX;
  64. // make sure that zero's come through as zero
  65. if (fabsf(val) < max_error)
  66. val = 0.f;
  67. return val;
  68. }
  69. inline U8 F32_to_U8_ROUND(F32 val, F32 lower, F32 upper)
  70. {
  71. val = llclamp(val, lower, upper);
  72. // make sure that the value is positive and normalized to <0, 1>
  73. val -= lower;
  74. val /= (upper - lower);
  75. // return the rounded U8
  76. return (U8)(llround(val*U8MAX));
  77. }
  78. inline U8 F32_to_U8(F32 val, F32 lower, F32 upper)
  79. {
  80. val = llclamp(val, lower, upper);
  81. // make sure that the value is positive and normalized to <0, 1>
  82. val -= lower;
  83. val /= (upper - lower);
  84. // return the U8
  85. return (U8)(llfloor(val*U8MAX));
  86. }
  87. inline F32 U8_to_F32(U8 ival, F32 lower, F32 upper)
  88. {
  89. F32 val = ival*OOU8MAX;
  90. F32 delta = (upper - lower);
  91. val *= delta;
  92. val += lower;
  93. F32 max_error = delta*OOU8MAX;
  94. // make sure that zero's come through as zero
  95. if (fabsf(val) < max_error)
  96. val = 0.f;
  97. return val;
  98. }
  99. inline U8 F32_TO_STRING(F32 val, F32 lower, F32 upper)
  100. {
  101. val = llclamp(val, lower, upper); //[lower, upper]
  102. // make sure that the value is positive and normalized to <0, 1>
  103. val -= lower; //[0, upper-lower]
  104. val /= (upper - lower); //[0,1]
  105. val = val * MAXSTRINGVAL; //[0, MAXSTRINGVAL]
  106. val = floor(val + 0.5f); //[0, MAXSTRINGVAL]
  107. U8 stringVal = (U8)(val) + FIRSTVALIDCHAR; //[FIRSTVALIDCHAR, MAXSTRINGVAL + FIRSTVALIDCHAR]
  108. return stringVal;
  109. }
  110. inline F32 STRING_TO_F32(U8 ival, F32 lower, F32 upper)
  111. {
  112. // remove empty space left for NULL, newline, etc.
  113. ival -= FIRSTVALIDCHAR; //[0, MAXSTRINGVAL]
  114. F32 val = (F32)ival * (1.f / (F32)MAXSTRINGVAL); //[0, 1]
  115. F32 delta = (upper - lower);
  116. val *= delta; //[0, upper - lower]
  117. val += lower; //[lower, upper]
  118. return val;
  119. }
  120. #endif