Huffman.java
资源名称:OA.rar [点击查看]
上传用户:mingda
上传日期:2017-06-20
资源大小:27691k
文件大小:11k
源码类别:
OA系统
开发平台:
Java
- // Decompiled by DJ v2.9.9.60 Copyright 2000 Atanas Neshkov Date: 2002-12-05 9:38:13
- // Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
- // Decompiler options: packimports(3)
- // Source File Name: JpegEncoder.java
- package cn.com.fcsoft.chart.jpeg;
- import java.io.*;
- import java.util.Vector;
- class Huffman
- {
- public Huffman(int i, int j)
- {
- bits = new Vector();
- bits.addElement(bitsDCluminance);
- bits.addElement(bitsACluminance);
- bits.addElement(bitsDCchrominance);
- bits.addElement(bitsACchrominance);
- val = new Vector();
- val.addElement(valDCluminance);
- val.addElement(valACluminance);
- val.addElement(valDCchrominance);
- val.addElement(valACchrominance);
- initHuf();
- code = code;
- ImageWidth = i;
- ImageHeight = j;
- }
- public void HuffmanBlockEncoder(BufferedOutputStream bufferedoutputstream, int ai[], int i, int j, int k)
- {
- NumOfDCTables = 2;
- NumOfACTables = 2;
- int i1;
- int l = i1 = ai[0] - i;
- if(l < 0)
- {
- l = -l;
- i1--;
- }
- int k1 = 0;
- for(; l != 0; l >>= 1)
- k1++;
- bufferIt(bufferedoutputstream, ((int[][])DC_matrix[j])[k1][0], ((int[][])DC_matrix[j])[k1][1]);
- if(k1 != 0)
- bufferIt(bufferedoutputstream, i1, k1);
- int j2 = 0;
- int i2 = 1;
- do
- if((l = ai[jpegNaturalOrder[i2]]) == 0)
- {
- j2++;
- } else
- {
- for(; j2 > 15; j2 -= 16)
- bufferIt(bufferedoutputstream, ((int[][])AC_matrix[k])[240][0], ((int[][])AC_matrix[k])[240][1]);
- int j1 = l;
- if(l < 0)
- {
- l = -l;
- j1--;
- }
- int l1;
- for(l1 = 1; (l >>= 1) != 0; l1++);
- int k2 = (j2 << 4) + l1;
- bufferIt(bufferedoutputstream, ((int[][])AC_matrix[k])[k2][0], ((int[][])AC_matrix[k])[k2][1]);
- bufferIt(bufferedoutputstream, j1, l1);
- j2 = 0;
- }
- while(++i2 < 64);
- if(j2 > 0)
- bufferIt(bufferedoutputstream, ((int[][])AC_matrix[k])[0][0], ((int[][])AC_matrix[k])[0][1]);
- }
- public void initHuf()
- {
- DC_matrix0 = new int[12][2];
- DC_matrix1 = new int[12][2];
- AC_matrix0 = new int[255][2];
- AC_matrix1 = new int[255][2];
- DC_matrix = new Object[2];
- AC_matrix = new Object[2];
- int ai[] = new int[257];
- int ai1[] = new int[257];
- int i = 0;
- int k = 1;
- do
- {
- for(int l = 1; l <= bitsDCchrominance[k]; l++)
- ai[i++] = k;
- } while(++k <= 16);
- ai[i] = 0;
- int l1 = i;
- int j2 = 0;
- int i2 = ai[0];
- for(i = 0; ai[i] != 0;)
- {
- while(ai[i] == i2)
- {
- ai1[i++] = j2;
- j2++;
- }
- j2 <<= 1;
- i2++;
- }
- for(i = 0; i < l1; i++)
- {
- DC_matrix1[valDCchrominance[i]][0] = ai1[i];
- DC_matrix1[valDCchrominance[i]][1] = ai[i];
- }
- i = 0;
- k = 1;
- do
- {
- for(int i1 = 1; i1 <= bitsACchrominance[k]; i1++)
- ai[i++] = k;
- } while(++k <= 16);
- ai[i] = 0;
- l1 = i;
- j2 = 0;
- i2 = ai[0];
- for(i = 0; ai[i] != 0;)
- {
- while(ai[i] == i2)
- {
- ai1[i++] = j2;
- j2++;
- }
- j2 <<= 1;
- i2++;
- }
- for(i = 0; i < l1; i++)
- {
- AC_matrix1[valACchrominance[i]][0] = ai1[i];
- AC_matrix1[valACchrominance[i]][1] = ai[i];
- }
- i = 0;
- k = 1;
- do
- {
- for(int j1 = 1; j1 <= bitsDCluminance[k]; j1++)
- ai[i++] = k;
- } while(++k <= 16);
- ai[i] = 0;
- l1 = i;
- j2 = 0;
- i2 = ai[0];
- for(i = 0; ai[i] != 0;)
- {
- while(ai[i] == i2)
- {
- ai1[i++] = j2;
- j2++;
- }
- j2 <<= 1;
- i2++;
- }
- for(i = 0; i < l1; i++)
- {
- DC_matrix0[valDCluminance[i]][0] = ai1[i];
- DC_matrix0[valDCluminance[i]][1] = ai[i];
- }
- i = 0;
- k = 1;
- do
- {
- for(int k1 = 1; k1 <= bitsACluminance[k]; k1++)
- ai[i++] = k;
- } while(++k <= 16);
- ai[i] = 0;
- l1 = i;
- j2 = 0;
- i2 = ai[0];
- for(int j = 0; ai[j] != 0;)
- {
- while(ai[j] == i2)
- {
- ai1[j++] = j2;
- j2++;
- }
- j2 <<= 1;
- i2++;
- }
- for(int k2 = 0; k2 < l1; k2++)
- {
- AC_matrix0[valACluminance[k2]][0] = ai1[k2];
- AC_matrix0[valACluminance[k2]][1] = ai[k2];
- }
- DC_matrix[0] = DC_matrix0;
- DC_matrix[1] = DC_matrix1;
- AC_matrix[0] = AC_matrix0;
- AC_matrix[1] = AC_matrix1;
- }
- void bufferIt(BufferedOutputStream bufferedoutputstream, int i, int j)
- {
- int k = i;
- int l = bufferPutBits;
- k &= (1 << j) - 1;
- l += j;
- k <<= 24 - l;
- k |= bufferPutBuffer;
- for(; l >= 8; l -= 8)
- {
- int i1 = k >> 16 & 0xff;
- try
- {
- bufferedoutputstream.write(i1);
- }
- catch(IOException ioexception)
- {
- System.out.println("IO Error: " + ioexception.getMessage());
- }
- if(i1 == 255)
- try
- {
- bufferedoutputstream.write(0);
- }
- catch(IOException ioexception1)
- {
- System.out.println("IO Error: " + ioexception1.getMessage());
- }
- k <<= 8;
- }
- bufferPutBuffer = k;
- bufferPutBits = l;
- }
- void flushBuffer(BufferedOutputStream bufferedoutputstream)
- {
- int i = bufferPutBuffer;
- int j;
- for(j = bufferPutBits; j >= 8; j -= 8)
- {
- int k = i >> 16 & 0xff;
- try
- {
- bufferedoutputstream.write(k);
- }
- catch(IOException ioexception)
- {
- System.out.println("IO Error: " + ioexception.getMessage());
- }
- if(k == 255)
- try
- {
- bufferedoutputstream.write(0);
- }
- catch(IOException ioexception1)
- {
- System.out.println("IO Error: " + ioexception1.getMessage());
- }
- i <<= 8;
- }
- if(j > 0)
- {
- int l = i >> 16 & 0xff;
- try
- {
- bufferedoutputstream.write(l);
- }
- catch(IOException ioexception2)
- {
- System.out.println("IO Error: " + ioexception2.getMessage());
- }
- }
- }
- int bufferPutBits;
- int bufferPutBuffer;
- public int ImageHeight;
- public int ImageWidth;
- public int DC_matrix0[][];
- public int AC_matrix0[][];
- public int DC_matrix1[][];
- public int AC_matrix1[][];
- public Object DC_matrix[];
- public Object AC_matrix[];
- public int code;
- public int NumOfDCTables;
- public int NumOfACTables;
- public int bitsDCluminance[] = {
- 0, 0, 1, 5, 1, 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 0, 0, 0
- };
- public int valDCluminance[] = {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
- 10, 11
- };
- public int bitsDCchrominance[] = {
- 1, 0, 3, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 0, 0, 0, 0, 0
- };
- public int valDCchrominance[] = {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
- 10, 11
- };
- public int bitsACluminance[] = {
- 16, 0, 2, 1, 3, 3, 2, 4, 3, 5,
- 5, 4, 4, 0, 0, 1, 125
- };
- public int valACluminance[] = {
- 1, 2, 3, 0, 4, 17, 5, 18, 33, 49,
- 65, 6, 19, 81, 97, 7, 34, 113, 20, 50,
- 129, 145, 161, 8, 35, 66, 177, 193, 21, 82,
- 209, 240, 36, 51, 98, 114, 130, 9, 10, 22,
- 23, 24, 25, 26, 37, 38, 39, 40, 41, 42,
- 52, 53, 54, 55, 56, 57, 58, 67, 68, 69,
- 70, 71, 72, 73, 74, 83, 84, 85, 86, 87,
- 88, 89, 90, 99, 100, 101, 102, 103, 104, 105,
- 106, 115, 116, 117, 118, 119, 120, 121, 122, 131,
- 132, 133, 134, 135, 136, 137, 138, 146, 147, 148,
- 149, 150, 151, 152, 153, 154, 162, 163, 164, 165,
- 166, 167, 168, 169, 170, 178, 179, 180, 181, 182,
- 183, 184, 185, 186, 194, 195, 196, 197, 198, 199,
- 200, 201, 202, 210, 211, 212, 213, 214, 215, 216,
- 217, 218, 225, 226, 227, 228, 229, 230, 231, 232,
- 233, 234, 241, 242, 243, 244, 245, 246, 247, 248,
- 249, 250
- };
- public int bitsACchrominance[] = {
- 17, 0, 2, 1, 2, 4, 4, 3, 4, 7,
- 5, 4, 4, 0, 1, 2, 119
- };
- public int valACchrominance[] = {
- 0, 1, 2, 3, 17, 4, 5, 33, 49, 6,
- 18, 65, 81, 7, 97, 113, 19, 34, 50, 129,
- 8, 20, 66, 145, 161, 177, 193, 9, 35, 51,
- 82, 240, 21, 98, 114, 209, 10, 22, 36, 52,
- 225, 37, 241, 23, 24, 25, 26, 38, 39, 40,
- 41, 42, 53, 54, 55, 56, 57, 58, 67, 68,
- 69, 70, 71, 72, 73, 74, 83, 84, 85, 86,
- 87, 88, 89, 90, 99, 100, 101, 102, 103, 104,
- 105, 106, 115, 116, 117, 118, 119, 120, 121, 122,
- 130, 131, 132, 133, 134, 135, 136, 137, 138, 146,
- 147, 148, 149, 150, 151, 152, 153, 154, 162, 163,
- 164, 165, 166, 167, 168, 169, 170, 178, 179, 180,
- 181, 182, 183, 184, 185, 186, 194, 195, 196, 197,
- 198, 199, 200, 201, 202, 210, 211, 212, 213, 214,
- 215, 216, 217, 218, 226, 227, 228, 229, 230, 231,
- 232, 233, 234, 242, 243, 244, 245, 246, 247, 248,
- 249, 250
- };
- public Vector bits;
- public Vector val;
- public static int jpegNaturalOrder[] = {
- 0, 1, 8, 16, 9, 2, 3, 10, 17, 24,
- 32, 25, 18, 11, 4, 5, 12, 19, 26, 33,
- 40, 48, 41, 34, 27, 20, 13, 6, 7, 14,
- 21, 28, 35, 42, 49, 56, 57, 50, 43, 36,
- 29, 22, 15, 23, 30, 37, 44, 51, 58, 59,
- 52, 45, 38, 31, 39, 46, 53, 60, 61, 54,
- 47, 55, 62, 63
- };
- }