Search in sources :

Example 6 with ColorModel

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

the class WBMPPluginTest method compare.

private boolean compare(BufferedImage in, BufferedImage out) {
    int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }
    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);
            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);
            if (newrgb != oldrgb || newalpha != oldalpha) {
                throw new RuntimeException("Pixels differ at " + i + ", " + j);
            }
        }
    }
    return true;
}
Also used : ColorModel(java.awt.image.ColorModel) Raster(java.awt.image.Raster)

Example 7 with ColorModel

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

the class PngDitDepthTest method main.

public static void main(String[] args) throws IIOInvalidTreeException {
    // getting the writer for the png format
    Iterator iter = ImageIO.getImageWritersByFormatName("png");
    ImageWriter writer = (ImageWriter) iter.next();
    // creating a color model
    ColorModel colorModel = ColorModel.getRGBdefault();
    // creating a sample model
    SampleModel sampleModel = colorModel.createCompatibleSampleModel(640, 480);
    // creating a default metadata object
    IIOMetadata metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(colorModel, sampleModel), null);
    String formatName = metaData.getNativeMetadataFormatName();
    // first call
    Node metaDataNode = metaData.getAsTree(formatName);
    try {
        metaData.setFromTree(formatName, metaDataNode);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    // second call (bitdepht is already set to an invalid value)
    metaDataNode = metaData.getAsTree(formatName);
    metaData.setFromTree(formatName, metaDataNode);
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException)

Example 8 with ColorModel

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

the class BMPWriteParamTest method compare.

private static boolean compare(final BufferedImage in, final BufferedImage out) {
    final int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }
    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);
            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);
            if (newrgb != oldrgb || newalpha != oldalpha) {
                // showDiff(in, out);
                throw new RuntimeException("Pixels differ at " + i + ", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));
            }
        }
    }
    return true;
}
Also used : ColorModel(java.awt.image.ColorModel) Raster(java.awt.image.Raster)

Example 9 with ColorModel

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

the class NoExtraBytesTest method createTestImage.

private static BufferedImage createTestImage(int type) {
    BufferedImage dst = null;
    ColorModel colorModel = null;
    WritableRaster raster = null;
    ColorSpace cs = null;
    System.out.println("Create image for " + getImageTypeName(type));
    switch(type) {
        case TYPE_INT_GRB:
            colorModel = new DirectColorModel(24, 0x0000ff00, 0x00ff0000, 0x000000ff);
            break;
        case TYPE_INT_GBR:
            colorModel = new DirectColorModel(24, 0x000000ff, 0x00ff0000, 0x0000ff00);
            break;
        case TYPE_INT_RBG:
            colorModel = new DirectColorModel(24, 0x00ff0000, 0x000000ff, 0x0000ff00);
            break;
        case TYPE_INT_BRG:
            colorModel = new DirectColorModel(24, 0x0000ff00, 0x000000ff, 0x00ff0000);
            break;
        case TYPE_INT_555_GRB:
            colorModel = new DirectColorModel(24, 0x0000001F, 0x000003e0, 0x00007c00);
            break;
        case TYPE_3BYTE_RGB:
            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
            int[] nBits = { 8, 8, 8 };
            int[] bOffs = { 0, 1, 2 };
            colorModel = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
            raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, w * 3, 3, bOffs, null);
            break;
        case TYPE_3BYTE_GRB:
            cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
            //nBits = {8, 8, 8};
            //bOffs = {0, 1, 2};
            colorModel = new ComponentColorModel(cs, new int[] { 8, 8, 8 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
            raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, w * 3, 3, new int[] { 1, 0, 2 }, null);
            break;
        default:
            dst = new BufferedImage(w, h, type);
    }
    if (dst == null) {
        if (raster == null) {
            raster = colorModel.createCompatibleWritableRaster(w, h);
        }
        dst = new BufferedImage(colorModel, raster, false, null);
    }
    Graphics g = dst.createGraphics();
    for (int i = 0; i < usedColors.length; i++) {
        g.setColor(usedColors[i]);
        g.fillRect(i * dx, 0, dx, h);
    }
    g.dispose();
    return dst;
}
Also used : Graphics(java.awt.Graphics) DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) DirectColorModel(java.awt.image.DirectColorModel) BufferedImage(java.awt.image.BufferedImage)

Example 10 with ColorModel

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

the class Write3ByteBgrTest method createTestImage.

private static BufferedImage createTestImage(int type, Color c) {
    BufferedImage i = null;
    if (type == BufferedImage.TYPE_CUSTOM) {
        WritableRaster wr = Raster.createBandedRaster(DataBuffer.TYPE_BYTE, width, height, // scanlineStride
        width, // bankIndices[],
        new int[] { 0, 1, 2 }, // bankOffsets[],
        new int[] { 1, 2, 0 }, null);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel cm = new ComponentColorModel(cs, new int[] { 8, 8, 8 }, false, false, ColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
        i = new BufferedImage(cm, wr, false, null);
    } else {
        i = new BufferedImage(width, height, type);
    }
    Graphics2D g = i.createGraphics();
    g.setColor(c);
    g.fillRect(0, 0, width, height);
    g.setColor(Color.white);
    g.drawRect(10, 10, width - 20, height - 20);
    return i;
}
Also used : WritableRaster(java.awt.image.WritableRaster) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

ColorModel (java.awt.image.ColorModel)85 IndexColorModel (java.awt.image.IndexColorModel)35 WritableRaster (java.awt.image.WritableRaster)32 BufferedImage (java.awt.image.BufferedImage)30 DirectColorModel (java.awt.image.DirectColorModel)23 ComponentColorModel (java.awt.image.ComponentColorModel)18 Raster (java.awt.image.Raster)16 SampleModel (java.awt.image.SampleModel)16 ColorSpace (java.awt.color.ColorSpace)12 SurfaceData (sun.java2d.SurfaceData)9 ComponentSampleModel (java.awt.image.ComponentSampleModel)8 Graphics2D (java.awt.Graphics2D)7 Rectangle (java.awt.Rectangle)7 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)7 Point (java.awt.Point)5 Component (java.awt.Component)4 Paint (java.awt.Paint)4 AffineTransform (java.awt.geom.AffineTransform)4 DataBufferByte (java.awt.image.DataBufferByte)4 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)4