FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

re-implement get/set handling (issue 925) · bchenault/processing@2d1501a · GitHub

Repository navigation

Commit 2d1501a

Browse files
committed
re-implement get/set handling (issue 925)
1 parent 76a12cc commit 2d1501a

3 files changed

Lines changed: 85 additions & 46 deletions

File tree

‎core/src/processing/core/PGraphicsJava2D.java‎

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,17 +2145,32 @@ public int get(int x, int y) {
21452145

21462146

21472147
@Override
2148-
public PImage getImpl(int x, int y, int w, int h) {
2149-
PImage output = new PImage(w, h);
2150-
output.parent = parent;
2151-
2152-
// oops, the last parameter is the scan size of the *target* buffer
2148+
// public PImage getImpl(int x, int y, int w, int h) {
2149+
protected void getImpl(int sourceX, int sourceY,
2150+
int sourceWidth, int sourceHeight,
2151+
PImage target, int targetX, int targetY) {
2152+
// last parameter to getRGB() is the scan size of the *target* buffer
21532153
//((BufferedImage) image).getRGB(x, y, w, h, output.pixels, 0, w);
2154-
WritableRaster raster = ((BufferedImage) (primarySurface ? offscreen : image)).getRaster();
2155-
// WritableRaster raster = image.getRaster();
2156-
raster.getDataElements(x, y, w, h, output.pixels);
2154+
WritableRaster raster =
2155+
((BufferedImage) (primarySurface ? offscreen : image)).getRaster();
21572156

2158-
return output;
2157+
if (sourceWidth == target.width && sourceHeight == target.height) {
2158+
raster.getDataElements(sourceX, sourceY, sourceWidth, sourceHeight, target.pixels);
2159+
2160+
} else {
2161+
// TODO optimize, incredibly inefficient to reallocate this much memory
2162+
int[] temp = new int[sourceWidth * sourceHeight];
2163+
raster.getDataElements(sourceX, sourceY, sourceWidth, sourceHeight, temp);
2164+
2165+
// Copy the temporary output pixels over to the outgoing image
2166+
int sourceOffset = 0;
2167+
int targetOffset = targetY*target.width + targetX;
2168+
for (int y = 0; y < sourceHeight; y++) {
2169+
System.arraycopy(temp, sourceOffset, target.pixels, targetOffset, sourceWidth);
2170+
sourceOffset += sourceWidth;
2171+
targetOffset += target.width;
2172+
}
2173+
}
21592174
}
21602175

21612176

@@ -2176,17 +2191,27 @@ public void set(int x, int y, int argb) {
21762191
}
21772192

21782193

2194+
2195+
// protected void setImpl(int dx, int dy, int sx, int sy, int sw, int sh,
2196+
// PImage src) {
21792197
@Override
2180-
protected void setImpl(int dx, int dy, int sx, int sy, int sw, int sh,
2181-
PImage src) {
2182-
WritableRaster raster = ((BufferedImage) (primarySurface ? offscreen : image)).getRaster();
2183-
// WritableRaster raster = image.getRaster();
2184-
if ((sx == 0) && (sy == 0) && (sw == src.width) && (sh == src.height)) {
2185-
raster.setDataElements(dx, dy, src.width, src.height, src.pixels);
2198+
protected void setImpl(PImage sourceImage,
2199+
int sourceX, int sourceY,
2200+
int sourceWidth, int sourceHeight,
2201+
int targetX, int targetY) {
2202+
WritableRaster raster =
2203+
((BufferedImage) (primarySurface ? offscreen : image)).getRaster();
2204+
2205+
if ((sourceX == 0) && (sourceY == 0) &&
2206+
(sourceWidth == sourceImage.width) &&
2207+
(sourceHeight == sourceImage.height)) {
2208+
raster.setDataElements(targetX, targetY,
2209+
sourceImage.width, sourceImage.height,
2210+
sourceImage.pixels);
21862211
} else {
2187-
// TODO Optimize, incredibly inefficient to reallocate this much memory
2188-
PImage temp = src.get(sx, sy, sw, sh);
2189-
raster.setDataElements(dx, dy, temp.width, temp.height, temp.pixels);
2212+
// TODO optimize, incredibly inefficient to reallocate this much memory
2213+
PImage temp = sourceImage.get(sourceX, sourceY, sourceWidth, sourceHeight);
2214+
raster.setDataElements(targetX, targetY, temp.width, temp.height, temp.pixels);
21902215
}
21912216
}
21922217

‎core/src/processing/core/PImage.java‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,17 @@ public int get(int x, int y) {
767767
* @param h height of pixel rectangle to get
768768
*/
769769
public PImage get(int x, int y, int w, int h) {
770+
int targetX = 0;
771+
int targetY = 0;
772+
770773
if (x < 0) {
771774
w += x; // clip off the left edge
775+
targetX = -x;
772776
x = 0;
773777
}
774778
if (y < 0) {
775779
h += y; // clip off some of the height
780+
targetY = -y;
776781
y = 0;
777782
}
778783

@@ -786,7 +791,10 @@ public PImage get(int x, int y, int w, int h) {
786791
h = 0;
787792
}
788793

789-
return getImpl(x, y, w, h);
794+
PImage target = new PImage(w, h, format);
795+
target.parent = parent;
796+
getImpl(x, y, w, h, target, targetX, targetY);
797+
return target;
790798
}
791799

792800

@@ -796,18 +804,16 @@ public PImage get(int x, int y, int w, int h) {
796804
* are guaranteed to be inside the image space, so the implementation can
797805
* use the fastest possible pixel copying method.
798806
*/
799-
protected PImage getImpl(int x, int y, int w, int h) {
800-
PImage newbie = new PImage(w, h, format);
801-
newbie.parent = parent;
802-
803-
int index = y*width + x;
804-
int index2 = 0;
805-
for (int row = y; row < y+h; row++) {
806-
System.arraycopy(pixels, index, newbie.pixels, index2, w);
807-
index += width;
808-
index2 += w;
807+
protected void getImpl(int sourceX, int sourceY,
808+
int sourceWidth, int sourceHeight,
809+
PImage target, int targetX, int targetY) {
810+
int sourceIndex = sourceY*width + sourceX;
811+
int targetIndex = targetY*target.width + targetX;
812+
for (int row = sourceY; row < sourceY+sourceHeight; row++) {
813+
System.arraycopy(pixels, sourceIndex, target.pixels, targetIndex, sourceWidth);
814+
sourceIndex += width;
815+
targetIndex += target.width;
809816
}
810-
return newbie;
811817
}
812818

813819

@@ -897,25 +903,27 @@ public void set(int x, int y, PImage img) {
897903
// this could be nonexistant
898904
if ((sw <= 0) || (sh <= 0)) return;
899905

900-
setImpl(x, y, sx, sy, sw, sh, img);
906+
setImpl(img, sx, sy, sw, sh, x, y);
901907
}
902908

903909

904910
/**
905911
* Internal function to actually handle setting a block of pixels that
906912
* has already been properly cropped from the image to a valid region.
907913
*/
908-
protected void setImpl(int dx, int dy, int sx, int sy, int sw, int sh,
909-
PImage img) {
910-
int srcOffset = sy * img.width + sx;
911-
int dstOffset = dy * width + dx;
912-
913-
for (int y = sy; y < sy + sh; y++) {
914-
System.arraycopy(img.pixels, srcOffset, pixels, dstOffset, sw);
915-
srcOffset += img.width;
916-
dstOffset += width;
914+
protected void setImpl(PImage sourceImage,
915+
int sourceX, int sourceY,
916+
int sourceWidth, int sourceHeight,
917+
int targetX, int targetY) {
918+
int sourceOffset = sourceY * sourceImage.width + sourceX;
919+
int targetOffset = targetY * width + targetX;
920+
921+
for (int y = sourceY; y < sourceY + sourceHeight; y++) {
922+
System.arraycopy(sourceImage.pixels, sourceOffset, pixels, targetOffset, sourceWidth);
923+
sourceOffset += sourceImage.width;
924+
targetOffset += width;
917925
}
918-
updatePixelsImpl(sx, sy, sw, sh);
926+
updatePixelsImpl(sourceX, sourceY, sourceWidth, sourceHeight);
919927
}
920928

921929

‎core/src/processing/opengl/PGraphicsOpenGL.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5239,10 +5239,13 @@ public int get(int x, int y) {
52395239

52405240

52415241
@Override
5242-
protected PImage getImpl(int x, int y, int w, int h) {
5242+
protected void getImpl(int sourceX, int sourceY,
5243+
int sourceWidth, int sourceHeight,
5244+
PImage target, int targetX, int targetY) {
52435245
loadPixels();
52445246
setgetPixels = true;
5245-
return super.getImpl(x, y, w, h);
5247+
super.getImpl(sourceX, sourceY, sourceWidth, sourceHeight,
5248+
target, targetX, targetY);
52465249
}
52475250

52485251

@@ -5255,11 +5258,14 @@ public void set(int x, int y, int argb) {
52555258

52565259

52575260
@Override
5258-
protected void setImpl(int dx, int dy, int sx, int sy, int sw, int sh,
5259-
PImage src) {
5261+
protected void setImpl(PImage sourceImage,
5262+
int sourceX, int sourceY,
5263+
int sourceWidth, int sourceHeight,
5264+
int targetX, int targetY) {
52605265
loadPixels();
52615266
setgetPixels = true;
5262-
super.setImpl(dx, dy, sx, sy, sw, sh, src);
5267+
super.setImpl(sourceImage, sourceX, sourceY, sourceWidth, sourceHeight,
5268+
targetX, targetY);
52635269
}
52645270

52655271

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL