Java Code Examples for javafx.scene.image.Image#getPixelReader()

The following examples show how to use javafx.scene.image.Image#getPixelReader() . 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: 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 3
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 4
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 5
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 6
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static boolean sameImage(Image imageA, Image imageB) {
    if (imageA == null || imageB == null
            || imageA.getWidth() != imageB.getWidth()
            || imageA.getHeight() != imageB.getHeight()) {
        return false;
    }
    PixelReader readA = imageA.getPixelReader();
    PixelReader readB = imageB.getPixelReader();
    for (int y = 0; y < imageA.getHeight(); y++) {
        for (int x = 0; x < imageA.getWidth(); x++) {
            if (!readA.getColor(x, y).equals(readB.getColor(x, y))) {
                return false;
            }
        }
    }
    return true;
}
 
Example 7
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static Image cropInsideFx(Image image, DoubleShape shape, Color bgColor) {
    if (image == null || shape == null || !shape.isValid()
            || bgColor == null) {
        return 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 y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (shape.include(x, y)) {
                pixelWriter.setColor(x, y, bgColor);
            } else {
                pixelWriter.setColor(x, y, pixelReader.getColor(x, y));
            }
        }
    }
    return newImage;
}
 
Example 8
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 9
Source File: BasicEncoder.java    From FXTutorials with MIT License 6 votes vote down vote up
@Override
public Image encode(Image image, String message) {
    int width = (int) image.getWidth();
    int height = (int) image.getHeight();

    WritableImage copy = new WritableImage(image.getPixelReader(), width, height);
    PixelWriter writer = copy.getPixelWriter();
    PixelReader reader = image.getPixelReader();

    boolean[] bits = encode(message);

    IntStream.range(0, bits.length)
            .mapToObj(i -> new Pair<>(i, reader.getArgb(i % width, i / width)))
            .map(pair -> new Pair<>(pair.getKey(), bits[pair.getKey()] ? pair.getValue() | 1 : pair.getValue() &~ 1))
            .forEach(pair -> {
                int x = pair.getKey() % width;
                int y = pair.getKey() / width;

                writer.setArgb(x, y, pair.getValue());
            });

    return copy;
}
 
Example 10
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 11
Source File: FuzzyTestImageUtils.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
private static double comparePoorMansImageComparison(final Image img1, final Image img2) {
    final int width = (int) img1.getWidth();
    final int height = (int) img1.getHeight();
    if (width != (int) img2.getWidth() || height != (int) img2.getHeight()) {
        return 0.0;
    }
    final double nPixels = width * height;
    double diff = 1.0;
    final PixelReader pixelReaderRef = img1.getPixelReader();
    final PixelReader pixelReaderTest = img2.getPixelReader();
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            diff *= 1.0 - (1.0 - pixelDiff(pixelReaderRef.getColor(x, y), pixelReaderTest.getColor(x, y))) / nPixels;
        }
    }
    return diff;
}
 
Example 12
Source File: WriteFxImage.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public static PaletteQuantizer estimatePalette(final Image image, final boolean alpha, final int nColors) {
    if (image == null) {
        throw new IllegalArgumentException(IMAGE_MUST_NOT_BE_NULL);
    }
    // get meta info
    final PixelReader pr = image.getPixelReader();
    if (pr == null) {
        throw new IllegalStateException(IMAGE_PIXEL_READER_NOT_AVAILABLE);
    }
    final int w = (int) image.getWidth();
    final int h = (int) image.getHeight();

    PaletteQuantizerNeuQuant cuant = new PaletteQuantizerNeuQuant(w, h, (x, y) -> pr.getArgb(y, x));
    cuant.setParReserveAlphaColor(alpha);
    cuant.setParNcolors(nColors);
    cuant.run();

    return cuant;
}
 
Example 13
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 14
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 15
Source File: OpenCVUtils.java    From OpenLabeler with Apache License 2.0 5 votes vote down vote up
/**
 * Return OpenCV MAT in CvType.CV_8UC4
 */
public static Mat imageToMat(Image image) {
   int width = (int) image.getWidth();
   int height = (int) image.getHeight();
   byte[] buffer = new byte[width * height * 4];

   PixelReader reader = image.getPixelReader();
   WritablePixelFormat<ByteBuffer> format = WritablePixelFormat.getByteBgraInstance();
   reader.getPixels(0, 0, width, height, format, buffer, 0, width * 4);

   Mat mat = new Mat(height, width, CvType.CV_8UC4);
   mat.put(0, 0, buffer);
   return mat;
}
 
Example 16
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image cropOutsideFx(Image image, DoubleShape shape, Color bgColor) {
    try {
        if (image == null || shape == null || !shape.isValid()
                || bgColor == null) {
            return image;
        }
        int width = (int) image.getWidth();
        int height = (int) image.getHeight();
        DoubleRectangle bound = shape.getBound();

        int x1 = (int) Math.round(Math.max(0, bound.getSmallX()));
        int y1 = (int) Math.round(Math.max(0, bound.getSmallY()));
        if (x1 >= width || y1 >= height) {
            return image;
        }
        int x2 = (int) Math.round(Math.min(width - 1, bound.getBigX()));
        int y2 = (int) Math.round(Math.min(height - 1, bound.getBigY()));
        int w = x2 - x1 + 1;
        int h = y2 - y1 + 1;
        PixelReader pixelReader = image.getPixelReader();
        WritableImage newImage = new WritableImage(w, h);
        PixelWriter pixelWriter = newImage.getPixelWriter();
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (shape.include(x1 + x, y1 + y)) {
                    pixelWriter.setColor(x, y, pixelReader.getColor(x1 + x, y1 + y));
                } else {
                    pixelWriter.setColor(x, y, bgColor);
                }
            }
        }
        return newImage;
    } catch (Exception e) {
        logger.debug(e.toString());
        return image;
    }
}
 
Example 17
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image dragMarginsFx(Image image, Color color, DoubleRectangle rect) {
    try {
        if (image == null || rect == null) {
            return image;
        }
        int iwidth = (int) image.getWidth();
        int iheight = (int) image.getHeight();

        int rwidth = (int) rect.getWidth();
        int rheight = (int) rect.getHeight();
        int rx = (int) rect.getSmallX();
        int ry = (int) rect.getSmallY();
        PixelReader pixelReader = image.getPixelReader();
        WritableImage newImage = new WritableImage(rwidth, rheight);
        PixelWriter pixelWriter = newImage.getPixelWriter();
        int ix, iy;
        for (int j = 0; j < rheight; ++j) {
            for (int i = 0; i < rwidth; ++i) {
                ix = i + rx;
                iy = j + ry;
                if (ix >= 0 && ix < iwidth && iy >= 0 && iy < iheight) {
                    pixelWriter.setColor(i, j, pixelReader.getColor(ix, iy));
                } else {
                    pixelWriter.setColor(i, j, color);
                }
            }
        }

        return newImage;

    } catch (Exception e) {
        logger.error(e.toString());
        return image;
    }

}
 
Example 18
Source File: NormalMap.java    From FXyzLib with GNU General Public License v3.0 5 votes vote down vote up
public NormalMap(Image src){
    super(src.getPixelReader(),0,0, (int)src.getWidth(), (int)src.getHeight());
    this.srcImage = src;
    this.pReader = getPixelReader();
    this.pWriter = getPixelWriter();
    this.buildNormalMap(DEFAULT_INTENSITY, DEFAULT_INTENSITY_SCALE, DEFAULT_INVERTED);
}
 
Example 19
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public static Image addMarginsFx(Image image, Color color, int MarginWidth,
            boolean addTop, boolean addBottom, boolean addLeft, boolean addRight) {
        try {
            if (image == null || MarginWidth <= 0) {
                return image;
            }
            int width = (int) image.getWidth();
            int height = (int) image.getHeight();
            int totalWidth = width, totalHegiht = height;
            int x1 = 0, y1 = 0, x2 = width, y2 = height;
            if (addLeft) {
                totalWidth += MarginWidth;
                x1 = MarginWidth;
                x2 = width + MarginWidth;
            }
            if (addRight) {
                totalWidth += MarginWidth;
            }
            if (addTop) {
                totalHegiht += MarginWidth;
                y1 = MarginWidth;
                y2 = height + MarginWidth;
            }
            if (addBottom) {
                totalHegiht += MarginWidth;
            }
//            logger.debug(width + "  " + totalWidth);

            PixelReader pixelReader = image.getPixelReader();
            WritableImage newImage = new WritableImage(totalWidth, totalHegiht);
            PixelWriter pixelWriter = newImage.getPixelWriter();
            pixelWriter.setPixels(x1, y1, width, height, pixelReader, 0, 0);

//            logger.debug(x1 + "  " + y1);
//            logger.debug(totalWidth + "  " + totalHegiht);
            if (addLeft) {
                for (int x = 0; x < MarginWidth; x++) {
                    for (int y = 0; y < totalHegiht; y++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }
            if (addRight) {
                for (int x = totalWidth - 1; x > totalWidth - MarginWidth - 1; x--) {
                    for (int y = 0; y < totalHegiht; y++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }
            if (addTop) {
                for (int y = 0; y < MarginWidth; y++) {
                    for (int x = 0; x < totalWidth; x++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }
            if (addBottom) {
                for (int y = totalHegiht - 1; y > totalHegiht - MarginWidth - 1; y--) {
                    for (int x = 0; x < totalWidth; x++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }

            return newImage;

        } catch (Exception e) {
            logger.error(e.toString());
            return image;
        }

    }
 
Example 20
Source File: ImageUtils.java    From Aidos with GNU General Public License v3.0 4 votes vote down vote up
public static Image crop(Image img,int x,int y,int w,int h){
    PixelReader reader = img.getPixelReader();
    WritableImage newImage = new WritableImage(reader, x, y, w, h);
    return newImage;
}