/// <summary> Returns the specified coded code-block, for the specified component, in /// the current tile. The first layer to return is indicated by 'fl'. The /// number of layers that is returned depends on 'nl' and the amount of /// available data. /// /// <p>The argument 'fl' is to be used by subsequent calls to this method /// for the same code-block. In this way supplemental data can be retrieved /// at a later time. The fact that data from more than one layer can be /// returned means that several packets from the same code-block, of the /// same component, and the same tile, have been concatenated.</p> /// /// <p>The returned compressed code-block can have its progressive /// attribute set. If this attribute is set it means that more data can be /// obtained by subsequent calls to this method (subject to transmission /// delays, etc). If the progressive attribute is not set it means that the /// returned data is all the data that can be obtained for the specified /// code-block.</p> /// /// <p>The compressed code-block is uniquely specified by the current tile, /// the component (identified by 'c'), the subband (indentified by 'sb') /// and the code-block vertical and horizontal indexes 'n' and 'm'.</p> /// /// <p>The 'ulx' and 'uly' members of the returned 'DecLyrdCBlk' object /// contain the coordinates of the top-left corner of the block, with /// respect to the tile, not the subband.</p> /// /// </summary> /// <param name="c">The index of the component, from 0 to N-1. /// /// </param> /// <param name="m">The vertical index of the code-block to return, in the /// specified subband. /// /// </param> /// <param name="n">The horizontal index of the code-block to return, in the /// specified subband. /// /// </param> /// <param name="sb">The subband in whic the requested code-block is. /// /// </param> /// <param name="fl">The first layer to return. /// /// </param> /// <param name="nl">The number of layers to return, if negative all available /// layers are returned, starting at 'fl'. /// /// </param> /// <param name="ccb">If not null this object is used to return the compressed /// code-block. If null a new object is created and returned. If the data /// array in ccb is not null then it can be reused to return the compressed /// data. /// /// </param> /// <returns> The compressed code-block, with a certain number of layers /// determined by the available data and 'nl'. /// /// </returns> public override DecLyrdCBlk getCodeBlock(int c, int m, int n, SubbandSyn sb, int fl, int nl, DecLyrdCBlk ccb) { int t = TileIdx; CBlkInfo rcb; // requested code-block int r = sb.resLvl; // Resolution level int s = sb.sbandIdx; // Subband index int tpidx; int passtype; // Number of layers int numLayers = ((System.Int32) decSpec.nls.getTileDef(t)); int options = ((System.Int32) decSpec.ecopts.getTileCompVal(t, c)); if (nl < 0) { nl = numLayers - fl + 1; } // If the l quit condition is used, Make sure that no layer // after lquit is returned if (lQuit != - 1 && fl + nl > lQuit) { nl = lQuit - fl; } // Check validity of resquested resolution level (according to the // "-res" option). 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."); } // Check validity of all the arguments try { rcb = cbI[c][r][s][m][n]; if (fl < 1 || fl > numLayers || fl + nl - 1 > numLayers) { throw new System.ArgumentException(); } } catch (System.IndexOutOfRangeException) { throw new System.ArgumentException("Code-block (t:" + t + ", c:" + c + ", r:" + r + ", s:" + s + ", " + m + "x" + (+ n) + ") not found in codestream"); } catch (System.NullReferenceException) { throw new System.ArgumentException("Code-block (t:" + t + ", c:" + c + ", r:" + r + ", s:" + s + ", " + m + "x" + n + ") not found in bit stream"); } // Create DecLyrdCBlk object if necessary if (ccb == null) { ccb = new DecLyrdCBlk(); } ccb.m = m; ccb.n = n; ccb.nl = 0; ccb.dl = 0; ccb.nTrunc = 0; if (rcb == null) { // This code-block was skipped when reading. Returns no data ccb.skipMSBP = 0; ccb.prog = false; ccb.w = ccb.h = ccb.ulx = ccb.uly = 0; return ccb; } // ccb initialization ccb.skipMSBP = rcb.msbSkipped; ccb.ulx = rcb.ulx; ccb.uly = rcb.uly; ccb.w = rcb.w; ccb.h = rcb.h; ccb.ftpIdx = 0; // Search for index of first truncation point (first layer where // length of data is not zero) int l = 0; while ((l < rcb.len.Length) && (rcb.len[l] == 0)) { ccb.ftpIdx += rcb.ntp[l]; l++; } // Calculate total length, number of included layer and number of // truncation points for (l = fl - 1; l < fl + nl - 1; l++) { ccb.nl++; ccb.dl += rcb.len[l]; ccb.nTrunc += rcb.ntp[l]; } // Calculate number of terminated segments int nts; if ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0) { // Regular termination in use One segment per pass // (i.e. truncation point) nts = ccb.nTrunc - ccb.ftpIdx; } else if ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS) != 0) { // Selective arithmetic coding bypass mode in use, but no regular // termination: 1 segment upto the end of the last pass of the 4th // most significant bit-plane, and, in each following bit-plane, // one segment upto the end of the 2nd pass and one upto the end // of the 3rd pass. if (ccb.nTrunc <= CSJ2K.j2k.entropy.StdEntropyCoderOptions.FIRST_BYPASS_PASS_IDX) { nts = 1; } else { nts = 1; // Adds one for each terminated pass for (tpidx = ccb.ftpIdx; tpidx < ccb.nTrunc; tpidx++) { if (tpidx >= CSJ2K.j2k.entropy.StdEntropyCoderOptions.FIRST_BYPASS_PASS_IDX - 1) { passtype = (tpidx + CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_EMPTY_PASSES_IN_MS_BP) % CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_PASSES; if (passtype == 1 || passtype == 2) { // lazy pass just before MQ pass or MQ pass just // before lazy pass => terminated nts++; } } } } } else { // Nothing special in use, just one terminated segment nts = 1; } // ccb.data creation if (ccb.data == null || ccb.data.Length < ccb.dl) { ccb.data = new byte[ccb.dl]; } // ccb.tsLengths creation if (nts > 1 && (ccb.tsLengths == null || ccb.tsLengths.Length < nts)) { ccb.tsLengths = new int[nts]; } else if (nts > 1 && (options & (CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS | CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS)) == CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS) { ArrayUtil.intArraySet(ccb.tsLengths, 0); } // Fill ccb with compressed data int dataIdx = - 1; tpidx = ccb.ftpIdx; int ctp = ccb.ftpIdx; // Cumulative number of truncation // point for the current layer layer int tsidx = 0; int j; for (l = fl - 1; l < fl + nl - 1; l++) { ctp += rcb.ntp[l]; // No data in this layer if (rcb.len[l] == 0) continue; // Read data // NOTE: we should never get an EOFException here since all // data is checked to be within the file. try { in_Renamed.seek(rcb.off[l]); in_Renamed.readFully(ccb.data, dataIdx + 1, rcb.len[l]); dataIdx += rcb.len[l]; } catch (System.IO.IOException e) { JJ2KExceptionHandler.handleException(e); } // Get the terminated segment lengths, if any if (nts == 1) continue; if ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0) { // Regular termination => each pass is terminated for (j = 0; tpidx < ctp; j++, tpidx++) { if (rcb.segLen[l] != null) { ccb.tsLengths[tsidx++] = rcb.segLen[l][j]; } else { // Only one terminated segment in packet ccb.tsLengths[tsidx++] = rcb.len[l]; } } } else { // Lazy coding without regular termination for (j = 0; tpidx < ctp; tpidx++) { if (tpidx >= CSJ2K.j2k.entropy.StdEntropyCoderOptions.FIRST_BYPASS_PASS_IDX - 1) { passtype = (tpidx + CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_EMPTY_PASSES_IN_MS_BP) % CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_PASSES; if (passtype != 0) { // lazy pass just before MQ pass or MQ // pass just before lazy pass => // terminated if (rcb.segLen[l] != null) { ccb.tsLengths[tsidx++] += rcb.segLen[l][j++]; rcb.len[l] -= rcb.segLen[l][j - 1]; } else { // Only one terminated segment in packet ccb.tsLengths[tsidx++] += rcb.len[l]; rcb.len[l] = 0; } } } } // Last length in packet always in (either terminated segment // or contribution to terminated segment) if (rcb.segLen[l] != null && j < rcb.segLen[l].Length) { ccb.tsLengths[tsidx] += rcb.segLen[l][j]; rcb.len[l] -= rcb.segLen[l][j]; } else { // Only one terminated segment in packet if (tsidx < nts) { ccb.tsLengths[tsidx] += rcb.len[l]; rcb.len[l] = 0; } } } } if (nts == 1 && ccb.tsLengths != null) { ccb.tsLengths[0] = ccb.dl; } // Set the progressive flag int lastlayer = fl + nl - 1; if (lastlayer < numLayers - 1) { for (l = lastlayer + 1; l < numLayers; l++) { // It remains data for this code-block in the bit stream if (rcb.len[l] != 0) { ccb.prog = true; } } } return ccb; }
/// <summary> Returns the specified code-block in the current tile for the specified /// component, as a copy (see below). /// /// <P>The returned code-block may be progressive, which is indicated by /// the 'progressive' variable of the returned 'DataBlk' object. If a /// code-block is progressive it means that in a later request to this /// method for the same code-block it is possible to retrieve data which is /// a better approximation, since meanwhile more data to decode for the /// code-block could have been received. If the code-block is not /// progressive then later calls to this method for the same code-block /// will return the exact same data values. /// /// <P>The data returned by this method is always a copy of the internal /// data of this object, if any, and it can be modified "in place" without /// any problems after being returned. The 'offset' of the returned data is /// 0, and the 'scanw' is the same as the code-block width. See the /// 'DataBlk' class. /// /// <P>The 'ulx' and 'uly' members of the returned 'DataBlk' object /// contain the coordinates of the top-left corner of the block, with /// respect to the tile, not the subband. /// /// </summary> /// <param name="c">The component for which to return the next code-block. /// /// </param> /// <param name="m">The vertical index of the code-block to return, in the /// specified subband. /// /// </param> /// <param name="n">The horizontal index of the code-block to return, in the /// specified subband. /// /// </param> /// <param name="sb">The subband in which the code-block to return is. /// /// </param> /// <param name="cblk">If non-null this object will be used to return the new /// code-block. If null a new one will be allocated and returned. If the /// "data" array of the object is non-null it will be reused, if possible, /// to return the data. /// /// </param> /// <returns> The next code-block in the current tile for component 'n', or /// null if all code-blocks for the current tile have been returned. /// /// </returns> /// <seealso cref="DataBlk"> /// /// </seealso> public override DataBlk getCodeBlock(int c, int m, int n, SubbandSyn sb, DataBlk cblk) { //long stime = 0L; // Start time for timed sections int[] zc_lut; // The ZC lookup table to use int[] out_data; // The outupt data buffer int npasses; // The number of coding passes to perform int curbp; // The current magnitude bit-plane (starts at 30) bool error; // Error indicator int tslen; // Length of first terminated segment int tsidx; // Index of current terminated segment ByteInputBuffer in_Renamed = null; bool isterm; // Get the code-block to decode srcblk = src.getCodeBlock(c, m, n, sb, 1, - 1, srcblk); #if DO_TIMING stime = (System.DateTime.Now.Ticks - 621355968000000000) / 10000; #endif // Retrieve options from decSpec options = ((System.Int32) decSpec.ecopts.getTileCompVal(tIdx, c)); // Reset state ArrayUtil.intArraySet(state, 0); // Initialize output code-block if (cblk == null) cblk = new DataBlkInt(); cblk.progressive = srcblk.prog; cblk.ulx = srcblk.ulx; cblk.uly = srcblk.uly; cblk.w = srcblk.w; cblk.h = srcblk.h; cblk.offset = 0; cblk.scanw = cblk.w; out_data = (int[]) cblk.Data; if (out_data == null || out_data.Length < srcblk.w * srcblk.h) { out_data = new int[srcblk.w * srcblk.h]; cblk.Data = out_data; } else { // Set data values to 0 ArrayUtil.intArraySet(out_data, 0); } if (srcblk.nl <= 0 || srcblk.nTrunc <= 0) { // 0 layers => no data to decode => return all 0s return cblk; } // Get the length of the first terminated segment tslen = (srcblk.tsLengths == null)?srcblk.dl:srcblk.tsLengths[0]; tsidx = 0; // Initialize for decoding npasses = srcblk.nTrunc; if (mq == null) { in_Renamed = new ByteInputBuffer(srcblk.data, 0, tslen); mq = new MQDecoder(in_Renamed, NUM_CTXTS, MQ_INIT); } else { // We always start by an MQ segment mq.nextSegment(srcblk.data, 0, tslen); mq.resetCtxts(); } error = false; if ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS) != 0) { if (bin == null) { if (in_Renamed == null) in_Renamed = mq.ByteInputBuffer; bin = new ByteToBitInput(in_Renamed); } } // Choose correct ZC lookup table for global orientation switch (sb.orientation) { case Subband.WT_ORIENT_HL: zc_lut = ZC_LUT_HL; break; case Subband.WT_ORIENT_LH: case Subband.WT_ORIENT_LL: zc_lut = ZC_LUT_LH; break; case Subband.WT_ORIENT_HH: zc_lut = ZC_LUT_HH; break; default: throw new System.ApplicationException("JJ2000 internal error"); } // NOTE: we don't currently detect which is the last magnitude // bit-plane so that 'isterm' is true for the last pass of it. Doing // so would aid marginally in error detection with the predictable // error resilient MQ termination. However, determining which is the // last magnitude bit-plane is quite hard (due to ROI, quantization, // etc.) and in any case the predictable error resilient termination // used without the arithmetic coding bypass and/or regular // termination modes is almost useless. // Loop on bit-planes and passes curbp = 30 - srcblk.skipMSBP; // Check for maximum number of bitplanes quit condition if (mQuit != - 1 && (mQuit * 3 - 2) < npasses) { npasses = mQuit * 3 - 2; } // First bit-plane has only the cleanup pass if (curbp >= 0 && npasses > 0) { isterm = (options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0 || ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS) != 0 && (31 - CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_NON_BYPASS_MS_BP - srcblk.skipMSBP) >= curbp); error = cleanuppass(cblk, mq, curbp, state, zc_lut, isterm); npasses--; if (!error || !doer) curbp--; } // Other bit-planes have the three coding passes if (!error || !doer) { while (curbp >= 0 && npasses > 0) { if ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS) != 0 && (curbp < 31 - CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_NON_BYPASS_MS_BP - srcblk.skipMSBP)) { // Use bypass decoding mode (only all bit-planes // after the first 4 bit-planes). // Here starts a new raw segment bin.setByteArray(null, - 1, srcblk.tsLengths[++tsidx]); isterm = (options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0; error = rawSigProgPass(cblk, bin, curbp, state, isterm); npasses--; if (npasses <= 0 || (error && doer)) break; if ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0) { // Start a new raw segment bin.setByteArray(null, - 1, srcblk.tsLengths[++tsidx]); } isterm = (options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0 || ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS) != 0 && (31 - CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_NON_BYPASS_MS_BP - srcblk.skipMSBP > curbp)); error = rawMagRefPass(cblk, bin, curbp, state, isterm); } else { // Do not use bypass decoding mode if ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0) { // Here starts a new MQ segment mq.nextSegment(null, - 1, srcblk.tsLengths[++tsidx]); } isterm = (options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0; error = sigProgPass(cblk, mq, curbp, state, zc_lut, isterm); npasses--; if (npasses <= 0 || (error && doer)) break; if ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0) { // Here starts a new MQ segment mq.nextSegment(null, - 1, srcblk.tsLengths[++tsidx]); } isterm = (options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0 || ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS) != 0 && (31 - CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_NON_BYPASS_MS_BP - srcblk.skipMSBP > curbp)); error = magRefPass(cblk, mq, curbp, state, isterm); } npasses--; if (npasses <= 0 || (error && doer)) break; if ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0 || ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS) != 0 && (curbp < 31 - CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_NON_BYPASS_MS_BP - srcblk.skipMSBP))) { // Here starts a new MQ segment mq.nextSegment(null, - 1, srcblk.tsLengths[++tsidx]); } isterm = (options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS) != 0 || ((options & CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS) != 0 && (31 - CSJ2K.j2k.entropy.StdEntropyCoderOptions.NUM_NON_BYPASS_MS_BP - srcblk.skipMSBP) >= curbp); error = cleanuppass(cblk, mq, curbp, state, zc_lut, isterm); npasses--; if (error && doer) break; // Goto next bit-plane curbp--; } } // If an error ocurred conceal it if (error && doer) { if (verber) { FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "Error detected at bit-plane " + curbp + " in code-block (" + m + "," + n + "), sb_idx " + sb.sbandIdx + ", res. level " + sb.resLvl + ". Concealing..."); } conceal(cblk, curbp); } #if DO_TIMING time[c] += (System.DateTime.Now.Ticks - 621355968000000000) / 10000 - stime; #endif // Return decoded block return cblk; }