Example usage for java.awt.image BufferedImage BufferedImage

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

Introduction

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

Prototype



public BufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied,
        Hashtable<?, ?> properties) 

Source Link

Document

Constructs a new BufferedImage with a specified ColorModel and Raster .

Usage

From source file:image.text.CreateTifAndAnnotate.java

public void createAnnotatedTif(String[] args, OutputStream out) throws Exception {

    byte[] byteArray = new byte[] { -1, 0 };
    ColorModel colorModel = new IndexColorModel(1, 2, byteArray, byteArray, byteArray);

    WritableRaster writeableRaster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1700, 2200, 1, 1, null);
    BufferedImage bufImg = new BufferedImage(colorModel, writeableRaster, false, null);

    // -------------------------------------------------------------------        
    Graphics2D g2d = bufImg.createGraphics();
    g2d.setColor(Color.black);//from w ww . j  a va2s  .  c om

    Font font = new Font("Arial Bold", Font.PLAIN, 36);
    g2d.setFont(font);
    int vertPos = 200;
    for (int i = 0; i < args.length; i++) {
        g2d.drawString(args[i], 75, vertPos);
        vertPos += 48;
    }

    PlanarImage planarImage = PlanarImage.wrapRenderedImage(bufImg);

    TIFFEncodeParam encodeParam = new TIFFEncodeParam();
    encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
    encodeParam.setWriteTiled(false); // false means use strips.
    encodeParam.setTileSize(0, 0); // tiling will make the file size much larger. 
    encodeParam.setLittleEndian(true); // create an Intel (II) format image

    String SoftwareVersion[] = new String[] { "TIFF by Joe, " + this.getClass().getName() };
    String docName[] = new String[] { "JoesTifAnnotator" };

    // Create a new TIFF fields including a new TIFF ASCII TIFF tag.
    TIFFField tiffFields[] = new TIFFField[5];

    tiffFields[0] = new TIFFField(269, TIFFField.TIFF_ASCII, docName.length, docName);
    tiffFields[1] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } });
    tiffFields[2] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } });
    // resolution unit 
    tiffFields[3] = new TIFFField(296, TIFFField.TIFF_SHORT, 1, new char[] { 2 });
    tiffFields[4] = new TIFFField(305, TIFFField.TIFF_ASCII, SoftwareVersion.length, SoftwareVersion);

    encodeParam.setExtraFields(tiffFields);

    TIFFImageEncoder encoder = new TIFFImageEncoder(out, encodeParam);
    encoder.encode(planarImage);
}

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:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static BufferedImage makeTransparent(BufferedImage image, int x, int y) {
    ColorModel cm = image.getColorModel();
    if (!(cm instanceof IndexColorModel))
        return image; // sorry...
    IndexColorModel icm = (IndexColorModel) cm;
    WritableRaster raster = image.getRaster();
    int pixel = raster.getSample(x, y, 0); // pixel is offset in ICM's
    // palette/* ww w  .  j a  va  2 s .  c o m*/
    int size = icm.getMapSize();
    byte[] reds = new byte[size];
    byte[] greens = new byte[size];
    byte[] blues = new byte[size];
    icm.getReds(reds);
    icm.getGreens(greens);
    icm.getBlues(blues);
    IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel);
    return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null);
}

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

/**
 * Convert a SWT Image to a {@link BufferedImage}
 * //from  w  w  w.ja  v a2s . c o  m
 * {@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:oct.util.Util.java

public static BufferedImage deepCopyBufferedImage(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = bi.copyData(null);
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}

From source file:GraphicsUtil.java

/**
 * Constructs a BufferedImage with a linear sRGB colorModel, and alpha.
 * @param width   The desired width of the BufferedImage
 * @param height  The desired height of the BufferedImage
 * @param premult The desired state of alpha premultiplied
 * @return        The requested BufferedImage.
 *//*  ww w.  ja v  a 2  s.c om*/
public static BufferedImage makeLinearBufferedImage(int width, int height, boolean premult) {
    ColorModel cm = makeLinear_sRGBCM(premult);
    WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
    return new BufferedImage(cm, wr, premult, null);
}