Skip to content

Commit 7839e38

Browse files
committed
Should normally fix the issue in #3814
1 parent 8e87a0f commit 7839e38

File tree

6 files changed

+63
-80
lines changed

6 files changed

+63
-80
lines changed

msi.gama.core/src/msi/gama/outputs/display/AWTDisplayGraphics.java

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@
4141
import java.awt.geom.Rectangle2D;
4242
import java.awt.image.BufferedImage;
4343
import java.awt.image.ImageObserver;
44-
import java.util.HashMap;
4544
import java.util.List;
46-
import java.util.Map;
47-
import java.util.concurrent.ExecutionException;
48-
import java.util.concurrent.TimeUnit;
4945

5046
import org.locationtech.jts.awt.PointTransformation;
5147
import org.locationtech.jts.awt.ShapeWriter;
@@ -55,10 +51,6 @@
5551
import org.locationtech.jts.geom.Lineal;
5652
import org.locationtech.jts.geom.Puntal;
5753

58-
import com.google.common.cache.CacheBuilder;
59-
import com.google.common.cache.CacheLoader;
60-
import com.google.common.cache.LoadingCache;
61-
6254
import msi.gama.common.geometry.AxisAngle;
6355
import msi.gama.common.geometry.GeometryUtils;
6456
import msi.gama.common.interfaces.IAsset;
@@ -246,37 +238,22 @@ public Rectangle2D drawImage(final BufferedImage img, final DrawingAttributes at
246238
curY = yFromModelUnitsToPixels(attributes.getLocation().getY());
247239
}
248240
imageTransform.translate(curX, curY);
249-
double curWidth, curHeight;
241+
int curWidth, curHeight;
250242
if (attributes.getSize() == null) {
251243
curWidth = getLayerWidth();
252244
curHeight = getLayerHeight();
253245
} else {
254-
curWidth = wFromModelUnitsToPixels(attributes.getSize().getX());
255-
curHeight = hFromModelUnitsToPixels(attributes.getSize().getY());
246+
curWidth = (int) wFromModelUnitsToPixels(attributes.getSize().getX());
247+
curHeight = (int) hFromModelUnitsToPixels(attributes.getSize().getY());
256248
}
257-
258249
if (attributes.getAngle() != null) {
259250
imageTransform.rotate(Maths.toRad * attributes.getAngle(), curWidth / 2d, curHeight / 2d);
260-
// currentRenderer.rotate(Maths.toRad * attributes.getAngle(), curX + curWidth / 2d, curY + curHeight / 2d);
261251
}
262-
263-
BufferedImage after = img;
264-
Point2D.Double point = new Point2D.Double(curWidth, curHeight);
265-
try {
266-
Map<Point2D, BufferedImage> sizes = cache.get(img);
267-
if (sizes.containsKey(point)) {
268-
after = sizes.get(point);
269-
} else {
270-
after = resize(img, (int) Math.round(curWidth), (int) Math.round(curHeight));
271-
}
272-
} catch (ExecutionException e) {}
273-
274-
// imageTransform.scale(curWidth / img.getWidth(), curHeight / img.getHeight());
275-
currentRenderer.drawImage(after, imageTransform, null);
276-
// currentRenderer.drawImage(img, (int) FastMath.round(curX), (int) FastMath.round(curY), (int) curWidth,
277-
// (int) curHeight, null);
252+
if (curWidth != img.getWidth() || curHeight != img.getHeight()) {
253+
imageTransform.scale((double) curWidth / img.getWidth(), (double) curHeight / img.getHeight());
254+
}
255+
currentRenderer.drawImage(img, imageTransform, null);
278256
if (attributes.getBorder() != null) { drawGridLine(img, attributes.getBorder()); }
279-
// currentRenderer.setTransform(saved);
280257
rect.setRect(curX, curY, curWidth, curHeight);
281258
if (highlight) { highlightRectangleInPixels(rect); }
282259
return rect.getBounds2D();
@@ -303,21 +280,6 @@ public static BufferedImage resize(final BufferedImage img, final int newW, fina
303280
return dimg;
304281
}
305282

306-
/** The cache. */
307-
@SuppressWarnings ({ "unchecked",
308-
"unused" }) static LoadingCache<BufferedImage, Map<Point2D, BufferedImage>> cache =
309-
CacheBuilder.<BufferedImage, Map<Point2D, BufferedImage>> newBuilder()
310-
.expireAfterAccess(30, TimeUnit.SECONDS).removalListener(notif -> {
311-
Map<Point2D, BufferedImage> map = (Map<Point2D, BufferedImage>) notif.getValue();
312-
map.forEach((a, b) -> { b.flush(); });
313-
}).build(new CacheLoader<>() {
314-
315-
@Override
316-
public Map<Point2D, BufferedImage> load(final BufferedImage arg0) throws Exception {
317-
return new HashMap<>();
318-
}
319-
});
320-
321283
/**
322284
* Method drawString.
323285
*

msi.gama.core/src/msi/gaml/statements/draw/DrawStatement.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import msi.gaml.statements.draw.DrawStatement.DrawValidator;
6161
import msi.gaml.types.IType;
6262
import msi.gaml.types.Types;
63+
import ummisco.gama.dev.utils.DEBUG;
6364

6465
// A command that is used to draw shapes, figures, text on the display
6566

@@ -404,7 +405,7 @@ private Rectangle2D privateExecuteIn(final IGraphicsScope scope) throws GamaRunt
404405
} catch (final GamaRuntimeException e) {
405406
throw e;
406407
} catch (final Throwable e) {
407-
java.lang.System.err.println("Error when drawing in a display : " + e.getMessage());
408+
DEBUG.ERR("Error when drawing in a display : " + e.getMessage());
408409
e.printStackTrace();
409410
}
410411
return null;

ummisco.gaml.extensions.image/src/ummisco/gaml/extensions/image/GamaImage.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import msi.gama.common.interfaces.IAsset;
2222
import msi.gama.common.interfaces.IImageProvider;
2323
import msi.gama.common.interfaces.IKeyword;
24+
import msi.gama.common.interfaces.IValue;
2425
import msi.gama.metamodel.topology.grid.GamaSpatialMatrix;
2526
import msi.gama.precompiler.GamlAnnotations.doc;
2627
import msi.gama.precompiler.GamlAnnotations.getter;
2728
import msi.gama.precompiler.GamlAnnotations.variable;
2829
import msi.gama.precompiler.GamlAnnotations.vars;
2930
import msi.gama.runtime.IScope;
31+
import msi.gama.runtime.exceptions.GamaRuntimeException;
3032
import msi.gama.util.IList;
3133
import msi.gama.util.file.IFieldMatrixProvider;
3234
import msi.gama.util.matrix.GamaField;
@@ -53,10 +55,10 @@
5355
name = IKeyword.WIDTH,
5456
type = IType.INT,
5557
doc = { @doc ("Returns the width (in pixels) of this image") }) })
56-
public class GamaImage extends BufferedImage implements IImageProvider, IAsset, IFieldMatrixProvider {
58+
public class GamaImage extends BufferedImage implements IImageProvider, IAsset, IFieldMatrixProvider, IValue {
5759

5860
/** The id. */
59-
final String id;
61+
String id;
6062

6163
/**
6264
* Gets the id.
@@ -351,4 +353,22 @@ public static GamaImage from(final ColorModel cm, final WritableRaster raster, f
351353
return new GamaImage(cm, raster, b, "raster" + System.currentTimeMillis());
352354
}
353355

356+
/**
357+
* Sets the id.
358+
*
359+
* @param string
360+
* the new id
361+
*/
362+
public void setId(final String string) { id = string; }
363+
364+
@Override
365+
public String stringValue(final IScope scope) throws GamaRuntimeException {
366+
return id;
367+
}
368+
369+
@Override
370+
public GamaImage copy(final IScope scope) throws GamaRuntimeException {
371+
return from(this, this.getAlpha(scope));
372+
}
373+
354374
}

ummisco.gaml.extensions.image/src/ummisco/gaml/extensions/image/ImageConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.awt.Toolkit;
1616
import java.awt.color.ColorSpace;
1717
import java.awt.datatransfer.Clipboard;
18+
import java.awt.image.BufferedImageOp;
1819
import java.awt.image.ColorConvertOp;
1920
import java.awt.image.ConvolveOp;
2021
import java.awt.image.Kernel;
@@ -73,4 +74,8 @@ public interface ImageConstants {
7374
*/
7475
ColorConvertOp OP_GRAYSCALE = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
7576

77+
/** The descriptions. */
78+
Map<BufferedImageOp, String> DESCRIPTIONS = Map.of(OP_DARKER, "darker", OP_ANTIALIAS, "antialiased", OP_BRIGHTER,
79+
"brighter", OP_SHARPEN, "sharpened", OP_BLUR, "blurred", OP_GRAYSCALE, "grayscaled");
80+
7681
}

ummisco.gaml.extensions.image/src/ummisco/gaml/extensions/image/ImageHelper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static GamaImage apply(GamaImage src, final BufferedImageOp op) {
5050
GamaImage result =
5151
GamaImage.bestFor(src, (int) Math.round(bounds.getWidth()), (int) Math.round(bounds.getHeight()));
5252
op.filter(src, result);
53+
result.setId(src.getId() + DESCRIPTIONS.get(op));
5354
return result;
5455
}
5556

@@ -71,6 +72,7 @@ protected static GamaImage copyToOptimalImage(final Image src) {
7172
GamaImage result = GamaImage.ofDimensions(src.getWidth(null), src.getHeight(null), type);
7273
Graphics g = result.getGraphics();
7374
g.drawImage(src, 0, 0, null);
75+
result.setId((src instanceof GamaImage gi ? gi.getId() : "image") + "optimized");
7476
g.dispose();
7577
return result;
7678
}
@@ -123,6 +125,7 @@ static GamaImage rotate(final GamaImage src, final int typeOfRotation)
123125
g2d.setRenderingHints(HINTS);
124126
g2d.drawImage(src, tx, null);
125127
g2d.dispose();
128+
result.setId(src.getId() + "rotated" + typeOfRotation);
126129
return result;
127130
}
128131

@@ -185,6 +188,7 @@ static GamaImage resize(final GamaImage src, final Mode resizeMode, int targetWi
185188
} else {
186189
result = ImageHelper.scaleImageIncrementally(src, targetWidth, targetHeight);
187190
}
191+
result.setId(src.getId() + "resized" + targetWidth + "|" + targetHeight);
188192
return result;
189193
}
190194

@@ -226,7 +230,7 @@ static GamaImage scaleImageIncrementally(GamaImage src, final int targetWidth, f
226230
src = incrementalImage;
227231
hasReassignedSrc = true;
228232
} while (currentWidth != targetWidth || currentHeight != targetHeight);
229-
233+
src.setId(src.getId() + "resized" + targetWidth + "|" + targetHeight);
230234
return src;
231235
}
232236

@@ -247,7 +251,7 @@ static GamaImage scaleImageIncrementally(GamaImage src, final int targetWidth, f
247251
public static GamaImage scaleImage(final Image bufferedImage, final int targetWidth, final int targetHeight) {
248252
GamaImage result = GamaImage.bestFor(bufferedImage, targetWidth, targetHeight);
249253
Graphics2D resultGraphics = result.createGraphics();
250-
resultGraphics.setRenderingHints(ImageHelper.HINTS);
254+
resultGraphics.setRenderingHints(ImageConstants.HINTS);
251255
resultGraphics.drawImage(bufferedImage, 0, 0, targetWidth, targetHeight, null);
252256
resultGraphics.dispose();
253257
return result;

ummisco.gaml.extensions.image/src/ummisco/gaml/extensions/image/ImageOperators.java

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ public static GamaImage rotated(final IScope scope, final GamaImage image, final
401401
g2.rotate(rads, w / 2, h / 2);
402402
g2.drawImage(image, 0, 0, null);
403403
g2.dispose();
404+
rotated.setId(image.getId() + "rotated" + angleInDegrees);
404405
return rotated;
405406
}
406407

@@ -419,18 +420,18 @@ public static GamaImage rotated(final IScope scope, final GamaImage image, final
419420
@doc ("Returns the image tinted using the color passed in parameter. This effectively multiplies the colors of the image by it. The original image is left untouched")
420421
@no_test
421422
public static GamaImage tint(final IScope scope, final GamaImage image, final GamaColor color) {
422-
GamaImage tintedSprite = GamaImage.ofDimensions(image.getWidth(), image.getHeight(), Transparency.TRANSLUCENT);
423-
Graphics2D graphics = tintedSprite.createGraphics();
423+
GamaImage result = GamaImage.ofDimensions(image.getWidth(), image.getHeight(), Transparency.TRANSLUCENT);
424+
Graphics2D graphics = result.createGraphics();
424425
graphics.drawImage(image, 0, 0, null);
425426
graphics.dispose();
426-
ColorModel cm = tintedSprite.getColorModel();
427-
WritableRaster raster = tintedSprite.getRaster();
427+
ColorModel cm = result.getColorModel();
428+
WritableRaster raster = result.getRaster();
428429
float r = color.getRed() / 255f;
429430
float g = color.getGreen() / 255f;
430431
float b = color.getBlue() / 255f;
431432
float a = color.getAlpha() / 255f;
432-
for (int i = 0; i < tintedSprite.getWidth(); i++) {
433-
for (int j = 0; j < tintedSprite.getHeight(); j++) {
433+
for (int i = 0; i < result.getWidth(); i++) {
434+
for (int j = 0; j < result.getHeight(); j++) {
434435
int ax = cm.getAlpha(raster.getDataElements(i, j, null));
435436
int rx = cm.getRed(raster.getDataElements(i, j, null));
436437
int gx = cm.getGreen(raster.getDataElements(i, j, null));
@@ -439,10 +440,11 @@ public static GamaImage tint(final IScope scope, final GamaImage image, final Ga
439440
gx *= g;
440441
bx *= b;
441442
ax *= a;
442-
tintedSprite.setRGB(i, j, ax << 24 | rx << 16 | gx << 8 | bx);
443+
result.setRGB(i, j, ax << 24 | rx << 16 | gx << 8 | bx);
443444
}
444445
}
445-
return tintedSprite;
446+
result.setId(image.getId() + "tinted" + color.getRGB());
447+
return result;
446448
}
447449

448450
/**
@@ -476,30 +478,17 @@ public static GamaImage tint(final IScope scope, final GamaImage image, final Ga
476478
public static GamaImage tint(final IScope scope, final GamaImage image, final GamaColor color, final double ratio) {
477479
int w = image.getWidth();
478480
int h = image.getHeight();
479-
GamaImage dyed = GamaImage.ofDimensions(w, h, BufferedImage.TYPE_INT_ARGB);
480-
Graphics2D g = dyed.createGraphics();
481+
GamaImage result = GamaImage.ofDimensions(w, h, BufferedImage.TYPE_INT_ARGB);
482+
Graphics2D g = result.createGraphics();
481483
g.drawImage(image, 0, 0, null);
482484
g.setComposite(AlphaComposite.SrcAtop.derive(Math.min(1f, Math.max((float) ratio, 0f))));
483485
g.setColor(color);
484486
g.fillRect(0, 0, w, h);
485487
g.dispose();
486-
return dyed;
488+
result.setId(image.getId() + "tinted" + color + "|" + ratio);
489+
return result;
487490
}
488491

489-
/**
490-
* Blend.
491-
*
492-
* @param scope
493-
* the scope
494-
* @param image
495-
* the image
496-
* @param overlay
497-
* the overlay
498-
* @param ratio
499-
* the ratio
500-
* @return the gama image
501-
*/
502-
503492
/**
504493
* Blend.
505494
*
@@ -526,14 +515,15 @@ public static GamaImage tint(final IScope scope, final GamaImage image, final Ga
526515
@no_test
527516
public static GamaImage blend(final IScope scope, final GamaImage image, final GamaImage overlay,
528517
final double ratio) {
529-
GamaImage composed = ImageHelper.copyToOptimalImage(image);
530-
Graphics2D g2d = composed.createGraphics();
518+
GamaImage result = ImageHelper.copyToOptimalImage(image);
519+
Graphics2D g2d = result.createGraphics();
531520
g2d.setComposite(AlphaComposite.SrcOver.derive(Math.min(1f, Math.max((float) ratio, 0f))));
532-
int x = (composed.getWidth() - overlay.getWidth()) / 2;
533-
int y = (composed.getHeight() - overlay.getHeight()) / 2;
521+
int x = (result.getWidth() - overlay.getWidth()) / 2;
522+
int y = (result.getHeight() - overlay.getHeight()) / 2;
534523
g2d.drawImage(overlay, x, y, null);
535524
g2d.dispose();
536-
return composed;
525+
result.setId(image.getId() + "|" + overlay.getId());
526+
return result;
537527
}
538528

539529
/**
@@ -605,6 +595,7 @@ public static GamaImage cropped(final IScope scope, final GamaImage image, final
605595
Graphics g = result.getGraphics();
606596
g.drawImage(image, 0, 0, width, height, x, y, x + width, y + height, null);
607597
g.dispose();
598+
result.setId(image.getId() + "crop" + ox + "|" + oy + "|" + ow + "|" + oh);
608599
return result;
609600
}
610601

0 commit comments

Comments
 (0)