Search in sources :

Example 71 with ColorModel

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

the class MrSIDTest method subBandsRead.

/**
 * Test read exploiting the setSourceBands and setDestinationType on
 * imageReadParam
 *
 * @throws FileNotFoundException
 * @throws IOException
 */
@Test
public void subBandsRead() throws IOException {
    if (!isMrSidAvailable) {
        return;
    }
    try {
        ImageReader reader = new MrSIDImageReaderSpi().createReaderInstance();
        final File file = TestData.file(this, fileName);
        reader.setInput(file);
        // //
        // 
        // Getting image properties
        // 
        // //
        ImageTypeSpecifier spec = (ImageTypeSpecifier) reader.getImageTypes(0).next();
        SampleModel sm = spec.getSampleModel();
        final int width = reader.getWidth(0);
        final int height = reader.getHeight(0);
        // //
        // 
        // Setting a ColorModel
        // 
        // //
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorModel cm = RasterFactory.createComponentColorModel(sm.getDataType(), // color space
        cs, // has alpha
        false, // is alphaPremultiplied
        false, // transparency
        Transparency.OPAQUE);
        // //
        // 
        // Setting Image Read Parameters
        // 
        // //
        final ImageReadParam param = new ImageReadParam();
        final int ssx = 2;
        final int ssy = 2;
        param.setSourceSubsampling(ssx, ssy, 0, 0);
        final Rectangle sourceRegion = new Rectangle(50, 50, 300, 300);
        param.setSourceRegion(sourceRegion);
        param.setSourceBands(new int[] { 0 });
        Rectangle intersRegion = new Rectangle(0, 0, width, height);
        intersRegion = intersRegion.intersection(sourceRegion);
        int subsampledWidth = (intersRegion.width + ssx - 1) / ssx;
        int subsampledHeight = (intersRegion.height + ssy - 1) / ssy;
        param.setDestinationType(new ImageTypeSpecifier(cm, sm.createCompatibleSampleModel(subsampledWidth, subsampledHeight).createSubsetSampleModel(new int[] { 0 })));
        // //
        // 
        // Preparing the ImageRead operation
        // 
        // //
        ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
        pbjImageRead.setParameter("Input", file);
        pbjImageRead.setParameter("readParam", param);
        pbjImageRead.setParameter("reader", reader);
        // //
        // 
        // Setting a Layout
        // 
        // //
        final ImageLayout l = new ImageLayout();
        l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(256).setTileWidth(256);
        RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
        if (TestData.isInteractiveTest())
            Viewer.visualizeAllInformation(image, "SourceBand selection");
        else {
            Assert.assertNotNull(image.getTiles());
            ImageIOUtilities.disposeImage(image);
        }
    } catch (FileNotFoundException fnfe) {
        warningMessage();
    }
}
Also used : ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) ColorSpace(java.awt.color.ColorSpace) Rectangle(java.awt.Rectangle) FileNotFoundException(java.io.FileNotFoundException) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Point(java.awt.Point) RenderingHints(java.awt.RenderingHints) ImageReadParam(javax.imageio.ImageReadParam) SampleModel(java.awt.image.SampleModel) RenderedOp(javax.media.jai.RenderedOp) ColorModel(java.awt.image.ColorModel) ImageReader(javax.imageio.ImageReader) File(java.io.File) ImageLayout(javax.media.jai.ImageLayout) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest) Test(org.junit.Test)

Example 72 with ColorModel

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

the class EmptyImage method write.

private void write(IIOMetadata sm, IIOImage iioimage, ImageWriteParam p, boolean writeHeader, boolean writeData) throws IOException {
    if (stream == null) {
        throw new IllegalStateException("output == null!");
    }
    if (iioimage == null) {
        throw new IllegalArgumentException("image == null!");
    }
    if (iioimage.hasRaster() && !canWriteRasters()) {
        throw new UnsupportedOperationException("TIFF ImageWriter cannot write Rasters!");
    }
    this.image = iioimage.getRenderedImage();
    SampleModel sampleModel = image.getSampleModel();
    this.sourceXOffset = image.getMinX();
    this.sourceYOffset = image.getMinY();
    this.sourceWidth = image.getWidth();
    this.sourceHeight = image.getHeight();
    Rectangle imageBounds = new Rectangle(sourceXOffset, sourceYOffset, sourceWidth, sourceHeight);
    ColorModel colorModel = null;
    if (p == null) {
        this.param = getDefaultWriteParam();
        this.sourceBands = null;
        this.periodX = 1;
        this.periodY = 1;
        this.numBands = sampleModel.getNumBands();
        colorModel = image.getColorModel();
    } else {
        this.param = p;
        // Get source region and subsampling factors
        Rectangle sourceRegion = param.getSourceRegion();
        if (sourceRegion != null) {
            // Clip to actual image bounds
            sourceRegion = sourceRegion.intersection(imageBounds);
            sourceXOffset = sourceRegion.x;
            sourceYOffset = sourceRegion.y;
            sourceWidth = sourceRegion.width;
            sourceHeight = sourceRegion.height;
        }
        // Adjust for subsampling offsets
        int gridX = param.getSubsamplingXOffset();
        int gridY = param.getSubsamplingYOffset();
        this.sourceXOffset += gridX;
        this.sourceYOffset += gridY;
        this.sourceWidth -= gridX;
        this.sourceHeight -= gridY;
        // Get subsampling factors
        this.periodX = param.getSourceXSubsampling();
        this.periodY = param.getSourceYSubsampling();
        int[] sBands = param.getSourceBands();
        if (sBands != null) {
            sourceBands = sBands;
            this.numBands = sourceBands.length;
        } else {
            this.numBands = sampleModel.getNumBands();
        }
        ImageTypeSpecifier destType = p.getDestinationType();
        if (destType != null) {
            ColorModel cm = destType.getColorModel();
            if (cm.getNumComponents() == numBands) {
                colorModel = cm;
            }
        }
        if (colorModel == null) {
            colorModel = image.getColorModel();
        }
    }
    this.imageType = new ImageTypeSpecifier(colorModel, sampleModel);
    ImageUtil.canEncodeImage(this, this.imageType);
    // Compute output dimensions
    int destWidth = (sourceWidth + periodX - 1) / periodX;
    int destHeight = (sourceHeight + periodY - 1) / periodY;
    if (destWidth <= 0 || destHeight <= 0) {
        throw new IllegalArgumentException("Empty source region!");
    }
    // this.bitDepth = 8; // XXX fix?
    clearAbortRequest();
    int progressStep = 1;
    processImageStarted(0);
    int[] sampleSize = sampleModel.getSampleSize();
    long tot = 0;
    for (int i = 0; i < this.numBands; i++) tot += sampleSize[i];
    long sizeImage = (tot * this.sourceHeight * this.sourceWidth) / 8;
    long var = 4294967296L;
    boolean isForceToBigTIFF = false;
    if (p instanceof TIFFImageWriteParam) {
        isForceToBigTIFF = ((TIFFImageWriteParam) p).isForceToBigTIFF();
    }
    if (sizeImage > var || isForceToBigTIFF || isBtiff == true)
        isBtiff = true;
    else
        isBtiff = false;
    // Optionally write the header.
    if (writeHeader) {
        // Clear previous stream metadata.
        this.streamMetadata = null;
        // Try to convert non-null input stream metadata.
        if (sm != null) {
            this.streamMetadata = (TIFFStreamMetadata) convertStreamMetadata(sm, param);
        }
        // Set to default if not converted.
        if (this.streamMetadata == null) {
            this.streamMetadata = (TIFFStreamMetadata) getDefaultStreamMetadata(param);
        }
        // Write the header.
        writeHeader();
        // 3) Write the pointer to the first IFD after the header.
        if (!isBtiff) {
            stream.seek(headerPosition + 4);
            nextSpace = (nextSpace + 3) & ~0x3;
            stream.writeInt((int) nextSpace);
        } else {
            stream.seek(headerPosition + 8);
            nextSpace = (nextSpace + 7) & ~0x7;
            stream.writeLong(nextSpace);
        }
    }
    // Write out the IFD and any sub IFDs, followed by a zero
    // Clear previous image metadata.
    this.imageMetadata = null;
    // Initialize the metadata object.
    IIOMetadata im = iioimage.getMetadata();
    if (im != null) {
        if (im instanceof TIFFImageMetadata) {
            // Clone the one passed in.
            this.imageMetadata = ((TIFFImageMetadata) im).getShallowClone();
        } else if (Arrays.asList(im.getMetadataFormatNames()).contains(TIFFImageMetadata.nativeMetadataFormatName)) {
            this.imageMetadata = convertNativeImageMetadata(im);
        } else if (im.isStandardMetadataFormatSupported()) {
            try {
                // Convert standard metadata.
                this.imageMetadata = convertStandardImageMetadata(im);
            } catch (IIOInvalidTreeException e) {
            // XXX Warning
            }
        }
    }
    // Use default metadata if still null.
    if (this.imageMetadata == null) {
        this.imageMetadata = (TIFFImageMetadata) getDefaultImageMetadata(this.imageType, this.param);
    }
    // Set or overwrite mandatory fields in the root IFD
    setupMetadata(colorModel, sampleModel, destWidth, destHeight);
    // Set compressor fields.
    compressor.setWriter(this);
    // Metadata needs to be set on the compressor before the IFD is
    // written as the compressor could modify the metadata.
    compressor.setMetadata(imageMetadata);
    compressor.setStream(stream);
    // Initialize scaling tables for this image
    initializeScaleTables(sampleModel.getSampleSize());
    // Determine whether bilevel.
    this.isBilevel = ImageUtil.isBinary(this.image.getSampleModel());
    // Check for photometric inversion.
    this.isInverted = (nativePhotometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO && photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO) || (nativePhotometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO && photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO);
    // Analyze image data suitability for direct copy.
    this.isImageSimple = (isBilevel || (!isInverted && ImageUtil.imageIsContiguous(this.image))) && // no value rescaling
    !isRescaling && // no subbanding
    sourceBands == null && periodX == 1 && // no subsampling
    periodY == 1 && colorConverter == null;
    TIFFIFD rootIFD = imageMetadata.getRootIFD();
    rootIFD.writeToStream(stream, isBtiff);
    this.nextIFDPointerPos = stream.getStreamPosition();
    if (!isBtiff) {
        stream.writeInt(0);
    } else {
        stream.writeLong(0);
    }
    // Seek to end of IFD data
    long lastIFDPosition = rootIFD.getLastPosition();
    stream.seek(lastIFDPosition);
    if (lastIFDPosition > this.nextSpace) {
        this.nextSpace = lastIFDPosition;
    }
    // empty image, return.
    if (!writeData) {
        return;
    }
    // Get positions of fields within the IFD to update as we write
    // each strip or tile
    long stripOrTileByteCountsPosition = rootIFD.getStripOrTileByteCountsPosition();
    long stripOrTileOffsetsPosition = rootIFD.getStripOrTileOffsetsPosition();
    // Compute total number of pixels for progress notification
    this.totalPixels = tileWidth * tileLength * tilesDown * tilesAcross;
    this.pixelsDone = 0;
    // Write the image, a strip or tile at a time
    for (int tj = 0; tj < tilesDown; tj++) {
        for (int ti = 0; ti < tilesAcross; ti++) {
            long pos = stream.getStreamPosition();
            // Write the (possibly compressed) tile data
            Rectangle tileRect = new Rectangle(sourceXOffset + ti * tileWidth * periodX, sourceYOffset + tj * tileLength * periodY, tileWidth * periodX, tileLength * periodY);
            try {
                int byteCount = writeTile(tileRect, compressor);
                if (pos + byteCount > nextSpace) {
                    nextSpace = pos + byteCount;
                }
                pixelsDone += tileRect.width * tileRect.height;
                float currentProgress = 100.0F * pixelsDone / totalPixels;
                if (currentProgress > progressStep * PROGRESS_FACTOR_MULTIPLIER) {
                    processImageProgress(currentProgress);
                    progressStep++;
                }
                // Fill in the offset and byte count for the file
                stream.mark();
                stream.seek(stripOrTileOffsetsPosition);
                if (!isBtiff) {
                    stream.writeInt((int) pos);
                    stripOrTileOffsetsPosition += 4;
                    stream.seek(stripOrTileByteCountsPosition);
                    stream.writeInt(byteCount);
                    stripOrTileByteCountsPosition += 4;
                } else {
                    stream.writeLong(pos);
                    stripOrTileOffsetsPosition += 8;
                    stream.seek(stripOrTileByteCountsPosition);
                    stream.writeLong(byteCount);
                    stripOrTileByteCountsPosition += 8;
                }
                stream.reset();
            } catch (IOException e) {
                throw new IIOException("I/O error writing TIFF file!", e);
            }
            if (abortRequested()) {
                processWriteAborted();
                return;
            }
        }
    }
    processImageComplete();
}
Also used : IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException) Rectangle(java.awt.Rectangle) TIFFImageWriteParam(it.geosolutions.imageio.plugins.tiff.TIFFImageWriteParam) IIOException(javax.imageio.IIOException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) Point(java.awt.Point) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOMetadata(javax.imageio.metadata.IIOMetadata) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) IndexColorModel(java.awt.image.IndexColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel)

Example 73 with ColorModel

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

the class PNMImageReader method getImageTypes.

public Iterator getImageTypes(int imageIndex) throws IOException {
    checkIndex(imageIndex);
    readHeader();
    int tmp = (variant - '1') % 3;
    ArrayList list = new ArrayList(1);
    int dataType = DataBuffer.TYPE_INT;
    // Determine data type based on maxValue.
    if (maxValue < 0x100) {
        dataType = DataBuffer.TYPE_BYTE;
    } else if (maxValue < 0x10000) {
        dataType = DataBuffer.TYPE_USHORT;
    }
    // Choose an appropriate SampleModel.
    SampleModel sampleModel = null;
    ColorModel colorModel = null;
    if ((variant == PBM_ASCII) || (variant == PBM_RAW)) {
        // Each pixel takes 1 bit, pack 8 pixels into a byte.
        sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, width, height, 1);
        byte[] color = { (byte) 0xFF, (byte) 0 };
        colorModel = new IndexColorModel(1, 2, color, color, color);
    } else {
        sampleModel = new PixelInterleavedSampleModel(dataType, width, height, tmp == 1 ? 1 : 3, width * (tmp == 1 ? 1 : 3), tmp == 1 ? new int[] { 0 } : new int[] { 0, 1, 2 });
        colorModel = ImageUtil.createColorModel(null, sampleModel);
    }
    list.add(new ImageTypeSpecifier(colorModel, sampleModel));
    return list.iterator();
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) ArrayList(java.util.ArrayList) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) Point(java.awt.Point) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IndexColorModel(java.awt.image.IndexColorModel)

Example 74 with ColorModel

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

the class PNMImageReader method read.

public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    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();
    // If the destination band is set used it
    int[] sourceBands = param.getSourceBands();
    int[] destBands = param.getDestinationBands();
    boolean seleBand = (sourceBands != null) && (destBands != null);
    boolean noTransform = destinationRegion.equals(new Rectangle(0, 0, width, height)) || seleBand;
    // base the maxValue on variant.
    if (isRaw(variant) && maxValue >= 0x100) {
        maxValue = 0xFF;
    }
    int numBands = 1;
    // bitmap (PBM) and greymap (PGM) are 1 band.
    if (variant == PPM_ASCII || variant == PPM_RAW) {
        numBands = 3;
    }
    if (!seleBand) {
        sourceBands = new int[numBands];
        destBands = new int[numBands];
        for (int i = 0; i < numBands; i++) destBands[i] = sourceBands[i] = i;
    }
    int dataType = DataBuffer.TYPE_INT;
    // Determine data type based on maxValue.
    if (maxValue < 0x100) {
        dataType = DataBuffer.TYPE_BYTE;
    } else if (maxValue < 0x10000) {
        dataType = DataBuffer.TYPE_USHORT;
    }
    // Choose an appropriate SampleModel.
    SampleModel sampleModel = null;
    ColorModel colorModel = null;
    if ((variant == PBM_ASCII) || (variant == PBM_RAW)) {
        // Each pixel takes 1 bit, pack 8 pixels into a byte.
        sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, destinationRegion.width, destinationRegion.height, 1);
        byte[] color = { (byte) 0xFF, (byte) 0 };
        colorModel = new IndexColorModel(1, 2, color, color, color);
    } else {
        sampleModel = new PixelInterleavedSampleModel(dataType, destinationRegion.width, destinationRegion.height, sourceBands.length, destinationRegion.width * sourceBands.length, destBands);
        colorModel = ImageUtil.createColorModel(null, sampleModel);
    }
    // If the destination is provided, then use it. Otherwise, create new
    // one
    BufferedImage bi = param.getDestination();
    // Get the image data.
    WritableRaster raster = null;
    if (bi == null) {
        sampleModel = sampleModel.createCompatibleSampleModel(destinationRegion.x + destinationRegion.width, destinationRegion.y + destinationRegion.height);
        if (seleBand)
            sampleModel = sampleModel.createSubsetSampleModel(sourceBands);
        raster = Raster.createWritableRaster(sampleModel, new Point());
        bi = new BufferedImage(colorModel, raster, false, null);
    } else {
        raster = bi.getWritableTile(0, 0);
        sampleModel = bi.getSampleModel();
        colorModel = bi.getColorModel();
        noTransform &= destinationRegion.equals(raster.getBounds());
    }
    final int destinationRegionHeight = destinationRegion.height;
    final int destinationRegionWidth = destinationRegion.width;
    final int numSourceBands = sourceBands.length;
    int j = 0;
    int k = 0;
    int i = 0;
    switch(variant) {
        case PBM_RAW:
            {
                // SampleModel for these cases should be MultiPixelPacked.
                DataBuffer dataBuffer = raster.getDataBuffer();
                // Read the entire image.
                byte[] buf = ((DataBufferByte) dataBuffer).getData();
                if (noTransform) {
                    iis.readFully(buf, 0, buf.length);
                    processImageUpdate(bi, 0, 0, width, height, 1, 1, destBands);
                    processImageProgress(100.0F);
                } else if (scaleX == 1 && sourceRegion.x % 8 == 0) {
                    int skip = sourceRegion.x >> 3;
                    int originalLS = width + 7 >> 3;
                    int destLS = raster.getWidth() + 7 >> 3;
                    int readLength = sourceRegion.width + 7 >> 3;
                    int offset = sourceRegion.y * originalLS;
                    iis.skipBytes(offset + skip);
                    offset = originalLS * (scaleY - 1) + originalLS - readLength;
                    byte[] lineData = new byte[readLength];
                    int bitoff = destinationRegion.x & 7;
                    boolean reformat = !(bitoff == 0);
                    for (i = 0, j = 0, k = destinationRegion.y * destLS + (destinationRegion.x >> 3); i < destinationRegionHeight; i++, j += scaleY) {
                        if (reformat) {
                            iis.read(lineData, 0, readLength);
                            int mask1 = (255 << bitoff) & 255;
                            int mask2 = ~mask1 & 255;
                            int shift = 8 - bitoff;
                            int n = 0;
                            int m = k;
                            for (; n < readLength - 1; n++, m++) buf[m] = (byte) (((lineData[n] & mask2) << shift) | (lineData[n + 1] & mask1) >> bitoff);
                            buf[m] = (byte) ((lineData[n] & mask2) << shift);
                        } else {
                            iis.read(buf, k, readLength);
                        }
                        iis.skipBytes(offset);
                        k += destLS;
                        processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                        processImageProgress(100.0F * i / destinationRegionHeight);
                    }
                } else {
                    int originalLS = width + 7 >> 3;
                    byte[] data = new byte[originalLS];
                    iis.skipBytes(sourceRegion.y * originalLS);
                    int destLS = bi.getWidth() + 7 >> 3;
                    int offset = originalLS * (scaleY - 1);
                    int dsx = destLS * destinationRegion.y + (destinationRegion.x >> 3);
                    int n = dsx;
                    for (i = 0, j = 0; i < destinationRegionHeight; i++, j += scaleY) {
                        iis.read(data, 0, originalLS);
                        iis.skipBytes(offset);
                        int b = 0;
                        int pos = 7 - (destinationRegion.x & 7);
                        for (int m = sourceRegion.x; m < sourceRegion.x + sourceRegion.width; m += scaleX) {
                            b |= (data[m >> 3] >> (7 - (m & 7)) & 1) << pos;
                            pos--;
                            if (pos == -1) {
                                buf[n++] = (byte) b;
                                b = 0;
                                pos = 7;
                            }
                        }
                        if (pos != 7)
                            buf[n++] = (byte) b;
                        n += destinationRegion.x >> 3;
                        processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                        processImageProgress(100.0F * i / destinationRegionHeight);
                    }
                }
                break;
            }
        case PBM_ASCII:
            {
                DataBuffer dataBuffer = raster.getDataBuffer();
                byte[] buf = ((DataBufferByte) dataBuffer).getData();
                i = 0;
                if (noTransform)
                    for (int n = 0; i < height; i++) {
                        int b = 0;
                        int pos = 7;
                        for (j = 0; j < width; j++) {
                            b |= (readInteger(iis) & 1) << pos;
                            pos--;
                            if (pos == -1) {
                                buf[n++] = (byte) b;
                                b = 0;
                                pos = 7;
                            }
                        }
                        if (pos != 7)
                            buf[n++] = (byte) b;
                        processImageUpdate(bi, 0, i, width, 1, 1, 1, destBands);
                        processImageProgress(100.0F * i / height);
                    }
                else {
                    skipInteger(iis, sourceRegion.y * width + sourceRegion.x);
                    int skipX = scaleX - 1;
                    int skipY = (scaleY - 1) * width + width - destinationRegionWidth * scaleX;
                    int dsx = (bi.getWidth() + 7 >> 3) * destinationRegion.y + (destinationRegion.x >> 3);
                    i = 0;
                    for (int n = dsx; i < destinationRegionHeight; i++) {
                        int b = 0;
                        int pos = 7 - (destinationRegion.x & 7);
                        for (j = 0; j < destinationRegionWidth; j++) {
                            b |= (readInteger(iis) & 1) << pos;
                            pos--;
                            if (pos == -1) {
                                buf[n++] = (byte) b;
                                b = 0;
                                pos = 7;
                            }
                            skipInteger(iis, skipX);
                        }
                        if (pos != 7)
                            buf[n++] = (byte) b;
                        n += destinationRegion.x >> 3;
                        skipInteger(iis, skipY);
                        processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                        processImageProgress(100.0F * i / destinationRegionHeight);
                    }
                }
                break;
            }
        case PGM_ASCII:
        case PGM_RAW:
        case PPM_ASCII:
        case PPM_RAW:
            // SampleModel for these cases should be PixelInterleaved.
            int skipX = (scaleX - 1) * numBands;
            int skipY = (scaleY * width - destinationRegion.width * scaleX) * numBands;
            int dsx = (bi.getWidth() * destinationRegion.y + destinationRegion.x) * numBands;
            switch(dataType) {
                case DataBuffer.TYPE_BYTE:
                    DataBufferByte bbuf = (DataBufferByte) raster.getDataBuffer();
                    byte[] byteArray = bbuf.getData();
                    if (isRaw(variant)) {
                        if (noTransform) {
                            iis.readFully(byteArray);
                            processImageUpdate(bi, 0, 0, width, height, 1, 1, destBands);
                            processImageProgress(100.0F);
                        } else {
                            iis.skipBytes(sourceRegion.y * width * numBands);
                            int skip = (scaleY - 1) * width * numBands;
                            byte[] data = new byte[width * numBands];
                            int pixelStride = scaleX * numBands;
                            int sx = sourceRegion.x * numBands;
                            i = 0;
                            for (int n = dsx; i < destinationRegionHeight; i++) {
                                iis.read(data);
                                for (j = sourceRegion.x, k = sx; j < sourceRegion.x + sourceRegion.width; j += scaleX, k += pixelStride) {
                                    for (int m = 0; m < numSourceBands; m++) byteArray[n + destBands[m]] = data[k + sourceBands[m]];
                                    n += numSourceBands;
                                }
                                n += destinationRegion.x * numBands;
                                iis.skipBytes(skip);
                                processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                                processImageProgress(100.0F * i / destinationRegionHeight);
                            }
                        }
                    } else {
                        skipInteger(iis, (sourceRegion.y * width + sourceRegion.x) * numBands);
                        i = 0;
                        if (seleBand) {
                            byte[] data = new byte[numBands];
                            for (int n = dsx; i < destinationRegionHeight; i++) {
                                for (j = 0; j < destinationRegionWidth; j++) {
                                    for (k = 0; k < numBands; k++) data[k] = (byte) readInteger(iis);
                                    for (k = 0; k < numSourceBands; k++) byteArray[n + destBands[k]] = data[sourceBands[k]];
                                    n += numSourceBands;
                                    skipInteger(iis, skipX);
                                }
                                n += destinationRegion.x * numSourceBands;
                                skipInteger(iis, skipY);
                                processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                                processImageProgress(100.0F * i / destinationRegionHeight);
                            }
                        } else
                            for (int n = dsx; i < destinationRegionHeight; i++) {
                                for (j = 0; j < destinationRegionWidth; j++) {
                                    for (k = 0; k < numBands; k++) byteArray[n++] = (byte) readInteger(iis);
                                    skipInteger(iis, skipX);
                                }
                                n += destinationRegion.x * numSourceBands;
                                skipInteger(iis, skipY);
                                processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                                processImageProgress(100.0F * i / destinationRegionHeight);
                            }
                    }
                    break;
                case DataBuffer.TYPE_USHORT:
                    DataBufferUShort sbuf = (DataBufferUShort) raster.getDataBuffer();
                    short[] shortArray = sbuf.getData();
                    skipInteger(iis, sourceRegion.y * width * numBands + sourceRegion.x);
                    i = 0;
                    if (seleBand) {
                        short[] data = new short[numBands];
                        for (int n = dsx; i < destinationRegionHeight; i++) {
                            for (j = 0; j < destinationRegionWidth; j++) {
                                for (k = 0; k < numBands; k++) data[k] = (short) readInteger(iis);
                                for (k = 0; k < numSourceBands; k++) shortArray[n + destBands[k]] = data[sourceBands[k]];
                                n += numSourceBands;
                                skipInteger(iis, skipX);
                            }
                            n += destinationRegion.x * numSourceBands;
                            skipInteger(iis, skipY);
                            processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                            processImageProgress(100.0F * i / destinationRegionHeight);
                        }
                    } else
                        for (int n = dsx; i < destinationRegionHeight; i++) {
                            for (j = 0; j < destinationRegionWidth; j++) {
                                for (k = 0; k < numBands; k++) shortArray[n++] = (short) readInteger(iis);
                                skipInteger(iis, skipX);
                            }
                            n += destinationRegion.x * numSourceBands;
                            skipInteger(iis, skipY);
                            processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                            processImageProgress(100.0F * i / destinationRegionHeight);
                        }
                    break;
                case DataBuffer.TYPE_INT:
                    DataBufferInt ibuf = (DataBufferInt) raster.getDataBuffer();
                    int[] intArray = ibuf.getData();
                    skipInteger(iis, sourceRegion.y * width * numBands + sourceRegion.x);
                    i = 0;
                    if (seleBand) {
                        int[] data = new int[numBands];
                        for (int n = dsx; i < destinationRegionHeight; i++) {
                            for (j = 0; j < destinationRegionWidth; j++) {
                                for (k = 0; k < numBands; k++) data[k] = readInteger(iis);
                                for (k = 0; k < numSourceBands; k++) intArray[n + destBands[k]] = data[sourceBands[k]];
                                n += numSourceBands;
                                skipInteger(iis, skipX);
                            }
                            n += destinationRegion.x * numSourceBands;
                            skipInteger(iis, skipY);
                            processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                            processImageProgress(100.0F * i / destinationRegionHeight);
                        }
                    } else {
                        for (int n = dsx; i < destinationRegionHeight; i++) {
                            for (j = 0; j < destinationRegionWidth; j++) {
                                for (k = 0; k < numSourceBands; k++) intArray[n++] = readInteger(iis);
                                skipInteger(iis, skipX);
                            }
                            n += destinationRegion.x * numSourceBands;
                            skipInteger(iis, skipY);
                            processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
                            processImageProgress(100.0F * i / destinationRegionHeight);
                        }
                    }
                    break;
            }
            break;
    }
    if (abortRequested())
        processReadAborted();
    else
        processImageComplete();
    return bi;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) Rectangle(java.awt.Rectangle) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) Point(java.awt.Point) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) DataBufferUShort(java.awt.image.DataBufferUShort) IndexColorModel(java.awt.image.IndexColorModel) DataBuffer(java.awt.image.DataBuffer)

Example 75 with ColorModel

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

the class TIFFDecompressor method createRawImage.

/**
 * Creates a <code>BufferedImage</code> whose underlying data
 * array will be suitable for holding the raw decoded output of
 * the <code>decodeRaw</code> method.
 *
 * <p> The default implementation calls
 * <code>getRawImageType</code>, and calls the resulting
 * <code>ImageTypeSpecifier</code>'s
 * <code>createBufferedImage</code> method.
 *
 * @return a <code>BufferedImage</code> whose underlying data
 * array has the same format as the raw source pixel data, or
 * <code>null</code> if it is not possible to create such an
 * image.
 */
public BufferedImage createRawImage() {
    if (planar) {
        // Create a single-banded image of the appropriate data type.
        // Get the number of bits per sample.
        int bps = bitsPerSample[sourceBands[0]];
        // Determine the data type.
        int dataType;
        if (sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_FLOATING_POINT) {
            if (this.bitsPerSample[0] <= 32)
                dataType = DataBuffer.TYPE_FLOAT;
            else
                dataType = DataBuffer.TYPE_DOUBLE;
        } else if (bps <= 8) {
            dataType = DataBuffer.TYPE_BYTE;
        } else if (bps <= 16) {
            if (sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER) {
                dataType = DataBuffer.TYPE_SHORT;
            } else {
                dataType = DataBuffer.TYPE_USHORT;
            }
        } else {
            dataType = DataBuffer.TYPE_INT;
        }
        ColorSpace csGray = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ImageTypeSpecifier its = null;
        // pixels are actually stored in the planar bands.
        if (bps == 1 || bps == 2 || bps == 4) {
            int bits = bps;
            int size = 1 << bits;
            byte[] r = new byte[size];
            byte[] g = new byte[size];
            byte[] b = new byte[size];
            for (int j = 0; j < r.length; j++) {
                r[j] = 0;
                g[j] = 0;
                b[j] = 0;
            }
            ColorModel cmGray = new IndexColorModel(bits, size, r, g, b);
            SampleModel smGray = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, bits);
            its = new ImageTypeSpecifier(cmGray, smGray);
        } else {
            its = ImageTypeSpecifier.createInterleaved(csGray, new int[] { 0 }, dataType, false, false);
        }
        return its.createBufferedImage(srcWidth, srcHeight);
    /* XXX Not necessarily byte for planar
            return new BufferedImage(srcWidth, srcHeight,
                                     BufferedImage.TYPE_BYTE_GRAY);
            */
    } else {
        ImageTypeSpecifier its = getRawImageType();
        if (its == null) {
            return null;
        }
        BufferedImage bi = its.createBufferedImage(srcWidth, srcHeight);
        return bi;
    }
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ColorSpace(java.awt.color.ColorSpace) BogusColorSpace(com.sun.media.imageioimpl.common.BogusColorSpace) SimpleCMYKColorSpace(com.sun.media.imageioimpl.common.SimpleCMYKColorSpace) IndexColorModel(java.awt.image.IndexColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) BufferedImage(java.awt.image.BufferedImage) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

ColorModel (java.awt.image.ColorModel)188 BufferedImage (java.awt.image.BufferedImage)92 IndexColorModel (java.awt.image.IndexColorModel)80 WritableRaster (java.awt.image.WritableRaster)57 ComponentColorModel (java.awt.image.ComponentColorModel)47 SampleModel (java.awt.image.SampleModel)40 DirectColorModel (java.awt.image.DirectColorModel)39 ColorSpace (java.awt.color.ColorSpace)28 Raster (java.awt.image.Raster)24 Rectangle (java.awt.Rectangle)21 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)20 Point (java.awt.Point)15 ComponentSampleModel (java.awt.image.ComponentSampleModel)15 Graphics2D (java.awt.Graphics2D)13 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)13 IOException (java.io.IOException)13 ColorConvertOp (java.awt.image.ColorConvertOp)12 DataBuffer (java.awt.image.DataBuffer)12 DataBufferInt (java.awt.image.DataBufferInt)11 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)11