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 == 4 ? 4 : 3); // **** Copy to Bitmap **** PixelFormat pixelFormat; switch (numComps) { case 1: pixelFormat = PixelFormat.Format24bppRgb; break; case 3: pixelFormat = PixelFormat.Format24bppRgb; break; case 4: 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; byte[] rowvalues = new byte[width * bytesPerPixel]; 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 * bytesPerPixel; 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: rowvalues[offset + 0] = (byte)tmp[3]; rowvalues[offset + 1] = (byte)tmp[2]; rowvalues[offset + 2] = (byte)tmp[1]; rowvalues[offset + 3] = (byte)tmp[0]; break; } } BitmapData dstdata = dst.LockBits( new System.Drawing.Rectangle(tOffx, tOffy + l, width, 1), ImageLockMode.ReadWrite, pixelFormat); IntPtr ptr = dstdata.Scan0; System.Runtime.InteropServices.Marshal.Copy(rowvalues, 0, ptr, rowvalues.Length); dst.UnlockBits(dstdata); } } } return dst; }
/// <summary> Reads all tiles headers and keep offset of their first /// packet. Finally it calls the rate allocation method. /// /// </summary> /// <param name="hd">HeaderDecoder of the codestream. /// /// </param> /// <param name="ehs">The input stream where to read bit-stream. /// /// </param> /// <param name="decSpec">The decoder specifications /// /// </param> /// <param name="pl">The ParameterList instance created from the /// command-line arguments. /// /// </param> /// <param name="cdstrInfo">Whether or not to print information found in /// codestream. /// /// </param> /// <seealso cref="allocateRate"> /// /// </seealso> public FileBitstreamReaderAgent(HeaderDecoder hd, RandomAccessIO ehs, DecoderSpecs decSpec, ParameterList pl, bool cdstrInfo, HeaderInfo hi):base(hd, decSpec) { this.pl = pl; this.printInfo = cdstrInfo; this.hi = hi; // Check whether quit conditiosn used usePOCQuit = pl.getBooleanParameter("poc_quit"); // Get decoding rate bool rateInBytes; bool parsing = pl.getBooleanParameter("parsing"); try { trate = pl.getFloatParameter("rate"); if (trate == - 1) { trate = System.Single.MaxValue; } } catch (System.FormatException) { throw new System.ApplicationException("Invalid value in 'rate' option: " + pl.getParameter("rate")); } catch (System.ArgumentException) { throw new System.ApplicationException("'rate' option is missing"); } try { tnbytes = pl.getIntParameter("nbytes"); } catch (System.FormatException) { throw new System.ApplicationException("Invalid value in 'nbytes' option: " + pl.getParameter("nbytes")); } catch (System.ArgumentException) { throw new System.ApplicationException("'nbytes' option is missing"); } // Check that '-rate' and '-nbytes' are not used at the same time ParameterList defaults = pl.DefaultParameterList; if (tnbytes != defaults.getFloatParameter("nbytes")) { rateInBytes = true; } else { rateInBytes = false; } if (rateInBytes) { trate = tnbytes * 8f / hd.MaxCompImgWidth / hd.MaxCompImgHeight; } else { //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; } isTruncMode = !pl.getBooleanParameter("parsing"); // Check if quit conditions are being used int ncbQuit; try { ncbQuit = pl.getIntParameter("ncb_quit"); } catch (System.FormatException) { throw new System.ApplicationException("Invalid value in 'ncb_quit' option: " + pl.getParameter("ncb_quit")); } catch (System.ArgumentException) { throw new System.ApplicationException("'ncb_quit' option is missing"); } if (ncbQuit != - 1 && !isTruncMode) { throw new System.ApplicationException("Cannot use -parsing and -ncb_quit condition at " + "the same time."); } try { lQuit = pl.getIntParameter("l_quit"); } catch (System.FormatException) { throw new System.ApplicationException("Invalid value in 'l_quit' option: " + pl.getParameter("l_quit")); } catch (System.ArgumentException) { throw new System.ApplicationException("'l_quit' option is missing"); } // initializations in_Renamed = ehs; pktDec = new PktDecoder(decSpec, hd, ehs, this, isTruncMode, ncbQuit); tileParts = new int[nt]; totTileLen = new int[nt]; tilePartLen = new int[nt][]; tilePartNum = new int[nt][]; firstPackOff = new int[nt][]; tilePartsRead = new int[nt]; totTileHeadLen = new int[nt]; tilePartHeadLen = new int[nt][]; nBytes = new int[nt]; baknBytes = new int[nt]; hd.nTileParts = new int[nt]; // CONVERSION PROBLEM? //this.isTruncMode = isTruncMode; int t = 0, pos, tp = 0, tptot = 0; // Keeps main header's length, takes file format overhead into account int cdstreamStart = hd.mainHeadOff; // Codestream offset in the file mainHeadLen = in_Renamed.Pos - cdstreamStart; headLen = mainHeadLen; // If ncb and lbody quit conditions are used, headers are not counted if (ncbQuit == - 1) { anbytes = mainHeadLen; } else { anbytes = 0; } // If cannot even read the first tile-part if (anbytes > tnbytes) { throw new System.ApplicationException("Requested bitrate is too small."); } // Read all tile-part headers from all tiles. int tilePartStart; bool rateReached = false; int mdl; //int numtp = 0; totAllTileLen = 0; remainingTileParts = nt; // at least as many tile-parts as tiles int maxTP = nt; // If maximum 1 tile part per tile specified try { while (remainingTileParts != 0) { tilePartStart = in_Renamed.Pos; // Read tile-part header try { t = readTilePartHeader(); if (isEOCFound) { // Some tiles are missing but the // codestream is OK break; } tp = tilePartsRead[t]; if (isPsotEqualsZero) { // Psot may equals zero for the // last tile-part: it is assumed that this tile-part // contain all data until EOC tilePartLen[t][tp] = in_Renamed.length() - 2 - tilePartStart; } } catch (System.IO.EndOfStreamException e) { firstPackOff[t][tp] = in_Renamed.length(); throw e; } pos = in_Renamed.Pos; // In truncation mode, if target decoding rate is reached in // tile-part header, skips the tile-part and stop reading // unless the ncb and lbody quit condition is in use if (isTruncMode && ncbQuit == - 1) { if ((pos - cdstreamStart) > tnbytes) { firstPackOff[t][tp] = in_Renamed.length(); rateReached = true; break; } } // Set tile part position and header length firstPackOff[t][tp] = pos; tilePartHeadLen[t][tp] = (pos - tilePartStart); // Update length counters totTileLen[t] += tilePartLen[t][tp]; totTileHeadLen[t] += tilePartHeadLen[t][tp]; totAllTileLen += tilePartLen[t][tp]; if (isTruncMode) { if (anbytes + tilePartLen[t][tp] > tnbytes) { anbytes += tilePartHeadLen[t][tp]; headLen += tilePartHeadLen[t][tp]; rateReached = true; nBytes[t] += (tnbytes - anbytes); break; } else { anbytes += tilePartHeadLen[t][tp]; headLen += tilePartHeadLen[t][tp]; nBytes[t] += (tilePartLen[t][tp] - tilePartHeadLen[t][tp]); } } else { if (anbytes + tilePartHeadLen[t][tp] > tnbytes) { break; } else { anbytes += tilePartHeadLen[t][tp]; headLen += tilePartHeadLen[t][tp]; } } // If this is first tile-part, remember header length if (tptot == 0) firstTilePartHeadLen = tilePartHeadLen[t][tp]; // Go to the beginning of next tile part tilePartsRead[t]++; in_Renamed.seek(tilePartStart + tilePartLen[t][tp]); remainingTileParts--; maxTP--; tptot++; // If Psot of the current tile-part was equal to zero, it is // assumed that it contains all data until the EOC marker if (isPsotEqualsZero) { if (remainingTileParts != 0) { FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "Some tile-parts have not " + "been found. The codestream may be corrupted."); } break; } } } catch (System.IO.EndOfStreamException) { if (printInfo) { } FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "Codestream truncated in tile " + t); // Set specified rate to end of file if valid int fileLen = in_Renamed.length(); if (fileLen < tnbytes) { tnbytes = fileLen; trate = tnbytes * 8f / hd.MaxCompImgWidth / hd.MaxCompImgHeight; } // Bit-rate allocation if (!isTruncMode) { allocateRate(); } // Update 'res' value once all tile-part headers are read if (pl.getParameter("res") == null) { targetRes = decSpec.dls.Min; } else { try { targetRes = pl.getIntParameter("res"); if (targetRes < 0) { throw new System.ArgumentException("Specified negative " + "resolution level " + "index: " + targetRes); } } catch (System.FormatException) { throw new System.ArgumentException("Invalid resolution level " + "index ('-res' option) " + pl.getParameter("res")); } } // Verify reduction in resolution level mdl = decSpec.dls.Min; if (targetRes > mdl) { FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "Specified resolution level (" + targetRes + ") is larger" + " than the maximum value. Setting it to " + mdl + " (maximum value)"); targetRes = mdl; } // Backup nBytes for (int tIdx = 0; tIdx < nt; tIdx++) { baknBytes[tIdx] = nBytes[tIdx]; } return ; } remainingTileParts = 0; // Update 'res' value once all tile-part headers are read if (pl.getParameter("res") == null) { targetRes = decSpec.dls.Min; } else { try { targetRes = pl.getIntParameter("res"); if (targetRes < 0) { throw new System.ArgumentException("Specified negative " + "resolution level index: " + targetRes); } } catch (System.FormatException) { throw new System.ArgumentException("Invalid resolution level " + "index ('-res' option) " + pl.getParameter("res")); } } // Verify reduction in resolution level mdl = decSpec.dls.Min; if (targetRes > mdl) { FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "Specified resolution level (" + targetRes + ") is larger" + " than the maximum possible. Setting it to " + mdl + " (maximum possible)"); targetRes = mdl; } if (printInfo) { } // Check presence of EOC marker is decoding rate not reached or if // this marker has not been found yet if (!isEOCFound && !isPsotEqualsZero) { 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) { FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "EOC marker not found. " + "Codestream is corrupted."); } } catch (System.IO.EndOfStreamException) { FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "EOC marker is missing"); } } // Bit-rate allocation if (!isTruncMode) { allocateRate(); } else { // Take EOC into account if rate is not reached if (in_Renamed.Pos >= tnbytes) anbytes += 2; } // Backup nBytes for (int tIdx = 0; tIdx < nt; tIdx++) { baknBytes[tIdx] = nBytes[tIdx]; if (printInfo) { FacilityManager.getMsgLogger().println("" + hi.toStringTileHeader(tIdx, tilePartLen[tIdx].Length), 2, 2); } } }
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 HeaderDecoder instance and read in two passes the main header /// of the codestream. The first and last marker segments shall be /// respectively SOC and SOT. /// /// </summary> /// <param name="ehs">The encoded header stream where marker segments are /// extracted. /// /// </param> /// <param name="pl">The ParameterList object of the decoder /// /// </param> /// <param name="hi">The HeaderInfo holding information found in marker segments /// /// </param> /// <exception cref="IOException">If an I/O error occurs while reading from the /// encoded header stream. /// </exception> /// <exception cref="EOFException">If the end of the encoded header stream is /// reached before getting all the data. /// </exception> /// <exception cref="CorruptedCodestreamException">If invalid data is found in the /// codestream main header. /// /// </exception> public HeaderDecoder(RandomAccessIO ehs, ParameterList pl, HeaderInfo hi) { this.hi = hi; // CONVERSION PROBLEM? //this.verbose = verbose; pl.checkList(OPT_PREFIX, ParameterList.toNameArray(pinfo)); mainHeadOff = ehs.Pos; if (((short) ehs.readShort()) != CSJ2K.j2k.codestream.Markers.SOC) { throw new CorruptedCodestreamException("SOC marker segment not " + " found at the " + "beginning of the " + "codestream."); } // First Pass: Decode and store main header information until the SOT // marker segment is found nfMarkSeg = 0; do { extractMainMarkSeg(ehs.readShort(), ehs); } while ((nfMarkSeg & SOT_FOUND) == 0); //Stop when SOT is found ehs.seek(ehs.Pos - 2); // Realign codestream on SOT marker // Second pass: Read each marker segment previously found readFoundMainMarkSeg(); }
private void InitBlock(HeaderInfo enclosingInstance) { this.enclosingInstance = enclosingInstance; }
public POC(HeaderInfo enclosingInstance) { InitBlock(enclosingInstance); }
/// <summary> Creates a bit stream reader of the correct type that works on the /// provided RandomAccessIO, with the special parameters from the parameter /// list. /// /// </summary> /// <param name="in">The RandomAccessIO source from which to read the bit stream. /// /// </param> /// <param name="hd">Header of the codestream. /// /// </param> /// <param name="pl">The parameter list containing parameters applicable to the /// bit stream read (other parameters may also be present). /// /// </param> /// <param name="decSpec">The decoder specifications /// /// </param> /// <param name="cdstrInfo">Whether or not to print information found in /// codestream. /// /// </param> /// <param name="hi">Reference to the HeaderInfo instance. /// /// </param> /// <exception cref="IOException">If an I/O error occurs while reading initial /// data from the bit stream. /// </exception> /// <exception cref="IllegalArgumentException">If an unrecognised bit stream /// reader option is present. /// /// </exception> public static BitstreamReaderAgent createInstance(RandomAccessIO in_Renamed, HeaderDecoder hd, ParameterList pl, DecoderSpecs decSpec, bool cdstrInfo, HeaderInfo hi) { // Check parameters pl.checkList(BitstreamReaderAgent.OPT_PREFIX, CSJ2K.j2k.util.ParameterList.toNameArray(BitstreamReaderAgent.ParameterInfo)); return new FileBitstreamReaderAgent(hd, in_Renamed, decSpec, pl, cdstrInfo, hi); }