Search in sources :

Example 76 with SampleModel

use of java.awt.image.SampleModel in project imageio-ext by geosolutions-it.

the class JP2KKakaduWriteTest method testReducedMemory.

public static void testReducedMemory() throws IOException {
    if (!isKakaduAvailable) {
        LOGGER.warning("Kakadu libs not found: test are skipped ");
        return;
    }
    System.setProperty(JP2KKakaduImageWriter.MAX_BUFFER_SIZE_KEY, "64K");
    final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorModel cm = new ComponentColorModel(cs, new int[] { 16 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
    final int w = 512;
    final int h = 512;
    SampleModel sm = cm.createCompatibleSampleModel(w, h);
    final int bufferSize = w * h;
    final short[] bufferValues = new short[bufferSize];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) // bufferValues[j + (i * h)] = (short) ((j + i) * (65536 /
        // 1024));
        bufferValues[j + (i * h)] = (short) (Math.random() * 65535);
    }
    DataBuffer imageBuffer = new DataBufferUShort(bufferValues, bufferSize);
    BufferedImage bi = new BufferedImage(cm, Raster.createWritableRaster(sm, imageBuffer, null), false, null);
    write(outputFileName + "_RM", bi, true, lossLessQuality);
    write(outputFileName + "_RM", bi, false, lossLessQuality);
    write(outputFileName + "_RM", bi, true, lossyQuality);
    write(outputFileName + "_RM", bi, false, lossyQuality);
    LOGGER.info(writeOperations + " write operations performed");
}
Also used : SampleModel(java.awt.image.SampleModel) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) DataBufferUShort(java.awt.image.DataBufferUShort) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 77 with SampleModel

use of java.awt.image.SampleModel in project imageio-ext by geosolutions-it.

the class JP2KKakaduWriteTest method test16BitGray.

public static void test16BitGray() throws IOException {
    if (!isKakaduAvailable) {
        LOGGER.warning("Kakadu libs not found: test are skipped ");
        return;
    }
    final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorModel cm = new ComponentColorModel(cs, new int[] { 16 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
    final int w = 512;
    final int h = 512;
    SampleModel sm = cm.createCompatibleSampleModel(w, h);
    final int bufferSize = w * h;
    final short[] bufferValues = new short[bufferSize];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) // bufferValues[j + (i * h)] = (short) ((j + i) * (65536 /
        // 1024));
        bufferValues[j + (i * h)] = (short) (Math.random() * 65535);
    }
    DataBuffer imageBuffer = new DataBufferUShort(bufferValues, bufferSize);
    BufferedImage bi = new BufferedImage(cm, Raster.createWritableRaster(sm, imageBuffer, null), false, null);
    JP2KKakaduImageWriteParam param = new JP2KKakaduImageWriteParam();
    param.setSourceSubsampling(2, 3, 0, 0);
    write(outputFileName + "_gray16", bi, true, lossLessQuality);
    write(outputFileName + "_gray16", bi, false, lossLessQuality);
    write(outputFileName + "_gray16", bi, true, lossyQuality);
    write(outputFileName + "_gray16", bi, false, lossyQuality);
    write(outputFileName + "_JAI_gray16", bi, true, lossLessQuality, true);
    write(outputFileName + "_JAI_gray16", bi, false, lossLessQuality, true);
    write(outputFileName + "_JAI_subSampled_gray16", bi, true, lossyQuality, true, param);
    write(outputFileName + "_JAI_subSampled_gray16", bi, false, lossyQuality, true, param);
    LOGGER.info(writeOperations + " write operations performed");
}
Also used : SampleModel(java.awt.image.SampleModel) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) DataBufferUShort(java.awt.image.DataBufferUShort) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 78 with SampleModel

use of java.awt.image.SampleModel in project imageio-ext by geosolutions-it.

the class TIFFBaseJPEGCompressor method encode.

public final int encode(byte[] b, int off, int width, int height, int[] bitsPerSample, int scanlineStride) throws IOException {
    if (this.JPEGWriter == null) {
        throw new IIOException("JPEG writer has not been initialized!");
    }
    if (!((bitsPerSample.length == 3 && bitsPerSample[0] == 8 && bitsPerSample[1] == 8 && bitsPerSample[2] == 8) || (bitsPerSample.length == 1 && bitsPerSample[0] == 8))) {
        throw new IIOException("Can only JPEG compress 8- and 24-bit images!");
    }
    // Set the stream.
    ImageOutputStream ios;
    // usingCodecLib && !writeAbbreviatedStream
    long initialStreamPosition;
    if (usingCodecLib && !writeAbbreviatedStream) {
        ios = stream;
        initialStreamPosition = stream.getStreamPosition();
    } else {
        // is using a stream on the native side which cannot be reset.
        if (baos == null) {
            baos = new IIOByteArrayOutputStream();
        } else {
            baos.reset();
        }
        ios = new MemoryCacheImageOutputStream(baos);
        initialStreamPosition = 0L;
    }
    JPEGWriter.setOutput(ios);
    // Create a DataBuffer.
    DataBufferByte dbb;
    if (off == 0 || usingCodecLib) {
        dbb = new DataBufferByte(b, b.length);
    } else {
        // 
        // Workaround for bug in core Java Image I/O JPEG
        // ImageWriter which cannot handle non-zero offsets.
        // 
        int bytesPerSegment = scanlineStride * height;
        byte[] btmp = new byte[bytesPerSegment];
        System.arraycopy(b, off, btmp, 0, bytesPerSegment);
        dbb = new DataBufferByte(btmp, bytesPerSegment);
        off = 0;
    }
    // Set up the ColorSpace.
    int[] offsets;
    ColorSpace cs;
    if (bitsPerSample.length == 3) {
        offsets = new int[] { off, off + 1, off + 2 };
        cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    } else {
        offsets = new int[] { off };
        cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    }
    // Create the ColorModel.
    ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    // Create the SampleModel.
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerSample.length, scanlineStride, offsets);
    // Create the WritableRaster.
    WritableRaster wras = Raster.createWritableRaster(sm, dbb, new Point(0, 0));
    // Create the BufferedImage.
    BufferedImage bi = new BufferedImage(cm, wras, false, null);
    // Get the pruned JPEG image metadata (may be null).
    IIOMetadata imageMetadata = getImageMetadata(writeAbbreviatedStream);
    // Compress the image into the output stream.
    int compDataLength;
    if (usingCodecLib && !writeAbbreviatedStream) {
        // Write complete JPEG stream
        JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
        compDataLength = (int) (stream.getStreamPosition() - initialStreamPosition);
    } else {
        if (writeAbbreviatedStream) {
            // Write abbreviated JPEG stream
            // First write the tables-only data.
            JPEGWriter.prepareWriteSequence(JPEGStreamMetadata);
            ios.flush();
            // Rewind to the beginning of the byte array.
            baos.reset();
            // Write the abbreviated image data.
            IIOImage image = new IIOImage(bi, null, imageMetadata);
            JPEGWriter.writeToSequence(image, JPEGParam);
            JPEGWriter.endWriteSequence();
        } else {
            // Write complete JPEG stream
            JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
        }
        compDataLength = baos.size();
        baos.writeTo(stream);
        baos.reset();
    }
    return compDataLength;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) IIOException(javax.imageio.IIOException) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) SampleModel(java.awt.image.SampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Example 79 with SampleModel

use of java.awt.image.SampleModel in project imageio-ext by geosolutions-it.

the class ScanlineProviderFactory method getProvider.

public static ScanlineProvider getProvider(RenderedImage image) {
    ColorModel cm = image.getColorModel();
    SampleModel sm = image.getSampleModel();
    Raster raster;
    if (image instanceof BufferedImage) {
        raster = ((BufferedImage) image).getRaster();
        // a copy of the raster to get a data buffer we can scroll over without issues
        if (raster.getParent() != null) {
            raster = image.getData(new Rectangle(0, 0, raster.getWidth(), raster.getHeight()));
        }
    } else {
        // TODO: we could build a tile oriented reader that fetches tiles in parallel here
        raster = image.getData();
    }
    // grab the right scanline extractor based on image features
    if (cm instanceof ComponentColorModel && sm.getDataType() == DataBuffer.TYPE_BYTE) {
        if (sm.getNumBands() == 3 || sm.getNumBands() == 4) {
            return new RasterByteABGRProvider(raster, cm.hasAlpha());
        } else if (sm.getNumBands() == 2 && cm.hasAlpha()) {
            return new RasterByteGrayAlphaProvider(raster);
        } else if (sm.getNumBands() == 1) {
            if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
                if (sm instanceof MultiPixelPackedSampleModel) {
                    if (cm.getPixelSize() == 8) {
                        return new RasterByteSingleBandProvider(raster, 8, raster.getWidth());
                    } else if (cm.getPixelSize() == 4) {
                        int scanlineLength = (raster.getWidth() + 1) / 2;
                        return new RasterByteSingleBandProvider(raster, 4, scanlineLength);
                    } else if (cm.getPixelSize() == 2) {
                        int scanlineLength = (raster.getWidth() + 2) / 4;
                        return new RasterByteSingleBandProvider(raster, 2, scanlineLength);
                    } else if (cm.getPixelSize() == 1) {
                        int scanlineLength = (raster.getWidth() + 4) / 8;
                        return new RasterByteSingleBandProvider(raster, 1, scanlineLength);
                    }
                } else {
                    if (cm.getPixelSize() == 8) {
                        return new RasterByteSingleBandProvider(raster, 8, raster.getWidth());
                    } else if (cm.getPixelSize() == 4) {
                        int scanlineLength = (raster.getWidth() + 1) / 2;
                        return new RasterByteRepackSingleBandProvider(raster, 4, scanlineLength);
                    } else if (cm.getPixelSize() == 2) {
                        int scanlineLength = (raster.getWidth() + 2) / 4;
                        return new RasterByteRepackSingleBandProvider(raster, 2, scanlineLength);
                    } else if (cm.getPixelSize() == 1) {
                        int scanlineLength = (raster.getWidth() + 4) / 8;
                        return new RasterByteRepackSingleBandProvider(raster, 1, scanlineLength);
                    }
                }
            }
        }
    } else if (cm instanceof ComponentColorModel && sm.getDataType() == DataBuffer.TYPE_USHORT) {
        if (sm.getNumBands() == 3 || sm.getNumBands() == 4) {
            return new RasterShortABGRProvider(raster, cm.hasAlpha());
        } else if (sm.getNumBands() == 2 && cm.hasAlpha()) {
            return new RasterShortGrayAlphaProvider(raster);
        } else if (sm.getNumBands() == 1) {
            return new RasterShortSingleBandProvider(raster);
        }
    } else if (cm instanceof DirectColorModel && sm.getDataType() == DataBuffer.TYPE_INT) {
        if (sm.getNumBands() == 3 || sm.getNumBands() == 4) {
            return new RasterIntABGRProvider(raster, cm.hasAlpha());
        }
    } else if (cm instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) cm;
        int pixelSize = icm.getPixelSize();
        // re-align to powers of two
        if ((pixelSize & (pixelSize - 1)) != 0) {
            int nextPower = (int) (Math.floor(Math.log(pixelSize) / Math.log(2)) + 1);
            pixelSize = (int) Math.pow(2, nextPower);
        }
        if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
            if (sm instanceof MultiPixelPackedSampleModel) {
                if (pixelSize == 8) {
                    return new RasterByteSingleBandProvider(raster, 8, raster.getWidth(), icm);
                } else if (pixelSize == 4) {
                    int scanlineLength = (raster.getWidth() + 1) / 2;
                    return new RasterByteSingleBandProvider(raster, 4, scanlineLength, icm);
                } else if (pixelSize == 2) {
                    int scanlineLength = (raster.getWidth() + 2) / 4;
                    return new RasterByteSingleBandProvider(raster, 2, scanlineLength, icm);
                } else if (pixelSize == 1) {
                    int scanlineLength = (raster.getWidth() + 4) / 8;
                    return new RasterByteSingleBandProvider(raster, 1, scanlineLength, icm);
                }
            } else {
                if (pixelSize == 8) {
                    return new RasterByteSingleBandProvider(raster, 8, raster.getWidth(), icm);
                } else if (pixelSize == 4) {
                    int scanlineLength = (raster.getWidth() + 1) / 2;
                    return new RasterByteRepackSingleBandProvider(raster, 4, scanlineLength, icm);
                } else if (pixelSize == 2) {
                    int scanlineLength = (raster.getWidth() + 2) / 4;
                    return new RasterByteRepackSingleBandProvider(raster, 2, scanlineLength, icm);
                } else if (pixelSize == 1) {
                    int scanlineLength = (raster.getWidth() + 4) / 8;
                    return new RasterByteRepackSingleBandProvider(raster, 1, scanlineLength, icm);
                }
            }
        } else if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
            if (sm instanceof MultiPixelPackedSampleModel) {
                if (pixelSize == 16) {
                    int scanlineLength = raster.getWidth() * 2;
                    return new RasterShortSingleBandProvider(raster, 16, scanlineLength, icm);
                } else if (pixelSize == 8) {
                    int scanlineLength = raster.getWidth() + ((raster.getWidth() % 2 == 0) ? 0 : 1);
                    return new RasterShortSingleBandProvider(raster, 8, scanlineLength, icm);
                } else if (pixelSize == 4) {
                    int scanlineLength = (raster.getWidth() + 1) / 2;
                    return new RasterShortSingleBandProvider(raster, 4, scanlineLength, icm);
                } else if (pixelSize == 2) {
                    int scanlineLength = (raster.getWidth() + 2) / 4;
                    return new RasterShortSingleBandProvider(raster, 2, scanlineLength, icm);
                } else if (pixelSize == 1) {
                    int scanlineLength = (raster.getWidth() + 4) / 8;
                    return new RasterShortSingleBandProvider(raster, 1, scanlineLength, icm);
                }
            }
        }
    }
    return null;
}
Also used : ComponentColorModel(java.awt.image.ComponentColorModel) Raster(java.awt.image.Raster) Rectangle(java.awt.Rectangle) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) BufferedImage(java.awt.image.BufferedImage) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 80 with SampleModel

use of java.awt.image.SampleModel in project imageio-ext by geosolutions-it.

the class JP2KKakaduImageReader method read.

/**
 * Read the image and returns it as a complete <code>BufferedImage</code>,
 * using a supplied <code>ImageReadParam</code>.
 *
 * @param imageIndex
 *                the index of the desired image.
 */
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    checkImageIndex(imageIndex);
    // ///////////////////////////////////////////////////////////
    // 
    // STEP 0.
    // -------
    // Setting local variables for i-th codestream
    // 
    // ///////////////////////////////////////////////////////////
    JP2KCodestreamProperties codestreamP = multipleCodestreams.get(imageIndex);
    final int maxAvailableQualityLayers = codestreamP.getMaxAvailableQualityLayers();
    final int[] componentIndexes = codestreamP.getComponentIndexes();
    final int nComponents = codestreamP.getNumComponents();
    final int maxBitDepth = codestreamP.getMaxBitDepth();
    final int height = codestreamP.getHeight();
    final int width = codestreamP.getWidth();
    final ColorModel cm = codestreamP.getColorModel();
    final SampleModel sm = codestreamP.getSampleModel();
    Kdu_simple_file_source localRawSource = null;
    Jp2_family_src localFamilySource = null;
    Jpx_source localWrappedSource = null;
    // get a default set of ImageReadParam if needed.
    if (param == null)
        param = getDefaultReadParam();
    // ///////////////////////////////////////////////////////////
    // 
    // STEP 1.
    // -------
    // local variables initialization
    // 
    // ///////////////////////////////////////////////////////////
    // The destination image properties
    final Rectangle destinationRegion = new Rectangle(0, 0, -1, -1);
    // The source region image properties
    final Rectangle sourceRegion = new Rectangle(0, 0, -1, -1);
    // The properties of the image we need to load from Kakadu
    final Rectangle requiredRegion = new Rectangle(0, 0, -1, -1);
    // Subsampling Factors
    int xSubsamplingFactor = -1;
    int ySubsamplingFactor = -1;
    // boolean used to specify when resampling is required (as an instance,
    // different subsampling factors (xSS != ySS) require resampling)
    boolean resamplingIsRequired = false;
    // ///////////////////////////////////////////////////////////
    if (!(param instanceof JP2KKakaduImageReadParam)) {
        // The parameter is not of JP2KakaduImageReadParam type but
        // simply an ImageReadParam instance (the superclass)
        // we need to build a proper JP2KakaduImageReadParam prior
        // to start parameters parsing.
        JP2KKakaduImageReadParam jp2kParam = (JP2KKakaduImageReadParam) getDefaultReadParam();
        jp2kParam.initialize(param);
        param = jp2kParam;
    }
    // selected interpolation type
    final int interpolationType = ((JP2KKakaduImageReadParam) param).getInterpolationType();
    // specified quality layers
    int qualityLayers = ((JP2KKakaduImageReadParam) param).getQualityLayers();
    if (qualityLayers != -1) {
        // qualityLayers != -1 means that the user have specified that value
        if (qualityLayers > maxAvailableQualityLayers)
            // correct the user defined qualityLayers parameter if this
            // exceed the max number of available quality layers
            qualityLayers = maxAvailableQualityLayers;
    } else
        qualityLayers = 0;
    // SubSampling variables initialization
    xSubsamplingFactor = param.getSourceXSubsampling();
    ySubsamplingFactor = param.getSourceYSubsampling();
    // //
    // Retrieving Information about Source Region and doing additional
    // initialization operations.
    // //
    computeRegions(width, height, param, sourceRegion, destinationRegion);
    // ////////////////////////////////////////////////////////////////////
    // 
    // STEP 3.
    // -------
    // check if the image need to be resampled/rescaled and find the proper
    // scale factor as well as the size of the required region we need to
    // provide to kakadu.
    // 
    // ////////////////////////////////////////////////////////////////////
    final int[] resolutionInfo = new int[2];
    resamplingIsRequired = getRequiredRegionsAndResolutions(codestreamP, xSubsamplingFactor, ySubsamplingFactor, sourceRegion, destinationRegion, requiredRegion, resolutionInfo);
    final int nDiscardLevels = resolutionInfo[1];
    // Setting the destination Buffer Size which will contains the samples
    // coming from stripe_decompressor
    BufferedImage bi = null;
    try {
        DataBuffer imageBuffer = null;
        // ////////////////////////////////////////////////////////////////
        // 
        // STEP 4.
        // -------
        // Initialize sources and codestream
        // ////////////////////////////////////////////////////////////////
        // Opening data source
        Kdu_codestream codestream = new Kdu_codestream();
        if (!isRawSource) {
            localFamilySource = new Jp2_family_src();
            localWrappedSource = new Jpx_source();
            localFamilySource.Open(fileName);
            localWrappedSource.Open(localFamilySource, true);
            final Jpx_codestream_source stream = localWrappedSource.Access_codestream(imageIndex);
            final Jpx_input_box inputbox = stream.Open_stream();
            codestream.Create(inputbox);
        } else {
            localRawSource = new Kdu_simple_file_source(fileName);
            codestream.Create(localRawSource);
        }
        // ////////////////////////////////////////////////////////////////
        // 
        // STEP 5.
        // -------
        // Set parameters for stripe decompression
        // ////////////////////////////////////////////////////////////////
        final Kdu_dims dims = new Kdu_dims();
        codestream.Apply_input_restrictions(0, nComponents, nDiscardLevels, qualityLayers, null, Kdu_global.KDU_WANT_OUTPUT_COMPONENTS);
        if (LOGGER.isLoggable(Level.FINE)) {
            final Kdu_dims original_dims = new Kdu_dims();
            codestream.Get_dims(-1, original_dims);
            StringBuilder sb = new StringBuilder("Original Hi-Res Image Region is: x=").append(original_dims.Access_pos().Get_x()).append("  y=").append(original_dims.Access_pos().Get_y()).append("  w=").append(original_dims.Access_size().Get_x()).append("  h=").append(original_dims.Access_size().Get_y());
            LOGGER.fine(sb.toString());
        }
        final Kdu_dims dimsROI = new Kdu_dims();
        codestream.Get_dims(0, dims);
        if (LOGGER.isLoggable(Level.FINE)) {
            final int mappedX = dims.Access_pos().Get_x();
            final int mappedY = dims.Access_pos().Get_y();
            final int mappedWidth = dims.Access_size().Get_x();
            final int mappedHeight = dims.Access_size().Get_y();
            StringBuilder sb = new StringBuilder("Mapped Region is: x=").append(mappedX).append("  y=").append(mappedY).append("  w=").append(mappedWidth).append("  h=").append(mappedHeight);
            LOGGER.fine(sb.toString());
        }
        // Checks on the required region and mapped region, in compliance with
        // Formula 11.1 of Taubman's JPEG2000 compression book.
        checkBounds(dims, requiredRegion);
        final int destBufferSize = (requiredRegion.height * requiredRegion.width) * nComponents;
        setDimsForCrop(dims, requiredRegion);
        // Getting a region of interest.
        codestream.Map_region(0, dims, dimsROI);
        if (LOGGER.isLoggable(Level.FINE)) {
            final int mappedROIWidth = dimsROI.Access_size().Get_x();
            final int mappedROIHeight = dimsROI.Access_size().Get_y();
            final int mappedROIX = dimsROI.Access_pos().Get_x();
            final int mappedROIY = dimsROI.Access_pos().Get_y();
            StringBuilder sb = new StringBuilder("ROI Region is: x=").append(mappedROIX).append("  y=").append(mappedROIY).append("  w=").append(mappedROIWidth).append("  h=").append(mappedROIHeight);
            LOGGER.fine(sb.toString());
        }
        codestream.Apply_input_restrictions(nComponents, componentIndexes, nDiscardLevels, qualityLayers, dimsROI, Kdu_global.KDU_WANT_OUTPUT_COMPONENTS);
        // //
        // 
        // Setting parameters for stripe decompression
        // 
        // //
        // Array with one entry for each image component, identifying the
        // number of lines to be decompressed for that component in the
        // present call. All entries must be non-negative.
        final int[] stripeHeights = new int[nComponents];
        // Array with one entry for each image component, identifying
        // the separation between horizontally adjacent samples within the
        // corresponding stripe buffer found in the stripe_bufs array.
        final int[] sampleGap = new int[nComponents];
        // Array with one entry for each image component, identifying
        // the separation between vertically adjacent samples within the
        // corresponding stripe buffer found in the stripe_bufs array.
        final int[] rowGap = new int[nComponents];
        // Array with one entry for each image component, identifying the
        // position of the first sample of that component within the buffer
        // array.
        final int[] sampleOffset = new int[nComponents];
        // Array with one entry for each image component, identifying the
        // number of significant bits used to represent each sample.
        // There is no implied connection between the precision values, P,
        // and the bit-depth, B, of each image component, as found in the
        // code-stream's SIZ marker segment, and returned via
        // kdu_codestream::get_bit_depth. The original image sample
        // bit-depth, B, may be larger or smaller than the value of P
        // supplied via the precisions argument. The samples returned by
        // pull_stripe all have a nominally signed representation unless
        // otherwise indicated by a non-NULL isSigned argument
        final int[] precision = new int[nComponents];
        for (int component = 0; component < nComponents; component++) {
            stripeHeights[component] = requiredRegion.height;
            sampleGap[component] = nComponents;
            rowGap[component] = requiredRegion.width * nComponents;
            sampleOffset[component] = component;
            precision[component] = codestream.Get_bit_depth(component);
        }
        // ////////////////////////////////////////////////////////////////
        // 
        // STEP 6.
        // -------
        // Stripe decompressor data loading
        // ////////////////////////////////////////////////////////////////
        Kdu_stripe_decompressor decompressor = new Kdu_stripe_decompressor();
        decompressor.Start(codestream);
        /**
         * @todo: actually, we only handle fixed point types. No float or
         *        double data are handled.
         */
        if (maxBitDepth <= 8) {
            final byte[] bufferValues = new byte[destBufferSize];
            decompressor.Pull_stripe(bufferValues, stripeHeights, sampleOffset, sampleGap, rowGap, precision);
            imageBuffer = new DataBufferByte(bufferValues, destBufferSize);
        } else if (maxBitDepth > 8 && maxBitDepth <= 16) {
            final boolean[] isSigned = new boolean[nComponents];
            for (int i = 0; i < isSigned.length; i++) isSigned[i] = codestream.Get_signed(i);
            final short[] bufferValues = new short[destBufferSize];
            decompressor.Pull_stripe(bufferValues, stripeHeights, sampleOffset, sampleGap, rowGap, precision, isSigned);
            imageBuffer = new DataBufferUShort(bufferValues, destBufferSize);
        } else if (maxBitDepth > 16 && maxBitDepth <= 32) {
            final int[] bufferValues = new int[destBufferSize];
            decompressor.Pull_stripe(bufferValues, stripeHeights, sampleOffset, sampleGap, rowGap, precision);
            imageBuffer = new DataBufferInt(bufferValues, destBufferSize);
        }
        // ////////////////////////////////////////////////////////////////
        // 
        // STEP 6.bis
        // ----------
        // Kakadu items deallocation/finalization
        // ////////////////////////////////////////////////////////////////
        decompressor.Finish();
        decompressor.Native_destroy();
        codestream.Destroy();
        // ////////////////////////////////////////////////////////////////
        // 
        // STEP 7.
        // -------
        // BufferedImage Creation
        // 
        // ////////////////////////////////////////////////////////////////
        final SampleModel sampleModel = sm.createCompatibleSampleModel(requiredRegion.width, requiredRegion.height);
        bi = new BufferedImage(cm, Raster.createWritableRaster(sampleModel, imageBuffer, null), false, null);
    } catch (KduException e) {
        throw new RuntimeException("Error caused by a Kakadu exception during creation of key objects! ", e);
    } catch (RasterFormatException rfe) {
        throw new RuntimeException("Error during raster creation", rfe);
    } finally {
        if (!isRawSource) {
            try {
                if (localWrappedSource.Exists())
                    localWrappedSource.Close();
            } catch (KduException e) {
            }
            localWrappedSource.Native_destroy();
            try {
                if (localFamilySource.Exists())
                    localFamilySource.Close();
            } catch (KduException e) {
            }
            localFamilySource.Native_destroy();
        } else {
            localRawSource.Native_destroy();
        }
        if (deleteInputFile && inputFile.exists()) {
            inputFile.delete();
        }
    }
    // ////////////////////////////////////////////////////////////////
    if (resamplingIsRequired && bi != null)
        return KakaduUtilities.subsampleImage(codestreamP.getColorModel(), bi, destinationRegion.width, destinationRegion.height, interpolationType);
    return bi;
}
Also used : Kdu_stripe_decompressor(kdu_jni.Kdu_stripe_decompressor) Rectangle(java.awt.Rectangle) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) Jpx_source(kdu_jni.Jpx_source) IndexColorModel(java.awt.image.IndexColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) Kdu_dims(kdu_jni.Kdu_dims) Kdu_codestream(kdu_jni.Kdu_codestream) DataBufferUShort(java.awt.image.DataBufferUShort) DataBuffer(java.awt.image.DataBuffer) KduException(kdu_jni.KduException) Kdu_simple_file_source(kdu_jni.Kdu_simple_file_source) Jpx_codestream_source(kdu_jni.Jpx_codestream_source) SampleModel(java.awt.image.SampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) Jpx_input_box(kdu_jni.Jpx_input_box) Jp2_family_src(kdu_jni.Jp2_family_src) RasterFormatException(java.awt.image.RasterFormatException)

Aggregations

SampleModel (java.awt.image.SampleModel)97 ColorModel (java.awt.image.ColorModel)43 ComponentSampleModel (java.awt.image.ComponentSampleModel)38 Point (java.awt.Point)33 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)33 DataBuffer (java.awt.image.DataBuffer)32 Rectangle (java.awt.Rectangle)30 IndexColorModel (java.awt.image.IndexColorModel)29 BufferedImage (java.awt.image.BufferedImage)27 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)26 WritableRaster (java.awt.image.WritableRaster)26 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)23 DataBufferByte (java.awt.image.DataBufferByte)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)18 ComponentColorModel (java.awt.image.ComponentColorModel)17 DataBufferInt (java.awt.image.DataBufferInt)17 ColorSpace (java.awt.color.ColorSpace)15 Raster (java.awt.image.Raster)15 DataBufferUShort (java.awt.image.DataBufferUShort)13 BandedSampleModel (java.awt.image.BandedSampleModel)12