Search in sources :

Example 71 with ColorSpace

use of java.awt.color.ColorSpace in project imageio-ext by geosolutions-it.

the class JpegVrtTest method sourceBands.

/**
 * Test sourceBands management capabilities.
 *
 * @throws FileNotFoundException
 * @throws IOException
 */
@Test
public void sourceBands() throws IOException, FileNotFoundException {
    if (!isGDALAvailable) {
        return;
    }
    final File inputFile = TestData.file(this, "small_world.jpg.vrt");
    // //
    // 
    // Preparing srcRegion constants
    // 
    // //
    final int srcRegionX = 40;
    final int srcRegionY = 50;
    final int srcRegionWidth = 400;
    final int srcRegionHeight = 300;
    final int subSamplingX = 2;
    final int subSamplingY = 1;
    // //
    // 
    // Setting source settings parameters
    // 
    // //
    ImageReadParam rparam = new ImageReadParam();
    rparam.setSourceRegion(new Rectangle(srcRegionX, srcRegionY, srcRegionWidth, srcRegionHeight));
    rparam.setSourceSubsampling(subSamplingX, subSamplingY, 0, 0);
    rparam.setSourceBands(new int[] { 0 });
    // //
    // 
    // Setting destination settings parameters
    // 
    // //
    rparam.setDestinationBands(new int[] { 0 });
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorModel cm = RasterFactory.createComponentColorModel(// dataType
    DataBuffer.TYPE_BYTE, // color space
    cs, // has alpha
    false, // is alphaPremultiplied
    false, // transparency
    Transparency.OPAQUE);
    final int destWidth = srcRegionWidth / subSamplingX;
    final int destHeight = srcRegionHeight / subSamplingY;
    Assert.assertEquals(destWidth, 200);
    Assert.assertEquals(destHeight, 300);
    final SampleModel sm = cm.createCompatibleSampleModel(destWidth, destHeight);
    rparam.setDestinationType(new ImageTypeSpecifier(cm, sm));
    // //
    // 
    // Preparing for image read operation
    // 
    // //
    final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
    pbjImageRead.setParameter("Input", inputFile);
    pbjImageRead.setParameter("readParam", rparam);
    final ImageLayout l = new ImageLayout();
    l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(128).setTileWidth(128);
    RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
    if (TestData.isInteractiveTest())
        Viewer.visualizeAllInformation(image, "imageread");
    else
        Assert.assertNotNull(image.getTiles());
    ImageIOUtilities.disposeImage(image);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) SampleModel(java.awt.image.SampleModel) RenderedOp(javax.media.jai.RenderedOp) ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) ColorSpace(java.awt.color.ColorSpace) ColorModel(java.awt.image.ColorModel) File(java.io.File) ImageLayout(javax.media.jai.ImageLayout) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Test(org.junit.Test) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest)

Example 72 with ColorSpace

use of java.awt.color.ColorSpace in project imageio-ext by geosolutions-it.

the class JPEGReadTest method sourceBands.

/**
 * Test sourceBands management capabilities.
 *
 * @throws FileNotFoundException
 * @throws IOException
 */
@Test
public void sourceBands() throws IOException, FileNotFoundException {
    if (!isGDALAvailable) {
        return;
    }
    final File inputFile = TestData.file(this, "small_world.jpg");
    // //
    // 
    // Preparing srcRegion constants
    // 
    // //
    final int srcRegionX = 40;
    final int srcRegionY = 50;
    final int srcRegionWidth = 400;
    final int srcRegionHeight = 300;
    final int subSamplingX = 2;
    final int subSamplingY = 1;
    // //
    // 
    // Setting source settings parameters
    // 
    // //
    ImageReadParam rparam = new ImageReadParam();
    rparam.setSourceRegion(new Rectangle(srcRegionX, srcRegionY, srcRegionWidth, srcRegionHeight));
    rparam.setSourceSubsampling(subSamplingX, subSamplingY, 0, 0);
    rparam.setSourceBands(new int[] { 0 });
    // //
    // 
    // Setting destination settings parameters
    // 
    // //
    rparam.setDestinationBands(new int[] { 0 });
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorModel cm = RasterFactory.createComponentColorModel(// dataType
    DataBuffer.TYPE_BYTE, // color space
    cs, // has alpha
    false, // is alphaPremultiplied
    false, // transparency
    Transparency.OPAQUE);
    final int destWidth = srcRegionWidth / subSamplingX;
    final int destHeight = srcRegionHeight / subSamplingY;
    Assert.assertEquals(destWidth, 200);
    Assert.assertEquals(destHeight, 300);
    final SampleModel sm = cm.createCompatibleSampleModel(destWidth, destHeight);
    rparam.setDestinationType(new ImageTypeSpecifier(cm, sm));
    // //
    // 
    // Preparing for image read operation
    // 
    // //
    ImageReader reader = new JpegGDALImageReaderSpi().createReaderInstance();
    final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
    pbjImageRead.setParameter("Input", inputFile);
    pbjImageRead.setParameter("reader", reader);
    pbjImageRead.setParameter("readParam", rparam);
    final ImageLayout l = new ImageLayout();
    l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(128).setTileWidth(128);
    RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
    if (TestData.isInteractiveTest())
        Viewer.visualizeAllInformation(image, "imageread");
    else
        Assert.assertNotNull(image.getTiles());
    ImageIOUtilities.disposeImage(image);
}
Also used : ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) ColorSpace(java.awt.color.ColorSpace) Rectangle(java.awt.Rectangle) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) 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) Test(org.junit.Test) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest)

Example 73 with ColorSpace

use of java.awt.color.ColorSpace in project imageio-ext by geosolutions-it.

the class GDALUtilities method buildColorModel.

/**
 * Builds a proper <code>ColorModel</code> for a specified
 * <code>SampleModel</code>
 *
 * @param sampleModel
 *                the sampleModel to be used as reference.
 * @return a proper <code>ColorModel</code> for the input
 *         <code>SampleModel</code>
 */
public static ColorModel buildColorModel(final SampleModel sampleModel) {
    ColorSpace cs = null;
    ColorModel colorModel = null;
    final int buffer_type = sampleModel.getDataType();
    final int numBands = sampleModel.getNumBands();
    if (numBands > 1) {
        // 
        // Number of Bands > 1.
        // ImageUtil.createColorModel provides to Creates a
        // ColorModel that may be used with the specified
        // SampleModel
        // 
        colorModel = ImageUtil.createColorModel(sampleModel);
        if (colorModel == null) {
            LOGGER.severe("No ColorModels found");
        }
    } else if ((buffer_type == DataBuffer.TYPE_BYTE) || (buffer_type == DataBuffer.TYPE_USHORT) || (buffer_type == DataBuffer.TYPE_INT) || (buffer_type == DataBuffer.TYPE_FLOAT) || (buffer_type == DataBuffer.TYPE_DOUBLE)) {
        // Just one band. Using the built-in Gray Scale Color Space
        cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        colorModel = // dataType
        RasterFactory.createComponentColorModel(// dataType
        buffer_type, // color space
        cs, // has alpha
        false, // is alphaPremultiplied
        false, // transparency
        Transparency.OPAQUE);
    } else {
        if (buffer_type == DataBuffer.TYPE_SHORT) {
            // Just one band. Using the built-in Gray Scale Color
            // Space
            cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
            colorModel = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_SHORT);
        }
    }
    return colorModel;
}
Also used : ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel)

Example 74 with ColorSpace

use of java.awt.color.ColorSpace in project com.revolsys.open by revolsys.

the class Gdal method getBufferedImage.

/**
 * <p>
 * Convert the overview raster from {@link Dataset} to a
 * {@link BufferedImage} . The raster will be clipped to the
 * sourceOffsetX,sourceOffsetY -> sourceWidth, sourceHeight rectangle. The
 * clip rectangle will be adjusted to fit inside the bounds of the source
 * image. The result image will scaled to the dimensions of targetWidth,
 * targetHeight.
 * </p>
 *
 * @param dataset
 *            The image dataset.
 * @param overviewIndex
 *            The index of the overview raster data. Use -1 for the whole
 *            image.
 * @param sourceOffsetX
 *            The x location of the clip rectangle.
 * @param sourceOffsetY
 *            The y location of the clip rectangle.
 * @param sourceWidth
 *            The width of the clip rectangle. Use -1 to auto calculate.
 * @param sourceHeight
 *            The height of the clip rectangle. Use -1 to auto calculate.
 * @param targetWidth
 *            The width of the result image. Use -1 to auto calculate.
 * @param targetHeight
 *            The height of the result image. Use -1 to auto calculate.
 * @return The buffered image.
 */
public static BufferedImage getBufferedImage(final Dataset dataset, final int overviewIndex, int sourceOffsetX, int sourceOffsetY, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {
    synchronized (dataset) {
        final int bandCount = dataset.getRasterCount();
        final ByteBuffer[] bandData = new ByteBuffer[bandCount];
        final int[] banks = new int[bandCount];
        final int[] offsets = new int[bandCount];
        int pixels = 0;
        int bandDataType = 0;
        int rasterColorInterpretation = -1;
        ColorTable rasterColorTable = null;
        for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
            final Band band = dataset.GetRasterBand(bandIndex + 1);
            try {
                Band overviewBand;
                if (overviewIndex == -1) {
                    overviewBand = band;
                } else {
                    overviewBand = band.GetOverview(overviewIndex);
                }
                try {
                    if (rasterColorTable == null) {
                        rasterColorTable = band.GetRasterColorTable();
                        rasterColorInterpretation = band.GetRasterColorInterpretation();
                        bandDataType = band.getDataType();
                        final int overviewWidth = overviewBand.getXSize();
                        final int overviewHeight = overviewBand.getYSize();
                        if (sourceOffsetX < 0) {
                            sourceOffsetX = 0;
                        }
                        if (sourceOffsetY < 0) {
                            sourceOffsetY = 0;
                        }
                        if (sourceOffsetX >= overviewWidth || sourceOffsetY >= overviewHeight) {
                            return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
                        }
                        if (sourceWidth < 0) {
                            sourceWidth = overviewWidth;
                        }
                        if (sourceOffsetX + sourceWidth > overviewWidth) {
                            sourceWidth = overviewWidth - sourceOffsetX;
                        }
                        if (targetWidth < 0) {
                            targetWidth = sourceWidth;
                        }
                        if (sourceHeight < 0) {
                            sourceHeight = overviewHeight;
                        }
                        if (sourceOffsetY + sourceHeight > overviewHeight) {
                            sourceHeight = overviewHeight - sourceOffsetY;
                        }
                        if (targetHeight < 0) {
                            targetHeight = sourceHeight;
                        }
                        pixels = targetWidth * targetHeight;
                    }
                    if (pixels > 0 && sourceHeight > 0 && sourceWidth > 0) {
                        final int bufferSize = pixels * gdal.GetDataTypeSize(bandDataType) / 8;
                        final ByteBuffer data = ByteBuffer.allocateDirect(bufferSize);
                        data.order(ByteOrder.nativeOrder());
                        final int result = overviewBand.ReadRaster_Direct(sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, targetWidth, targetHeight, bandDataType, data);
                        if (result == gdalconstConstants.CE_None) {
                            bandData[bandIndex] = data;
                        } else {
                            throw new RuntimeException("Error converting image");
                        }
                    } else {
                        return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
                    }
                    banks[bandIndex] = bandIndex;
                    offsets[bandIndex] = 0;
                } finally {
                    overviewBand.delete();
                }
            } finally {
                band.delete();
            }
        }
        DataBuffer imageBuffer = null;
        SampleModel sampleModel = null;
        int dataType = 0;
        int dataBufferType = 0;
        if (bandDataType == gdalconstConstants.GDT_Byte) {
            final byte[][] bytes = new byte[bandCount][];
            for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
                bytes[bandIndex] = new byte[pixels];
                bandData[bandIndex].get(bytes[bandIndex]);
            }
            imageBuffer = new DataBufferByte(bytes, pixels);
            dataBufferType = DataBuffer.TYPE_BYTE;
            sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
            dataType = rasterColorInterpretation == gdalconstConstants.GCI_PaletteIndex ? BufferedImage.TYPE_BYTE_INDEXED : BufferedImage.TYPE_BYTE_GRAY;
        } else if (bandDataType == gdalconstConstants.GDT_Int16) {
            final short[][] shorts = new short[bandCount][];
            for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
                shorts[bandIndex] = new short[pixels];
                bandData[bandIndex].asShortBuffer().get(shorts[bandIndex]);
            }
            imageBuffer = new DataBufferShort(shorts, pixels);
            dataBufferType = DataBuffer.TYPE_USHORT;
            sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
            dataType = BufferedImage.TYPE_USHORT_GRAY;
        } else if (bandDataType == gdalconstConstants.GDT_Int32) {
            final int[][] ints = new int[bandCount][];
            for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
                ints[bandIndex] = new int[pixels];
                bandData[bandIndex].asIntBuffer().get(ints[bandIndex]);
            }
            imageBuffer = new DataBufferInt(ints, pixels);
            dataBufferType = DataBuffer.TYPE_INT;
            sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
            dataType = BufferedImage.TYPE_CUSTOM;
        }
        final WritableRaster raster = Raster.createWritableRaster(sampleModel, imageBuffer, null);
        BufferedImage image = null;
        ColorModel colorModel = null;
        if (rasterColorInterpretation == gdalconstConstants.GCI_PaletteIndex) {
            dataType = BufferedImage.TYPE_BYTE_INDEXED;
            colorModel = rasterColorTable.getIndexColorModel(gdal.GetDataTypeSize(bandDataType));
            image = new BufferedImage(colorModel, raster, false, null);
        } else {
            ColorSpace colorSpace = null;
            if (bandCount > 2) {
                colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
                colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.OPAQUE, dataBufferType);
                image = new BufferedImage(colorModel, raster, true, null);
            } else {
                image = new BufferedImage(targetWidth, targetHeight, dataType);
                image.setData(raster);
            }
        }
        return image;
    }
}
Also used : ColorTable(org.gdal.gdal.ColorTable) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) Band(org.gdal.gdal.Band) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) DataBufferShort(java.awt.image.DataBufferShort) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) WritableRaster(java.awt.image.WritableRaster) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) DataBuffer(java.awt.image.DataBuffer)

Example 75 with ColorSpace

use of java.awt.color.ColorSpace in project pivot by apache.

the class GrayscaleDecorator method prepare.

@Override
public Graphics2D prepare(Component component, Graphics2D graphicsValue) {
    this.graphics = graphicsValue;
    int width = component.getWidth();
    int height = component.getHeight();
    if (bufferedImage == null || bufferedImage.getWidth() < width || bufferedImage.getHeight() < height) {
        ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
        WritableRaster raster = ccm.createCompatibleWritableRaster(width, height);
        bufferedImage = new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null);
    }
    bufferedImageGraphics = bufferedImage.createGraphics();
    bufferedImageGraphics.setClip(graphicsValue.getClip());
    return bufferedImageGraphics;
}
Also used : ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ColorSpace (java.awt.color.ColorSpace)86 ColorModel (java.awt.image.ColorModel)33 BufferedImage (java.awt.image.BufferedImage)30 ComponentColorModel (java.awt.image.ComponentColorModel)29 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)24 IndexColorModel (java.awt.image.IndexColorModel)17 SampleModel (java.awt.image.SampleModel)17 WritableRaster (java.awt.image.WritableRaster)13 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ColorConvertOp (java.awt.image.ColorConvertOp)10 DirectColorModel (java.awt.image.DirectColorModel)10 Point (java.awt.Point)8 DataBuffer (java.awt.image.DataBuffer)8 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)8 DataBufferByte (java.awt.image.DataBufferByte)6 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)6 IIOException (javax.imageio.IIOException)5 Test (org.junit.Test)5 Rectangle (java.awt.Rectangle)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)4