java.awt.image.WritableRaster Java Examples
The following examples show how to use
java.awt.image.WritableRaster.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CoverageUtilities.java From hortonmachine with GNU General Public License v3.0 | 6 votes |
/** * Create a {@link WritableRaster} from a int array. * * @param width the width of the raster to create. * @param height the height of the raster to create. * @param pixels the array of data. * @return the produced raster. */ public static WritableRaster createWritableRasterFromArray( int width, int height, int[] pixels ) { WritableRaster writableRaster = createWritableRaster(width, height, null, null, null); int index = 0; for( int row = 0; row < height; row++ ) { for( int col = 0; col < width; col++ ) { double value = (double) pixels[index]; if (value == 0) { value = doubleNovalue; } writableRaster.setSample(col, row, 0, value); index++; } } return writableRaster; }
Example #2
Source File: ImageUtils.java From orbit-image-analysis with GNU General Public License v3.0 | 6 votes |
/** * Calculates and returns band histograms of a BufferedImage w/o a need to extract the raster * * @param raster * @param width * @param height * @param iNumBins * @return */ public static int[][] getChannelHistograms(WritableRaster raster, int width, int height, int iNumBins) { int[][] histograms = new int[raster.getNumBands()][iNumBins]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { for (int band = 0; band < raster.getNumBands(); band++) { histograms[band][raster.getSample(col, row, band)]++; } } } return histograms; }
Example #3
Source File: ColorPaintContext.java From Java8CN with Apache License 2.0 | 6 votes |
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; }
Example #4
Source File: FXGraphics2D.java From openstock with GNU General Public License v3.0 | 6 votes |
/** * Converts a rendered image to a {@code BufferedImage}. This utility * method has come from a forum post by Jim Moore at: * <p> * <a href="http://www.jguru.com/faq/view.jsp?EID=114602"> * http://www.jguru.com/faq/view.jsp?EID=114602</a> * * @param img the rendered image. * * @return A buffered image. */ private static BufferedImage convertRenderedImage(RenderedImage img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } ColorModel cm = img.getColorModel(); int width = img.getWidth(); int height = img.getHeight(); WritableRaster raster = cm.createCompatibleWritableRaster(width, height); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); Hashtable properties = new Hashtable(); String[] keys = img.getPropertyNames(); if (keys != null) { for (int i = 0; i < keys.length; i++) { properties.put(keys[i], img.getProperty(keys[i])); } } BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties); img.copyData(raster); return result; }
Example #5
Source File: ImageTests.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
public void runTest(Object ctx, int numReps) { ImageOpTests.Context ictx = (ImageOpTests.Context)ctx; RasterOp op = ictx.rasterOp; Raster src = ictx.rasSrc; WritableRaster dst = ictx.rasDst; if (ictx.touchSrc) { Graphics gSrc = ictx.bufSrc.getGraphics(); do { gSrc.fillRect(0, 0, 1, 1); op.filter(src, dst); } while (--numReps > 0); } else { do { op.filter(src, dst); } while (--numReps > 0); } }
Example #6
Source File: EffectUtils.java From Bytecoder with Apache License 2.0 | 6 votes |
/** * <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"); } }
Example #7
Source File: CoverageDataPng.java From geopackage-java with MIT License | 6 votes |
/** * Draw a coverage data image tile from the flat array of coverage data * values of length tileWidth * tileHeight where each coverage data value is * at: (y * tileWidth) + x * * @param griddedTile * gridded tile * @param values * coverage data values of length tileWidth * tileHeight * @param tileWidth * tile width * @param tileHeight * tile height * @return coverage data image tile */ public BufferedImage drawTile(GriddedTile griddedTile, Double[] values, int tileWidth, int tileHeight) { BufferedImage image = createImage(tileWidth, tileHeight); WritableRaster raster = image.getRaster(); for (int x = 0; x < tileWidth; x++) { for (int y = 0; y < tileHeight; y++) { Double value = values[(y * tileWidth) + x]; short pixelValue = getPixelValue(griddedTile, value); setPixelValue(raster, x, y, pixelValue); } } return image; }
Example #8
Source File: EdgeNoOpCrash.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
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."); }
Example #9
Source File: ColorPaintContext.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
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; }
Example #10
Source File: Screenshots.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static void convertScreenShot2(IntBuffer bgraBuf, BufferedImage out){ WritableRaster wr = out.getRaster(); DataBufferInt db = (DataBufferInt) wr.getDataBuffer(); int[] cpuArray = db.getData(); bgraBuf.clear(); bgraBuf.get(cpuArray); // int width = wr.getWidth(); // int height = wr.getHeight(); // // // flip the components the way AWT likes them // for (int y = 0; y < height / 2; y++){ // for (int x = 0; x < width; x++){ // int inPtr = (y * width + x); // int outPtr = ((height-y-1) * width + x); // int pixel = cpuArray[inPtr]; // cpuArray[inPtr] = cpuArray[outPtr]; // cpuArray[outPtr] = pixel; // } // } }
Example #11
Source File: SplitFinder.java From Briss-2.0 with GNU General Public License v3.0 | 6 votes |
private static Float getSplitRatio(final BufferedImage image, int axis) { WritableRaster raster = image.getRaster(); double[] sdOfDerivationX = ImageFinderUtil.createSdOfDerivation(raster, axis); int width = image.getWidth(); int rangeStart = (int) Math.floor(width * (LOOK_RATIO - MAX_DIST_RATIO / 2)); int rangeEnd = (int) Math.ceil(width * (LOOK_RATIO + MAX_DIST_RATIO / 2)); double min = Double.MAX_VALUE; int minIndex = -1; for (int i = rangeStart; i < rangeEnd; i++) { if (sdOfDerivationX[i] < min) { min = sdOfDerivationX[i]; minIndex = i; } } return ((float) minIndex) / width; }
Example #12
Source File: EdgeNoOpCrash.java From hottub with GNU General Public License v2.0 | 6 votes |
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."); }
Example #13
Source File: TexturePaintContext.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public WritableRaster makeRaster(int w, int h) { // Note that we do not pass srcRas to makeRaster since it // is a Byte Raster and this colorModel needs an Int Raster WritableRaster ras = makeRaster(colorModel, null, w, h); IntegerInterleavedRaster iiRas = (IntegerInterleavedRaster) ras; outData = iiRas.getDataStorage(); outSpan = iiRas.getScanlineStride(); outOff = iiRas.getDataOffset(0); return ras; }
Example #14
Source File: JFIFMarkerSegment.java From Bytecoder with Apache License 2.0 | 5 votes |
BufferedImage getThumbnail(ImageInputStream iis, JPEGImageReader reader) throws IOException { iis.mark(); iis.seek(streamPos); DataBufferByte buffer = new DataBufferByte(getLength()); readByteBuffer(iis, buffer.getData(), reader, 1.0F, 0.0F); iis.reset(); WritableRaster raster = Raster.createInterleavedRaster(buffer, thumbWidth, thumbHeight, thumbWidth*3, 3, new int [] {0, 1, 2}, null); ColorModel cm = new ComponentColorModel(JPEG.JCS.sRGB, false, false, ColorModel.OPAQUE, DataBuffer.TYPE_BYTE); return new BufferedImage(cm, raster, false, null); }
Example #15
Source File: PixelTests.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public void runTest(Object context, int numReps) { WritableRaster ras = ((Context) context).ras; int pixeldata[] = ((Context) context).pixeldata; do { ras.setPixel(numReps&7, 0, pixeldata); } while (--numReps > 0); }
Example #16
Source File: OffScreenImage.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
/** * Constructs an OffScreenImage given a color model and tile, * for offscreen rendering to be used with a given component. * The component is used to obtain the foreground color, background * color and font. */ public OffScreenImage(Component c, ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied) { super(cm, raster, isRasterPremultiplied, null); this.c = c; initSurface(raster.getWidth(), raster.getHeight()); }
Example #17
Source File: ImageRepresentation.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public BufferedImage getOpaqueRGBImage() { if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w * h; // Note that we steal the data array here, but only for reading... DataBufferInt db = (DataBufferInt)biRaster.getDataBuffer(); int[] pixels = SunWritableRaster.stealData(db, 0); for (int i = 0; i < size; i++) { if ((pixels[i] >>> 24) != 0xff) { return bimage; } } ColorModel opModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff); int bandmasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff}; WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w, bandmasks, null); try { BufferedImage opImage = createImage(opModel, opRaster, false, null); return opImage; } catch (Exception e) { return bimage; } } return bimage; }
Example #18
Source File: CustomCompositeTest.java From hottub with GNU General Public License v2.0 | 5 votes |
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) { int w = src.getWidth(); int h = src.getHeight(); DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer(); DataBufferInt dstOutDB = (DataBufferInt) dstOut.getDataBuffer(); int srcRGB[] = srcDB.getBankData()[0]; int dstOutRGB[] = dstOutDB.getBankData()[0]; int srcOffset = srcDB.getOffset(); int dstOutOffset = dstOutDB.getOffset(); int srcScanStride = ((SinglePixelPackedSampleModel) src.getSampleModel()).getScanlineStride(); int dstOutScanStride = ((SinglePixelPackedSampleModel) dstOut.getSampleModel()).getScanlineStride(); int srcAdjust = srcScanStride - w; int dstOutAdjust = dstOutScanStride - w; int si = srcOffset; int doi = dstOutOffset; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { dstOutRGB[doi] = srcRGB[si] ^ 0x00ffffff; si++; doi++; } si += srcAdjust; doi += dstOutAdjust; } }
Example #19
Source File: ShortBandedRaster.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
/** * 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 ShortBandedRaster(sm, new Point(0,0)); }
Example #20
Source File: VectorPainter.java From mrgeo with Apache License 2.0 | 5 votes |
private static void averageRaster(WritableRaster raster, Raster count) { for (int y = 0; y < raster.getHeight(); y++) { for (int x = 0; x < raster.getWidth(); x++) { for (int b = 0; b < raster.getNumBands(); b++) { float v = raster.getSampleFloat(x, y, b); float c = count.getSampleFloat(x, y, b); if (!Float.isNaN(v)) { if (FloatUtils.isEqual(c, 0.0)) { v = Float.NaN; } else { v /= c; } raster.setSample(x, y, b, v); } } } } }
Example #21
Source File: IntegerInterleavedRaster.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * 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); }
Example #22
Source File: GeneralRenderer.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
static void doSetRect(SurfaceData sData, PixelWriter pw, int x1, int y1, int x2, int y2) { WritableRaster dstRast = (WritableRaster) sData.getRaster(x1, y1, x2-x1, y2-y1); pw.setRaster(dstRast); while (y1 < y2) { for (int x = x1; x < x2; x++) { pw.writePixel(x, y1); } y1++; } }
Example #23
Source File: AWTImageTools.java From scifio with BSD 2-Clause "Simplified" License | 5 votes |
/** * Whether we can return the data buffer's bank data without performing any * copy or conversion operations. */ private static boolean canUseBankDataDirectly(final WritableRaster r, final int transferType, final Class<? extends DataBuffer> dataBufferClass) { final int tt = r.getTransferType(); if (tt != transferType) return false; final DataBuffer buffer = r.getDataBuffer(); if (!dataBufferClass.isInstance(buffer)) return false; final SampleModel model = r.getSampleModel(); if (!(model instanceof ComponentSampleModel)) return false; final ComponentSampleModel csm = (ComponentSampleModel) model; final int pixelStride = csm.getPixelStride(); if (pixelStride != 1) return false; final int w = r.getWidth(); final int scanlineStride = csm.getScanlineStride(); if (scanlineStride != w) return false; final int c = r.getNumBands(); final int[] bandOffsets = csm.getBandOffsets(); if (bandOffsets.length != c) return false; for (int i = 0; i < bandOffsets.length; i++) { if (bandOffsets[i] != 0) return false; } for (int i = 0; i < bandOffsets.length; i++) { if (bandOffsets[i] != i) return false; } return true; }
Example #24
Source File: BMPSubsamplingTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
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); }
Example #25
Source File: OffScreenImage.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
/** * Constructs an OffScreenImage given a color model and tile, * for offscreen rendering to be used with a given component. * The component is used to obtain the foreground color, background * color and font. */ public OffScreenImage(Component c, ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied) { super(cm, raster, isRasterPremultiplied, null); this.c = c; initSurface(raster.getWidth(), raster.getHeight()); }
Example #26
Source File: WDataTransferer.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
/** * 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); }
Example #27
Source File: TriangleBasedShadingContext.java From sambox with Apache License 2.0 | 5 votes |
@Override public final Raster getRaster(int x, int y, int w, int h) { WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h); int[] data = new int[w * h * 4]; if (!isDataEmpty() || getBackground() != null) { for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { Point p = new IntPoint(x + col, y + row); int value; Integer v = pixelTable.get(p); if (v != null) { value = v; } else { if (getBackground() == null) { continue; } value = getRgbBackground(); } int index = (row * w + col) * 4; data[index] = value & 255; value >>= 8; data[index + 1] = value & 255; value >>= 8; data[index + 2] = value & 255; data[index + 3] = 255; } } } raster.setPixels(0, 0, w, h, data); return raster; }
Example #28
Source File: ShortBandedRaster.java From Bytecoder with Apache License 2.0 | 5 votes |
/** * 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 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 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 ShortBandedRaster(sm, (DataBufferUShort) dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX+deltaX, sampleModelTranslateY+deltaY), this); }
Example #29
Source File: TexturePaintContext.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public WritableRaster makeRaster(int w, int h) { WritableRaster ras = makeByteRaster(srcRas, w, h); ByteInterleavedRaster biRas = (ByteInterleavedRaster) ras; outData = biRas.getDataStorage(); outSpan = biRas.getScanlineStride(); outOff = biRas.getDataOffset(0); return ras; }
Example #30
Source File: TexturePaintContext.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
public WritableRaster makeRaster(int w, int h) { // Note that we do not pass srcRas to makeRaster since it // is a Byte Raster and this colorModel needs an Int Raster WritableRaster ras = makeRaster(colorModel, null, w, h); IntegerInterleavedRaster iiRas = (IntegerInterleavedRaster) ras; outData = iiRas.getDataStorage(); outSpan = iiRas.getScanlineStride(); outOff = iiRas.getDataOffset(0); return ras; }