How to rewrite this for faster performance? (image resize)

edited March 2018 in Questions about Code

Is there a faster way to write this? PGraphics buffer is HD, canvas size is SD. If canvas size and PGraphics buffer are the same, the framerate is 60. If they are different, the framerate is 20 meaning there is an insane performance hit to image resize.

image(buffer, 0, 0, width, height);

Alright, this is a bit weird.

With the standard image resize:

Default renderer  = 20fps. 

P3D = 50fps. 

I did an image resize manually by copying the pixels to an array with a for loop, then using a simple nearest neighbor solution, then copying the array into a pimage, then displaying the pimage.

default renderer = 50fps

P3D = 50fps. 

Obviously, my image resize method which is nearest neighbor looks terrible compared to the default one, and it gets the same fps in P3D. The goal however is to export to android, and for some reason P3D is super slow... any advice? Is there any examples resizing a PGraphics canvas with a custom algorithm in a processing sketch?

This is my resize algorithm

  int[] resize(int[] original, int x1, int y1, int x2, int y2) {
    int[] resized = new int[x2*y2];
    double xr = x1/(double)x2;
    double yr = y1/(double)y2;
    double px;
    double py; 
    for (int x = 0; x < x2; x++) {
      px = x * xr;
      for (int y = 0; y < y2; y++) {
        py = y * yr;
        double index = Math.floor((y* x2) + x);
        double index2 = Math.floor((int)((py * x1) + px));
        resized[(int)(index)] = original[(int)index2];
      }
    }
    return resized;
  }
Tagged:

Answers

  • Obviously, my image resize method which is nearest neighbor looks terrible compared to the default one, ...

    PImage.resize() method: https://Processing.org/reference/PImage_resize_.html
    depends on smooth() current quality level: https://Processing.org/reference/smooth_.html

    For default renderer JAVA2D, its default quality is 3, which is bicubic:

    The default renderer uses smooth(3) by default. This is bicubic smoothing. The other option for the default renderer is smooth(2), which is bilinear smoothing.

    For the OPENGL-based renderers P2D & P3D, default anti-aliasing is 2:

    With the P2D and P3D renderers, smooth(2) is the default, this is called "2x anti-aliasing."

  • Also switch those:

    for (int x = 0; x < x2; x++) {
        px = x * xr;
        for (int y = 0; y < y2; y++) {
    

    You want to loop in the y first and then the x.

Sign In or Register to comment.