| 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 "xfa/src/fxbarcode/common/BC_WhiteRectangleDetector.h" | |
| 24 | |
| 25 #include <memory> | |
| 26 | |
| 27 #include "xfa/src/fxbarcode/BC_ResultPoint.h" | |
| 28 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h" | |
| 29 #include "xfa/src/fxbarcode/utils.h" | |
| 30 | |
| 31 CBC_WhiteRectangleDetector::CBC_WhiteRectangleDetector( | |
| 32 CBC_CommonBitMatrix* image) { | |
| 33 m_image = image; | |
| 34 m_height = image->GetHeight(); | |
| 35 m_width = image->GetWidth(); | |
| 36 m_leftInit = (m_width - INIT_SIZE) >> 1; | |
| 37 m_rightInit = (m_width + INIT_SIZE) >> 1; | |
| 38 m_upInit = (m_height - INIT_SIZE) >> 1; | |
| 39 m_downInit = (m_height + INIT_SIZE) >> 1; | |
| 40 } | |
| 41 void CBC_WhiteRectangleDetector::Init(int32_t& e) { | |
| 42 if (m_upInit < 0 || m_leftInit < 0 || m_downInit >= m_height || | |
| 43 m_rightInit >= m_width) { | |
| 44 e = BCExceptionNotFound; | |
| 45 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 46 } | |
| 47 } | |
| 48 CBC_WhiteRectangleDetector::CBC_WhiteRectangleDetector( | |
| 49 CBC_CommonBitMatrix* image, | |
| 50 int32_t initSize, | |
| 51 int32_t x, | |
| 52 int32_t y) { | |
| 53 m_image = image; | |
| 54 m_height = image->GetHeight(); | |
| 55 m_width = image->GetWidth(); | |
| 56 int32_t halfsize = initSize >> 1; | |
| 57 m_leftInit = x - halfsize; | |
| 58 m_rightInit = x + halfsize; | |
| 59 m_upInit = y - halfsize; | |
| 60 m_downInit = y + halfsize; | |
| 61 } | |
| 62 CBC_WhiteRectangleDetector::~CBC_WhiteRectangleDetector() {} | |
| 63 CFX_PtrArray* CBC_WhiteRectangleDetector::Detect(int32_t& e) { | |
| 64 int32_t left = m_leftInit; | |
| 65 int32_t right = m_rightInit; | |
| 66 int32_t up = m_upInit; | |
| 67 int32_t down = m_downInit; | |
| 68 FX_BOOL sizeExceeded = FALSE; | |
| 69 FX_BOOL aBlackPointFoundOnBorder = TRUE; | |
| 70 FX_BOOL atLeastOneBlackPointFoundOnBorder = FALSE; | |
| 71 while (aBlackPointFoundOnBorder) { | |
| 72 aBlackPointFoundOnBorder = FALSE; | |
| 73 FX_BOOL rightBorderNotWhite = TRUE; | |
| 74 while (rightBorderNotWhite && right < m_width) { | |
| 75 rightBorderNotWhite = ContainsBlackPoint(up, down, right, FALSE); | |
| 76 if (rightBorderNotWhite) { | |
| 77 right++; | |
| 78 aBlackPointFoundOnBorder = TRUE; | |
| 79 } | |
| 80 } | |
| 81 if (right >= m_width) { | |
| 82 sizeExceeded = TRUE; | |
| 83 break; | |
| 84 } | |
| 85 FX_BOOL bottomBorderNotWhite = TRUE; | |
| 86 while (bottomBorderNotWhite && down < m_height) { | |
| 87 bottomBorderNotWhite = ContainsBlackPoint(left, right, down, TRUE); | |
| 88 if (bottomBorderNotWhite) { | |
| 89 down++; | |
| 90 aBlackPointFoundOnBorder = TRUE; | |
| 91 } | |
| 92 } | |
| 93 if (down >= m_height) { | |
| 94 sizeExceeded = TRUE; | |
| 95 break; | |
| 96 } | |
| 97 FX_BOOL leftBorderNotWhite = TRUE; | |
| 98 while (leftBorderNotWhite && left >= 0) { | |
| 99 leftBorderNotWhite = ContainsBlackPoint(up, down, left, FALSE); | |
| 100 if (leftBorderNotWhite) { | |
| 101 left--; | |
| 102 aBlackPointFoundOnBorder = TRUE; | |
| 103 } | |
| 104 } | |
| 105 if (left < 0) { | |
| 106 sizeExceeded = TRUE; | |
| 107 break; | |
| 108 } | |
| 109 FX_BOOL topBorderNotWhite = TRUE; | |
| 110 while (topBorderNotWhite && up >= 0) { | |
| 111 topBorderNotWhite = ContainsBlackPoint(left, right, up, TRUE); | |
| 112 if (topBorderNotWhite) { | |
| 113 up--; | |
| 114 aBlackPointFoundOnBorder = TRUE; | |
| 115 } | |
| 116 } | |
| 117 if (up < 0) { | |
| 118 sizeExceeded = TRUE; | |
| 119 break; | |
| 120 } | |
| 121 if (aBlackPointFoundOnBorder) { | |
| 122 atLeastOneBlackPointFoundOnBorder = TRUE; | |
| 123 } | |
| 124 } | |
| 125 if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) { | |
| 126 int32_t maxSize = right - left; | |
| 127 std::unique_ptr<CBC_ResultPoint> z; | |
| 128 for (int32_t i = 1; i < maxSize; i++) { | |
| 129 z.reset(GetBlackPointOnSegment((FX_FLOAT)left, (FX_FLOAT)(down - i), | |
| 130 (FX_FLOAT)(left + i), (FX_FLOAT)(down))); | |
| 131 if (z) | |
| 132 break; | |
| 133 } | |
| 134 if (z.get() == NULL) { | |
| 135 e = BCExceptionNotFound; | |
| 136 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 137 } | |
| 138 std::unique_ptr<CBC_ResultPoint> t; | |
| 139 for (int32_t j = 1; j < maxSize; j++) { | |
| 140 t.reset(GetBlackPointOnSegment((FX_FLOAT)left, (FX_FLOAT)(up + j), | |
| 141 (FX_FLOAT)(left + j), (FX_FLOAT)up)); | |
| 142 if (t) | |
| 143 break; | |
| 144 } | |
| 145 if (t.get() == NULL) { | |
| 146 e = BCExceptionNotFound; | |
| 147 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 148 } | |
| 149 std::unique_ptr<CBC_ResultPoint> x; | |
| 150 for (int32_t k = 1; k < maxSize; k++) { | |
| 151 x.reset(GetBlackPointOnSegment((FX_FLOAT)right, (FX_FLOAT)(up + k), | |
| 152 (FX_FLOAT)(right - k), (FX_FLOAT)up)); | |
| 153 if (x) | |
| 154 break; | |
| 155 } | |
| 156 if (x.get() == NULL) { | |
| 157 e = BCExceptionNotFound; | |
| 158 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 159 } | |
| 160 std::unique_ptr<CBC_ResultPoint> y; | |
| 161 for (int32_t m = 1; m < maxSize; m++) { | |
| 162 y.reset(GetBlackPointOnSegment((FX_FLOAT)right, (FX_FLOAT)(down - m), | |
| 163 (FX_FLOAT)(right - m), (FX_FLOAT)down)); | |
| 164 if (y) | |
| 165 break; | |
| 166 } | |
| 167 if (y.get() == NULL) { | |
| 168 e = BCExceptionNotFound; | |
| 169 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 170 } | |
| 171 return CenterEdges(y.get(), z.get(), x.get(), t.get()); | |
| 172 } else { | |
| 173 e = BCExceptionNotFound; | |
| 174 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 175 } | |
| 176 return NULL; | |
| 177 } | |
| 178 int32_t CBC_WhiteRectangleDetector::Round(FX_FLOAT d) { | |
| 179 return (int32_t)(d + 0.5f); | |
| 180 } | |
| 181 CBC_ResultPoint* CBC_WhiteRectangleDetector::GetBlackPointOnSegment( | |
| 182 FX_FLOAT aX, | |
| 183 FX_FLOAT aY, | |
| 184 FX_FLOAT bX, | |
| 185 FX_FLOAT bY) { | |
| 186 int32_t dist = DistanceL2(aX, aY, bX, bY); | |
| 187 float xStep = (bX - aX) / dist; | |
| 188 float yStep = (bY - aY) / dist; | |
| 189 for (int32_t i = 0; i < dist; i++) { | |
| 190 int32_t x = Round(aX + i * xStep); | |
| 191 int32_t y = Round(aY + i * yStep); | |
| 192 if (m_image->Get(x, y)) { | |
| 193 return new CBC_ResultPoint((FX_FLOAT)x, (FX_FLOAT)y); | |
| 194 } | |
| 195 } | |
| 196 return NULL; | |
| 197 } | |
| 198 int32_t CBC_WhiteRectangleDetector::DistanceL2(FX_FLOAT aX, | |
| 199 FX_FLOAT aY, | |
| 200 FX_FLOAT bX, | |
| 201 FX_FLOAT bY) { | |
| 202 float xDiff = aX - bX; | |
| 203 float yDiff = aY - bY; | |
| 204 return Round((float)sqrt(xDiff * xDiff + yDiff * yDiff)); | |
| 205 } | |
| 206 CFX_PtrArray* CBC_WhiteRectangleDetector::CenterEdges(CBC_ResultPoint* y, | |
| 207 CBC_ResultPoint* z, | |
| 208 CBC_ResultPoint* x, | |
| 209 CBC_ResultPoint* t) { | |
| 210 float yi = y->GetX(); | |
| 211 float yj = y->GetY(); | |
| 212 float zi = z->GetX(); | |
| 213 float zj = z->GetY(); | |
| 214 float xi = x->GetX(); | |
| 215 float xj = x->GetY(); | |
| 216 float ti = t->GetX(); | |
| 217 float tj = t->GetY(); | |
| 218 if (yi < m_width / 2) { | |
| 219 CFX_PtrArray* result = new CFX_PtrArray; | |
| 220 result->SetSize(4); | |
| 221 (*result)[0] = new CBC_ResultPoint(ti - CORR, tj + CORR); | |
| 222 (*result)[1] = new CBC_ResultPoint(zi + CORR, zj + CORR); | |
| 223 (*result)[2] = new CBC_ResultPoint(xi - CORR, xj - CORR); | |
| 224 (*result)[3] = new CBC_ResultPoint(yi + CORR, yj - CORR); | |
| 225 return result; | |
| 226 } else { | |
| 227 CFX_PtrArray* result = new CFX_PtrArray; | |
| 228 result->SetSize(4); | |
| 229 (*result)[0] = new CBC_ResultPoint(ti + CORR, tj + CORR); | |
| 230 (*result)[1] = new CBC_ResultPoint(zi + CORR, zj - CORR); | |
| 231 (*result)[2] = new CBC_ResultPoint(xi - CORR, xj + CORR); | |
| 232 (*result)[3] = new CBC_ResultPoint(yi - CORR, yj - CORR); | |
| 233 return result; | |
| 234 } | |
| 235 } | |
| 236 FX_BOOL CBC_WhiteRectangleDetector::ContainsBlackPoint(int32_t a, | |
| 237 int32_t b, | |
| 238 int32_t fixed, | |
| 239 FX_BOOL horizontal) { | |
| 240 if (horizontal) { | |
| 241 for (int32_t x = a; x <= b; x++) { | |
| 242 if (m_image->Get(x, fixed)) { | |
| 243 return TRUE; | |
| 244 } | |
| 245 } | |
| 246 } else { | |
| 247 for (int32_t y = a; y <= b; y++) { | |
| 248 if (m_image->Get(fixed, y)) { | |
| 249 return TRUE; | |
| 250 } | |
| 251 } | |
| 252 } | |
| 253 return FALSE; | |
| 254 } | |
| OLD | NEW |