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

📄 huffmandecode.java

📁 这是利用图像隐藏信息的代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }
    
    public int getByte() {
        int b=0;
        // Read Byte from DataInputStream
        try {
            b = dis.readUnsignedByte();
        } catch (IOException e) {
	    e.printStackTrace();
	}
        return b;
    }
    
    public int getInt() {
        int b=0;
        // Read Integer from DataInputStream
        try {
            b = dis.readUnsignedByte();
            b <<= 8;
            int tmp = dis.readUnsignedByte();
            b ^= tmp;
        } catch (IOException e) {
	    e.printStackTrace();
	}
        return b;
    }
    
    private void closeStream() {
        // Close input stream
        try {
            dis.close(); // close io stream to file
	} catch (IOException e) {}
    }
    
    // Public get methods
    public int getX() { return X; }
    public int getY() { return Y; }
    public int getPrec() { return P; }
    public int getComp() { return Nf; }
    // Calculate the Number of blocks encoded
    public int getBlockCount() {
	switch (Nf) {
	case 1:
	    return ((X+7)/8)*((Y+7)/8);
	case 3:
	    return 6*((X+15)/16)*((Y+15)/16);
	default:
	    System.out.println("Nf weder 1 noch 3");
	}
	return 0;
    }
    public void setCr(int[][][] chrome) { Cr = chrome; }
    public void setCb(int[][][] chrome) { Cb = chrome; }
    
    
    // Return image data
    public void HuffDecode(int[][][] buffer) {
        int x, y, tmp, sz = X * Y, scan=0;
        int[][] Block   = new int[8][8];        
        int Cs, Ta, Td, blocks;
        long t;
        double time;
	
        // Read in Scan Header information
        Ls = getInt();  Ns = getByte();
        Cs = getByte();
        Td = getByte();
        Ta = Td & 0x0f;
        Td >>= 4;
	
        Ss = getByte(); Se = getByte();
        Ah = getByte(); Al = Ah & 0x0f;
        Ah >>= 4;
        
        // Calculate the Number of blocks encoded
        //blocks = X * Y / 64;
	blocks = getBlockCount()/6;
        
        // decode image data and return image data in array        
        for(cnt=0;cnt<blocks;cnt++) {
	    // Get DC coefficient
	    if(Td == 0)
		hftbl = 0;
	    else
		hftbl = 2;
	    tmp = DECODE();
	    DIFF = RECEIVE(tmp);
	    ZZ[0] = PRED + EXTEND(DIFF, tmp);
	    PRED = ZZ[0];
	    
	    // Get AC coefficients
	    if(Ta == 0)
		hftbl = 1;
	    else
		hftbl = 3;
	    Decode_AC_coefficients();
	    
	    // dezigzag and dequantize block
	    for(lp=0;lp<64;lp++)
		Block[deZZ[lp][0]][deZZ[lp][1]] = ZZ[lp] * QNT[0][lp];
	    
	    // store blocks in buffer
	    for(x=0;x<8;x++)
		for(y=0;y<8;y++)
		buffer[cnt][x][y]=Block[x][y];
        }
        closeStream();
    }
    
    // Return quantized coefficients
    public void rawDecode(int[][][] buffer) {
        int x, y, tmp;
        int[][] Block   = new int[8][8];        
        int Cs, Ta, Td, blocks;
        long t;
        double time;
	
        // Read in Scan Header information
        Ls = getInt();  Ns = getByte();
        Cs = getByte();
        Td = getByte();
        Ta = Td & 0x0f;
        Td >>= 4;
	
        Ss = getByte(); Se = getByte();
        Ah = getByte(); Al = Ah & 0x0f;
        Ah >>= 4;
        
        // Calculate the Number of blocks encoded
        blocks = getBlockCount()/6;
        
        // decode image data and return image data in array        
        for(cnt=0;cnt<blocks;cnt++) {
	    // Get DC coefficient
	    if(Td == 0)
		hftbl = 0;
	    else
		hftbl = 2;
	    tmp = DECODE();
	    DIFF = RECEIVE(tmp);
	    ZZ[0] = PRED + EXTEND(DIFF, tmp);
	    PRED = ZZ[0];
	    
	    // Get AC coefficients
	    if(Ta == 0)
		hftbl = 1;
	    else
		hftbl = 3;
	    Decode_AC_coefficients();
	    
	    // dezigzag
	    for(lp=0;lp<64;lp++)
		Block[deZZ[lp][0]][deZZ[lp][1]] = ZZ[lp];
	    
	    // store blocks in buffer
	    System.out.print(cnt+" ");
	    for(x=0;x<8;x++) {
		for(y=0;y<8;y++) {
		    buffer[cnt][x][y]=Block[x][y];
		}
	    }
        }
        closeStream();
    }
    
    // Return image data for RGB images
    public void RGBdecode(int[][][] Lum) {
        int x, y, a, b, line, col, tmp, sz = X * Y;
        int blocks, MCU, scan=0;
        int[][] Block   = new int[8][8];
        int[] Cs, Ta, Td;
        int[] PRED = {0, 0, 0};
        long t;
        double time;
	
        // Read in Scan Header information
        Ls = getInt();  Ns = getByte();
        Cs = new int[Ns];
        Td = new int[Ns];
        Ta = new int[Ns];
	
        // get table information
        for(lp=0;lp<Ns;lp++) {
            Cs[lp] = getByte();
            Td[lp] = getByte();
            Ta[lp] = Td[lp] & 0x0f;
            Td[lp] >>= 4;
        }
	
        Ss = getByte(); Se = getByte();
        Ah = getByte(); Al = Ah & 0x0f;
        Ah >>= 4;
	
        // Calculate the Number of blocks encoded
        //blocks = X * Y / 64;
	blocks = getBlockCount()/6;
        col = 2;        
	
        // decode image data and return image data in array
        for(a=0;a<32;a++)
            for(b=0;b<32;b++) {
            // Get component 1 of MCU
            for(cnt=0;cnt<4;cnt++){
                // Get DC coefficient
                hftbl = 0;
                tmp = DECODE();
                DIFF = RECEIVE(tmp);
                ZZ[0] = PRED[0] + EXTEND(DIFF, tmp);
                PRED[0] = ZZ[0];
		
                // Get AC coefficients
                hftbl = 1;
                Decode_AC_coefficients();
		
                // dezigzag and dequantize block
                for(lp=0;lp<64;lp++)
                    Block[deZZ[lp][0]][deZZ[lp][1]] = ZZ[lp] * QNT[0][lp];
		
                if(cnt<2) line = 0;
                else line = 62;
		
                // store blocks in buffer
                for(x=0;x<8;x++)
                    for(y=0;y<8;y++)
		    Lum[b*2+cnt+line+a*128][x][y]=Block[x][y];
            }
	    
            // getComponent 2 and 3 of image
            for(cnt=0;cnt<2;cnt++) {
                // Get DC coefficient
                hftbl = 2;
                tmp = DECODE();
                DIFF = RECEIVE(tmp);
                ZZ[0] = PRED[cnt+1] + EXTEND(DIFF, tmp);
                PRED[cnt+1] = ZZ[0];
		
                // Get AC coefficients
                hftbl = 3;
                Decode_AC_coefficients();
		
                // dezigzag and dequantize block
                for(lp=0;lp<64;lp++)
                    Block[deZZ[lp][0]][deZZ[lp][1]] = ZZ[lp] * QNT[1][lp];
		
                // store blocks in buffer
                if(cnt == 0) {
		    for(x=0;x<8;x++)
			for(y=0;y<8;y++)
			Cb[a*32+b][x][y]=Block[x][y];
                }
                else {
		    for(x=0;x<8;x++)
			for(y=0;y<8;y++)
			Cr[a*32+b][x][y]=Block[x][y];
                }
            }
        }
        closeStream();
    }

    // Return image data 
    public int [] decode() {
        int x, y, a, b, line,/* col,*/ tmp;//, sz = X * Y;
        int blocks, MCU;//, scan=0;
        int[] Cs, Ta, Td;
        int[] PRED =new int[Nf];
	for(int nComponent=0;nComponent<Nf;nComponent++)
	    PRED[nComponent]=0;
        long t;
        double time;
	CNT=0;
        // Read in Scan Header information
        Ls = getInt();  Ns = getByte();
	//System.out.println("SOS - Components: "+Integer.toString(Ns));
        Cs = new int[Ns];
        Td = new int[Ns];
        Ta = new int[Ns];
	
        // get table information
        for(lp=0;lp<Ns;lp++) {
            Cs[lp] = getByte();
            Td[lp] = getByte();
            Ta[lp] = Td[lp] & 0x0f;
            Td[lp] >>= 4;
	    //System.out.println("DC-Table: "+Integer.toString(Td[lp])+"AC-Table: "+Integer.toString(Ta[lp])); 
	}
	
        Ss = getByte(); Se = getByte();
        Ah = getByte(); Al = Ah & 0x0f;
        Ah >>= 4;
	
        // Calculate the Number of blocks encoded
// warum doppelt so viel?
	int buff[]=new int[2*8*8*getBlockCount()];
	int pos=0;
	int MCUCount=0;

//System.out.println("BlockCount="+getBlockCount());
	boolean bDoIt=true;
	while(bDoIt) {
	    // Get component 1 of MCU
            for(int nComponent=0;nComponent<Nf;nComponent++) {
		for(cnt=0;cnt<H[nComponent]*V[nComponent];cnt++) {
		    // Get DC coefficient
		    hftbl = Td[nComponent]*2;
		    tmp = DECODE();
		    DIFF = RECEIVE(tmp);
		    ZZ[0] = PRED[0] + EXTEND(DIFF, tmp);
		    PRED[nComponent] = ZZ[0];
		    
		    // Get AC coefficients
		    hftbl = Ta[nComponent]*2+1;
		    Decode_AC_coefficients();
		    
		    for(lp=0;lp<64;lp++) {
			//System.out.println("pos="+pos);
//Zickzack???
			// buff[pos++]=ZZ[deZigZag[lp]];
			buff[pos++]=ZZ[lp];
		    }
		}
	    }
	    
	    MCUCount++;
	    if(MCUCount==RI) {
		MCUCount=0;
		CNT=0;
		for(int nComponent=0;nComponent<Nf;nComponent++)
		    PRED[nComponent]=0;
		//				System.out.println("MCUCount");
		getByte();
		//				System.out.println(Integer.toHexString(getByte()));
		int tmpB=getByte();
		//				System.out.println(Integer.toHexString(tmpB));
		if(tmpB==EOI)
		    break;
		//				System.out.println("MCUCount-Ende");
	    }
	    if(available()<=2) {
		//System.out.println("expecting end of image");
		if (available()==2) {
		    getByte();
		    if (getByte() != EOI)
			System.out.println("file does not end with EOI");
		} else {
		    if (available()>0)
			System.out.println(Integer.toHexString(getByte()));
		    System.out.println("file does not end with EOI");
		}
		break;
	    }
        }
	int[] tmpBuff=new int[pos];
	System.arraycopy(buff,0,tmpBuff,0,pos);
	return tmpBuff;
    }
    
    //{{ Control Objects
    HuffTable htDC0, htDC1;
    HuffTable htAC0, htAC1;
    DataInputStream dis;
    TextArea ta;
    Date dt;
    //}}
}

⌨️ 快捷键说明

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