1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
// Original code is licensed as follows:
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "xfa/fxbarcode/common/BC_WhiteRectangleDetector.h"
#include <memory>
#include "xfa/fxbarcode/BC_ResultPoint.h"
#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h"
#include "xfa/fxbarcode/utils.h"
CBC_WhiteRectangleDetector::CBC_WhiteRectangleDetector(
CBC_CommonBitMatrix* image) {
m_image = image;
m_height = image->GetHeight();
m_width = image->GetWidth();
m_leftInit = (m_width - INIT_SIZE) >> 1;
m_rightInit = (m_width + INIT_SIZE) >> 1;
m_upInit = (m_height - INIT_SIZE) >> 1;
m_downInit = (m_height + INIT_SIZE) >> 1;
}
void CBC_WhiteRectangleDetector::Init(int32_t& e) {
if (m_upInit < 0 || m_leftInit < 0 || m_downInit >= m_height ||
m_rightInit >= m_width) {
e = BCExceptionNotFound;
BC_EXCEPTION_CHECK_ReturnVoid(e);
}
}
CBC_WhiteRectangleDetector::CBC_WhiteRectangleDetector(
CBC_CommonBitMatrix* image,
int32_t initSize,
int32_t x,
int32_t y) {
m_image = image;
m_height = image->GetHeight();
m_width = image->GetWidth();
int32_t halfsize = initSize >> 1;
m_leftInit = x - halfsize;
m_rightInit = x + halfsize;
m_upInit = y - halfsize;
m_downInit = y + halfsize;
}
CBC_WhiteRectangleDetector::~CBC_WhiteRectangleDetector() {}
CFX_PtrArray* CBC_WhiteRectangleDetector::Detect(int32_t& e) {
int32_t left = m_leftInit;
int32_t right = m_rightInit;
int32_t up = m_upInit;
int32_t down = m_downInit;
FX_BOOL sizeExceeded = FALSE;
FX_BOOL aBlackPointFoundOnBorder = TRUE;
FX_BOOL atLeastOneBlackPointFoundOnBorder = FALSE;
while (aBlackPointFoundOnBorder) {
aBlackPointFoundOnBorder = FALSE;
FX_BOOL rightBorderNotWhite = TRUE;
while (rightBorderNotWhite && right < m_width) {
rightBorderNotWhite = ContainsBlackPoint(up, down, right, FALSE);
if (rightBorderNotWhite) {
right++;
aBlackPointFoundOnBorder = TRUE;
}
}
if (right >= m_width) {
sizeExceeded = TRUE;
break;
}
FX_BOOL bottomBorderNotWhite = TRUE;
while (bottomBorderNotWhite && down < m_height) {
bottomBorderNotWhite = ContainsBlackPoint(left, right, down, TRUE);
if (bottomBorderNotWhite) {
down++;
aBlackPointFoundOnBorder = TRUE;
}
}
if (down >= m_height) {
sizeExceeded = TRUE;
break;
}
FX_BOOL leftBorderNotWhite = TRUE;
while (leftBorderNotWhite && left >= 0) {
leftBorderNotWhite = ContainsBlackPoint(up, down, left, FALSE);
if (leftBorderNotWhite) {
left--;
aBlackPointFoundOnBorder = TRUE;
}
}
if (left < 0) {
sizeExceeded = TRUE;
break;
}
FX_BOOL topBorderNotWhite = TRUE;
while (topBorderNotWhite && up >= 0) {
topBorderNotWhite = ContainsBlackPoint(left, right, up, TRUE);
if (topBorderNotWhite) {
up--;
aBlackPointFoundOnBorder = TRUE;
}
}
if (up < 0) {
sizeExceeded = TRUE;
break;
}
if (aBlackPointFoundOnBorder) {
atLeastOneBlackPointFoundOnBorder = TRUE;
}
}
if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) {
int32_t maxSize = right - left;
std::unique_ptr<CBC_ResultPoint> z;
for (int32_t i = 1; i < maxSize; i++) {
z.reset(GetBlackPointOnSegment((FX_FLOAT)left, (FX_FLOAT)(down - i),
(FX_FLOAT)(left + i), (FX_FLOAT)(down)));
if (z)
break;
}
if (z.get() == NULL) {
e = BCExceptionNotFound;
BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
}
std::unique_ptr<CBC_ResultPoint> t;
for (int32_t j = 1; j < maxSize; j++) {
t.reset(GetBlackPointOnSegment((FX_FLOAT)left, (FX_FLOAT)(up + j),
(FX_FLOAT)(left + j), (FX_FLOAT)up));
if (t)
break;
}
if (t.get() == NULL) {
e = BCExceptionNotFound;
BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
}
std::unique_ptr<CBC_ResultPoint> x;
for (int32_t k = 1; k < maxSize; k++) {
x.reset(GetBlackPointOnSegment((FX_FLOAT)right, (FX_FLOAT)(up + k),
(FX_FLOAT)(right - k), (FX_FLOAT)up));
if (x)
break;
}
if (x.get() == NULL) {
e = BCExceptionNotFound;
BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
}
std::unique_ptr<CBC_ResultPoint> y;
for (int32_t m = 1; m < maxSize; m++) {
y.reset(GetBlackPointOnSegment((FX_FLOAT)right, (FX_FLOAT)(down - m),
(FX_FLOAT)(right - m), (FX_FLOAT)down));
if (y)
break;
}
if (y.get() == NULL) {
e = BCExceptionNotFound;
BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
}
return CenterEdges(y.get(), z.get(), x.get(), t.get());
} else {
e = BCExceptionNotFound;
BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
}
return NULL;
}
int32_t CBC_WhiteRectangleDetector::Round(FX_FLOAT d) {
return (int32_t)(d + 0.5f);
}
CBC_ResultPoint* CBC_WhiteRectangleDetector::GetBlackPointOnSegment(
FX_FLOAT aX,
FX_FLOAT aY,
FX_FLOAT bX,
FX_FLOAT bY) {
int32_t dist = DistanceL2(aX, aY, bX, bY);
float xStep = (bX - aX) / dist;
float yStep = (bY - aY) / dist;
for (int32_t i = 0; i < dist; i++) {
int32_t x = Round(aX + i * xStep);
int32_t y = Round(aY + i * yStep);
if (m_image->Get(x, y)) {
return new CBC_ResultPoint((FX_FLOAT)x, (FX_FLOAT)y);
}
}
return NULL;
}
int32_t CBC_WhiteRectangleDetector::DistanceL2(FX_FLOAT aX,
FX_FLOAT aY,
FX_FLOAT bX,
FX_FLOAT bY) {
float xDiff = aX - bX;
float yDiff = aY - bY;
return Round((float)sqrt(xDiff * xDiff + yDiff * yDiff));
}
CFX_PtrArray* CBC_WhiteRectangleDetector::CenterEdges(CBC_ResultPoint* y,
CBC_ResultPoint* z,
CBC_ResultPoint* x,
CBC_ResultPoint* t) {
float yi = y->GetX();
float yj = y->GetY();
float zi = z->GetX();
float zj = z->GetY();
float xi = x->GetX();
float xj = x->GetY();
float ti = t->GetX();
float tj = t->GetY();
if (yi < m_width / 2) {
CFX_PtrArray* result = new CFX_PtrArray;
result->SetSize(4);
(*result)[0] = new CBC_ResultPoint(ti - CORR, tj + CORR);
(*result)[1] = new CBC_ResultPoint(zi + CORR, zj + CORR);
(*result)[2] = new CBC_ResultPoint(xi - CORR, xj - CORR);
(*result)[3] = new CBC_ResultPoint(yi + CORR, yj - CORR);
return result;
} else {
CFX_PtrArray* result = new CFX_PtrArray;
result->SetSize(4);
(*result)[0] = new CBC_ResultPoint(ti + CORR, tj + CORR);
(*result)[1] = new CBC_ResultPoint(zi + CORR, zj - CORR);
(*result)[2] = new CBC_ResultPoint(xi - CORR, xj + CORR);
(*result)[3] = new CBC_ResultPoint(yi - CORR, yj - CORR);
return result;
}
}
FX_BOOL CBC_WhiteRectangleDetector::ContainsBlackPoint(int32_t a,
int32_t b,
int32_t fixed,
FX_BOOL horizontal) {
if (horizontal) {
for (int32_t x = a; x <= b; x++) {
if (m_image->Get(x, fixed)) {
return TRUE;
}
}
} else {
for (int32_t y = a; y <= b; y++) {
if (m_image->Get(fixed, y)) {
return TRUE;
}
}
}
return FALSE;
}
|