/** * Returns the next code-block in the current tile for the specified component, as a copy (see * below). The order in which code-blocks are returned is not specified. However each code-block * is returned only once and all code-blocks will be returned if the method is called 'N' times, * where 'N' is the number of code-blocks in the tile. After all the code-blocks have been * returned for the current tile calls to this method will return 'null'. * * <p>When changing the current tile (through 'setTile()' or 'nextTile()') this method will always * return the first code-block, as if this method was never called before for the new current * tile. * * <p>The data returned by this method is always a copy of the internal data of this object, 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. The 'magbits' of the * returned data is not set by this method and should be ignored. See the 'CBlkWTData' class. * * <p>The 'ulx' and 'uly' members of the returned 'CBlkWTData' object contain the coordinates of * the top-left corner of the block, with respect to the tile, not the subband. * * @param c The component for which to return the next code-block. * @param 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. * @return The next code-block in the current tile for component 'c', or null if all code-blocks * for the current tile have been returned. * @see CBlkWTData */ public CBlkWTData getNextCodeBlock(int c, CBlkWTData cblk) { // We can not directly use getNextInternCodeBlock() since that returns // a reference to the internal buffer, we have to copy that data int j, k; int w; Object dst_data; // a int[] or float[] object int[] dst_data_int; float[] dst_data_float; Object src_data; // a int[] or float[] object intData = (filters.getWTDataType(tIdx, c) == DataBlk.TYPE_INT); dst_data = null; // Cache the data array, if any if (cblk != null) { dst_data = cblk.getData(); } // Get the next code-block cblk = getNextInternCodeBlock(c, cblk); if (cblk == null) { return null; // No more code-blocks in current tile for component // c } // Ensure size of output buffer if (intData) { // int data dst_data_int = (int[]) dst_data; if (dst_data_int == null || dst_data_int.length < cblk.w * cblk.h) { dst_data = new int[cblk.w * cblk.h]; } } else { // float data dst_data_float = (float[]) dst_data; if (dst_data_float == null || dst_data_float.length < cblk.w * cblk.h) { dst_data = new float[cblk.w * cblk.h]; } } // Copy data line by line src_data = cblk.getData(); w = cblk.w; for (j = w * (cblk.h - 1), k = cblk.offset + (cblk.h - 1) * cblk.scanw; j >= 0; j -= w, k -= cblk.scanw) { System.arraycopy(src_data, k, dst_data, j, w); } cblk.setData(dst_data); cblk.offset = 0; cblk.scanw = w; return cblk; }
/** * Returns the next code-block in the current tile for the specified component. The order in which * code-blocks are returned is not specified. However each code-block is returned only once and * all code-blocks will be returned if the method is called 'N' times, where 'N' is the number of * code-blocks in the tile. After all the code-blocks have been returned for the current tile * calls to this method will return 'null'. * * <p>When changing the current tile (through 'setTile()' or 'nextTile()') this method will always * return the first code-block, as if this method was never called before for the new current * tile. * * <p>The data returned by this method can be the data in the internal buffer of this object, if * any, and thus can not be modified by the caller. The 'offset' and 'scanw' of the returned data * can be arbitrary. See the 'CBlkWTData' class. * * <p>The 'ulx' and 'uly' members of the returned 'CBlkWTData' object contain the coordinates of * the top-left corner of the block, with respect to the tile, not the subband. * * @param c The component for which to return the next code-block. * @param 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. * @return The next code-block in the current tile for component 'n', or null if all code-blocks * for the current tile have been returned. * @see CBlkWTData */ public final CBlkWTData getNextInternCodeBlock(int c, CBlkWTData cblk) { // NOTE: this method is declared final since getNextCodeBlock() relies // on this particular implementation int k, j; int tmp, shiftBits, jmin; int w, h; int outarr[]; float infarr[] = null; CBlkWTDataFloat infblk; float invstep; // The inverse of the quantization step size boolean intq; // flag for quantizig ints SubbandAn sb; float stepUDR; // The quantization step size (for a dynamic // range of 1, or unit) int g = ((Integer) gbs.getTileCompVal(tIdx, c)).intValue(); // Are we quantizing ints or floats? intq = (src.getDataType(tIdx, c) == DataBlk.TYPE_INT); // Check that we have an output object if (cblk == null) { cblk = new CBlkWTDataInt(); } // Cache input float code-block infblk = this.infblk; // Get data to quantize. When quantizing int data 'cblk' is used to // get the data to quantize and to return the quantized data as well, // that's why 'getNextCodeBlock()' is used. This can not be done when // quantizing float data because of the different data types, that's // why 'getNextInternCodeBlock()' is used in that case. if (intq) { // Source data is int cblk = src.getNextCodeBlock(c, cblk); if (cblk == null) { return null; // No more code-blocks in current tile for comp. } // Input and output arrays are the same (for "in place" quant.) outarr = (int[]) cblk.getData(); } else { // Source data is float // Can not use 'cblk' to get float data, use 'infblk' infblk = (CBlkWTDataFloat) src.getNextInternCodeBlock(c, infblk); if (infblk == null) { // Release buffer from infblk: this enables to garbage collect // the big buffer when we are done with last code-block of // component. this.infblk.setData(null); return null; // No more code-blocks in current tile for comp. } this.infblk = infblk; // Save local cache infarr = (float[]) infblk.getData(); // Get output data array and check that there is memory to put the // quantized coeffs in outarr = (int[]) cblk.getData(); if (outarr == null || outarr.length < infblk.w * infblk.h) { outarr = new int[infblk.w * infblk.h]; cblk.setData(outarr); } cblk.m = infblk.m; cblk.n = infblk.n; cblk.sb = infblk.sb; cblk.ulx = infblk.ulx; cblk.uly = infblk.uly; cblk.w = infblk.w; cblk.h = infblk.h; cblk.wmseScaling = infblk.wmseScaling; cblk.offset = 0; cblk.scanw = cblk.w; } // Cache width, height and subband of code-block w = cblk.w; h = cblk.h; sb = cblk.sb; if (isReversible(tIdx, c)) { // Reversible only for int data cblk.magbits = g - 1 + src.getNomRangeBits(c) + sb.anGainExp; shiftBits = 31 - cblk.magbits; // Update the convertFactor field cblk.convertFactor = (1 << shiftBits); // Since we used getNextCodeBlock() to get the int data then // 'offset' is 0 and 'scanw' is the width of the code-block The // input and output arrays are the same (i.e. "in place") for (j = w * h - 1; j >= 0; j--) { tmp = (outarr[j] << shiftBits); outarr[j] = ((tmp < 0) ? (1 << 31) | (-tmp) : tmp); } } else { // Non-reversible, use step size float baseStep = ((Float) qsss.getTileCompVal(tIdx, c)).floatValue(); // Calculate magnitude bits and quantization step size if (isDerived(tIdx, c)) { cblk.magbits = g - 1 + sb.level - (int) Math.floor(Math.log(baseStep) / log2); stepUDR = baseStep / (1 << sb.level); } else { cblk.magbits = g - 1 - (int) Math.floor(Math.log(baseStep / (sb.l2Norm * (1 << sb.anGainExp))) / log2); stepUDR = baseStep / (sb.l2Norm * (1 << sb.anGainExp)); } shiftBits = 31 - cblk.magbits; // Calculate step that decoder will get and use that one. stepUDR = convertFromExpMantissa(convertToExpMantissa(stepUDR)); invstep = 1.0f / ((1L << (src.getNomRangeBits(c) + sb.anGainExp)) * stepUDR); // Normalize to magnitude bits (output fractional point) invstep *= (1 << (shiftBits - src.getFixedPoint(c))); // Update convertFactor and stepSize fields cblk.convertFactor = invstep; cblk.stepSize = ((1L << (src.getNomRangeBits(c) + sb.anGainExp)) * stepUDR); if (intq) { // Quantizing int data // Since we used getNextCodeBlock() to get the int data then // 'offset' is 0 and 'scanw' is the width of the code-block // The input and output arrays are the same (i.e. "in place") for (j = w * h - 1; j >= 0; j--) { tmp = (int) (outarr[j] * invstep); outarr[j] = ((tmp < 0) ? (1 << 31) | (-tmp) : tmp); } } else { // Quantizing float data for (j = w * h - 1, k = infblk.offset + (h - 1) * infblk.scanw + w - 1, jmin = w * (h - 1); j >= 0; jmin -= w) { for (; j >= jmin; k--, j--) { tmp = (int) (infarr[k] * invstep); outarr[j] = ((tmp < 0) ? (1 << 31) | (-tmp) : tmp); } // Jump to beggining of previous line in input k -= infblk.scanw - w; } } } // Return the quantized code-block return cblk; }