⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 headerencoder.java

📁 jpeg2000算法实现
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	    sb = sbRoot;            sb = (SubbandAn) sb.getSubbandByIdx(0,0);            // Output one exponent per subband            for (int j=0; j<=mrl; j++) {		sb2 = sb;                while (sb2 != null) {                    int tmp = (imgnr+sb2.anGainExp);                    hbuf.write(tmp<<SQCX_EXP_SHIFT);		    sb2 = (SubbandAn)sb2.nextSubband();		}                // Go up one resolution level		sb = (SubbandAn)sb.getNextResLevel();            }            break;        case SQCX_SCALAR_DERIVED:            // Get resolution level 0 subband            sb = sbRoot;            sb = (SubbandAn) sb.getSubbandByIdx(0,0);            // Calculate subband step (normalized to unit            // dynamic range)            step = baseStep/(1<<sb.level);                            // Write exponent-mantissa, 16 bits            hbuf.writeShort(StdQuantizer.                            convertToExpMantissa(step));            break;        case SQCX_SCALAR_EXPOUNDED:            // Get resolution level 0 subband            sb = sbRoot;            mrl = sb.resLvl;            sb = (SubbandAn) sb.getSubbandByIdx(0,0);            for (int j=0; j<=mrl; j++) {                sb2 = sb;                while (sb2 != null) {                    // Calculate subband step (normalized to unit                    // dynamic range)                    step = baseStep/(sb2.l2Norm*(1<<sb2.anGainExp));                                            // Write exponent-mantissa, 16 bits                    hbuf.writeShort(StdQuantizer.                                    convertToExpMantissa(step));                    sb2 = (SubbandAn)sb2.nextSubband();                }                // Go up one resolution level                sb = (SubbandAn) sb.getNextResLevel();            }            break;        default:            throw new Error("Internal JJ2000 error");        }    }    /**     * Writes POC marker segment. POC is a functional marker segment     * containing the bounds and progression order for any progression order     * other than default in the codestream.     *     * @param mh Flag indicating whether the main header is to be written      *     * @param tileIdx Tile index     * */    protected void writePOC(boolean mh, int tileIdx) throws IOException {        int markSegLen=0;        // Segment marker length        int lenCompField;        // Holds the size of any component field as                                 // this size depends on the number of                                  //components        Progression[] prog = null; // Holds the progression(s)        int npoc;                // Number of progression order changes        // Get the progression order changes, their number and checks        // if it is ok        if(mh){            prog = (Progression[])(encSpec.ps.getDefault());        }        else {            prog = (Progression[])(encSpec.ps.getTileDef(tileIdx));        }        // Calculate the length of a component field (depends on the number of         // components)        lenCompField = (nComp<257 ? 1 : 2);        // POC marker        hbuf.writeShort(POC);        // Lpoc (marker segment length (in bytes))        // Basic: Lpoc(2 bytes) + npoc * [ RSpoc(1) + CSpoc(1 or 2) + LYEpoc(2)         // + REpoc(1) + CEpoc(1 or 2) + Ppoc(1) ]	npoc = prog.length-1;        markSegLen = 2 + npoc * (1+lenCompField+2+1+lenCompField+1);        hbuf.writeShort(markSegLen);        // Write each progression order change         for (int i=0 ; i<npoc ; i++){            // RSpoc(i)            hbuf.write(prog[i].rs);            // CSpoc(i)            if ( lenCompField==2 ) {                hbuf.writeShort(prog[i].cs);            }            else {                hbuf.write(prog[i].cs);            }            // LYEpoc(i)            hbuf.writeShort(prog[i].lye);            // REpoc(i)            hbuf.write(prog[i].re);            // CEpoc(i)            if ( lenCompField==2 ) {                hbuf.writeShort(prog[i].ce);            }            else {                hbuf.write(prog[i].ce);            }            // Ppoc(i)            hbuf.write(prog[i+1].type);        }    }    /**     * Write main header. JJ2000 main header corresponds to the following     * sequence of marker     * segments:<ol><li>SOC</li><li>SIZ</li><li>COD</li><li>COC (if     * needed)</li><li>QCD</li><li>QCC (if needed)</li><li>POC (if     * needed)</li></ol>     * */    public void encodeMainHeader() throws IOException {        int i;	        // +---------------------------------+        // |    SOC marker segment           |        // +---------------------------------+        writeSOC();        // +---------------------------------+        // |    Image and tile SIZe (SIZ)    |        // +---------------------------------+        writeSIZ();        // +-------------------------------+        // |   COding style Default (COD)  |        // +-------------------------------+        boolean isEresUsed = ((String)encSpec.tts.getDefault()).            equals("predict");        writeCOD(true,0);                    // +---------------------------------+        // |   COding style Component (COC)  |        // +---------------------------------+	for (i= 0; i<nComp; i++) {            boolean isEresUsedinComp = ((String)encSpec.tts.getCompDef(i)).                equals("predict");	    if(encSpec.wfs.isCompSpecified(i) ||               encSpec.dls.isCompSpecified(i) ||               encSpec.bms.isCompSpecified(i) ||               encSpec.mqrs.isCompSpecified(i) ||               encSpec.rts.isCompSpecified(i) ||               encSpec.sss.isCompSpecified(i) ||               encSpec.css.isCompSpecified(i) ||               encSpec.pss.isCompSpecified(i) ||               encSpec.cblks.isCompSpecified(i) ||               (isEresUsed != isEresUsedinComp ) )		// Some component non-default stuff => need COC		writeCOC(true,0,i);        }        // +-------------------------------+        // |   Quantization Default (QCD)  |        // +-------------------------------+        writeMainQCD();        // +-------------------------------+        // | Quantization Component (QCC)  |        // +-------------------------------+        // Write needed QCC markers        for(i=0; i<nComp; i++){	    if(dwt.getNomRangeBits(i)!= defimgn ||	       encSpec.qts.isCompSpecified(i) ||                encSpec.qsss.isCompSpecified(i) ||               encSpec.dls.isCompSpecified(i) ||               encSpec.gbs.isCompSpecified(i)){                writeMainQCC(i);	    }        }        // +--------------------------+        // |    POC maker segment     |	// +--------------------------+	Progression[] prog = (Progression[])(encSpec.ps.getDefault());        if(prog.length>1)            writePOC(true, 0);        // +--------------------------+        // |      Comment (COM)       |        // +--------------------------+        writeCOM();    }    /**      * Write a COM marker segment adding some comments to the codestream.      *      * <p> This marker is currently written in main header and indicates the     * JJ2000 encoder's version that has created the codestream.      * */    private void writeCOM() throws IOException {        String str = "CREATOR: JJ2000 version "+JJ2KInfo.version;        int markSegLen; // the marker segment length                // COM marker        hbuf.writeShort(COM);        // Calculate length: Lcom(2) + Rcom (2) + string's length;        markSegLen = 2 + 2 + str.length();        hbuf.writeShort(markSegLen);        // Rcom         hbuf.writeShort(1); // General use (IS 8859-15:1999(Latin) values)        byte[] chars = str.getBytes();        for(int i=0; i<chars.length; i++) {            hbuf.writeByte(chars[i]);        }    }    /**     * Writes the RGN marker segment in the tile header. It describes the     * scaling value in each tile component     *     * <P>May be used in tile or main header. If used in main header, it     * refers to a ROI of the whole image, regardless of tiling. When used in     * tile header, only the particular tile is affected.     *     * @param tIdx The tile index      *     * @exception IOException If an I/O error occurs while reading from the     * encoder header stream     * */    private void writeRGN(int tIdx) throws IOException {        int i;        int markSegLen;    // the marker length        // Write one RGN marker per component         for(i=0;i<nComp;i++){            // RGN marker            hbuf.writeShort(RGN);                        // Calculate length (Lrgn)            // Basic: Lrgn (2) + Srgn (1) + SPrgn + one byte             // or two for component number            markSegLen = 4+((nComp<257)? 1:2);            hbuf.writeShort(markSegLen);                        // Write component (Crgn)            if(nComp<257)                hbuf.writeByte(i);            else                hbuf.writeShort(i);                        // Write type of ROI (Srgn)             hbuf.writeByte(SRGN_IMPLICIT);                        // Write ROI info (SPrgn)            hbuf.writeByte(((Integer)(encSpec.rois.			      getTileCompVal(tIdx,i))).intValue());        }    }    /**      * Writes tile-part header. JJ2000 tile-part header corresponds to the     * following sequence of marker segments:<ol> <li>SOT</li> <li>COD (if     * needed)</li> <li>COC (if needed)</li> <li>QCD (if needed)</li> <li>QCC     * (if needed)</li> <li>RGN (if needed)</li> <li>POC (if needed)</li>     * <li>SOD</li> </ol>     *     * @param length The length of the current tile-part.     *     * @param tileIdx Index of the tile to write     * */    public void encodeTilePartHeader(int tileLength,int tileIdx)         throws IOException {                        int tmp;          	// +--------------------------+        // |    SOT maker segment     |	// +--------------------------+        // SOT marker        hbuf.writeByte(SOT>>8);        hbuf.writeByte(SOT);                    // Lsot (10 bytes)        hbuf.writeByte(0);        hbuf.writeByte(10);                    // Isot        if(tileIdx>65534){            throw new IllegalArgumentException("Trying to write a tile-part "+                                               "header whose tile index is too"+                                               " high");        }        hbuf.writeByte(tileIdx>>8);        hbuf.writeByte(tileIdx);                // Psot        tmp = tileLength;        hbuf.writeByte(tmp>>24);        hbuf.writeByte(tmp>>16);        hbuf.writeByte(tmp>>8);        hbuf.writeByte(tmp);                // TPsot        hbuf.writeByte(0); // Only one tile-part currently supported !	// TNsot	hbuf.writeByte(1); // Only one tile-part currently supported !        	// +--------------------------+        // |    COD maker segment     |	// +--------------------------+        boolean isEresUsed = ((String)encSpec.tts.getDefault()).            equals("predict");        boolean isEresUsedInTile = ((String)encSpec.tts.getTileDef(tileIdx)).            equals("predict");        boolean tileCODwritten = false;	if(encSpec.wfs.isTileSpecified(tileIdx) ||           encSpec.cts.isTileSpecified(tileIdx) ||           encSpec.dls.isTileSpecified(tileIdx) ||           encSpec.bms.isTileSpecified(tileIdx) ||           encSpec.mqrs.isTileSpecified(tileIdx) ||           encSpec.rts.isTileSpecified(tileIdx) ||           encSpec.css.isTileSpecified(tileIdx) ||           encSpec.pss.isTileSpecified(tileIdx) ||           encSpec.sops.isTileSpecified(tileIdx) ||           encSpec.sss.isTileSpecified(tileIdx) ||           encSpec.ps.isTileSpecified(tileIdx) ||           encSpec.ephs.isTileSpecified(tileIdx) ||           encSpec.cblks.isTileSpecified(tileIdx) ||           ( isEresUsed != isEresUsedInTile ) ) {	    writeCOD(false,tileIdx);            tileCODwritten = true;	}	// +--------------------------+        // |    COC maker segment     |	// +--------------------------+	for(int c=0; c<nComp; c++){            boolean isEresUsedInTileComp = ((String)encSpec.tts.                                            getTileCompVal(tileIdx,c)).		equals("predict");	    if(encSpec.wfs.isTileCompSpecified(tileIdx,c) ||               encSpec.dls.isTileCompSpecified(tileIdx,c) ||               encSpec.bms.isTileCompSpecified(tileIdx,c) ||               encSpec.mqrs.isTileCompSpecified(tileIdx,c) ||               encSpec.rts.isTileCompSpecified(tileIdx,c) ||               encSpec.css.isTileCompSpecified(tileIdx,c) ||               encSpec.pss.isTileCompSpecified(tileIdx,c) ||               encSpec.sss.isTileCompSpecified(tileIdx,c) ||               encSpec.cblks.isTileCompSpecified(tileIdx,c) ||               ( isEresUsedInTileComp != isEresUsed ) ) {		writeCOC(false,tileIdx,c);	    }            else if(tileCODwritten){                if(encSpec.wfs.isCompSpecified(c) ||                   encSpec.dls.isCompSpecified(c) ||                   encSpec.bms.isCompSpecified(c) ||                   encSpec.mqrs.isCompSpecified(c) ||                   encSpec.rts.isCompSpecified(c) ||                   encSpec.sss.isCompSpecified(c) ||                   encSpec.css.isCompSpecified(c) ||                   encSpec.pss.isCompSpecified(c) ||                   encSpec.cblks.isCompSpecified(c) ||                   (encSpec.tts.isCompSpecified(c)&&                    ((String)encSpec.tts.getCompDef(c)).equals("predict"))){                    writeCOC(false,tileIdx,c);                }            }        }	// +--------------------------+        // |    QCD maker segment     |	// +--------------------------+        boolean tileQCDwritten = false;	if(encSpec.qts.isTileSpecified(tileIdx) ||           encSpec.qsss.isTileSpecified(tileIdx) ||           encSpec.dls.isTileSpecified(tileIdx) ||           encSpec.gbs.isTileSpecified(tileIdx)){	    writeTileQCD(tileIdx);            tileQCDwritten = true;	} else {            deftilenr = defimgn;        }		// +--------------------------+        // |    QCC maker segment     |	// +--------------------------+	for(int c=0; c<nComp; c++){	    if(dwt.getNomRangeBits(c)!= deftilenr ||               encSpec.qts.isTileCompSpecified(tileIdx,c) ||               encSpec.qsss.isTileCompSpecified(tileIdx,c) ||               encSpec.dls.isTileCompSpecified(tileIdx,c) ||               encSpec.gbs.isTileCompSpecified(tileIdx,c)){		writeTileQCC(tileIdx,c);	    }            else if(tileQCDwritten){                if(encSpec.qts.isCompSpecified(c) ||                    encSpec.qsss.isCompSpecified(c) ||                   encSpec.dls.isCompSpecified(c) ||                   encSpec.gbs.isCompSpecified(c)){                    writeTileQCC(tileIdx,c);                }            }	}	// +--------------------------+        // |    RGN maker segment     |	// +--------------------------+        if(roiSc.useRoi() &&(!roiSc.getBlockAligned()))            writeRGN(tileIdx);      	// +--------------------------+        // |    POC maker segment     |	// +--------------------------+	Progression[] prog;        if( encSpec.ps.isTileSpecified(tileIdx) ){	    prog = (Progression[])(encSpec.ps.getTileDef(tileIdx));	    if(prog.length>1)                writePOC(false,tileIdx);        }	// +--------------------------+        // |         SOD maker        |	// +--------------------------+        hbuf.writeByte(SOD>>8);        hbuf.writeByte(SOD);    } }

⌨️ 快捷键说明

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