Issue 563: Support non-rectangular Data Matrix
[zxing.git] / core / src / com / google / zxing / common / GridSampler.java
index 4cf2776..1a79b93 100644 (file)
@@ -16,8 +16,7 @@
 
 package com.google.zxing.common;
 
-import com.google.zxing.MonochromeBitmapSource;
-import com.google.zxing.ReaderException;
+import com.google.zxing.NotFoundException;
 
 /**
  * Implementations of this class can, given locations of finder patterns for a QR code in an
@@ -34,7 +33,7 @@ import com.google.zxing.ReaderException;
  */
 public abstract class GridSampler {
 
-  private static GridSampler gridSampler = null;
+  private static GridSampler gridSampler = new DefaultGridSampler();
 
   /**
    * Sets the implementation of {@link GridSampler} used by the library. One global
@@ -43,7 +42,7 @@ public abstract class GridSampler {
    * in the whole lifetime of the JVM. For instance, an Android activity can swap in
    * an implementation that takes advantage of native platform libraries.
    * 
-   * @param newGridSampler
+   * @param newGridSampler The platform-specific object to install.
    */
   public static void setGridSampler(GridSampler newGridSampler) {
     if (newGridSampler == null) {
@@ -56,11 +55,6 @@ public abstract class GridSampler {
    * @return the current implementation of {@link GridSampler}
    */
   public static GridSampler getInstance() {
-    // No real point in trying to make this thread-safe;
-    // doesn't matter if a second instance is created
-    if (gridSampler == null) {
-      gridSampler = new DefaultGridSampler();
-    }
     return gridSampler;
   }
 
@@ -81,13 +75,13 @@ public abstract class GridSampler {
    * <p>These 16 parameters define the transformation needed to sample the image.</p>
    *
    * @param image image to sample
-   * @param dimension width/height of {@link BitMatrix} to sample from iamge
+   * @param dimension width/height of {@link BitMatrix} to sample from image
    * @return {@link BitMatrix} representing a grid of points sampled from the image within a region
    *   defined by the "from" parameters
-   * @throws ReaderException if image can't be sampled, for example, if the transformation defined
+   * @throws NotFoundException if image can't be sampled, for example, if the transformation defined
    *   by the given points is invalid or results in sampling outside the image boundaries
    */
-  public abstract BitMatrix sampleGrid(MonochromeBitmapSource image,
+  public abstract BitMatrix sampleGrid(BitMatrix image,
                                        int dimension,
                                        float p1ToX, float p1ToY,
                                        float p2ToX, float p2ToY,
@@ -96,7 +90,36 @@ public abstract class GridSampler {
                                        float p1FromX, float p1FromY,
                                        float p2FromX, float p2FromY,
                                        float p3FromX, float p3FromY,
-                                       float p4FromX, float p4FromY) throws ReaderException;
+                                       float p4FromX, float p4FromY) throws NotFoundException;
+
+  /**
+   * Samples an image for a rectangular matrix of bits of the given dimension.
+   * @param image image to sample
+   * @param dimensionX width of {@link BitMatrix} to sample from image
+   * @param dimensionY height of {@link BitMatrix} to sample from image
+   * @return {@link BitMatrix} representing a grid of points sampled from the image within a region
+   *   defined by the "from" parameters
+   * @throws NotFoundException if image can't be sampled, for example, if the transformation defined
+   *   by the given points is invalid or results in sampling outside the image boundaries
+   */
+  public abstract BitMatrix sampleGrid(BitMatrix image,
+          int dimensionX,
+          int dimensionY,
+          float p1ToX, float p1ToY,
+          float p2ToX, float p2ToY,
+          float p3ToX, float p3ToY,
+          float p4ToX, float p4ToY,
+          float p1FromX, float p1FromY,
+          float p2FromX, float p2FromY,
+          float p3FromX, float p3FromY,
+          float p4FromX, float p4FromY) throws NotFoundException;
+  
+  public BitMatrix sampleGrid(BitMatrix image,
+                              int dimension,
+                              PerspectiveTransform transform) throws NotFoundException {
+    throw new IllegalStateException(); // Can't use UnsupportedOperationException
+  }
+  
 
   /**
    * <p>Checks a set of points that have been transformed to sample points on an image against
@@ -111,10 +134,10 @@ public abstract class GridSampler {
    *
    * @param image image into which the points should map
    * @param points actual points in x1,y1,...,xn,yn form
-   * @throws ReaderException if an endpoint is lies outside the image boundaries
+   * @throws NotFoundException if an endpoint is lies outside the image boundaries
    */
-  protected static void checkAndNudgePoints(MonochromeBitmapSource image, float[] points)
-      throws ReaderException {
+  protected static void checkAndNudgePoints(BitMatrix image, float[] points)
+      throws NotFoundException {
     int width = image.getWidth();
     int height = image.getHeight();
     // Check and nudge points from start until we see some that are OK:
@@ -123,7 +146,7 @@ public abstract class GridSampler {
       int x = (int) points[offset];
       int y = (int) points[offset + 1];
       if (x < -1 || x > width || y < -1 || y > height) {
-        throw ReaderException.getInstance();
+        throw NotFoundException.getNotFoundInstance();
       }
       nudged = false;
       if (x == -1) {
@@ -147,7 +170,7 @@ public abstract class GridSampler {
       int x = (int) points[offset];
       int y = (int) points[offset + 1];
       if (x < -1 || x > width || y < -1 || y > height) {
-        throw ReaderException.getInstance();
+        throw NotFoundException.getNotFoundInstance();
       }
       nudged = false;
       if (x == -1) {