From 45e7dd675f8dc820d51fe87468cdeed0ebc4f391 Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Thu, 5 Aug 2010 20:49:03 +0000 Subject: [PATCH] CSJ2K: * Fixed decoding to System.Drawing.Image for single component textures * Improved exception throwing * Removed unused ProgressWatch class * Sanity check for seeking to a negative offset, gracefully handle * Small speed improvement to the inverse wavelet transform by using Buffer.BlockCopy() and avoiding extra typecasts git-svn-id: http://libopenmetaverse.googlecode.com/svn/libopenmetaverse/trunk@3412 52acb1d6-8a22-11de-b505-999d5b087335 --- CSJ2K/J2kImage.cs | 2 +- CSJ2K/j2k/JJ2KExceptionHandler.cs | 4 +- .../reader/FileBitstreamReaderAgent.cs | 2 +- .../j2k/entropy/encoder/EBCOTRateAllocator.cs | 27 +++++------ CSJ2K/j2k/util/FacilityManager.cs | 17 +------ CSJ2K/j2k/util/ISRandomAccessIO.cs | 5 ++ CSJ2K/j2k/wavelet/synthesis/InvWTFull.cs | 48 +++++++++---------- 7 files changed, 46 insertions(+), 59 deletions(-) diff --git a/CSJ2K/J2kImage.cs b/CSJ2K/J2kImage.cs index 47ace950..82e7fead 100644 --- a/CSJ2K/J2kImage.cs +++ b/CSJ2K/J2kImage.cs @@ -259,7 +259,7 @@ namespace CSJ2K int[] k = new int[numComps]; for (int i = numComps - 1; i >= 0; i--) k[i] = db[i].offset + width - 1; - int outputBytesPerPixel = Math.Min(bytesPerPixel, 4); + int outputBytesPerPixel = Math.Max(3, Math.Min(4, bytesPerPixel)); byte[] rowvalues = new byte[width * outputBytesPerPixel]; for (int i = width - 1; i >= 0; i--) diff --git a/CSJ2K/j2k/JJ2KExceptionHandler.cs b/CSJ2K/j2k/JJ2KExceptionHandler.cs index a1526cda..328a061b 100644 --- a/CSJ2K/j2k/JJ2KExceptionHandler.cs +++ b/CSJ2K/j2k/JJ2KExceptionHandler.cs @@ -84,11 +84,11 @@ namespace CSJ2K.j2k //e.fillInStackTrace(); SupportClass.WriteStackTrace(e, Console.Error); // Print an explicative message - System.Console.Error.WriteLine("The Thread is being terminated bacause an " + "Exception (shown above)\n" + "has been thrown and no special action was " + "defined for this Thread."); + //System.Console.Error.WriteLine("The Thread is being terminated bacause an " + "Exception (shown above)\n" + "has been thrown and no special action was " + "defined for this Thread."); // Stop the thread (do not use stop, since it's deprecated in // Java 1.2) //UPGRADE_NOTE: Exception 'java.lang.ThreadDeath' was converted to 'System.ApplicationException' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'" - throw new System.ApplicationException(); + throw new System.ApplicationException(e.Message, e); } } } \ No newline at end of file diff --git a/CSJ2K/j2k/codestream/reader/FileBitstreamReaderAgent.cs b/CSJ2K/j2k/codestream/reader/FileBitstreamReaderAgent.cs index e992cad6..7fa04c2d 100644 --- a/CSJ2K/j2k/codestream/reader/FileBitstreamReaderAgent.cs +++ b/CSJ2K/j2k/codestream/reader/FileBitstreamReaderAgent.cs @@ -2366,7 +2366,7 @@ namespace CSJ2K.j2k.codestream.reader catch (System.IO.IOException e) { SupportClass.WriteStackTrace(e, Console.Error); - throw new System.ApplicationException("IO Error when reading tile " + x + " x " + y); + throw new System.ApplicationException("IO Error when reading tile " + x + " x " + y + ": " + e.Message, e); } } diff --git a/CSJ2K/j2k/entropy/encoder/EBCOTRateAllocator.cs b/CSJ2K/j2k/entropy/encoder/EBCOTRateAllocator.cs index e29cd506..857b1a1a 100644 --- a/CSJ2K/j2k/entropy/encoder/EBCOTRateAllocator.cs +++ b/CSJ2K/j2k/entropy/encoder/EBCOTRateAllocator.cs @@ -732,7 +732,6 @@ namespace CSJ2K.j2k.entropy.encoder SubbandAn root, sb; int cblkToEncode = 0; int nEncCblk = 0; - ProgressWatch pw = FacilityManager.ProgressWatch; //Get all coded code-blocks Goto first tile src.setTile(0, 0); @@ -766,10 +765,10 @@ namespace CSJ2K.j2k.entropy.encoder } } } - if (pw != null) - { - pw.initProgressWatch(0, cblkToEncode, "Encoding tile " + t + "..."); - } + //if (pw != null) + //{ + // pw.initProgressWatch(0, cblkToEncode, "Encoding tile " + t + "..."); + //} for (c = 0; c < numComps; c++) { @@ -782,11 +781,11 @@ namespace CSJ2K.j2k.entropy.encoder stime = (System.DateTime.Now.Ticks - 621355968000000000) / 10000; #endif - if (pw != null) - { - nEncCblk++; - pw.updateProgressWatch(nEncCblk, null); - } + //if (pw != null) + //{ + // nEncCblk++; + // pw.updateProgressWatch(nEncCblk, null); + //} subb = ccb.sb; @@ -827,10 +826,10 @@ namespace CSJ2K.j2k.entropy.encoder } } - if (pw != null) - { - pw.terminateProgressWatch(); - } + //if (pw != null) + //{ + // pw.terminateProgressWatch(); + //} //Goto next tile if (t < numTiles - 1) diff --git a/CSJ2K/j2k/util/FacilityManager.cs b/CSJ2K/j2k/util/FacilityManager.cs index 661eeb5f..618c0197 100644 --- a/CSJ2K/j2k/util/FacilityManager.cs +++ b/CSJ2K/j2k/util/FacilityManager.cs @@ -68,22 +68,7 @@ namespace CSJ2K.j2k.util /// /// public class FacilityManager - { - /// Returns the ProgressWatch instance registered with the current - /// thread (the thread that calls this method). If the current - /// thread has no registered ProgressWatch, then the default one is used. - /// - /// - public static ProgressWatch ProgressWatch - { - get - { - ProgressWatch pw = (ProgressWatch) watchProgList[SupportClass.ThreadClass.Current()]; - return (pw == null)?defWatchProg:pw; - } - - } - + { /// The loggers associated to different threads //UPGRADE_NOTE: Final was removed from the declaration of 'loggerList '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" private static readonly System.Collections.Hashtable loggerList = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable()); diff --git a/CSJ2K/j2k/util/ISRandomAccessIO.cs b/CSJ2K/j2k/util/ISRandomAccessIO.cs index 2155aefc..ebc9ebbc 100644 --- a/CSJ2K/j2k/util/ISRandomAccessIO.cs +++ b/CSJ2K/j2k/util/ISRandomAccessIO.cs @@ -321,6 +321,11 @@ namespace CSJ2K.j2k.util { throw new System.IO.EndOfStreamException(); } + + if (off < 0) + { + throw new System.IO.EndOfStreamException("Cannot seek to a negative position"); + } } pos = off; } diff --git a/CSJ2K/j2k/wavelet/synthesis/InvWTFull.cs b/CSJ2K/j2k/wavelet/synthesis/InvWTFull.cs index 0e3ba9c3..b2b72606 100644 --- a/CSJ2K/j2k/wavelet/synthesis/InvWTFull.cs +++ b/CSJ2K/j2k/wavelet/synthesis/InvWTFull.cs @@ -79,11 +79,7 @@ namespace CSJ2K.j2k.wavelet.synthesis /// /// public class InvWTFull:InverseWT - { - - /// Reference to the ProgressWatch instance if any - private ProgressWatch pw = null; - + { /// The total number of code-blocks to decode private int cblkToDecode = 0; @@ -128,7 +124,6 @@ namespace CSJ2K.j2k.wavelet.synthesis int nc = src.NumComps; reconstructedComps = new DataBlk[nc]; ndl = new int[nc]; - pw = FacilityManager.ProgressWatch; } /// Returns the reversibility of the current subband. It computes @@ -298,10 +293,10 @@ namespace CSJ2K.j2k.wavelet.synthesis } //Reconstruct source image waveletTreeReconstruction(reconstructedComps[c], src.getSynSubbandTree(tIdx, c), c); - if (pw != null && c == src.NumComps - 1) - { - pw.terminateProgressWatch(); - } + //if (pw != null && c == src.NumComps - 1) + //{ + // pw.terminateProgressWatch(); + //} } if (blk.DataType != dtype) @@ -560,12 +555,12 @@ namespace CSJ2K.j2k.wavelet.synthesis if (!sb.isNode) { int i, m, n; - System.Object src_data, dst_data; + Array src_dataArray, dst_dataArray; Coord ncblks; - + if (sb.w == 0 || sb.h == 0) { - return ; // If empty subband do nothing + return; // If empty subband do nothing } // Get all code-blocks in subband @@ -577,24 +572,27 @@ namespace CSJ2K.j2k.wavelet.synthesis { subbData = new DataBlkFloat(); } + ncblks = sb.numCb; - dst_data = img.Data; + dst_dataArray = (Array)img.Data; + for (m = 0; m < ncblks.y; m++) { for (n = 0; n < ncblks.x; n++) { subbData = src.getInternCodeBlock(c, m, n, sb, subbData); - src_data = subbData.Data; - if (pw != null) - { - nDecCblk++; - pw.updateProgressWatch(nDecCblk, null); - } + src_dataArray = (Array)subbData.Data; + + //if (pw != null) + //{ + // nDecCblk++; + // pw.updateProgressWatch(nDecCblk, null); + //} // Copy the data line by line for (i = subbData.h - 1; i >= 0; i--) { // CONVERSION PROBLEM - Array.Copy((System.Array)src_data, subbData.offset + i * subbData.scanw, (System.Array)dst_data, (subbData.uly + i) * img.w + subbData.ulx, subbData.w); + Buffer.BlockCopy(src_dataArray, subbData.offset + i * subbData.scanw, dst_dataArray, (subbData.uly + i) * img.w + subbData.ulx, subbData.w); } } } @@ -701,10 +699,10 @@ namespace CSJ2K.j2k.wavelet.synthesis } // Loop on components nDecCblk = 0; - if (pw != null) - { - pw.initProgressWatch(0, cblkToDecode, "Decoding tile " + tIdx + "..."); - } + //if (pw != null) + //{ + // pw.initProgressWatch(0, cblkToDecode, "Decoding tile " + tIdx + "..."); + //} } /// Advances to the next tile, in standard scan-line order (by rows then