Search in sources :

Example 21 with ComponentColorModel

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

the class JP2KKakaduImageReader method getColorModel.

/**
 * Setup a proper <code>ColorModel</code>
 *
 * @return a color model.
 * @throws KduException
 */
private static ColorModel getColorModel(JP2KCodestreamProperties codestreamP) throws KduException {
    if (codestreamP.getColorModel() != null)
        return codestreamP.getColorModel();
    if (codestreamP.getColorModel() != null)
        return codestreamP.getColorModel();
    final int nComponents = codestreamP.getNumComponents();
    if (nComponents <= 4) {
        ColorSpace cs;
        if (nComponents > 2) {
            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        } else {
            cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        }
        boolean hasAlpha = nComponents % 2 == 0;
        final int maxBitDepth = codestreamP.getMaxBitDepth();
        if (maxBitDepth <= 8) {
            codestreamP.setDataBufferType(DataBuffer.TYPE_BYTE);
        } else if (maxBitDepth <= 16) {
            if (codestreamP.isSigned())
                codestreamP.setDataBufferType(DataBuffer.TYPE_SHORT);
            else
                codestreamP.setDataBufferType(DataBuffer.TYPE_USHORT);
        } else if (maxBitDepth <= 32) {
            codestreamP.setDataBufferType(DataBuffer.TYPE_INT);
        }
        final int dataBufferType = codestreamP.getDataBufferType();
        if (dataBufferType != -1) {
            if (nComponents == 1 && (maxBitDepth == 1 || maxBitDepth == 2 || maxBitDepth == 4)) {
                codestreamP.setColorModel(ImageUtil.createColorModel(getSampleModel(codestreamP)));
            } else {
                codestreamP.setColorModel(new ComponentColorModel(cs, codestreamP.getBitsPerComponent(), hasAlpha, false, hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, dataBufferType));
            }
            return codestreamP.getColorModel();
        }
    }
    if (codestreamP.getSampleModel() == null)
        codestreamP.setSampleModel(getSampleModel(codestreamP));
    if (codestreamP.getSampleModel() == null)
        return null;
    return ImageUtil.createColorModel(codestreamP.getSampleModel());
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel)

Example 22 with ComponentColorModel

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

the class ImageIOUtilities method createColorModel.

/**
 * Creates a <code>ColorModel</code> that may be used with the
 * specified <code>SampleModel</code>.  If a suitable
 * <code>ColorModel</code> cannot be found, this method returns
 * <code>null</code>.
 *
 * <p> Suitable <code>ColorModel</code>s are guaranteed to exist
 * for all instances of <code>ComponentSampleModel</code>.
 * For 1- and 3- banded <code>SampleModel</code>s, the returned
 * <code>ColorModel</code> will be opaque.  For 2- and 4-banded
 * <code>SampleModel</code>s, the output will use alpha transparency
 * which is not premultiplied.  1- and 2-banded data will use a
 * grayscale <code>ColorSpace</code>, and 3- and 4-banded data a sRGB
 * <code>ColorSpace</code>. Data with 5 or more bands will have a
 * <code>BogusColorSpace</code>.</p>
 *
 * <p>An instance of <code>DirectColorModel</code> will be created for
 * instances of <code>SinglePixelPackedSampleModel</code> with no more
 * than 4 bands.</p>
 *
 * <p>An instance of <code>IndexColorModel</code> will be created for
 * instances of <code>MultiPixelPackedSampleModel</code>. The colormap
 * will be a grayscale ramp with <code>1&nbsp;<<&nbsp;numberOfBits</code>
 * entries ranging from zero to at most 255.</p>
 *
 * @return An instance of <code>ColorModel</code> that is suitable for
 *         the supplied <code>SampleModel</code>, or <code>null</code>.
 *
 * @throws IllegalArgumentException  If <code>sampleModel</code> is
 *         <code>null</code>.
 */
public static final ColorModel createColorModel(SampleModel sampleModel) {
    // Check the parameter.
    if (sampleModel == null) {
        throw new IllegalArgumentException("sampleModel == null!");
    }
    // Get the data type.
    int dataType = sampleModel.getDataType();
    // Check the data type
    switch(dataType) {
        case DataBuffer.TYPE_BYTE:
        case DataBuffer.TYPE_USHORT:
        case DataBuffer.TYPE_SHORT:
        case DataBuffer.TYPE_INT:
        case DataBuffer.TYPE_FLOAT:
        case DataBuffer.TYPE_DOUBLE:
            break;
        default:
            // Return null for other types.
            return null;
    }
    // The return variable.
    ColorModel colorModel = null;
    // Get the sample size.
    int[] sampleSize = sampleModel.getSampleSize();
    // Create a Component ColorModel.
    if (sampleModel instanceof ComponentSampleModel) {
        // Get the number of bands.
        int numBands = sampleModel.getNumBands();
        // Determine the color space.
        ColorSpace colorSpace = null;
        if (numBands <= 2) {
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        } else if (numBands <= 4) {
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        } else {
            colorSpace = new BogusColorSpace(numBands);
        }
        boolean hasAlpha = (numBands == 2) || (numBands == 4);
        boolean isAlphaPremultiplied = false;
        int transparency = hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
        if (dataType == DataBuffer.TYPE_SHORT)
            colorModel = new ComponentColorModel(colorSpace, sampleSize, hasAlpha, isAlphaPremultiplied, transparency, dataType);
        else
            colorModel = RasterFactory.createComponentColorModel(dataType, colorSpace, hasAlpha, isAlphaPremultiplied, transparency);
    } else if (sampleModel.getNumBands() <= 4 && sampleModel instanceof SinglePixelPackedSampleModel) {
        SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
        int[] bitMasks = sppsm.getBitMasks();
        int rmask = 0;
        int gmask = 0;
        int bmask = 0;
        int amask = 0;
        int numBands = bitMasks.length;
        if (numBands <= 2) {
            rmask = gmask = bmask = bitMasks[0];
            if (numBands == 2) {
                amask = bitMasks[1];
            }
        } else {
            rmask = bitMasks[0];
            gmask = bitMasks[1];
            bmask = bitMasks[2];
            if (numBands == 4) {
                amask = bitMasks[3];
            }
        }
        int bits = 0;
        for (int i = 0; i < sampleSize.length; i++) {
            bits += sampleSize[i];
        }
        return new DirectColorModel(bits, rmask, gmask, bmask, amask);
    } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
        // Load the colormap with a ramp.
        int bitsPerSample = sampleSize[0];
        int numEntries = 1 << bitsPerSample;
        byte[] map = new byte[numEntries];
        for (int i = 0; i < numEntries; i++) {
            map[i] = (byte) (i * 255 / (numEntries - 1));
        }
        colorModel = new IndexColorModel(bitsPerSample, numEntries, map, map, map);
    }
    return colorModel;
}
Also used : ColorSpace(java.awt.color.ColorSpace) BogusColorSpace(com.sun.imageio.plugins.common.BogusColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) DirectColorModel(java.awt.image.DirectColorModel) BogusColorSpace(com.sun.imageio.plugins.common.BogusColorSpace) IndexColorModel(java.awt.image.IndexColorModel)

Example 23 with ComponentColorModel

use of java.awt.image.ComponentColorModel in project jwt by emweb.

the class WServerGLWidget method texImage2D.

@Override
public void texImage2D(GLenum target, int level, GLenum internalformat, GLenum format, GLenum type, String image) {
    BufferedImage initialImage = null;
    try {
        initialImage = ImageIO.read(new File(image));
    } catch (IOException e) {
        logger.error("IOException reading {}", image);
    }
    int openGlInternalFormat = serverGLenum(internalformat);
    int openGlImageFormat = serverGLenum(format);
    WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, initialImage.getWidth(), initialImage.getHeight(), 4, null);
    ComponentColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false, ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
    BufferedImage bufImg = new BufferedImage(colorModel, raster, false, null);
    Graphics2D g = bufImg.createGraphics();
    g.translate(0, initialImage.getHeight());
    g.scale(1, -1);
    g.drawImage(initialImage, null, null);
    DataBufferByte imgBuf = (DataBufferByte) raster.getDataBuffer();
    byte[] bytes = imgBuf.getData();
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    g.dispose();
    glCtx_.glTexImage2D(GL2.GL_TEXTURE_2D, 0, openGlInternalFormat, initialImage.getWidth(), initialImage.getHeight(), 0, openGlImageFormat, GL2.GL_UNSIGNED_BYTE, buffer);
    logger.debug("texImage2D: {}", glCtx_.glGetError());
}
Also used : WritableRaster(java.awt.image.WritableRaster) ComponentColorModel(java.awt.image.ComponentColorModel) IOException(java.io.IOException) DataBufferByte(java.awt.image.DataBufferByte) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 24 with ComponentColorModel

use of java.awt.image.ComponentColorModel in project jwt by emweb.

the class WServerGLWidget method texImage2D.

@Override
public void texImage2D(GLenum target, int level, GLenum internalformat, GLenum format, GLenum type, WPaintDevice paintdevice) {
    ByteArrayInputStream dataIn = null;
    if (((paintdevice) instanceof WRasterPaintDevice ? (WRasterPaintDevice) (paintdevice) : null) != null) {
        WRasterPaintDevice rpd = ((paintdevice) instanceof WRasterPaintDevice ? (WRasterPaintDevice) (paintdevice) : null);
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {
            rpd.write(data);
        } catch (IOException e) {
            logger.error("IOException writing image to memory");
        }
        dataIn = new ByteArrayInputStream(data.toByteArray());
    } else {
        throw new WException("WServerGLWidget: invalid WPaintDevice");
    }
    BufferedImage initialImage = null;
    try {
        initialImage = ImageIO.read(dataIn);
    } catch (IOException e) {
        logger.error("IOException reading image from memory");
    }
    int openGlInternalFormat = serverGLenum(internalformat);
    int openGlImageFormat = serverGLenum(format);
    WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, initialImage.getWidth(), initialImage.getHeight(), 4, null);
    ComponentColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false, ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
    BufferedImage bufImg = new BufferedImage(colorModel, raster, false, null);
    Graphics2D g = bufImg.createGraphics();
    g.translate(0, initialImage.getHeight());
    g.scale(1, -1);
    g.drawImage(initialImage, null, null);
    DataBufferByte imgBuf = (DataBufferByte) raster.getDataBuffer();
    byte[] bytes = imgBuf.getData();
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    g.dispose();
    glCtx_.glTexImage2D(GL2.GL_TEXTURE_2D, 0, openGlInternalFormat, initialImage.getWidth(), initialImage.getHeight(), 0, openGlImageFormat, GL2.GL_UNSIGNED_BYTE, buffer);
    logger.debug("texImage2D: {}", glCtx_.glGetError());
}
Also used : ComponentColorModel(java.awt.image.ComponentColorModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataBufferByte(java.awt.image.DataBufferByte) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ByteArrayInputStream(java.io.ByteArrayInputStream) WritableRaster(java.awt.image.WritableRaster)

Example 25 with ComponentColorModel

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

the class ColCvtAlpha method main.

public static void main(String[] args) {
    BufferedImage src = new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB);
    // Set src pixel values
    Color pelColor = new Color(100, 100, 100, 128);
    for (int i = 0; i < 10; i++) {
        src.setRGB(0, i, pelColor.getRGB());
    }
    ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] { 8, 8 }, true, src.getColorModel().isAlphaPremultiplied(), Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, 100, 100, 2, 200, new int[] { 0, 1 });
    WritableRaster wr = Raster.createWritableRaster(sm, new Point(0, 0));
    BufferedImage dst = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
    dst = dst.getSubimage(0, 0, 1, 10);
    ColorConvertOp op = new ColorConvertOp(null);
    op.filter(src, dst);
    for (int i = 0; i < 10; i++) {
        if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) {
            throw new RuntimeException("Incorrect destination alpha value.");
        }
    }
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) ColorConvertOp(java.awt.image.ColorConvertOp) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) WritableRaster(java.awt.image.WritableRaster) Color(java.awt.Color) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point)

Aggregations

ComponentColorModel (java.awt.image.ComponentColorModel)44 ColorModel (java.awt.image.ColorModel)28 BufferedImage (java.awt.image.BufferedImage)26 ColorSpace (java.awt.color.ColorSpace)22 WritableRaster (java.awt.image.WritableRaster)17 IndexColorModel (java.awt.image.IndexColorModel)11 SampleModel (java.awt.image.SampleModel)11 Point (java.awt.Point)9 DirectColorModel (java.awt.image.DirectColorModel)9 DataBufferByte (java.awt.image.DataBufferByte)8 DataBuffer (java.awt.image.DataBuffer)7 Graphics2D (java.awt.Graphics2D)5 ComponentSampleModel (java.awt.image.ComponentSampleModel)5 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)5 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)4 ByteBuffer (java.nio.ByteBuffer)4 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)3 DataBufferUShort (java.awt.image.DataBufferUShort)3 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)3 File (java.io.File)3