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

📄 bmpimage.java

📁 处理PDF
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        //	    bdata = (byte[])((DataBufferByte)tile.getDataBuffer()).getData();        //	else if (sampleModel.getDataType() == DataBuffer.TYPE_USHORT)        //	    sdata = (short[])((DataBufferUShort)tile.getDataBuffer()).getData();        //	else if (sampleModel.getDataType() == DataBuffer.TYPE_INT)        //	    idata = (int[])((DataBufferInt)tile.getDataBuffer()).getData();                // There should only be one tile.        switch(imageType) {                        case VERSION_2_1_BIT:                // no compression                return read1Bit(3);                            case VERSION_2_4_BIT:                // no compression                return read4Bit(3);                            case VERSION_2_8_BIT:                // no compression                return read8Bit(3);                            case VERSION_2_24_BIT:                // no compression                bdata = new byte[width * height * 3];                read24Bit(bdata);                return new ImgRaw(width, height, 3, 8, bdata);                            case VERSION_3_1_BIT:                // 1-bit images cannot be compressed.                return read1Bit(4);                            case VERSION_3_4_BIT:                switch((int)compression) {                    case BI_RGB:                        return read4Bit(4);                                            case BI_RLE4:                        return readRLE4();                                            default:                        throw new                        RuntimeException("Invalid compression specified for BMP file.");                }                            case VERSION_3_8_BIT:                switch((int)compression) {                    case BI_RGB:                        return read8Bit(4);                                            case BI_RLE8:                        return readRLE8();                                            default:                        throw new                        RuntimeException("Invalid compression specified for BMP file.");                }                            case VERSION_3_24_BIT:                // 24-bit images are not compressed                bdata = new byte[width * height * 3];                read24Bit(bdata);                return new ImgRaw(width, height, 3, 8, bdata);                            case VERSION_3_NT_16_BIT:                return read1632Bit(false);                            case VERSION_3_NT_32_BIT:                return read1632Bit(true);                            case VERSION_4_1_BIT:                return read1Bit(4);                            case VERSION_4_4_BIT:                switch((int)compression) {                                        case BI_RGB:                        return read4Bit(4);                                            case BI_RLE4:                        return readRLE4();                                            default:                        throw new                        RuntimeException("Invalid compression specified for BMP file.");                }                            case VERSION_4_8_BIT:                switch((int)compression) {                                        case BI_RGB:                        return read8Bit(4);                                            case BI_RLE8:                        return readRLE8();                                            default:                        throw new                        RuntimeException("Invalid compression specified for BMP file.");                }                            case VERSION_4_16_BIT:                return read1632Bit(false);                            case VERSION_4_24_BIT:                bdata = new byte[width * height * 3];                read24Bit(bdata);                return new ImgRaw(width, height, 3, 8, bdata);                            case VERSION_4_32_BIT:                return read1632Bit(true);        }        return null;    }        private Image indexedModel(byte bdata[], int bpc, int paletteEntries) throws BadElementException {        Image img = new ImgRaw(width, height, 1, bpc, bdata);        PdfArray colorspace = new PdfArray();        colorspace.add(PdfName.INDEXED);        colorspace.add(PdfName.DEVICERGB);        byte np[] = getPalette(paletteEntries);        int len = np.length;        colorspace.add(new PdfNumber(len / 3 - 1));        colorspace.add(new PdfString(np));        PdfDictionary ad = new PdfDictionary();        ad.put(PdfName.COLORSPACE, colorspace);        img.setAdditional(ad);        return img;    }        private void readPalette(int sizeOfPalette) throws IOException {        if (sizeOfPalette == 0) {            return;        }        palette = new byte[sizeOfPalette];        int bytesRead = 0;        while (bytesRead < sizeOfPalette) {            int r = inputStream.read(palette, bytesRead, sizeOfPalette - bytesRead);            if (r < 0) {                throw new RuntimeException("incomplete palette");            }            bytesRead += r;        }        properties.put("palette", palette);    }        // Deal with 1 Bit images using IndexColorModels    private Image read1Bit(int paletteEntries) throws IOException, BadElementException {        byte bdata[] = new byte[((width + 7) / 8) * height];        int padding = 0;        int bytesPerScanline = (int)Math.ceil(width/8.0d);                int remainder = bytesPerScanline % 4;        if (remainder != 0) {            padding = 4 - remainder;        }                int imSize = (bytesPerScanline + padding) * height;                // Read till we have the whole image        byte values[] = new byte[imSize];        int bytesRead = 0;        while (bytesRead < imSize) {            bytesRead += inputStream.read(values, bytesRead,            imSize - bytesRead);        }                if (isBottomUp) {                        // Convert the bottom up image to a top down format by copying            // one scanline from the bottom to the top at a time.                        for (int i=0; i<height; i++) {                System.arraycopy(values,                imSize - (i+1)*(bytesPerScanline + padding),                bdata,                i*bytesPerScanline, bytesPerScanline);            }        } else {                        for (int i=0; i<height; i++) {                System.arraycopy(values,                i * (bytesPerScanline + padding),                bdata,                i * bytesPerScanline,                bytesPerScanline);            }        }        return indexedModel(bdata, 1, paletteEntries);    }        // Method to read a 4 bit BMP image data    private Image read4Bit(int paletteEntries) throws IOException, BadElementException {        byte bdata[] = new byte[((width + 1) / 2) * height];                // Padding bytes at the end of each scanline        int padding = 0;                int bytesPerScanline = (int)Math.ceil(width/2.0d);        int remainder = bytesPerScanline % 4;        if (remainder != 0) {            padding = 4 - remainder;        }                int imSize = (bytesPerScanline + padding) * height;                // Read till we have the whole image        byte values[] = new byte[imSize];        int bytesRead = 0;        while (bytesRead < imSize) {            bytesRead += inputStream.read(values, bytesRead,            imSize - bytesRead);        }                if (isBottomUp) {                        // Convert the bottom up image to a top down format by copying            // one scanline from the bottom to the top at a time.            for (int i=0; i<height; i++) {                System.arraycopy(values,                imSize - (i+1)*(bytesPerScanline + padding),                bdata,                i*bytesPerScanline,                bytesPerScanline);            }        } else {            for (int i=0; i<height; i++) {                System.arraycopy(values,                i * (bytesPerScanline + padding),                bdata,                i * bytesPerScanline,                bytesPerScanline);            }        }        return indexedModel(bdata, 4, paletteEntries);    }        // Method to read 8 bit BMP image data    private Image read8Bit(int paletteEntries) throws IOException, BadElementException {        byte bdata[] = new byte[width * height];        // Padding bytes at the end of each scanline        int padding = 0;                // width * bitsPerPixel should be divisible by 32        int bitsPerScanline = width * 8;        if ( bitsPerScanline%32 != 0) {            padding = (bitsPerScanline/32 + 1)*32 - bitsPerScanline;            padding = (int)Math.ceil(padding/8.0);        }                int imSize = (width + padding) * height;                // Read till we have the whole image        byte values[] = new byte[imSize];        int bytesRead = 0;        while (bytesRead < imSize) {            bytesRead += inputStream.read(values, bytesRead, imSize - bytesRead);        }                if (isBottomUp) {                        // Convert the bottom up image to a top down format by copying            // one scanline from the bottom to the top at a time.            for (int i=0; i<height; i++) {                System.arraycopy(values,                imSize - (i+1) * (width + padding),                bdata,                i * width,                width);            }        } else {            for (int i=0; i<height; i++) {                System.arraycopy(values,                i * (width + padding),                bdata,                i * width,                width);            }        }        return indexedModel(bdata, 8, paletteEntries);    }        // Method to read 24 bit BMP image data    private void read24Bit(byte[] bdata) {        // Padding bytes at the end of each scanline        int padding = 0;                // width * bitsPerPixel should be divisible by 32        int bitsPerScanline = width * 24;        if ( bitsPerScanline%32 != 0) {            padding = (bitsPerScanline/32 + 1)*32 - bitsPerScanline;            padding = (int)Math.ceil(padding/8.0);        }                        int imSize = ((width * 3 + 3) / 4 * 4) * height;        // Read till we have the whole image        byte values[] = new byte[imSize];        try {            int bytesRead = 0;            while (bytesRead < imSize) {                int r = inputStream.read(values, bytesRead,                imSize - bytesRead);                if (r < 0)                    break;                bytesRead += r;            }        } catch (IOException ioe) {            throw new ExceptionConverter(ioe);        }                int l=0, count;                if (isBottomUp) {            int max = width*height*3-1;                        count = -padding;            for (int i=0; i<height; i++) {                l = max - (i+1)*width*3 + 1;                count += padding;                for (int j=0; j<width; j++) {                    bdata[l + 2] = values[count++];                    bdata[l + 1] = values[count++];                    bdata[l] = values[count++];                    l += 3;                }

⌨️ 快捷键说明

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