decoder.java

来自「jpeg2000算法实现」· Java 代码 · 共 1,149 行 · 第 1/3 页

JAVA
1,149
字号
                outext = outfile.substring(outfile.lastIndexOf('.'),                                           outfile.length());                outbase = outfile.substring(0,outfile.lastIndexOf('.'));            }            else {                outbase = outfile;                outext = ".pgx";            }                        // **** Open input files ****            // Creater BEBufferedRandomAccessFIle 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("Could not 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("Could not 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("Could not 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("Could not 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());                        // **** Header decoder ****            // Instantiate header decoder and read main header 	    try{		hd = new HeaderDecoder(in,verbose,pl);	    }	    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;	    }	    decSpec = hd.getDecoderSpecs();            // **** Instantiate decoding chain ****            // Get demixed bitdepths	    nComp = hd.getNumComps();            depth = new int[nComp];            for (i=0; i<nComp;i++) {                depth[i] = hd.getOriginalBitDepth(i);            }            // **** Bitstream reader ****            try {                breader = BitstreamReaderAgent.createInstance(in,hd,                                                              pl,decSpec);            }            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("Could not 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;            }            // **** Entropy decoder ****            try {                entdec = hd.createEntropyDecoder(breader,pl);            }            catch (IllegalArgumentException e) {                error("Could not 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;            }            // **** ROI de-scaler ****            try {                roids = hd.createROIDeScaler(entdec,pl);            }            catch (IllegalArgumentException e) {                error("Could not 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);            }            catch (IllegalArgumentException e) {                error("Could not 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("Could not instantiate inverse wavelet transform"+                          ((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;            }	    res = breader.getImgRes();	    int mrl = decSpec.dls.getMin();            invWT.setImgResLevel(res);	    // **** Data converter **** (after inverse transform module)	    converter = new ImgDataConverter(invWT,0);            // **** Inverse component transformation ****             ictransf = new InvCompTransf(converter,decSpec,depth);	                // **** Create image writers/image display ****	    if(disp){ // No output file has been specified. Display		// decoded image				// Set up the display elements		btitle = "JJ2000: "+(new File(infile)).getName()+" "+		    ictransf.getImgWidth()+"x"+ictransf.getImgHeight();                if (isp == null) {                    win = new Frame(btitle+" @ (0,0) : 1");                    win.setBackground(Color.white);                    win.addWindowListener(new ExitHandler(this));                    isp = new ImgScrollPane(ImgScrollPane.SCROLLBARS_AS_NEEDED);                    win.add(isp,BorderLayout.CENTER);                    isp.addKeyListener(new ImgKeyListener(isp,this));                    // HACK to make it work under Windows: for some reason                    // under JDK 1.1.x the event is delivered to the window                    // and not to the ImgScrollPane.                    win.addKeyListener(new ImgKeyListener(isp,this));                    // END HACK                }                else {                    win = null;                }                                // Get the window dimension to use, do not use more                // than 8/10 of the screen size, in either dimension.                if (win != null) {                    win.addNotify();  // Instantiate peer to get insets                    ins = win.getInsets();                    winDim =                        new Dimension(ictransf.getImgWidth()+ins.left+ins.right,                                      ictransf.getImgHeight()+ins.top+                                      ins.bottom);                    scrnDim = win.getToolkit().getScreenSize();                    if (winDim.width > scrnDim.width*8/10f) {                        // Width too large for screen                        winDim.width = (int)(scrnDim.width*8/10f);                    }                    if (winDim.height > scrnDim.height*8/10f) {                        // Height too large for screen                        winDim.height = (int)(scrnDim.height*8/10f);                    }                    win.setSize(winDim);                    win.validate();                    win.setVisible(true);                    // Start the title updater                    Thread tu;                    title = new TitleUpdater(isp,win,btitle);                    tu = new Thread(title);                    tu.start();                }                else {                    title = null;                }	    }	    else{ // Write decoded image to specified output file		// Create output file names		if (outext.equalsIgnoreCase(".PPM") &&		    (nComp != 3 ||		     ictransf.getNomRangeBits(0) > 8 ||		     ictransf.getNomRangeBits(1) > 8 ||		     ictransf.getNomRangeBits(2) > 8 ||		     hd.isOriginalSigned(0) || hd.isOriginalSigned(1) ||		     hd.isOriginalSigned(2))) {		    error("Specified PPM output file but compressed image is "+                          "not of the correct format for PPM or limited "+                          "decoded components to less than 3.",1);		    return;		}		out = new String[nComp];		if (nComp > 1 &&		    !outext.equalsIgnoreCase(".PPM")) { // Multiple output files		    // If PGM verify bitdepth and if signed		    if (outext.equalsIgnoreCase(".PGM")) {			for (i=0;i<nComp; i++) {			    if (ictransf.getNomRangeBits(i) > 8 ||				hd.isOriginalSigned(i)) {				error("Specified PGM output file but "+                                      "compressed image is not of the "+				      "correct "+                                      "format for PGM.",1);				return;			    }			}		    }		    // Open multiple output files		    for (i=0; i<nComp; i++) {			out[i] = outbase + "-" + (i+1) +  outext;		    }		}		else { // Single output file		    out[0] = outbase + outext;		}		// Now get the image writers		if (outext.equalsIgnoreCase(".PPM")) {		    imwriter = new ImgWriter[1];		    try {			imwriter[0] = new ImgWriterPPM(out[0],ictransf,						       0,1,2);		    }		    catch (IOException e) {			error("Could not write PPM header or open output file" 			      + i + ((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;		    }		    		}		else if (outext.equalsIgnoreCase(".PGM")) {		    imwriter = new ImgWriter[nComp];		    try {			for (i=0;i<nComp;i++) {			    imwriter[i] = new ImgWriterPGM(out[i],ictransf,i);			}		    }		    catch (IOException e) {			error("Could not write PGM header or open output file "+			      "for component " 			      + i + ((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;		    }		}		else { // PGX		    imwriter = new ImgWriter[nComp];		    try {			for (i=0;i<nComp;i++) {			    imwriter[i] =				new ImgWriterPGX(out[i],ictransf,i,						 hd.isOriginalSigned(i));			}		    }		    catch (IOException e) {			error("Could not write PGX header or open output file "+			      "for component " 			      + i + ((e.getMessage() != null) ?				     (":\n"+e.getMessage()) : ""),2);                        if(pl.getParameter("debug").equals("on"))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?