We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Has there been any change to the behavior of PImage.get() in Processing 2.1? I have been working with 2.0x and copying and resizing images thusly:
fitImg = img.get();
fitImg.resize(scaledWidth, scaledHeight);
if (null != myFrame) myFrame.setSize(scaledWidth, scaledHeight + 48);
In 2.1, the image was displaying as a blank (white) display. I corrected the problem by replacing img.get()
with:
fitImg = copyImagePixels(img);
where copyImagePixels
is:
public PImage copyImagePixels(PImage image) {
int h = image.height;
int w = image.width;
int i = 0;
PImage newImage = createImage(w, h, ARGB);
newImage.loadPixels();
for (i = 0; i < image.pixels.length; i++) {
newImage.pixels[i] = image.pixels[i];
}
newImage.updatePixels();
return newImage;
}
I'm developing in Eclipse, but the problem also occurs in Processing 2.1, too. It does not occur in Processing 2.0x.
Why would get()
no longer behave the same way? Has the way that alpha channels are copied changed? Or is it something else?
Puzzled, and wondering how much of my code has depended on the previous behavior of get()
and may have to be changed.
cheers,
-- Paul
Answers
Don't know about get()'s behavior in the Eclipse environment, but the following code works perfectly in the Processing environment. Plus the getCopy() function is faster than Processing's get():
Thanks, but your solution is pretty much exactly what I am doing. It clearly has less overhead than get() (https://github.com/processing/processing/blob/master/core/src/processing/core/PImage.java), so that is useful. Maybe the System.arraycopy call will be somewhat optimized compared with my loop, maybe not. In any case, it leaves the fundamental question of how get() behaves in 2.1 unanswered. I'll work up an app that demonstrates the problem. more precisely.
Java's arraycopy() is native and therefore implemented in platform-dependent code (mostly C, C++ or an assembly language) which reduces it's overhead to a minimum. Additionally, instead of copying every single array element, it copys whole memory areas. So, yes, it is faster - roughly twice as fast - depending on your platform.
Yes, a more detailed example would be great. :)
Edit: Does get() work in your Processing 2.1 environment (it works for me)? Just to eliminate JRE/OS based problems.
Processing canonized System.arraycopy() as arrayCopy() FYI. ;;)
http://processing.org/reference/arrayCopy_.html
Okay, here is a test application that demonstrates the problem. It resizes an image to fit the screen. If you use PImage.get in Processing 2.1, the resized image is blank. In 2.0.1 it works. If you use arrayCopy, everything works in both versions. Eclipse behaves the same way, depending on which version of processing.core I am using.
At the very least, there's an inconsistency in the behavior of PImage.get between Processing 2.0.1 (old copy I have) and Processing 2.1. I haven't tested in 1.5.1, but assume it will behave as in 2.0.1.
The app gives you a file dialog to open an image. Once open, you can toggle resizing the window to fit the screen with an 'F' keypress. 'O' will open another file. 'G' will toggle between using PImage.get and arrayCopy, and revela the problem.
Obviously, there are all sorts of good reasons to use arrayCopy, as comments have pointed out. OTOH, inconsistencies of this sort worry me, in case they point other weirdness ("bugs" rather than "features").
Thanks for the comments. I'd be real interested to hear if my problems are replicated.
The code style munges my JavaDoc comments with HTML markup, but I'll leave them in as they may be useful.
In this thread, I see a lot of resize() calls and a lot of problems with transparency, except when an ARGB PImage is created. I believe you may be experiencing the same issue as filed here: https://github.com/processing/processing/issues/2228
For an analysis of the problem see the pull request I have made, which also contains a proposed fix for this issue, here: https://github.com/processing/processing/pull/2312
Thanks, amnon! That makes a lot of sense. I'm glad to see you've proposed a fix.
And in fact, if I write to the image alpha right after resizing the image, the problem goes away. That is, however, just a workaround.
Of course, I can speed the remedy up a little: