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