@Override public int[] getPixels(Image img, int x, int y, int w, int h) { int[] pixels = new int[w * h]; PixelReader reader = img.getPixelReader(); PixelFormat.Type type = reader.getPixelFormat().getType(); WritablePixelFormat<IntBuffer> format = null; if(type == PixelFormat.Type.INT_ARGB_PRE) { format = PixelFormat.getIntArgbPreInstance(); } else { format = PixelFormat.getIntArgbInstance(); } reader.getPixels(x, y, w, h, format, pixels, 0, w); return pixels; }
@Override protected Image getImage() { WritableImage image = new WritableImage(256, 256); byte[] imageInArray = new byte[256 * 256 * 4]; //Create byte image array for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { int color = i + j; int intValue = getColorComponentProvider().getARGBColor( ((color > 255) ? 255 : color)); for (int k = 0; k < 4; k++) { imageInArray[i * 4 + j * 256 * 4 + k] = (byte)((intValue >>> (k * 8)) & (0x000000FF)); } } } //Copy bytes to image image.getPixelWriter().setPixels(0, 0, 256, 256, PixelFormat.getByteBgraInstance(), imageInArray, 0, 256 * 4); return image; }
@Override protected Image getImage() { WritableImage image = new WritableImage(256, 256); int[] imageInArray = new int[256 * 256]; for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { int color = i + j; imageInArray[i + j * 256] = getColorComponentProvider().getARGBColor( ((color > 255) ? 255 : color)); } } image.getPixelWriter().setPixels(0, 0, 256, 256, PixelFormat.getIntArgbInstance(), imageInArray, 0, 256); return image; }
/** * Determine the appropriate {@link WritablePixelFormat} type that can be * used to transfer data into the indicated BufferedImage. * * @param bimg * the BufferedImage that will be used as a destination for a * {@code PixelReader<IntBuffer>#getPixels()} operation. * @return */ private static WritablePixelFormat<IntBuffer> getAssociatedPixelFormat(BufferedImage bimg) { switch (bimg.getType()) { // We lie here for xRGB, but we vetted that the src data was opaque // so we can ignore the alpha. We use ArgbPre instead of Argb // just to get a loop that does not have divides in it if the // PixelReader happens to not know the data is opaque. case BufferedImage.TYPE_INT_RGB: case BufferedImage.TYPE_INT_ARGB_PRE: return PixelFormat.getIntArgbPreInstance(); case BufferedImage.TYPE_INT_ARGB: return PixelFormat.getIntArgbInstance(); default: // Should not happen... throw new InternalError("Failed to validate BufferedImage type"); } }
private static ColorModel getColorModel(final Image image) { @SuppressWarnings("rawtypes") PixelFormat pixelFormat = image.getPixelReader().getPixelFormat(); switch (pixelFormat.getType()) { case INT_ARGB_PRE: case INT_ARGB: return ColorModel.getRGBdefault(); case BYTE_BGRA_PRE: return new ComponentColorModel(sRGB, true, true, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); case BYTE_BGRA: return new ComponentColorModel(sRGB, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); case BYTE_RGB: return new ComponentColorModel(sRGB, false, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); case BYTE_INDEXED: // TODO: Will need to create an IndexColorModel default: throw new IllegalArgumentException("Unsupported pixel format: " + pixelFormat.getType()); } }
private static SampleModel crateSampleModel(final Image image) { PixelReader pixelReader = image.getPixelReader(); @SuppressWarnings("rawtypes") PixelFormat pixelFormat = pixelReader.getPixelFormat(); switch (pixelFormat.getType()) { case INT_ARGB_PRE: case INT_ARGB: return new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, (int) image.getWidth(), (int) image.getHeight(), new int[] {0xFF000000, 0xFF0000, 0xFF00, 0xFF}); case BYTE_BGRA_PRE: case BYTE_BGRA: case BYTE_RGB: int sampleSize = getSampleSize(pixelFormat); return new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, (int) image.getWidth(), (int) image.getHeight(), sampleSize, sampleSize * (int) image.getWidth(), createOffsets(pixelFormat)); case BYTE_INDEXED: default: throw new IllegalArgumentException("Unsupported pixel format: " + pixelFormat.getType()); } }
private void makeConstantImage(WritableImage img, Color color) { int w = (int) img.getWidth(); int h = (int) img.getHeight(); ByteBuffer scanline = ByteBuffer.allocate(3 * w); byte r = (byte) ((int) Math.round(color.getRed() * 255.0)); byte g = (byte) ((int) Math.round(color.getGreen() * 255.0)); byte b = (byte) ((int) Math.round(color.getBlue() * 255.0)); for (int i = 0; i < w; i++) { scanline.put(r); scanline.put(g); scanline.put(b); } scanline.rewind(); img.getPixelWriter().setPixels(0, 0, w, w, PixelFormat.getByteRgbInstance(), scanline, 0); }
@Test public void readBlockColor(){ ColorComponentProvider provider = getColorComponentProvider(); Image testImage = getImage(); PixelReader reader = testImage.getPixelReader(); assertFalse("Open testing image", testImage.isError()); int[] buf = new int[256 * 256]; reader.getPixels(0, 0, 256, 256, PixelFormat.getIntArgbInstance(), buf, 0, 256); int samePixelsCounter = 0; for (int i = 1; i < testImage.getHeight() - 1; i++) { for (int j = 1; j < testImage.getWidth() - 1; j++) { if(provider.getComponent(buf[i * 256 + j]) == provider.getComponent(buf[(i + 1) * 256 + j])) samePixelsCounter++; testGradientImage( provider.getComponent(buf[i * 256 + j]), provider.getComponent(buf[i * 256 + (j - 1)]), provider.getComponent(buf[(i + 1) * 256 + j]), provider.getComponent(buf[i * 256 + (j - 1)]), provider.getComponent(buf[i * 256 + (j + 1)])); } } assertTrue("More than half of the pixels the same color.", samePixelsCounter < 256 * 256 / 2); }
@Override public void render(RenderManager rm, FrameBuffer out) { synchronized (byteBuffer) { rm.getRenderer().readFrameBufferWithFormat(out, byteBuffer, Format.BGRA8); } Platform.runLater(() -> { synchronized (byteBuffer) { PixelWriter pw = renderImage.getPixelWriter(); pw.setPixels(0, 0, width, height, PixelFormat.getByteBgraInstance(), byteBuffer, scanlineStride); } }); }
public ChromaFxPreviewWindow init() { Pane previewPane = new Pane(); final WritableImage img = new WritableImage(width, height); ImageView imageView = new ImageView(); imageView.setScaleY(-1.0); imageView.setImage(img); previewPane.getChildren().add(imageView); // TODO: parallelize? animationTimer = new AnimationTimer() { @Override public void handle(long now) { if (chroma.hasChanges()) { img.getPixelWriter().setPixels(0, 0, width, height, PixelFormat.getByteRgbInstance(), chroma.getCurrentFrame(), 0, height * 3); } } }; animationTimer.stop(); setOnHiding(event -> animationTimer.stop()); setOnShowing(event -> animationTimer.start()); setTitle("Chroma Preview"); setScene(new Scene(previewPane, width, height)); return this; }
/** * Determine the optimal BufferedImage type to use for the specified * {@code fxFormat} allowing for the specified {@code bimg} to be use as a * potential default storage space if it is not null and is compatible. * * @param fxFormat * the PixelFormat of the source FX Image * @param bimg * an optional existing {@code BufferedImage} to be used for * storage if it is compatible, or null * @return */ private static int getBestBufferedImageType(PixelFormat<?> fxFormat, BufferedImage bimg) { if (bimg != null) { int bimgType = bimg.getType(); if (bimgType == BufferedImage.TYPE_INT_ARGB || bimgType == BufferedImage.TYPE_INT_ARGB_PRE) { // We will allow the caller to give us a BufferedImage // that has an alpha channel, but we might not otherwise // construct one ourselves. // We will also allow them to choose their own premultiply // type which may not match the image. // If left to our own devices we might choose a more specific // format as indicated by the choices below. return bimgType; } } switch (fxFormat.getType()) { default: case BYTE_BGRA_PRE: case INT_ARGB_PRE: return BufferedImage.TYPE_INT_ARGB_PRE; case BYTE_BGRA: case INT_ARGB: return BufferedImage.TYPE_INT_ARGB; case BYTE_RGB: return BufferedImage.TYPE_INT_RGB; case BYTE_INDEXED: return (fxFormat.isPremultiplied() ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_ARGB); } }
public void setPixelFormat(ColourMapEvent event) { int[] colors = new int[event.getNumberOfColor()]; int r, g, b; for (int i = event.getFirstColor(); i < colors.length; i++) { r = event.getColors().readUnsignedShort(); g = event.getColors().readUnsignedShort(); b = event.getColors().readUnsignedShort(); colors[i] = (0xff << 24) | ((r >> 8) << 16) | ((g >> 8) << 8) | (b >> 8); } pixelFormat.set(PixelFormat.createByteIndexedInstance(colors)); }
private Image applyEffect() { int a,b, i=oldInd; oldInd=newInd; newInd=i; i=0; mapInd=oldInd; for (int y=0;y<height;y++) { for (int x=0;x<width;x++) { short data = (short)((waterMap[mapInd-width]+waterMap[mapInd+width]+ waterMap[mapInd-1]+waterMap[mapInd+1])>>1); data -= waterMap[newInd+i]; data -= data>>4; waterMap[newInd+i]=data; data = (short)(1024-data); a=((x-halfWidth)*data/1024)+halfWidth; if (a>=width){ a=width-1; } if (a<0){ a=0; } b=((y-halfHeight)*data/1024)+halfHeight; if (b>=height){ b=height-1; } if (b<0){ b=0; } water[i++]=original[a+(b*width)]; mapInd++; } } WritableImage raster = new WritableImage(width, height); PixelWriter pixelWriter = raster.getPixelWriter(); pixelWriter.setPixels(0,0,width,height, PixelFormat.getIntArgbInstance(),water, 0,width); return raster; }
static int getSampleSize(@SuppressWarnings("rawtypes") final PixelFormat pixelFormat) { switch (pixelFormat.getType()) { case INT_ARGB_PRE: case INT_ARGB: case BYTE_INDEXED: return 1; case BYTE_BGRA_PRE: case BYTE_BGRA: return 4; case BYTE_RGB: return 3; default: throw new IllegalArgumentException("Unsupported pixel format: " + pixelFormat.getType()); } }
private static int[] createOffsets(@SuppressWarnings("rawtypes") final PixelFormat pixelFormat) { switch (pixelFormat.getType()) { case BYTE_BGRA: case BYTE_BGRA_PRE: return new int[] {2, 1, 0, 3}; case BYTE_RGB: return new int[] {0, 1, 2}; case BYTE_INDEXED: return new int[] {0}; case INT_ARGB_PRE: case INT_ARGB: default: throw new IllegalArgumentException("Unsupported pixel format: " + pixelFormat.getType()); } }
private void initialize() { mediaPlayerComponent = new CanvasPlayerComponent(); playerHolder = new Pane(); videoSourceRatioProperty = new SimpleFloatProperty(0.4f); pixelFormat = PixelFormat.getByteBgraPreInstance(); initializeImageView(); Scene scene = new Scene(new BorderPane(playerHolder)); scene.setFill(Color.BLACK); setScene(scene); mediaPlayerComponent.getMediaPlayer().prepareMedia(pathToVideo); mediaPlayerComponent.getMediaPlayer().start(); //primaryStage.show(); }
private void paintRectangularBrick(PixelWriter pixelWriter, int startX, int startY, ColoredBrick coloredBrick) { final int bytesPerPixel = 4; Brick brick = coloredBrick.getBrick(); boolean[][] matrix = brick.getMatrix(); final int pixelsPerRow = drawnBasicBrickPixelWidth * matrix[0].length; byte[] bgraRenderedBrick = renderedBrick.get(coloredBrick); if (bgraRenderedBrick == null) { Color rgb = coloredBrick.getColor().getRGB(); byte r, g, b, a; r = MathUtils.colorComponentToByte(rgb.getRed()); g = MathUtils.colorComponentToByte(rgb.getGreen()); b = MathUtils.colorComponentToByte(rgb.getBlue()); a = MathUtils.colorComponentToByte(rgb.getOpacity()); bgraRenderedBrick = new byte[matrix[0].length * matrix.length * drawnBasicBrickPixelHeight * drawnBasicBrickPixelWidth * bytesPerPixel]; renderedBrick.put(coloredBrick, bgraRenderedBrick); // fill with the regular color for (int i = 0; i < bgraRenderedBrick.length; i += bytesPerPixel) { bgraRenderedBrick[i] = b; bgraRenderedBrick[i + 1] = g; bgraRenderedBrick[i + 2] = r; bgraRenderedBrick[i + 3] = a; } if (threeDEffect) { Color colorDark = rgb.darker(); Color colorLight = rgb.brighter(); byte rDark, gDark, bDark, aDark; byte rLight, gLight, bLight, aLight; rDark = MathUtils.colorComponentToByte(colorDark.getRed()); gDark = MathUtils.colorComponentToByte(colorDark.getGreen()); bDark = MathUtils.colorComponentToByte(colorDark.getBlue()); aDark = a; rLight = MathUtils.colorComponentToByte(colorLight.getRed()); gLight = MathUtils.colorComponentToByte(colorLight.getGreen()); bLight = MathUtils.colorComponentToByte(colorLight.getBlue()); aLight = a; //border left fillRectBgra(bgraRenderedBrick, bLight, gLight, rLight, aLight, 0, 2, 2, drawnBasicBrickPixelHeight * matrix.length - 2, pixelsPerRow); //border top fillRectBgra(bgraRenderedBrick, bLight, gLight, rLight, aLight, 0, 0, pixelsPerRow - 2, 2, pixelsPerRow); //border right fillRectBgra(bgraRenderedBrick, bDark, gDark, rDark, aDark, pixelsPerRow - 2, 0, 2, drawnBasicBrickPixelHeight * matrix.length, pixelsPerRow); //border bottom fillRectBgra(bgraRenderedBrick, bDark, gDark, rDark, aDark, 2, drawnBasicBrickPixelHeight * matrix.length - 2, pixelsPerRow - 2, 2, pixelsPerRow); } } PixelFormat<ByteBuffer> pixelFormat = PixelFormat.getByteBgraPreInstance(); pixelWriter.setPixels(startX, startY, pixelsPerRow, matrix.length * drawnBasicBrickPixelHeight, pixelFormat, bgraRenderedBrick, 0, pixelsPerRow * bytesPerPixel); }
public static void writeImg(Frame frame, int width, int height){ int[] pixels = frame.getPixels(); pw.setPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(), pixels, 0, width); }
@Override public PixelFormat getPixelFormat() { throw new UnsupportedOperationException("Not supported yet."); }
@Override public <T extends Buffer> void setPixels(int i, int i1, int i2, int i3, PixelFormat<T> pf, T t, int i4) { }
@Override public void setPixels(int i, int i1, int i2, int i3, PixelFormat<ByteBuffer> pf, byte[] bytes, int i4, int i5) { }
@Override public void setPixels(int i, int i1, int i2, int i3, PixelFormat<IntBuffer> pf, int[] ints, int i4, int i5) { }