javafx.scene.image.PixelFormat Java Examples

The following examples show how to use javafx.scene.image.PixelFormat. 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: VgaController.java    From Sword_emulator with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onTick(long ticks) {
    Platform.runLater(() -> {
        if (VgaConfigures.getResolution() == VgaConfigures.Resolution.CLOSE) {
            return;
        }
        if (machine.isLooping() && screen.getImage() != content) {
            screen.setImage(content);
        }
        ScreenProvider currentProvider = VgaConfigures.isTextMode() ? textProvider : graphProvider;
        content.getPixelWriter().setPixels(0, 0, WIDTH, HEIGHT, PixelFormat.getByteBgraPreInstance()
                , currentProvider.get(), 0, WIDTH * 4);
        // blink cursor
        if (ticks % 10 == 0) {
            machine.blink();
        }
    });

}
 
Example #2
Source File: WriteFxImageBenchmark.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public static void initalizeImage() {
    noisePixels = ByteBuffer.allocate(w * h * 4);
    noiseBuffer = new PixelBuffer<>(w, h, noisePixels, PixelFormat.getByteBgraPreInstance());
    testimage = new WritableImage(noiseBuffer);
    final Canvas noiseCanvas = new Canvas(w, h);
    final GraphicsContext noiseContext = noiseCanvas.getGraphicsContext2D();
    final byte[] randomArray = new byte[w * h * 4];
    new Random().nextBytes(randomArray);
    noiseContext.getPixelWriter().setPixels(0, 0, w, h, PixelFormat.getByteBgraInstance(), randomArray, 0, w);
    noiseCanvas.snapshot(null, testimage);

    final Canvas easyCanvas = new Canvas(w2, h2);
    final GraphicsContext easyContext = easyCanvas.getGraphicsContext2D();
    easyContext.setStroke(Color.BLUE);
    easyContext.strokeOval(20, 30, 40, 50);
    easyContext.setStroke(Color.RED);
    easyContext.strokeOval(30, 40, 50, 60);
    easyContext.setStroke(Color.GREEN);
    easyContext.strokeOval(40, 50, 60, 70);
    easyContext.setStroke(Color.ORANGE);
    easyContext.strokeRect(0, 0, w2, h2);
    testimage2 = easyCanvas.snapshot(null, null);

    initialized.set(true);
}
 
Example #3
Source File: FxNinePatch.java    From NinePatch with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #4
Source File: SwingFXUtils.java    From ShootOFF with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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");
	}
}
 
Example #5
Source File: ContourDataSetCache.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
protected WritableImage convertDataArrayToImage(final double[] inputData, final int dataWidth, final int dataHeight,
        final ColorGradient colorGradient) {
    final int length = dataWidth * dataHeight;

    final byte[] byteBuffer = ByteArrayCache.getInstance().getArrayExact(length * BGRA_BYTE_SIZE);
    final int rowSizeInBytes = BGRA_BYTE_SIZE * dataWidth;
    final WritableImage image = this.getImage(dataWidth, dataHeight);
    final PixelWriter pixelWriter = image.getPixelWriter();
    if (pixelWriter == null) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.atError().log("Could not get PixelWriter for image");
        }
        return image;
    }

    final int hMinus1 = dataHeight - 1;
    for (int yIndex = 0; yIndex < dataHeight; yIndex++) {
        final int rowIndex = dataWidth * yIndex;
        final int rowPixelIndex = rowSizeInBytes * (hMinus1 - yIndex);
        for (int xIndex = 0; xIndex < dataWidth; xIndex++) {
            final int x = xIndex;
            final int[] color = colorGradient.getColorBytes(inputData[rowIndex + xIndex]);

            final int pixelIndex = rowPixelIndex + x * BGRA_BYTE_SIZE;
            byteBuffer[pixelIndex] = (byte) (color[3]);
            byteBuffer[pixelIndex + 1] = (byte) (color[2]);
            byteBuffer[pixelIndex + 2] = (byte) (color[1]);
            byteBuffer[pixelIndex + 3] = (byte) (color[0]);
        }
    }

    pixelWriter.setPixels(0, 0, dataWidth, dataHeight, PixelFormat.getByteBgraPreInstance(), byteBuffer, 0,
            rowSizeInBytes);
    ByteArrayCache.getInstance().add(byteBuffer);
    return image;
}
 
Example #6
Source File: WriteFxImageTests.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
@Start
public void setUp(@SuppressWarnings("unused") final Stage stage) {
    int w = 200;
    int h = 300;
    // generate test image with simple shapes
    final Canvas originalCanvas = new Canvas(w, h);
    final GraphicsContext originalContext = originalCanvas.getGraphicsContext2D();
    originalContext.setStroke(Color.BLUE);
    originalContext.strokeOval(20, 30, 40, 50);
    originalContext.setStroke(Color.RED);
    originalContext.strokeOval(30, 40, 50, 60);
    originalContext.setStroke(Color.GREEN);
    originalContext.strokeOval(40, 50, 60, 70);
    originalContext.setStroke(Color.ORANGE);
    originalContext.strokeRect(0, 0, w, h);
    imageOvals = originalCanvas.snapshot(null, null);

    //generate test image with noise data (not very compressible)
    final Canvas noiseCanvas = new Canvas(600, 333);
    final GraphicsContext noiseContext = noiseCanvas.getGraphicsContext2D();
    byte[] randomArray = new byte[600 * 333 * 4];
    new Random().nextBytes(randomArray);
    noiseContext.getPixelWriter().setPixels(0, 0, 600, 333, PixelFormat.getByteBgraInstance(), randomArray, 0, 600);
    imageRandom = noiseCanvas.snapshot(null, null);

    // generate test image with minimal dimensions
    final Canvas onexoneCanvas = new Canvas(1, 1);
    final GraphicsContext onexoneContext = onexoneCanvas.getGraphicsContext2D();
    onexoneContext.getPixelWriter().setArgb(0, 0, 0xdeadbeef);
    image1x1 = onexoneCanvas.snapshot(null, null);
}
 
Example #7
Source File: UiUtil.java    From Recaf with MIT License 5 votes vote down vote up
/**
 * Convert a AWT image to a JavaFX image.
 *
 * @param img
 * 		The image to convert.
 *
 * @return JavaFX image.
 */
public static WritableImage toFXImage(BufferedImage img) {
	// This is a stripped down version of "SwingFXUtils.toFXImage(img, fxImg)"
	int w = img.getWidth();
	int h = img.getHeight();
	// Ensure image type is ARGB.
	switch(img.getType()) {
		case BufferedImage.TYPE_INT_ARGB:
		case BufferedImage.TYPE_INT_ARGB_PRE:
			break;
		default:
			// Convert to ARGB
			BufferedImage converted = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
			Graphics2D g2d = converted.createGraphics();
			g2d.drawImage(img, 0, 0, null);
			g2d.dispose();
			img = converted;
			break;
	}
	// Even if the image type is ARGB_PRE, we use "getIntArgbInstance()"
	// Using "getIntArgbPreInstance()" removes transparency.
	WritableImage fxImg = new WritableImage(w, h);
	PixelWriter pw = fxImg.getPixelWriter();
	int[] data = img.getRGB(0, 0, w, h, null, 0, w);
	pw.setPixels(0, 0, w, h, PixelFormat.getIntArgbInstance(), data, 0, w);
	return fxImg;
}
 
Example #8
Source File: SwingFXUtils.java    From ShootOFF with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example #9
Source File: VncImageView.java    From jfxvnc with Apache License 2.0 5 votes vote down vote up
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));
  }
 
Example #10
Source File: VLCWindowDirect.java    From Quelea with GNU General Public License v3.0 4 votes vote down vote up
private VLCWindowDirect() {

        runOnVLCThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Utils.fxRunAndWait(new Runnable() {

                        @Override
                        public void run() {
                            stage = new Stage(StageStyle.UNDECORATED);
                            PlatformUtils.setFullScreenAlwaysOnTop(stage, true);
                            stage.focusedProperty().addListener(new ChangeListener<Boolean>() {
                                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                                    if (newValue.booleanValue()) {
                                        //focused
                                        stage.toBack();
                                    } else {
                                        //not focused
                                    }
                                }
                            });
                            borderPane = new BorderPane();
                            borderPane.setStyle("-fx-background-color: black;");
                            canvas = new Canvas(1920, 1080);
                            canvas.getGraphicsContext2D().setFill(Color.BLACK);
                            borderPane.setCenter(canvas);
                            scene = new Scene(borderPane);
                            scene.setFill(Color.BLACK);
                            pixelWriter = canvas.getGraphicsContext2D().getPixelWriter();
                            pixelFormat = PixelFormat.getByteBgraPreInstance();

                        }
                    });
                    mediaPlayer = new MediaPlayerComponent();
                    mediaPlayer.getMediaPlayer().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {

                        @Override
                        public void finished(MediaPlayer mp) {
                            if (mediaPlayer.getMediaPlayer().subItemCount() > 0) {
                                String mrl = mediaPlayer.getMediaPlayer().subItems().remove(0);
                                mediaPlayer.getMediaPlayer().startMedia(mrl);
                                Platform.runLater(new Runnable() {

                                    @Override
                                    public void run() {
                                        canvas.setVisible(true);
                                    }
                                });
                            }
                        }
                    });
                    Utils.fxRunAndWait(new Runnable() {

                        @Override
                        public void run() {
                            stage.setScene(scene);

                        }
                    });
                    init = true;

                    LOGGER.log(Level.INFO, "Video initialised ok");
                } catch (Exception ex) {
                    LOGGER.log(Level.INFO, "Couldn't initialise video, almost definitely because VLC (or correct version of VLC) was not found.", ex);
                }
            }
        });
        ScheduledExecutorService exc = Executors.newSingleThreadScheduledExecutor();
        exc.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                if (init) {
                    runOnVLCThread(new Runnable() {
                        @Override
                        public void run() {
                            mediaPlayer.getMediaPlayer().setAdjustVideo(true);
                            int hueVal = (int) (hue * 360);
                            if(hueVal>180) hueVal-=360;
                            hueVal += 180;
                            mediaPlayer.getMediaPlayer().setHue(hueVal);
                            Platform.runLater(new Runnable() {

                                @Override
                                public void run() {
                                    if (stage != null) {
                                        stage.toBack();
                                    }
                                }
                            });
                        }
                    });
                }
            }
        }, 0, 30, TimeUnit.MILLISECONDS);
    }