Changed the order of the BaseMonochromeBitmapSource constructor arguments to be width...
[zxing.git] / javase / src / com / google / zxing / client / j2se / BufferedImageMonochromeBitmapSource.java
index 8e27609..5c6d75b 100644 (file)
@@ -33,19 +33,14 @@ import java.awt.image.BufferedImageOp;
  * based on a region of a {@link BufferedImage}; see
  * {@link #BufferedImageMonochromeBitmapSource(BufferedImage, int, int, int, int)}.</p>
  *
- * @author srowen@google.com (Sean Owen), Daniel Switkin (dswitkin@google.com)
+ * @author Sean Owen
+ * @author Daniel Switkin (dswitkin@google.com)
  */
 public final class BufferedImageMonochromeBitmapSource extends BaseMonochromeBitmapSource {
 
   private final BufferedImage image;
   private final int left;
   private final int top;
-  private final int width;
-  private final int height;
-  private int[] rgbRow;
-  private int[] rgbColumn;
-  private int cachedRow;
-  private int cachedColumn;
 
   /**
    * Creates an instance that uses the entire given image as a source of pixels to decode.
@@ -54,8 +49,6 @@ public final class BufferedImageMonochromeBitmapSource extends BaseMonochromeBit
    */
   public BufferedImageMonochromeBitmapSource(BufferedImage image) {
     this(image, 0, 0, image.getWidth(), image.getHeight());
-    rgbRow = new int[image.getWidth()];
-    cachedRow = -1;
   }
 
   /**
@@ -64,43 +57,34 @@ public final class BufferedImageMonochromeBitmapSource extends BaseMonochromeBit
    * @param image image to decode a region of
    * @param left x coordinate of leftmost pixels to decode
    * @param top y coordinate of topmost pixels to decode
-   * @param right one more than the x coordinate of rightmost pixels to decode. That is, we will decode
+   * @param right one more than the x coordinate of rightmost pixels to decode, i.e. we will decode
    *  pixels whose x coordinate is in [left,right)
    * @param bottom likewise, one more than the y coordinate of the bottommost pixels to decode
    */
-  public BufferedImageMonochromeBitmapSource(BufferedImage image, int left, int top, int right, int bottom) {
+  public BufferedImageMonochromeBitmapSource(BufferedImage image, int left, int top, int right,
+      int bottom) {
+    super(right - left, bottom - top);
     this.image = image;
     int sourceHeight = image.getHeight();
     int sourceWidth = image.getWidth();
-    if (left < 0 || top < 0 || right > sourceWidth || bottom > sourceHeight || right <= left || bottom <= top) {
-      throw new IllegalArgumentException("Invalid bounds: (" + top + ',' + left + ") (" + right + ',' + bottom + ')');
+    if (left < 0 || top < 0 || right > sourceWidth || bottom > sourceHeight || right <= left ||
+        bottom <= top) {
+      throw new IllegalArgumentException("Invalid bounds: (" + top + ',' + left + ") (" + right +
+          ',' + bottom + ')');
     }
     this.left = left;
     this.top = top;
-    this.width = right - left;
-    this.height = bottom - top;
-    rgbRow = new int[width];
-    rgbColumn = new int[height];
-    cachedRow = -1;
-    cachedColumn = -1;
   }
 
   /**
    * @return underlying {@link BufferedImage} behind this instance. Note that even if this instance
-   *  only uses a subset of the full image, the returned value here represents the entire backing image.
+   *  only uses a subset of the full image, the returned value here represents the entire backing
+   *  image.
    */
   public BufferedImage getImage() {
     return image;
   }
 
-  public int getHeight() {
-    return height;
-  }
-
-  public int getWidth() {
-    return width;
-  }
-
   @Override
   public MonochromeBitmapSource rotateCounterClockwise() {
     if (!isRotateSupported()) {
@@ -108,16 +92,18 @@ public final class BufferedImageMonochromeBitmapSource extends BaseMonochromeBit
     }
     int sourceWidth = image.getWidth();
     int sourceHeight = image.getHeight();
+
     // 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);
     return new BufferedImageMonochromeBitmapSource(rotatedImage,
                                                    top,
-                                                   sourceWidth - (left + width),
-                                                   top + height,
+                                                   sourceWidth - (left + getWidth()),
+                                                   top + getHeight(),
                                                    sourceWidth - left);
   }
 
@@ -136,34 +122,45 @@ public final class BufferedImageMonochromeBitmapSource extends BaseMonochromeBit
    *
    * where R, G, and B are values in [0,1].
    */
-  public int getLuminance(int x, int y) {
-    int pixel;
-    if (cachedRow == y) {
-      pixel = rgbRow[x];
-    } else if (cachedColumn == x) {
-      pixel = rgbColumn[y];
-    } else {
-      pixel = image.getRGB(left + x, top + y);
-    }
-
+  @Override
+  protected int getLuminance(int x, int y) {
+    int pixel = image.getRGB(left + x, top + y);
     // Coefficients add up to 1024 to make the divide into a fast shift
     return (306 * ((pixel >> 16) & 0xFF) +
         601 * ((pixel >> 8) & 0xFF) +
         117 * (pixel & 0xFF)) >> 10;
   }
 
-  public void cacheRowForLuminance(int y) {
-    if (y != cachedRow) {
-      image.getRGB(left, top + y, width, 1, rgbRow, 0, width);
-      cachedRow = y;
+  @Override
+  protected int[] getLuminanceRow(int y, int[] row) {
+    int width = getWidth();
+    if (row == null || row.length < width) {
+      row = new int[width];
     }
+    image.getRGB(left, top + y, width, 1, row, 0, width);
+    for (int x = 0; x < width; x++) {
+      int pixel = row[x];
+      row[x] = (306 * ((pixel >> 16) & 0xFF) +
+          601 * ((pixel >> 8) & 0xFF) +
+          117 * (pixel & 0xFF)) >> 10;
+    }
+    return row;
   }
 
-  public void cacheColumnForLuminance(int x) {
-    if (x != cachedColumn) {
-      image.getRGB(left + x, top, 1, height, rgbColumn, 0, 1);
-      cachedColumn = x;
+  @Override
+  protected int[] getLuminanceColumn(int x, int[] column) {
+    int height = getHeight();
+    if (column == null || column.length < height) {
+      column = new int[height];
+    }
+    image.getRGB(left + x, top, 1, height, column, 0, 1);
+    for (int y = 0; y < height; y++) {
+      int pixel = column[y];
+      column[y] = (306 * ((pixel >> 16) & 0xFF) +
+          601 * ((pixel >> 8) & 0xFF) +
+          117 * (pixel & 0xFF)) >> 10;
     }
+    return column;
   }
 
 }