Issue 563: Support non-rectangular Data Matrix
[zxing.git] / core / src / com / google / zxing / common / DefaultGridSampler.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  * @author Sean Owen
23  */
24 public final class DefaultGridSampler extends GridSampler {
25
26   public BitMatrix sampleGrid(BitMatrix image,
27                               int dimension,
28                               float p1ToX, float p1ToY,
29                               float p2ToX, float p2ToY,
30                               float p3ToX, float p3ToY,
31                               float p4ToX, float p4ToY,
32                               float p1FromX, float p1FromY,
33                               float p2FromX, float p2FromY,
34                               float p3FromX, float p3FromY,
35                               float p4FromX, float p4FromY) throws NotFoundException {
36
37     PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral(
38         p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY,
39         p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);
40
41     return sampleGrid(image, dimension, dimension, transform);
42   }
43   
44   public BitMatrix sampleGrid(BitMatrix image,
45           int dimensionX,
46           int dimensionY,
47           float p1ToX, float p1ToY,
48           float p2ToX, float p2ToY,
49           float p3ToX, float p3ToY,
50           float p4ToX, float p4ToY,
51           float p1FromX, float p1FromY,
52           float p2FromX, float p2FromY,
53           float p3FromX, float p3FromY,
54           float p4FromX, float p4FromY) throws NotFoundException {
55
56 PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral(
57 p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY,
58 p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);
59
60 return sampleGrid(image, dimensionX, dimensionY, transform);
61 }
62
63   public BitMatrix sampleGrid(BitMatrix image,
64           int dimensionX, int dimensionY,
65           PerspectiveTransform transform) throws NotFoundException {
66 BitMatrix bits = new BitMatrix(dimensionX, dimensionY);
67 float[] points = new float[dimensionX << 1];
68 for (int y = 0; y < dimensionY; y++) {
69 int max = points.length;
70 float iValue = (float) y + 0.5f;
71 for (int x = 0; x < max; x += 2) {
72 points[x] = (float) (x >> 1) + 0.5f;
73 points[x + 1] = iValue;
74 }
75 transform.transformPoints(points);
76 // Quick check to see if points transformed to something inside the image;
77 // sufficient to check the endpoints
78 checkAndNudgePoints(image, points);
79 try {
80 for (int x = 0; x < max; x += 2) {
81 if (image.get((int) points[x], (int) points[x + 1])) {
82 // Black(-ish) pixel
83 bits.set(x >> 1, y);
84 }
85 }
86 } catch (ArrayIndexOutOfBoundsException aioobe) {
87 // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
88 // transform gets "twisted" such that it maps a straight line of points to a set of points
89 // whose endpoints are in bounds, but others are not. There is probably some mathematical
90 // way to detect this about the transformation that I don't know yet.
91 // This results in an ugly runtime exception despite our clever checks above -- can't have
92 // that. We could check each point's coordinates but that feels duplicative. We settle for
93 // catching and wrapping ArrayIndexOutOfBoundsException.
94 throw NotFoundException.getNotFoundInstance();
95 }
96 }
97 return bits;
98 }
99   
100 }