From cff152d2ea6e0868da59ab8b31bddc6bdcd9e5ed Mon Sep 17 00:00:00 2001 From: jSdCool <37940266+jSdCool@users.noreply.github.com> Date: Thu, 23 Nov 2023 00:02:54 -0500 Subject: [PATCH 1/2] added the ability to choose witch interpolation mode to use when resizing an image. to do this there is a new overload for PImage.resize() that takes 3 ints(width,height,interpolation mode) there are also 3 new PConstants for the mode they are: NEAREST_NEIGHBOR, BILINEAR, and BICUBIC --- core/src/processing/awt/ShimAWT.java | 16 +++++++++---- core/src/processing/core/PConstants.java | 5 ++++ core/src/processing/core/PImage.java | 30 ++++++++++++++++++++++-- java/keywords.txt | 3 +++ 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/core/src/processing/awt/ShimAWT.java b/core/src/processing/awt/ShimAWT.java index f335296442..118ac0015a 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,7 @@ 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 +274,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 +314,15 @@ 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..8b1416fce4 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 d2ac4e3b99..f6f8f3acf6 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); } diff --git a/java/keywords.txt b/java/keywords.txt index bf02b74d98..8d50dff173 100644 --- a/java/keywords.txt +++ b/java/keywords.txt @@ -23,6 +23,8 @@ ARGB LITERAL2 BACKSPACE LITERAL2 keyCode BASELINE LITERAL2 textAlign_ BEVEL LITERAL2 strokeJoin_ +BICUBIC LITERAL2 +BILINEAR LITERAL2 BLEND LITERAL2 blend_ BLUE_MASK LITERAL2 BLUR LITERAL2 filter_ @@ -116,6 +118,7 @@ MITER LITERAL2 stokeJoin_ MODEL LITERAL2 textMode_ MOVE LITERAL2 cursor_ MULTIPLY LITERAL2 blend_ +NEAREST_NEIGHBOR LITERAL2 NORMAL LITERAL2 NORMALIZED LITERAL2 textureMode_ NO_DEPTH_TEST LITERAL2 From 76e1e6cc4c560aa09da05d3f848a7bc9b641b0d1 Mon Sep 17 00:00:00 2001 From: jSdCool Date: Sat, 14 Dec 2024 11:45:59 -0500 Subject: [PATCH 2/2] fix formatting to match style guide --- core/src/processing/awt/ShimAWT.java | 10 ++++++---- core/src/processing/core/PConstants.java | 2 +- core/src/processing/core/PImage.java | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/core/src/processing/awt/ShimAWT.java b/core/src/processing/awt/ShimAWT.java index 118ac0015a..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,int interpolationMode) { // 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,int interpolationMode) { } BufferedImage bimg = - shrinkImage((BufferedImage) img.getNative(), w*img.pixelDensity, h*img.pixelDensity,interpolationMode); + shrinkImage((BufferedImage) img.getNative(), w*img.pixelDensity, + h*img.pixelDensity, interpolationMode); PImage temp = new PImageAWT(bimg); img.pixelWidth = temp.width; @@ -314,8 +315,9 @@ 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){ + // 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; diff --git a/core/src/processing/core/PConstants.java b/core/src/processing/core/PConstants.java index 8b1416fce4..d21a1fa49d 100644 --- a/core/src/processing/core/PConstants.java +++ b/core/src/processing/core/PConstants.java @@ -483,7 +483,7 @@ public interface PConstants { int WAIT = Cursor.WAIT_CURSOR; - //image interpolation modes + // image interpolation modes int NEAREST_NEIGHBOR = 0; int BILINEAR = 1; int BICUBIC = 2; diff --git a/core/src/processing/core/PImage.java b/core/src/processing/core/PImage.java index f6f8f3acf6..33738de728 100644 --- a/core/src/processing/core/PImage.java +++ b/core/src/processing/core/PImage.java @@ -502,7 +502,7 @@ public Object clone() throws CloneNotSupportedException { // 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,interpolationMode); + ShimAWT.resizeImage(this, w, h, interpolationMode); } /** @@ -527,7 +527,7 @@ public void resize(int w, int h,int interpolationMode) { // ignore * @see PImage#get(int, int, int, int) */ public void resize(int w, int h) { // ignore - resize(w,h,1); + resize(w, h, 1); }