pixelDensity (FX): draw 2x images properly

This commit is contained in:
Jakub Valtar
2017-02-19 00:18:01 +01:00
parent 8f7e3b1ff3
commit c24164c320
+24 -19
View File
@@ -1031,8 +1031,8 @@ public class PGraphicsFX2D extends PGraphics {
// Nuke the cache if the image was resized
if (cash != null) {
if (who.width != cash.image.getWidth() ||
who.height != cash.image.getHeight()) {
if (who.pixelWidth != cash.image.getWidth() ||
who.pixelHeight != cash.image.getHeight()) {
cash = null;
}
}
@@ -1059,12 +1059,17 @@ public class PGraphicsFX2D extends PGraphics {
// This might be a PGraphics that hasn't been drawn to yet.
// Can't just bail because the cache has been created above.
// https://github.com/processing/processing/issues/2208
who.pixels = new int[who.width * who.height];
who.pixels = new int[who.pixelWidth * who.pixelHeight];
}
cash.update(who, tint, tintColor);
who.setModified(false);
}
u1 *= who.pixelDensity;
v1 *= who.pixelDensity;
u2 *= who.pixelDensity;
v2 *= who.pixelDensity;
context.drawImage(((ImageCache) getCache(who)).image,
u1, v1, u2-u1, v2-v1,
x1, y1, x2-x1, y2-y1);
@@ -1105,14 +1110,14 @@ public class PGraphicsFX2D extends PGraphics {
// BufferedImage.TYPE_INT_ARGB);
// }
if (image == null) {
image = new WritableImage(source.width, source.height);
image = new WritableImage(source.pixelWidth, source.pixelHeight);
}
//WritableRaster wr = image.getRaster();
PixelWriter pw = image.getPixelWriter();
if (tint) {
if (tintedTemp == null || tintedTemp.length != source.width) {
tintedTemp = new int[source.width];
if (tintedTemp == null || tintedTemp.length != source.pixelWidth) {
tintedTemp = new int[source.pixelWidth];
}
int a2 = (tintColor >> 24) & 0xff;
// System.out.println("tint color is " + a2);
@@ -1126,8 +1131,8 @@ public class PGraphicsFX2D extends PGraphics {
// The target image is opaque, meaning that the source image has no
// alpha (is not ARGB), and the tint has no alpha.
int index = 0;
for (int y = 0; y < source.height; y++) {
for (int x = 0; x < source.width; x++) {
for (int y = 0; y < source.pixelHeight; y++) {
for (int x = 0; x < source.pixelWidth; x++) {
int argb1 = source.pixels[index++];
int r1 = (argb1 >> 16) & 0xff;
int g1 = (argb1 >> 8) & 0xff;
@@ -1142,7 +1147,7 @@ public class PGraphicsFX2D extends PGraphics {
(((b2 * b1) & 0xff00) >> 8);
}
//wr.setDataElements(0, y, source.width, 1, tintedTemp);
pw.setPixels(0, y, source.width, 1, argbFormat, tintedTemp, 0, source.width);
pw.setPixels(0, y, source.pixelWidth, 1, argbFormat, tintedTemp, 0, source.pixelWidth);
}
// could this be any slower?
// float[] scales = { tintR, tintG, tintB };
@@ -1156,19 +1161,19 @@ public class PGraphicsFX2D extends PGraphics {
(tintColor & 0xffffff) == 0xffffff) {
int hi = tintColor & 0xff000000;
int index = 0;
for (int y = 0; y < source.height; y++) {
for (int x = 0; x < source.width; x++) {
for (int y = 0; y < source.pixelHeight; y++) {
for (int x = 0; x < source.pixelWidth; x++) {
tintedTemp[x] = hi | (source.pixels[index++] & 0xFFFFFF);
}
//wr.setDataElements(0, y, source.width, 1, tintedTemp);
pw.setPixels(0, y, source.width, 1, argbFormat, tintedTemp, 0, source.width);
pw.setPixels(0, y, source.pixelWidth, 1, argbFormat, tintedTemp, 0, source.pixelHeight);
}
} else {
int index = 0;
for (int y = 0; y < source.height; y++) {
for (int y = 0; y < source.pixelHeight; y++) {
if (source.format == RGB) {
int alpha = tintColor & 0xFF000000;
for (int x = 0; x < source.width; x++) {
for (int x = 0; x < source.pixelWidth; x++) {
int argb1 = source.pixels[index++];
int r1 = (argb1 >> 16) & 0xff;
int g1 = (argb1 >> 8) & 0xff;
@@ -1179,7 +1184,7 @@ public class PGraphicsFX2D extends PGraphics {
(((b2 * b1) & 0xff00) >> 8);
}
} else if (source.format == ARGB) {
for (int x = 0; x < source.width; x++) {
for (int x = 0; x < source.pixelWidth; x++) {
int argb1 = source.pixels[index++];
int a1 = (argb1 >> 24) & 0xff;
int r1 = (argb1 >> 16) & 0xff;
@@ -1193,14 +1198,14 @@ public class PGraphicsFX2D extends PGraphics {
}
} else if (source.format == ALPHA) {
int lower = tintColor & 0xFFFFFF;
for (int x = 0; x < source.width; x++) {
for (int x = 0; x < source.pixelWidth; x++) {
int a1 = source.pixels[index++];
tintedTemp[x] =
(((a2 * a1) & 0xff00) << 16) | lower;
}
}
//wr.setDataElements(0, y, source.width, 1, tintedTemp);
pw.setPixels(0, y, source.width, 1, argbFormat, tintedTemp, 0, source.width);
pw.setPixels(0, y, source.pixelWidth, 1, argbFormat, tintedTemp, 0, source.pixelWidth);
}
}
// Not sure why ARGB images take the scales in this order...
@@ -1222,8 +1227,8 @@ public class PGraphicsFX2D extends PGraphics {
// If no tint, just shove the pixels on in there verbatim
//wr.setDataElements(0, 0, source.width, source.height, source.pixels);
//System.out.println("moving the big one");
pw.setPixels(0, 0, source.width, source.height,
argbFormat, source.pixels, 0, source.width);
pw.setPixels(0, 0, source.pixelWidth, source.pixelHeight,
argbFormat, source.pixels, 0, source.pixelWidth);
}
this.tinted = tint;
this.tintedColor = tintColor;