/** * <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> * will unmanage the image.</p> * * @param img the destination image * @param x the x location at which to start storing pixels * @param y the y location at which to start storing pixels * @param w the width of the rectangle of pixels to store * @param h the height of the rectangle of pixels to store * @param pixels an array of pixels, stored as integers * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length < w*h */ static void setPixels(BufferedImage img, int x, int y, int w, int h, byte[] pixels) { if (pixels == null || w == 0 || h == 0) { return; } else if (pixels.length < w * h) { throw new IllegalArgumentException("pixels array must have a length >= w*h"); } int imageType = img.getType(); if (imageType == BufferedImage.TYPE_BYTE_GRAY) { WritableRaster raster = img.getRaster(); raster.setDataElements(x, y, w, h, pixels); } else { throw new IllegalArgumentException("Only type BYTE_GRAY is supported"); } }
@Override public String convert(FieldAccessor fa, Instance instance) throws FieldAccessor.InvalidFieldException { Instance raster = fa.getInstance(instance, "raster", WritableRaster.class, true); // NOI18N int width = fa.getInt(raster, "width"); // NOI18N int height = fa.getInt(raster, "height"); // NOI18N Instance colorModel = fa.getInstance(instance, "colorModel", ColorModel.class, true); int color_count = 0; if (FieldAccessor.isInstanceOf(colorModel, IndexColorModel.class)) { color_count = DetailsUtils.getIntFieldValue(colorModel, "map_size", 0); // NOI18N } if (color_count > 0) { return Bundle.ImageDetailProvider_ImageDescrColors(width, height, color_count); } else { return Bundle.ImageDetailProvider_ImageDescr(width, height); } }
/** * Loads a single glyph to the backing texture, if it fits. * * @param glyph The glyph to be rendered * @param width The expected width of the glyph * @param height The expected height of the glyph * @throws SlickException if the glyph could not be rendered. */ private void renderGlyph(Glyph glyph, int width, int height) throws SlickException { // Draw the glyph to the scratch image using Java2D. scratchGraphics.setComposite(AlphaComposite.Clear); scratchGraphics.fillRect(0, 0, MAX_GLYPH_SIZE, MAX_GLYPH_SIZE); scratchGraphics.setComposite(AlphaComposite.SrcOver); scratchGraphics.setColor(java.awt.Color.white); for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext();) ((Effect)iter.next()).draw(scratchImage, scratchGraphics, unicodeFont, glyph); glyph.setShape(null); // The shape will never be needed again. WritableRaster raster = scratchImage.getRaster(); int[] row = new int[width]; for (int y = 0; y < height; y++) { raster.getDataElements(0, y, width, 1, row); scratchIntBuffer.put(row); } GL.glTexSubImage2D(SGL.GL_TEXTURE_2D, 0, pageX, pageY, width, height, SGL.GL_BGRA, SGL.GL_UNSIGNED_BYTE, scratchByteBuffer); scratchIntBuffer.clear(); glyph.setImage(pageImage.getSubImage(pageX, pageY, width, height)); }
public static List<Integer> getPixeisVizinhos3x3(BufferedImage img, int i, int j, int tipoPixel) { List<Integer> pixeisVizinhos = new ArrayList<>(); WritableRaster raster = img.getRaster(); int pixels[] = new int[4]; pixeisVizinhos.add(raster.getPixel(i - 1, j - 1, pixels)[tipoPixel]); pixeisVizinhos.add(raster.getPixel(i - 1, j, pixels)[tipoPixel]); pixeisVizinhos.add(raster.getPixel(i - 1, j + 1, pixels)[tipoPixel]); pixeisVizinhos.add(raster.getPixel(i, j - 1, pixels)[tipoPixel]); pixeisVizinhos.add(raster.getPixel(i, j, pixels)[tipoPixel]); pixeisVizinhos.add(raster.getPixel(i, j + 1, pixels)[tipoPixel]); pixeisVizinhos.add(raster.getPixel(i + 1, j - 1, pixels)[tipoPixel]); pixeisVizinhos.add(raster.getPixel(i + 1, j, pixels)[tipoPixel]); pixeisVizinhos.add(raster.getPixel(i + 1, j + 1, pixels)[tipoPixel]); Collections.sort(pixeisVizinhos); return pixeisVizinhos; }
public BufferedImage eval() throws Exception { if (dw < 1 || dh < 1) return ImageUtils.NULL_IMAGE; // ensure that src is a type which GeneralFilter can handle final BufferedImage src = ImageUtils.coerceToIntType(sop.getImage(null)); final Rectangle sr = new Rectangle(0, 0, (int)(sop.getWidth()*scale), (int)(sop.getHeight()*scale)); final WritableRaster dstR = src.getColorModel() .createCompatibleWritableRaster(dw, dh) .createWritableTranslatedChild(dx0, dy0); // zoom! zoom! GeneralFilter.zoom(dstR, sr, src, scale < 1.0f ? downFilter : upFilter); return ImageUtils.toCompatibleImage(new BufferedImage( src.getColorModel(), dstR.createWritableTranslatedChild(0,0), src.isAlphaPremultiplied(), null )); }
@Override protected BufferedImage sliceTile() { // get actual tile width, height (edge tiles can be less than full size) final int atw = Math.min(tw, dw - tx*tw); final int ath = Math.min(th, dh - ty*th); final int type = src.getType(); // scale the tile from the source image final BufferedImage tile = new BufferedImage(atw, ath, type); final WritableRaster tileR = tile.getRaster().createWritableTranslatedChild(tx*tw, ty*th); final Rectangle dstFR = new Rectangle(0, 0, dw, dh); GeneralFilter.zoom(tileR, dstFR, src, filter); return tile; }
protected RenderedImage getIndexedImage() { IndexColorModel icm = getIndexColorModel(); BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED, icm); WritableRaster wr = dst.getRaster(); for (int y =0; y < dst.getHeight(); y++) { for (int x = 0; x < dst.getWidth(); x++) { Color aColor = getSrcColor(x,y); wr.setSample(x, y, 0, findColorIndex(root, aColor)); } } return dst; }
private static int[] getSplits(BufferedImage image, String name) { WritableRaster raster = image.getRaster(); int startX = getSplitPoint(raster, name, 1, 0, true, true); int endX = getSplitPoint(raster, name, startX, 0, false, true); int startY = getSplitPoint(raster, name, 0, 1, true, false); int endY = getSplitPoint(raster, name, 0, startY, false, false); getSplitPoint(raster, name, endX + 1, 0, true, true); getSplitPoint(raster, name, 0, endY + 1, true, false); if (startX == 0 && endX == 0 && startY == 0 && endY == 0) return null; if (startX != 0) { startX--; endX = raster.getWidth() - 2 - (endX - 1); } else { endX = raster.getWidth() - 2; } if (startY != 0) { startY--; endY = raster.getHeight() - 2 - (endY - 1); } else { endY = raster.getHeight() - 2; } return new int[]{startX, endX, startY, endY}; }
private static void crashTest() { Raster src = createSrcRaster(); WritableRaster dst = createDstRaster(); ConvolveOp op = createConvolveOp(ConvolveOp.EDGE_NO_OP); try { op.filter(src, dst); } catch (ImagingOpException e) { /* * The test pair of source and destination rasters * may cause failure of the medialib convolution routine, * so this exception is expected. * * The JVM crash is the only manifestation of this * test failure. */ } System.out.println("Test PASSED."); }
@Test public void createAnBlankAndWhitePngImage() throws IOException { int m = 5; int n = 5; BufferedImage image = new BufferedImage(m, n, BufferedImage.TYPE_BYTE_BINARY); WritableRaster raster = image.getRaster(); for (int x = 0; x < m; x++) { for (int y = 0; y < m; y++) { int index = (x + y) % 2 == 0 ? 1: 0; raster.setPixel(x, y, new int[]{ index }); } } File output = new File("src/test/resources/png-test.black-white.png"); ImageIO.write(image, "png", output); }
public BufferedImage thresholdImage(BufferedImage image, int threshold) { BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); result.getGraphics().drawImage(image, 0, 0, null); WritableRaster raster = result.getRaster(); int[] pixels = new int[image.getWidth()]; for (int y = 0; y < image.getHeight(); y++) { raster.getPixels(0, y, image.getWidth(), 1, pixels); for (int i = 0; i < pixels.length; i++) { if (pixels[i] < threshold) { pixels[i] = 0; } else { pixels[i] = 255; } } raster.setPixels(0, y, image.getWidth(), 1, pixels); } return result; }
/** * <p>Writes a rectangular area of pixels in the destination * <code>BufferedImage</code>. Calling this method on * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p> * * @param img the destination image * @param x the x location at which to start storing pixels * @param y the y location at which to start storing pixels * @param w the width of the rectangle of pixels to store * @param h the height of the rectangle of pixels to store * @param pixels an array of pixels, stored as integers * @throws IllegalArgumentException is <code>pixels</code> is non-null and * of length < w*h */ public static void setPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) { if (pixels == null || w == 0 || h == 0) { return; } else if (pixels.length < w * h) { throw new IllegalArgumentException("pixels array must have a length" + " >= w*h"); } int imageType = img.getType(); if (imageType == BufferedImage.TYPE_INT_ARGB || imageType == BufferedImage.TYPE_INT_RGB) { WritableRaster raster = img.getRaster(); raster.setDataElements(x, y, w, h, pixels); } else { // Unmanages the image img.setRGB(x, y, w, h, pixels, 0, w); } }
public synchronized Raster getRaster(int x, int y, int w, int h) { WritableRaster t = savedTile; if (t == null || w > t.getWidth() || h > t.getHeight()) { t = getColorModel().createCompatibleWritableRaster(w, h); IntegerComponentRaster icr = (IntegerComponentRaster) t; Arrays.fill(icr.getDataStorage(), color); // Note - markDirty is probably unnecessary since icr is brand new icr.markDirty(); if (w <= 64 && h <= 64) { savedTile = t; } } return t; }
static synchronized WritableRaster makeByteRaster(Raster srcRas, int w, int h) { if (byteRasRef != null) { WritableRaster wr = (WritableRaster) byteRasRef.get(); if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) { byteRasRef = null; return wr; } } // If we are going to cache this Raster, make it non-tiny if (w <= 32 && h <= 32) { w = h = 32; } return srcRas.createCompatibleWritableRaster(w, h); }
protected static BufferedImage createComponentImage(int w, int h, ComponentColorModel cm) { WritableRaster wr = cm.createCompatibleWritableRaster(w, h); BufferedImage img = new BufferedImage(cm, wr, false, null); Graphics2D g = img.createGraphics(); int width = w / 8; Color[] colors = new Color[8]; colors[0] = Color.red; colors[1] = Color.green; colors[2] = Color.blue; colors[3] = Color.white; colors[4] = Color.black; colors[5] = new Color(0x80, 0x80, 0x80, 0x00); colors[6] = Color.yellow; colors[7] = Color.cyan; for (int i = 0; i < 8; i++) { g.setColor(colors[i]); g.fillRect(i * width, 0, width, h); } return img; }
/** * Converts raw data into JPG image and encode it into Base64 string for sending it to the JS client * * @param rawImg * raw image bytes * @return Base64 encoded string with JPG image */ private String bufferAsJpgString(byte[] rawImg) { int[] pixels = new int[rawImg.length]; for (int i = 0; i < rawImg.length; i++) { pixels[i] = (int) rawImg[i]; } DataBufferInt buffer = new DataBufferInt(pixels, pixels.length); WritableRaster raster = Raster.createPackedRaster(buffer, IMG_SIZE, IMG_SIZE, IMG_SIZE, BAND_MASKS, null); ColorModel cm = ColorModel.getRGBdefault(); BufferedImage image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); byte[] imgBytes = null; try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { ImageIO.write(image, "JPG", baos); baos.flush(); imgBytes = baos.toByteArray(); } catch (IOException e) { // TODO log exception } byte[] encoded = Base64.getEncoder().encode(imgBytes); return new String(encoded); }
private static RemoteScreenshot createRemoteAWTScreenshot(DebuggerEngine engine, String title, int width, int height, int[] dataArray, AWTComponentInfo componentInfo) { final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); WritableRaster raster = bi.getRaster(); raster.setDataElements(0, 0, width, height, dataArray); if (FAST_FIELDS_SEARCH) { ComponentsFieldFinder.findFieldsForComponents(componentInfo); } return new RemoteScreenshot(engine, title, width, height, bi, componentInfo); }
@Override public BufferedImage createCompatibleImage(int width, int height) { ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff); WritableRaster raster = model.createCompatibleWritableRaster(width, height); return new BufferedImage(model, raster, model.isAlphaPremultiplied(), null); }
/** * Creates a Raster with the same layout but using a different * width and height, and with new zeroed data arrays. */ public WritableRaster createCompatibleWritableRaster(int w, int h) { if (w <= 0 || h <=0) { throw new RasterFormatException("negative "+ ((w <= 0) ? "width" : "height")); } SampleModel sm = sampleModel.createCompatibleSampleModel(w, h); return new ByteInterleavedRaster(sm, new Point(0,0)); }
public static PaintContext getContext(BufferedImage bufImg, AffineTransform xform, RenderingHints hints, Rectangle devBounds) { WritableRaster raster = bufImg.getRaster(); ColorModel cm = bufImg.getColorModel(); int maxw = devBounds.width; Object val = hints.get(RenderingHints.KEY_INTERPOLATION); boolean filter = (val == null ? (hints.get(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY) : (val != RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)); if (raster instanceof IntegerInterleavedRaster && (!filter || isFilterableDCM(cm))) { IntegerInterleavedRaster iir = (IntegerInterleavedRaster) raster; if (iir.getNumDataElements() == 1 && iir.getPixelStride() == 1) { return new Int(iir, cm, xform, maxw, filter); } } else if (raster instanceof ByteInterleavedRaster) { ByteInterleavedRaster bir = (ByteInterleavedRaster) raster; if (bir.getNumDataElements() == 1 && bir.getPixelStride() == 1) { if (filter) { if (isFilterableICM(cm)) { return new ByteFilter(bir, cm, xform, maxw); } } else { return new Byte(bir, cm, xform, maxw); } } } return new Any(raster, cm, xform, maxw, filter); }
@Override public byte[] glitchPixels(byte[] inputImageBytes) throws Exception { int audioBitRate = ((Integer) getPixelGlitchParameters().get("bitRateBlend")).intValue(); float bitRateBlend = (float) audioBitRate / 10; if(bitRateBlend < 0.1F || bitRateBlend > 0.9F) { return null; } BufferedImage inputImage = ImageUtil.getImageFromBytes(inputImageBytes); InputStream imageInputStream = new ByteArrayInputStream(inputImageBytes); AudioInputStream distortionAudioStream = new AudioInputStream(imageInputStream, new AudioFormat(AudioFormat.Encoding.ULAW, ThreadLocalRandom.current().nextInt(8000, 20000), 8, 5, 9, ThreadLocalRandom.current().nextInt(8000, 20000), true), inputImageBytes.length); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); AudioSystem.write(distortionAudioStream, Type.WAVE, outputStream); BufferedImage outputImage = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); byte[] imageData = ((DataBufferByte) outputImage.getRaster().getDataBuffer()).getData(); System.arraycopy(outputStream.toByteArray(),0,imageData,0,outputStream.toByteArray().length); int[] abgrOffsets = {3, 2, 1, 0}; DataBuffer outputBuffer = new DataBufferByte(imageData, imageData.length); WritableRaster raster = Raster.createInterleavedRaster(outputBuffer, inputImage.getWidth(), inputImage.getHeight(), 4 * inputImage.getWidth(), 4, abgrOffsets, null); ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); BufferedImage rasterizedImage = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); rasterizedImage = resizeImage(rasterizedImage, inputImage.getWidth() * 4, inputImage.getHeight() * 4); Graphics2D g2d = rasterizedImage.createGraphics(); g2d.setComposite(AlphaComposite.SrcOver.derive(bitRateBlend)); g2d.drawImage(inputImage, 0, 0, null); g2d.dispose(); rasterizedImage = rasterizedImage.getSubimage(0, 0, inputImage.getWidth(), inputImage.getHeight()); return ImageUtil.getImageBytes(rasterizedImage); }
/** * Creates a new managed image of the given width and height * that is associated with the target Component. */ public Image createAcceleratedImage(Component target, int width, int height) { ColorModel model = getColorModel(Transparency.OPAQUE); WritableRaster wr = model.createCompatibleWritableRaster(width, height); return new OffScreenImage(target, model, wr, model.isAlphaPremultiplied()); }
/** * Creates a Writable subRaster given a region of the Raster. The x and y * coordinates specify the horizontal and vertical offsets * from the upper-left corner of this Raster to the upper-left corner * of the subRaster. A subset of the bands of the parent Raster may * be specified. If this is null, then all the bands are present in the * subRaster. A translation to the subRaster may also be specified. * Note that the subRaster will reference the same * DataBuffers as the parent Raster, but using different offsets. * @param x X offset. * @param y Y offset. * @param width Width (in pixels) of the subraster. * @param height Height (in pixels) of the subraster. * @param x0 Translated X origin of the subraster. * @param y0 Translated Y origin of the subraster. * @param bandList Array of band indices. * @exception RasterFormatException * if the specified bounding box is outside of the parent Raster. */ public WritableRaster createWritableChild(int x, int y, int width, int height, int x0, int y0, int[] bandList) { if (x < this.minX) { throw new RasterFormatException("x lies outside the raster"); } if (y < this.minY) { throw new RasterFormatException("y lies outside the raster"); } if ((x+width < x) || (x+width > this.minX + this.width)) { throw new RasterFormatException("(x + width) is outside of Raster"); } if ((y+height < y) || (y+height > this.minY + this.height)) { throw new RasterFormatException("(y + height) is outside of Raster"); } SampleModel sm; if (bandList != null) sm = sampleModel.createSubsetSampleModel(bandList); else sm = sampleModel; int deltaX = x0 - x; int deltaY = y0 - y; return new ShortInterleavedRaster(sm, dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX+deltaX, sampleModelTranslateY+deltaY), this); }
/** * Creates a Writable subRaster given a region of the Raster. The x and y * coordinates specify the horizontal and vertical offsets * from the upper-left corner of this Raster to the upper-left corner * of the subRaster. A subset of the bands of the parent Raster may * be specified. If this is null, then all the bands are present in the * subRaster. A translation to the subRaster may also be specified. * Note that the subRaster will reference the same * DataBuffer as the parent Raster, but using different offsets. * @param x X offset. * @param y Y offset. * @param width Width (in pixels) of the subraster. * @param height Height (in pixels) of the subraster. * @param x0 Translated X origin of the subraster. * @param y0 Translated Y origin of the subraster. * @param bandList Array of band indices. * @exception RasterFormatException * if the specified bounding box is outside of the parent Raster. */ public WritableRaster createWritableChild(int x, int y, int width, int height, int x0, int y0, int[] bandList) { if (x < this.minX) { throw new RasterFormatException("x lies outside the raster"); } if (y < this.minY) { throw new RasterFormatException("y lies outside the raster"); } if ((x+width < x) || (x+width > this.minX + this.width)) { throw new RasterFormatException("(x + width) is outside of Raster"); } if ((y+height < y) || (y+height > this.minY + this.height)) { throw new RasterFormatException("(y + height) is outside of Raster"); } SampleModel sm; if (bandList != null) sm = sampleModel.createSubsetSampleModel(bandList); else sm = sampleModel; int deltaX = x0 - x; int deltaY = y0 - y; return new ByteComponentRaster(sm, (DataBufferByte) dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX+deltaX, sampleModelTranslateY+deltaY), this); }
private static URL createOverview(GeoImageReader gir, boolean toFlip) throws IOException { File f = File.createTempFile("kmloverview", ".png"); // generate a suitable size image double ratio = Math.max(((double) gir.getWidth()) / 1024., ((double) gir.getHeight()) / 1024.); // generate overview image BufferedImage temp = new BufferedImage((int) (gir.getWidth() * (1.0 / ratio)), (int) (gir.getHeight() * (1.0 / ratio)), gir.getType(true)); // get a handle on the raster data WritableRaster raster = temp.getRaster(); int[] data = gir.readAndDecimateTile(0, 0, gir.getWidth(), gir.getHeight(), 1.0 / ratio, true,0); raster.setSamples(0, 0, temp.getWidth(), temp.getHeight(), 0, data); float average = 0; for (int i = 0; i < data.length; i++) { average = average + data[i]; } average = average / data.length; RescaleOp rescale = new RescaleOp(((1 << (8 * gir.getNumberOfBytes())) / 5f / average), 0, null); rescale.filter(temp, temp); ColorConvertOp bop = new ColorConvertOp(null); BufferedImage out = bop.createCompatibleDestImage(temp, new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE)); out = bop.filter(temp, out); //flip the image if necessary if (toFlip) { int w = out.getWidth(); int h = out.getHeight(); BufferedImage dimg = new BufferedImage(w, h, out.getType()); Graphics2D g = dimg.createGraphics(); g.drawImage(out, 0, 0, w, h, w, 0, 0, h, null); g.dispose(); ImageIO.write(dimg, "png", f); } else { ImageIO.write(out, "png", f); } return f.toURI().toURL(); }
@Override public void preloadLineTile(int y, int length, int band) { if (y < 0) { return; } preloadedInterval = new int[]{y, y + length}; Rectangle rect = new Rectangle(0, y, getImage(band).xSize, length); TIFF tiff=getImage(band); rect=tiff.getBounds().intersection(rect); try { BufferedImage bi=null; try{ bi=tiff.read(0, rect); }catch(Exception e){ logger.warn("Problem reading image POS x:"+0+ " y: "+y +" try to read again"); try { Thread.sleep(100); } catch(InterruptedException exx) { Thread.currentThread().interrupt(); } bi=tiff.read(0, rect); } WritableRaster raster=bi.getRaster(); short[]ss=(short[])raster.getDataElements(0, 0, raster.getWidth(), raster.getHeight(), null);//tSamples(0, 0, raster.getWidth(), raster.getHeight(), 0, (short[]) null); preloadedData=ArrayUtils.toObject(ss); } catch (Exception ex) { logger.error(ex.getMessage(),ex); }finally{ //tiff.reader.addIIOReadProgressListener(this); //readComplete=false; } }
/** * Translates either a byte array or an input stream which contain * platform-specific image data in the given format into an Image. */ @Override protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException { String mimeType = null; if (format == CF_PNG) { mimeType = "image/png"; } else if (format == CF_JFIF) { mimeType = "image/jpeg"; } if (mimeType != null) { return standardImageBytesToImage(bytes, mimeType); } int[] imageData = platformImageBytesToImageData(bytes, format); if (imageData == null) { throw new IOException("data translation failed"); } int len = imageData.length - 2; int width = imageData[len]; int height = imageData[len + 1]; DataBufferInt buffer = new DataBufferInt(imageData, len); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, bandmasks, null); return new BufferedImage(directColorModel, raster, false, null); }
/** * Creates a subraster given a region of the raster. The x and y * coordinates specify the horizontal and vertical offsets * from the upper-left corner of this raster to the upper-left corner * of the subraster. A subset of the bands of the parent Raster may * be specified. If this is null, then all the bands are present in the * subRaster. A translation to the subRaster may also be specified. * Note that the subraster will reference the same * DataBuffer as the parent raster, but using different offsets. * @param x X offset. * @param y Y offset. * @param width Width (in pixels) of the subraster. * @param height Height (in pixels) of the subraster. * @param x0 Translated X origin of the subraster. * @param y0 Translated Y origin of the subraster. * @param bandList Array of band indices. * @exception RasterFormatException * if the specified bounding box is outside of the parent raster. */ public WritableRaster createWritableChild (int x, int y, int width, int height, int x0, int y0, int bandList[]) { if (x < this.minX) { throw new RasterFormatException("x lies outside raster"); } if (y < this.minY) { throw new RasterFormatException("y lies outside raster"); } if ((x+width < x) || (x+width > this.minX + this.width)) { throw new RasterFormatException("(x + width) is outside raster"); } if ((y+height < y) || (y+height > this.minY + this.height)) { throw new RasterFormatException("(y + height) is outside raster"); } SampleModel sm; if (bandList != null) sm = sampleModel.createSubsetSampleModel(bandList); else sm = sampleModel; int deltaX = x0 - x; int deltaY = y0 - y; return new IntegerComponentRaster(sm, dataBuffer, new Rectangle(x0,y0,width,height), new Point(sampleModelTranslateX+deltaX, sampleModelTranslateY+deltaY), this); }
public void Blit(SurfaceData srcData, SurfaceData dstData, Composite comp, Region clip, int srcx, int srcy, int dstx, int dsty, int width, int height) { ColorModel srcCM = srcData.getColorModel(); ColorModel dstCM = dstData.getColorModel(); // REMIND: Should get RenderingHints from sg2d CompositeContext ctx = comp.createContext(srcCM, dstCM, new RenderingHints(null)); Raster srcRas = srcData.getRaster(srcx, srcy, width, height); WritableRaster dstRas = (WritableRaster) dstData.getRaster(dstx, dsty, width, height); if (clip == null) { clip = Region.getInstanceXYWH(dstx, dsty, width, height); } int span[] = {dstx, dsty, dstx+width, dsty+height}; SpanIterator si = clip.getSpanIterator(span); srcx -= dstx; srcy -= dsty; while (si.nextSpan(span)) { int w = span[2] - span[0]; int h = span[3] - span[1]; Raster tmpSrcRas = srcRas.createChild(srcx + span[0], srcy + span[1], w, h, 0, 0, null); WritableRaster tmpDstRas = dstRas.createWritableChild(span[0], span[1], w, h, 0, 0, null); ctx.compose(tmpSrcRas, tmpDstRas, tmpDstRas); } ctx.dispose(); }
private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) { ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, w*3, 3, bOffs, null); return new BufferedImage(colorModel, raster, false, null); }
/** * Creates a Raster with the same layout but using a different * width and height, and with new zeroed data arrays. */ public WritableRaster createCompatibleWritableRaster(int w, int h) { if (w <= 0 || h <=0) { throw new RasterFormatException("negative "+ ((w <= 0) ? "width" : "height")); } SampleModel sm = sampleModel.createCompatibleSampleModel(w,h); return new ByteBandedRaster(sm, new Point(0,0)); }
/** * Creates a subraster given a region of the raster. The x and y * coordinates specify the horizontal and vertical offsets * from the upper-left corner of this raster to the upper-left corner * of the subraster. A subset of the bands of the parent Raster may * be specified. If this is null, then all the bands are present in the * subRaster. A translation to the subRaster may also be specified. * Note that the subraster will reference the same * DataBuffer as the parent raster, but using different offsets. * @param x X offset. * @param y Y offset. * @param width Width (in pixels) of the subraster. * @param height Height (in pixels) of the subraster. * @param x0 Translated X origin of the subraster. * @param y0 Translated Y origin of the subraster. * @param bandList Array of band indices. * @exception RasterFormatException * if the specified bounding box is outside of the parent raster. */ public WritableRaster createWritableChild (int x, int y, int width, int height, int x0, int y0, int bandList[]) { if (x < this.minX) { throw new RasterFormatException("x lies outside raster"); } if (y < this.minY) { throw new RasterFormatException("y lies outside raster"); } if ((x+width < x) || (x+width > this.minX + this.width)) { throw new RasterFormatException("(x + width) is outside raster"); } if ((y+height < y) || (y+height > this.minY + this.height)) { throw new RasterFormatException("(y + height) is outside raster"); } SampleModel sm; if (bandList != null) sm = sampleModel.createSubsetSampleModel(bandList); else sm = sampleModel; int deltaX = x0 - x; int deltaY = y0 - y; return new IntegerInterleavedRaster(sm, dataBuffer, new Rectangle(x0,y0,width,height), new Point(sampleModelTranslateX+deltaX, sampleModelTranslateY+deltaY), this); }
/** * Create a duplicate of a BufferedImage. * @param bi * @return the copied image. */ private static BufferedImage deepCopy(BufferedImage bi) { ColorModel cm = bi.getColorModel(); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); WritableRaster raster = bi.copyData(null); return new BufferedImage(cm, raster, isAlphaPremultiplied, null); }
public void doTest(Raster src, WritableRaster dst) { System.out.println("Test for raster:" + src); try { dst = op.filter(src, dst); } catch (Exception e) { throw new RuntimeException("Test failed.", e); } }
/** * Creates a WritableRaster with the specified width and height, that * has a data layout (SampleModel) compatible with this ColorModel. * @see WritableRaster * @see SampleModel */ public WritableRaster createCompatibleWritableRaster (int w, int h) { int[] bOffs = {2, 1, 0}; return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, w*3, 3, bOffs, null); }
/** * Creates a Writable subRaster given a region of the Raster. The x and y * coordinates specify the horizontal and vertical offsets * from the upper-left corner of this Raster to the upper-left corner * of the subRaster. A subset of the bands of the parent Raster may * be specified. If this is null, then all the bands are present in the * subRaster. A translation to the subRaster may also be specified. * Note that the subRaster will reference the same * DataBuffer as the parent Raster, but using different offsets. * @param x X offset. * @param y Y offset. * @param width Width (in pixels) of the subraster. * @param height Height (in pixels) of the subraster. * @param x0 Translated X origin of the subraster. * @param y0 Translated Y origin of the subraster. * @param bandList Array of band indices. * @exception RasterFormatException * if the specified bounding box is outside of the parent Raster. */ public WritableRaster createWritableChild(int x, int y, int width, int height, int x0, int y0, int[] bandList) { if (x < this.minX) { throw new RasterFormatException("x lies outside the raster"); } if (y < this.minY) { throw new RasterFormatException("y lies outside the raster"); } if ((x+width < x) || (x+width > this.minX + this.width)) { throw new RasterFormatException("(x + width) is outside of Raster"); } if ((y+height < y) || (y+height > this.minY + this.height)) { throw new RasterFormatException("(y + height) is outside of Raster"); } SampleModel sm; if (bandList != null) sm = sampleModel.createSubsetSampleModel(bandList); else sm = sampleModel; int deltaX = x0 - x; int deltaY = y0 - y; return new ByteInterleavedRaster(sm, dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX+deltaX, sampleModelTranslateY+deltaY), this); }
/** * Creates a new hidden-acceleration image of the given width and height * that is associated with the target Component. */ @Override public Image createAcceleratedImage(Component target, int width, int height) { ColorModel model = getColorModel(Transparency.OPAQUE); WritableRaster wr = model.createCompatibleWritableRaster(width, height); return new OffScreenImage(target, model, wr, model.isAlphaPremultiplied()); }
@Ignore public void output(File out) throws IOException { BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); WritableRaster r = img.getRaster(); for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { r.setSample(x, y, 0, data[y * height + x]); } } ImageIO.write(img, "png", out); }