Skip to content

Commit e94d290

Browse files
committed
pixelDensity (FX): draw 2x images properly
1 parent 5d3d9aa commit e94d290

1 file changed

Lines changed: 24 additions & 19 deletions

File tree

core/src/processing/javafx/PGraphicsFX2D.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,8 +1031,8 @@ protected void imageImpl(PImage who,
10311031

10321032
// Nuke the cache if the image was resized
10331033
if (cash != null) {
1034-
if (who.width != cash.image.getWidth() ||
1035-
who.height != cash.image.getHeight()) {
1034+
if (who.pixelWidth != cash.image.getWidth() ||
1035+
who.pixelHeight != cash.image.getHeight()) {
10361036
cash = null;
10371037
}
10381038
}
@@ -1059,12 +1059,17 @@ protected void imageImpl(PImage who,
10591059
// This might be a PGraphics that hasn't been drawn to yet.
10601060
// Can't just bail because the cache has been created above.
10611061
// https://github.com/processing/processing/issues/2208
1062-
who.pixels = new int[who.width * who.height];
1062+
who.pixels = new int[who.pixelWidth * who.pixelHeight];
10631063
}
10641064
cash.update(who, tint, tintColor);
10651065
who.setModified(false);
10661066
}
10671067

1068+
u1 *= who.pixelDensity;
1069+
v1 *= who.pixelDensity;
1070+
u2 *= who.pixelDensity;
1071+
v2 *= who.pixelDensity;
1072+
10681073
context.drawImage(((ImageCache) getCache(who)).image,
10691074
u1, v1, u2-u1, v2-v1,
10701075
x1, y1, x2-x1, y2-y1);
@@ -1105,14 +1110,14 @@ public void update(PImage source, boolean tint, int tintColor) {
11051110
// BufferedImage.TYPE_INT_ARGB);
11061111
// }
11071112
if (image == null) {
1108-
image = new WritableImage(source.width, source.height);
1113+
image = new WritableImage(source.pixelWidth, source.pixelHeight);
11091114
}
11101115

11111116
//WritableRaster wr = image.getRaster();
11121117
PixelWriter pw = image.getPixelWriter();
11131118
if (tint) {
1114-
if (tintedTemp == null || tintedTemp.length != source.width) {
1115-
tintedTemp = new int[source.width];
1119+
if (tintedTemp == null || tintedTemp.length != source.pixelWidth) {
1120+
tintedTemp = new int[source.pixelWidth];
11161121
}
11171122
int a2 = (tintColor >> 24) & 0xff;
11181123
// System.out.println("tint color is " + a2);
@@ -1126,8 +1131,8 @@ public void update(PImage source, boolean tint, int tintColor) {
11261131
// The target image is opaque, meaning that the source image has no
11271132
// alpha (is not ARGB), and the tint has no alpha.
11281133
int index = 0;
1129-
for (int y = 0; y < source.height; y++) {
1130-
for (int x = 0; x < source.width; x++) {
1134+
for (int y = 0; y < source.pixelHeight; y++) {
1135+
for (int x = 0; x < source.pixelWidth; x++) {
11311136
int argb1 = source.pixels[index++];
11321137
int r1 = (argb1 >> 16) & 0xff;
11331138
int g1 = (argb1 >> 8) & 0xff;
@@ -1142,7 +1147,7 @@ public void update(PImage source, boolean tint, int tintColor) {
11421147
(((b2 * b1) & 0xff00) >> 8);
11431148
}
11441149
//wr.setDataElements(0, y, source.width, 1, tintedTemp);
1145-
pw.setPixels(0, y, source.width, 1, argbFormat, tintedTemp, 0, source.width);
1150+
pw.setPixels(0, y, source.pixelWidth, 1, argbFormat, tintedTemp, 0, source.pixelWidth);
11461151
}
11471152
// could this be any slower?
11481153
// float[] scales = { tintR, tintG, tintB };
@@ -1156,19 +1161,19 @@ public void update(PImage source, boolean tint, int tintColor) {
11561161
(tintColor & 0xffffff) == 0xffffff) {
11571162
int hi = tintColor & 0xff000000;
11581163
int index = 0;
1159-
for (int y = 0; y < source.height; y++) {
1160-
for (int x = 0; x < source.width; x++) {
1164+
for (int y = 0; y < source.pixelHeight; y++) {
1165+
for (int x = 0; x < source.pixelWidth; x++) {
11611166
tintedTemp[x] = hi | (source.pixels[index++] & 0xFFFFFF);
11621167
}
11631168
//wr.setDataElements(0, y, source.width, 1, tintedTemp);
1164-
pw.setPixels(0, y, source.width, 1, argbFormat, tintedTemp, 0, source.width);
1169+
pw.setPixels(0, y, source.pixelWidth, 1, argbFormat, tintedTemp, 0, source.pixelHeight);
11651170
}
11661171
} else {
11671172
int index = 0;
1168-
for (int y = 0; y < source.height; y++) {
1173+
for (int y = 0; y < source.pixelHeight; y++) {
11691174
if (source.format == RGB) {
11701175
int alpha = tintColor & 0xFF000000;
1171-
for (int x = 0; x < source.width; x++) {
1176+
for (int x = 0; x < source.pixelWidth; x++) {
11721177
int argb1 = source.pixels[index++];
11731178
int r1 = (argb1 >> 16) & 0xff;
11741179
int g1 = (argb1 >> 8) & 0xff;
@@ -1179,7 +1184,7 @@ public void update(PImage source, boolean tint, int tintColor) {
11791184
(((b2 * b1) & 0xff00) >> 8);
11801185
}
11811186
} else if (source.format == ARGB) {
1182-
for (int x = 0; x < source.width; x++) {
1187+
for (int x = 0; x < source.pixelWidth; x++) {
11831188
int argb1 = source.pixels[index++];
11841189
int a1 = (argb1 >> 24) & 0xff;
11851190
int r1 = (argb1 >> 16) & 0xff;
@@ -1193,14 +1198,14 @@ public void update(PImage source, boolean tint, int tintColor) {
11931198
}
11941199
} else if (source.format == ALPHA) {
11951200
int lower = tintColor & 0xFFFFFF;
1196-
for (int x = 0; x < source.width; x++) {
1201+
for (int x = 0; x < source.pixelWidth; x++) {
11971202
int a1 = source.pixels[index++];
11981203
tintedTemp[x] =
11991204
(((a2 * a1) & 0xff00) << 16) | lower;
12001205
}
12011206
}
12021207
//wr.setDataElements(0, y, source.width, 1, tintedTemp);
1203-
pw.setPixels(0, y, source.width, 1, argbFormat, tintedTemp, 0, source.width);
1208+
pw.setPixels(0, y, source.pixelWidth, 1, argbFormat, tintedTemp, 0, source.pixelWidth);
12041209
}
12051210
}
12061211
// Not sure why ARGB images take the scales in this order...
@@ -1222,8 +1227,8 @@ public void update(PImage source, boolean tint, int tintColor) {
12221227
// If no tint, just shove the pixels on in there verbatim
12231228
//wr.setDataElements(0, 0, source.width, source.height, source.pixels);
12241229
//System.out.println("moving the big one");
1225-
pw.setPixels(0, 0, source.width, source.height,
1226-
argbFormat, source.pixels, 0, source.width);
1230+
pw.setPixels(0, 0, source.pixelWidth, source.pixelHeight,
1231+
argbFormat, source.pixels, 0, source.pixelWidth);
12271232
}
12281233
this.tinted = tint;
12291234
this.tintedColor = tintColor;

0 commit comments

Comments
 (0)