/indra/llkdu/llkdumem.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 196 lines · 153 code · 17 blank · 26 comment · 36 complexity · 13b24353ca6ece3e718a0479a3896797 MD5 · raw file

  1. /**
  2. * @file llkdumem.cpp
  3. * @brief Helper class for kdu memory management
  4. *
  5. * $LicenseInfo:firstyear=2010&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llkdumem.h"
  28. #include "llerror.h"
  29. #if defined(LL_WINDOWS)
  30. # pragma warning(disable: 4702) // unreachable code
  31. #endif
  32. LLKDUMemIn::LLKDUMemIn(const U8 *data,
  33. const U32 size,
  34. const U16 width,
  35. const U16 height,
  36. const U8 in_num_components,
  37. siz_params *siz)
  38. {
  39. U8 n;
  40. first_comp_idx = 0;
  41. rows = height;
  42. cols = width;
  43. num_components = in_num_components;
  44. alignment_bytes = 0;
  45. for (n = 0; n < 3; ++n)
  46. {
  47. precision[n] = 0;
  48. }
  49. for (n = 0; n < num_components; ++n)
  50. {
  51. siz->set(Sdims,n,0,rows);
  52. siz->set(Sdims,n,1,cols);
  53. siz->set(Ssigned,n,0,false);
  54. siz->set(Sprecision,n,0,8);
  55. }
  56. incomplete_lines = NULL;
  57. free_lines = NULL;
  58. num_unread_rows = rows;
  59. mData = data;
  60. mDataSize = size;
  61. mCurPos = 0;
  62. }
  63. LLKDUMemIn::~LLKDUMemIn()
  64. {
  65. if ((num_unread_rows > 0) || (incomplete_lines != NULL))
  66. {
  67. kdu_warning w;
  68. w << "Not all rows of image components "
  69. << first_comp_idx << " through "
  70. << first_comp_idx+num_components-1
  71. << " were consumed!";
  72. }
  73. image_line_buf *tmp;
  74. while ((tmp=incomplete_lines) != NULL)
  75. {
  76. incomplete_lines = tmp->next;
  77. delete tmp;
  78. }
  79. while ((tmp=free_lines) != NULL)
  80. {
  81. free_lines = tmp->next;
  82. delete tmp;
  83. }
  84. }
  85. bool LLKDUMemIn::get(int comp_idx, kdu_line_buf &line, int x_tnum)
  86. {
  87. int idx = comp_idx - this->first_comp_idx;
  88. assert((idx >= 0) && (idx < num_components));
  89. x_tnum = x_tnum*num_components+idx;
  90. image_line_buf *scan, *prev=NULL;
  91. for (scan = incomplete_lines; scan != NULL; prev = scan, scan = scan->next)
  92. {
  93. assert(scan->next_x_tnum >= x_tnum);
  94. if (scan->next_x_tnum == x_tnum)
  95. {
  96. break;
  97. }
  98. }
  99. if (scan == NULL)
  100. { // Need to read a new image line.
  101. assert(x_tnum == 0); // Must consume in very specific order.
  102. if (num_unread_rows == 0)
  103. {
  104. return false;
  105. }
  106. if ((scan = free_lines) == NULL)
  107. {
  108. scan = new image_line_buf(cols+3,num_components);
  109. }
  110. free_lines = scan->next;
  111. if (prev == NULL)
  112. {
  113. incomplete_lines = scan;
  114. }
  115. else
  116. {
  117. prev->next = scan;
  118. }
  119. // Copy from image buffer into scan.
  120. memcpy(scan->buf, mData+mCurPos, cols*num_components);
  121. mCurPos += cols*num_components;
  122. num_unread_rows--;
  123. scan->accessed_samples = 0;
  124. scan->next_x_tnum = 0;
  125. }
  126. assert((cols-scan->accessed_samples) >= line.get_width());
  127. int comp_offset = idx;
  128. kdu_byte *sp = scan->buf+num_components*scan->accessed_samples + comp_offset;
  129. int n=line.get_width();
  130. if (line.get_buf32() != NULL)
  131. {
  132. kdu_sample32 *dp = line.get_buf32();
  133. if (line.is_absolute())
  134. { // 32-bit absolute integers
  135. for (; n > 0; n--, sp+=num_components, dp++)
  136. {
  137. dp->ival = ((kdu_int32)(*sp)) - 128;
  138. }
  139. }
  140. else
  141. { // true 32-bit floats
  142. for (; n > 0; n--, sp+=num_components, dp++)
  143. {
  144. dp->fval = (((float)(*sp)) / 256.0F) - 0.5F;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. kdu_sample16 *dp = line.get_buf16();
  151. if (line.is_absolute())
  152. { // 16-bit absolute integers
  153. for (; n > 0; n--, sp+=num_components, dp++)
  154. {
  155. dp->ival = ((kdu_int16)(*sp)) - 128;
  156. }
  157. }
  158. else
  159. { // 16-bit normalized representation.
  160. for (; n > 0; n--, sp+=num_components, dp++)
  161. {
  162. dp->ival = (((kdu_int16)(*sp)) - 128) << (KDU_FIX_POINT-8);
  163. }
  164. }
  165. }
  166. scan->next_x_tnum++;
  167. if (idx == (num_components-1))
  168. {
  169. scan->accessed_samples += line.get_width();
  170. }
  171. if (scan->accessed_samples == cols)
  172. { // Send empty line to free list.
  173. assert(scan == incomplete_lines);
  174. incomplete_lines = scan->next;
  175. scan->next = free_lines;
  176. free_lines = scan;
  177. }
  178. return true;
  179. }