BufferedImage makeImage() { // Generate 8-bit image //Image img8; byte[] pixels8; int color16; pixels8 = new byte[width*height]; for (int i=0; i<width*height; i++) { color16 = rgb(pixels32[i]); pixels8[i] = (byte)hist[color16]; } SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, new int[] {0}); DataBufferByte Buffer = new DataBufferByte(pixels8, pixels8.length); WritableRaster raster = Raster.createWritableRaster(sampleModel, Buffer, null); return new BufferedImage(cm, raster, false, null); }
BufferedImage createFloatBufferedImage(int w, int h, int bands) { // Define dimensions and layout of the image //int bands = 4; // 4 bands for ARGB, 3 for RGB etc int[] bandOffsets = {0, 1, 2, 3}; // length == bands, 0 == R, 1 == G, 2 == B and 3 == A // Create a TYPE_FLOAT sample model (specifying how the pixels are stored) SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, w, h, bands, w * bands, bandOffsets); // ...and data buffer (where the pixels are stored) DataBuffer buffer = new DataBufferFloat(w * h * bands); // Wrap it in a writable raster WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null); // Create a color model compatible with this sample model/raster (TYPE_FLOAT) // Note that the number of bands must equal the number of color components in the // color space (3 for RGB) + 1 extra band if the color model contains alpha ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); ColorModel colorModel = new ComponentColorModel(colorSpace, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_FLOAT); // And finally create an image with this raster return new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); }
public static void main(String[] args) { byte[][] data = new byte[1][10]; ByteLookupTable lut = new ByteLookupTable(0, data); RasterOp op = new LookupOp(lut, null); int[] bandOffsets = {0}; Point location = new Point(0, 0); DataBuffer db = new DataBufferByte(10 * 10); SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, 10, 10, 1, 10, bandOffsets); Raster src = Raster.createRaster(sm, db, location); op.filter(src, null); // this used to result in NullPointerException }
/** * Construct an alpah channel from the given src, according to * the SVG masking rules. * * @param src The image to convert to an alpha channel (mask image) */ public FilterAsAlphaRed(CachableRed src) { super(new Any2LumRed(src),src.getBounds(), new ComponentColorModel (ColorSpace.getInstance(ColorSpace.CS_GRAY), new int [] {8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE), new PixelInterleavedSampleModel (DataBuffer.TYPE_BYTE, src.getSampleModel().getWidth(), src.getSampleModel().getHeight(), 1, src.getSampleModel().getWidth(), new int [] { 0 }), src.getTileGridXOffset(), src.getTileGridYOffset(), null); props.put(ColorSpaceHintKey.PROPERTY_COLORSPACE, ColorSpaceHintKey.VALUE_COLORSPACE_ALPHA); }
public static SampleModel fixSampleModel(CachableRed src) { ColorModel cm = src.getColorModel(); SampleModel srcSM = src.getSampleModel(); if (cm.hasAlpha()) return srcSM; int w = srcSM.getWidth(); int h = srcSM.getHeight(); int b = srcSM.getNumBands()+1; int [] offsets = new int[b]; for (int i=0; i < b; i++) offsets[i] = i; // Really should check DataType range in srcSM... return new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, w, h, b, w*b, offsets); }
public static PlanarImage createFloatPlanarImage(float[] src, int width, int height){ int[] bandOffsets = {0}; SampleModel sampleModel = new PixelInterleavedSampleModel(TYPE_FLOAT, width, height, 1, width, bandOffsets); ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY); ColorModel colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.TRANSLUCENT, TYPE_FLOAT); PlanarImage opImage; DataBuffer buffer = new DataBufferFloat(width * height); // Wrap it in a writable raster WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null); raster.setPixels(0, 0, width, height, src); // Create an image with this raster BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); opImage = PlanarImage.wrapRenderedImage(image); return opImage; }
private SampleModel createSampleModel() { if (samples == 1) { return new PixelInterleavedSampleModel(dataType, width, height, 1, width, OFFSETS_0); } // samples == 3 if (banded) { return new BandedSampleModel(dataType, width, height, width, OFFSETS_0_1_2, OFFSETS_0_0_0); } if( (!compressed) && pmi.endsWith("422" ) ) { return new PartialComponentSampleModel(width, height, 2, 1); } if( (!compressed) && pmi.endsWith("420") ) { return new PartialComponentSampleModel(width,height,2,2); } return new PixelInterleavedSampleModel(dataType, width, height, 3, width * 3, OFFSETS_0_1_2); }
@Override public SampleModel convert(FieldAccessor fa, Instance instance) throws FieldAccessor.InvalidFieldException { int width = fa.getInt(instance, "width"); // NOI18N int height = fa.getInt(instance, "height"); // NOI18N int dataType = fa.getInt(instance, "dataType"); // NOI18N int pixelStride = fa.getInt(instance, "pixelStride"); // NOI18N int scanlineStride = fa.getInt(instance, "scanlineStride"); // NOI18N int[] bandOffsets = fa.getIntArray(instance, "bandOffsets", false); // NOI18N return new PixelInterleavedSampleModel(dataType, width, height, pixelStride, scanlineStride, bandOffsets); }
public static void main(String[] args) { Vector<Class<? extends SampleModel>> classes = new Vector<Class<? extends SampleModel>>(); classes.add(ComponentSampleModel.class); classes.add(MultiPixelPackedSampleModel.class); classes.add(SinglePixelPackedSampleModel.class); classes.add(BandedSampleModel.class); classes.add(PixelInterleavedSampleModel.class); for (Class<? extends SampleModel> c : classes) { doTest(c); } }
public static void main(String args[]) { BufferedImage src = new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB); // Set src pixel values Color pelColor = new Color(100, 100, 100, 128); for (int i = 0; i < 10; i++) { src.setRGB(0, i, pelColor.getRGB()); } ColorModel cm = new ComponentColorModel (ColorSpace.getInstance(ColorSpace.CS_GRAY), new int [] {8,8}, true, src.getColorModel().isAlphaPremultiplied(), Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); SampleModel sm = new PixelInterleavedSampleModel (DataBuffer.TYPE_BYTE, 100, 100, 2, 200, new int [] { 0, 1 }); WritableRaster wr = Raster.createWritableRaster(sm, new Point(0,0)); BufferedImage dst = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null); dst = dst.getSubimage(0, 0, 1, 10); ColorConvertOp op = new ColorConvertOp(null); op.filter(src, dst); for (int i = 0; i < 10; i++) { if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) { throw new RuntimeException( "Incorrect destination alpha value."); } } }
/** * Create a {@code PixelInterleavedSampleModel} for use in creating * an {@code ImageTypeSpecifier}. Its dimensions will be 1x1 and * it will have ascending band offsets as {0, 1, 2, ..., numBands}. * * @param dataType The data type (DataBuffer.TYPE_*). * @param numBands The number of bands. * @return A {@code PixelInterleavedSampleModel}. */ static SampleModel createInterleavedSM(int dataType, int numBands) { int[] bandOffsets = new int[numBands]; for(int i = 0; i < numBands; i++) { bandOffsets[i] = i; } return new PixelInterleavedSampleModel(dataType, 1, // width 1, // height numBands, // pixelStride, numBands, // scanlineStride bandOffsets); }
private static SampleModel createPixelInterleavedSampleModel (int dataType, int tileWidth, int tileHeight, int bands) { int [] bandOffsets = new int[bands]; for (int i=0; i<bands; i++) bandOffsets[i] = i; return new PixelInterleavedSampleModel (dataType, tileWidth, tileHeight, bands, tileWidth*bands, bandOffsets); }
/** * This function 'fixes' the source's sample model. * Right now it just selects if it should have one or two bands * based on if the source had an alpha channel. */ protected static SampleModel fixSampleModel(CachableRed src) { SampleModel sm = src.getSampleModel(); int width = sm.getWidth(); int height = sm.getHeight(); ColorModel cm = src.getColorModel(); if (cm != null) { if (cm.hasAlpha()) return new PixelInterleavedSampleModel (DataBuffer.TYPE_BYTE, width, height, 2, 2*width, new int [] { 0, 1 }); return new PixelInterleavedSampleModel (DataBuffer.TYPE_BYTE, width, height, 1, width, new int [] { 0 }); } else { // No ColorModel so try to make some intelligent // decisions based just on the number of bands... // 1 bands -> lum // 2 bands -> lum (Band 0) & alpha (Band 1) // >2 bands -> lum (Band 0) - No color conversion... if (sm.getNumBands() == 2) return new PixelInterleavedSampleModel (DataBuffer.TYPE_BYTE, width, height, 2, 2*width, new int [] { 0, 1 }); return new PixelInterleavedSampleModel (DataBuffer.TYPE_BYTE, width, height, 1, width, new int [] { 0 }); } }
private static BufferedImage createTestImage2(int nbits, int transfertype) { final int colorShift = 2; int SIZE = 256; BufferedImage image = null; ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY); ColorModel colorModel = new ComponentColorModel(colorSpace, new int[] {nbits}, false, false, Transparency.OPAQUE, transfertype); SampleModel sampleModel = new PixelInterleavedSampleModel(transfertype, SIZE, SIZE, 1, SIZE, new int[] {0}); image = new BufferedImage(colorModel, Raster.createWritableRaster(sampleModel, null), false, null); WritableRaster raster = image.getWritableTile(0, 0); int[] samples = raster.getSamples(0, 0, SIZE, SIZE, 0, (int[])null); int off = 0; int[] row = new int[SIZE]; for(int i = 0; i < SIZE; i++) { Arrays.fill(row, i << colorShift); System.arraycopy(row, 0, samples, off, SIZE); off += SIZE; } raster.setSamples(0, 0, SIZE, SIZE, 0, samples); return image; }
private static BufferedImage createTestImage3(int nbits, int transfertype) { final int colorShift = 2; int SIZE = 256; BufferedImage image = null; ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); ColorModel colorModel = new IndexColorModel(nbits, 4, new byte[] { (byte)255, 0, 0, (byte)255}, new byte[] { 0, (byte)255, 0, (byte)255}, new byte[] { 0, 0, (byte)255, (byte)255}); SampleModel sampleModel = new PixelInterleavedSampleModel(transfertype, SIZE, SIZE, 1, SIZE, new int[] {0}); image = new BufferedImage(colorModel, Raster.createWritableRaster(sampleModel, null), false, null); Graphics2D g = image.createGraphics(); g.setColor(Color.white); g.fillRect(0,0, SIZE, SIZE); g.setColor(Color.red); g.fillOval(10, 10, SIZE -20, SIZE-20); return image; }
private static SampleModel createSampleModel( int width, int height, int bytesPerPixel, int[] bandOffsets ) { PixelInterleavedSampleModel csm = new PixelInterleavedSampleModel( DataBuffer.TYPE_BYTE, width, height, bytesPerPixel, width * bytesPerPixel, bandOffsets ); return ( csm ); }
private static BufferedImage rawImage32toARGB(RawImage rawImage) { // Do as much as we can to not make an extra copy of the data. This is just a bunch of // classes that wrap's the raw byte array of the image data. DataBufferByte dataBuffer = new DataBufferByte(rawImage.data, rawImage.size); PixelInterleavedSampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, rawImage.width, rawImage.height, 4, rawImage.width * 4, BAND_OFFSETS_32); WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0)); return new BufferedImage(new ThirtyTwoBitColorModel(rawImage), raster, false, EMPTY_HASH); }
private static BufferedImage rawImage16toARGB(BufferedImage image, RawImage rawImage) { // Do as much as we can to not make an extra copy of the data. This is just a bunch of // classes that wrap's the raw byte array of the image data. DataBufferByte dataBuffer = new DataBufferByte(rawImage.data, rawImage.size); PixelInterleavedSampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, rawImage.width, rawImage.height, 2, rawImage.width * 2, BAND_OFFSETS_16); WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0)); return new BufferedImage(new SixteenBitColorModel(rawImage), raster, false, EMPTY_HASH); }
public static DecodeResult decode(byte[] i_src) throws IOException { JPEGImageReader jr=(JPEGImageReader)ImageIO.getImageReadersBySuffix("jpeg").next(); jr.setInput(new MemoryCacheImageInputStream(new ByteArrayInputStream(i_src))); IIOMetadata data = jr.getImageMetadata(0); Element tree = (Element)data.getAsTree("javax_imageio_jpeg_image_1.0"); Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0); int xd=Integer.parseInt(jfif.getAttribute("Xdensity")); int yd=Integer.parseInt(jfif.getAttribute("Ydensity")); int unit=Integer.parseInt(jfif.getAttribute("resUnits")); BufferedImage b=jr.read(0); jr.dispose();//dispose //カラーモデルの分析 SampleModel sp=b.getSampleModel(); if(sp instanceof PixelInterleavedSampleModel){ PixelInterleavedSampleModel ssp=(PixelInterleavedSampleModel)sp; int data_type=ssp.getDataType(); int scan_stride=ssp.getScanlineStride(); if(data_type==DataBuffer.TYPE_BYTE && scan_stride==b.getWidth()){ DataBufferByte bb=(DataBufferByte)b.getData().getDataBuffer(); return new DecodeResult(xd,yd,bb.getData(),b.getWidth(),b.getHeight(),unit); }else{ //nothing todo } }else{ //nothing todo } throw new NyARRuntimeException(); }