Adjust formatting on last change. Simplify GridSampler
[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.NotFoundException;
20
21 /**
22  * Implementations of this class can, given locations of finder patterns for a QR code in an
23  * image, sample the right points in the image to reconstruct the QR code, accounting for
24  * perspective distortion. It is abstracted since it is relatively expensive and should be allowed
25  * to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
26  * Imaging library, but which may not be available in other environments such as J2ME, and vice
27  * versa.
28  *
29  * The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}
30  * with an instance of a class which implements this interface.
31  *
32  * @author Sean Owen
33  */
34 public abstract class GridSampler {
35
36   private static GridSampler gridSampler = new DefaultGridSampler();
37
38   /**
39    * Sets the implementation of {@link GridSampler} used by the library. One global
40    * instance is stored, which may sound problematic. But, the implementation provided
41    * ought to be appropriate for the entire platform, and all uses of this library
42    * in the whole lifetime of the JVM. For instance, an Android activity can swap in
43    * an implementation that takes advantage of native platform libraries.
44    * 
45    * @param newGridSampler The platform-specific object to install.
46    */
47   public static void setGridSampler(GridSampler newGridSampler) {
48     if (newGridSampler == null) {
49       throw new IllegalArgumentException();
50     }
51     gridSampler = newGridSampler;
52   }
53
54   /**
55    * @return the current implementation of {@link GridSampler}
56    */
57   public static GridSampler getInstance() {
58     return gridSampler;
59   }
60
61   /**
62    * Samples an image for a rectangular matrix of bits of the given dimension.
63    * @param image image to sample
64    * @param dimensionX width of {@link BitMatrix} to sample from image
65    * @param dimensionY height of {@link BitMatrix} to sample from image
66    * @return {@link BitMatrix} representing a grid of points sampled from the image within a region
67    *   defined by the "from" parameters
68    * @throws NotFoundException if image can't be sampled, for example, if the transformation defined
69    *   by the given points is invalid or results in sampling outside the image boundaries
70    */
71   public abstract BitMatrix sampleGrid(BitMatrix image,
72                                        int dimensionX,
73                                        int dimensionY,
74                                        float p1ToX, float p1ToY,
75                                        float p2ToX, float p2ToY,
76                                        float p3ToX, float p3ToY,
77                                        float p4ToX, float p4ToY,
78                                        float p1FromX, float p1FromY,
79                                        float p2FromX, float p2FromY,
80                                        float p3FromX, float p3FromY,
81                                        float p4FromX, float p4FromY) throws NotFoundException;
82   
83   public abstract BitMatrix sampleGrid(BitMatrix image,
84                                        int dimensionX,
85                                        int dimensionY,
86                                        PerspectiveTransform transform) throws NotFoundException;
87
88   /**
89    * <p>Checks a set of points that have been transformed to sample points on an image against
90    * the image's dimensions to see if the point are even within the image.</p>
91    *
92    * <p>This method will actually "nudge" the endpoints back onto the image if they are found to be
93    * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
94    * patterns in an image where the QR Code runs all the way to the image border.</p>
95    *
96    * <p>For efficiency, the method will check points from either end of the line until one is found
97    * to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
98    *
99    * @param image image into which the points should map
100    * @param points actual points in x1,y1,...,xn,yn form
101    * @throws NotFoundException if an endpoint is lies outside the image boundaries
102    */
103   protected static void checkAndNudgePoints(BitMatrix image, float[] points) throws NotFoundException {
104     int width = image.getWidth();
105     int height = image.getHeight();
106     // Check and nudge points from start until we see some that are OK:
107     boolean nudged = true;
108     for (int offset = 0; offset < points.length && nudged; offset += 2) {
109       int x = (int) points[offset];
110       int y = (int) points[offset + 1];
111       if (x < -1 || x > width || y < -1 || y > height) {
112         throw NotFoundException.getNotFoundInstance();
113       }
114       nudged = false;
115       if (x == -1) {
116         points[offset] = 0.0f;
117         nudged = true;
118       } else if (x == width) {
119         points[offset] = width - 1;
120         nudged = true;
121       }
122       if (y == -1) {
123         points[offset + 1] = 0.0f;
124         nudged = true;
125       } else if (y == height) {
126         points[offset + 1] = height - 1;
127         nudged = true;
128       }
129     }
130     // Check and nudge points from end:
131     nudged = true;
132     for (int offset = points.length - 2; offset >= 0 && nudged; offset -= 2) {
133       int x = (int) points[offset];
134       int y = (int) points[offset + 1];
135       if (x < -1 || x > width || y < -1 || y > height) {
136         throw NotFoundException.getNotFoundInstance();
137       }
138       nudged = false;
139       if (x == -1) {
140         points[offset] = 0.0f;
141         nudged = true;
142       } else if (x == width) {
143         points[offset] = width - 1;
144         nudged = true;
145       }
146       if (y == -1) {
147         points[offset + 1] = 0.0f;
148         nudged = true;
149       } else if (y == height) {
150         points[offset + 1] = height - 1;
151         nudged = true;
152       }
153     }
154   }
155
156 }