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

📄 pktencoder.java

📁 jpeg2000算法实现
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                    cur_prevtIdxs[b] < 0) { // First inclusion                    // Set layer index                    cur_ttIncl.setValue(by-firstCB_vertIdx,                                        bx-firstCB_horIdx, ly-1);                }            }                           if (ly == 1) { // First layer, need to set the skip of MSBP                for ( int numcb=1 ; numcb<cbv.size() ; numcb++ ) {                    b = ((Integer)cbv.elementAt(numcb)).intValue();                    cbCoord = cbArrayI[t][c][r][s][b].idx;                    bx = cbCoord.x;                    by = cbCoord.y;                                    // Save "offset" as tag trees are indexed from 0                    if ( numcb==1 ) {                        firstCB_horIdx = bx;                        firstCB_vertIdx = by;                    }                                    // Set skip of most sig. bit-plane                    cur_ttMaxBP.setValue(by-firstCB_vertIdx,                                         bx-firstCB_horIdx,                                         cur_cbs[b].skipMSBP);                }            }                          // Now encode the information            for(int numcb=1; numcb<cbv.size(); numcb++){                b = ((Integer)cbv.elementAt(numcb)).intValue();                cbCoord = cbArrayI[t][c][r][s][b].idx;                bx = cbCoord.x;                by = cbCoord.y;                                // Save "offset" as tag trees are indexed from 0                if( numcb==1 ){                    firstCB_horIdx = bx;                    firstCB_vertIdx = by;                }                // 1) Inclusion information                if (cur_tIndx[b] > cur_prevtIdxs[b]) {                    // Block included in this precinct                    if (cur_prevtIdxs[b] < 0) { // First inclusion                        // Encode layer info                        cur_ttIncl.encode(by-firstCB_vertIdx,                                          bx-firstCB_horIdx,ly,hbuf);                        // 2) Max bitdepth info. Encode value                        thmax = cur_cbs[b].skipMSBP+1;                        for (i=1; i<=thmax;i++) {                            cur_ttMaxBP.encode(by-firstCB_vertIdx,                                               bx-firstCB_horIdx,i,hbuf);                        }                        // Count body size for packet                        lblen += cur_cbs[b].                            truncRates[cur_cbs[b].truncIdxs[cur_tIndx[b]]];                    }                    else { // Already in previous layer                        // Send "1" bit                        hbuf.writeBit(1);                        // Count body size for packet                        lblen +=                            cur_cbs[b].                            truncRates[cur_cbs[b].truncIdxs[cur_tIndx[b]]] -                            cur_cbs[b].                            truncRates[cur_cbs[b].                                      truncIdxs[cur_prevtIdxs[b]]];                    }                    // 3) Truncation point information                    if (cur_prevtIdxs[b]<0) {                        newtp = cur_cbs[b].truncIdxs[cur_tIndx[b]];                    }                    else {                        newtp = cur_cbs[b].truncIdxs[cur_tIndx[b]]-                            cur_cbs[b].truncIdxs[cur_prevtIdxs[b]]-1;                    }                    // Mix of switch and if is faster                    switch (newtp) {                    case 0:                        hbuf.writeBit(0); // Send one "0" bit                        break;                    case 1:                        hbuf.writeBits(2,2); // Send one "1" and one "0"                        break;                    case 2:                    case 3:                    case 4:                            // Send two "1" bits followed by 2 bits                            // representation of newtp-2                        hbuf.writeBits((3<<2)|(newtp-2),4);                        break;                    default:                        if (newtp <= 35) {                            // Send four "1" bits followed by a five bits                            // representation of newtp-5                            hbuf.writeBits((15<<5)|(newtp-5),9);                        }                        else if (newtp <= 163) {                            // Send nine "1" bits followed by a seven bits                            // representation of newtp-36                            hbuf.writeBits((511<<7)|(newtp-36),16);                        }                        else {                            throw new                                ArithmeticException("Maximum number "+                                                    "of truncation "+                                                    "points exceeded");                        }                    }                }                else { // Block not included in this layer                    if (cur_prevtIdxs[b] >= 0) {                        // Already in previous layer. Send "0" bit                        hbuf.writeBit(0);                    }                    else { // Not in any previous layers                        cur_ttIncl.encode(by-firstCB_vertIdx,                                          bx-firstCB_horIdx,ly,hbuf);                    }                    // Go to the next one.                    continue;                }                // Code-block length                                // We need to compute the maximum number of bits needed to                // signal the length of each terminated segment and the final                // truncation point.                newtp = 1;                maxi = cur_cbs[b].truncIdxs[cur_tIndx[b]];                cblen = (cur_prevtIdxs[b]<0) ? 0 :                    cur_cbs[b].truncRates[cur_cbs[b].                                         truncIdxs[cur_prevtIdxs[b]]];                                // Loop on truncation points                i = (cur_prevtIdxs[b]<0) ? 0 :                    cur_cbs[b].truncIdxs[cur_prevtIdxs[b]]+1;                int minbits = 0;                for (; i<maxi; i++, newtp++) {                    // If terminated truncation point calculate length                    if (cur_cbs[b].isTermPass != null &&                        cur_cbs[b].isTermPass[i]) {                        // Calculate length                        cblen = cur_cbs[b].truncRates[i] - cblen;                        // Calculate number of needed bits                        prednbits = lblock[t][c][r][s][b] +                            MathUtil.log2(newtp);                        minbits = ((cblen>0) ? MathUtil.log2(cblen) : 0)+1;                        // Update Lblock increment if needed                        for(int j=prednbits; j<minbits; j++) {                            lblock[t][c][r][s][b]++;                            hbuf.writeBit(1);                        }                        // Initialize for next length                        newtp = 0;                        cblen = cur_cbs[b].truncRates[i];                    }                }                // Last truncation point length always sent                // Calculate length                cblen = cur_cbs[b].truncRates[i] - cblen;                                // Calculate number of bits                prednbits = lblock[t][c][r][s][b] + MathUtil.log2(newtp);                minbits = ((cblen>0) ? MathUtil.log2(cblen) : 0)+1;                // Update Lblock increment if needed                for(int j=prednbits; j<minbits; j++) {                    lblock[t][c][r][s][b]++;                    hbuf.writeBit(1);                }                                // End of comma-code increment                hbuf.writeBit(0);                // There can be terminated several segments, send length info                // for all terminated truncation points in addition to final                // one                newtp = 1;                maxi = cur_cbs[b].truncIdxs[cur_tIndx[b]];                cblen = (cur_prevtIdxs[b]<0) ? 0 :                    cur_cbs[b].truncRates[cur_cbs[b].                                         truncIdxs[cur_prevtIdxs[b]]];                // Loop on truncation points and count the groups                i = (cur_prevtIdxs[b]<0) ? 0 :                    cur_cbs[b].truncIdxs[cur_prevtIdxs[b]]+1;                for (; i<maxi; i++, newtp++) {                    // If terminated truncation point, send length                    if (cur_cbs[b].isTermPass != null &&                        cur_cbs[b].isTermPass[i]) {                        cblen = cur_cbs[b].truncRates[i] - cblen;                        nbits = MathUtil.log2(newtp)+lblock[t][c][r][s][b];                        hbuf.writeBits(cblen,nbits);                        // Initialize for next length                        newtp = 0;                        cblen = cur_cbs[b].truncRates[i];                    }                }                // Last truncation point length is always signalled                // First calculate number of bits needed to signal                // Calculate length                cblen = cur_cbs[b].truncRates[i] - cblen;                nbits = MathUtil.log2(newtp) + lblock[t][c][r][s][b];                hbuf.writeBits(cblen,nbits);            } // End loop on code-blocks        } // End loop on subband                // -> Copy the data to the body buffer                // Ensure size for body data        if (bbuf == null || bbuf.length < lblen){            bbuf = new byte[lblen];        }        lbbuf = bbuf;        lblen = 0;                    for (s=minsb; s<maxsb; s++) { // Loop on subbands                    // Get in the vector cblks the code-blocks that "belong" to the             // precinct whose index is 'p'            cbv = getCBlkInPrec(t,c,r,s,pIdx,cbv);                        cur_prevtIdxs = prevtIdxs[t][c][r][s];            cur_cbs = cbs[s];            cur_tIndx = tIndx[s];            ncb = cur_prevtIdxs.length;                        for ( int numcb=1 ; numcb<cbv.size() ; numcb++ ) {                b = ((Integer)cbv.elementAt(numcb)).intValue();                if (cur_tIndx[b] > cur_prevtIdxs[b]) {                    // Block included in this precinct                    // Copy data to body buffer and get code-size                    if (cur_prevtIdxs[b] < 0) {                        cblen = cur_cbs[b].                            truncRates[cur_cbs[b].truncIdxs[cur_tIndx[b]]];                        System.arraycopy(cur_cbs[b].data,0,                                         lbbuf,lblen,cblen);                    }                    else {                        cblen = cur_cbs[b].                            truncRates[cur_cbs[b].truncIdxs[cur_tIndx[b]]] -                            cur_cbs[b].                            truncRates[cur_cbs[b].truncIdxs[cur_prevtIdxs[b]]];                        System.                            arraycopy(cur_cbs[b].data,                                      cur_cbs[b].                                      truncRates[cur_cbs[b].                                                truncIdxs[cur_prevtIdxs[b]]],                                      lbbuf,lblen,cblen);                    }                    lblen += cblen;                    // Verify if this code-block contains new ROI information                    if(cur_cbs[b].nROIcoeff!=0 &&                        (cur_prevtIdxs[b]==-1 ||                       cur_cbs[b].truncIdxs[cur_prevtIdxs[b]] <=                       cur_cbs[b].nROIcp-1) ) {                        roiInPkt = true;                        roiLen = lblen;                    }                     // Update truncation point                    cur_prevtIdxs[b] = cur_tIndx[b];                }            }        }        if(!precIdxExist){ // No precinct found in this resolution level            packetWritable = false;            return hbuf;        } else {            packetWritable = true;        }        // Must never happen	if(hbuf.getLength()==0){            throw new Error("You have found a bug in PktEncoder, method:"+                            " encodePacket");	}        return hbuf;    }    /**     * Returns the buffer of the body of the last encoded packet. The length     * of the body can be retrieved with the getLastBodyLen() method. The     * length of the array returned by this method may be larger than the     * actual body length.     *     * @return The buffer of body of the last encoded packet.     *     * @exception IllegalArgumentException If no packet has been coded since     * last reset(), last restore(), or object creation.     *     * @see #getLastBodyLen     * */    public byte[] getLastBodyBuf() {        if (lbbuf == null) {            throw new IllegalArgumentException();        }        return lbbuf;    }    /**     * Returns the length of the body of the last encoded packet, in     * bytes. The body itself can be retrieved with the getLastBodyBuf()     * method.     *     * @return The length of the body of last encoded packet, in bytes.     *     * @see #getLastBodyBuf     * */    public int getLastBodyLen() {        return lblen;    }    /**     * Saves the current state of this object. The last saved state     * can be restored with the restore() method.

⌨️ 快捷键说明

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