Skip to content

Commit cff152d

Browse files
committed
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
1 parent 6a2cf8c commit cff152d

4 files changed

Lines changed: 48 additions & 6 deletions

File tree

core/src/processing/awt/ShimAWT.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ static public Object getNativeImage(PImage img) {
237237
}
238238

239239

240-
static public void resizeImage(PImage img, int w, int h) { // ignore
240+
static public void resizeImage(PImage img, int w, int h,int interpolationMode) { // ignore
241241
if (w <= 0 && h <= 0) {
242242
throw new IllegalArgumentException("width or height must be > 0 for resize");
243243
}
@@ -251,7 +251,7 @@ static public void resizeImage(PImage img, int w, int h) { // ignore
251251
}
252252

253253
BufferedImage bimg =
254-
shrinkImage((BufferedImage) img.getNative(), w*img.pixelDensity, h*img.pixelDensity);
254+
shrinkImage((BufferedImage) img.getNative(), w*img.pixelDensity, h*img.pixelDensity,interpolationMode);
255255

256256
PImage temp = new PImageAWT(bimg);
257257
img.pixelWidth = temp.width;
@@ -274,7 +274,8 @@ static public void resizeImage(PImage img, int w, int h) { // ignore
274274
// plus a fix to deal with an infinite loop if images are expanded.
275275
// https://github.com/processing/processing/issues/1501
276276
static private BufferedImage shrinkImage(BufferedImage img,
277-
int targetWidth, int targetHeight) {
277+
int targetWidth, int targetHeight,
278+
int interpolationMode) {
278279
int type = (img.getTransparency() == Transparency.OPAQUE) ?
279280
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
280281
BufferedImage outgoing = img;
@@ -313,8 +314,15 @@ static private BufferedImage shrinkImage(BufferedImage img,
313314
scratchImage = new BufferedImage(w, h, type);
314315
g2 = scratchImage.createGraphics();
315316
}
317+
//convert the passed int value of interpolationMode to the object expected by setRenderingHint
318+
Object interpolationModeValue = switch(interpolationMode){
319+
case 0 -> RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
320+
//case 1 is the same as the default
321+
case 2 -> RenderingHints.VALUE_INTERPOLATION_BICUBIC;
322+
default -> RenderingHints.VALUE_INTERPOLATION_BILINEAR;
323+
};
316324
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
317-
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
325+
interpolationModeValue);
318326
g2.drawImage(outgoing, 0, 0, w, h, 0, 0, prevW, prevH, null);
319327
prevW = w;
320328
prevH = h;

core/src/processing/core/PConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,11 @@ public interface PConstants {
483483
int WAIT = Cursor.WAIT_CURSOR;
484484

485485

486+
//image interpolation modes
487+
int NEAREST_NEIGHBOR = 0;
488+
int BILINEAR = 1;
489+
int BICUBIC = 2;
490+
486491
// hints - hint values are positive for the alternate version,
487492
// negative of the same value returns to the normal/default state
488493

core/src/processing/core/PImage.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,37 @@ public Object clone() throws CloneNotSupportedException { // ignore
497497
* @usage web_application
498498
* @param w the resized image width
499499
* @param h the resized image height
500+
* @param interpolationMode the type of interpolation that should be used when resizing the image
500501
* @see PImage#get(int, int, int, int)
501502
*/
502-
public void resize(int w, int h) { // ignore
503+
public void resize(int w, int h,int interpolationMode) { // ignore
503504
//throw new RuntimeException("resize() not implemented for this PImage type");
504-
ShimAWT.resizeImage(this, w, h);
505+
ShimAWT.resizeImage(this, w, h,interpolationMode);
506+
}
507+
508+
/**
509+
*
510+
* Resize the image to a new width and height. To make the image scale
511+
* proportionally, use 0 as the value for the <b>wide</b> or <b>high</b>
512+
* parameter. For instance, to make the width of an image 150 pixels, and
513+
* change the height using the same proportion, use <b>resize(150, 0)</b>.<br />
514+
* <br />
515+
* Even though a PGraphics is technically a <b>PImage</b>, it is not possible to
516+
* rescale the image data found in a <b>PGraphics</b>. (It's simply not possible
517+
* to do this consistently across renderers: technically infeasible with
518+
* P3D, or what would it even do with PDF?) If you want to resize <b>PGraphics</b>
519+
* content, first get a copy of its image data using the <b>get()</b>
520+
* method, and call <b>resize()</b> on the PImage that is returned.
521+
*
522+
* @webref pimage:method
523+
* @webBrief Resize the image to a new width and height
524+
* @usage web_application
525+
* @param w the resized image width
526+
* @param h the resized image height
527+
* @see PImage#get(int, int, int, int)
528+
*/
529+
public void resize(int w, int h) { // ignore
530+
resize(w,h,1);
505531
}
506532

507533

java/keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ ARGB LITERAL2
2323
BACKSPACE LITERAL2 keyCode
2424
BASELINE LITERAL2 textAlign_
2525
BEVEL LITERAL2 strokeJoin_
26+
BICUBIC LITERAL2
27+
BILINEAR LITERAL2
2628
BLEND LITERAL2 blend_
2729
BLUE_MASK LITERAL2
2830
BLUR LITERAL2 filter_
@@ -116,6 +118,7 @@ MITER LITERAL2 stokeJoin_
116118
MODEL LITERAL2 textMode_
117119
MOVE LITERAL2 cursor_
118120
MULTIPLY LITERAL2 blend_
121+
NEAREST_NEIGHBOR LITERAL2
119122
NORMAL LITERAL2
120123
NORMALIZED LITERAL2 textureMode_
121124
NO_DEPTH_TEST LITERAL2

0 commit comments

Comments
 (0)