git-svn-id: http://zxing.googlecode.com/svn/trunk@2 59b500cc-1b3d-0410-9834-0bbf25fbcc57
[zxing.git] / 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 bottomRightX;
35     float bottomRightY;
36     if (alignmentPattern != null) {
37       bottomRightX = alignmentPattern.getX();
38       bottomRightY = alignmentPattern.getY();
39     } else {
40       // Don't have an alignment pattern, just make up the bottom-right point
41       bottomRightX = (topRight.getX() - topLeft.getX()) + bottomLeft.getX();
42       bottomRightY = (topRight.getY() - topLeft.getY()) + bottomLeft.getY();
43     }
44
45     float dimMinusThree = (float) dimension - 3.5f;
46     JAIPerspectiveTransform transform = JAIPerspectiveTransform.getQuadToQuad(
47         3.5f,
48         3.5f,
49         dimMinusThree,
50         3.5f,
51         3.5f,
52         dimMinusThree,
53         dimMinusThree - 3.0f,
54         dimMinusThree - 3.0f,
55         topLeft.getX(),
56         topLeft.getY(),
57         topRight.getX(),
58         topRight.getY(),
59         bottomLeft.getX(),
60         bottomLeft.getY(),
61         bottomRightX,
62         bottomRightY);
63
64     BitMatrix bits = new BitMatrix(dimension);
65     float[] points = new float[dimension << 1];
66     for (int i = 0; i < dimension; i++) {
67       int max = points.length;
68       float iValue = (float) i + 0.5f;
69       for (int j = 0; j < max; j += 2) {
70         points[j] = (float) j + 0.5f;
71         points[j + 1] = iValue;
72       }
73       transform.transform(points);
74       // Quick check to see if points transformed to something inside the image;
75       // sufficent to check the endpoints
76       checkEndpoint(image, points);
77       for (int j = 0; j < dimension; j++) {
78         int offset = j << 1;
79         if (image.isBlack((int) points[offset], (int) points[offset + 1])) {
80           // Black(-ish) pixel
81           bits.set(i, j);
82         }
83       }
84     }
85     return bits;
86   }
87
88 }