Undo earlier change that rejects TYPE_CUSTOM -- too many images parse this way, and...
[zxing.git] / javase / src / com / google / zxing / client / j2se / BufferedImageMonochromeBitmapSource.java
index 479865c..5030d7b 100644 (file)
 
 package com.google.zxing.client.j2se;
 
-import com.google.zxing.MonochromeBitmapSource;
 import com.google.zxing.BlackPointEstimationMethod;
+import com.google.zxing.MonochromeBitmapSource;
 import com.google.zxing.common.BitArray;
 import com.google.zxing.common.BlackPointEstimator;
 
+import java.awt.geom.AffineTransform;
+import java.awt.image.AffineTransformOp;
 import java.awt.image.BufferedImage;
+import java.awt.image.BufferedImageOp;
 
 /**
  * <p>An implementation based upon {@link BufferedImage}. This provides access to the
  * underlying image as if it were a monochrome image. Behind the scenes, it is evaluating
  * the luminance of the underlying image by retrieving its pixels' RGB values.</p>
- * 
+ *
  * @author srowen@google.com (Sean Owen), Daniel Switkin (dswitkin@google.com)
  */
 public final class BufferedImageMonochromeBitmapSource implements MonochromeBitmapSource {
@@ -35,10 +38,17 @@ public final class BufferedImageMonochromeBitmapSource implements MonochromeBitm
   private final BufferedImage image;
   private int blackPoint;
   private BlackPointEstimationMethod lastMethod;
+  private int lastArgument;
+
+  private static final int LUMINANCE_BITS = 5;
+  private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
+  private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
 
   public BufferedImageMonochromeBitmapSource(BufferedImage image) {
     this.image = image;
     blackPoint = 0x7F;
+    lastMethod = null;
+    lastArgument = 0;
   }
 
   public boolean isBlack(int x, int y) {
@@ -69,32 +79,59 @@ public final class BufferedImageMonochromeBitmapSource implements MonochromeBitm
   }
 
   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
-    if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
-      if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(lastMethod)) {
-        int width = image.getWidth();
-        int height = image.getHeight();
-        int[] luminanceBuckets = new int[32];
+    if (!method.equals(lastMethod) || argument != lastArgument) {
+      int width = image.getWidth();
+      int height = image.getHeight();
+      int[] histogram = new int[LUMINANCE_BUCKETS];
+      float biasTowardsWhite = 1.0f;
+      if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
         int minDimension = width < height ? width : height;
         int startI = height == minDimension ? 0 : (height - width) >> 1;
         int startJ = width == minDimension ? 0 : (width - height) >> 1;
         for (int n = 0; n < minDimension; n++) {
           int pixel = image.getRGB(startJ + n, startI + n);
-          luminanceBuckets[computeRGBLuminance(pixel) >> 3]++;
+          histogram[computeRGBLuminance(pixel) >> LUMINANCE_SHIFT]++;
+        }
+      } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
+        if (argument < 0 || argument >= height) {
+          throw new IllegalArgumentException("Row is not within the image: " + argument);
+        }
+        biasTowardsWhite = 2.0f;
+        int[] rgbArray = new int[width];
+        image.getRGB(0, argument, width, 1, rgbArray, 0, width);
+        for (int x = 0; x < width; x++) {
+          histogram[computeRGBLuminance(rgbArray[x]) >> LUMINANCE_SHIFT]++;
         }
-        blackPoint = BlackPointEstimator.estimate(luminanceBuckets) << 3;
+      } else {
+        throw new IllegalArgumentException("Unknown method: " + method);
       }
-    } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
-      // TODO
-    } else {
-      throw new IllegalArgumentException("Unknown method: " + method);
+      blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
+      lastMethod = method;
+      lastArgument = argument;
     }
-    lastMethod = method;
   }
 
   public BlackPointEstimationMethod getLastEstimationMethod() {
     return lastMethod;
   }
 
+  public MonochromeBitmapSource rotateCounterClockwise() {
+    if (!isRotateSupported()) {
+      throw new IllegalStateException("Rotate not supported");
+    }
+    // 90 degrees counterclockwise:
+    AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, image.getHeight());
+    BufferedImageOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
+    BufferedImage rotatedImage = new BufferedImage(image.getHeight(), image.getWidth(), image.getType());
+    op.filter(image, rotatedImage);
+    return new BufferedImageMonochromeBitmapSource(rotatedImage);
+  }
+
+  public boolean isRotateSupported() {
+    // Can't run AffineTransforms on images of unknown format
+    return image.getType() != BufferedImage.TYPE_CUSTOM;
+  }
+
   /**
    * Extracts luminance from a pixel from this source. By default, the source is assumed to use RGB,
    * so this implementation computes luminance is a function of a red, green and blue components as