Image() is slow, are there any alternatives?

It’s in my post! It’s also here - PGraphics For some reason not included on the Processing image() docs page.

Possibly due to differences in scaling algorithm - have a look at different settings of smooth() - smooth() / Reference / Processing.org

1 Like

Hi @KevinWho,

As far as appearance goes, I usually set the textureSampling of the P2D renderer. I don’t know if it’s in the documentation, so I usually look here. You can also look at rendering hints. Here’s an example of working with pixel art.

import processing.opengl.*;

int point = 2;
int linear = 3;
int bilinear = 4;
int trilinear = 5;

PImage img;

void setup() {
  size(512, 512, P2D);
  PGraphicsOpenGL rndr = (PGraphicsOpenGL)getGraphics();
  rndr.textureSampling(point);
  rndr.hint(DISABLE_TEXTURE_MIPMAPS);

  // 256 x 128
  img = loadImage("https://upload.wikimedia.org/wikipedia/" + 
     "commons/e/e9/2xsai_example.png");
}

void draw() {
  image(img, 0, 0, width, height); // replace with 9 param
}

So a single renderer gives you improved frame rate, but a buffer does not?

Best,
Jeremy

I am not exactly sure what you are asking, but it does seem that if I set the size() to
size(800,800,P2D); instead of size(800,800); it runs faster (about 2 times faster). also the code that you gave me there, I tried adapting it and it says that these give nullpointers. rndr.textureSampling(point); rndr.hint(DISABLE_TEXTURE_MIPMAPS); not sure what they do.
what does this stuff below do?

  PGraphicsOpenGL rndr = (PGraphicsOpenGL)getGraphics();
  rndr.textureSampling(point);
  rndr.hint(DISABLE_TEXTURE_MIPMAPS);

also after adding that to my code, and messing with some settings in size(), a lot of things are giving errors even after I deleted them. I’m getting nullpoints, and a few divbyzero errors, that don’t make sense. all I did was put size(800, 800, OPENGL); and all this happened, I have since put it back to how it was, and after restarted processing, I still get the same errors.

I keep getting that its returning null with this one function:

PImage animateOffset(PImage[] imgs, int speed, int offset) {
  int returnNumb = ((millis() + offset) / speed) % imgs.length;
  return imgs[returnNumb];
}

It has always worked, and no speed is not 0, and imgs has stuff in it.
Even this print statement is giving me crashing and giving me nulls now.

  println(returnNumb, imgs.length);

I am confused why when I did nothing, and then came back to the program an hour later it worked with no changes, when before it would crash every time it ran.

Hi @KevinWho,

It is difficult to address these issues with the screenshots and code snippets you have provided. In the mean time, general advice:

  1. When you encounter a red band error which stops your program, but you feel it hasn’t given you enough information, click on the bug in the upper right hand corner, to the left of the label that says “Java”. This will make the error message more informative, although more overwhelming. There is a video by Dan Shiffman about debugging in Processing.

  1. Try to strip away complexity when you encounter a problem. Simplify. Open a new Processing sketch and type in code that is new to you until you feel you understand it; then integrate it into a complex project.

  2. Keep a separate copy of a complex project that is known to be working; work on a separate experimental copy. A more systematic, though complicated to learn, process for doing this is called source control.

Many of the questions you’ve raised are related to fundamental programming concepts. I’ll rephrase them as I interpret them. Try searching on Processing for the answers, and/or on the web; they will give you better answers than I can. First, it might help you to organize your questions into a numbered list according to priority.

  • What is a graphics renderer?
  • What is the difference between a primitive and an object?
  • How does inheritance work in object oriented programming?
  • What is OpenGL?
  • What is a pointer?
  • How does pass by reference vs. pass by value work?
  • What is mutability?
  • What is a mip-map?
  • How does a renderer sample a texture?

Feel free to correct me if I’ve misinterpreted the topics you’ve raised. You may know a lot about some of these topics in isolation, for example, I’m sure you’ve defined a class before, but even so, research might help you turn islands into peninsulas into continents. Making video games is hard, even in a dedicated game engine, which Processing is not.

Best,
Jeremy

2 Likes

ok, I will take a look at that when I have time. For now my error is gone, so ill do that if it comes back.