Update to newer version of CSJ2K from github.com/cureos/csj2k with modification

This commit is contained in:
Cinder Biscuits
2017-05-16 19:41:03 -05:00
parent c719ef284c
commit 6fcb14800b
120 changed files with 9067 additions and 7812 deletions

View File

@@ -41,6 +41,7 @@
* Copyright (c) 1999/2000 JJ2000 Partners.
* */
using System;
using System.Collections.Generic;
using CSJ2K.j2k.quantization.dequantizer;
using CSJ2K.j2k.wavelet.synthesis;
using CSJ2K.j2k.entropy.decoder;
@@ -113,7 +114,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (firstPackOff == null || firstPackOff[t] == null)
{
throw new System.ApplicationException("Tile " + t + " not found in input codestream.");
throw new System.InvalidOperationException("Tile " + t + " not found in input codestream.");
}
return firstPackOff[t].Length;
}
@@ -160,10 +161,7 @@ namespace CSJ2K.j2k.codestream.reader
private int[][] tilePartHeadLen;
/// <summary>Length of each packet head found in the tile </summary>
private System.Collections.ArrayList pktHL;
/// <summary>Layer starting positions</summary>
public System.Collections.Generic.List<int> layerStarts;
private System.Collections.Generic.List<System.Int32> pktHL;
/// <summary>True if truncation mode is used. False if parsing mode </summary>
private bool isTruncMode;
@@ -243,6 +241,7 @@ namespace CSJ2K.j2k.codestream.reader
this.pl = pl;
this.printInfo = cdstrInfo;
this.hi = hi;
System.String strInfo = "Codestream elements information in bytes " + "(offset, total length, header length):\n\n";
// Check whether quit conditiosn used
usePOCQuit = pl.getBooleanParameter("poc_quit");
@@ -258,26 +257,26 @@ namespace CSJ2K.j2k.codestream.reader
trate = System.Single.MaxValue;
}
}
catch (System.FormatException)
catch (System.FormatException e)
{
throw new System.ApplicationException("Invalid value in 'rate' option: " + pl.getParameter("rate"));
throw new System.InvalidOperationException("Invalid value in 'rate' option: " + pl.getParameter("rate"));
}
catch (System.ArgumentException)
catch (System.ArgumentException e)
{
throw new System.ApplicationException("'rate' option is missing");
throw new System.InvalidOperationException("'rate' option is missing");
}
try
{
tnbytes = pl.getIntParameter("nbytes");
}
catch (System.FormatException)
catch (System.FormatException e)
{
throw new System.ApplicationException("Invalid value in 'nbytes' option: " + pl.getParameter("nbytes"));
throw new System.InvalidOperationException("Invalid value in 'nbytes' option: " + pl.getParameter("nbytes"));
}
catch (System.ArgumentException)
catch (System.ArgumentException e)
{
throw new System.ApplicationException("'nbytes' option is missing");
throw new System.InvalidOperationException("'nbytes' option is missing");
}
// Check that '-rate' and '-nbytes' are not used at the same time
@@ -299,7 +298,7 @@ namespace CSJ2K.j2k.codestream.reader
{
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
tnbytes = (int) (trate * hd.MaxCompImgWidth * hd.MaxCompImgHeight) / 8;
if (tnbytes <= 0) tnbytes = int.MaxValue;
if (tnbytes < 0) tnbytes = int.MaxValue;
}
isTruncMode = !pl.getBooleanParameter("parsing");
@@ -309,30 +308,30 @@ namespace CSJ2K.j2k.codestream.reader
{
ncbQuit = pl.getIntParameter("ncb_quit");
}
catch (System.FormatException)
catch (System.FormatException e)
{
throw new System.ApplicationException("Invalid value in 'ncb_quit' option: " + pl.getParameter("ncb_quit"));
throw new System.InvalidOperationException("Invalid value in 'ncb_quit' option: " + pl.getParameter("ncb_quit"));
}
catch (System.ArgumentException)
catch (System.ArgumentException e)
{
throw new System.ApplicationException("'ncb_quit' option is missing");
throw new System.InvalidOperationException("'ncb_quit' option is missing");
}
if (ncbQuit != - 1 && !isTruncMode)
{
throw new System.ApplicationException("Cannot use -parsing and -ncb_quit condition at " + "the same time.");
throw new System.InvalidOperationException("Cannot use -parsing and -ncb_quit condition at " + "the same time.");
}
try
{
lQuit = pl.getIntParameter("l_quit");
}
catch (System.FormatException)
catch (System.FormatException e)
{
throw new System.ApplicationException("Invalid value in 'l_quit' option: " + pl.getParameter("l_quit"));
throw new System.InvalidOperationException("Invalid value in 'l_quit' option: " + pl.getParameter("l_quit"));
}
catch (System.ArgumentException)
catch (System.ArgumentException e)
{
throw new System.ApplicationException("'l_quit' option is missing");
throw new System.InvalidOperationException("'l_quit' option is missing");
}
// initializations
@@ -370,10 +369,12 @@ namespace CSJ2K.j2k.codestream.reader
anbytes = 0;
}
strInfo += ("Main header length : " + cdstreamStart + ", " + mainHeadLen + ", " + mainHeadLen + "\n");
// If cannot even read the first tile-part
if (anbytes > tnbytes)
{
throw new System.ApplicationException("Requested bitrate is too small.");
throw new System.InvalidOperationException("Requested bitrate is too small.");
}
// Read all tile-part headers from all tiles.
@@ -435,6 +436,8 @@ namespace CSJ2K.j2k.codestream.reader
firstPackOff[t][tp] = pos;
tilePartHeadLen[t][tp] = (pos - tilePartStart);
strInfo += ("Tile-part " + tp + " of tile " + t + " : " + tilePartStart + ", " + tilePartLen[t][tp] + ", " + tilePartHeadLen[t][tp] + "\n");
// Update length counters
totTileLen[t] += tilePartLen[t][tp];
totTileHeadLen[t] += tilePartHeadLen[t][tp];
@@ -492,10 +495,11 @@ namespace CSJ2K.j2k.codestream.reader
}
}
}
catch (System.IO.EndOfStreamException)
catch (System.IO.EndOfStreamException e)
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "Codestream truncated in tile " + t);
@@ -528,7 +532,7 @@ namespace CSJ2K.j2k.codestream.reader
throw new System.ArgumentException("Specified negative " + "resolution level " + "index: " + targetRes);
}
}
catch (System.FormatException)
catch (System.FormatException f)
{
throw new System.ArgumentException("Invalid resolution level " + "index ('-res' option) " + pl.getParameter("res"));
}
@@ -567,7 +571,7 @@ namespace CSJ2K.j2k.codestream.reader
throw new System.ArgumentException("Specified negative " + "resolution level index: " + targetRes);
}
}
catch (System.FormatException)
catch (System.FormatException e)
{
throw new System.ArgumentException("Invalid resolution level " + "index ('-res' option) " + pl.getParameter("res"));
}
@@ -583,6 +587,7 @@ namespace CSJ2K.j2k.codestream.reader
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
// Check presence of EOC marker is decoding rate not reached or if
@@ -591,16 +596,12 @@ namespace CSJ2K.j2k.codestream.reader
{
try
{
short eocCheck = 0;
if (in_Renamed.Pos + sizeof(short) <= in_Renamed.length())
eocCheck = in_Renamed.readShort();
if (!rateReached && !isPsotEqualsZero && eocCheck != CSJ2K.j2k.codestream.Markers.EOC)
if (!rateReached && !isPsotEqualsZero && in_Renamed.readShort() != CSJ2K.j2k.codestream.Markers.EOC)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "EOC marker not found. " + "Codestream is corrupted.");
}
}
catch (System.IO.EndOfStreamException)
catch (System.IO.EndOfStreamException e)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "EOC marker is missing");
}
@@ -648,7 +649,7 @@ namespace CSJ2K.j2k.codestream.reader
// error
if (anbytes > stopOff)
{
throw new System.ApplicationException("Requested bitrate is too small for parsing");
throw new System.InvalidOperationException("Requested bitrate is too small for parsing");
}
// Calculate bitrate for each tile
@@ -710,7 +711,7 @@ namespace CSJ2K.j2k.codestream.reader
isPsotEqualsZero = (psot != 0)?false:true;
if (psot < 0)
{
throw new NotImplementedException("Tile length larger " + "than maximum supported");
throw new NotImplementedException("Tile length larger " + "than maximum supported");
}
// TPsot
int tilePart = in_Renamed.read();
@@ -927,6 +928,7 @@ namespace CSJ2K.j2k.codestream.reader
int numLayers = ((System.Int32) decSpec.nls.getTileDef(t));
int nPrec = 1;
int hlen, plen;
System.String strInfo = "Tile " + TileIdx + " (tile-part:" + curTilePart + "): offset, length, header length\n"; ;
bool pph = false;
if (((System.Boolean) decSpec.pphs.getTileDef(t)))
{
@@ -934,9 +936,6 @@ namespace CSJ2K.j2k.codestream.reader
}
for (int l = minlys; l < lye; l++)
{
// store the layer starting position
layerStarts.Add(in_Renamed.Pos);
// loop on layers
for (int r = ress; r < rese; r++)
{
@@ -985,6 +984,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -998,6 +998,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1009,11 +1010,13 @@ namespace CSJ2K.j2k.codestream.reader
// Reads packet's body
status = pktDec.readPktBody(l, r, c, p, cbI[c][r], nBytes);
plen = in_Renamed.Pos - start;
strInfo += (" Pkt l=" + l + ",r=" + r + ",c=" + c + ",p=" + p + ": " + start + ", " + plen + ", " + hlen + "\n");
if (status)
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1024,6 +1027,7 @@ namespace CSJ2K.j2k.codestream.reader
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return false; // Decoding rate was not reached
}
@@ -1079,6 +1083,7 @@ namespace CSJ2K.j2k.codestream.reader
}
}
System.String strInfo = "Tile " + TileIdx + " (tile-part:" + curTilePart + "): offset, length, header length\n"; ;
int numLayers = ((System.Int32) decSpec.nls.getTileDef(t));
bool pph = false;
if (((System.Boolean) decSpec.pphs.getTileDef(t)))
@@ -1139,6 +1144,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1152,6 +1158,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
// Output rate of EOF reached
return true;
@@ -1164,11 +1171,13 @@ namespace CSJ2K.j2k.codestream.reader
// Reads packet's body
status = pktDec.readPktBody(l, r, c, p, cbI[c][r], nBytes);
plen = in_Renamed.Pos - start;
strInfo += (" Pkt l=" + l + ",r=" + r + ",c=" + c + ",p=" + p + ": " + start + ", " + plen + ", " + hlen + "\n");
if (status)
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
// Output rate or EOF reached
return true;
@@ -1180,6 +1189,7 @@ namespace CSJ2K.j2k.codestream.reader
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return false; // Decoding rate was not reached
}
@@ -1298,7 +1308,7 @@ namespace CSJ2K.j2k.codestream.reader
if (nPrec == 0)
{
throw new System.ApplicationException("Image cannot have no precinct");
throw new System.InvalidOperationException("Image cannot have no precinct");
}
int pyend = (maxy - miny) / gcd_y + 1;
@@ -1309,6 +1319,7 @@ namespace CSJ2K.j2k.codestream.reader
bool status = false;
int lastByte = firstPackOff[t][curTilePart] + tilePartLen[t][curTilePart] - 1 - tilePartHeadLen[t][curTilePart];
int numLayers = ((System.Int32) decSpec.nls.getTileDef(t));
System.String strInfo = "Tile " + TileIdx + " (tile-part:" + curTilePart + "): offset, length, header length\n"; ;
bool pph = false;
if (((System.Boolean) decSpec.pphs.getTileDef(t)))
{
@@ -1375,6 +1386,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1388,6 +1400,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1399,11 +1412,13 @@ namespace CSJ2K.j2k.codestream.reader
// Reads packet's body
status = pktDec.readPktBody(l, r, c, nextPrec[c][r], cbI[c][r], nBytes);
plen = in_Renamed.Pos - start;
strInfo += (" Pkt l=" + l + ",r=" + r + ",c=" + c + ",p=" + nextPrec[c][r] + ": " + start + ", " + plen + ", " + hlen + "\n");
if (status)
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1432,6 +1447,7 @@ namespace CSJ2K.j2k.codestream.reader
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return false; // Decoding rate was not reached
}
@@ -1548,7 +1564,7 @@ namespace CSJ2K.j2k.codestream.reader
if (nPrec == 0)
{
throw new System.ApplicationException("Image cannot have no precinct");
throw new System.InvalidOperationException("Image cannot have no precinct");
}
int pyend = (maxy - miny) / gcd_y + 1;
@@ -1558,6 +1574,7 @@ namespace CSJ2K.j2k.codestream.reader
bool status = false;
int lastByte = firstPackOff[t][curTilePart] + tilePartLen[t][curTilePart] - 1 - tilePartHeadLen[t][curTilePart];
int numLayers = ((System.Int32) decSpec.nls.getTileDef(t));
System.String strInfo = "Tile " + TileIdx + " (tile-part:" + curTilePart + "): offset, length, header length\n"; ;
bool pph = false;
if (((System.Boolean) decSpec.pphs.getTileDef(t)))
{
@@ -1625,6 +1642,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1638,6 +1656,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1649,11 +1668,13 @@ namespace CSJ2K.j2k.codestream.reader
// Reads packet's body
status = pktDec.readPktBody(l, r, c, nextPrec[c][r], cbI[c][r], nBytes);
plen = in_Renamed.Pos - start;
strInfo += (" Pkt l=" + l + ",r=" + r + ",c=" + c + ",p=" + nextPrec[c][r] + ": " + start + ", " + plen + ", " + hlen + "\n");
if (status)
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1682,6 +1703,7 @@ namespace CSJ2K.j2k.codestream.reader
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return false; // Decoding rate was not reached
}
@@ -1798,7 +1820,7 @@ namespace CSJ2K.j2k.codestream.reader
if (nPrec == 0)
{
throw new System.ApplicationException("Image cannot have no precinct");
throw new System.InvalidOperationException("Image cannot have no precinct");
}
int pyend = (maxy - miny) / gcd_y + 1;
@@ -1808,6 +1830,7 @@ namespace CSJ2K.j2k.codestream.reader
bool status = false;
int lastByte = firstPackOff[t][curTilePart] + tilePartLen[t][curTilePart] - 1 - tilePartHeadLen[t][curTilePart];
int numLayers = ((System.Int32) decSpec.nls.getTileDef(t));
System.String strInfo = "Tile " + TileIdx + " (tile-part:" + curTilePart + "): offset, length, header length\n"; ;
bool pph = false;
if (((System.Boolean) decSpec.pphs.getTileDef(t)))
{
@@ -1877,6 +1900,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1890,6 +1914,7 @@ namespace CSJ2K.j2k.codestream.reader
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1901,11 +1926,13 @@ namespace CSJ2K.j2k.codestream.reader
// Reads packet's body
status = pktDec.readPktBody(l, r, c, nextPrec[c][r], cbI[c][r], nBytes);
plen = in_Renamed.Pos - start;
strInfo += (" Pkt l=" + l + ",r=" + r + ",c=" + c + ",p=" + nextPrec[c][r] + ": " + start + ", " + plen + ", " + hlen + "\n");
if (status)
{
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return true;
}
@@ -1934,6 +1961,7 @@ namespace CSJ2K.j2k.codestream.reader
if (printInfo)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
}
return false; // Decoding rate was not reached
}
@@ -1956,8 +1984,7 @@ namespace CSJ2K.j2k.codestream.reader
/// </seealso>
private void readTilePkts(int t)
{
pktHL = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
layerStarts = new System.Collections.Generic.List<int>(5);
pktHL = new List<int>(10);
// Number of layers
int nl = ((System.Int32) decSpec.nls.getTileDef(t));
@@ -2030,7 +2057,7 @@ namespace CSJ2K.j2k.codestream.reader
}
in_Renamed.seek(firstPackOff[t][0]);
}
catch (System.IO.EndOfStreamException)
catch (System.IO.EndOfStreamException e)
{
FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "Codestream truncated in tile " + t);
return ;
@@ -2365,8 +2392,8 @@ 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);
SupportClass.WriteStackTrace(e);
throw new System.InvalidOperationException("IO Error when reading tile " + x + " x " + y);
}
}
@@ -2482,7 +2509,7 @@ namespace CSJ2K.j2k.codestream.reader
int maxdl = getSynSubbandTree(t, c).resLvl;
if (r > targetRes + maxdl - decSpec.dls.Min)
{
throw new System.ApplicationException("JJ2000 error: requesting a code-block " + "disallowed by the '-res' option.");
throw new System.InvalidOperationException("JJ2000 error: requesting a code-block " + "disallowed by the '-res' option.");
}
// Check validity of all the arguments
@@ -2495,11 +2522,11 @@ namespace CSJ2K.j2k.codestream.reader
throw new System.ArgumentException();
}
}
catch (System.IndexOutOfRangeException)
catch (System.IndexOutOfRangeException e)
{
throw new System.ArgumentException("Code-block (t:" + t + ", c:" + c + ", r:" + r + ", s:" + s + ", " + m + "x" + (+ n) + ") not found in codestream");
}
catch (System.NullReferenceException)
catch (System.NullReferenceException e)
{
throw new System.ArgumentException("Code-block (t:" + t + ", c:" + c + ", r:" + r + ", s:" + s + ", " + m + "x" + n + ") not found in bit stream");
}
@@ -2727,4 +2754,4 @@ namespace CSJ2K.j2k.codestream.reader
return ccb;
}
}
}
}