PageRenderTime 53ms CodeModel.GetById 20ms app.highlight 19ms RepoModel.GetById 11ms app.codeStats 0ms

/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
 27#include "linden_common.h"
 28#include "llkdumem.h"
 29#include "llerror.h"
 30
 31#if defined(LL_WINDOWS)
 32# pragma warning(disable: 4702) // unreachable code
 33#endif
 34
 35LLKDUMemIn::LLKDUMemIn(const U8 *data,
 36					   const U32 size,
 37					   const U16 width,
 38					   const U16 height,
 39					   const U8 in_num_components,
 40					   siz_params *siz)
 41{
 42	U8 n;
 43
 44	first_comp_idx = 0;
 45	rows = height;
 46	cols = width;
 47	num_components = in_num_components;
 48	alignment_bytes = 0;
 49
 50	for (n = 0; n < 3; ++n)
 51	{
 52		precision[n] = 0;
 53	}
 54
 55	for (n = 0; n < num_components; ++n)
 56	{
 57		siz->set(Sdims,n,0,rows);
 58		siz->set(Sdims,n,1,cols);
 59		siz->set(Ssigned,n,0,false);
 60		siz->set(Sprecision,n,0,8);
 61	}
 62	incomplete_lines = NULL;
 63	free_lines = NULL;
 64	num_unread_rows = rows;
 65
 66	mData = data;
 67	mDataSize = size;
 68	mCurPos = 0;
 69}
 70
 71LLKDUMemIn::~LLKDUMemIn()
 72{
 73	if ((num_unread_rows > 0) || (incomplete_lines != NULL))
 74	{
 75		kdu_warning w;
 76		w << "Not all rows of image components "
 77			<< first_comp_idx << " through "
 78			<< first_comp_idx+num_components-1
 79			<< " were consumed!";
 80	}
 81	image_line_buf *tmp;
 82	while ((tmp=incomplete_lines) != NULL)
 83	{
 84		incomplete_lines = tmp->next;
 85		delete tmp; 
 86	}
 87	while ((tmp=free_lines) != NULL)
 88	{
 89		free_lines = tmp->next;
 90		delete tmp;
 91	}
 92}
 93
 94
 95bool LLKDUMemIn::get(int comp_idx, kdu_line_buf &line, int x_tnum)
 96{
 97	int idx = comp_idx - this->first_comp_idx;
 98	assert((idx >= 0) && (idx < num_components));
 99	x_tnum = x_tnum*num_components+idx;
100	image_line_buf *scan, *prev=NULL;
101	for (scan = incomplete_lines; scan != NULL; prev = scan, scan = scan->next)
102	{
103		assert(scan->next_x_tnum >= x_tnum);
104		if (scan->next_x_tnum == x_tnum)
105		{
106			break;
107		}
108	}
109	if (scan == NULL)
110	{ // Need to read a new image line.
111		assert(x_tnum == 0); // Must consume in very specific order.
112		if (num_unread_rows == 0)
113		{
114	        return false;
115		}
116		if ((scan = free_lines) == NULL)
117		{
118			scan = new image_line_buf(cols+3,num_components);
119		}
120		free_lines = scan->next;
121		if (prev == NULL)
122		{
123	        incomplete_lines = scan;
124		}
125		else
126		{
127			prev->next = scan;
128		}
129
130		// Copy from image buffer into scan.
131		memcpy(scan->buf, mData+mCurPos, cols*num_components);
132		mCurPos += cols*num_components;
133
134		num_unread_rows--;
135		scan->accessed_samples = 0;
136		scan->next_x_tnum = 0;
137	}
138
139	assert((cols-scan->accessed_samples) >= line.get_width());
140
141	int comp_offset = idx;
142	kdu_byte *sp = scan->buf+num_components*scan->accessed_samples + comp_offset;
143	int n=line.get_width();
144
145	if (line.get_buf32() != NULL)
146	{
147		kdu_sample32 *dp = line.get_buf32();
148		if (line.is_absolute())
149		{ // 32-bit absolute integers
150			for (; n > 0; n--, sp+=num_components, dp++)
151			{
152				dp->ival = ((kdu_int32)(*sp)) - 128;
153			}
154		}
155		else
156		{ // true 32-bit floats
157			for (; n > 0; n--, sp+=num_components, dp++)
158			{
159				dp->fval = (((float)(*sp)) / 256.0F) - 0.5F;
160			}
161		}
162	}
163	else
164	{
165		kdu_sample16 *dp = line.get_buf16();
166		if (line.is_absolute())
167		{ // 16-bit absolute integers
168			for (; n > 0; n--, sp+=num_components, dp++)
169			{
170				dp->ival = ((kdu_int16)(*sp)) - 128;
171			}
172		}
173		else
174		{ // 16-bit normalized representation.
175			for (; n > 0; n--, sp+=num_components, dp++)
176			{
177				dp->ival = (((kdu_int16)(*sp)) - 128) << (KDU_FIX_POINT-8);
178			}
179		}
180	}
181
182	scan->next_x_tnum++;
183	if (idx == (num_components-1))
184	{
185		scan->accessed_samples += line.get_width();
186	}
187	if (scan->accessed_samples == cols)
188	{ // Send empty line to free list.
189		assert(scan == incomplete_lines);
190		incomplete_lines = scan->next;
191		scan->next = free_lines;
192		free_lines = scan;
193	}
194
195  return true;
196}