decoder.java
来自「jpeg2000编解码」· Java 代码 · 共 1,326 行 · 第 1/4 页
JAVA
1,326 行
boolean verbose; int res; // resolution level to reconstruct String infile; RandomAccessIO in; FileFormatReader ff; String outfile="", outbase="", outext=""; String out[] = null; BitstreamReaderAgent breader; HeaderDecoder hd; EntropyDecoder entdec; ROIDeScaler roids; Dequantizer deq; InverseWT invWT; InvCompTransf ictransf; ImgWriter imwriter[] = null; ImgDataConverter converter; DecoderSpecs decSpec = null; BlkImgDataSrc palettized; BlkImgDataSrc channels; BlkImgDataSrc resampled; BlkImgDataSrc color; int i; int depth[]; float rate; int nbytes; boolean disp = false; Image img = null; Dimension winDim,scrnDim; Insets ins = null; String btitle = ""; try { // **** Usage and version **** try { // Do we print version information? if(pl.getBooleanParameter("v")) { printVersionAndCopyright(); } // Do we print usage information? if (pl.getParameter("u").equals("on")) { printUsage(); return; // When printing usage => exit } // Do we print info ? verbose = pl.getBooleanParameter("verbose"); } catch (StringFormatException e) { error("An error occured while parsing the arguments:\n"+ e.getMessage(),1); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } catch (NumberFormatException e) { error("An error occured while parsing the arguments:\n"+ e.getMessage(),1); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } // **** Check parameters **** try { pl.checkList(vprfxs,pl.toNameArray(pinfo)); } catch (IllegalArgumentException e) { error(e.getMessage(),2); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } // Get input file infile = pl.getParameter("i"); if(infile == null) { error("Input file ('-i' option) has not been specified",1); return; } // Get output files outfile = pl.getParameter("o"); if(outfile == null) { disp = true; } else if(outfile.lastIndexOf('.') != -1) { outext = outfile.substring(outfile.lastIndexOf('.'), outfile.length()); outbase = outfile.substring(0,outfile.lastIndexOf('.')); } else { outbase = outfile; outext = ".pgx"; } // **** Open input files **** // Creates a BEBufferedRandomAccessFile of a ISRandomAccessIO // instance for reading the file format and codestream data if(infile.indexOf("/") >= 1 && infile.charAt(infile.indexOf("/")-1) == ':') { // an URL URL inurl; URLConnection conn; int datalen; InputStream is; try { inurl = new URL(infile); } catch (MalformedURLException e) { error("Malformed URL for input file "+infile,4); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } try { conn = inurl.openConnection(); conn.connect(); } catch (IOException e) { error("Cannot open connection to "+infile+ ((e.getMessage() != null) ? (":\n"+e.getMessage()) : ""), 4); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } datalen = conn.getContentLength(); try { is = conn.getInputStream(); } catch (IOException e) { error("Cannot get data from connection to "+infile+ ((e.getMessage() != null) ? (":\n"+e.getMessage()) : ""), 4); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } if(datalen != -1) { // known length => initialize to length in = new ISRandomAccessIO(is,datalen,1,datalen); } else { // unknown length => use defaults in = new ISRandomAccessIO(is); } // HACK: to verify if the URL is valid try to read some data try { in.read(); in.seek(0); } catch (IOException e) { error("Cannot get input data from "+infile+ " Invalid URL?",4); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } } else { // a normal file try { in = new BEBufferedRandomAccessFile(infile,"r"); } catch (IOException e) { error("Cannot open input file "+ ((e.getMessage() != null) ? (":\n"+e.getMessage()) : ""), 4); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } } // **** File Format **** // If the codestream is wrapped in the jp2 fileformat, Read the // file format wrapper ff = new FileFormatReader(in); ff.readFileFormat(); if(ff.JP2FFUsed) { in.seek(ff.getFirstCodeStreamPos()); } // +----------------------------+ // | Instantiate decoding chain | // +----------------------------+ // **** Header decoder **** // Instantiate header decoder and read main header hi = new HeaderInfo(); try { hd = new HeaderDecoder(in,pl,hi); } catch (EOFException e) { error("Codestream too short or bad header, "+ "unable to decode.",2); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } int nCompCod = hd.getNumComps(); int nTiles = hi.siz.getNumTiles(); decSpec = hd.getDecoderSpecs(); // Report information if(verbose) { String info = nCompCod+" component(s) in codestream, "+nTiles+ " tile(s)\n"; info += "Image dimension: "; for(int c=0; c<nCompCod; c++) { info += hi.siz.getCompImgWidth(c)+"x"+ hi.siz.getCompImgHeight(c)+" "; } if(nTiles!=1) { info += "\nNom. Tile dim. (in canvas): "+ hi.siz.xtsiz+"x"+hi.siz.ytsiz; } FacilityManager.getMsgLogger().printmsg(MsgLogger.INFO,info); } if (pl.getBooleanParameter("cdstr_info")) { FacilityManager.getMsgLogger().printmsg(MsgLogger.INFO, "Main header:\n"+hi. toStringMainHeader()); } // Get demixed bitdepths depth = new int[nCompCod]; for(i=0; i<nCompCod;i++) { depth[i] = hd.getOriginalBitDepth(i); } // **** Bit stream reader **** try { breader = BitstreamReaderAgent. createInstance(in,hd,pl,decSpec, pl.getBooleanParameter("cdstr_info"),hi); } catch (IOException e) { error("Error while reading bit stream header or parsing "+ "packets"+((e.getMessage() != null) ? (":\n"+e.getMessage()) : ""),4); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } catch (IllegalArgumentException e) { error("Cannot instantiate bit stream reader"+ ((e.getMessage() != null) ? (":\n"+e.getMessage()) : ""),2); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } // **** Side information parser **** SideInfoRetrieval sir = new SideInfoRetrieval(breader,pl); // **** Entropy decoder **** try { entdec = hd.createEntropyDecoder(sir,pl); } catch (IllegalArgumentException e) { error("Cannot instantiate entropy decoder"+ ((e.getMessage() != null) ? (":\n"+e.getMessage()) : ""),2); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } // **** Unscrambling **** Unscrambling wavUns = new Unscrambling(entdec,pl); // **** ROI de-scaler **** try { roids = hd.createROIDeScaler(wavUns,pl,decSpec); } catch (IllegalArgumentException e) { error("Cannot instantiate roi de-scaler."+ ((e.getMessage() != null) ? (":\n"+e.getMessage()) : ""),2); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } // **** Dequantizer **** try { deq = hd.createDequantizer(roids,depth,decSpec); } catch (IllegalArgumentException e) { error("Cannot instantiate dequantizer"+ ((e.getMessage() != null) ? (":\n"+e.getMessage()) : ""),2); if(pl.getParameter("debug").equals("on")) { e.printStackTrace(); } else { error("Use '-debug' option for more details",2); } return; } // **** Inverse wavelet transform *** try { // full page inverse wavelet transform invWT = InverseWT.createInstance(deq,decSpec); } catch (IllegalArgumentException e) { error("Cannot instantiate inverse wavelet transform"+ ((e.getMessage() != null) ? (":\n"+e.getMessage()) : ""),2); if(pl.getParameter("debug").equals("on")) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?