下面列出了javafx.scene.image.WritableImage#getPixelWriter ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
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;
}
}
@Override
@BackgroundThread
protected void writeData(@NotNull final VarTable vars, final @NotNull Path resultFile) throws IOException {
super.writeData(vars, resultFile);
final Color color = UiUtils.from(vars.get(PROP_COLOR, ColorRGBA.class));
final int width = vars.getInteger(PROP_WIDTH);
final int height = vars.getInteger(PROP_HEIGHT);
final WritableImage writableImage = new WritableImage(width, height);
final PixelWriter pixelWriter = writableImage.getPixelWriter();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
pixelWriter.setColor(i, j, color);
}
}
final BufferedImage bufferedImage = SwingFXUtils.fromFXImage(writableImage, null);
try (final OutputStream out = Files.newOutputStream(resultFile)) {
ImageIO.write(bufferedImage, "png", out);
}
}
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;
}
}
@Override
public void start(final Stage stage)
{
// Image with red border
final WritableImage image = new WritableImage(WIDTH, HEIGHT);
final PixelWriter writer = image.getPixelWriter();
for (int x=0; x<WIDTH; ++x)
{
writer.setColor(x, 0, Color.RED);
writer.setColor(x, HEIGHT-1, Color.RED);
}
for (int y=0; y<HEIGHT; ++y)
{
writer.setColor(0, y, Color.RED);
writer.setColor(WIDTH-1, y, Color.RED);
}
// Draw into canvas, scaling 'up'
final Canvas canvas = new Canvas(800, 600);
canvas.getGraphicsContext2D().drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight());
final Scene scene = new Scene(new Group(canvas), canvas.getWidth(), canvas.getHeight());
stage.setScene(scene);
stage.show();
}
protected void showBW(WritableImage screen, int xOffset, int y, int dhgrWord) {
// Using the data buffer directly is about 15 times faster than setRGB
// This is because setRGB does extra (useless) color model logic
// For that matter even Graphics.drawLine is faster than setRGB!
// This is equivilant to y*560 but is 5% faster
// Also, adding xOffset now makes it additionally 5% faster
//int yOffset = ((y << 4) + (y << 5) + (y << 9))+xOffset;
int xx = xOffset;
PixelWriter writer = screen.getPixelWriter();
for (int i = 0; i < 28; i++) {
if (xx < 560) {
// yOffset++ is used instead of yOffset+i, because it is faster
writer.setColor(xx++, y, (dhgrWord & 1) == 1 ? WHITE : BLACK);
}
dhgrWord >>= 1;
}
}
private Image getHuesCircle(int width, int height) {
WritableImage raster = new WritableImage(width, height);
PixelWriter pixelWriter = raster.getPixelWriter();
Point2D center = new Point2D((double) width / 2, (double) height / 2);
double rsmall = 0.8 * width / 2;
double rbig = (double) width / 2;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
double dx = x - center.getX();
double dy = y - center.getY();
double distance = Math.sqrt((dx * dx) + (dy * dy));
double o = Math.atan2(dy, dx);
if (distance > rsmall && distance < rbig) {
double H = map(o, -Math.PI, Math.PI, 0, 255);
double S = 255;
double L = 152;
pixelWriter.setColor(x, y, HSL2RGB(H, S, L));
}
}
}
return raster;
}
private Image createNoiseImage(final double WIDTH, final double HEIGHT, final Color DARK_COLOR, final Color BRIGHT_COLOR, final double ALPHA_VARIATION_IN_PERCENT) {
int width = WIDTH <= 0 ? (int) PREFERRED_WIDTH : (int) WIDTH;
int height = HEIGHT <= 0 ? (int) PREFERRED_HEIGHT : (int) HEIGHT;
double alphaVariationInPercent = getSkinnable().clamp(0, 100, ALPHA_VARIATION_IN_PERCENT);
final WritableImage IMAGE = new WritableImage(width, height);
final PixelWriter PIXEL_WRITER = IMAGE.getPixelWriter();
final Random BW_RND = new Random();
final Random ALPHA_RND = new Random();
final double ALPHA_START = alphaVariationInPercent / 100 / 2;
final double ALPHA_VARIATION = alphaVariationInPercent / 100;
Color noiseColor;
double noiseAlpha;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
noiseColor = BW_RND.nextBoolean() == true ? BRIGHT_COLOR : DARK_COLOR;
noiseAlpha = getSkinnable().clamp(0, 1, ALPHA_START + ALPHA_RND.nextDouble() * ALPHA_VARIATION);
PIXEL_WRITER.setColor(x, y, Color.color(noiseColor.getRed(), noiseColor.getGreen(), noiseColor.getBlue(), noiseAlpha));
}
}
return IMAGE;
}
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;
}
protected void displayLores(WritableImage screen, int xOffset, int y, int rowAddress) {
int c1 = ((RAM128k) computer.getMemory()).getMainMemory().readByte(rowAddress + xOffset) & 0x0FF;
if ((y & 7) < 4) {
c1 &= 15;
} else {
c1 >>= 4;
}
Color color = Palette.color[c1];
// Unrolled loop, faster
PixelWriter writer = screen.getPixelWriter();
int xx = xOffset * 14;
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
writer.setColor(xx++, y, color);
}
/**
* Draws a color wheel into the given {@link WritableImage}, starting at the
* given offsets, in the given size (in pixel).
*
* @param image
* The {@link WritableImage} in which the color wheel is drawn.
* @param offsetX
* The horizontal offset (in pixel).
* @param offsetY
* The vertical offset (in pixel).
* @param size
* The size (in pixel).
*/
private void renderColorWheel(WritableImage image, int offsetX, int offsetY,
int size) {
PixelWriter px = image.getPixelWriter();
double radius = size / 2;
Point2D mid = new Point2D(radius, radius);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
double d = mid.distance(x, y);
if (d <= radius) {
// compute hue angle
double angleRad = d == 0 ? 0
: Math.atan2(y - mid.getY(), x - mid.getX());
// compute saturation depending on distance to
// middle
// ([0;1])
double sat = d / radius;
Color color = Color.hsb(angleRad * 180 / Math.PI, sat, 1);
px.setColor(offsetX + x, offsetY + y, color);
} else {
px.setColor(offsetX + x, offsetY + y, Color.TRANSPARENT);
}
}
}
}
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;
}
private static void renderImage(WritableImage img, double gridSize, double hueFactor, double hueOffset) {
PixelWriter pw = img.getPixelWriter();
double w = img.getWidth();
double h = img.getHeight();
double xRatio = 0.0;
double yRatio = 0.0;
double hue = 0.0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
xRatio = x/w;
yRatio = y/h;
hue = Math.sin(yRatio*(gridSize*Math.PI))*Math.sin(xRatio*(gridSize*Math.PI))*Math.tan(hueFactor/20.0)*360.0 + hueOffset;
Color c = Color.hsb(hue, 1.0, 1.0);
pw.setColor(x, y, c);
}
}
}
private static void renderImage(WritableImage img, double gridSize, double hueFactor, double hueOffset) {
PixelWriter pw = img.getPixelWriter();
double w = img.getWidth();
double h = img.getHeight();
double xRatio = 0.0;
double yRatio = 0.0;
double hue = 0.0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
xRatio = x/w;
yRatio = y/h;
hue = Math.sin(yRatio*(gridSize*Math.PI))*Math.sin(xRatio*(gridSize*Math.PI))*Math.tan(hueFactor/20.0)*360.0 + hueOffset;
Color c = Color.hsb(hue, 1.0, 1.0);
pw.setColor(x, y, c);
}
}
}
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;
}
/**
* Get an image filled with the specified colour.
* <p/>
* @param color the colour of the image.
* @return the image.
*/
public static Image getImageFromColour(final Color color) {
WritableImage image = new WritableImage(2, 2);
PixelWriter writer = image.getPixelWriter();
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
writer.setColor(i, j, color);
}
}
return image;
}
public static Image createImage(int width, int height, Color color) {
WritableImage newImage = new WritableImage(width, height);
PixelWriter pixelWriter = newImage.getPixelWriter();
for (int y = 0; y < newImage.getHeight(); y++) {
for (int x = 0; x < newImage.getWidth(); x++) {
pixelWriter.setColor(x, y, color);
}
}
return newImage;
}
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;
}
}
private Image getSLCricle(int width, int height) {
WritableImage raster = new WritableImage(width, height);
PixelWriter pixelWriter = raster.getPixelWriter();
Point2D center = new Point2D((double) width / 2, (double) height / 2);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
double dy = x - center.getX();
double dx = y - center.getY();
pixelWriter.setColor(x, y, getColor(dx, dy));
}
}
return raster;
}