Workaround (I think) for bizarre array corruption problem on Sun WTK and some SE...
[zxing.git] / javame / src / com / google / zxing / client / j2me / LCDUIImageMonochromeBitmapSource.java
index 3b76266..04d0ca3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2007 Google Inc.
+ * Copyright 2007 ZXing authors
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package com.google.zxing.client.j2me;
 
-import com.google.zxing.MonochromeBitmapSource;
-import com.google.zxing.common.BitArray;
-import com.google.zxing.common.BlackPointEstimator;
+import com.google.zxing.common.BaseMonochromeBitmapSource;
 
 import javax.microedition.lcdui.Image;
 
 /**
  * <p>An implementation based on Java ME's {@link Image} representation.</p>
- * 
- * @author Sean Owen (srowen@google.com)
+ *
+ * @author Sean Owen (srowen@google.com), Daniel Switkin (dswitkin@google.com)
  */
-final class LCDUIImageMonochromeBitmapSource implements MonochromeBitmapSource {
+public final class LCDUIImageMonochromeBitmapSource extends BaseMonochromeBitmapSource {
 
-  private final int[] rgbPixels;
-  private final int blackPoint;
-  private final int width;
+  private final Image image;
   private final int height;
+  private final int width;
+  // For why this isn't final, see below
+  private int[] rgbRow;
+  private final int[] pixelHolder;
+  private int cachedRow;
 
-  LCDUIImageMonochromeBitmapSource(final Image image) {
-    int width = image.getWidth();
-    int height = image.getHeight();
-    this.width = width;
-    this.height = height;
-    int[] rgbPixels = new int[width * height];
-    this.rgbPixels = rgbPixels;
-    image.getRGB(rgbPixels, 0, width, 0, 0, width, height);
-    int[] luminanceBuckets = new int[32];
-    int minDimension = width < height ? width : height;
-    for (int n = 0, offset = 0; n < minDimension; n++, offset += width + 1) {
-      luminanceBuckets[computeRGBLuminance(rgbPixels[offset]) >> 3]++;
-    }
-    blackPoint = BlackPointEstimator.estimate(luminanceBuckets) << 3;
-  }
-
-  public boolean isBlack(int x, int y) {
-    return computeRGBLuminance(rgbPixels[x + y * width]) < blackPoint;
-  }
-
-  public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
-    if (row == null) {
-      row = new BitArray(getWidth);
-    } else {
-      row.clear();
-    }
-    for (int i = 0, offset = y * width + startX; i < getWidth; i++, offset++) {
-      if (computeRGBLuminance(rgbPixels[offset]) < blackPoint) {
-        row.set(i);
-      }
-    }
-    return row;
+  public LCDUIImageMonochromeBitmapSource(Image image) {
+    this.image = image;
+    height = image.getHeight();
+    width = image.getWidth();
+    rgbRow = new int[width];
+    pixelHolder = new int[1];
+    cachedRow = -1;
   }
 
   public int getHeight() {
@@ -76,20 +52,46 @@ final class LCDUIImageMonochromeBitmapSource implements MonochromeBitmapSource {
     return width;
   }
 
-  /**
-   * 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
-   * follows:
-   *
-   * <code>Y = 0.299R + 0.587G + 0.114B</code>
-   *
-   * where R, G, and B are values in [0,1].
-   */
-  private static int computeRGBLuminance(int pixel) {
-    // 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 int getLuminance(int x, int y) {
+
+    // Below, why the check for rgbRow being the right size? it should never change size
+    // or need to be reallocated. But bizarrely we have seen a but on Sun's WTK, and on
+    // some phones, where the array becomes zero-sized somehow. So we keep making sure the
+    // array is OK.
+    int pixel;
+    if (cachedRow == y && rgbRow.length == width) {
+      pixel = rgbRow[x];
+    } else {
+      image.getRGB(pixelHolder, 0, width, x, y, 1, 1);
+      pixel = pixelHolder[0];
+    }
+
+    // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
+    // the multiplies can be implemented as shifts.
+    //
+    // Really, it's:
+    //
+    // return ((((pixel >> 16) & 0xFF) << 8) +
+    //         (((pixel >>  8) & 0xFF) << 9) +
+    //         (( pixel        & 0xFF) << 8)) >> 10;
+    //
+    // That is, we're replacing the coefficients in the original with powers of two,
+    // which can be implemented as shifts, even though changing the coefficients slightly
+    // corrupts the conversion. Not significant for our purposes.
+    return (((pixel & 0x00FF0000) >> 16) +
+            ((pixel & 0x0000FF00) >>  7) +
+             (pixel & 0x000000FF       )) >> 2;
+  }
+
+  public void cacheRowForLuminance(int y) {
+    if (y != cachedRow) {
+      // See explanation above
+      if (rgbRow.length != width) {
+        rgbRow = new int[width];
+      }
+      image.getRGB(rgbRow, 0, width, 0, y, width, 1);
+      cachedRow = y;
+    }
   }
 
 }
\ No newline at end of file