Search in sources :

Example 1 with LZWCompressor

use of com.sun.imageio.plugins.common.LZWCompressor 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)

Aggregations

LZWCompressor (com.sun.imageio.plugins.common.LZWCompressor)1 ComponentSampleModel (java.awt.image.ComponentSampleModel)1 DataBufferByte (java.awt.image.DataBufferByte)1 Raster (java.awt.image.Raster)1 SampleModel (java.awt.image.SampleModel)1 WritableRaster (java.awt.image.WritableRaster)1 ByteComponentRaster (sun.awt.image.ByteComponentRaster)1