javafx.scene.image.PixelReader Java Examples

The following examples show how to use javafx.scene.image.PixelReader. 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: 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 #3
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 #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: 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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
Source File: PupilAndIrisDetector.java    From Augendiagnose with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Collect the information of all circles around the center.
 *
 * @param maxRelevantRadius The maximal circle radius considered
 */
private void collectCircleInfo(final int maxRelevantRadius) {
	PixelReader pixelReader = mImage.getPixelReader();
	int maxPossibleRadius = (int) Math.min(
			Math.min(mImage.getWidth() - 1 - mXCenter, mXCenter),
			Math.min(mImage.getHeight() - 1 - mYCenter, mYCenter));
	int maxRadius = Math.min(maxRelevantRadius, maxPossibleRadius);
	// For iris refinement, ignore points on top and bottom
	long maxRadius2 = (maxRadius + 1) * (maxRadius + 1);
	for (int x = mXCenter - maxRadius; x <= mXCenter + maxRadius; x++) {
		for (int y = mYCenter - maxRadius; y <= mYCenter + maxRadius; y++) {
			long d2 = (x - mXCenter) * (x - mXCenter) + (y - mYCenter) * (y - mYCenter);
			if (d2 <= maxRadius2) {
				int d = (int) Math.round(Math.sqrt(d2));
				float brightness = getBrightness(pixelReader.getColor(x, y));
				addInfo(d, brightness);
			}
		}
	}

}
 
Example #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
Source File: WriteFxImage.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public static void copyImageDataToPixelBuffer(final Image image, final int[] uncompressedImageData) {
    if (image == null) {
        throw new IllegalArgumentException("image is null");
    }
    final PixelReader pr = image.getPixelReader();
    if (pr == null) {
        throw new IllegalStateException(IMAGE_PIXEL_READER_NOT_AVAILABLE);
    }
    if (uncompressedImageData == null) {
        throw new IllegalArgumentException("uncompressedImageData is null");
    }
    final int w = (int) image.getWidth();
    final int h = (int) image.getHeight();
    final int requiredSize = w * h;
    if (uncompressedImageData.length < requiredSize) {
        throw new IllegalArgumentException("uncompressedImageData.length = " //
                                           + uncompressedImageData.length + " too small, should be at least" + requiredSize);
    }
    int i = 0;
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            uncompressedImageData[i++] = pr.getArgb(x, y);
        }
    }
}
 
Example #23
Source File: HelperTest.java    From latexdraw with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Compute the similarity of two JavaFX images.
 * @param image1 The first image to test.
 * @param image2 The second image to test.
 * @return A double value in [0;100] corresponding to the similarity between the two images (pixel comparison).
 * @throws NullPointerException If image1 or image2 is null.
 */
default double computeSnapshotSimilarity(final Image image1, final Image image2) {
	final int width = (int) image1.getWidth();
	final int height = (int) image1.getHeight();
	final PixelReader reader1 = image1.getPixelReader();
	final PixelReader reader2 = image2.getPixelReader();

	final double nbNonSimilarPixels = IntStream.range(0, width).parallel().mapToLong(i ->
		IntStream.range(0, height).parallel().filter(j -> reader1.getArgb(i, j) != reader2.getArgb(i, j)).count()).sum();

	return 100d - nbNonSimilarPixels / (width * height) * 100d;
}
 
Example #24
Source File: PupilAndIrisDetector.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Search points on the iris boundary.
 */
private void determineBoundaryPoints() {
	PixelReader pixelReader = mImage.getPixelReader();

	for (int yCoord = mYCenter; yCoord <= mYCenter + mRadius * IRIS_BOUNDARY_SEARCH_RANGE && yCoord < mImage.getHeight(); yCoord++) {
		determineBoundaryPoints(pixelReader, yCoord);
	}

	for (int yCoord = mYCenter - 1; yCoord >= mYCenter - mRadius * IRIS_BOUNDARY_SEARCH_RANGE && yCoord >= 0; yCoord--) {
		determineBoundaryPoints(pixelReader, yCoord);
	}
}
 
Example #25
Source File: PupilAndIrisDetector.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine the boundary points for a certain y coordinate.
 *
 * @param pixelReader The pixel reader.
 * @param yCoord The y coordinate for which to find the boundary points.
 * @return true if a boundary point has been found.
 */
private boolean determineBoundaryPoints(final PixelReader pixelReader, final int yCoord) {
	int xDistanceRange = Math.round(IRIS_BOUNDARY_UNCERTAINTY_FACTOR * mRadius);
	int xDistanceMinRange = Math.round(IRIS_BOUNDARY_MIN_RANGE * mRadius);
	boolean found = false;

	while (!found && xDistanceRange >= xDistanceMinRange) {
		found = determineBoundaryPoints(pixelReader, yCoord, xDistanceRange);
		xDistanceRange *= IRIS_BOUNDARY_RETRY_FACTOR;
	}
	return found;
}
 
Example #26
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 #27
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 #28
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 #29
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 #30
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);
}