Example usage for java.awt.image DirectColorModel DirectColorModel

List of usage examples for java.awt.image DirectColorModel DirectColorModel

Introduction

In this page you can find the example usage for java.awt.image DirectColorModel DirectColorModel.

Prototype

public DirectColorModel(int bits, int rmask, int gmask, int bmask) 

Source Link

Document

Constructs a DirectColorModel from the specified masks that indicate which bits in an int pixel representation contain the red, green and blue color samples.

Usage

From source file:Snippet156.java

static BufferedImage convertToAWT(ImageData data) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;//  ww  w .j  a  va 2s  . c om
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                pixelArray[0] = rgb.red;
                pixelArray[1] = rgb.green;
                pixelArray[2] = rgb.blue;
                raster.setPixels(x, y, 1, 1, pixelArray);
            }
        }
        return bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        return bufferedImage;
    }
}

From source file:org.eclipse.swt.snippets.Snippet156.java

static BufferedImage convertToAWT(ImageData data) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;//  w  w w  . j a va  2 s. c  o m
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue);
            }
        }
        return bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        return bufferedImage;
    }
}

From source file:de.codesourcery.jasm16.emulator.devices.impl.RawImage.java

public RawImage(int width, int height) {
    data = new int[width * height];

    final DataBufferInt dataBuffer = new DataBufferInt(data, width * height);

    final ColorModel cm = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
    final SampleModel sm = cm.createCompatibleSampleModel(width, height);
    final WritableRaster wr = Raster.createWritableRaster(sm, dataBuffer, null);

    image = new BufferedImage(cm, wr, false, null);
}

From source file:de.mfo.jsurf.grid.RotationGrid.java

static BufferedImage createBufferedImageFromRGB(ImgBuffer ib) {
    int w = ib.width;
    int h = ib.height;

    DirectColorModel colormodel = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
    SampleModel sampleModel = colormodel.createCompatibleSampleModel(w, h);
    DataBufferInt data = new DataBufferInt(ib.rgbBuffer, w * h);
    WritableRaster raster = WritableRaster.createWritableRaster(sampleModel, data, new Point(0, 0));
    return new BufferedImage(colormodel, raster, false, null);
}

From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java

/**
 * Convert a SWT Image to a {@link BufferedImage}
 * /*  w  w w  .j  a v  a 2s  .c om*/
 * {@link "http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java?view=co"}
 * 
 * @param data the SWT {@link ImageData}
 * @param applyAlphaMask true if the image data's alpha mask should be
 *            applied to the result image (if there is any). This method
 *            calls {@link #applyTransparencyMask(BufferedImage, ImageData)}
 *            for that purpose.
 * @return the AWT {@link BufferedImage}
 */
public static BufferedImage convertToAWT(ImageData data, boolean applyAlphaMask) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;

    BufferedImage result;
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue);
            }
        }
        result = bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        result = bufferedImage;
    }

    if (data.getTransparencyType() == SWT.TRANSPARENCY_MASK && applyAlphaMask) {
        result = applyTransparencyMask(result, data.getTransparencyMask());
    }
    return result;
}

From source file:SampleAWTRenderer.java

private void newImage(Buffer buffer) {

    if (!(buffer.getData() instanceof int[]))
        return;//from w  w  w  . j a  va2 s .c  o  m

    int[] data = (int[]) buffer.getData();
    RGBFormat format = (RGBFormat) buffer.getFormat();

    //data = processImage(buffer);
    data = (int[]) buffer.getData();

    DirectColorModel dcm = new DirectColorModel(format.getBitsPerPixel(), format.getRedMask(),
            format.getGreenMask(), format.getBlueMask());

    sourceImage = new MemoryImageSource(format.getLineStride(), format.getSize().height, dcm, (int[]) data, 0,
            format.getLineStride());
    sourceImage.setAnimated(true);
    sourceImage.setFullBufferUpdates(true);
    if (component != null) {
        destImage = component.createImage(sourceImage);
        component.prepareImage(destImage, component);
    }
}

From source file:SampleAWTRenderer.java

private int[] processImage(Buffer buf) {
    int[] data = (int[]) buf.getData();
    int[] ret = new int[data.length];
    RGBFormat fmt = (RGBFormat) buf.getFormat();

    DirectColorModel dcm = new DirectColorModel(fmt.getBitsPerPixel(), fmt.getRedMask(), fmt.getGreenMask(),
            fmt.getBlueMask());/*from w w  w  . j a v a 2 s .  co m*/
    int[] rgb;
    int k;
    for (int i = 0; i < data.length; i++) {
        rgb = dcm.getComponents(data[i], null, 0);
        k = toGray(rgb);
        rgb[0] = rgb[1] = rgb[2] = k;
        ret[i] = dcm.getDataElement(rgb, 0);
    }
    return ret;
}

From source file:lucee.runtime.img.Image.java

private BufferedImage jpgImage(BufferedImage src) {
    int w = src.getWidth();
    int h = src.getHeight();
    SampleModel srcSM = src.getSampleModel();
    WritableRaster srcWR = src.getRaster();
    java.awt.image.DataBuffer srcDB = srcWR.getDataBuffer();

    ColorModel rgb = new DirectColorModel(32, 0xff0000, 65280, 255);
    int[] bitMasks = new int[] { 0xff0000, 65280, 255 };

    SampleModel csm = new SinglePixelPackedSampleModel(3, w, h, bitMasks);
    int data[] = new int[w * h];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int pix[] = null;
            int sample[] = srcSM.getPixel(j, i, pix, srcDB);
            if (sample[3] == 0 && sample[2] == 0 && sample[1] == 0 && sample[0] == 0)
                data[i * w + j] = 0xffffff;
            else//from w w  w  .  j av a  2s. c  om
                data[i * w + j] = sample[0] << 16 | sample[1] << 8 | sample[2];
        }

    }

    java.awt.image.DataBuffer db = new DataBufferInt(data, w * h * 3);
    WritableRaster wr = Raster.createWritableRaster(csm, db, new Point(0, 0));
    return new BufferedImage(rgb, wr, false, null);
}

From source file:org.apache.flex.compiler.internal.embedding.transcoders.JPEGTranscoder.java

private byte[] bufferedImageToJPEG(ImageInfo imageInfo, int[] pixels) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(imageInfo.width, imageInfo.height,
            BufferedImage.TYPE_INT_ARGB);
    bufferedImage.setRGB(0, 0, imageInfo.width, imageInfo.height, pixels, 0, imageInfo.width);

    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageWriteParam writeParam = writer.getDefaultWriteParam();
    ColorModel colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
    ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(colorModel,
            colorModel.createCompatibleSampleModel(1, 1));
    writeParam.setDestinationType(imageTypeSpecifier);
    writeParam.setSourceBands(new int[] { 0, 1, 2 });
    writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

    float q = 1.0f;
    if (quality != null)
        q = quality.floatValue();/*from   w ww .j a v  a2  s.c o  m*/
    writeParam.setCompressionQuality(q);

    DAByteArrayOutputStream buffer = new DAByteArrayOutputStream();
    writer.setOutput(new MemoryCacheImageOutputStream(buffer));

    IIOImage ioImage = new IIOImage(bufferedImage, null, null);

    writer.write(null, ioImage, writeParam);
    writer.dispose();

    return buffer.getDirectByteArray();
}