From c6c1419faa6e4acef19b3ddfc6c89b4a10bca15f Mon Sep 17 00:00:00 2001
From: Merov Linden <merov@lindenlab.com>
Date: Wed, 4 May 2011 22:43:51 -0700
Subject: EXP-664 : Make encoding parameters more resilient to bad entries, add
 levels as encoding parameters, update test applet to support this

---
 indra/llkdu/llimagej2ckdu.cpp            | 37 ++++++++++++++++++++++++++++----
 indra/llkdu/llimagej2ckdu.h              |  3 ++-
 indra/llkdu/tests/llimagej2ckdu_test.cpp |  1 +
 3 files changed, 36 insertions(+), 5 deletions(-)

(limited to 'indra/llkdu')

diff --git a/indra/llkdu/llimagej2ckdu.cpp b/indra/llkdu/llimagej2ckdu.cpp
index ae456a48be..f83accf356 100644
--- a/indra/llkdu/llimagej2ckdu.cpp
+++ b/indra/llkdu/llimagej2ckdu.cpp
@@ -29,6 +29,7 @@
 
 #include "lltimer.h"
 #include "llpointer.h"
+#include "llmath.h"
 #include "llkdumem.h"
 
 
@@ -192,7 +193,8 @@ mTileIndicesp(NULL),
 mRawImagep(NULL),
 mDecodeState(NULL),
 mBlocksSize(-1),
-mPrecinctsSize(-1)
+mPrecinctsSize(-1),
+mLevels(0)
 {
 }
 
@@ -328,10 +330,30 @@ BOOL LLImageJ2CKDU::initDecode(LLImageJ2C &base, LLImageRaw &raw_image, int disc
 	return initDecode(base,raw_image,0.0f,MODE_FAST,0,4,discard_level,region);
 }
 
-BOOL LLImageJ2CKDU::initEncode(LLImageJ2C &base, LLImageRaw &raw_image, int blocks_size, int precincts_size)
+BOOL LLImageJ2CKDU::initEncode(LLImageJ2C &base, LLImageRaw &raw_image, int blocks_size, int precincts_size, int levels)
 {
-	mBlocksSize = blocks_size;
 	mPrecinctsSize = precincts_size;
+	if (mPrecinctsSize != -1)
+	{
+		mPrecinctsSize = get_lower_power_two(mPrecinctsSize,MAX_PRECINCT_SIZE);
+		mPrecinctsSize = llmax(mPrecinctsSize,MIN_PRECINCT_SIZE);
+	}
+	mBlocksSize = blocks_size;
+	if (mBlocksSize != -1)
+	{
+		mBlocksSize = get_lower_power_two(mBlocksSize,MAX_BLOCK_SIZE);
+		mBlocksSize = llmax(mBlocksSize,MIN_BLOCK_SIZE);
+		if (mPrecinctsSize != -1)
+		{
+			mBlocksSize = llmin(mBlocksSize,mPrecinctsSize);	// blocks *must* be smaller than precincts
+		}
+	}
+	mLevels = levels;
+	if (mLevels != 0)
+	{
+		mLevels = llmin(mLevels,MAX_DECOMPOSITION_LEVELS);
+		mLevels = llmax(MIN_DECOMPOSITION_LEVELS,mLevels);
+	}
 	return TRUE;
 }
 
@@ -373,10 +395,12 @@ BOOL LLImageJ2CKDU::initDecode(LLImageJ2C &base, LLImageRaw &raw_image, F32 deco
 
 		// Resize raw_image according to the image to be decoded
 		kdu_dims dims; mCodeStreamp->get_dims(0,dims);
+		// *TODO: Use the real number of levels read from the file throughout the code instead of relying on an infered value from dimensions
+		//S32 levels = mCodeStreamp->get_min_dwt_levels();
 		S32 channels = base.getComponents() - first_channel;
 		channels = llmin(channels,max_channel_count);
 		raw_image.resize(dims.size.x, dims.size.y, channels);
-		//	llinfos << "Resizing raw_image to " << dims.size.x << ":" << dims.size.y << llendl;
+		//llinfos << "j2c image dimension: width = " << dims.size.x << ", height = " << dims.size.y << ", channels = " << channels << ", levels = " << levels << llendl;
 
 		if (!mTileIndicesp)
 		{
@@ -653,6 +677,11 @@ BOOL LLImageJ2CKDU::encodeImpl(LLImageJ2C &base, const LLImageRaw &raw_image, co
 			std::string Parts_string = llformat("ORGtparts=R");
 			codestream.access_siz()->parse_string(Parts_string.c_str());
 		}
+		if (mLevels != 0)
+		{
+			std::string levels_string = llformat("Clevels=%d",mLevels);
+			codestream.access_siz()->parse_string(levels_string.c_str());
+		}
 		
 		codestream.access_siz()->finalize_all();
 		codestream.change_appearance(transpose,vflip,hflip);
diff --git a/indra/llkdu/llimagej2ckdu.h b/indra/llkdu/llimagej2ckdu.h
index 9fce58b762..1489dbf704 100644
--- a/indra/llkdu/llimagej2ckdu.h
+++ b/indra/llkdu/llimagej2ckdu.h
@@ -59,7 +59,7 @@ protected:
 	/*virtual*/ BOOL encodeImpl(LLImageJ2C &base, const LLImageRaw &raw_image, const char* comment_text, F32 encode_time=0.0,
 								BOOL reversible=FALSE);
 	/*virtual*/ BOOL initDecode(LLImageJ2C &base, LLImageRaw &raw_image, int discard_level = -1, int* region = NULL);
-	/*virtual*/ BOOL initEncode(LLImageJ2C &base, LLImageRaw &raw_image, int blocks_size = -1, int precincts_size = -1);
+	/*virtual*/ BOOL initEncode(LLImageJ2C &base, LLImageRaw &raw_image, int blocks_size = -1, int precincts_size = -1, int levels = 0);
 
 private:
 	BOOL initDecode(LLImageJ2C &base, LLImageRaw &raw_image, F32 decode_time, ECodeStreamMode mode, S32 first_channel, S32 max_channel_count, int discard_level = -1, int* region = NULL);
@@ -73,6 +73,7 @@ private:
 	kdu_dims *mTileIndicesp;
 	int mBlocksSize;
 	int mPrecinctsSize;
+	int mLevels;
 
 	// Temporary variables for in-progress decodes...
 	LLImageRaw *mRawImagep;
diff --git a/indra/llkdu/tests/llimagej2ckdu_test.cpp b/indra/llkdu/tests/llimagej2ckdu_test.cpp
index 7ac24a969a..ab60ab6d50 100644
--- a/indra/llkdu/tests/llimagej2ckdu_test.cpp
+++ b/indra/llkdu/tests/llimagej2ckdu_test.cpp
@@ -134,6 +134,7 @@ kdu_params* kdu_params::access_cluster(const char*) { return NULL; }
 void kdu_codestream::set_fast() { }
 void kdu_codestream::set_fussy() { }
 void kdu_codestream::get_dims(int, kdu_dims&, bool ) { }
+int kdu_codestream::get_min_dwt_levels() { return 5; }
 void kdu_codestream::change_appearance(bool, bool, bool) { }
 void kdu_codestream::get_tile_dims(kdu_coords, int, kdu_dims&, bool ) { }
 void kdu_codestream::destroy() { }
-- 
cgit v1.2.3


From 1e0d3fc7bc1102faaa98ea6753c4366d9cef7379 Mon Sep 17 00:00:00 2001
From: Merov Linden <merov@lindenlab.com>
Date: Sat, 7 May 2011 11:09:34 -0700
Subject: EXP-664 : following richard codereview: use llclamp where appropriate

---
 indra/llkdu/llimagej2ckdu.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

(limited to 'indra/llkdu')

diff --git a/indra/llkdu/llimagej2ckdu.cpp b/indra/llkdu/llimagej2ckdu.cpp
index f83accf356..39ae09650e 100644
--- a/indra/llkdu/llimagej2ckdu.cpp
+++ b/indra/llkdu/llimagej2ckdu.cpp
@@ -351,8 +351,7 @@ BOOL LLImageJ2CKDU::initEncode(LLImageJ2C &base, LLImageRaw &raw_image, int bloc
 	mLevels = levels;
 	if (mLevels != 0)
 	{
-		mLevels = llmin(mLevels,MAX_DECOMPOSITION_LEVELS);
-		mLevels = llmax(MIN_DECOMPOSITION_LEVELS,mLevels);
+		mLevels = llclamp(mLevels,MIN_DECOMPOSITION_LEVELS,MIN_DECOMPOSITION_LEVELS);		
 	}
 	return TRUE;
 }
-- 
cgit v1.2.3