Move GridSampler into common package and refactor to ready it for use with Data Matrix
[zxing.git] / core / src / com / google / zxing / common / GridSampler.java
1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.common;
18
19 import com.google.zxing.MonochromeBitmapSource;
20 import com.google.zxing.ReaderException;
21
22 /**
23  * Implementations of this class can, given locations of finder patterns for a QR code in an
24  * image, sample the right points in the image to reconstruct the QR code, accounting for
25  * perspective distortion. It is abstracted since it is relatively expensive and should be allowed
26  * to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
27  * Imaging library, but which may not be available in other environments such as J2ME, and vice
28  * versa.
29  *
30  * The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}
31  * with an instance of a class which implements this interface.
32  *
33  * @author srowen@google.com (Sean Owen)
34  */
35 public abstract class GridSampler {
36
37   private static GridSampler gridSampler;
38
39   /**
40    * Sets the implementation of {@link GridSampler} used by the library. One global
41    * instance is stored, which may sound problematic. But, the implementation provided
42    * ought to be appropriate for the entire platform, and all uses of this library
43    * in the whole lifetime of the JVM. For instance, an Android activity can swap in
44    * an implementation that takes advantage of native platform libraries.
45    * 
46    * @param newGridSampler
47    */
48   public static void setGridSampler(GridSampler newGridSampler) {
49     if (newGridSampler == null) {
50       throw new IllegalArgumentException();
51     }
52     gridSampler = newGridSampler;
53   }
54
55   /**
56    * @return the current implementation of {@link GridSampler}
57    */
58   public static GridSampler getInstance() {
59     if (gridSampler == null) {
60       gridSampler = new DefaultGridSampler();
61     }
62     return gridSampler;
63   }
64
65   /**
66    * <p>Samples an image for a square matrix of bits of the given dimension. This is used to extract the
67    * black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode may be
68    * rotated or perspective-distorted, the caller supplies four points in the source image that define
69    * known points in the barcode, so that the image may be sampled appropriately.</p>
70    *
71    * <p>The last eight "from" parameters are four X/Y coordinate pairs of locations of points in
72    * the image that define some significant points in the image to be sample. For example,
73    * these may be the location of finder pattern in a QR Code.</p>
74    *
75    * <p>The first eight "to" parameters are four X/Y coordinate pairs measured in the destination
76    * {@link BitMatrix}, from the top left, where the known points in the image given by the "from" parameters
77    * map to.</p>
78    *
79    * <p>These 16 parameters define the transformation needed to sample the image.</p>
80    *
81    * @param image image to sample
82    * @param dimension width/height of {@link BitMatrix} to sample from iamge
83    * @return {@link BitMatrix} representing a grid of points sampled from the image within a region
84    *  defined by the "from" parameters
85    * @throws ReaderException if image can't be sampled, for example, if the transformation defined by
86    *  the given points is invalid or results in sampling outside the image boundaries
87    */
88   public abstract BitMatrix sampleGrid(MonochromeBitmapSource image,
89                                        int dimension,
90                                        float p1ToX, float p1ToY,
91                                        float p2ToX, float p2ToY,
92                                        float p3ToX, float p3ToY,
93                                        float p4ToX, float p4ToY,
94                                        float p1FromX, float p1FromY,
95                                        float p2FromX, float p2FromY,
96                                        float p3FromX, float p3FromY,
97                                        float p4FromX, float p4FromY) throws ReaderException;
98
99   /**
100    * <p>Checks a set of points that have been transformed to sample points on an image against
101    * the image's dimensions to see if the endpoints are even within the image.
102    * This method actually only checks the endpoints since the points are assumed to lie
103    * on a line.</p>
104    *
105    * <p>This method will actually "nudge" the endpoints back onto the image if they are found to be barely
106    * (less than 1 pixel) off the image. This accounts for imperfect detection of finder patterns in an image
107    * where the QR Code runs all the way to the image border.</p>
108    *
109    * @param image image into which the points should map
110    * @param points actual points in x1,y1,...,xn,yn form
111    * @throws ReaderException if an endpoint is lies outside the image boundaries
112    */
113   protected static void checkEndpoint(MonochromeBitmapSource image, float[] points) throws ReaderException {
114     int width = image.getWidth();
115     int height = image.getHeight();
116     checkOneEndpoint(points, (int) points[0], (int) points[1], width, height);
117     checkOneEndpoint(points, (int) points[points.length - 2], (int) points[points.length - 1], width, height);
118   }
119
120   private static void checkOneEndpoint(float[] points, int x, int y, int width, int height) throws ReaderException {
121     if (x < -1 || x > width || y < -1 || y > height) {
122       throw new ReaderException("Transformed point out of bounds at " + x + ',' + y);
123     }
124     if (x == -1) {
125       points[0] = 0.0f;
126     }
127     if (y == -1) {
128       points[1] = 0.0f;
129     }
130     if (x == width) {
131       points[0] = width - 1;
132     }
133     if (y == height) {
134       points[1] = height - 1;
135     }
136   }
137
138 }