Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: xfa/src/fxbarcode/datamatrix/BC_DataMatrixDetector.cpp

Issue 1734823002: Get rid of CBC_AutoPtr and use std::unique_ptr instead. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: rebase Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 2008 ZXing authors 8 * Copyright 2008 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 "xfa/src/fxbarcode/datamatrix/BC_DataMatrixDetector.h"
24
23 #include <algorithm> 25 #include <algorithm>
26 #include <memory>
24 27
25 #include "xfa/src/fxbarcode/BC_ResultPoint.h" 28 #include "xfa/src/fxbarcode/BC_ResultPoint.h"
26 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h" 29 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
27 #include "xfa/src/fxbarcode/common/BC_WhiteRectangleDetector.h" 30 #include "xfa/src/fxbarcode/common/BC_WhiteRectangleDetector.h"
28 #include "xfa/src/fxbarcode/datamatrix/BC_DataMatrixDetector.h"
29 #include "xfa/src/fxbarcode/qrcode/BC_QRDetectorResult.h" 31 #include "xfa/src/fxbarcode/qrcode/BC_QRDetectorResult.h"
30 #include "xfa/src/fxbarcode/qrcode/BC_QRFinderPatternFinder.h" 32 #include "xfa/src/fxbarcode/qrcode/BC_QRFinderPatternFinder.h"
31 #include "xfa/src/fxbarcode/qrcode/BC_QRGridSampler.h" 33 #include "xfa/src/fxbarcode/qrcode/BC_QRGridSampler.h"
32 #include "xfa/src/fxbarcode/utils.h" 34 #include "xfa/src/fxbarcode/utils.h"
33 35
34 const int32_t CBC_DataMatrixDetector::INTEGERS[5] = {0, 1, 2, 3, 4}; 36 const int32_t CBC_DataMatrixDetector::INTEGERS[5] = {0, 1, 2, 3, 4};
35 CBC_DataMatrixDetector::CBC_DataMatrixDetector(CBC_CommonBitMatrix* image) 37 CBC_DataMatrixDetector::CBC_DataMatrixDetector(CBC_CommonBitMatrix* image)
36 : m_image(image), m_rectangleDetector(NULL) {} 38 : m_image(image), m_rectangleDetector(NULL) {}
37 void CBC_DataMatrixDetector::Init(int32_t& e) { 39 void CBC_DataMatrixDetector::Init(int32_t& e) {
38 m_rectangleDetector = new CBC_WhiteRectangleDetector(m_image); 40 m_rectangleDetector = new CBC_WhiteRectangleDetector(m_image);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 int32_t value; 116 int32_t value;
115 if (!pointCount.Lookup(pointA, value)) { 117 if (!pointCount.Lookup(pointA, value)) {
116 topRight = pointA; 118 topRight = pointA;
117 } else if (!pointCount.Lookup(pointB, value)) { 119 } else if (!pointCount.Lookup(pointB, value)) {
118 topRight = pointB; 120 topRight = pointB;
119 } else if (!pointCount.Lookup(pointC, value)) { 121 } else if (!pointCount.Lookup(pointC, value)) {
120 topRight = pointC; 122 topRight = pointC;
121 } else { 123 } else {
122 topRight = pointD; 124 topRight = pointD;
123 } 125 }
124 int32_t dimensionTop = CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 126 int32_t dimensionTop = std::unique_ptr<CBC_ResultPointsAndTransitions>(
125 TransitionsBetween(topLeft, topRight)) 127 TransitionsBetween(topLeft, topRight))
126 ->GetTransitions(); 128 ->GetTransitions();
127 int32_t dimensionRight = CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 129 int32_t dimensionRight = std::unique_ptr<CBC_ResultPointsAndTransitions>(
128 TransitionsBetween(bottomRight, topRight)) 130 TransitionsBetween(bottomRight, topRight))
129 ->GetTransitions(); 131 ->GetTransitions();
130 if ((dimensionTop & 0x01) == 1) { 132 if ((dimensionTop & 0x01) == 1) {
131 dimensionTop++; 133 dimensionTop++;
132 } 134 }
133 dimensionTop += 2; 135 dimensionTop += 2;
134 if ((dimensionRight & 0x01) == 1) { 136 if ((dimensionRight & 0x01) == 1) {
135 dimensionRight++; 137 dimensionRight++;
136 } 138 }
137 dimensionRight += 2; 139 dimensionRight += 2;
138 CBC_AutoPtr<CBC_CommonBitMatrix> bits(NULL); 140 std::unique_ptr<CBC_CommonBitMatrix> bits;
139 CBC_AutoPtr<CBC_ResultPoint> correctedTopRight(NULL); 141 std::unique_ptr<CBC_ResultPoint> correctedTopRight;
140 if (4 * dimensionTop >= 7 * dimensionRight || 142 if (4 * dimensionTop >= 7 * dimensionRight ||
141 4 * dimensionRight >= 7 * dimensionTop) { 143 4 * dimensionRight >= 7 * dimensionTop) {
142 correctedTopRight = CBC_AutoPtr<CBC_ResultPoint>( 144 correctedTopRight.reset(
143 CorrectTopRightRectangular(bottomLeft, bottomRight, topLeft, topRight, 145 CorrectTopRightRectangular(bottomLeft, bottomRight, topLeft, topRight,
144 dimensionTop, dimensionRight)); 146 dimensionTop, dimensionRight));
145 if (correctedTopRight.get() == NULL) { 147 if (correctedTopRight.get() == NULL) {
146 correctedTopRight = CBC_AutoPtr<CBC_ResultPoint>(topRight); 148 correctedTopRight.reset(topRight);
147 } else { 149 } else {
148 delete topRight; 150 delete topRight;
149 topRight = NULL; 151 topRight = NULL;
150 } 152 }
151 dimensionTop = CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 153 dimensionTop = std::unique_ptr<CBC_ResultPointsAndTransitions>(
152 TransitionsBetween(topLeft, correctedTopRight.get())) 154 TransitionsBetween(topLeft, correctedTopRight.get()))
153 ->GetTransitions(); 155 ->GetTransitions();
154 dimensionRight = 156 dimensionRight =
dsinclair 2016/02/25 01:56:55 Why not .reset() like the others?
Lei Zhang 2016/02/25 02:18:12 |dimensionRight| is an int.
155 CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 157 std::unique_ptr<CBC_ResultPointsAndTransitions>(
156 TransitionsBetween(bottomRight, correctedTopRight.get())) 158 TransitionsBetween(bottomRight, correctedTopRight.get()))
157 ->GetTransitions(); 159 ->GetTransitions();
158 if ((dimensionTop & 0x01) == 1) { 160 if ((dimensionTop & 0x01) == 1) {
159 dimensionTop++; 161 dimensionTop++;
160 } 162 }
161 if ((dimensionRight & 0x01) == 1) { 163 if ((dimensionRight & 0x01) == 1) {
162 dimensionRight++; 164 dimensionRight++;
163 } 165 }
164 bits = CBC_AutoPtr<CBC_CommonBitMatrix>( 166 bits.reset(SampleGrid(m_image, topLeft, bottomLeft, bottomRight,
165 SampleGrid(m_image, topLeft, bottomLeft, bottomRight, 167 correctedTopRight.get(), dimensionTop, dimensionRight,
166 correctedTopRight.get(), dimensionTop, dimensionRight, e)); 168 e));
167 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 169 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
168 } else { 170 } else {
169 int32_t dimension = std::min(dimensionRight, dimensionTop); 171 int32_t dimension = std::min(dimensionRight, dimensionTop);
170 correctedTopRight = CBC_AutoPtr<CBC_ResultPoint>( 172 correctedTopRight.reset(
171 CorrectTopRight(bottomLeft, bottomRight, topLeft, topRight, dimension)); 173 CorrectTopRight(bottomLeft, bottomRight, topLeft, topRight, dimension));
172 if (correctedTopRight.get() == NULL) { 174 if (correctedTopRight.get() == NULL) {
173 correctedTopRight = CBC_AutoPtr<CBC_ResultPoint>(topRight); 175 correctedTopRight.reset(topRight);
174 } else { 176 } else {
175 delete topRight; 177 delete topRight;
176 topRight = NULL; 178 topRight = NULL;
177 } 179 }
178 int32_t dimensionCorrected = 180 int32_t dimensionCorrected =
179 std::max(CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 181 std::max(std::unique_ptr<CBC_ResultPointsAndTransitions>(
180 TransitionsBetween(topLeft, correctedTopRight.get())) 182 TransitionsBetween(topLeft, correctedTopRight.get()))
181 ->GetTransitions(), 183 ->GetTransitions(),
182 CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 184 std::unique_ptr<CBC_ResultPointsAndTransitions>(
183 TransitionsBetween(bottomRight, correctedTopRight.get())) 185 TransitionsBetween(bottomRight, correctedTopRight.get()))
184 ->GetTransitions()); 186 ->GetTransitions());
185 dimensionCorrected++; 187 dimensionCorrected++;
186 if ((dimensionCorrected & 0x01) == 1) { 188 if ((dimensionCorrected & 0x01) == 1) {
187 dimensionCorrected++; 189 dimensionCorrected++;
188 } 190 }
189 bits = CBC_AutoPtr<CBC_CommonBitMatrix>(SampleGrid( 191 bits.reset(SampleGrid(m_image, topLeft, bottomLeft, bottomRight,
190 m_image, topLeft, bottomLeft, bottomRight, correctedTopRight.get(), 192 correctedTopRight.get(), dimensionCorrected,
191 dimensionCorrected, dimensionCorrected, e)); 193 dimensionCorrected, e));
192 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 194 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
193 } 195 }
194 CFX_PtrArray* result = new CFX_PtrArray; 196 CFX_PtrArray* result = new CFX_PtrArray;
195 result->SetSize(4); 197 result->SetSize(4);
196 result->Add(topLeft); 198 result->Add(topLeft);
197 result->Add(bottomLeft); 199 result->Add(bottomLeft);
198 result->Add(bottomRight); 200 result->Add(bottomRight);
199 result->Add(correctedTopRight.release()); 201 result->Add(correctedTopRight.release());
200 return new CBC_QRDetectorResult(bits.release(), result); 202 return new CBC_QRDetectorResult(bits.release(), result);
201 } 203 }
202 CBC_ResultPoint* CBC_DataMatrixDetector::CorrectTopRightRectangular( 204 CBC_ResultPoint* CBC_DataMatrixDetector::CorrectTopRightRectangular(
203 CBC_ResultPoint* bottomLeft, 205 CBC_ResultPoint* bottomLeft,
204 CBC_ResultPoint* bottomRight, 206 CBC_ResultPoint* bottomRight,
205 CBC_ResultPoint* topLeft, 207 CBC_ResultPoint* topLeft,
206 CBC_ResultPoint* topRight, 208 CBC_ResultPoint* topRight,
207 int32_t dimensionTop, 209 int32_t dimensionTop,
208 int32_t dimensionRight) { 210 int32_t dimensionRight) {
209 FX_FLOAT corr = Distance(bottomLeft, bottomRight) / (FX_FLOAT)dimensionTop; 211 FX_FLOAT corr = Distance(bottomLeft, bottomRight) / (FX_FLOAT)dimensionTop;
210 int32_t norm = Distance(topLeft, topRight); 212 int32_t norm = Distance(topLeft, topRight);
211 FX_FLOAT cos = (topRight->GetX() - topLeft->GetX()) / norm; 213 FX_FLOAT cos = (topRight->GetX() - topLeft->GetX()) / norm;
212 FX_FLOAT sin = (topRight->GetY() - topLeft->GetY()) / norm; 214 FX_FLOAT sin = (topRight->GetY() - topLeft->GetY()) / norm;
213 CBC_AutoPtr<CBC_ResultPoint> c1(new CBC_ResultPoint( 215 std::unique_ptr<CBC_ResultPoint> c1(new CBC_ResultPoint(
214 topRight->GetX() + corr * cos, topRight->GetY() + corr * sin)); 216 topRight->GetX() + corr * cos, topRight->GetY() + corr * sin));
215 corr = Distance(bottomLeft, topLeft) / (FX_FLOAT)dimensionRight; 217 corr = Distance(bottomLeft, topLeft) / (FX_FLOAT)dimensionRight;
216 norm = Distance(bottomRight, topRight); 218 norm = Distance(bottomRight, topRight);
217 cos = (topRight->GetX() - bottomRight->GetX()) / norm; 219 cos = (topRight->GetX() - bottomRight->GetX()) / norm;
218 sin = (topRight->GetY() - bottomRight->GetY()) / norm; 220 sin = (topRight->GetY() - bottomRight->GetY()) / norm;
219 CBC_AutoPtr<CBC_ResultPoint> c2(new CBC_ResultPoint( 221 std::unique_ptr<CBC_ResultPoint> c2(new CBC_ResultPoint(
220 topRight->GetX() + corr * cos, topRight->GetY() + corr * sin)); 222 topRight->GetX() + corr * cos, topRight->GetY() + corr * sin));
221 if (!IsValid(c1.get())) { 223 if (!IsValid(c1.get())) {
222 if (IsValid(c2.get())) { 224 if (IsValid(c2.get())) {
223 return c2.release(); 225 return c2.release();
224 } 226 }
225 return NULL; 227 return NULL;
226 } else if (!IsValid(c2.get())) { 228 } else if (!IsValid(c2.get())) {
227 return c1.release(); 229 return c1.release();
228 } 230 }
229 int32_t l1 = FXSYS_abs(dimensionTop - 231 int32_t l1 = FXSYS_abs(dimensionTop -
230 CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 232 std::unique_ptr<CBC_ResultPointsAndTransitions>(
231 TransitionsBetween(topLeft, c1.get())) 233 TransitionsBetween(topLeft, c1.get()))
232 ->GetTransitions()) + 234 ->GetTransitions()) +
233 FXSYS_abs(dimensionRight - 235 FXSYS_abs(dimensionRight -
234 CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 236 std::unique_ptr<CBC_ResultPointsAndTransitions>(
235 TransitionsBetween(bottomRight, c1.get())) 237 TransitionsBetween(bottomRight, c1.get()))
236 ->GetTransitions()); 238 ->GetTransitions());
237 int32_t l2 = FXSYS_abs(dimensionTop - 239 int32_t l2 = FXSYS_abs(dimensionTop -
238 CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 240 std::unique_ptr<CBC_ResultPointsAndTransitions>(
239 TransitionsBetween(topLeft, c2.get())) 241 TransitionsBetween(topLeft, c2.get()))
240 ->GetTransitions()) + 242 ->GetTransitions()) +
241 FXSYS_abs(dimensionRight - 243 FXSYS_abs(dimensionRight -
242 CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 244 std::unique_ptr<CBC_ResultPointsAndTransitions>(
243 TransitionsBetween(bottomRight, c2.get())) 245 TransitionsBetween(bottomRight, c2.get()))
244 ->GetTransitions()); 246 ->GetTransitions());
245 if (l1 <= l2) { 247 if (l1 <= l2) {
246 return c1.release(); 248 return c1.release();
247 } 249 }
248 return c2.release(); 250 return c2.release();
249 } 251 }
250 CBC_ResultPoint* CBC_DataMatrixDetector::CorrectTopRight( 252 CBC_ResultPoint* CBC_DataMatrixDetector::CorrectTopRight(
251 CBC_ResultPoint* bottomLeft, 253 CBC_ResultPoint* bottomLeft,
252 CBC_ResultPoint* bottomRight, 254 CBC_ResultPoint* bottomRight,
253 CBC_ResultPoint* topLeft, 255 CBC_ResultPoint* topLeft,
254 CBC_ResultPoint* topRight, 256 CBC_ResultPoint* topRight,
255 int32_t dimension) { 257 int32_t dimension) {
256 FX_FLOAT corr = Distance(bottomLeft, bottomRight) / (FX_FLOAT)dimension; 258 FX_FLOAT corr = Distance(bottomLeft, bottomRight) / (FX_FLOAT)dimension;
257 int32_t norm = Distance(topLeft, topRight); 259 int32_t norm = Distance(topLeft, topRight);
258 FX_FLOAT cos = (topRight->GetX() - topLeft->GetX()) / norm; 260 FX_FLOAT cos = (topRight->GetX() - topLeft->GetX()) / norm;
259 FX_FLOAT sin = (topRight->GetY() - topLeft->GetY()) / norm; 261 FX_FLOAT sin = (topRight->GetY() - topLeft->GetY()) / norm;
260 CBC_AutoPtr<CBC_ResultPoint> c1(new CBC_ResultPoint( 262 std::unique_ptr<CBC_ResultPoint> c1(new CBC_ResultPoint(
261 topRight->GetX() + corr * cos, topRight->GetY() + corr * sin)); 263 topRight->GetX() + corr * cos, topRight->GetY() + corr * sin));
262 corr = Distance(bottomLeft, bottomRight) / (FX_FLOAT)dimension; 264 corr = Distance(bottomLeft, bottomRight) / (FX_FLOAT)dimension;
263 norm = Distance(bottomRight, topRight); 265 norm = Distance(bottomRight, topRight);
264 cos = (topRight->GetX() - bottomRight->GetX()) / norm; 266 cos = (topRight->GetX() - bottomRight->GetX()) / norm;
265 sin = (topRight->GetY() - bottomRight->GetY()) / norm; 267 sin = (topRight->GetY() - bottomRight->GetY()) / norm;
266 CBC_AutoPtr<CBC_ResultPoint> c2(new CBC_ResultPoint( 268 std::unique_ptr<CBC_ResultPoint> c2(new CBC_ResultPoint(
267 topRight->GetX() + corr * cos, topRight->GetY() + corr * sin)); 269 topRight->GetX() + corr * cos, topRight->GetY() + corr * sin));
268 if (!IsValid(c1.get())) { 270 if (!IsValid(c1.get())) {
269 if (IsValid(c2.get())) { 271 if (IsValid(c2.get())) {
270 return c2.release(); 272 return c2.release();
271 } 273 }
272 return NULL; 274 return NULL;
273 } else if (!IsValid(c2.get())) { 275 } else if (!IsValid(c2.get())) {
274 return c1.release(); 276 return c1.release();
275 } 277 }
276 int32_t l1 = FXSYS_abs(CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 278 int32_t l1 = FXSYS_abs(std::unique_ptr<CBC_ResultPointsAndTransitions>(
277 TransitionsBetween(topLeft, c1.get())) 279 TransitionsBetween(topLeft, c1.get()))
278 ->GetTransitions() - 280 ->GetTransitions() -
279 CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 281 std::unique_ptr<CBC_ResultPointsAndTransitions>(
280 TransitionsBetween(bottomRight, c1.get())) 282 TransitionsBetween(bottomRight, c1.get()))
281 ->GetTransitions()); 283 ->GetTransitions());
282 int32_t l2 = FXSYS_abs(CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 284 int32_t l2 = FXSYS_abs(std::unique_ptr<CBC_ResultPointsAndTransitions>(
283 TransitionsBetween(topLeft, c2.get())) 285 TransitionsBetween(topLeft, c2.get()))
284 ->GetTransitions() - 286 ->GetTransitions() -
285 CBC_AutoPtr<CBC_ResultPointsAndTransitions>( 287 std::unique_ptr<CBC_ResultPointsAndTransitions>(
286 TransitionsBetween(bottomRight, c2.get())) 288 TransitionsBetween(bottomRight, c2.get()))
287 ->GetTransitions()); 289 ->GetTransitions());
288 return l1 <= l2 ? c1.release() : c2.release(); 290 return l1 <= l2 ? c1.release() : c2.release();
289 } 291 }
290 FX_BOOL CBC_DataMatrixDetector::IsValid(CBC_ResultPoint* p) { 292 FX_BOOL CBC_DataMatrixDetector::IsValid(CBC_ResultPoint* p) {
291 return p->GetX() >= 0 && p->GetX() < m_image->GetWidth() && p->GetY() > 0 && 293 return p->GetX() >= 0 && p->GetX() < m_image->GetWidth() && p->GetY() > 0 &&
292 p->GetY() < m_image->GetHeight(); 294 p->GetY() < m_image->GetHeight();
293 } 295 }
294 int32_t CBC_DataMatrixDetector::Round(FX_FLOAT d) { 296 int32_t CBC_DataMatrixDetector::Round(FX_FLOAT d) {
295 return (int32_t)(d + 0.5f); 297 return (int32_t)(d + 0.5f);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 (bottomLeft->GetX() - topLeft->GetX()) * 397 (bottomLeft->GetX() - topLeft->GetX()) *
396 (topRight->GetY() - topLeft->GetY())) { 398 (topRight->GetY() - topLeft->GetY())) {
397 CBC_ResultPoint* temp = topRight; 399 CBC_ResultPoint* temp = topRight;
398 topRight = bottomLeft; 400 topRight = bottomLeft;
399 bottomLeft = temp; 401 bottomLeft = temp;
400 } 402 }
401 (*patterns)[0] = bottomLeft; 403 (*patterns)[0] = bottomLeft;
402 (*patterns)[1] = topLeft; 404 (*patterns)[1] = topLeft;
403 (*patterns)[2] = topRight; 405 (*patterns)[2] = topRight;
404 } 406 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698