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

📄 pngimageproducer.java

📁 java 开源,Swing外观:JGoodies look & feel. ... JGoodies look&feels让你的Swing 应用程序与applets看起来更漂亮.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        */
        switch (chunkType) {
          case CHUNK_bKGD:
            handlebKGD();
            break;
          case CHUNK_cHRM:
            handlecHRM();
            break;
          case CHUNK_gAMA:
            handlegAMA();
            break;
          case CHUNK_hIST:
            handlehIST();
            break;
          case CHUNK_IDAT:
            handleIDAT();
            break;
          case CHUNK_IEND:
            handleIEND();
            break;
          case CHUNK_IHDR:
            handleIHDR();
            break;
          case CHUNK_pHYs:
            handlepHYs();
            break;
          case CHUNK_PLTE:
            handlePLTE();
            break;
          case CHUNK_sBIT:
            handlesBIT();
            break;
          case CHUNK_tEXt:
            handletEXt();
            break;
          case CHUNK_tIME:
            handletIME();
            break;
          case CHUNK_tRNS:
            handletRNS();
            break;
          case CHUNK_zTXt:
            handlezTXt();
            break;
          default:
            System.err.println("unrecognized chunk type " +
                                Integer.toHexString(chunkType) + ". skipping");
            inputStream.skip(chunkLength);
        }
        //inputStream = new DataInputStream(underlyingStream);
        int crc = inputStream.readInt();
        needChunkInfo = true;
    }

    private void handlegAMA() throws IOException {
        inputStream.skip(chunkLength);
    }

    private void handlehIST() throws IOException {
        inputStream.skip(chunkLength);
    }

    /**
     * Handle the image data chunks.  Note that sending info to ImageConsumers
     * is delayed until the first IDAT chunk is seen.  This allows for any
     * changes in ancilliary chunks that may alter the overall properties of
     * the image, such as aspect ratio altering the image dimensions.  We
     * assume that all ancilliary chunks which have these effects will appear
     * before the first IDAT chunk, and once seen, image properties are set in
     * stone.
     **/

    private void handleIDAT() throws IOException {
        if (!infoAvailable) {
            if (width == -1)
                width = dataWidth;
            if (height == -1)
                height = dataHeight;
            setColorModel();
            if (interlaceMethod != 0)
                multipass = true;

            Vector consumers;
            synchronized (this) {
                infoAvailable = true;
                consumers = (Vector) theConsumers.clone();
            }
            for (int i = 0; i < consumers.size(); i++) {
                initConsumer((ImageConsumer) consumers.elementAt(i));
            }
        }

        readImageData();

        sendPixels(0, 0, width, height);
    }

    private void handleIEND() throws IOException {
        complete = true;
    }

    private void handleIHDR() throws IOException {
        if (headerFound)
            throw new IOException("Extraneous IHDR chunk encountered.");
        if (chunkLength != 13)
            throw new IOException("IHDR chunk length wrong: " + chunkLength);
        dataWidth = inputStream.readInt();
        dataHeight = inputStream.readInt();
        depth = inputStream.read();
        colorType = inputStream.read();
        compressionMethod = inputStream.read();
        filterMethod = inputStream.read();
        interlaceMethod = inputStream.read();
    }

    private void handlePLTE() throws IOException {
        if (colorType == 3) {
            palette = new byte[chunkLength];
            inputStream.readFully(palette);
        } else {
            // Ignore suggested palette
            inputStream.skip(chunkLength);
        }
    }

    private void handlepHYs() throws IOException {
        /*  Not yet implemented -JDM
        int w = inputStream.readInt();
        int h = inputStream.readInt();
        inputStream.read();
        width = dataWidth * w / h;
        height = dataHeight * h / w;
        */
        inputStream.skip(chunkLength);
    }

    private void handlesBIT() throws IOException {
        inputStream.skip(chunkLength);
    }

    private void handletEXt() throws IOException {
        inputStream.skip(chunkLength);
    }

    private void handletIME() throws IOException {
        if (chunkLength != 7)
            System.err.println("tIME chunk length incorrect: " + chunkLength);
        inputStream.skip(chunkLength);
    }

    private void handletRNS() throws IOException {
        int chunkLen = chunkLength;
		if (palette == null)
			throw new IOException("tRNS chunk encountered before pLTE");

		int len = palette.length;

        switch (colorType) {
          case 3: {
            transparency = true;
            int transLength = len/3;
            byte[] trans = new byte[transLength];
            inputStream.readFully(trans, 0, chunkLength);

            byte b = (byte) 0xff;

            for (int i = len; i < transLength; i++) {
                trans[i] = b;
            }

            byte[] newPalette = new byte[len + transLength];

            for (int i = newPalette.length; i > 0;) {
                newPalette[--i] = trans[--transLength];
                newPalette[--i] = palette[--len];
                newPalette[--i] = palette[--len];
                newPalette[--i] = palette[--len];
            }

            palette = newPalette;
            break;
          }
          default:
            inputStream.skip(chunkLength);
        }
    }

    private void handlezTXt() throws IOException {
        inputStream.skip(chunkLength);
    }

    private void handleSignature() throws IOException {
        if ((inputStream.read() != 137) ||
            (inputStream.read() != 80) ||
            (inputStream.read() != 78) ||
            (inputStream.read() != 71) ||
            (inputStream.read() != 13) ||
            (inputStream.read() != 10) ||
            (inputStream.read() != 26) ||
            (inputStream.read() != 10)) {
            throw new IOException("Not a PNG File");
        }
    }

    private void initConsumer(ImageConsumer ic) {
        if (infoAvailable) {
            if (isConsumer(ic)) {
                ic.setDimensions(width, height);
            }
            if (isConsumer(ic)) {
                ic.setProperties(properties);
            }
            if (isConsumer(ic)) {
                ic.setColorModel(model);
            }
            if (isConsumer(ic)) {
                ic.setHints(multipass ?
                                        (ImageConsumer.TOPDOWNLEFTRIGHT |
                                        ImageConsumer.COMPLETESCANLINES)
                                    : (ImageConsumer.TOPDOWNLEFTRIGHT |
                                        ImageConsumer.COMPLETESCANLINES |
                                        ImageConsumer.SINGLEPASS |
                                        ImageConsumer.SINGLEFRAME));
            }
        }
    }

    private void insertGreyPixels(int pix[], int offset, int samples) {
        int p = pix[0];
        int ipix[] = ipixels;
        int cInc = colInc[pass];
        int rs = 0;

        switch (colorType) {
          case 0: {
            switch (depth) {
              case 1: {
                for (int j = 0; j < samples; j++, offset += cInc) {
                    if (rs != 0) {
                        rs--;
                    } else {
                        rs = 7;
                        p = pix[j>>3];
                    }
                    ipix[offset] = (p>>rs) & 0x1;
                }
                break;
              }
              case 2: {
                for (int j = 0; j < samples; j++, offset += cInc) {
                    if (rs != 0) {
                        rs -= 2;
                    } else {
                        rs = 6;
                        p = pix[j>>2];
                    }
                    ipix[offset] = (p>>rs) & 0x3;
                }
                break;
              }
              case 4: {
                for (int j = 0; j < samples; j++, offset += cInc) {
                    if (rs != 0) {
                        rs = 0;
                    } else {
                        rs = 4;
                        p = pix[j>>1];
                    }
                    ipix[offset] = (p>>rs) & 0xf;
                }
                break;
              }
              case 8: {
                for (int j = 0; j < samples; offset += cInc) {
                    ipix[offset] = (byte) pix[j++];
                }
                break;
              }
              case 16: {
                samples = samples<<1;
                for (int j = 0; j < samples; j += 2, offset += cInc) {
                    ipix[offset] = pix[j];
                }
                break;
              }
              default:
                break;
            }
            break;
          }
          case 4: {
            if (depth == 8) {
                for (int j = 0; j < samples; offset += cInc) {
                    ipix[offset] = (pix[j++]<<8) | pix[j++];
                }
            } else {
                samples = samples<<1;
                for (int j = 0; j < samples; j += 2, offset += cInc) {
                    ipix[offset] = (pix[j]<<8) | pix[j+=2];
                }
            }
            break;
          }
        }
    }

    private void insertPalettedPixels(int pix[], int offset, int samples) 
    {
        int rs = 0;
        int p = pix[0];
        byte bpix[] = bpixels;
        int cInc = colInc[pass];

        switch (depth) 
        {
          case 1: 
          {
            for (int j = 0; j < samples; j++, offset += cInc) 
            {
                if (rs != 0) 
                {
                    rs--;
                } 
                else 
                {
                    rs = 7;
                    p = pix[j>>3];
    
                }
                
                bpix[offset] = (byte) ((p>>rs) & 0x1);
            }
            break;
          }
          case 2: 
          {
            for (int j = 0; j < samples; j++, offset += cInc) 
            {
                if (rs != 0) 
                {
                    rs -= 2;
                } 
                else 
                {
                    rs = 6;
                    p = pix[j>>2];
                }
                
                bpix[offset] = (byte) ((p>>rs) & 0x3);
            }
            break;
          }
          case 4: 
          {
            for (int j = 0; j < samples; j++, offset += cInc) 
            {
                if (rs != 0) 
                {
                    rs = 0;
                } 
                else 
                {
                    rs = 4;
                    p = pix[j>>1];      
                }
                
                bpix[offset] = (byte) ((p>>rs) & 0xf);
            }
            break;
          }
          case 8: 
          {
            for (int j = 0; j < samples; j++, offset += cInc) 
            {
                bpix[offset] = (byte)pix[j];
            }
            break;
          }
          default:
            break;
        }
    }

⌨️ 快捷键说明

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