Bug fix from K. Kakima
[zxing.git] / core / src / com / google / zxing / qrcode / detector / DefaultGridSampler.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.qrcode.detector;
18
19 import com.google.zxing.MonochromeBitmapSource;
20 import com.google.zxing.ReaderException;
21 import com.google.zxing.common.BitMatrix;
22
23 /**
24  * @author srowen@google.com (Sean Owen)
25  */
26 public final class DefaultGridSampler extends GridSampler {
27
28   protected BitMatrix sampleGrid(MonochromeBitmapSource image,
29                                  FinderPattern topLeft,
30                                  FinderPattern topRight,
31                                  FinderPattern bottomLeft,
32                                  AlignmentPattern alignmentPattern,
33                                  int dimension) throws ReaderException {
34     float dimMinusThree = (float) dimension - 3.5f;
35     float bottomRightX, bottomRightY;
36     float sourceBottomRightX, sourceBottomRightY;
37     if (alignmentPattern != null) {
38       bottomRightX = alignmentPattern.getX();
39       bottomRightY = alignmentPattern.getY();
40       sourceBottomRightX = sourceBottomRightY = dimMinusThree - 3.0f;
41     } else {
42       // Don't have an alignment pattern, just make up the bottom-right point
43       bottomRightX = (topRight.getX() - topLeft.getX()) + bottomLeft.getX();
44       bottomRightY = (topRight.getY() - topLeft.getY()) + bottomLeft.getY();
45       sourceBottomRightX = sourceBottomRightY = dimMinusThree;
46     }
47
48     PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral(
49         3.5f,
50         3.5f,
51         dimMinusThree,
52         3.5f,
53         sourceBottomRightX,
54         sourceBottomRightY,
55         3.5f,
56         dimMinusThree,
57         topLeft.getX(),
58         topLeft.getY(),
59         topRight.getX(),
60         topRight.getY(),
61         bottomRightX,
62         bottomRightY,
63         bottomLeft.getX(),
64         bottomLeft.getY());
65
66     BitMatrix bits = new BitMatrix(dimension);
67     float[] points = new float[dimension << 1];
68     for (int i = 0; i < dimension; i++) {
69       int max = points.length;
70       float iValue = (float) i + 0.5f;
71       for (int j = 0; j < max; j += 2) {
72         points[j] = (float) (j >> 1) + 0.5f;
73         points[j + 1] = iValue;
74       }
75       transform.transformPoints(points);
76       // Quick check to see if points transformed to something inside the image;
77       // sufficent to check the endpoints
78       checkEndpoint(image, points);
79       for (int j = 0; j < max; j += 2) {
80         if (image.isBlack((int) points[j], (int) points[j + 1])) {
81           // Black(-ish) pixel
82           bits.set(i, j >> 1);
83         }
84       }
85     }
86     return bits;
87   }
88
89 }