BufferedImage imageFromRawData(int width, int height, int scanLine, byte[] data) { DataBuffer buffer = new DataBufferByte(data, data.length); WritableRaster raster = Raster.createInterleavedRaster( buffer, width, height, scanLine, 3, new int[]{0, 1, 2}, null ); ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); }
/** * Returns the {@code String} representation of the contents of this {@code ColorModel}object. * * @return a {@code String} representing the contents of this {@code ColorModel} object. */ public String toString() { return new String( "ComponentColorModelJAI: #pixelBits = " + pixel_bits + " numComponents = " + super.getNumComponents() + " color space = " + super.getColorSpace() + " transparency = " + super.getTransparency() + " has alpha = " + super.hasAlpha() + " isAlphaPre = " + super.isAlphaPremultiplied()); } }
@Override public Graphics2D prepare(Component component, Graphics2D graphics) { this.graphics = graphics; int width = component.getWidth(); int height = component.getHeight(); /* To convert to gray, we create a BufferedImage in the grayscale color * space into which the decorated component draws, and we output the * resulting image. The naive way to create the buffer is new * BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); but that * doesn't respect transparency. Hence the following more complicated * method. */ if (bufferedImage == null || bufferedImage.getWidth() < width || bufferedImage.getHeight() < height) { ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY); ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); WritableRaster raster = ccm.createCompatibleWritableRaster(width, height); bufferedImage = new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null); } bufferedImageGraphics = bufferedImage.createGraphics(); bufferedImageGraphics.setClip(graphics.getClip()); return bufferedImageGraphics; }
!(img.getSampleModel() instanceof BandedSampleModel) || (imgCM.hasAlpha() && imgCM.isAlphaPremultiplied() )) { ComponentColorModel imgCompCM = new ComponentColorModel WritableRaster wr = Raster.createBandedRaster (DataBuffer.TYPE_BYTE, argbWR.getWidth(), argbWR.getHeight(), imgCompCM.getNumComponents(), new Point(0, 0)); BufferedImage imgComp = new BufferedImage (imgCompCM, wr, imgCompCM.isAlphaPremultiplied(), null); ComponentColorModel newCM = new ComponentColorModel (newCM, srcWR, newCM.isAlphaPremultiplied(), null); ComponentColorModel sRGBCompCM = new ComponentColorModel (ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8, 8, 8}, false, WritableRaster wr = Raster.createBandedRaster (DataBuffer.TYPE_BYTE, argbWR.getWidth(), argbWR.getHeight(), sRGBCompCM.getNumComponents(), new Point(0, 0)); argbWR.getWidth(), new int[]{0, 1, 2, 3}, new int[]{0, 0, 0, 0}, new Point(0, 0));
@Test public void testExpandGrayCaseC() { // create gray indexed image BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_BYTE_GRAY); Graphics2D gr = bi.createGraphics(); gr.setColor(Color.GRAY); gr.fillRect(0, 0, 10, 10); gr.dispose(); // create a RGB color model final ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, Transparency.OPAQUE, bi.getSampleModel().getDataType()); // set the destination image layout final ImageLayout2 il = new ImageLayout2(bi); il.setColorModel(cm); il.setSampleModel(cm.createCompatibleSampleModel(bi.getWidth(), bi.getHeight())); RenderingHints ri = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, il); // perform color expansion ParameterBlockJAI pbj = new ParameterBlockJAI("ColorConvert"); pbj.addSource(bi); pbj.setParameter("colorModel", cm); pbj.setParameter("noData", RangeFactory.create(-1, -1)); RenderedOp finalimage = JAI.create("ColorConvert", pbj, ri); int[] pixel = new int[3]; finalimage.getData().getPixel(0, 0, pixel); assertEquals(128, pixel[0]); assertEquals(128, pixel[1]); assertEquals(128, pixel[2]); }
/** * Converts the decoded buffer into a BufferedImage. * precision: 16 bit, componentCount = 1 * * @param decoded data buffer * @param precision * @param width of the image * @param height of the image @return a BufferedImage.TYPE_USHORT_GRAY */ private BufferedImage to16Bit1ComponentGrayScale(int[][] decoded, int precision, int width, int height) { BufferedImage image; if (precision == 16) { image = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY); } else { ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] {precision}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT); image = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(width, height), colorModel.isAlphaPremultiplied(), null); } short[] imageBuffer = ((DataBufferUShort) image.getRaster().getDataBuffer()).getData(); for (int i = 0; i < imageBuffer.length; i++) { imageBuffer[i] = (short) decoded[0][i]; } return image; }
public static BufferedImage imageFromArray(float[] input, int width, int height) { SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, width, height, 1, width, new int[]{0}); ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY); ColorModel colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_FLOAT); DataBuffer buffer = new DataBufferFloat(width * height * 1); WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null); raster.setPixels(0, 0, width, height, input); return new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); }
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); raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, 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);
DataBuffer videoBuffer = new DataBufferByte(frame, frame.length); ComponentSampleModel sampleModel = new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 3, width * 3, new int[] {0, 1, 2}); this.grid = Raster.createWritableRaster(sampleModel, videoBuffer, null); ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), null, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); this.image = new BufferedImage(colorModel, this.grid, false, null); } catch (final OutOfMemoryError e) { this.frame = null; try { this.image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED); } catch (final OutOfMemoryError ee) { try { this.image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); } catch (final OutOfMemoryError eee) { this.image = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY);
final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); final Graphics2D g = bi.createGraphics(); bi.flush(); final BufferedImage bi = new BufferedImage(paint.getComponentColorModel(), paint.getComponentColorModel().createCompatibleWritableRaster(width, height), false, null); final Graphics2D g = bi.createGraphics(); final int yN = Math.min(height, sBounds.y + sBounds.height); final byte[] bpix = ((DataBufferByte)bi.getRaster().getDataBuffer()).getData(); final float value = labels.get(ali);
@Override protected BufferedImage createImage(int width, int height) { ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY); ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); WritableRaster raster = ccm.createCompatibleWritableRaster(width, height); return new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null); } }
if (zSize == 1) { ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); cm = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); cm = new ComponentColorModel(cs, nBits, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); cm = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); cm = new ComponentColorModel(cs, nBits, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); WritableRaster r = cm.createCompatibleWritableRaster(xSize, ySize); BufferedImage bi = new BufferedImage(cm, r, false, null);
out = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); }else if ( (rgb && alpha) || (luminance && alpha) ){ if (do16bit){ if (fullalpha){ ColorModel model = AWTLoader.AWT_RGBA4444; WritableRaster raster = model.createCompatibleWritableRaster(width, width); out = new BufferedImage(model, raster, false, null); }else{ ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); int[] nBits = {5, 5, 5, 1}; int[] bOffs = {0, 1, 2, 3}; ColorModel colorModel = new ComponentColorModel(cs, nBits, true, false, Transparency.BITMASK, DataBuffer.TYPE_BYTE); WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width*2, 2, bOffs, null); out = new BufferedImage(colorModel, raster, false, null);
final ColorModel cm = image.getColorModel(); if (cm instanceof ComponentColorModel) { final SampleModel sm = this.image.getSampleModel(); final int datatype = sm.getDataType(); Range noData = getNoData(); final boolean gray = for (int i = 0; i < numDestinationBands; i++) bits[i] = sm.getSampleSize(i); final ComponentColorModel destinationColorModel = new ComponentColorModel( numDestinationBands >= 3 ? ColorSpace.getInstance(ColorSpace.CS_sRGB) : ColorSpace.getInstance(ColorSpace.CS_GRAY), bits, alpha, cm.isAlphaPremultiplied(), alpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, datatype); final SampleModel destinationSampleModel = destinationColorModel.createCompatibleSampleModel( image.getWidth(), image.getHeight()); layout.setColorModel(destinationColorModel); layout.setSampleModel(destinationSampleModel);
@Override public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException { readHeader(); if (imageIndex > 0) { throw new ArrayIndexOutOfBoundsException("imageIndex is " + imageIndex + " must be 0."); } ImageInputStream in = (ImageInputStream) getInput(); in.seek(dataOffset + imageIndex * width * height * (maxGray > 255 ? 2 : 1)); ComponentColorModel ccm = new ComponentColorModel(// new ICC_ColorSpace(ICC_Profile.getInstance(ColorSpace.CS_GRAY)), new int[]{maxGray > 255 ? 16 : 8},// false, false, Transparency.OPAQUE,// (maxGray > 255) ? DataBuffer.TYPE_SHORT : DataBuffer.TYPE_BYTE); SampleModel sm = ccm.createCompatibleSampleModel(width, height); BufferedImage img; if (maxGray > 255) { DataBufferShort db = new DataBufferShort(width * height); in.readFully(db.getData(), 0, width * height); img = new BufferedImage(ccm, Raster.createWritableRaster(sm, db, new Point(0, 0)), false, new Hashtable<Object, Object>()); } else { DataBufferByte db = new DataBufferByte(width * height); in.readFully(db.getData(), 0, width * height); img = new BufferedImage(ccm, Raster.createWritableRaster(sm, db, new Point(0, 0)), false, new Hashtable<Object, Object>()); } return img; }
protected static BufferedImage createBufferedImage(int w, int h) { ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); int[] nBits = {8, 8, 8, 8}; ColorModel cm = new ComponentColorModel(cs, nBits, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); SampleModel sm = cm.createCompatibleSampleModel(w, h); DataBufferByte db = new DataBufferByte(w * h * 4); WritableRaster r = WritableRaster.createWritableRaster(sm, db, new Point(0, 0)); BufferedImage bm = new BufferedImage(cm, r, false, null); return bm; }
private BufferedImage readTrueColorPcx(InputStream is) throws Exception { byte brgb[] = IOUtils.readFully(is, 4096); byte pixels[] = new byte[bytesPerLine*NPlanes*height]; /** * A BufferedInputStream could have been constructed from the InputStream, * but for maximum decoding speed, one time reading of the image data * into memory is ideal though this is memory consuming. */ LOGGER.info("true color pcx image!"); readScanLines(brgb, brgb.length, pixels); is.close(); DataBuffer db = new DataBufferByte(pixels, pixels.length); int trans = Transparency.OPAQUE; int[] nBits = {8, 8, 8}; WritableRaster raster = Raster.createBandedRaster(db, width, height, bytesPerLine*3, new int[]{0, 0, 0}, new int[] {0, bytesPerLine, bytesPerLine*2}, null); ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), nBits, false, false, trans, DataBuffer.TYPE_BYTE); return new BufferedImage(cm, raster, false, null); }
checkReadParamBandSettings(pParam, isConvertToRGB() ? 3 : 1, image.getSampleModel().getNumBands()); WritableRaster destination = image.getRaster(); if (destinationBands != null || offset.x != 0 || offset.y != 0) { destination = destination.createWritableChild(0, 0, destination.getWidth(), destination.getHeight(), offset.x, offset.y, destinationBands); cm = new ComponentColorModel( ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8, 8, 8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE ); raster = Raster.createInterleavedRaster( DataBuffer.TYPE_BYTE, width, 1, width * 3, 3, new int[]{2, 1, 0}, null ); final byte[] data = ((DataBufferByte) raster.getDataBuffer()).getData(); if (cm.isCompatibleRaster(destination)) { for (int srcX = 0; srcX < sourceRow.getWidth(); srcX += sourceXSubsampling) { dataElements = sourceRow.getDataElements(srcX, 0, dataElements); int dstX = /*offset.x +*/ srcX / sourceXSubsampling; destination.setDataElements(dstX, dstY, dataElements); converter = new ColorConvertOp(cm.getColorSpace(), image.getColorModel().getColorSpace(), null);
new ComponentColorModel( ColorSpace.getInstance(ColorSpace.CS_GRAY), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); final BufferedImage image = new BufferedImage(cm, raster, false, null);
this.numColors = numColors; this.alphaThreshold = alpaThreshold; if (image.getColorModel().hasAlpha()) { RenderedImage alpha = BandSelectDescriptor.create( image, new int[] {image.getSampleModel().getNumBands() - 1}, null); alpha = MultiplyConstDescriptor.create(alpha, new double[] {alphaThreshold}, null); image = BandSelectDescriptor.create(image, new int[] {0, 1, 2}, null); new ComponentColorModel( ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false,