We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexSuggestions & BugsSoftware Bugs › PImage.copy() bug
Page Index Toggle Pages: 1
PImage.copy() bug (Read 913 times)
PImage.copy() bug
Jun 20th, 2005, 12:36am
 
I've tried to check the forums for any other reference to this apparent bug but haven't found anything, so here goes...

I'm trying to write a themeable GUI class and need to stretch areas of images to expand the GUI controls as required. (I'll post a demo to the forums soon.)

The bug that I think I've found is that when I use PImage.copy() to construct a new image by copying an area of an existing image, the copy does not work correctly -- too much of the source image is included in the copied area leading to an incorrect display. The following code illustrates the sort of situation I'm dealing with, I've tried to make the problem as obvious as possible so I've drawn a red box around the source and the destination areas involved in the copying.

Code:

size(1000, 400);
noFill();
stroke(255, 0, 0, 64);

// Set top and bottom border used to calculate area to copy
int top = 10;
int bottom = 10;

// Initialise image to copy from
PImage img1 = loadImage("slider_vert_normal.png");
// Calculate the height of the middle section to copy from
int middle1 = img1.height - (top + bottom);
// Draw image to screen with red rectangles around area to be copied from
image(img1, 0, 0, img1.width, img1.height);
rect(0, top, img1.width, middle1);

translate(img1.width + 8, 0);

for (int height2 = img1.height; height2 < img1.height * 3; height2 += 3) {
// Initialise image to copy into
PImage img2 = new PImage(img1.width, height2);
// Calculate the height of the middle section to copy into
int middle2 = img2.height - (top + bottom);
// Copy middle section from img1 to img2
img2.copy(img1, 0, top, img1.width, middle1, 0, top, img2.width, middle2);
// Draw images to screen with red rectangle around copied area
image(img2, 0, 0, img2.width, img2.height);
rect(0, top, img2.width, middle2);
// Translate the display ready for next copy
translate(img2.width + 4, 0);
}


The bug can be clearly seen when the applet is run:

http://homepage.mac.com/rob.saunders/copytest1/

Interestingly, the copying error appears even when the source and the destination areas are the same size. It is also interesting to note that the copying error does not seem to occur when scaling horizontally, as can be seen in the following applet:

http://homepage.mac.com/rob.saunders/copytest2/

To my uninformed eye, it seems that there is some sort of rounding error in the vertical scaling calculations.

Can anyone advise how I might get around this problem until a bug fix is released Does the size of the region I'm trying to copy have an effect

Any help would be greatly appreciated,

-RoB-
Re: PImage.copy() bug
Reply #1 - Jun 20th, 2005, 4:15pm
 
OK. So I think I've tracked down the bug. I've had a look at the source code for PImage via the CVS viewer at Sourceforge. The following code is the first few lines of code for PImage.blit_resize:

Code:

if (srcX1 < 0) srcX1 = 0;
if (srcY1 < 0) srcY1 = 0;
if (srcX2 >= img.width) srcX2 = img.width - 1;
if (srcY2 >= img.width) srcY2 = img.height - 1;


The last of these checks is (obviously) incorrect, and should be changed to:

Code:

if (srcY2 >= img.height) srcY2 = img.height - 1;


Hope that helps someone fix the next release.

-RoB-
Re: PImage.copy() bug
Reply #2 - Jun 21st, 2005, 4:25pm
 
yeehaa nothing like people finding and fixing bugs for me. i've made the change for 92 (and in cvs) so it'll be all set to go. i'm surprised this hadn't been found earlier.

thanks!
Re: PImage.copy() bug
Reply #3 - Jun 21st, 2005, 6:26pm
 
ben, i don't want to scare you, but i'm not sure if that is re-appearance of an old bug which i thought to have had fixed last year (can't back up my claim for lack of a old local copy, though)...

anyway, there's another issue with the overloaded PImage.blend() and copy() methods using 2 images. i think there's some (wrong) tweaking of coordinates going on, e.g.:
Code:
public void blend(PImage src,
int sx1, int sy1, int sx2, int sy2,
int dx1, int dy1, int dx2, int dy2, int mode) {
if (imageMode == CORNER) { // if CORNERS, do nothing
sx2 += sx1; sy2 += sy1;
dx2 += dx1; dy2 += dy1;
...
<snip>

this check and the adjustments look wrong to me! but again, can't remember if i actually did put that in or someone else...

hth!
Re: PImage.copy() bug
Reply #4 - Jun 21st, 2005, 7:26pm
 
nah, that code should be ok (unless i'm missing something). since the function expects x1/y1/x2/y2 coords, i added that code to properly respond to the imageMode. so when imageMode is CORNER, the x2 and y2 passed in are gonna be width/height, meaning that the real x2 is x1 + width (or x2, as labeled by the function).
Page Index Toggle Pages: 1