xtifftilecodecimpl.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 762 行 · 第 1/2 页

JAVA
762
字号
                spixvals = _sdata;        }        if (decodePaletteAsShorts) {            int len = _sdata.length;            if (bitsPerSample[0] == 16)                spixvals = new short[len];            else                bpixvals = new byte[len];        }    }    /**     * Decode a rectangle of data stored in bpixels into a raster tile. Usually     * you will not need to override this, but instead implement the     * decodeTilePixels methods.     */    public WritableRaster decode(RenderedImage img, Rectangle newRect,                                 byte[] bpixels) {        if (image == null) {            setupSourceImage(img);        }        setupBufferForDecoding(); // set up every time        unitsInThisTile = newRect.width * newRect.height * numBands;        // uncompress data        decodeTilePixels(bpixels, newRect);        // post-processing of color data        decodeColor(newRect);        // put buffer into a tile        return setTilePixels(newRect);    }    /**     * Postprocess the uncompressed color data into the appropriate display     * color model. This implementation Does a number of things:     * <ul>     * <li> For RGB color, reverse to BGR which apparently is faster for Java 2D     * display     * <li> For one-bit WHITE_IS_ZERO data, flip the values so that they will     * look correct     * <li> If the decodePaletteAsShorts flag is true then unpack the bits and     * apply the lookup table, as 16-bit lookup is not supported in JAI.     * </ul>     * Override this if you have other color types.     *      * @see XTIFFDecodeParam     */    protected void decodeColor(Rectangle newRect) {        switch (dataType) {        case DataBuffer.TYPE_BYTE:            decodeColor(bpixvals, _bdata, newRect);            break;        case DataBuffer.TYPE_SHORT:        case DataBuffer.TYPE_USHORT:            if (bpixvals != null)                decodeColor(bpixvals, _sdata, newRect);            else                decodeColor(spixvals, _sdata, newRect);        }    }    /**     * Decode a tile of data into either byte or short pixel buffers. Override     * this if you have other buffer types (e.g. int)     */    protected void decodeTilePixels(byte[] bpixels, Rectangle newRect) {        // decodeTilePixels into the appropriate buffer        if (bpixvals != null)            decodeTilePixels(bpixels, newRect, bpixvals);        else            decodeTilePixels(bpixels, newRect, spixvals);    }    /**     * Take the values from the buffer and store them in a WritableRaster     * object.     */    protected WritableRaster setTilePixels(Rectangle rect) {        return (WritableRaster) RasterFactory.createWritableRaster(sampleModel,                buffer,                new Point((int) rect.getX(), (int) rect.getY()));    }    /**     * A useful Method to interpret a byte array as shorts. Method depends on     * whether the bytes are stored in a big endian or little endian format.     */    protected void unpackShorts(byte byteArray[], short output[], int shortCount) {        int j;        int firstByte, secondByte;        if (directory.isBigEndian()) {            for (int i = 0; i < shortCount; i++) {                j = 2 * i;                firstByte = byteArray[j] & 0xff;                secondByte = byteArray[j + 1] & 0xff;                output[i] = (short) ((firstByte << 8) + secondByte);            }        } else {            for (int i = 0; i < shortCount; i++) {                j = 2 * i;                firstByte = byteArray[j] & 0xff;                secondByte = byteArray[j + 1] & 0xff;                output[i] = (short) ((secondByte << 8) + firstByte);            }        }    }    // ////////////////////////////////////////////////////////////////////////    // /// Color decoding section    // ////////////////////////////////////////////////////////////////////////    /**     * Decode short pixel data, or interpret palette data as short from byte.     */    protected void decodeColor(byte[] bpix, short[] sdata, Rectangle newRect) {        // short sswap;        switch (image_type) {        case XTIFF.TYPE_PALETTE:            if (bitsPerSample[0] == 8) {                // At this point the data is 1 banded and will                // become 3 banded only after we've done the palette                // lookup, since unitsInThisTile was calculated with                // 3 bands, we need to divide this by 3.                int unitsBeforeLookup = unitsInThisTile / 3;                // Expand the palette image into an rgb image with ushort                // data type.                int cmapValue;                int count = 0, lookup, len = colormap.length / 3;                int len2 = len * 2;                for (int i = 0; i < unitsBeforeLookup; i++) {                    // Get the index into the colormap                    lookup = bpix[i] & 0xff;                    // Get the blue value                    cmapValue = colormap[lookup + len2];                    sdata[count++] = (short) (cmapValue & 0xffff);                    // Get the green value                    cmapValue = colormap[lookup + len];                    sdata[count++] = (short) (cmapValue & 0xffff);                    // Get the red value                    cmapValue = colormap[lookup];                    sdata[count++] = (short) (cmapValue & 0xffff);                }            } else if (bitsPerSample[0] == 4) {                int padding = newRect.width % 2;//                int bytesPostDecoding = ((newRect.width + 1) / 2)//                        * newRect.height;                int bytes = unitsInThisTile / 3;                // Unpack the 2 pixels packed into each byte.                byte[] data = new byte[bytes];                int srcCount = 0, dstCount = 0;                for (int j = 0; j < newRect.height; j++) {                    for (int i = 0; i < newRect.width / 2; i++) {                        data[dstCount++] = (byte) ((bpix[srcCount] & 0xf0) >> 4);                        data[dstCount++] = (byte) (bpix[srcCount++] & 0x0f);                    }                    if (padding == 1) {                        data[dstCount++] = (byte) ((bpix[srcCount++] & 0xf0) >> 4);                    }                }                int len = colormap.length / 3;                int len2 = len * 2;                int cmapValue, lookup;                int count = 0;                for (int i = 0; i < bytes; i++) {                    lookup = data[i] & 0xff;                    cmapValue = colormap[lookup + len2];                    sdata[count++] = (short) (cmapValue & 0xffff);                    cmapValue = colormap[lookup + len];                    sdata[count++] = (short) (cmapValue & 0xffff);                    cmapValue = colormap[lookup];                    sdata[count++] = (short) (cmapValue & 0xffff);                }            } else {                throw new RuntimeException(JaiI18N.getString("XTIFFImageDecoder7"));            }            break;        }    }    /**     * Decode short color data, or interpret palette data as short.     */    protected void decodeColor(short[] spix, short[] sdata, Rectangle newRect) {        short sswap;        switch (image_type) {        case XTIFF.TYPE_GREYSCALE_WHITE_IS_ZERO:        case XTIFF.TYPE_GREYSCALE_BLACK_IS_ZERO:            // Since we are using a ComponentColorModel with this image,            // we need to change the WhiteIsZero data to BlackIsZero data            // so it will display properly.            if (image_type == XTIFF.TYPE_GREYSCALE_WHITE_IS_ZERO) {                if (dataType == DataBuffer.TYPE_USHORT) {                    for (int l = 0; l < sdata.length; l++) {                        sdata[l] = (short) (65535 - spix[l]);                    }                } else if (dataType == DataBuffer.TYPE_SHORT) {                    for (int l = 0; l < sdata.length; l++) {                        sdata[l] = (short) (~spix[l]);                    }                }            }            break;        case XTIFF.TYPE_RGB:            // Change to BGR order, as Java2D displays that faster            for (int i = 0; i < unitsInThisTile; i += 3) {                sswap = spix[i];                sdata[i] = spix[i + 2];                sdata[i + 2] = sswap;            }            break;        case XTIFF.TYPE_ORGB:        case XTIFF.TYPE_ARGB_PRE:        case XTIFF.TYPE_ARGB:            // Change from RGBA to ABGR for Java2D's faster special cases            for (int i = 0; i < unitsInThisTile; i += 4) {                // Swap R and A                sswap = spix[i];                sdata[i] = spix[i + 3];                sdata[i + 3] = sswap;                // Swap G and B                sswap = spix[i + 1];                sdata[i + 1] = spix[i + 2];                sdata[i + 2] = sswap;            }            break;        case XTIFF.TYPE_RGB_EXTRA:            break;        case XTIFF.TYPE_PALETTE:            if (decodePaletteAsShorts) {                // At this point the data is 1 banded and will                // become 3 banded only after we've done the palette                // lookup, since unitsInThisTile was calculated with                // 3 bands, we need to divide this by 3.                int unitsBeforeLookup = unitsInThisTile / 3;                // Since unitsBeforeLookup is the number of shorts,                // but we do our decompression in terms of bytes, we                // need to multiply it by 2 in order to figure out                // how many bytes we'll get after decompression.//                int entries = unitsBeforeLookup * 2;                if (dataType == DataBuffer.TYPE_USHORT) {                    // Expand the palette image into an rgb image with ushort                    // data type.                    int cmapValue;                    int count = 0, lookup, len = colormap.length / 3;                    int len2 = len * 2;                    for (int i = 0; i < unitsBeforeLookup; i++) {                        // Get the index into the colormap                        lookup = spix[i] & 0xffff;                        // Get the blue value                        cmapValue = colormap[lookup + len2];                        sdata[count++] = (short) (cmapValue & 0xffff);                        // Get the green value                        cmapValue = colormap[lookup + len];                        sdata[count++] = (short) (cmapValue & 0xffff);                        // Get the red value                        cmapValue = colormap[lookup];                        sdata[count++] = (short) (cmapValue & 0xffff);                    }                } else if (dataType == DataBuffer.TYPE_SHORT) {                    // Expand the palette image into an rgb image with                    // short data type.                    int cmapValue;                    int count = 0, lookup, len = colormap.length / 3;                    int len2 = len * 2;                    for (int i = 0; i < unitsBeforeLookup; i++) {                        // Get the index into the colormap                        lookup = spix[i] & 0xffff;                        // Get the blue value                        cmapValue = colormap[lookup + len2];                        sdata[count++] = (short) cmapValue;                        // Get the green value                        cmapValue = colormap[lookup + len];                        sdata[count++] = (short) cmapValue;                        // Get the red value                        cmapValue = colormap[lookup];                        sdata[count++] = (short) cmapValue;                    }                }// dataType            }// decodePaletteAsShorts            break;        case XTIFF.TYPE_TRANS:            break;        }    }    /**     * Decode byte color data     */    protected void decodeColor(byte[] bpix, byte[] bdata, Rectangle newRect) {        byte bswap;        switch (image_type) {        case XTIFF.TYPE_BILEVEL_WHITE_IS_ZERO:        case XTIFF.TYPE_BILEVEL_BLACK_IS_ZERO:        case XTIFF.TYPE_GREYSCALE_WHITE_IS_ZERO:        case XTIFF.TYPE_GREYSCALE_BLACK_IS_ZERO:        case XTIFF.TYPE_RGB_EXTRA:        case XTIFF.TYPE_TRANS:            // nothing            break;        case XTIFF.TYPE_RGB:            if (bitsPerSample[0] == 8) {                // Change to BGR order, as Java2D displays that faster                for (int i = 0; i < unitsInThisTile; i += 3) {                    bswap = bpix[i];                    bdata[i] = bpix[i + 2];                    bdata[i + 2] = bswap;                }            }            break;        case XTIFF.TYPE_ORGB:        case XTIFF.TYPE_ARGB_PRE:        case XTIFF.TYPE_ARGB:            if (bitsPerSample[0] == 8) {                // Convert from RGBA to ABGR for Java2D                for (int i = 0; i < unitsInThisTile; i += 4) {                    // Swap R and A                    bswap = bpix[i];                    bdata[i] = bpix[i + 3];                    bdata[i + 3] = bswap;                    // Swap G and B                    bswap = bpix[i + 1];                    bdata[i + 1] = bpix[i + 2];                    bdata[i + 2] = bswap;                }            }            break;        case XTIFF.TYPE_PALETTE:            //             break;        }// switch    }// decodeColor}

⌨️ 快捷键说明

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