Search in sources :

Example 36 with DataBufferByte

use of java.awt.image.DataBufferByte in project jdk8u_jdk by JetBrains.

the class GIFImageWriteParam method writeRasterData.

private void writeRasterData(RenderedImage image, Rectangle sourceBounds, Dimension destSize, ImageWriteParam param, boolean interlaceFlag) throws IOException {
    int sourceXOffset = sourceBounds.x;
    int sourceYOffset = sourceBounds.y;
    int sourceWidth = sourceBounds.width;
    int sourceHeight = sourceBounds.height;
    int destWidth = destSize.width;
    int destHeight = destSize.height;
    int periodX;
    int periodY;
    if (param == null) {
        periodX = 1;
        periodY = 1;
    } else {
        periodX = param.getSourceXSubsampling();
        periodY = param.getSourceYSubsampling();
    }
    SampleModel sampleModel = image.getSampleModel();
    int bitsPerPixel = sampleModel.getSampleSize()[0];
    int initCodeSize = bitsPerPixel;
    if (initCodeSize == 1) {
        initCodeSize++;
    }
    stream.write(initCodeSize);
    LZWCompressor compressor = new LZWCompressor(stream, initCodeSize, false);
    /* At this moment we know that input image is indexed image.
         * We can directly copy data iff:
         *   - no subsampling required (periodX = 1, periodY = 0)
         *   - we can access data directly (image is non-tiled,
         *     i.e. image data are in single block)
         *   - we can calculate offset in data buffer (next 3 lines)
         */
    boolean isOptimizedCase = periodX == 1 && periodY == 1 && image.getNumXTiles() == 1 && image.getNumYTiles() == 1 && sampleModel instanceof ComponentSampleModel && image.getTile(0, 0) instanceof ByteComponentRaster && image.getTile(0, 0).getDataBuffer() instanceof DataBufferByte;
    int numRowsWritten = 0;
    int progressReportRowPeriod = Math.max(destHeight / 20, 1);
    processImageStarted(imageIndex);
    if (interlaceFlag) {
        if (DEBUG)
            System.out.println("Writing interlaced");
        if (isOptimizedCase) {
            ByteComponentRaster tile = (ByteComponentRaster) image.getTile(0, 0);
            byte[] data = ((DataBufferByte) tile.getDataBuffer()).getData();
            ComponentSampleModel csm = (ComponentSampleModel) tile.getSampleModel();
            int offset = csm.getOffset(sourceXOffset, sourceYOffset, 0);
            // take into account the raster data offset
            offset += tile.getDataOffset(0);
            int lineStride = csm.getScanlineStride();
            writeRowsOpt(data, offset, lineStride, compressor, 0, 8, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += destHeight / 8;
            writeRowsOpt(data, offset, lineStride, compressor, 4, 8, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += (destHeight - 4) / 8;
            writeRowsOpt(data, offset, lineStride, compressor, 2, 4, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += (destHeight - 2) / 4;
            writeRowsOpt(data, offset, lineStride, compressor, 1, 2, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
        } else {
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset, 8 * periodY, sourceWidth, 0, 8, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += destHeight / 8;
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset + 4 * periodY, 8 * periodY, sourceWidth, 4, 8, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += (destHeight - 4) / 8;
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset + 2 * periodY, 4 * periodY, sourceWidth, 2, 4, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
            if (abortRequested()) {
                return;
            }
            numRowsWritten += (destHeight - 2) / 4;
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset + periodY, 2 * periodY, sourceWidth, 1, 2, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
        }
    } else {
        if (DEBUG)
            System.out.println("Writing non-interlaced");
        if (isOptimizedCase) {
            Raster tile = image.getTile(0, 0);
            byte[] data = ((DataBufferByte) tile.getDataBuffer()).getData();
            ComponentSampleModel csm = (ComponentSampleModel) tile.getSampleModel();
            int offset = csm.getOffset(sourceXOffset, sourceYOffset, 0);
            int lineStride = csm.getScanlineStride();
            writeRowsOpt(data, offset, lineStride, compressor, 0, 1, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
        } else {
            writeRows(image, compressor, sourceXOffset, periodX, sourceYOffset, periodY, sourceWidth, 0, 1, destWidth, destHeight, numRowsWritten, progressReportRowPeriod);
        }
    }
    if (abortRequested()) {
        return;
    }
    processImageProgress(100.0F);
    compressor.flush();
    stream.write(0x00);
    processImageComplete();
}
Also used : LZWCompressor(com.sun.imageio.plugins.common.LZWCompressor) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) ByteComponentRaster(sun.awt.image.ByteComponentRaster) Raster(java.awt.image.Raster) ByteComponentRaster(sun.awt.image.ByteComponentRaster) WritableRaster(java.awt.image.WritableRaster) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBufferByte(java.awt.image.DataBufferByte)

Example 37 with DataBufferByte

use of java.awt.image.DataBufferByte in project jdk8u_jdk by JetBrains.

the class WBMPImageReader method read.

public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    if (iis == null) {
        throw new IllegalStateException(I18N.getString("WBMPImageReader1"));
    }
    checkIndex(imageIndex);
    clearAbortRequest();
    processImageStarted(imageIndex);
    if (param == null)
        param = getDefaultReadParam();
    //read header
    readHeader();
    Rectangle sourceRegion = new Rectangle(0, 0, 0, 0);
    Rectangle destinationRegion = new Rectangle(0, 0, 0, 0);
    computeRegions(param, this.width, this.height, param.getDestination(), sourceRegion, destinationRegion);
    int scaleX = param.getSourceXSubsampling();
    int scaleY = param.getSourceYSubsampling();
    int xOffset = param.getSubsamplingXOffset();
    int yOffset = param.getSubsamplingYOffset();
    // If the destination is provided, then use it.  Otherwise, create new one
    BufferedImage bi = param.getDestination();
    if (bi == null)
        bi = new BufferedImage(destinationRegion.x + destinationRegion.width, destinationRegion.y + destinationRegion.height, BufferedImage.TYPE_BYTE_BINARY);
    boolean noTransform = destinationRegion.equals(new Rectangle(0, 0, width, height)) && destinationRegion.equals(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
    // Get the image data.
    WritableRaster tile = bi.getWritableTile(0, 0);
    // Get the SampleModel.
    MultiPixelPackedSampleModel sm = (MultiPixelPackedSampleModel) bi.getSampleModel();
    if (noTransform) {
        if (abortRequested()) {
            processReadAborted();
            return bi;
        }
        // If noTransform is necessary, read the data.
        iis.read(((DataBufferByte) tile.getDataBuffer()).getData(), 0, height * sm.getScanlineStride());
        processImageUpdate(bi, 0, 0, width, height, 1, 1, new int[] { 0 });
        processImageProgress(100.0F);
    } else {
        int len = (this.width + 7) / 8;
        byte[] buf = new byte[len];
        byte[] data = ((DataBufferByte) tile.getDataBuffer()).getData();
        int lineStride = sm.getScanlineStride();
        iis.skipBytes(len * sourceRegion.y);
        int skipLength = len * (scaleY - 1);
        // cache the values to avoid duplicated computation
        int[] srcOff = new int[destinationRegion.width];
        int[] destOff = new int[destinationRegion.width];
        int[] srcPos = new int[destinationRegion.width];
        int[] destPos = new int[destinationRegion.width];
        for (int i = destinationRegion.x, x = sourceRegion.x, j = 0; i < destinationRegion.x + destinationRegion.width; i++, j++, x += scaleX) {
            srcPos[j] = x >> 3;
            srcOff[j] = 7 - (x & 7);
            destPos[j] = i >> 3;
            destOff[j] = 7 - (i & 7);
        }
        for (int j = 0, y = sourceRegion.y, k = destinationRegion.y * lineStride; j < destinationRegion.height; j++, y += scaleY) {
            if (abortRequested())
                break;
            iis.read(buf, 0, len);
            for (int i = 0; i < destinationRegion.width; i++) {
                //get the bit and assign to the data buffer of the raster
                int v = (buf[srcPos[i]] >> srcOff[i]) & 1;
                data[k + destPos[i]] |= v << destOff[i];
            }
            k += lineStride;
            iis.skipBytes(skipLength);
            processImageUpdate(bi, 0, j, destinationRegion.width, 1, 1, 1, new int[] { 0 });
            processImageProgress(100.0F * j / destinationRegion.height);
        }
    }
    if (abortRequested())
        processReadAborted();
    else
        processImageComplete();
    return bi;
}
Also used : WritableRaster(java.awt.image.WritableRaster) Rectangle(java.awt.Rectangle) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage)

Example 38 with DataBufferByte

use of java.awt.image.DataBufferByte in project jdk8u_jdk by JetBrains.

the class WBMPImageWriter method write.

public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
    if (stream == null) {
        throw new IllegalStateException(I18N.getString("WBMPImageWriter3"));
    }
    if (image == null) {
        throw new IllegalArgumentException(I18N.getString("WBMPImageWriter4"));
    }
    clearAbortRequest();
    processImageStarted(0);
    if (param == null)
        param = getDefaultWriteParam();
    RenderedImage input = null;
    Raster inputRaster = null;
    boolean writeRaster = image.hasRaster();
    Rectangle sourceRegion = param.getSourceRegion();
    SampleModel sampleModel = null;
    if (writeRaster) {
        inputRaster = image.getRaster();
        sampleModel = inputRaster.getSampleModel();
    } else {
        input = image.getRenderedImage();
        sampleModel = input.getSampleModel();
        inputRaster = input.getData();
    }
    checkSampleModel(sampleModel);
    if (sourceRegion == null)
        sourceRegion = inputRaster.getBounds();
    else
        sourceRegion = sourceRegion.intersection(inputRaster.getBounds());
    if (sourceRegion.isEmpty())
        throw new RuntimeException(I18N.getString("WBMPImageWriter1"));
    int scaleX = param.getSourceXSubsampling();
    int scaleY = param.getSourceYSubsampling();
    int xOffset = param.getSubsamplingXOffset();
    int yOffset = param.getSubsamplingYOffset();
    sourceRegion.translate(xOffset, yOffset);
    sourceRegion.width -= xOffset;
    sourceRegion.height -= yOffset;
    int minX = sourceRegion.x / scaleX;
    int minY = sourceRegion.y / scaleY;
    int w = (sourceRegion.width + scaleX - 1) / scaleX;
    int h = (sourceRegion.height + scaleY - 1) / scaleY;
    Rectangle destinationRegion = new Rectangle(minX, minY, w, h);
    sampleModel = sampleModel.createCompatibleSampleModel(w, h);
    SampleModel destSM = sampleModel;
    // If the data are not formatted nominally then reformat.
    if (sampleModel.getDataType() != DataBuffer.TYPE_BYTE || !(sampleModel instanceof MultiPixelPackedSampleModel) || ((MultiPixelPackedSampleModel) sampleModel).getDataBitOffset() != 0) {
        destSM = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, 1, w + 7 >> 3, 0);
    }
    if (!destinationRegion.equals(sourceRegion)) {
        if (scaleX == 1 && scaleY == 1)
            inputRaster = inputRaster.createChild(inputRaster.getMinX(), inputRaster.getMinY(), w, h, minX, minY, null);
        else {
            WritableRaster ras = Raster.createWritableRaster(destSM, new Point(minX, minY));
            byte[] data = ((DataBufferByte) ras.getDataBuffer()).getData();
            for (int j = minY, y = sourceRegion.y, k = 0; j < minY + h; j++, y += scaleY) {
                for (int i = 0, x = sourceRegion.x; i < w; i++, x += scaleX) {
                    int v = inputRaster.getSample(x, y, 0);
                    data[k + (i >> 3)] |= v << (7 - (i & 7));
                }
                k += w + 7 >> 3;
            }
            inputRaster = ras;
        }
    }
    // If the data are not formatted nominally then reformat.
    if (!destSM.equals(inputRaster.getSampleModel())) {
        WritableRaster raster = Raster.createWritableRaster(destSM, new Point(inputRaster.getMinX(), inputRaster.getMinY()));
        raster.setRect(inputRaster);
        inputRaster = raster;
    }
    // Check whether the image is white-is-zero.
    boolean isWhiteZero = false;
    if (!writeRaster && input.getColorModel() instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) input.getColorModel();
        isWhiteZero = icm.getRed(0) > icm.getRed(1);
    }
    // Get the line stride, bytes per row, and data array.
    int lineStride = ((MultiPixelPackedSampleModel) destSM).getScanlineStride();
    int bytesPerRow = (w + 7) / 8;
    byte[] bdata = ((DataBufferByte) inputRaster.getDataBuffer()).getData();
    // Write WBMP header.
    // TypeField
    stream.write(0);
    // FixHeaderField
    stream.write(0);
    // width
    stream.write(intToMultiByte(w));
    // height
    stream.write(intToMultiByte(h));
    // Write the data.
    if (!isWhiteZero && lineStride == bytesPerRow) {
        // Write the entire image.
        stream.write(bdata, 0, h * bytesPerRow);
        processImageProgress(100.0F);
    } else {
        // Write the image row-by-row.
        int offset = 0;
        if (!isWhiteZero) {
            // Black-is-zero
            for (int row = 0; row < h; row++) {
                if (abortRequested())
                    break;
                stream.write(bdata, offset, bytesPerRow);
                offset += lineStride;
                processImageProgress(100.0F * row / h);
            }
        } else {
            // White-is-zero: need to invert data.
            byte[] inverted = new byte[bytesPerRow];
            for (int row = 0; row < h; row++) {
                if (abortRequested())
                    break;
                for (int col = 0; col < bytesPerRow; col++) {
                    inverted[col] = (byte) (~(bdata[col + offset]));
                }
                stream.write(inverted, 0, bytesPerRow);
                offset += lineStride;
                processImageProgress(100.0F * row / h);
            }
        }
    }
    if (abortRequested())
        processWriteAborted();
    else {
        processImageComplete();
        stream.flushBefore(stream.getStreamPosition());
    }
}
Also used : Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) Rectangle(java.awt.Rectangle) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) WritableRaster(java.awt.image.WritableRaster) RenderedImage(java.awt.image.RenderedImage) IndexColorModel(java.awt.image.IndexColorModel)

Example 39 with DataBufferByte

use of java.awt.image.DataBufferByte in project jdk8u_jdk by JetBrains.

the class ImageUtil method setPackedBinaryData.

/**
     * Sets the supplied <code>Raster</code>'s data from an array
     * of packed binary data of the form returned by
     * <code>getPackedBinaryData()</code>.
     *
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
     * <code>false</code> with the <code>SampleModel</code> of the
     * supplied <code>Raster</code> as argument.
     */
public static void setPackedBinaryData(byte[] binaryDataArray, WritableRaster raster, Rectangle rect) {
    SampleModel sm = raster.getSampleModel();
    if (!isBinary(sm)) {
        throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
    }
    int rectX = rect.x;
    int rectY = rect.y;
    int rectWidth = rect.width;
    int rectHeight = rect.height;
    DataBuffer dataBuffer = raster.getDataBuffer();
    int dx = rectX - raster.getSampleModelTranslateX();
    int dy = rectY - raster.getSampleModelTranslateY();
    MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel) sm;
    int lineStride = mpp.getScanlineStride();
    int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
    int bitOffset = mpp.getBitOffset(dx);
    int b = 0;
    if (bitOffset == 0) {
        if (dataBuffer instanceof DataBufferByte) {
            byte[] data = ((DataBufferByte) dataBuffer).getData();
            if (data == binaryDataArray) {
                // Optimal case: simply return.
                return;
            }
            int stride = (rectWidth + 7) / 8;
            int offset = 0;
            for (int y = 0; y < rectHeight; y++) {
                System.arraycopy(binaryDataArray, offset, data, eltOffset, stride);
                offset += stride;
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
            short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int xRemaining = rectWidth;
                int i = eltOffset;
                while (xRemaining > 8) {
                    data[i++] = (short) (((binaryDataArray[b++] & 0xFF) << 8) | (binaryDataArray[b++] & 0xFF));
                    xRemaining -= 16;
                }
                if (xRemaining > 0) {
                    data[i++] = (short) ((binaryDataArray[b++] & 0xFF) << 8);
                }
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferInt) {
            int[] data = ((DataBufferInt) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int xRemaining = rectWidth;
                int i = eltOffset;
                while (xRemaining > 24) {
                    data[i++] = (int) (((binaryDataArray[b++] & 0xFF) << 24) | ((binaryDataArray[b++] & 0xFF) << 16) | ((binaryDataArray[b++] & 0xFF) << 8) | (binaryDataArray[b++] & 0xFF));
                    xRemaining -= 32;
                }
                int shift = 24;
                while (xRemaining > 0) {
                    data[i] |= (int) ((binaryDataArray[b++] & 0xFF) << shift);
                    shift -= 8;
                    xRemaining -= 8;
                }
                eltOffset += lineStride;
            }
        }
    } else {
        // bitOffset != 0
        int stride = (rectWidth + 7) / 8;
        int offset = 0;
        if (dataBuffer instanceof DataBufferByte) {
            byte[] data = ((DataBufferByte) dataBuffer).getData();
            if ((bitOffset & 7) == 0) {
                for (int y = 0; y < rectHeight; y++) {
                    System.arraycopy(binaryDataArray, offset, data, eltOffset, stride);
                    offset += stride;
                    eltOffset += lineStride;
                }
            } else {
                // bitOffset % 8 != 0
                int rightShift = bitOffset & 7;
                int leftShift = 8 - rightShift;
                int leftShift8 = 8 + leftShift;
                int mask = (byte) (255 << leftShift);
                int mask1 = (byte) ~mask;
                for (int y = 0; y < rectHeight; y++) {
                    int i = eltOffset;
                    int xRemaining = rectWidth;
                    while (xRemaining > 0) {
                        byte datum = binaryDataArray[b++];
                        if (xRemaining > leftShift8) {
                            // when all the bits in this BYTE will be set
                            // into the data buffer.
                            data[i] = (byte) ((data[i] & mask) | ((datum & 0xFF) >>> rightShift));
                            data[++i] = (byte) ((datum & 0xFF) << leftShift);
                        } else if (xRemaining > leftShift) {
                            // All the "leftShift" high bits will be set
                            // into the data buffer.  But not all the
                            // "rightShift" low bits will be set.
                            data[i] = (byte) ((data[i] & mask) | ((datum & 0xFF) >>> rightShift));
                            i++;
                            data[i] = (byte) ((data[i] & mask1) | ((datum & 0xFF) << leftShift));
                        } else {
                            // Less than "leftShift" high bits will be set.
                            int remainMask = (1 << leftShift - xRemaining) - 1;
                            data[i] = (byte) ((data[i] & (mask | remainMask)) | (datum & 0xFF) >>> rightShift & ~remainMask);
                        }
                        xRemaining -= 8;
                    }
                    eltOffset += lineStride;
                }
            }
        } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
            short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
            int rightShift = bitOffset & 7;
            int leftShift = 8 - rightShift;
            int leftShift16 = 16 + leftShift;
            int mask = (short) (~(255 << leftShift));
            int mask1 = (short) (65535 << leftShift);
            int mask2 = (short) ~mask1;
            for (int y = 0; y < rectHeight; y++) {
                int bOffset = bitOffset;
                int xRemaining = rectWidth;
                for (int x = 0; x < rectWidth; x += 8, bOffset += 8, xRemaining -= 8) {
                    int i = eltOffset + (bOffset >> 4);
                    int mod = bOffset & 15;
                    int datum = binaryDataArray[b++] & 0xFF;
                    if (mod <= 8) {
                        // This BYTE is set into one SHORT
                        if (xRemaining < 8) {
                            // Mask the bits to be set.
                            datum &= 255 << 8 - xRemaining;
                        }
                        data[i] = (short) ((data[i] & mask) | (datum << leftShift));
                    } else if (xRemaining > leftShift16) {
                        // This BYTE will be set into two SHORTs
                        data[i] = (short) ((data[i] & mask1) | ((datum >>> rightShift) & 0xFFFF));
                        data[++i] = (short) ((datum << leftShift) & 0xFFFF);
                    } else if (xRemaining > leftShift) {
                        // This BYTE will be set into two SHORTs;
                        // But not all the low bits will be set into SHORT
                        data[i] = (short) ((data[i] & mask1) | ((datum >>> rightShift) & 0xFFFF));
                        i++;
                        data[i] = (short) ((data[i] & mask2) | ((datum << leftShift) & 0xFFFF));
                    } else {
                        // Only some of the high bits will be set into
                        // SHORTs
                        int remainMask = (1 << leftShift - xRemaining) - 1;
                        data[i] = (short) ((data[i] & (mask1 | remainMask)) | ((datum >>> rightShift) & 0xFFFF & ~remainMask));
                    }
                }
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferInt) {
            int[] data = ((DataBufferInt) dataBuffer).getData();
            int rightShift = bitOffset & 7;
            int leftShift = 8 - rightShift;
            int leftShift32 = 32 + leftShift;
            int mask = 0xFFFFFFFF << leftShift;
            int mask1 = ~mask;
            for (int y = 0; y < rectHeight; y++) {
                int bOffset = bitOffset;
                int xRemaining = rectWidth;
                for (int x = 0; x < rectWidth; x += 8, bOffset += 8, xRemaining -= 8) {
                    int i = eltOffset + (bOffset >> 5);
                    int mod = bOffset & 31;
                    int datum = binaryDataArray[b++] & 0xFF;
                    if (mod <= 24) {
                        // This BYTE is set into one INT
                        int shift = 24 - mod;
                        if (xRemaining < 8) {
                            // Mask the bits to be set.
                            datum &= 255 << 8 - xRemaining;
                        }
                        data[i] = (data[i] & (~(255 << shift))) | (datum << shift);
                    } else if (xRemaining > leftShift32) {
                        // All the bits of this BYTE will be set into two INTs
                        data[i] = (data[i] & mask) | (datum >>> rightShift);
                        data[++i] = datum << leftShift;
                    } else if (xRemaining > leftShift) {
                        // This BYTE will be set into two INTs;
                        // But not all the low bits will be set into INT
                        data[i] = (data[i] & mask) | (datum >>> rightShift);
                        i++;
                        data[i] = (data[i] & mask1) | (datum << leftShift);
                    } else {
                        // Only some of the high bits will be set into INT
                        int remainMask = (1 << leftShift - xRemaining) - 1;
                        data[i] = (data[i] & (mask | remainMask)) | (datum >>> rightShift & ~remainMask);
                    }
                }
                eltOffset += lineStride;
            }
        }
    }
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) DataBufferUShort(java.awt.image.DataBufferUShort) Point(java.awt.Point) DataBuffer(java.awt.image.DataBuffer)

Example 40 with DataBufferByte

use of java.awt.image.DataBufferByte in project jdk8u_jdk by JetBrains.

the class ImageUtil method getPackedBinaryData.

/**
     * For the case of binary data (<code>isBinary()</code> returns
     * <code>true</code>), return the binary data as a packed byte array.
     * The data will be packed as eight bits per byte with no bit offset,
     * i.e., the first bit in each image line will be the left-most of the
     * first byte of the line.  The line stride in bytes will be
     * <code>(int)((getWidth()+7)/8)</code>.  The length of the returned
     * array will be the line stride multiplied by <code>getHeight()</code>
     *
     * @return the binary data as a packed array of bytes with zero offset
     * of <code>null</code> if the data are not binary.
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
     * <code>false</code> with the <code>SampleModel</code> of the
     * supplied <code>Raster</code> as argument.
     */
public static byte[] getPackedBinaryData(Raster raster, Rectangle rect) {
    SampleModel sm = raster.getSampleModel();
    if (!isBinary(sm)) {
        throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
    }
    int rectX = rect.x;
    int rectY = rect.y;
    int rectWidth = rect.width;
    int rectHeight = rect.height;
    DataBuffer dataBuffer = raster.getDataBuffer();
    int dx = rectX - raster.getSampleModelTranslateX();
    int dy = rectY - raster.getSampleModelTranslateY();
    MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel) sm;
    int lineStride = mpp.getScanlineStride();
    int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
    int bitOffset = mpp.getBitOffset(dx);
    int numBytesPerRow = (rectWidth + 7) / 8;
    if (dataBuffer instanceof DataBufferByte && eltOffset == 0 && bitOffset == 0 && numBytesPerRow == lineStride && ((DataBufferByte) dataBuffer).getData().length == numBytesPerRow * rectHeight) {
        return ((DataBufferByte) dataBuffer).getData();
    }
    byte[] binaryDataArray = new byte[numBytesPerRow * rectHeight];
    int b = 0;
    if (bitOffset == 0) {
        if (dataBuffer instanceof DataBufferByte) {
            byte[] data = ((DataBufferByte) dataBuffer).getData();
            int stride = numBytesPerRow;
            int offset = 0;
            for (int y = 0; y < rectHeight; y++) {
                System.arraycopy(data, eltOffset, binaryDataArray, offset, stride);
                offset += stride;
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
            short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int xRemaining = rectWidth;
                int i = eltOffset;
                while (xRemaining > 8) {
                    short datum = data[i++];
                    binaryDataArray[b++] = (byte) ((datum >>> 8) & 0xFF);
                    binaryDataArray[b++] = (byte) (datum & 0xFF);
                    xRemaining -= 16;
                }
                if (xRemaining > 0) {
                    binaryDataArray[b++] = (byte) ((data[i] >>> 8) & 0XFF);
                }
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferInt) {
            int[] data = ((DataBufferInt) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int xRemaining = rectWidth;
                int i = eltOffset;
                while (xRemaining > 24) {
                    int datum = data[i++];
                    binaryDataArray[b++] = (byte) ((datum >>> 24) & 0xFF);
                    binaryDataArray[b++] = (byte) ((datum >>> 16) & 0xFF);
                    binaryDataArray[b++] = (byte) ((datum >>> 8) & 0xFF);
                    binaryDataArray[b++] = (byte) (datum & 0xFF);
                    xRemaining -= 32;
                }
                int shift = 24;
                while (xRemaining > 0) {
                    binaryDataArray[b++] = (byte) ((data[i] >>> shift) & 0xFF);
                    shift -= 8;
                    xRemaining -= 8;
                }
                eltOffset += lineStride;
            }
        }
    } else {
        // bitOffset != 0
        if (dataBuffer instanceof DataBufferByte) {
            byte[] data = ((DataBufferByte) dataBuffer).getData();
            if ((bitOffset & 7) == 0) {
                int stride = numBytesPerRow;
                int offset = 0;
                for (int y = 0; y < rectHeight; y++) {
                    System.arraycopy(data, eltOffset, binaryDataArray, offset, stride);
                    offset += stride;
                    eltOffset += lineStride;
                }
            } else {
                // bitOffset % 8 != 0
                int leftShift = bitOffset & 7;
                int rightShift = 8 - leftShift;
                for (int y = 0; y < rectHeight; y++) {
                    int i = eltOffset;
                    int xRemaining = rectWidth;
                    while (xRemaining > 0) {
                        if (xRemaining > rightShift) {
                            binaryDataArray[b++] = (byte) (((data[i++] & 0xFF) << leftShift) | ((data[i] & 0xFF) >>> rightShift));
                        } else {
                            binaryDataArray[b++] = (byte) ((data[i] & 0xFF) << leftShift);
                        }
                        xRemaining -= 8;
                    }
                    eltOffset += lineStride;
                }
            }
        } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
            short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int bOffset = bitOffset;
                for (int x = 0; x < rectWidth; x += 8, bOffset += 8) {
                    int i = eltOffset + bOffset / 16;
                    int mod = bOffset % 16;
                    int left = data[i] & 0xFFFF;
                    if (mod <= 8) {
                        binaryDataArray[b++] = (byte) (left >>> (8 - mod));
                    } else {
                        int delta = mod - 8;
                        int right = data[i + 1] & 0xFFFF;
                        binaryDataArray[b++] = (byte) ((left << delta) | (right >>> (16 - delta)));
                    }
                }
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferInt) {
            int[] data = ((DataBufferInt) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int bOffset = bitOffset;
                for (int x = 0; x < rectWidth; x += 8, bOffset += 8) {
                    int i = eltOffset + bOffset / 32;
                    int mod = bOffset % 32;
                    int left = data[i];
                    if (mod <= 24) {
                        binaryDataArray[b++] = (byte) (left >>> (24 - mod));
                    } else {
                        int delta = mod - 24;
                        int right = data[i + 1];
                        binaryDataArray[b++] = (byte) ((left << delta) | (right >>> (32 - delta)));
                    }
                }
                eltOffset += lineStride;
            }
        }
    }
    return binaryDataArray;
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) DataBufferUShort(java.awt.image.DataBufferUShort) Point(java.awt.Point) DataBuffer(java.awt.image.DataBuffer)

Aggregations

DataBufferByte (java.awt.image.DataBufferByte)64 BufferedImage (java.awt.image.BufferedImage)38 Point (java.awt.Point)36 WritableRaster (java.awt.image.WritableRaster)18 DataBufferInt (java.awt.image.DataBufferInt)14 DataBuffer (java.awt.image.DataBuffer)13 DataBufferShort (java.awt.image.DataBufferShort)10 UShortImage (cbit.vcell.VirtualMicroscopy.UShortImage)8 Rectangle (java.awt.Rectangle)8 SampleModel (java.awt.image.SampleModel)8 DataBufferUShort (java.awt.image.DataBufferUShort)7 IndexColorModel (java.awt.image.IndexColorModel)7 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)7 Graphics2D (java.awt.Graphics2D)6 ComponentSampleModel (java.awt.image.ComponentSampleModel)6 UserCancelException (org.vcell.util.UserCancelException)6 UtilCancelException (org.vcell.util.UtilCancelException)6 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)5 Hashtable (java.util.Hashtable)5 VCImage (cbit.image.VCImage)4