Search in sources :

Example 41 with ComponentColorModel

use of java.awt.image.ComponentColorModel in project quick-media by liuyueyi.

the class PNGRed method createComponentColorModel.

/**
 * A convenience method to create an instance of
 * <code>ComponentColorModel</code> suitable for use with the
 * given <code>SampleModel</code>.  The <code>SampleModel</code>
 * should have a data type of <code>DataBuffer.TYPE_BYTE</code>,
 * <code>TYPE_USHORT</code>, or <code>TYPE_INT</code> and between
 * 1 and 4 bands.  Depending on the number of bands of the
 * <code>SampleModel</code>, either a gray, gray+alpha, rgb, or
 * rgb+alpha <code>ColorModel</code> is returned.
 */
public static ColorModel createComponentColorModel(SampleModel sm) {
    int type = sm.getDataType();
    int bands = sm.getNumBands();
    ComponentColorModel cm = null;
    if (type == DataBuffer.TYPE_BYTE) {
        switch(bands) {
            case 1:
                cm = colorModelGray8;
                break;
            case 2:
                cm = colorModelGrayAlpha8;
                break;
            case 3:
                cm = colorModelRGB8;
                break;
            case 4:
                cm = colorModelRGBA8;
                break;
        }
    } else if (type == DataBuffer.TYPE_USHORT) {
        switch(bands) {
            case 1:
                cm = colorModelGray16;
                break;
            case 2:
                cm = colorModelGrayAlpha16;
                break;
            case 3:
                cm = colorModelRGB16;
                break;
            case 4:
                cm = colorModelRGBA16;
                break;
        }
    } else if (type == DataBuffer.TYPE_INT) {
        switch(bands) {
            case 1:
                cm = colorModelGray32;
                break;
            case 2:
                cm = colorModelGrayAlpha32;
                break;
            case 3:
                cm = colorModelRGB32;
                break;
            case 4:
                cm = colorModelRGBA32;
                break;
        }
    }
    return cm;
}
Also used : ComponentColorModel(java.awt.image.ComponentColorModel) Point(java.awt.Point)

Example 42 with ComponentColorModel

use of java.awt.image.ComponentColorModel in project quick-media by liuyueyi.

the class PNGImage method createComponentColorModel.

/**
 * A convenience method to create an instance of
 * <code>ComponentColorModel</code> suitable for use with the
 * given <code>SampleModel</code>.  The <code>SampleModel</code>
 * should have a data type of <code>DataBuffer.TYPE_BYTE</code>,
 * <code>TYPE_USHORT</code>, or <code>TYPE_INT</code> and between
 * 1 and 4 bands.  Depending on the number of bands of the
 * <code>SampleModel</code>, either a gray, gray+alpha, rgb, or
 * rgb+alpha <code>ColorModel</code> is returned.
 */
public static ColorModel createComponentColorModel(SampleModel sm) {
    int type = sm.getDataType();
    int bands = sm.getNumBands();
    ComponentColorModel cm = null;
    if (type == DataBuffer.TYPE_BYTE) {
        switch(bands) {
            case 1:
                cm = colorModelGray8;
                break;
            case 2:
                cm = colorModelGrayAlpha8;
                break;
            case 3:
                cm = colorModelRGB8;
                break;
            case 4:
                cm = colorModelRGBA8;
                break;
        }
    } else if (type == DataBuffer.TYPE_USHORT) {
        switch(bands) {
            case 1:
                cm = colorModelGray16;
                break;
            case 2:
                cm = colorModelGrayAlpha16;
                break;
            case 3:
                cm = colorModelRGB16;
                break;
            case 4:
                cm = colorModelRGBA16;
                break;
        }
    } else if (type == DataBuffer.TYPE_INT) {
        switch(bands) {
            case 1:
                cm = colorModelGray32;
                break;
            case 2:
                cm = colorModelGrayAlpha32;
                break;
            case 3:
                cm = colorModelRGB32;
                break;
            case 4:
                cm = colorModelRGBA32;
                break;
        }
    }
    return cm;
}
Also used : ComponentColorModel(java.awt.image.ComponentColorModel) Point(java.awt.Point)

Example 43 with ComponentColorModel

use of java.awt.image.ComponentColorModel in project nutz by nutzam.

the class Images method createJPEG4.

/**
 * Java's ImageIO can't process 4-component images and Java2D can't apply
 * AffineTransformOp either, so convert raster data to RGB. Technique due to
 * MArk Stephens. Free for any use.
 */
private static BufferedImage createJPEG4(Raster raster) {
    int w = raster.getWidth();
    int h = raster.getHeight();
    byte[] rgb = new byte[w * h * 3];
    float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
    float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
    float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
    float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
    for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
        float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
        double val = y + 1.402 * (cr - 128) - k;
        val = (val - 128) * .65f + 128;
        rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
        val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
        val = (val - 128) * .65f + 128;
        rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
        val = y + 1.772 * (cb - 128) - k;
        val = (val - 128) * .65f + 128;
        rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
    }
    raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3, new int[] { 0, 1, 2 }, null);
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    return new BufferedImage(cm, (WritableRaster) raster, true, null);
}
Also used : ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage)

Example 44 with ComponentColorModel

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

the class WServerGLWidget method texImage2D.

@Override
public void texImage2D(WGLWidget.GLenum target, int level, WGLWidget.GLenum internalformat, WGLWidget.GLenum format, WGLWidget.GLenum type, WGLWidget.Texture texture) {
    BufferedImage initialImage = null;
    try {
        initialImage = ImageIO.read(new File(texture.getUrl()));
    } catch (IOException e) {
        logger.error("IOException reading {}", texture.getUrl(), e);
    }
    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);
}
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)

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