# Change resampling method when resizing

**URL:** <https://discourse.processing.org/t/change-resampling-method-when-resizing/6440>\
**Category:** Coding Questions\
**Created:** [December 9, 2018, 1:04pm UTC](https://discourse.processing.org/t/change-resampling-method-when-resizing/6440 "2018-12-09T13:04:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 9, 2018, 1:04pm UTC](https://discourse.processing.org/t/change-resampling-method-when-resizing/6440/1 "2018-12-09T13:04:38Z")

</div>

Hi,

Is there a way to change the resampling method when resizing a PGraphics object?

I would like to have the nearest-neighbor interpolation in the following code:

```auto
void setup() {
  size(512, 512);
  
  PGraphics pg = createGraphics(50, 50);
  
  pg.beginDraw();
  pg.background(20);
  pg.noStroke();
  pg.fill(200);
  pg.ellipse(25, 25, 25, 25);
  pg.endDraw();
  
  image(pg, 0, 0, width, height);
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [December 9, 2018, 1:52pm UTC](https://discourse.processing.org/t/change-resampling-method-when-resizing/6440/2 "2018-12-09T13:52:33Z")

</div>

Dunno. You can roll your own?

```auto
void setup() {
  size(512, 512);

  PGraphics pg = createGraphics(50, 50);

  pg.beginDraw();
  pg.background(20);
  pg.noStroke();
  pg.fill(200);
  pg.ellipse(25, 25, 25, 25);
  pg.endDraw();

  loadPixels();
  for ( int x = 0; x < width; x++) {
    for ( int y = 0; y < height; y++) {
      pixels[x + width*y] = pg.get( floor(x * pg.width/float(width)), floor(y * pg.height/float(height)) );
    }
  }
  updatePixels();
}

void draw() {
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 9, 2018, 1:59pm UTC](https://discourse.processing.org/t/change-resampling-method-when-resizing/6440/3 "2018-12-09T13:59:54Z")

</div>

Thanks @TfGuy44,

It is of course a solution but I was thinking something more general. Like for example beeing able to switch between the most common algorithms:

- Nearest-neighbor interpolation
- Bilinear algorithms
- Bicubic algorithms
- And maybe some more.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 9, 2018, 3:39pm UTC](https://discourse.processing.org/t/change-resampling-method-when-resizing/6440/4 "2018-12-09T15:39:48Z")

</div>

[Processing.org/reference/smooth\_.html](http://Processing.org/reference/smooth_.html)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 13, 2018, 6:46pm UTC](https://discourse.processing.org/t/change-resampling-method-when-resizing/6440/5 "2018-12-13T18:46:35Z")

</div>

If you are looking to play with options beyond the OpenGL smooth sampling rate in `smooth()` and want to try rolling your own, there is also a demo on the old forum of [FXAA](https://en.wikipedia.org/wiki/Fast_approximate_anti-aliasing) (Fast\_approximate\_anti-aliasing) implemented as a PShader:

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/comment/52576/#Comment_52576)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

If you get really into it you could make it a library. My first thought is that pixel-aesthetic games might be one example of a use case where someone might want fine control over upscaling or downscaling effects.
