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

📄 jpegencoder.java

📁 一个完整的
💻 JAVA
字号:
// Decompiled by DJ v2.9.9.60 Copyright 2000 Atanas Neshkov  Date: 2002-12-05 9:38:29
// 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.awt.Image;
import java.io.*;
import java.util.Vector;

// Referenced classes of package com.objectplanet.chart.jpeg:
//            JpegInfo, DCT, Huffman

public class JpegEncoder
{

    public JpegEncoder(Image image1, int i, OutputStream outputstream)
    {
        Quality = i;
        JpegObj = new JpegInfo(image1);
        imageHeight = JpegObj.imageHeight;
        imageWidth = JpegObj.imageWidth;
        outStream = new BufferedOutputStream(outputstream);
        dct = new DCT(Quality);
        Huf = new Huffman(imageWidth, imageHeight);
    }

    public void Compress()
    {
        WriteHeaders(outStream);
        WriteCompressedData(outStream);
        WriteEOI(outStream);
        try
        {
            outStream.flush();
        }
        catch(IOException ioexception)
        {
            System.out.println("IO Error: " + ioexception.getMessage());
        }
    }

    public void WriteCompressedData(BufferedOutputStream bufferedoutputstream)
    {
        float af1[][] = new float[8][8];
        double ad[][] = new double[8][8];
        int ai[] = new int[64];
        int ai2[] = new int[JpegObj.NumberOfComponents];
        int[] _tmp = new int[64];
        boolean flag1 = false;
        boolean flag2 = false;
        int k3 = imageWidth % 8 == 0 ? imageWidth : (int)(Math.floor((double)imageWidth / 8D) + 1.0D) * 8;
        int l3 = imageHeight % 8 == 0 ? imageHeight : (int)(Math.floor((double)imageHeight / 8D) + 1.0D) * 8;
        for(int k1 = 0; k1 < JpegObj.NumberOfComponents; k1++)
        {
            k3 = Math.min(k3, JpegObj.BlockWidth[k1]);
            l3 = Math.min(l3, JpegObj.BlockHeight[k1]);
        }

        boolean flag = false;
        for(int k = 0; k < l3; k++)
        {
            for(int l = 0; l < k3; l++)
            {
                int i2 = l * 8;
                int j2 = k * 8;
                for(int l1 = 0; l1 < JpegObj.NumberOfComponents; l1++)
                {
                    int i3 = JpegObj.BlockWidth[l1];
                    int j3 = JpegObj.BlockHeight[l1];
                    float af[][] = (float[][])JpegObj.Components[l1];
                    for(int i = 0; i < JpegObj.VsampFactor[l1]; i++)
                    {
                        for(int j = 0; j < JpegObj.HsampFactor[l1]; j++)
                        {
                            int k2 = j * 8;
                            int l2 = i * 8;
                            int i1 = 0;
                            do
                            {
                                int j1 = 0;
                                do
                                    af1[i1][j1] = af[j2 + l2 + i1][i2 + k2 + j1];
                                while(++j1 < 8);
                            } while(++i1 < 8);
                            double ad1[][] = dct.forwardDCT(af1);
                            int ai1[] = dct.quantizeBlock(ad1, JpegObj.QtableNumber[l1]);
                            Huf.HuffmanBlockEncoder(bufferedoutputstream, ai1, ai2[l1], JpegObj.DCtableNumber[l1], JpegObj.ACtableNumber[l1]);
                            ai2[l1] = ai1[0];
                        }

                    }

                }

            }

        }

        Huf.flushBuffer(bufferedoutputstream);
    }

    public void WriteHeaders(BufferedOutputStream bufferedoutputstream)
    {
        byte abyte0[] = {
            -1, -40
        };
        WriteMarker(abyte0, bufferedoutputstream);
        byte abyte1[] = new byte[18];
        abyte1[0] = -1;
        abyte1[1] = -32;
        abyte1[2] = 0;
        abyte1[3] = 16;
        abyte1[4] = 74;
        abyte1[5] = 70;
        abyte1[6] = 73;
        abyte1[7] = 70;
        abyte1[8] = 0;
        abyte1[9] = 1;
        abyte1[10] = 0;
        abyte1[11] = 0;
        abyte1[12] = 0;
        abyte1[13] = 1;
        abyte1[14] = 0;
        abyte1[15] = 1;
        abyte1[16] = 0;
        abyte1[17] = 0;
        WriteArray(abyte1, bufferedoutputstream);
        String s = new String();
        s = JpegObj.getComment();
        int l1 = s.length();
        byte abyte2[] = new byte[l1 + 4];
        abyte2[0] = -1;
        abyte2[1] = -2;
        abyte2[2] = (byte)(l1 >> 8 & 0xff);
        abyte2[3] = (byte)(l1 & 0xff);
        System.arraycopy(JpegObj.Comment.getBytes(), 0, abyte2, 4, JpegObj.Comment.length());
        WriteArray(abyte2, bufferedoutputstream);
        byte abyte3[] = new byte[134];
        abyte3[0] = -1;
        abyte3[1] = -37;
        abyte3[2] = 0;
        abyte3[3] = -124;
        int k1 = 4;
        int i = 0;
        do
        {
            abyte3[k1++] = (byte)i;
            int ai[] = (int[])dct.quantum[i];
            int k = 0;
            do
                abyte3[k1++] = (byte)ai[jpegNaturalOrder[k]];
            while(++k < 64);
        } while(++i < 2);
        WriteArray(abyte3, bufferedoutputstream);
        byte abyte4[] = new byte[19];
        abyte4[0] = -1;
        abyte4[1] = -64;
        abyte4[2] = 0;
        abyte4[3] = 17;
        abyte4[4] = (byte)JpegObj.Precision;
        abyte4[5] = (byte)(JpegObj.imageHeight >> 8 & 0xff);
        abyte4[6] = (byte)(JpegObj.imageHeight & 0xff);
        abyte4[7] = (byte)(JpegObj.imageWidth >> 8 & 0xff);
        abyte4[8] = (byte)(JpegObj.imageWidth & 0xff);
        abyte4[9] = (byte)JpegObj.NumberOfComponents;
        int j1 = 10;
        for(i = 0; i < abyte4[9]; i++)
        {
            abyte4[j1++] = (byte)JpegObj.CompID[i];
            abyte4[j1++] = (byte)((JpegObj.HsampFactor[i] << 4) + JpegObj.VsampFactor[i]);
            abyte4[j1++] = (byte)JpegObj.QtableNumber[i];
        }

        WriteArray(abyte4, bufferedoutputstream);
        l1 = 2;
        j1 = 4;
        int k2 = 4;
        byte abyte5[] = new byte[17];
        byte abyte8[] = new byte[4];
        abyte8[0] = -1;
        abyte8[1] = -60;
        i = 0;
        do
        {
            int i2 = 0;
            abyte5[j1++ - k2] = (byte)((int[])Huf.bits.elementAt(i))[0];
            int l = 1;
            do
            {
                int j2 = ((int[])Huf.bits.elementAt(i))[l];
                abyte5[j1++ - k2] = (byte)j2;
                i2 += j2;
            } while(++l < 17);
            int l2 = j1;
            byte abyte6[] = new byte[i2];
            for(int i1 = 0; i1 < i2; i1++)
                abyte6[j1++ - l2] = (byte)((int[])Huf.val.elementAt(i))[i1];

            byte abyte7[] = new byte[j1];
            System.arraycopy(abyte8, 0, abyte7, 0, k2);
            System.arraycopy(abyte5, 0, abyte7, k2, 17);
            System.arraycopy(abyte6, 0, abyte7, k2 + 17, i2);
            abyte8 = abyte7;
            k2 = j1;
        } while(++i < 4);
        abyte8[2] = (byte)(j1 - 2 >> 8 & 0xff);
        abyte8[3] = (byte)(j1 - 2 & 0xff);
        WriteArray(abyte8, bufferedoutputstream);
        byte abyte9[] = new byte[14];
        abyte9[0] = -1;
        abyte9[1] = -38;
        abyte9[2] = 0;
        abyte9[3] = 12;
        abyte9[4] = (byte)JpegObj.NumberOfComponents;
        j1 = 5;
        for(int j = 0; j < abyte9[4]; j++)
        {
            abyte9[j1++] = (byte)JpegObj.CompID[j];
            abyte9[j1++] = (byte)((JpegObj.DCtableNumber[j] << 4) + JpegObj.ACtableNumber[j]);
        }

        abyte9[j1++] = (byte)JpegObj.Ss;
        abyte9[j1++] = (byte)JpegObj.Se;
        abyte9[j1++] = (byte)((JpegObj.Ah << 4) + JpegObj.Al);
        WriteArray(abyte9, bufferedoutputstream);
    }

    void WriteMarker(byte abyte0[], BufferedOutputStream bufferedoutputstream)
    {
        try
        {
            bufferedoutputstream.write(abyte0, 0, 2);
        }
        catch(IOException ioexception)
        {
            System.out.println("IO Error: " + ioexception.getMessage());
        }
    }

    public void setQuality(int i)
    {
        dct = new DCT(i);
    }

    public int getQuality()
    {
        return Quality;
    }

    void WriteArray(byte abyte0[], BufferedOutputStream bufferedoutputstream)
    {
        try
        {
            int i = ((abyte0[2] & 0xff) << 8) + (abyte0[3] & 0xff) + 2;
            bufferedoutputstream.write(abyte0, 0, i);
        }
        catch(IOException ioexception)
        {
            System.out.println("IO Error: " + ioexception.getMessage());
        }
    }

    public void WriteEOI(BufferedOutputStream bufferedoutputstream)
    {
        byte abyte0[] = {
            -1, -39
        };
        WriteMarker(abyte0, bufferedoutputstream);
    }

    Thread runner;
    BufferedOutputStream outStream;
    Image image;
    JpegInfo JpegObj;
    Huffman Huf;
    DCT dct;
    int imageHeight;
    int imageWidth;
    int Quality;
    int code;
    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
    };

}

⌨️ 快捷键说明

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