| 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 2007 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/qrcode/BC_QRGridSampler.h" | |
| 24 | |
| 25 #include <memory> | |
| 26 | |
| 27 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h" | |
| 28 #include "xfa/src/fxbarcode/common/BC_CommonPerspectiveTransform.h" | |
| 29 #include "xfa/src/fxbarcode/utils.h" | |
| 30 | |
| 31 CBC_QRGridSampler CBC_QRGridSampler::m_gridSampler; | |
| 32 | |
| 33 CBC_QRGridSampler::CBC_QRGridSampler() {} | |
| 34 CBC_QRGridSampler::~CBC_QRGridSampler() {} | |
| 35 CBC_QRGridSampler& CBC_QRGridSampler::GetInstance() { | |
| 36 return m_gridSampler; | |
| 37 } | |
| 38 void CBC_QRGridSampler::CheckAndNudgePoints(CBC_CommonBitMatrix* image, | |
| 39 CFX_FloatArray* points, | |
| 40 int32_t& e) { | |
| 41 int32_t width = image->GetWidth(); | |
| 42 int32_t height = image->GetHeight(); | |
| 43 FX_BOOL nudged = TRUE; | |
| 44 int32_t offset; | |
| 45 for (offset = 0; offset < points->GetSize() && nudged; offset += 2) { | |
| 46 int32_t x = (int32_t)(*points)[offset]; | |
| 47 int32_t y = (int32_t)(*points)[offset + 1]; | |
| 48 if (x < -1 || x > width || y < -1 || y > height) { | |
| 49 e = BCExceptionRead; | |
| 50 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 51 } | |
| 52 nudged = FALSE; | |
| 53 if (x == -1) { | |
| 54 (*points)[offset] = 0.0f; | |
| 55 nudged = TRUE; | |
| 56 } else if (x == width) { | |
| 57 (*points)[offset] = (FX_FLOAT)(width - 1); | |
| 58 nudged = TRUE; | |
| 59 } | |
| 60 if (y == -1) { | |
| 61 (*points)[offset + 1] = 0.0f; | |
| 62 nudged = TRUE; | |
| 63 } else if (y == height) { | |
| 64 (*points)[offset + 1] = (FX_FLOAT)(height - 1); | |
| 65 nudged = TRUE; | |
| 66 } | |
| 67 } | |
| 68 nudged = TRUE; | |
| 69 for (offset = (*points).GetSize() - 2; offset >= 0 && nudged; offset -= 2) { | |
| 70 int32_t x = (int32_t)(*points)[offset]; | |
| 71 int32_t y = (int32_t)(*points)[offset + 1]; | |
| 72 if (x < -1 || x > width || y < -1 || y > height) { | |
| 73 e = BCExceptionRead; | |
| 74 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 75 } | |
| 76 nudged = FALSE; | |
| 77 if (x == -1) { | |
| 78 (*points)[offset] = 0.0f; | |
| 79 nudged = TRUE; | |
| 80 } else if (x == width) { | |
| 81 (*points)[offset] = (FX_FLOAT)(width - 1); | |
| 82 nudged = TRUE; | |
| 83 } | |
| 84 if (y == -1) { | |
| 85 (*points)[offset + 1] = 0.0f; | |
| 86 nudged = TRUE; | |
| 87 } else if (y == height) { | |
| 88 (*points)[offset + 1] = (FX_FLOAT)(height - 1); | |
| 89 nudged = TRUE; | |
| 90 } | |
| 91 } | |
| 92 } | |
| 93 CBC_CommonBitMatrix* CBC_QRGridSampler::SampleGrid(CBC_CommonBitMatrix* image, | |
| 94 int32_t dimensionX, | |
| 95 int32_t dimensionY, | |
| 96 FX_FLOAT p1ToX, | |
| 97 FX_FLOAT p1ToY, | |
| 98 FX_FLOAT p2ToX, | |
| 99 FX_FLOAT p2ToY, | |
| 100 FX_FLOAT p3ToX, | |
| 101 FX_FLOAT p3ToY, | |
| 102 FX_FLOAT p4ToX, | |
| 103 FX_FLOAT p4ToY, | |
| 104 FX_FLOAT p1FromX, | |
| 105 FX_FLOAT p1FromY, | |
| 106 FX_FLOAT p2FromX, | |
| 107 FX_FLOAT p2FromY, | |
| 108 FX_FLOAT p3FromX, | |
| 109 FX_FLOAT p3FromY, | |
| 110 FX_FLOAT p4FromX, | |
| 111 FX_FLOAT p4FromY, | |
| 112 int32_t& e) { | |
| 113 std::unique_ptr<CBC_CommonPerspectiveTransform> transform( | |
| 114 CBC_CommonPerspectiveTransform::QuadrilateralToQuadrilateral( | |
| 115 p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, | |
| 116 p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY)); | |
| 117 std::unique_ptr<CBC_CommonBitMatrix> bits(new CBC_CommonBitMatrix()); | |
| 118 bits->Init(dimensionX, dimensionY); | |
| 119 CFX_FloatArray points; | |
| 120 points.SetSize(dimensionX << 1); | |
| 121 for (int32_t y = 0; y < dimensionY; y++) { | |
| 122 int32_t max = points.GetSize(); | |
| 123 FX_FLOAT iValue = (FX_FLOAT)(y + 0.5f); | |
| 124 int32_t x; | |
| 125 for (x = 0; x < max; x += 2) { | |
| 126 points[x] = (FX_FLOAT)((x >> 1) + 0.5f); | |
| 127 points[x + 1] = iValue; | |
| 128 } | |
| 129 transform->TransformPoints(&points); | |
| 130 CheckAndNudgePoints(image, &points, e); | |
| 131 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 132 for (x = 0; x < max; x += 2) { | |
| 133 if (image->Get((int32_t)points[x], (int32_t)points[x + 1])) { | |
| 134 bits->Set(x >> 1, y); | |
| 135 } | |
| 136 } | |
| 137 } | |
| 138 return bits.release(); | |
| 139 } | |
| OLD | NEW |