/// <summary> Constructs a new ForwCompTransf object that operates on the /// specified source of image data. /// /// </summary> /// <param name="imgSrc">The source from where to get the data to be /// transformed /// /// </param> /// <param name="decSpec">The decoder specifications /// /// </param> /// <param name="utdepth">The bit depth of the un-transformed components /// /// </param> /// <param name="pl">The command line optinons of the decoder /// /// </param> /// <seealso cref="BlkImgDataSrc"> /// /// </seealso> public InvCompTransf(BlkImgDataSrc imgSrc, DecoderSpecs decSpec, int[] utdepth, ParameterList pl) : base(imgSrc) { this.cts = decSpec.cts; this.wfs = decSpec.wfs; src = imgSrc; this.utdepth = utdepth; noCompTransf = !(pl.getBooleanParameter("comp_transf")); }
//private bool[][] reversible; /// <summary> Initializes this object with the given source of wavelet /// coefficients. It initializes the resolution level for full resolutioin /// reconstruction. /// /// </summary> /// <param name="src">from where the wavelet coefficinets should be obtained. /// /// </param> /// <param name="decSpec">The decoder specifications /// /// </param> public InvWTFull(CBlkWTDataSrcDec src, DecoderSpecs decSpec) : base(src, decSpec) { this.src = src; int nc = src.NumComps; reconstructedComps = new DataBlk[nc]; ndl = new int[nc]; }
//private bool[][] reversible; /// <summary> Initializes this object with the given source of wavelet /// coefficients. It initializes the resolution level for full resolutioin /// reconstruction. /// /// </summary> /// <param name="src">from where the wavelet coefficinets should be obtained. /// /// </param> /// <param name="decSpec">The decoder specifications /// /// </param> public InvWTFull(CBlkWTDataSrcDec src, DecoderSpecs decSpec) : base(src, decSpec) { this.src = src; int nc = src.NumComps; reconstructedComps = new DataBlk[nc]; ndl = new int[nc]; pw = FacilityManager.ProgressWatch; }
/// <summary> Initializes the source of compressed data. And sets the number of range /// bits and fraction bits and receives the parameters for the dequantizer. /// /// </summary> /// <param name="src">From where to obtain the quantized data. /// /// </param> /// <param name="rb">The number of "range bits" (bitdepth) for each component /// (must be the "range bits" of the un-transformed components). For a /// definition of "range bits" see the getNomRangeBits() method. /// /// </param> /// <param name="qts">The quantizer type spec /// /// </param> /// <param name="qsss">The dequantizer step sizes spec /// /// </param> /// <seealso cref="Dequantizer.getNomRangeBits"> /// /// </seealso> /// <exception cref="IllegalArgumentException">Thrown if 'outdt' is neither /// TYPE_FLOAT nor TYPE_INT, or if 'param' specify reversible quantization /// and 'outdt' is not TYPE_INT or 'fp' has non-zero values, or if 'outdt' /// is TYPE_FLOAT and 'fp' has non-zero values. /// /// </exception> public StdDequantizer(CBlkQuantDataSrcDec src, int[] utrb, DecoderSpecs decSpec) : base(src, utrb, decSpec) { if (utrb.Length != src.NumComps) { throw new System.ArgumentException("Invalid rb argument"); } this.qsss = decSpec.qsss; this.qts = decSpec.qts; this.gbs = decSpec.gbs; }
/// <summary> Initializes the source of compressed data. /// /// </summary> /// <param name="src">From where to obtain the quantized data. /// /// </param> /// <param name="rb">The number of "range bits" for each component (must be the /// "range bits" of the un-transformed components. For a definition of /// "range bits" see the getNomRangeBits() method. /// /// </param> /// <seealso cref="getNomRangeBits"> /// /// </seealso> public Dequantizer(CBlkQuantDataSrcDec src, int[] utrb, DecoderSpecs decSpec) : base(src) { if (utrb.Length != src.NumComps) { throw new System.ArgumentException(); } this.src = src; this.utrb = utrb; this.cts = decSpec.cts; this.wfs = decSpec.wfs; }
/// <summary> Initializes this object with the given source of wavelet /// coefficients. It initializes the resolution level for full resolutioin /// reconstruction (i.e. the maximum resolution available from the 'src' /// source). /// /// <p>It is assumed here that all tiles and components have the same /// reconstruction resolution level. If that was not the case it should be /// the value for the current tile of the source.</p> /// /// </summary> /// <param name="src">from where the wavelet coefficinets should be obtained. /// /// </param> /// <param name="decSpec">The decoder specifications /// /// </param> public InverseWT(MultiResImgData src, DecoderSpecs decSpec) : base(src, decSpec) { }
/// <summary> Creates an InverseWT object that works on the data type of the source, /// with the special additional parameters from the parameter /// list. Currently the parameter list is ignored since no special /// parameters can be specified for the inverse wavelet transform yet. /// /// </summary> /// <param name="src">The source of data for the inverse wavelet /// transform. /// /// </param> /// <param name="pl">The parameter list containing parameters applicable to the /// inverse wavelet transform (other parameters can also be present). /// /// </param> public static InverseWT createInstance(CBlkWTDataSrcDec src, DecoderSpecs decSpec) { // full page wavelet transform return(new InvWTFull(src, decSpec)); }
public static Image FromStream(Stream stream) { RandomAccessIO in_stream = new ISRandomAccessIO(stream); // Initialize default parameters ParameterList defpl = GetDefaultParameterList(decoder_pinfo); // Create parameter list using defaults ParameterList pl = new ParameterList(defpl); // **** File Format **** // If the codestream is wrapped in the jp2 fileformat, Read the // file format wrapper FileFormatReader ff = new FileFormatReader(in_stream); ff.readFileFormat(); if (ff.JP2FFUsed) { in_stream.seek(ff.FirstCodeStreamPos); } // +----------------------------+ // | Instantiate decoding chain | // +----------------------------+ // **** Header decoder **** // Instantiate header decoder and read main header HeaderInfo hi = new HeaderInfo(); HeaderDecoder hd; try { hd = new HeaderDecoder(in_stream, pl, hi); } catch (EndOfStreamException e) { throw new ApplicationException("Codestream too short or bad header, unable to decode.", e); } int nCompCod = hd.NumComps; int nTiles = hi.sizValue.NumTiles; DecoderSpecs decSpec = hd.DecoderSpecs; // Get demixed bitdepths int[] depth = new int[nCompCod]; for (int i = 0; i < nCompCod; i++) { depth[i] = hd.getOriginalBitDepth(i); } // **** Bit stream reader **** BitstreamReaderAgent breader; try { breader = BitstreamReaderAgent. createInstance(in_stream, hd, pl, decSpec, false, hi); } catch (IOException e) { throw new ApplicationException("Error while reading bit stream header or parsing packets.", e); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate bit stream reader.", e); } // **** Entropy decoder **** EntropyDecoder entdec; try { entdec = hd.createEntropyDecoder(breader, pl); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate entropy decoder.", e); } // **** ROI de-scaler **** ROIDeScaler roids; try { roids = hd.createROIDeScaler(entdec, pl, decSpec); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate roi de-scaler.", e); } // **** Dequantizer **** Dequantizer deq; try { deq = hd.createDequantizer(roids, depth, decSpec); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate dequantizer.", e); } // **** Inverse wavelet transform *** InverseWT invWT; try { // full page inverse wavelet transform invWT = InverseWT.createInstance(deq, decSpec); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate inverse wavelet transform.", e); } int res = breader.ImgRes; invWT.ImgResLevel = res; // **** Data converter **** (after inverse transform module) ImgDataConverter converter = new ImgDataConverter(invWT, 0); // **** Inverse component transformation **** InvCompTransf ictransf = new InvCompTransf(converter, decSpec, depth, pl); // **** Color space mapping **** BlkImgDataSrc color; if (ff.JP2FFUsed && pl.getParameter("nocolorspace").Equals("off")) { try { ColorSpace csMap = new ColorSpace(in_stream, hd, pl); BlkImgDataSrc channels = hd.createChannelDefinitionMapper(ictransf, csMap); BlkImgDataSrc resampled = hd.createResampler(channels, csMap); BlkImgDataSrc palettized = hd.createPalettizedColorSpaceMapper(resampled, csMap); color = hd.createColorSpaceMapper(palettized, csMap); } catch (ArgumentException e) { throw new ApplicationException("Could not instantiate ICC profiler.", e); } catch (ColorSpaceException e) { throw new ApplicationException("Error processing ColorSpace information.", e); } } else { // Skip colorspace mapping color = ictransf; } // This is the last image in the decoding chain and should be // assigned by the last transformation: BlkImgDataSrc decodedImage = color; if (color == null) { decodedImage = ictransf; } int numComps = decodedImage.NumComps; int bytesPerPixel = numComps; // Assuming 8-bit components // **** Copy to Bitmap **** PixelFormat pixelFormat; switch (numComps) { case 1: pixelFormat = PixelFormat.Format24bppRgb; break; case 3: pixelFormat = PixelFormat.Format24bppRgb; break; case 4: case 5: pixelFormat = PixelFormat.Format32bppArgb; break; default: throw new ApplicationException("Unsupported PixelFormat. " + numComps + " components."); } Bitmap dst = new Bitmap(decodedImage.ImgWidth, decodedImage.ImgHeight, pixelFormat); Coord numTiles = decodedImage.getNumTiles(null); int tIdx = 0; for (int y = 0; y < numTiles.y; y++) { // Loop on horizontal tiles for (int x = 0; x < numTiles.x; x++, tIdx++) { decodedImage.setTile(x, y); int height = decodedImage.getTileCompHeight(tIdx, 0); int width = decodedImage.getTileCompWidth(tIdx, 0); int tOffx = decodedImage.getCompULX(0) - (int)Math.Ceiling(decodedImage.ImgULX / (double)decodedImage.getCompSubsX(0)); int tOffy = decodedImage.getCompULY(0) - (int)Math.Ceiling(decodedImage.ImgULY / (double)decodedImage.getCompSubsY(0)); DataBlkInt[] db = new DataBlkInt[numComps]; int[] ls = new int[numComps]; int[] mv = new int[numComps]; int[] fb = new int[numComps]; for (int i = 0; i < numComps; i++) { db[i] = new DataBlkInt(); ls[i] = 1 << (decodedImage.getNomRangeBits(0) - 1); mv[i] = (1 << decodedImage.getNomRangeBits(0)) - 1; fb[i] = decodedImage.getFixedPoint(0); } for (int l = 0; l < height; l++) { for (int i = numComps - 1; i >= 0; i--) { db[i].ulx = 0; db[i].uly = l; db[i].w = width; db[i].h = 1; decodedImage.getInternCompData(db[i], i); } int[] k = new int[numComps]; for (int i = numComps - 1; i >= 0; i--) { k[i] = db[i].offset + width - 1; } int outputBytesPerPixel = Math.Max(3, Math.Min(4, bytesPerPixel)); byte[] rowvalues = new byte[width * outputBytesPerPixel]; for (int i = width - 1; i >= 0; i--) { int[] tmp = new int[numComps]; for (int j = numComps - 1; j >= 0; j--) { tmp[j] = (db[j].data_array[k[j]--] >> fb[j]) + ls[j]; tmp[j] = (tmp[j] < 0) ? 0 : ((tmp[j] > mv[j]) ? mv[j] : tmp[j]); if (decodedImage.getNomRangeBits(j) != 8) { tmp[j] = (int)Math.Round(((double)tmp[j] / Math.Pow(2D, (double)decodedImage.getNomRangeBits(j))) * 255D); } } int offset = i * outputBytesPerPixel; switch (numComps) { case 1: rowvalues[offset + 0] = (byte)tmp[0]; rowvalues[offset + 1] = (byte)tmp[0]; rowvalues[offset + 2] = (byte)tmp[0]; break; case 3: rowvalues[offset + 0] = (byte)tmp[2]; rowvalues[offset + 1] = (byte)tmp[1]; rowvalues[offset + 2] = (byte)tmp[0]; break; case 4: case 5: rowvalues[offset + 0] = (byte)tmp[2]; rowvalues[offset + 1] = (byte)tmp[1]; rowvalues[offset + 2] = (byte)tmp[0]; rowvalues[offset + 3] = (byte)tmp[3]; break; } } BitmapData dstdata = dst.LockBits( new System.Drawing.Rectangle(tOffx, tOffy + l, width, 1), ImageLockMode.WriteOnly, pixelFormat); IntPtr ptr = dstdata.Scan0; System.Runtime.InteropServices.Marshal.Copy(rowvalues, 0, ptr, rowvalues.Length); dst.UnlockBits(dstdata); } } } return(dst); }
public static List <int> GetLayerBoundaries(Stream stream) { RandomAccessIO in_stream = new ISRandomAccessIO(stream); // Create parameter list using defaults ParameterList pl = new ParameterList(GetDefaultParameterList(decoder_pinfo)); // **** File Format **** // If the codestream is wrapped in the jp2 fileformat, Read the // file format wrapper FileFormatReader ff = new FileFormatReader(in_stream); ff.readFileFormat(); if (ff.JP2FFUsed) { in_stream.seek(ff.FirstCodeStreamPos); } // +----------------------------+ // | Instantiate decoding chain | // +----------------------------+ // **** Header decoder **** // Instantiate header decoder and read main header HeaderInfo hi = new HeaderInfo(); HeaderDecoder hd; try { hd = new HeaderDecoder(in_stream, pl, hi); } catch (EndOfStreamException e) { throw new ArgumentException("Codestream too short or bad header, unable to decode.", e); } int nCompCod = hd.NumComps; int nTiles = hi.sizValue.NumTiles; DecoderSpecs decSpec = hd.DecoderSpecs; // Get demixed bitdepths int[] depth = new int[nCompCod]; for (int i = 0; i < nCompCod; i++) { depth[i] = hd.getOriginalBitDepth(i); } // **** Bit stream reader **** BitstreamReaderAgent breader; try { breader = BitstreamReaderAgent.createInstance(in_stream, hd, pl, decSpec, false, hi); } catch (IOException e) { throw new ArgumentException("Error while reading bit stream header or parsing packets.", e); } catch (ArgumentException e) { throw new ArgumentException("Cannot instantiate bit stream reader.", e); } breader.setTile(0, 0); return(((FileBitstreamReaderAgent)breader).layerStarts); }
/// <summary> Creates a ROIDeScaler object. The information needed to create the /// object is the Entropy decoder used and the parameters. /// /// </summary> /// <param name="src">The source of data that is to be descaled /// /// </param> /// <param name="pl">The parameter list (or options). /// /// </param> /// <param name="decSpec">The decoding specifications /// /// </param> /// <exception cref="IllegalArgumentException">If an error occurs while parsing /// the options in 'pl' /// /// </exception> public static ROIDeScaler createInstance(CBlkQuantDataSrcDec src, ParameterList pl, DecoderSpecs decSpec) { System.String noRoi; //int i; // Check parameters pl.checkList(OPT_PREFIX, CSJ2K.j2k.util.ParameterList.toNameArray(pinfo)); // Check if no_roi specified in command line or no roi signalled // in bit stream noRoi = pl.getParameter("Rno_roi"); if (noRoi != null || decSpec.rois == null) { // no_roi specified in commandline! return(new ROIDeScaler(src, null)); } return(new ROIDeScaler(src, decSpec.rois)); }
/// <summary> Instantiates the 'InvWTAdapter' object using the specified /// 'MultiResImgData' source. The reconstruction resolution level is set to /// full resolution (i.e. the maximum resolution level). /// /// </summary> /// <param name="src">From where to obtain the values to return /// /// </param> /// <param name="decSpec">The decoder specifications /// /// </param> protected internal InvWTAdapter(MultiResImgData src, DecoderSpecs decSpec) { mressrc = src; this.decSpec = decSpec; maxImgRes = decSpec.dls.Min; }