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