From 06453b801b9a92158763fd64989f57941c406abf Mon Sep 17 00:00:00 2001 From: jSdCool Date: Sun, 27 Apr 2025 12:57:16 -0400 Subject: [PATCH] Reapply clean changes for PR --- core/src/processing/awt/ShimAWT.java | 18 ++++++++++---- core/src/processing/core/PConstants.java | 5 ++++ core/src/processing/core/PImage.java | 30 ++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/core/src/processing/awt/ShimAWT.java b/core/src/processing/awt/ShimAWT.java index f335296442..901f359bb2 100644 --- a/core/src/processing/awt/ShimAWT.java +++ b/core/src/processing/awt/ShimAWT.java @@ -237,7 +237,7 @@ static public Object getNativeImage(PImage img) { } - static public void resizeImage(PImage img, int w, int h) { // ignore + static public void resizeImage(PImage img, int w, int h, int interpolationMode) { // ignore if (w <= 0 && h <= 0) { throw new IllegalArgumentException("width or height must be > 0 for resize"); } @@ -251,7 +251,8 @@ static public void resizeImage(PImage img, int w, int h) { // ignore } BufferedImage bimg = - shrinkImage((BufferedImage) img.getNative(), w*img.pixelDensity, h*img.pixelDensity); + shrinkImage((BufferedImage) img.getNative(), w*img.pixelDensity, + h*img.pixelDensity, interpolationMode); PImage temp = new PImageAWT(bimg); img.pixelWidth = temp.width; @@ -274,7 +275,8 @@ static public void resizeImage(PImage img, int w, int h) { // ignore // plus a fix to deal with an infinite loop if images are expanded. // https://github.com/processing/processing/issues/1501 static private BufferedImage shrinkImage(BufferedImage img, - int targetWidth, int targetHeight) { + int targetWidth, int targetHeight, + int interpolationMode) { int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage outgoing = img; @@ -313,8 +315,16 @@ static private BufferedImage shrinkImage(BufferedImage img, scratchImage = new BufferedImage(w, h, type); g2 = scratchImage.createGraphics(); } + // convert the passed int value of interpolationMode to the object expected + // by setRenderingHint + Object interpolationModeValue = switch(interpolationMode) { + case 0 -> RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR; + //case 1 is the same as the default + case 2 -> RenderingHints.VALUE_INTERPOLATION_BICUBIC; + default -> RenderingHints.VALUE_INTERPOLATION_BILINEAR; + }; g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, - RenderingHints.VALUE_INTERPOLATION_BILINEAR); + interpolationModeValue); g2.drawImage(outgoing, 0, 0, w, h, 0, 0, prevW, prevH, null); prevW = w; prevH = h; diff --git a/core/src/processing/core/PConstants.java b/core/src/processing/core/PConstants.java index af17c1fbfb..d21a1fa49d 100644 --- a/core/src/processing/core/PConstants.java +++ b/core/src/processing/core/PConstants.java @@ -483,6 +483,11 @@ public interface PConstants { int WAIT = Cursor.WAIT_CURSOR; + // image interpolation modes + int NEAREST_NEIGHBOR = 0; + int BILINEAR = 1; + int BICUBIC = 2; + // hints - hint values are positive for the alternate version, // negative of the same value returns to the normal/default state diff --git a/core/src/processing/core/PImage.java b/core/src/processing/core/PImage.java index 666061ee9d..b982eedd19 100644 --- a/core/src/processing/core/PImage.java +++ b/core/src/processing/core/PImage.java @@ -497,11 +497,37 @@ public Object clone() throws CloneNotSupportedException { // ignore * @usage web_application * @param w the resized image width * @param h the resized image height + * @param interpolationMode the type of interpolation that should be used when resizing the image * @see PImage#get(int, int, int, int) */ - public void resize(int w, int h) { // ignore + public void resize(int w, int h,int interpolationMode) { // ignore //throw new RuntimeException("resize() not implemented for this PImage type"); - ShimAWT.resizeImage(this, w, h); + ShimAWT.resizeImage(this, w, h, interpolationMode); + } + + /** + * + * Resize the image to a new width and height. To make the image scale + * proportionally, use 0 as the value for the wide or high + * parameter. For instance, to make the width of an image 150 pixels, and + * change the height using the same proportion, use resize(150, 0).
+ *
+ * Even though a PGraphics is technically a PImage, it is not possible to + * rescale the image data found in a PGraphics. (It's simply not possible + * to do this consistently across renderers: technically infeasible with + * P3D, or what would it even do with PDF?) If you want to resize PGraphics + * content, first get a copy of its image data using the get() + * method, and call resize() on the PImage that is returned. + * + * @webref pimage:method + * @webBrief Resize the image to a new width and height + * @usage web_application + * @param w the resized image width + * @param h the resized image height + * @see PImage#get(int, int, int, int) + */ + public void resize(int w, int h) { // ignore + resize(w, h, 1); }