5 * Created by Christian Brunschen on 18/05/2008.
6 * Copyright 2008 ZXing authors All rights reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 #include "GridSampler.h"
22 #include "PerspectiveTransform.h"
23 #include "ReaderException.h"
28 GridSampler GridSampler::gridSampler;
30 GridSampler::GridSampler() { }
32 Ref<BitMatrix> GridSampler::sampleGrid(Ref<MonochromeBitmapSource> image,
34 float p1ToX, float p1ToY,
35 float p2ToX, float p2ToY,
36 float p3ToX, float p3ToY,
37 float p4ToX, float p4ToY,
38 float p1FromX, float p1FromY,
39 float p2FromX, float p2FromY,
40 float p3FromX, float p3FromY,
41 float p4FromX, float p4FromY) {
42 Ref<PerspectiveTransform> transform
43 (PerspectiveTransform::quadrilateralToQuadrilateral
44 (p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY,
45 p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY));
47 Ref<BitMatrix> bits(new BitMatrix(dimension));
48 valarray<float> points((const float)0.0f, dimension << 1);
49 for (int i = 0; i < dimension; i++) {
50 int max = points.size();
51 float iValue = (float) i + 0.5f;
52 for (int j = 0; j < max; j+= 2) {
53 points[j] = (float) (j >> 1) + 0.5f;
54 points[j + 1] = iValue;
56 transform->transformPoints(points);
57 checkAndNudgePoints(image, points);
58 for (int j = 0; j < max; j += 2) {
59 if (image->isBlack((int) points[j], (int) points[j + 1])) {
68 void GridSampler::checkAndNudgePoints(Ref<MonochromeBitmapSource> image,
69 valarray<float> &points) {
70 int width = image->getWidth();
71 int height = image->getHeight();
72 // Check and nudge points from start until we see some that are OK:
74 for (size_t offset = 0; offset < points.size() && nudged; offset += 2) {
75 int x = (int) points[offset];
76 int y = (int) points[offset + 1];
77 if (x < -1 || x > width || y < -1 || y > height) {
79 s << "Transformed point out of bounds at " << x << "," << y;
80 throw new ReaderException(s.str().c_str());
84 points[offset] = 0.0f;
86 } else if (x == width) {
87 points[offset] = width - 1;
91 points[offset + 1] = 0.0f;
93 } else if (y == height) {
94 points[offset + 1] = height - 1;
98 // Check and nudge points from end:
100 for (size_t offset = points.size() - 2; offset >= 0 && nudged; offset -= 2) {
101 int x = (int) points[offset];
102 int y = (int) points[offset + 1];
103 if (x < -1 || x > width || y < -1 || y > height) {
105 s << "Transformed point out of bounds at " << x << "," << y;
106 throw new ReaderException(s.str().c_str());
110 points[offset] = 0.0f;
112 } else if (x == width) {
113 points[offset] = width - 1;
117 points[offset + 1] = 0.0f;
119 } else if (y == height) {
120 points[offset + 1] = height - 1;
126 GridSampler &GridSampler::getInstance() {