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

Side by Side Diff: xfa/fxbarcode/qrcode/BC_QRCoderEncoder.cpp

Issue 2221023003: Use smart pointers for class owned pointers in xfa/fxbarcode (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 4 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/fxbarcode/qrcode/BC_QRCoderEncoder.h" 23 #include "xfa/fxbarcode/qrcode/BC_QRCoderEncoder.h"
24 24
25 #include <algorithm> 25 #include <algorithm>
26 #include <memory> 26 #include <memory>
27 #include <utility>
27 28
28 #include "xfa/fxbarcode/BC_UtilCodingConvert.h" 29 #include "xfa/fxbarcode/BC_UtilCodingConvert.h"
29 #include "xfa/fxbarcode/common/BC_CommonByteArray.h" 30 #include "xfa/fxbarcode/common/BC_CommonByteArray.h"
30 #include "xfa/fxbarcode/common/BC_CommonByteMatrix.h" 31 #include "xfa/fxbarcode/common/BC_CommonByteMatrix.h"
31 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.h" 32 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.h"
32 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" 33 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
33 #include "xfa/fxbarcode/qrcode/BC_QRCoder.h" 34 #include "xfa/fxbarcode/qrcode/BC_QRCoder.h"
34 #include "xfa/fxbarcode/qrcode/BC_QRCoderBitVector.h" 35 #include "xfa/fxbarcode/qrcode/BC_QRCoderBitVector.h"
35 #include "xfa/fxbarcode/qrcode/BC_QRCoderBlockPair.h" 36 #include "xfa/fxbarcode/qrcode/BC_QRCoderBlockPair.h"
36 #include "xfa/fxbarcode/qrcode/BC_QRCoderECBlocks.h" 37 #include "xfa/fxbarcode/qrcode/BC_QRCoderECBlocks.h"
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 qrCode->GetMatrixWidth(), qrCode->GetMatrixWidth())); 402 qrCode->GetMatrixWidth(), qrCode->GetMatrixWidth()));
402 matrix->Init(); 403 matrix->Init();
403 int32_t maskPattern = ChooseMaskPattern( 404 int32_t maskPattern = ChooseMaskPattern(
404 &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get(), e); 405 &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get(), e);
405 BC_EXCEPTION_CHECK_ReturnVoid(e); 406 BC_EXCEPTION_CHECK_ReturnVoid(e);
406 qrCode->SetMaskPattern(maskPattern); 407 qrCode->SetMaskPattern(maskPattern);
407 CBC_QRCoderMatrixUtil::BuildMatrix(&finalBits, qrCode->GetECLevel(), 408 CBC_QRCoderMatrixUtil::BuildMatrix(&finalBits, qrCode->GetECLevel(),
408 qrCode->GetVersion(), 409 qrCode->GetVersion(),
409 qrCode->GetMaskPattern(), matrix.get(), e); 410 qrCode->GetMaskPattern(), matrix.get(), e);
410 BC_EXCEPTION_CHECK_ReturnVoid(e); 411 BC_EXCEPTION_CHECK_ReturnVoid(e);
411 qrCode->SetMatrix(matrix.release()); 412 qrCode->SetMatrix(std::move(matrix));
412 if (!qrCode->IsValid()) { 413 if (!qrCode->IsValid()) {
413 e = BCExceptionInvalidQRCode; 414 e = BCExceptionInvalidQRCode;
414 BC_EXCEPTION_CHECK_ReturnVoid(e); 415 BC_EXCEPTION_CHECK_ReturnVoid(e);
415 } 416 }
416 } 417 }
417 void CBC_QRCoderEncoder::EncodeWithAutoVersion( 418 void CBC_QRCoderEncoder::EncodeWithAutoVersion(
418 const CFX_ByteString& content, 419 const CFX_ByteString& content,
419 CBC_QRCoderErrorCorrectionLevel* ecLevel, 420 CBC_QRCoderErrorCorrectionLevel* ecLevel,
420 CBC_QRCoder* qrCode, 421 CBC_QRCoder* qrCode,
421 int32_t& e) { 422 int32_t& e) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 std::unique_ptr<CBC_CommonByteMatrix> matrix(new CBC_CommonByteMatrix( 486 std::unique_ptr<CBC_CommonByteMatrix> matrix(new CBC_CommonByteMatrix(
486 qrCode->GetMatrixWidth(), qrCode->GetMatrixWidth())); 487 qrCode->GetMatrixWidth(), qrCode->GetMatrixWidth()));
487 matrix->Init(); 488 matrix->Init();
488 int32_t maskPattern = ChooseMaskPattern( 489 int32_t maskPattern = ChooseMaskPattern(
489 &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get(), e); 490 &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get(), e);
490 BC_EXCEPTION_CHECK_ReturnVoid(e); 491 BC_EXCEPTION_CHECK_ReturnVoid(e);
491 qrCode->SetMaskPattern(maskPattern); 492 qrCode->SetMaskPattern(maskPattern);
492 CBC_QRCoderMatrixUtil::BuildMatrix(&finalBits, qrCode->GetECLevel(), 493 CBC_QRCoderMatrixUtil::BuildMatrix(&finalBits, qrCode->GetECLevel(),
493 qrCode->GetVersion(), 494 qrCode->GetVersion(),
494 qrCode->GetMaskPattern(), matrix.get(), e); 495 qrCode->GetMaskPattern(), matrix.get(), e);
495 BC_EXCEPTION_CHECK_ReturnVoid(e) qrCode->SetMatrix(matrix.release()); 496 BC_EXCEPTION_CHECK_ReturnVoid(e) qrCode->SetMatrix(std::move(matrix));
496 if (!qrCode->IsValid()) { 497 if (!qrCode->IsValid()) {
497 e = BCExceptionInvalidQRCode; 498 e = BCExceptionInvalidQRCode;
498 BC_EXCEPTION_CHECK_ReturnVoid(e); 499 BC_EXCEPTION_CHECK_ReturnVoid(e);
499 } 500 }
500 } 501 }
501 void CBC_QRCoderEncoder::Encode(const CFX_WideString& content, 502 void CBC_QRCoderEncoder::Encode(const CFX_WideString& content,
502 CBC_QRCoderErrorCorrectionLevel* ecLevel, 503 CBC_QRCoderErrorCorrectionLevel* ecLevel,
503 CBC_QRCoder* qrCode, 504 CBC_QRCoder* qrCode,
504 int32_t& e) { 505 int32_t& e) {
505 CFX_ByteString encoding = "utf8"; 506 CFX_ByteString encoding = "utf8";
(...skipping 29 matching lines...) Expand all
535 std::unique_ptr<CBC_CommonByteMatrix> matrix(new CBC_CommonByteMatrix( 536 std::unique_ptr<CBC_CommonByteMatrix> matrix(new CBC_CommonByteMatrix(
536 qrCode->GetMatrixWidth(), qrCode->GetMatrixWidth())); 537 qrCode->GetMatrixWidth(), qrCode->GetMatrixWidth()));
537 matrix->Init(); 538 matrix->Init();
538 int32_t maskPattern = ChooseMaskPattern( 539 int32_t maskPattern = ChooseMaskPattern(
539 &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get(), e); 540 &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get(), e);
540 BC_EXCEPTION_CHECK_ReturnVoid(e); 541 BC_EXCEPTION_CHECK_ReturnVoid(e);
541 qrCode->SetMaskPattern(maskPattern); 542 qrCode->SetMaskPattern(maskPattern);
542 CBC_QRCoderMatrixUtil::BuildMatrix(&finalBits, qrCode->GetECLevel(), 543 CBC_QRCoderMatrixUtil::BuildMatrix(&finalBits, qrCode->GetECLevel(),
543 qrCode->GetVersion(), 544 qrCode->GetVersion(),
544 qrCode->GetMaskPattern(), matrix.get(), e); 545 qrCode->GetMaskPattern(), matrix.get(), e);
545 BC_EXCEPTION_CHECK_ReturnVoid(e) qrCode->SetMatrix(matrix.release()); 546 BC_EXCEPTION_CHECK_ReturnVoid(e) qrCode->SetMatrix(std::move(matrix));
546 if (!qrCode->IsValid()) { 547 if (!qrCode->IsValid()) {
547 e = BCExceptionInvalidQRCode; 548 e = BCExceptionInvalidQRCode;
548 BC_EXCEPTION_CHECK_ReturnVoid(e); 549 BC_EXCEPTION_CHECK_ReturnVoid(e);
549 } 550 }
550 } 551 }
551 void CBC_QRCoderEncoder::TerminateBits(int32_t numDataBytes, 552 void CBC_QRCoderEncoder::TerminateBits(int32_t numDataBytes,
552 CBC_QRCoderBitVector* bits, 553 CBC_QRCoderBitVector* bits,
553 int32_t& e) { 554 int32_t& e) {
554 int32_t capacity = numDataBytes << 3; 555 int32_t capacity = numDataBytes << 3;
555 if (bits->Size() > capacity) { 556 if (bits->Size() > capacity) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 } 589 }
589 } 590 }
590 int32_t CBC_QRCoderEncoder::ChooseMaskPattern( 591 int32_t CBC_QRCoderEncoder::ChooseMaskPattern(
591 CBC_QRCoderBitVector* bits, 592 CBC_QRCoderBitVector* bits,
592 CBC_QRCoderErrorCorrectionLevel* ecLevel, 593 CBC_QRCoderErrorCorrectionLevel* ecLevel,
593 int32_t version, 594 int32_t version,
594 CBC_CommonByteMatrix* matrix, 595 CBC_CommonByteMatrix* matrix,
595 int32_t& e) { 596 int32_t& e) {
596 int32_t minPenalty = 65535; 597 int32_t minPenalty = 65535;
597 int32_t bestMaskPattern = -1; 598 int32_t bestMaskPattern = -1;
598 for (int32_t maskPattern = 0; maskPattern < CBC_QRCoder::NUM_MASK_PATTERNS; 599 for (int32_t maskPattern = 0; maskPattern < CBC_QRCoder::kNumMaskPatterns;
599 maskPattern++) { 600 maskPattern++) {
600 CBC_QRCoderMatrixUtil::BuildMatrix(bits, ecLevel, version, maskPattern, 601 CBC_QRCoderMatrixUtil::BuildMatrix(bits, ecLevel, version, maskPattern,
601 matrix, e); 602 matrix, e);
602 BC_EXCEPTION_CHECK_ReturnValue(e, 0); 603 BC_EXCEPTION_CHECK_ReturnValue(e, 0);
603 int32_t penalty = CalculateMaskPenalty(matrix); 604 int32_t penalty = CalculateMaskPenalty(matrix);
604 if (penalty < minPenalty) { 605 if (penalty < minPenalty) {
605 minPenalty = penalty; 606 minPenalty = penalty;
606 bestMaskPattern = maskPattern; 607 bestMaskPattern = maskPattern;
607 } 608 }
608 } 609 }
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 int32_t maxNumDataBytes = 0; 848 int32_t maxNumDataBytes = 0;
848 int32_t maxNumEcBytes = 0; 849 int32_t maxNumEcBytes = 0;
849 CFX_ArrayTemplate<CBC_QRCoderBlockPair*> blocks; 850 CFX_ArrayTemplate<CBC_QRCoderBlockPair*> blocks;
850 int32_t i; 851 int32_t i;
851 for (i = 0; i < numRSBlocks; i++) { 852 for (i = 0; i < numRSBlocks; i++) {
852 int32_t numDataBytesInBlock; 853 int32_t numDataBytesInBlock;
853 int32_t numEcBytesInBlosk; 854 int32_t numEcBytesInBlosk;
854 GetNumDataBytesAndNumECBytesForBlockID(numTotalBytes, numDataBytes, 855 GetNumDataBytesAndNumECBytesForBlockID(numTotalBytes, numDataBytes,
855 numRSBlocks, i, numDataBytesInBlock, 856 numRSBlocks, i, numDataBytesInBlock,
856 numEcBytesInBlosk); 857 numEcBytesInBlosk);
857 CBC_CommonByteArray* dataBytes = new CBC_CommonByteArray; 858 std::unique_ptr<CBC_CommonByteArray> dataBytes(new CBC_CommonByteArray);
858 dataBytes->Set(bits->GetArray(), dataBytesOffset, numDataBytesInBlock); 859 dataBytes->Set(bits->GetArray(), dataBytesOffset, numDataBytesInBlock);
859 CBC_CommonByteArray* ecBytes = 860 std::unique_ptr<CBC_CommonByteArray> ecBytes(
860 GenerateECBytes(dataBytes, numEcBytesInBlosk, e); 861 GenerateECBytes(dataBytes.get(), numEcBytesInBlosk, e));
861 BC_EXCEPTION_CHECK_ReturnVoid(e); 862 BC_EXCEPTION_CHECK_ReturnVoid(e);
862 blocks.Add(new CBC_QRCoderBlockPair(dataBytes, ecBytes)); 863 blocks.Add(
864 new CBC_QRCoderBlockPair(std::move(dataBytes), std::move(ecBytes)));
863 maxNumDataBytes = std::max(maxNumDataBytes, dataBytes->Size()); 865 maxNumDataBytes = std::max(maxNumDataBytes, dataBytes->Size());
864 maxNumEcBytes = std::max(maxNumEcBytes, ecBytes->Size()); 866 maxNumEcBytes = std::max(maxNumEcBytes, ecBytes->Size());
865 dataBytesOffset += numDataBytesInBlock; 867 dataBytesOffset += numDataBytesInBlock;
866 } 868 }
867 if (numDataBytes != dataBytesOffset) { 869 if (numDataBytes != dataBytesOffset) {
868 e = BCExceptionBytesNotMatchOffset; 870 e = BCExceptionBytesNotMatchOffset;
869 BC_EXCEPTION_CHECK_ReturnVoid(e); 871 BC_EXCEPTION_CHECK_ReturnVoid(e);
870 } 872 }
871 for (int32_t x = 0; x < maxNumDataBytes; x++) { 873 for (int32_t x = 0; x < maxNumDataBytes; x++) {
872 for (int32_t j = 0; j < blocks.GetSize(); j++) { 874 for (int32_t j = 0; j < blocks.GetSize(); j++) {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 CBC_ReedSolomonEncoder encode(CBC_ReedSolomonGF256::QRCodeFild); 935 CBC_ReedSolomonEncoder encode(CBC_ReedSolomonGF256::QRCodeFild);
934 encode.Init(); 936 encode.Init();
935 encode.Encode(&toEncode, numEcBytesInBlock, e); 937 encode.Encode(&toEncode, numEcBytesInBlock, e);
936 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); 938 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
937 CBC_CommonByteArray* ecBytes = new CBC_CommonByteArray(numEcBytesInBlock); 939 CBC_CommonByteArray* ecBytes = new CBC_CommonByteArray(numEcBytesInBlock);
938 for (int32_t j = 0; j < numEcBytesInBlock; j++) { 940 for (int32_t j = 0; j < numEcBytesInBlock; j++) {
939 ecBytes->Set(j, toEncode[numDataBytes + j]); 941 ecBytes->Set(j, toEncode[numDataBytes + j]);
940 } 942 }
941 return ecBytes; 943 return ecBytes;
942 } 944 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698