[libbluray-devel] BDGraphics: avoid copying image in drawImageN()

Ian Curtis git at videolan.org
Thu Apr 3 11:27:52 CEST 2014


libbluray | branch: master | Ian Curtis <i.curtis at gmail.com> | Wed Apr  2 15:58:36 2014 +0300| [dea19e704063039aff7dce85c7132a47b26293b9] | committer: hpi1

BDGraphics: avoid copying image in drawImageN()

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=dea19e704063039aff7dce85c7132a47b26293b9
---

 .../bdj/java/java/awt/BDGraphicsBase.java          |   54 +++++++++-----------
 1 file changed, 24 insertions(+), 30 deletions(-)

diff --git a/src/libbluray/bdj/java/java/awt/BDGraphicsBase.java b/src/libbluray/bdj/java/java/awt/BDGraphicsBase.java
index 1668064..31a091d 100644
--- a/src/libbluray/bdj/java/java/awt/BDGraphicsBase.java
+++ b/src/libbluray/bdj/java/java/awt/BDGraphicsBase.java
@@ -1001,50 +1001,44 @@ abstract class BDGraphicsBase extends Graphics2D implements ConstrainableGraphic
             }
         }
 
+        if (sw < 0) sw = bdImage.width;
+        if (sh < 0) sh = bdImage.height;
+        if (dw < 0) dw = bdImage.width;
+        if (dh < 0) dh = bdImage.height;
+
+        int   stride   = bdImage.width;
+        int[] rgbArray = bdImage.getBdBackBuffer();
         int   bgColor  = 0;
 
         if (bg != null) {
             bgColor = bg.getRGB();
         }
 
-        if(sx + sw > bdImage.width || sy + sh > bdImage.height) {
-            logger.info("drawImageN: fixing too small src image (src " + sx + "," + sy + " " + sw + "x" + sh + " ; img " + bdImage.width + "x" + bdImage.height + ")");
-
-            BDImage subImage = new BDImage(null, sw, sh, null);
-            bdImage.getRGB(sx, sy, Math.min(sw, bdImage.width - sx), Math.min(sh, bdImage.height - sy), subImage.getBdBackBuffer(), 0, sw);
-            bdImage = subImage;
-            sx = 0;
-            sy = 0;
-        }
-
-        if ((sw > 0) && (sh > 0) &&
-            ((sx != 0) || (sy != 0) || (sw != bdImage.width) || (sh != bdImage.height))) {
-            BDImage subImage = new BDImage(null, sw, sh, null);
-            bdImage.getRGB(sx, sy, sw, sh, subImage.getBdBackBuffer(), 0, sw);
-            bdImage = subImage;
-        }
-        if ((dw > 0) && (dh > 0) &&
-            ((dw != bdImage.width) || (dh != bdImage.height))) {
+        // resize if needed
+        if (dw != sw || dh != sh) {
             BDImageConsumer scaledImage = new BDImageConsumer(null);
             AreaAveragingScaleFilter scaleFilter =
                 new AreaAveragingScaleFilter(dw, dh);
             scaleFilter = (AreaAveragingScaleFilter)scaleFilter.getFilterInstance(scaledImage);
-            scaleFilter.setDimensions(bdImage.width, bdImage.height);
-            scaleFilter.setPixels(0, 0, bdImage.width, bdImage.height,
-                                  bdImage.getColorModel(), bdImage.getBdBackBuffer(),
-                                  0, bdImage.width);
+            scaleFilter.setDimensions(sw, sh);
+            scaleFilter.setPixels(0, 0, sw, sh,
+                                  bdImage.getColorModel(), rgbArray,
+                                  sx + sy * bdImage.width, bdImage.width);
             scaleFilter.imageComplete(ImageConsumer.STATICIMAGEDONE);
-            bdImage = scaledImage;
+            rgbArray = scaledImage.getBdBackBuffer();
+            sx = 0;
+            sy = 0;
+            stride = dw;
         }
-        int[] rgbArray = bdImage.getBdBackBuffer();
 
-        for (int y = dy; y < (dy + bdImage.height); y++) {
-
-            if (bg != null) {
-                drawSpan(dx, y, bdImage.width, bgColor);
-            }
+        // draw background colour
+        for (int i = 0; i < dh && bg != null; i++) {
+            drawSpan(dx, dy + i, dw, bgColor);
+        }
 
-            drawSpan(dx, y, bdImage.width, rgbArray, (y - dy) * bdImage.width);
+        // draw actual colour array
+        for (int i = 0; i < dh; i++) {
+            drawSpan(dx, dy + i, dw, rgbArray, (stride * (i + sy)) + sx);
         }
 
         return true;



More information about the libbluray-devel mailing list