1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 //
5 // Tencent is pleased to support the open source community by making WeChat QRCode available.
6 // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
7 #include "../../precomp.hpp"
8 #include "bytematrix.hpp"
9 
10 using zxing::ArrayRef;
11 using zxing::ByteMatrix;
12 using zxing::ErrorHandler;
13 using zxing::Ref;
14 
init(int _width,int _height)15 void ByteMatrix::init(int _width, int _height) {
16     if (_width < 1 || _height < 1) {
17         return;
18     }
19     this->width = _width;
20     this->height = _height;
21     bytes = new unsigned char[width * height];
22     row_offsets = new int[height];
23     row_offsets[0] = 0;
24     for (int i = 1; i < height; i++) {
25         row_offsets[i] = row_offsets[i - 1] + width;
26     }
27 }
28 
ByteMatrix(int dimension)29 ByteMatrix::ByteMatrix(int dimension) { init(dimension, dimension); }
30 
ByteMatrix(int _width,int _height)31 ByteMatrix::ByteMatrix(int _width, int _height) { init(_width, _height); }
32 
ByteMatrix(int _width,int _height,ArrayRef<char> source)33 ByteMatrix::ByteMatrix(int _width, int _height, ArrayRef<char> source) {
34     init(_width, _height);
35     int size = _width * _height;
36     memcpy(&bytes[0], &source[0], size);
37 }
38 
~ByteMatrix()39 ByteMatrix::~ByteMatrix() {
40     if (bytes) delete[] bytes;
41     if (row_offsets) delete[] row_offsets;
42 }
43 
getByteRow(int y,ErrorHandler & err_handler)44 unsigned char* ByteMatrix::getByteRow(int y, ErrorHandler& err_handler) {
45     if (y < 0 || y >= getHeight()) {
46         err_handler = IllegalArgumentErrorHandler("Requested row is outside the image.");
47         return NULL;
48     }
49     return &bytes[row_offsets[y]];
50 }
51