PageRenderTime 1000ms CodeModel.GetById 212ms app.highlight 528ms RepoModel.GetById 156ms app.codeStats 1ms

/indra/llkdu/llimagej2ckdu.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1156 lines | 888 code | 129 blank | 139 comment | 126 complexity | 4b00bea675ffa7dbaf6d17ee3eb77c36 MD5 | raw file
   1 /** 
   2 * @file llimagej2ckdu.cpp
   3 * @brief This is an implementation of JPEG2000 encode/decode using Kakadu
   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 "llimagej2ckdu.h"
  29
  30#include "lltimer.h"
  31#include "llpointer.h"
  32#include "llmath.h"
  33#include "llkdumem.h"
  34
  35
  36class kdc_flow_control {
  37	
  38public:
  39	kdc_flow_control(kdu_image_in_base *img_in, kdu_codestream codestream);
  40	~kdc_flow_control();
  41	bool advance_components();
  42	void process_components();
  43	
  44private:
  45	
  46	struct kdc_component_flow_control {
  47	public:
  48		kdu_image_in_base *reader;
  49		int vert_subsampling;
  50		int ratio_counter;  /*  Initialized to 0, decremented by `count_delta';
  51                                when < 0, a new line must be processed, after
  52                                which it is incremented by `vert_subsampling'.  */
  53		int initial_lines;
  54		int remaining_lines;
  55		kdu_line_buf *line;
  56	};
  57	
  58	kdu_codestream codestream;
  59	kdu_dims valid_tile_indices;
  60	kdu_coords tile_idx;
  61	kdu_tile tile;
  62	int num_components;
  63	kdc_component_flow_control *components;
  64	int count_delta; // Holds the minimum of the `vert_subsampling' fields
  65	kdu_multi_analysis engine;
  66	kdu_long max_buffer_memory;
  67};
  68
  69//
  70// Kakadu specific implementation
  71//
  72void set_default_colour_weights(kdu_params *siz);
  73
  74const char* engineInfoLLImageJ2CKDU()
  75{
  76	static std::string version = llformat("KDU %s", KDU_CORE_VERSION);
  77	return version.c_str();
  78}
  79
  80LLImageJ2CKDU* createLLImageJ2CKDU()
  81{
  82	return new LLImageJ2CKDU();
  83}
  84
  85void destroyLLImageJ2CKDU(LLImageJ2CKDU* kdu)
  86{
  87	delete kdu;
  88	kdu = NULL;
  89}
  90
  91LLImageJ2CImpl* fallbackCreateLLImageJ2CImpl()
  92{
  93	return new LLImageJ2CKDU();
  94}
  95
  96void fallbackDestroyLLImageJ2CImpl(LLImageJ2CImpl* impl)
  97{
  98	delete impl;
  99	impl = NULL;
 100}
 101
 102const char* fallbackEngineInfoLLImageJ2CImpl()
 103{
 104	return engineInfoLLImageJ2CKDU();
 105}
 106
 107class LLKDUDecodeState
 108{
 109public:
 110	LLKDUDecodeState(kdu_tile tile, kdu_byte *buf, S32 row_gap);
 111	~LLKDUDecodeState();
 112	BOOL processTileDecode(F32 decode_time, BOOL limit_time = TRUE);
 113
 114private:
 115	S32 mNumComponents;
 116	BOOL mUseYCC;
 117	kdu_dims mDims;
 118	kdu_sample_allocator mAllocator;
 119	kdu_tile_comp mComps[4];
 120	kdu_line_buf mLines[4];
 121	kdu_pull_ifc mEngines[4];
 122	bool mReversible[4]; // Some components may be reversible and others not
 123	int mBitDepths[4];   // Original bit-depth may be quite different from 8
 124	
 125	kdu_tile mTile;
 126	kdu_byte *mBuf;
 127	S32 mRowGap;
 128};
 129
 130void ll_kdu_error( void )
 131{
 132	// *FIX: This exception is bad, bad, bad. It gets thrown from a
 133	// destructor which can lead to immediate program termination!
 134	throw "ll_kdu_error() throwing an exception";
 135}
 136
 137// Stuff for new kdu error handling
 138class LLKDUMessageWarning : public kdu_message
 139{
 140public:
 141	/*virtual*/ void put_text(const char *s);
 142	/*virtual*/ void put_text(const kdu_uint16 *s);
 143
 144	static LLKDUMessageWarning sDefaultMessage;
 145};
 146
 147class LLKDUMessageError : public kdu_message
 148{
 149public:
 150	/*virtual*/ void put_text(const char *s);
 151	/*virtual*/ void put_text(const kdu_uint16 *s);
 152	/*virtual*/ void flush(bool end_of_message = false);
 153	static LLKDUMessageError sDefaultMessage;
 154};
 155
 156void LLKDUMessageWarning::put_text(const char *s)
 157{
 158	llinfos << "KDU Warning: " << s << llendl;
 159}
 160
 161void LLKDUMessageWarning::put_text(const kdu_uint16 *s)
 162{
 163	llinfos << "KDU Warning: " << s << llendl;
 164}
 165
 166void LLKDUMessageError::put_text(const char *s)
 167{
 168	llinfos << "KDU Error: " << s << llendl;
 169}
 170
 171void LLKDUMessageError::put_text(const kdu_uint16 *s)
 172{
 173	llinfos << "KDU Error: " << s << llendl;
 174}
 175
 176void LLKDUMessageError::flush(bool end_of_message)
 177{
 178	if (end_of_message) 
 179	{
 180		throw "KDU throwing an exception";
 181	}
 182}
 183
 184LLKDUMessageWarning LLKDUMessageWarning::sDefaultMessage;
 185LLKDUMessageError	LLKDUMessageError::sDefaultMessage;
 186static bool kdu_message_initialized = false;
 187
 188LLImageJ2CKDU::LLImageJ2CKDU() : LLImageJ2CImpl(),
 189mInputp(NULL),
 190mCodeStreamp(NULL),
 191mTPosp(NULL),
 192mTileIndicesp(NULL),
 193mRawImagep(NULL),
 194mDecodeState(NULL),
 195mBlocksSize(-1),
 196mPrecinctsSize(-1),
 197mLevels(0)
 198{
 199}
 200
 201LLImageJ2CKDU::~LLImageJ2CKDU()
 202{
 203	cleanupCodeStream(); // in case destroyed before decode completed
 204}
 205
 206// Stuff for new simple decode
 207void transfer_bytes(kdu_byte *dest, kdu_line_buf &src, int gap, int precision);
 208
 209void LLImageJ2CKDU::setupCodeStream(LLImageJ2C &base, BOOL keep_codestream, ECodeStreamMode mode)
 210{
 211	S32 data_size = base.getDataSize();
 212	S32 max_bytes = (base.getMaxBytes() ? base.getMaxBytes() : data_size);
 213
 214	//
 215	//  Initialization
 216	//
 217	if (!kdu_message_initialized)
 218	{
 219		kdu_message_initialized = true;
 220		kdu_customize_errors(&LLKDUMessageError::sDefaultMessage);
 221		kdu_customize_warnings(&LLKDUMessageWarning::sDefaultMessage);
 222	}
 223
 224	if (mCodeStreamp)
 225	{
 226		mCodeStreamp->destroy();
 227		delete mCodeStreamp;
 228		mCodeStreamp = NULL;
 229	}
 230
 231	if (!mInputp && base.getData())
 232	{
 233		// The compressed data has been loaded
 234		// Setup the source for the codestream
 235		mInputp = new LLKDUMemSource(base.getData(), data_size);
 236	}
 237
 238	if (mInputp)
 239	{
 240		mInputp->reset();
 241	}
 242	mCodeStreamp = new kdu_codestream;
 243
 244	mCodeStreamp->create(mInputp);
 245
 246	// Set the maximum number of bytes to use from the codestream
 247	mCodeStreamp->set_max_bytes(max_bytes);
 248
 249	//	If you want to flip or rotate the image for some reason, change
 250	// the resolution, or identify a restricted region of interest, this is
 251	// the place to do it.  You may use "kdu_codestream::change_appearance"
 252	// and "kdu_codestream::apply_input_restrictions" for this purpose.
 253	//	If you wish to truncate the code-stream prior to decompression, you
 254	// may use "kdu_codestream::set_max_bytes".
 255	//	If you wish to retain all compressed data so that the material
 256	// can be decompressed multiple times, possibly with different appearance
 257	// parameters, you should call "kdu_codestream::set_persistent" here.
 258	//	There are a variety of other features which must be enabled at
 259	// this point if you want to take advantage of them.  See the
 260	// descriptions appearing with the "kdu_codestream" interface functions
 261	// in "kdu_compressed.h" for an itemized account of these capabilities.
 262
 263	switch (mode)
 264	{
 265	case MODE_FAST:
 266		mCodeStreamp->set_fast();
 267		break;
 268	case MODE_RESILIENT:
 269		mCodeStreamp->set_resilient();
 270		break;
 271	case MODE_FUSSY:
 272		mCodeStreamp->set_fussy();
 273		break;
 274	default:
 275		llassert(0);
 276		mCodeStreamp->set_fast();
 277	}
 278
 279	kdu_dims dims;
 280	mCodeStreamp->get_dims(0,dims);
 281
 282	S32 components = mCodeStreamp->get_num_components();
 283
 284	if (components >= 3)
 285	{ // Check that components have consistent dimensions (for PPM file)
 286		kdu_dims dims1; mCodeStreamp->get_dims(1,dims1);
 287		kdu_dims dims2; mCodeStreamp->get_dims(2,dims2);
 288		if ((dims1 != dims) || (dims2 != dims))
 289		{
 290			llerrs << "Components don't have matching dimensions!" << llendl;
 291		}
 292	}
 293
 294	base.setSize(dims.size.x, dims.size.y, components);
 295
 296	if (!keep_codestream)
 297	{
 298		mCodeStreamp->destroy();
 299		delete mCodeStreamp;
 300		mCodeStreamp = NULL;
 301		delete mInputp;
 302		mInputp = NULL;
 303	}
 304}
 305
 306void LLImageJ2CKDU::cleanupCodeStream()
 307{
 308	delete mInputp;
 309	mInputp = NULL;
 310
 311	delete mDecodeState;
 312	mDecodeState = NULL;
 313
 314	if (mCodeStreamp)
 315	{
 316		mCodeStreamp->destroy();
 317		delete mCodeStreamp;
 318		mCodeStreamp = NULL;
 319	}
 320
 321	delete mTPosp;
 322	mTPosp = NULL;
 323
 324	delete mTileIndicesp;
 325	mTileIndicesp = NULL;
 326}
 327
 328BOOL LLImageJ2CKDU::initDecode(LLImageJ2C &base, LLImageRaw &raw_image, int discard_level, int* region)
 329{
 330	return initDecode(base,raw_image,0.0f,MODE_FAST,0,4,discard_level,region);
 331}
 332
 333BOOL LLImageJ2CKDU::initEncode(LLImageJ2C &base, LLImageRaw &raw_image, int blocks_size, int precincts_size, int levels)
 334{
 335	mPrecinctsSize = precincts_size;
 336	if (mPrecinctsSize != -1)
 337	{
 338		mPrecinctsSize = get_lower_power_two(mPrecinctsSize,MAX_PRECINCT_SIZE);
 339		mPrecinctsSize = llmax(mPrecinctsSize,MIN_PRECINCT_SIZE);
 340	}
 341	mBlocksSize = blocks_size;
 342	if (mBlocksSize != -1)
 343	{
 344		mBlocksSize = get_lower_power_two(mBlocksSize,MAX_BLOCK_SIZE);
 345		mBlocksSize = llmax(mBlocksSize,MIN_BLOCK_SIZE);
 346		if (mPrecinctsSize != -1)
 347		{
 348			mBlocksSize = llmin(mBlocksSize,mPrecinctsSize);	// blocks *must* be smaller than precincts
 349		}
 350	}
 351	mLevels = levels;
 352	if (mLevels != 0)
 353	{
 354		mLevels = llclamp(mLevels,MIN_DECOMPOSITION_LEVELS,MIN_DECOMPOSITION_LEVELS);		
 355	}
 356	return TRUE;
 357}
 358
 359BOOL LLImageJ2CKDU::initDecode(LLImageJ2C &base, LLImageRaw &raw_image, F32 decode_time, ECodeStreamMode mode, S32 first_channel, S32 max_channel_count, int discard_level, int* region)
 360{
 361	base.resetLastError();
 362
 363	// *FIX: kdu calls our callback function if there's an error, and then bombs.
 364	// To regain control, we throw an exception, and catch it here.
 365	try
 366	{
 367		base.updateRawDiscardLevel();
 368		setupCodeStream(base, TRUE, mode);
 369
 370		mRawImagep = &raw_image;
 371		mCodeStreamp->change_appearance(false, true, false);
 372
 373		// Apply loading discard level and cropping if required
 374		kdu_dims* region_kdu = NULL;
 375		if (region != NULL)
 376		{
 377			region_kdu = new kdu_dims;
 378			region_kdu->pos.x  = region[0];
 379			region_kdu->pos.y  = region[1];
 380			region_kdu->size.x = region[2] - region[0];
 381			region_kdu->size.y = region[3] - region[1];
 382		}
 383		int discard = (discard_level != -1 ? discard_level : base.getRawDiscardLevel());
 384		
 385		// Apply loading restrictions
 386		mCodeStreamp->apply_input_restrictions( first_channel, max_channel_count, discard, 0, region_kdu);
 387		
 388		// Clean-up
 389		if (region_kdu)
 390		{
 391			delete region_kdu;
 392			region_kdu = NULL;
 393		}
 394
 395		// Resize raw_image according to the image to be decoded
 396		kdu_dims dims; mCodeStreamp->get_dims(0,dims);
 397		// *TODO: Use the real number of levels read from the file throughout the code instead of relying on an infered value from dimensions
 398		//S32 levels = mCodeStreamp->get_min_dwt_levels();
 399		S32 channels = base.getComponents() - first_channel;
 400		channels = llmin(channels,max_channel_count);
 401		raw_image.resize(dims.size.x, dims.size.y, channels);
 402		//llinfos << "j2c image dimension: width = " << dims.size.x << ", height = " << dims.size.y << ", channels = " << channels << ", levels = " << levels << llendl;
 403
 404		if (!mTileIndicesp)
 405		{
 406			mTileIndicesp = new kdu_dims;
 407		}
 408		mCodeStreamp->get_valid_tiles(*mTileIndicesp);
 409		if (!mTPosp)
 410		{
 411			mTPosp = new kdu_coords;
 412			mTPosp->y = 0;
 413			mTPosp->x = 0;
 414		}
 415	}
 416	catch (const char* msg)
 417	{
 418		base.setLastError(ll_safe_string(msg));
 419		return FALSE;
 420	}
 421	catch (...)
 422	{
 423		base.setLastError("Unknown J2C error");
 424		return FALSE;
 425	}
 426
 427	return TRUE;
 428}
 429
 430
 431// Returns TRUE to mean done, whether successful or not.
 432BOOL LLImageJ2CKDU::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decode_time, S32 first_channel, S32 max_channel_count)
 433{
 434	ECodeStreamMode mode = MODE_FAST;
 435
 436	LLTimer decode_timer;
 437
 438	if (!mCodeStreamp)
 439	{
 440		if (!initDecode(base, raw_image, decode_time, mode, first_channel, max_channel_count))
 441		{
 442			// Initializing the J2C decode failed, bail out.
 443			cleanupCodeStream();
 444			return TRUE; // done
 445		}
 446	}
 447
 448	// These can probably be grabbed from what's saved in the class.
 449	kdu_dims dims;
 450	mCodeStreamp->get_dims(0,dims);
 451
 452	// Now we are ready to walk through the tiles processing them one-by-one.
 453	kdu_byte *buffer = raw_image.getData();
 454
 455	while (mTPosp->y < mTileIndicesp->size.y)
 456	{
 457		while (mTPosp->x < mTileIndicesp->size.x)
 458		{
 459			try
 460			{
 461				if (!mDecodeState)
 462				{
 463					kdu_tile tile = mCodeStreamp->open_tile(*(mTPosp)+mTileIndicesp->pos);
 464
 465					// Find the region of the buffer occupied by this
 466					// tile.  Note that we have no control over
 467					// sub-sampling factors which might have been used
 468					// during compression and so it can happen that tiles
 469					// (at the image component level) actually have
 470					// different dimensions.  For this reason, we cannot
 471					// figure out the buffer region occupied by a tile
 472					// directly from the tile indices.  Instead, we query
 473					// the highest resolution of the first tile-component
 474					// concerning its location and size on the canvas --
 475					// the `dims' object already holds the location and
 476					// size of the entire image component on the same
 477					// canvas coordinate system.  Comparing the two tells
 478					// us where the current tile is in the buffer.
 479					S32 channels = base.getComponents() - first_channel;
 480					if (channels > max_channel_count)
 481					{
 482						channels = max_channel_count;
 483					}
 484					kdu_resolution res = tile.access_component(0).access_resolution();
 485					kdu_dims tile_dims; res.get_dims(tile_dims);
 486					kdu_coords offset = tile_dims.pos - dims.pos;
 487					int row_gap = channels*dims.size.x; // inter-row separation
 488					kdu_byte *buf = buffer + offset.y*row_gap + offset.x*channels;
 489					mDecodeState = new LLKDUDecodeState(tile, buf, row_gap);
 490				}
 491				// Do the actual processing
 492				F32 remaining_time = decode_time - decode_timer.getElapsedTimeF32();
 493				// This is where we do the actual decode.  If we run out of time, return false.
 494				if (mDecodeState->processTileDecode(remaining_time, (decode_time > 0.0f)))
 495				{
 496					delete mDecodeState;
 497					mDecodeState = NULL;
 498				}
 499				else
 500				{
 501					// Not finished decoding yet.
 502					//					setLastError("Ran out of time while decoding");
 503					return FALSE;
 504				}
 505			}
 506			catch (const char* msg)
 507			{
 508				base.setLastError(ll_safe_string(msg));
 509				base.decodeFailed();
 510				cleanupCodeStream();
 511				return TRUE; // done
 512			}
 513			catch (...)
 514			{
 515				base.setLastError( "Unknown J2C error" );
 516				base.decodeFailed();
 517				cleanupCodeStream();
 518				return TRUE; // done
 519			}
 520
 521
 522			mTPosp->x++;
 523		}
 524		mTPosp->y++;
 525		mTPosp->x = 0;
 526	}
 527
 528	cleanupCodeStream();
 529
 530	return TRUE;
 531}
 532
 533
 534BOOL LLImageJ2CKDU::encodeImpl(LLImageJ2C &base, const LLImageRaw &raw_image, const char* comment_text, F32 encode_time, BOOL reversible)
 535{
 536	// Declare and set simple arguments
 537	bool transpose = false;
 538	bool vflip = true;
 539	bool hflip = false;
 540
 541	try
 542	{
 543		// Set up input image files
 544		siz_params siz;
 545		
 546		// Should set rate someplace here
 547		LLKDUMemIn mem_in(raw_image.getData(),
 548			raw_image.getDataSize(),
 549			raw_image.getWidth(),
 550			raw_image.getHeight(),
 551			raw_image.getComponents(),
 552			&siz);
 553
 554		base.setSize(raw_image.getWidth(), raw_image.getHeight(), raw_image.getComponents());
 555
 556		int num_components = raw_image.getComponents();
 557
 558		siz.set(Scomponents,0,0,num_components);
 559		siz.set(Sdims,0,0,base.getHeight());  // Height of first image component
 560		siz.set(Sdims,0,1,base.getWidth());   // Width of first image component
 561		siz.set(Sprecision,0,0,8);  // Image samples have original bit-depth of 8
 562		siz.set(Ssigned,0,0,false); // Image samples are originally unsigned
 563
 564		kdu_params *siz_ref = &siz; 
 565		siz_ref->finalize();
 566		siz_params transformed_siz; // Use this one to construct code-stream
 567		transformed_siz.copy_from(&siz,-1,-1,-1,0,transpose,false,false);
 568
 569		// Construct the `kdu_codestream' object and parse all remaining arguments
 570		U32 max_output_size = base.getWidth()*base.getHeight()*base.getComponents();
 571		max_output_size = (max_output_size < 1000 ? 1000 : max_output_size);
 572		U8 *output_buffer = new U8[max_output_size];
 573		U32 output_size = 0; // Address updated by LLKDUMemTarget to give the final compressed buffer size
 574		LLKDUMemTarget output(output_buffer, output_size, max_output_size);
 575
 576		kdu_codestream codestream;
 577		codestream.create(&transformed_siz,&output);
 578
 579		if (comment_text)
 580		{
 581			// Set the comments for the codestream
 582			kdu_codestream_comment comment = codestream.add_comment();
 583			comment.put_text(comment_text);
 584		}
 585
 586		// Set codestream options
 587		int num_layer_specs = 0;
 588
 589		kdu_long layer_bytes[64];
 590		U32 max_bytes = 0;
 591
 592		if (num_components >= 3)
 593		{
 594			// Note that we always use YCC and not YUV
 595			// *TODO: Verify this doesn't screws up reversible textures (like sculpties) as YCC is not reversible but YUV is...
 596			set_default_colour_weights(codestream.access_siz());
 597		}
 598
 599		if (reversible)
 600		{
 601			codestream.access_siz()->parse_string("Creversible=yes");
 602			// *TODO: we should use yuv in reversible mode and one level since those images are small. 
 603			// Don't turn this on now though as both create problems on decoding for the moment
 604			//codestream.access_siz()->parse_string("Clevels=1");
 605			//codestream.access_siz()->parse_string("Cycc=no");
 606			// If we're doing reversible (i.e. lossless compression), assumes we're not using quality layers.
 607			// *TODO: this is incorrect and unecessary. Try using the regular layer setting.
 608			codestream.access_siz()->parse_string("Clayers=1");
 609			num_layer_specs = 1;
 610			layer_bytes[0] = 0;
 611		}
 612		else
 613		{
 614			// Rate is the argument passed into the LLImageJ2C which
 615			// specifies the target compression rate.  The default is 8:1.
 616			// Possibly if max_bytes < 500, we should just use the default setting?
 617			// *TODO: mRate is actually always 8:1 in the viewer. Test different values. Also force to reversible for small (< 500 bytes) textures.
 618			if (base.mRate != 0.f)
 619			{
 620				max_bytes = (U32)(base.mRate*base.getWidth()*base.getHeight()*base.getComponents());
 621			}
 622			else
 623			{
 624				max_bytes = (U32)(base.getWidth()*base.getHeight()*base.getComponents()*0.125);
 625			}
 626
 627			const U32 min_bytes = FIRST_PACKET_SIZE;
 628			if (max_bytes > min_bytes)
 629			{
 630				U32 i;
 631				// This code is where we specify the target number of bytes for
 632				// each layer.  Not sure if we should do this for small images
 633				// or not.  The goal is to have this roughly align with
 634				// different quality levels that we decode at.
 635				for (i = min_bytes; i < max_bytes; i*=4)
 636				{
 637					if (i == min_bytes * 4)
 638					{
 639						i = 2000;
 640					}
 641					layer_bytes[num_layer_specs] = i;
 642					num_layer_specs++;
 643				}
 644				layer_bytes[num_layer_specs] = max_bytes;
 645				num_layer_specs++;
 646
 647				std::string layer_string = llformat("Clayers=%d",num_layer_specs);
 648				codestream.access_siz()->parse_string(layer_string.c_str());
 649			}
 650			else
 651			{
 652				layer_bytes[0] = min_bytes;
 653				num_layer_specs = 1;
 654				std::string layer_string = llformat("Clayers=%d",num_layer_specs);
 655				codestream.access_siz()->parse_string(layer_string.c_str());
 656			}
 657		}
 658		
 659		// Set up data ordering, markers, etc... if precincts or blocks specified
 660		if ((mBlocksSize != -1) || (mPrecinctsSize != -1))
 661		{
 662			if (mPrecinctsSize != -1)
 663			{
 664				std::string precincts_string = llformat("Cprecincts={%d,%d}",mPrecinctsSize,mPrecinctsSize);
 665				codestream.access_siz()->parse_string(precincts_string.c_str());
 666			}
 667			if (mBlocksSize != -1)
 668			{
 669				std::string blocks_string = llformat("Cblk={%d,%d}",mBlocksSize,mBlocksSize);
 670				codestream.access_siz()->parse_string(blocks_string.c_str());
 671			}
 672			std::string ordering_string = llformat("Corder=RPCL");
 673			codestream.access_siz()->parse_string(ordering_string.c_str());
 674			std::string PLT_string = llformat("ORGgen_plt=yes");
 675			codestream.access_siz()->parse_string(PLT_string.c_str());
 676			std::string Parts_string = llformat("ORGtparts=R");
 677			codestream.access_siz()->parse_string(Parts_string.c_str());
 678		}
 679		if (mLevels != 0)
 680		{
 681			std::string levels_string = llformat("Clevels=%d",mLevels);
 682			codestream.access_siz()->parse_string(levels_string.c_str());
 683		}
 684		
 685		codestream.access_siz()->finalize_all();
 686		codestream.change_appearance(transpose,vflip,hflip);
 687
 688		// Now we are ready for sample data processing.
 689		kdc_flow_control *tile = new kdc_flow_control(&mem_in,codestream);
 690		bool done = false;
 691		while (!done)
 692		{ 
 693			// Process line by line
 694			if (tile->advance_components())
 695			{
 696				tile->process_components();
 697			}
 698			else
 699			{
 700				done = true;
 701			}
 702		}
 703
 704		// Produce the compressed output
 705		codestream.flush(layer_bytes,num_layer_specs);
 706
 707		// Cleanup
 708		delete tile;
 709		codestream.destroy();
 710
 711		// Now that we're done encoding, create the new data buffer for the compressed
 712		// image and stick it there.
 713		base.copyData(output_buffer, output_size);
 714		base.updateData(); // set width, height
 715		delete[] output_buffer;
 716	}
 717	catch(const char* msg)
 718	{
 719		base.setLastError(ll_safe_string(msg));
 720		return FALSE;
 721	}
 722	catch( ... )
 723	{
 724		base.setLastError( "Unknown J2C error" );
 725		return FALSE;
 726	}
 727
 728	return TRUE;
 729}
 730
 731BOOL LLImageJ2CKDU::getMetadata(LLImageJ2C &base)
 732{
 733	// *FIX: kdu calls our callback function if there's an error, and
 734	// then bombs. To regain control, we throw an exception, and
 735	// catch it here.
 736	try
 737	{
 738		setupCodeStream(base, FALSE, MODE_FAST);
 739		return TRUE;
 740	}
 741	catch (const char* msg)
 742	{
 743		base.setLastError(ll_safe_string(msg));
 744		return FALSE;
 745	}
 746	catch (...)
 747	{
 748		base.setLastError( "Unknown J2C error" );
 749		return FALSE;
 750	}
 751}
 752
 753void set_default_colour_weights(kdu_params *siz)
 754{
 755	kdu_params *cod = siz->access_cluster(COD_params);
 756	assert(cod != NULL);
 757
 758	bool can_use_ycc = true;
 759	bool rev0 = false;
 760	int depth0 = 0, sub_x0 = 1, sub_y0 = 1;
 761	for (int c = 0; c < 3; c++)
 762	{
 763		int depth = 0; siz->get(Sprecision,c,0,depth);
 764		int sub_y = 1; siz->get(Ssampling,c,0,sub_y);
 765		int sub_x = 1; siz->get(Ssampling,c,1,sub_x);
 766		kdu_params *coc = cod->access_relation(-1,c);
 767		bool rev = false; coc->get(Creversible,0,0,rev);
 768		if (c == 0)
 769		{
 770			rev0 = rev; depth0 = depth; sub_x0 = sub_x; sub_y0 = sub_y;
 771		}
 772		else if ((rev != rev0) || (depth != depth0) || 
 773				 (sub_x != sub_x0) || (sub_y != sub_y0))
 774		{
 775			can_use_ycc = false;
 776		}
 777	}
 778	if (!can_use_ycc)
 779	{
 780		return;
 781	}
 782
 783	bool use_ycc;
 784	if (!cod->get(Cycc,0,0,use_ycc))
 785	{
 786		cod->set(Cycc,0,0,use_ycc=true);
 787	}
 788	if (!use_ycc)
 789	{
 790		return;
 791	}
 792	float weight;
 793	if (cod->get(Clev_weights,0,0,weight) || cod->get(Cband_weights,0,0,weight))
 794	{
 795		// Weights already specified explicitly -> nothing to do
 796		return; 
 797	}
 798
 799	// These example weights are adapted from numbers generated by Marcus Nadenau
 800	// at EPFL, for a viewing distance of 15 cm and a display resolution of
 801	// 300 DPI.
 802
 803	cod->parse_string("Cband_weights:C0="
 804		"{0.0901},{0.2758},{0.2758},"
 805		"{0.7018},{0.8378},{0.8378},{1}");
 806	cod->parse_string("Cband_weights:C1="
 807		"{0.0263},{0.0863},{0.0863},"
 808		"{0.1362},{0.2564},{0.2564},"
 809		"{0.3346},{0.4691},{0.4691},"
 810		"{0.5444},{0.6523},{0.6523},"
 811		"{0.7078},{0.7797},{0.7797},{1}");
 812	cod->parse_string("Cband_weights:C2="
 813		"{0.0773},{0.1835},{0.1835},"
 814		"{0.2598},{0.4130},{0.4130},"
 815		"{0.5040},{0.6464},{0.6464},"
 816		"{0.7220},{0.8254},{0.8254},"
 817		"{0.8769},{0.9424},{0.9424},{1}");
 818}
 819
 820/******************************************************************************/
 821/*                              transfer_bytes                                */
 822/******************************************************************************/
 823
 824void transfer_bytes(kdu_byte *dest, kdu_line_buf &src, int gap, int precision)
 825/* Transfers source samples from the supplied line buffer into the output
 826byte buffer, spacing successive output samples apart by `gap' bytes
 827(to allow for interleaving of colour components).  The function performs
 828all necessary level shifting, type conversion, rounding and truncation. */
 829{
 830	int width = src.get_width();
 831	if (src.get_buf32() != NULL)
 832	{ // Decompressed samples have a 32-bit representation (integer or float)
 833		assert(precision >= 8); // Else would have used 16 bit representation
 834		kdu_sample32 *sp = src.get_buf32();
 835		if (!src.is_absolute())
 836		{ // Transferring normalized floating point data.
 837			float scale16 = (float)(1<<16);
 838			kdu_int32 val;
 839
 840			for (; width > 0; width--, sp++, dest+=gap)
 841			{
 842				val = (kdu_int32)(sp->fval*scale16);
 843				val = (val+128)>>8; // May be faster than true rounding
 844				val += 128;
 845				if (val & ((-1)<<8))
 846				{
 847					val = (val < 0 ? 0 : 255);
 848				}
 849				*dest = (kdu_byte) val;
 850			}
 851		}
 852		else
 853		{ // Transferring 32-bit absolute integers.
 854			kdu_int32 val;
 855			kdu_int32 downshift = precision-8;
 856			kdu_int32 offset = (1<<downshift)>>1;
 857
 858			for (; width > 0; width--, sp++, dest+=gap)
 859			{
 860				val = sp->ival;
 861				val = (val+offset)>>downshift;
 862				val += 128;
 863				if (val & ((-1)<<8))
 864				{
 865					val = (val < 0 ? 0 : 255);
 866				}
 867				*dest = (kdu_byte) val;
 868			}
 869		}
 870	}
 871	else
 872	{ // Source data is 16 bits.
 873		kdu_sample16 *sp = src.get_buf16();
 874		if (!src.is_absolute())
 875		{ // Transferring 16-bit fixed point quantities
 876			kdu_int16 val;
 877
 878			if (precision >= 8)
 879			{ // Can essentially ignore the bit-depth.
 880				for (; width > 0; width--, sp++, dest+=gap)
 881				{
 882					val = sp->ival;
 883					val += (1<<(KDU_FIX_POINT-8))>>1;
 884					val >>= (KDU_FIX_POINT-8);
 885					val += 128;
 886					if (val & ((-1)<<8))
 887					{
 888						val = (val < 0 ? 0 : 255);
 889					}
 890					*dest = (kdu_byte) val;
 891				}
 892			}
 893			else
 894			{ // Need to force zeros into one or more least significant bits.
 895				kdu_int16 downshift = KDU_FIX_POINT-precision;
 896				kdu_int16 upshift = 8-precision;
 897				kdu_int16 offset = 1<<(downshift-1);
 898
 899				for (; width > 0; width--, sp++, dest+=gap)
 900				{
 901					val = sp->ival;
 902					val = (val+offset)>>downshift;
 903					val <<= upshift;
 904					val += 128;
 905					if (val & ((-1)<<8))
 906					{
 907						val = (val < 0 ? 0 : 256 - (1<<upshift));
 908					}
 909					*dest = (kdu_byte) val;
 910				}
 911			}
 912		}
 913		else
 914		{ // Transferring 16-bit absolute integers.
 915			kdu_int16 val;
 916
 917			if (precision >= 8)
 918			{
 919				kdu_int16 downshift = precision-8;
 920				kdu_int16 offset = (1<<downshift)>>1;
 921
 922				for (; width > 0; width--, sp++, dest+=gap)
 923				{
 924					val = sp->ival;
 925					val = (val+offset)>>downshift;
 926					val += 128;
 927					if (val & ((-1)<<8))
 928					{
 929						val = (val < 0 ? 0 : 255);
 930					}
 931					*dest = (kdu_byte) val;
 932				}
 933			}
 934			else
 935			{
 936				kdu_int16 upshift = 8-precision;
 937
 938				for (; width > 0; width--, sp++, dest+=gap)
 939				{
 940					val = sp->ival;
 941					val <<= upshift;
 942					val += 128;
 943					if (val & ((-1)<<8))
 944					{
 945						val = (val < 0 ? 0 : 256 - (1<<upshift));
 946					}
 947					*dest = (kdu_byte) val;
 948				}
 949			}
 950		}
 951	}
 952}
 953
 954LLKDUDecodeState::LLKDUDecodeState(kdu_tile tile, kdu_byte *buf, S32 row_gap)
 955{
 956	S32 c;
 957
 958	mTile = tile;
 959	mBuf = buf;
 960	mRowGap = row_gap;
 961
 962	mNumComponents = tile.get_num_components();
 963
 964	llassert(mNumComponents <= 4);
 965	mUseYCC = tile.get_ycc();
 966
 967	for (c = 0; c < 4; ++c)
 968	{
 969		mReversible[c] = false;
 970		mBitDepths[c] = 0;
 971	}
 972
 973	// Open tile-components and create processing engines and resources
 974	for (c = 0; c < mNumComponents; c++)
 975	{
 976		mComps[c] = mTile.access_component(c);
 977		mReversible[c] = mComps[c].get_reversible();
 978		mBitDepths[c] = mComps[c].get_bit_depth();
 979		kdu_resolution res = mComps[c].access_resolution(); // Get top resolution
 980		kdu_dims comp_dims; res.get_dims(comp_dims);
 981		if (c == 0)
 982		{
 983			mDims = comp_dims;
 984		}
 985		else
 986		{
 987			llassert(mDims == comp_dims); // Safety check; the caller has ensured this
 988		}
 989		bool use_shorts = (mComps[c].get_bit_depth(true) <= 16);
 990		mLines[c].pre_create(&mAllocator,mDims.size.x,mReversible[c],use_shorts);
 991		if (res.which() == 0) // No DWT levels used
 992		{
 993			mEngines[c] = kdu_decoder(res.access_subband(LL_BAND),&mAllocator,use_shorts);
 994		}
 995		else
 996		{
 997			mEngines[c] = kdu_synthesis(res,&mAllocator,use_shorts);
 998		}
 999	}
1000	mAllocator.finalize(); // Actually creates buffering resources
1001	for (c = 0; c < mNumComponents; c++)
1002	{
1003		mLines[c].create(); // Grabs resources from the allocator.
1004	}
1005}
1006
1007LLKDUDecodeState::~LLKDUDecodeState()
1008{
1009	// Cleanup
1010	for (S32 c = 0; c < mNumComponents; c++)
1011	{
1012		mEngines[c].destroy(); // engines are interfaces; no default destructors
1013	}
1014	mTile.close();
1015}
1016
1017BOOL LLKDUDecodeState::processTileDecode(F32 decode_time, BOOL limit_time)
1018/* Decompresses a tile, writing the data into the supplied byte buffer.
1019The buffer contains interleaved image components, if there are any.
1020Although you may think of the buffer as belonging entirely to this tile,
1021the `buf' pointer may actually point into a larger buffer representing
1022multiple tiles.  For this reason, `row_gap' is needed to identify the
1023separation between consecutive rows in the real buffer. */
1024{
1025	S32 c;
1026	// Now walk through the lines of the buffer, recovering them from the
1027	// relevant tile-component processing engines.
1028
1029	LLTimer decode_timer;
1030	while (mDims.size.y--)
1031	{
1032		for (c = 0; c < mNumComponents; c++)
1033		{
1034			mEngines[c].pull(mLines[c],true);
1035		}
1036		if ((mNumComponents >= 3) && mUseYCC)
1037		{
1038			kdu_convert_ycc_to_rgb(mLines[0],mLines[1],mLines[2]);
1039		}
1040		for (c = 0; c < mNumComponents; c++)
1041		{
1042			transfer_bytes(mBuf+c,mLines[c],mNumComponents,mBitDepths[c]);
1043		}
1044		mBuf += mRowGap;
1045		if (mDims.size.y % 10)
1046		{
1047			if (limit_time && decode_timer.getElapsedTimeF32() > decode_time)
1048			{
1049				return FALSE;
1050			}
1051		}
1052	}
1053	return TRUE;
1054}
1055
1056// kdc_flow_control 
1057
1058kdc_flow_control::kdc_flow_control (kdu_image_in_base *img_in, kdu_codestream codestream)
1059{
1060	int n;
1061	
1062	this->codestream = codestream;
1063	codestream.get_valid_tiles(valid_tile_indices);
1064	tile_idx = valid_tile_indices.pos;
1065	tile = codestream.open_tile(tile_idx,NULL);
1066	
1067	// Set up the individual components
1068	num_components = codestream.get_num_components(true);
1069	components = new kdc_component_flow_control[num_components];
1070	count_delta = 0;
1071	kdc_component_flow_control *comp = components;
1072	for (n = 0; n < num_components; n++, comp++)
1073	{
1074		comp->line = NULL;
1075		comp->reader = img_in;
1076		kdu_coords subsampling;  
1077		codestream.get_subsampling(n,subsampling,true);
1078		kdu_dims dims;  
1079		codestream.get_tile_dims(tile_idx,n,dims,true);
1080		comp->vert_subsampling = subsampling.y;
1081		if ((n == 0) || (comp->vert_subsampling < count_delta))
1082		{
1083			count_delta = comp->vert_subsampling;
1084		}
1085		comp->ratio_counter = 0;
1086		comp->remaining_lines = comp->initial_lines = dims.size.y;
1087	}
1088	assert(num_components >= 0);
1089	
1090	tile.set_components_of_interest(num_components);
1091	max_buffer_memory = engine.create(codestream,tile,false,NULL,false,1,NULL,NULL,false);
1092}
1093
1094kdc_flow_control::~kdc_flow_control()
1095{
1096	if (components != NULL)
1097	{
1098		delete[] components;
1099	}
1100	if (engine.exists())
1101	{
1102		engine.destroy();
1103	}
1104}
1105
1106bool kdc_flow_control::advance_components()
1107{
1108	bool found_line = false;
1109	while (!found_line)
1110	{
1111		bool all_done = true;
1112		kdc_component_flow_control *comp = components;
1113		for (int n = 0; n < num_components; n++, comp++)
1114		{
1115			assert(comp->ratio_counter >= 0);
1116			if (comp->remaining_lines > 0)
1117			{
1118				all_done = false;
1119				comp->ratio_counter -= count_delta;
1120				if (comp->ratio_counter < 0)
1121				{
1122					found_line = true;
1123					comp->line = engine.exchange_line(n,NULL,NULL);
1124					assert(comp->line != NULL);
1125					if (comp->line->get_width())
1126					{
1127						comp->reader->get(n,*(comp->line),0);
1128					}
1129				}
1130			}
1131		}
1132		if (all_done)
1133		{
1134			return false;
1135		}
1136	}
1137	return true;
1138}
1139
1140void kdc_flow_control::process_components()
1141{
1142	kdc_component_flow_control *comp = components;
1143	for (int n = 0; n < num_components; n++, comp++)
1144	{
1145		if (comp->ratio_counter < 0)
1146		{
1147			comp->ratio_counter += comp->vert_subsampling;
1148			assert(comp->ratio_counter >= 0);
1149			assert(comp->remaining_lines > 0);
1150			comp->remaining_lines--;
1151			assert(comp->line != NULL);
1152			engine.exchange_line(n,comp->line,NULL);
1153			comp->line = NULL;
1154		}
1155	}
1156}