Added rounding code to getRow() as well and updated the tests accordingly.
[zxing.git] / javase / src / com / google / zxing / client / j2se / BufferedImageLuminanceSource.java
index e60d3e2..1d44564 100644 (file)
@@ -18,9 +18,8 @@ package com.google.zxing.client.j2se;
 
 import com.google.zxing.LuminanceSource;
 
+import java.awt.Graphics2D;
 import java.awt.image.BufferedImage;
-import java.awt.image.BufferedImageOp;
-import java.awt.image.AffineTransformOp;
 import java.awt.geom.AffineTransform;
 
 /**
@@ -75,7 +74,8 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
       int pixel = rgbData[x];
       int luminance = (306 * ((pixel >> 16) & 0xFF) +
           601 * ((pixel >> 8) & 0xFF) +
-          117 * (pixel & 0xFF)) >> 10;
+          117 * (pixel & 0xFF) +
+          (0x200)) >> 10; // 0x200 = 1<<9, half an lsb of the result to force rounding
       row[x] = (byte) luminance;
     }
     return row;
@@ -96,7 +96,8 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
         int pixel = rgb[offset + x];
         int luminance = (306 * ((pixel >> 16) & 0xFF) +
             601 * ((pixel >> 8) & 0xFF) +
-            117 * (pixel & 0xFF)) >> 10;
+            117 * (pixel & 0xFF) +
+            (0x200)) >> 10; // 0x200 = 1<<9, half an lsb of the result to force rounding
         matrix[offset + x] = (byte) luminance;
       }
     }
@@ -129,11 +130,14 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
 
     // Rotate 90 degrees counterclockwise.
     AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
-    BufferedImageOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
 
     // Note width/height are flipped since we are rotating 90 degrees.
     BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, image.getType());
-    op.filter(image, rotatedImage);
+
+    // Draw the original image into rotated, via transformation
+    Graphics2D g = rotatedImage.createGraphics();
+    g.drawImage(image, transform, null);
+    g.dispose();
 
     // Maintain the cropped region, but rotate it too.
     int width = getWidth();