Try adding current javadoc to SVN
[zxing.git] / core / src / com / google / zxing / common / GridSampler.java
1 /*
2  * Copyright 2007 ZXing authors
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 Sean Owen
34  */
35 public abstract class GridSampler {
36
37   private static GridSampler gridSampler = new DefaultGridSampler();
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 The platform-specific object to install.
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     return gridSampler;
60   }
61
62   /**
63    * <p>Samples an image for a square matrix of bits of the given dimension. This is used to extract
64    * the black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode
65    * may be rotated or perspective-distorted, the caller supplies four points in the source image
66    * that define known points in the barcode, so that the image may be sampled appropriately.</p>
67    *
68    * <p>The last eight "from" parameters are four X/Y coordinate pairs of locations of points in
69    * the image that define some significant points in the image to be sample. For example,
70    * these may be the location of finder pattern in a QR Code.</p>
71    *
72    * <p>The first eight "to" parameters are four X/Y coordinate pairs measured in the destination
73    * {@link BitMatrix}, from the top left, where the known points in the image given by the "from"
74    * parameters map to.</p>
75    *
76    * <p>These 16 parameters define the transformation needed to sample the image.</p>
77    *
78    * @param image image to sample
79    * @param dimension width/height of {@link BitMatrix} to sample from iamge
80    * @return {@link BitMatrix} representing a grid of points sampled from the image within a region
81    *   defined by the "from" parameters
82    * @throws ReaderException if image can't be sampled, for example, if the transformation defined
83    *   by the given points is invalid or results in sampling outside the image boundaries
84    */
85   public abstract BitMatrix sampleGrid(MonochromeBitmapSource image,
86                                        int dimension,
87                                        float p1ToX, float p1ToY,
88                                        float p2ToX, float p2ToY,
89                                        float p3ToX, float p3ToY,
90                                        float p4ToX, float p4ToY,
91                                        float p1FromX, float p1FromY,
92                                        float p2FromX, float p2FromY,
93                                        float p3FromX, float p3FromY,
94                                        float p4FromX, float p4FromY) throws ReaderException;
95
96   /**
97    * <p>Checks a set of points that have been transformed to sample points on an image against
98    * the image's dimensions to see if the point are even within the image.</p>
99    *
100    * <p>This method will actually "nudge" the endpoints back onto the image if they are found to be
101    * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
102    * patterns in an image where the QR Code runs all the way to the image border.</p>
103    *
104    * <p>For efficiency, the method will check points from either end of the line until one is found
105    * to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
106    *
107    * @param image image into which the points should map
108    * @param points actual points in x1,y1,...,xn,yn form
109    * @throws ReaderException if an endpoint is lies outside the image boundaries
110    */
111   protected static void checkAndNudgePoints(MonochromeBitmapSource image, float[] points)
112       throws ReaderException {
113     int width = image.getWidth();
114     int height = image.getHeight();
115     // Check and nudge points from start until we see some that are OK:
116     boolean nudged = true;
117     for (int offset = 0; offset < points.length && nudged; offset += 2) {
118       int x = (int) points[offset];
119       int y = (int) points[offset + 1];
120       if (x < -1 || x > width || y < -1 || y > height) {
121         throw ReaderException.getInstance();
122       }
123       nudged = false;
124       if (x == -1) {
125         points[offset] = 0.0f;
126         nudged = true;
127       } else if (x == width) {
128         points[offset] = width - 1;
129         nudged = true;
130       }
131       if (y == -1) {
132         points[offset + 1] = 0.0f;
133         nudged = true;
134       } else if (y == height) {
135         points[offset + 1] = height - 1;
136         nudged = true;
137       }
138     }
139     // Check and nudge points from end:
140     nudged = true;
141     for (int offset = points.length - 2; offset >= 0 && nudged; offset -= 2) {
142       int x = (int) points[offset];
143       int y = (int) points[offset + 1];
144       if (x < -1 || x > width || y < -1 || y > height) {
145         throw ReaderException.getInstance();
146       }
147       nudged = false;
148       if (x == -1) {
149         points[offset] = 0.0f;
150         nudged = true;
151       } else if (x == width) {
152         points[offset] = width - 1;
153         nudged = true;
154       }
155       if (y == -1) {
156         points[offset + 1] = 0.0f;
157         nudged = true;
158       } else if (y == height) {
159         points[offset + 1] = height - 1;
160         nudged = true;
161       }
162     }
163   }
164
165 }