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-