Java Code Examples for javafx.scene.image.PixelReader#getColor()

The following examples show how to use javafx.scene.image.PixelReader#getColor() . 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: WriteFxImage.java    From chart-fx with Apache License 2.0 7 votes vote down vote up
/**
 * copy the given Image to a WritableImage
 * 
 * @param image the input image
 * @return clone of image
 */
public static WritableImage clone(Image image) {
    int height = (int) image.getHeight();
    int width = (int) image.getWidth();
    WritableImage writableImage = WritableImageCache.getInstance().getImage(width, height);
    PixelWriter pixelWriter = writableImage.getPixelWriter();
    if (pixelWriter == null) {
        throw new IllegalStateException(IMAGE_PIXEL_READER_NOT_AVAILABLE);
    }

    final PixelReader pixelReader = image.getPixelReader();
    if (pixelReader == null) {
        throw new IllegalStateException(IMAGE_PIXEL_READER_NOT_AVAILABLE);
    }
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            Color color = pixelReader.getColor(x, y);
            pixelWriter.setColor(x, y, color);
        }
    }
    return writableImage;
}
 
Example 2
Source File: ViewText.java    From latexdraw with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds transparency to the given image.
 * @param img The image to transform.
 * @return The same image with white replaced by transparent.
 */
private WritableImage toTransparentPNG(final Image img) {
	final PixelReader pixelReader = img.getPixelReader();
	final WritableImage wImage = new WritableImage((int) img.getWidth(), (int) img.getHeight());
	final PixelWriter pixelWriter = wImage.getPixelWriter();

	for(int readY = 0; readY < img.getHeight(); readY++) {
		for(int readX = 0; readX < img.getWidth(); readX++) {
			final javafx.scene.paint.Color color = pixelReader.getColor(readX, readY);
			if (color.equals(javafx.scene.paint.Color.WHITE)) {
				pixelWriter.setColor(readX, readY, new javafx.scene.paint.Color(color.getRed(), color.getGreen(), color.getBlue(), 0)); // new javafx.scene.paint.Color(1, 1, 1, 0));
			} else {
				pixelWriter.setColor(readX, readY, color);
			}
		}
	}

	return wImage;
}
 
Example 3
Source File: Font.java    From jace with GNU General Public License v2.0 6 votes vote down vote up
private static void initalize() {
    initialized = true;
    font = new int[256][8];
    Thread fontLoader = new Thread(() -> {
        InputStream in = Font.class.getClassLoader().getResourceAsStream("jace/data/font.png");
        Image image = new Image(in);
        PixelReader reader = image.getPixelReader();
        for (int i = 0; i < 256; i++) {
            int x = (i >> 4) * 13 + 2;
            int y = (i & 15) * 13 + 4;
            for (int j = 0; j < 8; j++) {
                int row = 0;
                for (int k = 0; k < 7; k++) {
                    Color color = reader.getColor((7 - k) + x, j + y);
                    boolean on = color.getRed() != 0;
                    row = (row << 1) | (on ? 0 : 1);
                }
                font[i][j] = row;
            }
        }
    });
    fontLoader.start();
}
 
Example 4
Source File: DisintegrationApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private void disintegrate(Image image) {
    PixelReader pixelReader = image.getPixelReader();

    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            Color color = pixelReader.getColor(x, y);

            if (!color.equals(Color.TRANSPARENT)) {
                Particle p = new Particle(x + 700, y + 50, color);
                particles.add(p);
            }
        }
    }

    fullSize = particles.size();
}
 
Example 5
Source File: Service.java    From gluon-samples with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private INDArray fromImage(File image) {
    System.out.println("FROMImage called for "+image);
    Image im = new Image(image.toURI().toString(), width, height, false, true);
    PixelReader pr = im.getPixelReader();
    System.out.println("crated image, "+im.getWidth()+", "+im.getHeight());
    INDArray row = Nd4j.createUninitialized(width * height);
    boolean bw = pr.getColor(0, 0).getBrightness() < .5;
    System.out.println("bw = " + bw);
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            int pixel = pr.getArgb(i, j);
            Color c = pr.getColor(i, j);
            // int red = ((pixel >> 16) & 0xff);
            //  int green = ((pixel >> 8) & 0xff);
            //  int blue = (pixel & 0xff);

            //   int grayLevel = (int) (0.2162 * (double) red + 0.7152 * (double) green + 0.0722 * (double) blue) / 3;
            //   grayLevel = (int)((red + green + blue)/3.);
            //   grayLevel = 255 - grayLevel; // Inverted the grayLevel value here.
            //  int gray = (grayLevel << 16) + (grayLevel << 8) + grayLevel;
            //    System.out.println("p[" + i + "][" + j + "]: " + pixel+" -> "+grayLevel+ "("+red+", "+green+", "+blue+")"+" -> "+c.getBrightness()+", "+c.getSaturation()+", "+c.getHue()+", "+c.getOpacity());
            row.putScalar(j * height + i, bw ? c.getBrightness() : 1 - c.getBrightness());
        }
    }
    return row;
}
 
Example 6
Source File: HeatMap.java    From charts with Apache License 2.0 6 votes vote down vote up
/**
 * Recreates the heatmap based on the current monochrome map.
 * Using this approach makes it easy to change the used color
 * mapping.
 */
private void updateHeatMap() {
    monochrome.snapshot(SNAPSHOT_PARAMETERS, monochromeImage);

    int width  = monochromeImage.widthProperty().intValue();
    int height = monochromeImage.heightProperty().intValue();
    heatMap    = new WritableImage(width, height);

    Color       colorFromMonoChromeImage;
    double      brightness;
    Color       mappedColor;
    PixelWriter pixelWriter = heatMap.getPixelWriter();
    PixelReader pixelReader = monochromeImage.getPixelReader();
    for (int y = 0 ; y < height ; y++) {
        for (int x = 0 ; x < width ; x++) {
            colorFromMonoChromeImage = pixelReader.getColor(x, y);
            brightness               = colorFromMonoChromeImage.getOpacity();
            mappedColor              = Helper.getColorAt(mappingGradient, brightness);
            pixelWriter.setColor(x, y, fadeColors ? Color.color(mappedColor.getRed(), mappedColor.getGreen(), mappedColor.getBlue(), brightness) : mappedColor);
        }
    }
    setImage(heatMap);
}
 
Example 7
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static Image verticalImage(Image image) {
    int width = (int) image.getWidth();
    int height = (int) image.getHeight();
    PixelReader pixelReader = image.getPixelReader();
    WritableImage newImage = new WritableImage(width, height);
    PixelWriter pixelWriter = newImage.getPixelWriter();
    for (int i = 0; i < width; ++i) {
        int t = 0, b = height - 1;
        while (t <= b) {
            Color ct = pixelReader.getColor(i, t);
            Color cb = pixelReader.getColor(i, b);
            pixelWriter.setColor(i, t, cb);
            pixelWriter.setColor(i, b, ct);
            t++;
            b--;
        }
    }
    return newImage;
}
 
Example 8
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static Image horizontalImage(Image image) {
    int width = (int) image.getWidth();
    int height = (int) image.getHeight();
    PixelReader pixelReader = image.getPixelReader();
    WritableImage newImage = new WritableImage(width, height);
    PixelWriter pixelWriter = newImage.getPixelWriter();

    for (int j = 0; j < height; ++j) {
        int l = 0, r = width - 1;
        while (l <= r) {
            Color cl = pixelReader.getColor(l, j);
            Color cr = pixelReader.getColor(r, j);
            pixelWriter.setColor(l, j, cr);
            pixelWriter.setColor(r, j, cl);
            l++;
            r--;
        }
    }
    return newImage;
}
 
Example 9
Source File: ImageManufacturePaneController.java    From MyBox with Apache License 2.0 6 votes vote down vote up
@FXML
@Override
public void paneClicked(MouseEvent event) {
    if (isPickingColor.get()) {
        IntPoint p = getImageXYint(event, imageView);
        if (p == null) {
            return;
        }
        PixelReader pixelReader = imageView.getImage().getPixelReader();
        Color color = pixelReader.getColor(p.getX(), p.getY());
        colorPicked(color);

    } else if (scopeCommonBox.isDisabled()) {
        super.paneClicked(event);
        parent.operationController.paneClicked(event);

    } else if (scope != null && scope.getScopeType() != null) {
        paneClickedForScope(event);

    }

}
 
Example 10
Source File: ImageMaskController.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public DoublePoint showXY(MouseEvent event, DoublePoint p) {
    if (needNotCoordinates
            || !AppVariables.ImagePopCooridnate
            || xyText == null || !xyText.isVisible()) {
        return null;
    }
    if (p == null) {

        if (xyText != null) {
            xyText.setText("");
        }
        return null;
    }

    if (xyText != null && xyText.isVisible()) {
        PixelReader pixelReader = imageView.getImage().getPixelReader();
        Color color = pixelReader.getColor((int) p.getX(), (int) p.getY());
        String s = (int) Math.round(p.getX()) + "," + (int) Math.round(p.getY()) + "\n"
                + FxmlColor.colorDisplaySimple(color);
        xyText.setText(s);
        xyText.setX(event.getX() + 10);
        xyText.setY(event.getY());
    }
    return p;
}
 
Example 11
Source File: ImageViewerController.java    From MyBox with Apache License 2.0 6 votes vote down vote up
@FXML
@Override
public void paneClicked(MouseEvent event) {
    if (paletteController == null || !paletteController.getParentController().equals(this)) {
        isPickingColor.set(false);
    }
    if (isPickingColor.get()) {
        IntPoint p = getImageXYint(event, imageView);
        if (p == null) {
            return;
        }
        PixelReader pixelReader = imageView.getImage().getPixelReader();
        Color color = pixelReader.getColor(p.getX(), p.getY());
        paletteController.setColor(color);
    } else {
        super.paneClicked(event);
    }
}
 
Example 12
Source File: ImagePane.java    From oim-fx with MIT License 6 votes vote down vote up
private void setGrayImage(Image image) {
	if (null != image && !image.isBackgroundLoading()) {
		int imageWidth = (int) image.getWidth();
		int imageHeight = (int) image.getHeight();
		if (imageWidth > 0 && imageHeight > 0) {
			PixelReader pixelReader = image.getPixelReader();
			grayImage = new WritableImage(imageWidth, imageHeight);
			PixelWriter pixelWriter = grayImage.getPixelWriter();
			for (int y = 0; y < imageHeight; y++) {
				for (int x = 0; x < imageWidth; x++) {
					Color color = pixelReader.getColor(x, y);
					color = color.grayscale();
					pixelWriter.setColor(x, y, color);
				}
			}
		} else {
			grayImage = null;
		}
	} else {
		grayImage = null;
	}
}
 
Example 13
Source File: HeadImageItemPane.java    From oim-fx with MIT License 6 votes vote down vote up
private void setGrayImage(Image image) {
	if (null != image&&!image.isBackgroundLoading()) {
		int imageWidth = (int) image.getWidth();
		int imageHeight = (int) image.getHeight();
		if (imageWidth > 0 && imageHeight > 0) {
			PixelReader pixelReader = image.getPixelReader();
			grayImage = new WritableImage(imageWidth, imageHeight);
			PixelWriter pixelWriter = grayImage.getPixelWriter();
			for (int y = 0; y < imageHeight; y++) {
				for (int x = 0; x < imageWidth; x++) {
					Color color = pixelReader.getColor(x, y);
					color = color.grayscale();
					pixelWriter.setColor(x, y, color);
				}
			}
		} else {
			grayImage = null;
		}
	} else {
		grayImage = null;
	}
}
 
Example 14
Source File: HeadImagePanel.java    From oim-fx with MIT License 6 votes vote down vote up
private void setGrayImage(Image image) {
	if (null != image && !image.isBackgroundLoading()) {
		int imageWidth = (int) image.getWidth();
		int imageHeight = (int) image.getHeight();
		if (imageWidth > 0 && imageHeight > 0) {
			PixelReader pixelReader = image.getPixelReader();
			grayImage = new WritableImage(imageWidth, imageHeight);
			PixelWriter pixelWriter = grayImage.getPixelWriter();
			if (null != pixelWriter && null != pixelReader) {
				for (int y = 0; y < imageHeight; y++) {
					for (int x = 0; x < imageWidth; x++) {
						Color color = pixelReader.getColor(x, y);
						color = color.grayscale();
						pixelWriter.setColor(x, y, color);
					}
				}
			}
		} else {
			grayImage = null;
		}
	} else {
		grayImage = null;
	}
}
 
Example 15
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image manufactureImage(Image image, int manuType) {
    PixelReader pixelReader = image.getPixelReader();
    WritableImage newImage = new WritableImage((int) image.getWidth(), (int) image.getHeight());
    PixelWriter pixelWriter = newImage.getPixelWriter();

    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            Color color = pixelReader.getColor(x, y);
            switch (manuType) {
                case ImageManufactureType.Brighter:
                    color = color.brighter();
                    break;
                case ImageManufactureType.Darker:
                    color = color.darker();
                    break;
                case ImageManufactureType.Gray:
                    color = color.grayscale();
                    break;
                case ImageManufactureType.Invert:
                    color = color.invert();
                    break;
                case ImageManufactureType.Saturate:
                    color = color.saturate();
                    break;
                case ImageManufactureType.Desaturate:
                    color = color.desaturate();
                    break;
                default:
                    break;
            }
            pixelWriter.setColor(x, y, color);
        }
    }
    return newImage;
}
 
Example 16
Source File: ImageHandler.java    From oim-fx with MIT License 5 votes vote down vote up
private void pixWithImage(int type) {
	PixelReader pixelReader = imageView.getImage().getPixelReader();
	// Create WritableImage
	wImage = new WritableImage((int) image.getWidth(),(int) image.getHeight());
	PixelWriter pixelWriter = wImage.getPixelWriter();

	for (int y = 0; y < image.getHeight(); y++) {
		for (int x = 0; x < image.getWidth(); x++) {
			Color color = pixelReader.getColor(x, y);
			switch (type) {
			case 0:
				color = color.brighter();
				break;
			case 1:
				color = color.darker();
				break;
			case 2:
				color = color.grayscale();
				break;
			case 3:
				color = color.invert();
				break;
			case 4:
				color = color.saturate();
				break;
			case 5:
				color = color.desaturate();
				break;
			default:
				break;
			}
			pixelWriter.setColor(x, y, color);
		}
	}
	imageView.setImage(wImage);
}
 
Example 17
Source File: FindUserItem.java    From oim-fx with MIT License 5 votes vote down vote up
private void setGrayImage(Image image) {
	PixelReader pixelReader = image.getPixelReader();
	grayImage = new WritableImage((int) image.getWidth(), (int) image.getHeight());
	PixelWriter pixelWriter = grayImage.getPixelWriter();

	for (int y = 0; y < image.getHeight(); y++) {
		for (int x = 0; x < image.getWidth(); x++) {
			Color color = pixelReader.getColor(x, y);
			color = color.grayscale();
			pixelWriter.setColor(x, y, color);
		}
	}
}
 
Example 18
Source File: MapGenerator.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * You will have to supply an object that will create the Hexagons as you like. E.g.
 * <p>
 * class HexagonCreator implements IHexagonCreator {
 *
 * @param creator the object that will actually create the Hexagon.
 * @Override public void createHexagon(GridPosition position, javafx.scene.paint.Color color) { Hexagon h = new
 *           Hexagon(position, 20, 0, 0); h.setBackgroundColor(color); map.addHexagon(h); } }
 */
public void generate(IHexagonCreator creator) {
    final PixelReader pr = image.getPixelReader();
    if (pr == null) {
        return;
    }
    final double imageWidth = image.getWidth();
    final double imageHeight = image.getHeight();
    final double hexagonMapWidthInPixels = map.getGraphicsHorizontalDistanceBetweenHexagons() * mapWidth;
    horizontalRelation = imageWidth / hexagonMapWidthInPixels;
    final double estimatedHexagonMapHeightInPixels = imageHeight / horizontalRelation;

    final int mapHeight = (int) (estimatedHexagonMapHeightInPixels
                                 / map.getGraphicsverticalDistanceBetweenHexagons());
    verticalRelation = imageHeight
                       / (map.getGraphicsverticalDistanceBetweenHexagons() * mapHeight + map.getGraphicsHexagonHeight() / 2);
    // Not really sure about the last part but it seems to work. And should I make the corresponding correction on
    // the horizontalRelation ?

    for (int x = 0; x < mapWidth; x++) {
        for (int y = 0; y < mapHeight; y++) {
            final int axialQ = x - (y - (y & 1)) / 2;
            final int axialR = y;
            final Hexagon h = new Hexagon(axialQ, axialR);
            h.setMap(map);
            final int xOnImage = (int) ((h.getGraphicsXoffset() - map.graphicsXpadding) * horizontalRelation);
            final int yOnImage = (int) ((h.getGraphicsYoffset() - map.graphicsYpadding) * verticalRelation);
            final Color pixelColor = pr.getColor(xOnImage, yOnImage);
            creator.createHexagon(axialQ, axialR, pixelColor, map);
        }
    }
}