下面列出了怎么用javafx.scene.image.PixelReader的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
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);
}
}
}
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;
}
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;
}
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;
}
}
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;
}
}
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;
}
}
@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);
}
}
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;
}
@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);
}
}
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;
}
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;
}
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;
}
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;
}
/**
* 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);
}
@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;
}
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;
}
@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;
}
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();
}
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();
}
/**
* 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);
}
}
}
}
/**
* 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;
}
/**
* 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;
}
/**
* 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);
}
}
}
/**
* Encodes a JavaFx image as an RGB png image. If you pass in a ByteBuffer to
* use, please make sure that it has enough capacity to fit the encoded image or
* handle errors (IndexOutOfBoundsException) accordingly. If you want to be on
* the safe side, use {@link #getCompressedSizeBound(int, int, boolean)
* getCompressedSizeBound(width, height, alpha)} to get an upper bound for the
* output size.
*
* @param image The input image to be encoded
* @param byteBuffer optional byte buffer to store the output in, pass
* null to return a new one.
* @param alpha whether to include alpha information in the image
* @param compressionLevel {@link Deflater#BEST_COMPRESSION} (9) to
* {@link Deflater#BEST_SPEED} (0)
* @param metaInfo an optional map which will be filled with debugging
* information like compression efficiency
* @return a byte buffer with the encoded image
* @see "https://tools.ietf.org/html/rfc2083"
*/
public static ByteBuffer encodeAlt(final Image image, final ByteBuffer byteBuffer, final boolean alpha, final int compressionLevel, final Map<String, Object> metaInfo) {
if (image == null) {
throw new IllegalArgumentException(IMAGE_MUST_NOT_BE_NULL);
}
// get meta info
// get necessary helper classes
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();
// allocate output buffer if necessary. conservative allocation, assume upper
// bound for deflate algorithm
final ByteBuffer outputByteBuffer = byteBuffer == null ? ByteBuffer.allocate(getCompressedSizeBound(w, h, alpha)) : byteBuffer;
final CRC32 crc = new CRC32();
final Deflater compressor = new Deflater(compressionLevel);
// actual PNG encoding
writeImageHeader(w, h, alpha, outputByteBuffer, crc);
writeImageData(pr, w, h, alpha, compressor, outputByteBuffer, crc);
writeImageFooter(outputByteBuffer, crc);
// prepare to return outputBuffer
outputByteBuffer.flip();
// DEGBUG output
if (metaInfo != null) {
final int bytesPerPixel = alpha ? 4 : 3;
metaInfo.put("bufferSize", outputByteBuffer.capacity());
metaInfo.put("outputSizeBound", getCompressedSizeBound(w, h, alpha));
metaInfo.put("compression", (double) compressor.getBytesWritten() / (w * h * bytesPerPixel));
metaInfo.put("width", w);
metaInfo.put("height", h);
metaInfo.put("colorMode", alpha ? "rgba" : "rgb");
metaInfo.put("compressionLevel", compressionLevel);
metaInfo.put("outputSize", outputByteBuffer.limit() - outputByteBuffer.position());
}
return outputByteBuffer;
}
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);
}
}
}
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);
}
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;
}
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;
}
}
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;
}
}