OLD | NEW |
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 2006 Jeremias Maerki in part, and ZXing Authors in part | 8 * Copyright 2006 Jeremias Maerki in part, and ZXing Authors in part |
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 "../barcode.h" | 23 #include "../barcode.h" |
24 #include "BC_PDF417Compaction.h" | 24 #include "BC_PDF417Compaction.h" |
25 #include "BC_PDF417BarcodeRow.h" | 25 #include "BC_PDF417BarcodeRow.h" |
26 #include "BC_PDF417BarcodeMatrix.h" | 26 #include "BC_PDF417BarcodeMatrix.h" |
27 #include "BC_PDF417ErrorCorrection.h" | 27 #include "BC_PDF417ErrorCorrection.h" |
28 #include "BC_PDF417HighLevelEncoder.h" | 28 #include "BC_PDF417HighLevelEncoder.h" |
29 #include "BC_PDF417.h" | 29 #include "BC_PDF417.h" |
30 FX_INT32 CBC_PDF417::START_PATTERN = 0x1fea8; | 30 int32_t CBC_PDF417::START_PATTERN = 0x1fea8; |
31 FX_INT32 CBC_PDF417::STOP_PATTERN = 0x3fa29; | 31 int32_t CBC_PDF417::STOP_PATTERN = 0x3fa29; |
32 FX_INT32 CBC_PDF417::CODEWORD_TABLE[][1000] = { | 32 int32_t CBC_PDF417::CODEWORD_TABLE[][1000] = { |
33 { | 33 { |
34 0x1d5c0, 0x1eaf0, 0x1f57c, 0x1d4e0, 0x1ea78, 0x1f53e, | 34 0x1d5c0, 0x1eaf0, 0x1f57c, 0x1d4e0, 0x1ea78, 0x1f53e, |
35 0x1a8c0, 0x1d470, 0x1a860, 0x15040, 0x1a830, 0x15020, | 35 0x1a8c0, 0x1d470, 0x1a860, 0x15040, 0x1a830, 0x15020, |
36 0x1adc0, 0x1d6f0, 0x1eb7c, 0x1ace0, 0x1d678, 0x1eb3e, | 36 0x1adc0, 0x1d6f0, 0x1eb7c, 0x1ace0, 0x1d678, 0x1eb3e, |
37 0x158c0, 0x1ac70, 0x15860, 0x15dc0, 0x1aef0, 0x1d77c, | 37 0x158c0, 0x1ac70, 0x15860, 0x15dc0, 0x1aef0, 0x1d77c, |
38 0x15ce0, 0x1ae78, 0x1d73e, 0x15c70, 0x1ae3c, 0x15ef0, | 38 0x15ce0, 0x1ae78, 0x1d73e, 0x15c70, 0x1ae3c, 0x15ef0, |
39 0x1af7c, 0x15e78, 0x1af3e, 0x15f7c, 0x1f5fa, 0x1d2e0, | 39 0x1af7c, 0x15e78, 0x1af3e, 0x15f7c, 0x1f5fa, 0x1d2e0, |
40 0x1e978, 0x1f4be, 0x1a4c0, 0x1d270, 0x1e93c, 0x1a460, | 40 0x1e978, 0x1f4be, 0x1a4c0, 0x1d270, 0x1e93c, 0x1a460, |
41 0x1d238, 0x14840, 0x1a430, 0x1d21c, 0x14820, 0x1a418, | 41 0x1d238, 0x14840, 0x1a430, 0x1d21c, 0x14820, 0x1a418, |
42 0x14810, 0x1a6e0, 0x1d378, 0x1e9be, 0x14cc0, 0x1a670, | 42 0x14810, 0x1a6e0, 0x1d378, 0x1e9be, 0x14cc0, 0x1a670, |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 CBC_PDF417::~CBC_PDF417() | 528 CBC_PDF417::~CBC_PDF417() |
529 { | 529 { |
530 if (m_barcodeMatrix) { | 530 if (m_barcodeMatrix) { |
531 delete m_barcodeMatrix; | 531 delete m_barcodeMatrix; |
532 } | 532 } |
533 } | 533 } |
534 CBC_BarcodeMatrix* CBC_PDF417::getBarcodeMatrix() | 534 CBC_BarcodeMatrix* CBC_PDF417::getBarcodeMatrix() |
535 { | 535 { |
536 return m_barcodeMatrix; | 536 return m_barcodeMatrix; |
537 } | 537 } |
538 void CBC_PDF417::generateBarcodeLogic(CFX_WideString msg, FX_INT32 errorCorrecti
onLevel, FX_INT32 &e) | 538 void CBC_PDF417::generateBarcodeLogic(CFX_WideString msg, int32_t errorCorrectio
nLevel, int32_t &e) |
539 { | 539 { |
540 FX_INT32 errorCorrectionCodeWords = CBC_PDF417ErrorCorrection::getErrorCorre
ctionCodewordCount(errorCorrectionLevel, e); | 540 int32_t errorCorrectionCodeWords = CBC_PDF417ErrorCorrection::getErrorCorrec
tionCodewordCount(errorCorrectionLevel, e); |
541 BC_EXCEPTION_CHECK_ReturnVoid(e); | 541 BC_EXCEPTION_CHECK_ReturnVoid(e); |
542 CFX_WideString highLevel = CBC_PDF417HighLevelEncoder::encodeHighLevel(msg,
m_compaction, e); | 542 CFX_WideString highLevel = CBC_PDF417HighLevelEncoder::encodeHighLevel(msg,
m_compaction, e); |
543 BC_EXCEPTION_CHECK_ReturnVoid(e); | 543 BC_EXCEPTION_CHECK_ReturnVoid(e); |
544 FX_INT32 sourceCodeWords = highLevel.GetLength(); | 544 int32_t sourceCodeWords = highLevel.GetLength(); |
545 CFX_Int32Array* dimension = determineDimensions(sourceCodeWords, errorCorrec
tionCodeWords, e); | 545 CFX_Int32Array* dimension = determineDimensions(sourceCodeWords, errorCorrec
tionCodeWords, e); |
546 BC_EXCEPTION_CHECK_ReturnVoid(e); | 546 BC_EXCEPTION_CHECK_ReturnVoid(e); |
547 FX_INT32 cols = dimension->GetAt(0); | 547 int32_t cols = dimension->GetAt(0); |
548 FX_INT32 rows = dimension->GetAt(1); | 548 int32_t rows = dimension->GetAt(1); |
549 delete dimension; | 549 delete dimension; |
550 FX_INT32 pad = getNumberOfPadCodewords(sourceCodeWords, errorCorrectionCodeW
ords, cols, rows); | 550 int32_t pad = getNumberOfPadCodewords(sourceCodeWords, errorCorrectionCodeWo
rds, cols, rows); |
551 if (sourceCodeWords + errorCorrectionCodeWords + 1 > 929) { | 551 if (sourceCodeWords + errorCorrectionCodeWords + 1 > 929) { |
552 e = BCExceptionEncodedMessageContainsTooManyCodeWords; | 552 e = BCExceptionEncodedMessageContainsTooManyCodeWords; |
553 return; | 553 return; |
554 } | 554 } |
555 FX_INT32 n = sourceCodeWords + pad + 1; | 555 int32_t n = sourceCodeWords + pad + 1; |
556 CFX_WideString sb; | 556 CFX_WideString sb; |
557 sb += (FX_WCHAR) n; | 557 sb += (FX_WCHAR) n; |
558 sb += highLevel; | 558 sb += highLevel; |
559 for (FX_INT32 i = 0; i < pad; i++) { | 559 for (int32_t i = 0; i < pad; i++) { |
560 sb += (FX_WCHAR) 900; | 560 sb += (FX_WCHAR) 900; |
561 } | 561 } |
562 CFX_WideString dataCodewords(sb); | 562 CFX_WideString dataCodewords(sb); |
563 CFX_WideString ec = CBC_PDF417ErrorCorrection::generateErrorCorrection(dataC
odewords, errorCorrectionLevel, e); | 563 CFX_WideString ec = CBC_PDF417ErrorCorrection::generateErrorCorrection(dataC
odewords, errorCorrectionLevel, e); |
564 BC_EXCEPTION_CHECK_ReturnVoid(e); | 564 BC_EXCEPTION_CHECK_ReturnVoid(e); |
565 CFX_WideString fullCodewords = dataCodewords + ec; | 565 CFX_WideString fullCodewords = dataCodewords + ec; |
566 m_barcodeMatrix = FX_NEW CBC_BarcodeMatrix(rows, cols); | 566 m_barcodeMatrix = FX_NEW CBC_BarcodeMatrix(rows, cols); |
567 encodeLowLevel(fullCodewords, cols, rows, errorCorrectionLevel, m_barcodeMat
rix); | 567 encodeLowLevel(fullCodewords, cols, rows, errorCorrectionLevel, m_barcodeMat
rix); |
568 } | 568 } |
569 void CBC_PDF417::setDimensions(FX_INT32 maxCols, FX_INT32 minCols, FX_INT32 maxR
ows, FX_INT32 minRows) | 569 void CBC_PDF417::setDimensions(int32_t maxCols, int32_t minCols, int32_t maxRows
, int32_t minRows) |
570 { | 570 { |
571 m_maxCols = maxCols; | 571 m_maxCols = maxCols; |
572 m_minCols = minCols; | 572 m_minCols = minCols; |
573 m_maxRows = maxRows; | 573 m_maxRows = maxRows; |
574 m_minRows = minRows; | 574 m_minRows = minRows; |
575 } | 575 } |
576 void CBC_PDF417::setCompaction(Compaction compaction) | 576 void CBC_PDF417::setCompaction(Compaction compaction) |
577 { | 577 { |
578 m_compaction = compaction; | 578 m_compaction = compaction; |
579 } | 579 } |
580 void CBC_PDF417::setCompact(FX_BOOL compact) | 580 void CBC_PDF417::setCompact(FX_BOOL compact) |
581 { | 581 { |
582 m_compact = compact; | 582 m_compact = compact; |
583 } | 583 } |
584 FX_INT32 CBC_PDF417::calculateNumberOfRows(FX_INT32 m, FX_INT32 k, FX_INT32 c) | 584 int32_t CBC_PDF417::calculateNumberOfRows(int32_t m, int32_t k, int32_t c) |
585 { | 585 { |
586 FX_INT32 r = ((m + 1 + k) / c) + 1; | 586 int32_t r = ((m + 1 + k) / c) + 1; |
587 if (c * r >= (m + 1 + k + c)) { | 587 if (c * r >= (m + 1 + k + c)) { |
588 r--; | 588 r--; |
589 } | 589 } |
590 return r; | 590 return r; |
591 } | 591 } |
592 FX_INT32 CBC_PDF417::getNumberOfPadCodewords(FX_INT32 m, FX_INT32 k, FX_INT32 c,
FX_INT32 r) | 592 int32_t CBC_PDF417::getNumberOfPadCodewords(int32_t m, int32_t k, int32_t c, int
32_t r) |
593 { | 593 { |
594 FX_INT32 n = c * r - k; | 594 int32_t n = c * r - k; |
595 return n > m + 1 ? n - m - 1 : 0; | 595 return n > m + 1 ? n - m - 1 : 0; |
596 } | 596 } |
597 void CBC_PDF417::encodeChar(FX_INT32 pattern, FX_INT32 len, CBC_BarcodeRow* logi
c) | 597 void CBC_PDF417::encodeChar(int32_t pattern, int32_t len, CBC_BarcodeRow* logic) |
598 { | 598 { |
599 FX_INT32 map = 1 << (len - 1); | 599 int32_t map = 1 << (len - 1); |
600 FX_BOOL last = ((pattern & map) != 0); | 600 FX_BOOL last = ((pattern & map) != 0); |
601 FX_INT32 width = 0; | 601 int32_t width = 0; |
602 for (FX_INT32 i = 0; i < len; i++) { | 602 for (int32_t i = 0; i < len; i++) { |
603 FX_BOOL black = ((pattern & map) != 0); | 603 FX_BOOL black = ((pattern & map) != 0); |
604 if (last == black) { | 604 if (last == black) { |
605 width++; | 605 width++; |
606 } else { | 606 } else { |
607 logic->addBar(last, width); | 607 logic->addBar(last, width); |
608 last = black; | 608 last = black; |
609 width = 1; | 609 width = 1; |
610 } | 610 } |
611 map >>= 1; | 611 map >>= 1; |
612 } | 612 } |
613 logic->addBar(last, width); | 613 logic->addBar(last, width); |
614 } | 614 } |
615 void CBC_PDF417::encodeLowLevel(CFX_WideString fullCodewords, FX_INT32 c, FX_INT
32 r, FX_INT32 errorCorrectionLevel, CBC_BarcodeMatrix* logic) | 615 void CBC_PDF417::encodeLowLevel(CFX_WideString fullCodewords, int32_t c, int32_t
r, int32_t errorCorrectionLevel, CBC_BarcodeMatrix* logic) |
616 { | 616 { |
617 FX_INT32 idx = 0; | 617 int32_t idx = 0; |
618 for (FX_INT32 y = 0; y < r; y++) { | 618 for (int32_t y = 0; y < r; y++) { |
619 FX_INT32 cluster = y % 3; | 619 int32_t cluster = y % 3; |
620 logic->startRow(); | 620 logic->startRow(); |
621 FX_INT32 a = START_PATTERN; | 621 int32_t a = START_PATTERN; |
622 encodeChar(START_PATTERN, 17, logic->getCurrentRow()); | 622 encodeChar(START_PATTERN, 17, logic->getCurrentRow()); |
623 FX_INT32 left; | 623 int32_t left; |
624 FX_INT32 right; | 624 int32_t right; |
625 if (cluster == 0) { | 625 if (cluster == 0) { |
626 left = (30 * (y / 3)) + ((r - 1) / 3); | 626 left = (30 * (y / 3)) + ((r - 1) / 3); |
627 right = (30 * (y / 3)) + (c - 1); | 627 right = (30 * (y / 3)) + (c - 1); |
628 } else if (cluster == 1) { | 628 } else if (cluster == 1) { |
629 left = (30 * (y / 3)) + (errorCorrectionLevel * 3) + ((r - 1) % 3); | 629 left = (30 * (y / 3)) + (errorCorrectionLevel * 3) + ((r - 1) % 3); |
630 right = (30 * (y / 3)) + ((r - 1) / 3); | 630 right = (30 * (y / 3)) + ((r - 1) / 3); |
631 } else { | 631 } else { |
632 left = (30 * (y / 3)) + (c - 1); | 632 left = (30 * (y / 3)) + (c - 1); |
633 right = (30 * (y / 3)) + (errorCorrectionLevel * 3) + ((r - 1) % 3); | 633 right = (30 * (y / 3)) + (errorCorrectionLevel * 3) + ((r - 1) % 3); |
634 } | 634 } |
635 FX_INT32 pattern = CODEWORD_TABLE[cluster][left]; | 635 int32_t pattern = CODEWORD_TABLE[cluster][left]; |
636 encodeChar(pattern, 17, logic->getCurrentRow()); | 636 encodeChar(pattern, 17, logic->getCurrentRow()); |
637 for (FX_INT32 x = 0; x < c; x++) { | 637 for (int32_t x = 0; x < c; x++) { |
638 pattern = CODEWORD_TABLE[cluster][fullCodewords.GetAt(idx)]; | 638 pattern = CODEWORD_TABLE[cluster][fullCodewords.GetAt(idx)]; |
639 encodeChar(pattern, 17, logic->getCurrentRow()); | 639 encodeChar(pattern, 17, logic->getCurrentRow()); |
640 idx++; | 640 idx++; |
641 } | 641 } |
642 if (m_compact) { | 642 if (m_compact) { |
643 encodeChar(STOP_PATTERN, 1, logic->getCurrentRow()); | 643 encodeChar(STOP_PATTERN, 1, logic->getCurrentRow()); |
644 } else { | 644 } else { |
645 pattern = CODEWORD_TABLE[cluster][right]; | 645 pattern = CODEWORD_TABLE[cluster][right]; |
646 encodeChar(pattern, 17, logic->getCurrentRow()); | 646 encodeChar(pattern, 17, logic->getCurrentRow()); |
647 encodeChar(STOP_PATTERN, 18, logic->getCurrentRow()); | 647 encodeChar(STOP_PATTERN, 18, logic->getCurrentRow()); |
648 } | 648 } |
649 } | 649 } |
650 } | 650 } |
651 CFX_Int32Array* CBC_PDF417::determineDimensions(FX_INT32 sourceCodeWords, FX_INT
32 errorCorrectionCodeWords, FX_INT32 &e) | 651 CFX_Int32Array* CBC_PDF417::determineDimensions(int32_t sourceCodeWords, int32_t
errorCorrectionCodeWords, int32_t &e) |
652 { | 652 { |
653 FX_FLOAT ratio = 0.0f; | 653 FX_FLOAT ratio = 0.0f; |
654 CFX_Int32Array* dimension = NULL; | 654 CFX_Int32Array* dimension = NULL; |
655 for (FX_INT32 cols = m_minCols; cols <= m_maxCols; cols++) { | 655 for (int32_t cols = m_minCols; cols <= m_maxCols; cols++) { |
656 FX_INT32 rows = calculateNumberOfRows(sourceCodeWords, errorCorrectionCo
deWords, cols); | 656 int32_t rows = calculateNumberOfRows(sourceCodeWords, errorCorrectionCod
eWords, cols); |
657 if (rows < m_minRows) { | 657 if (rows < m_minRows) { |
658 break; | 658 break; |
659 } | 659 } |
660 if (rows > m_maxRows) { | 660 if (rows > m_maxRows) { |
661 continue; | 661 continue; |
662 } | 662 } |
663 FX_FLOAT newRatio = ((17 * cols + 69) * DEFAULT_MODULE_WIDTH) / (rows *
HEIGHT); | 663 FX_FLOAT newRatio = ((17 * cols + 69) * DEFAULT_MODULE_WIDTH) / (rows *
HEIGHT); |
664 if (dimension != NULL && fabsf(newRatio - PREFERRED_RATIO) > fabsf(ratio
- PREFERRED_RATIO)) { | 664 if (dimension != NULL && fabsf(newRatio - PREFERRED_RATIO) > fabsf(ratio
- PREFERRED_RATIO)) { |
665 continue; | 665 continue; |
666 } | 666 } |
667 ratio = newRatio; | 667 ratio = newRatio; |
668 if (dimension) { | 668 if (dimension) { |
669 delete dimension; | 669 delete dimension; |
670 } | 670 } |
671 dimension = FX_NEW CFX_Int32Array; | 671 dimension = FX_NEW CFX_Int32Array; |
672 dimension->Add(cols); | 672 dimension->Add(cols); |
673 dimension->Add(rows); | 673 dimension->Add(rows); |
674 } | 674 } |
675 if (dimension == NULL) { | 675 if (dimension == NULL) { |
676 FX_INT32 rows = calculateNumberOfRows(sourceCodeWords, errorCorrectionCo
deWords, m_minCols); | 676 int32_t rows = calculateNumberOfRows(sourceCodeWords, errorCorrectionCod
eWords, m_minCols); |
677 if (rows < m_minRows) { | 677 if (rows < m_minRows) { |
678 dimension = FX_NEW CFX_Int32Array; | 678 dimension = FX_NEW CFX_Int32Array; |
679 dimension->Add(m_minCols); | 679 dimension->Add(m_minCols); |
680 dimension->Add(m_minRows); | 680 dimension->Add(m_minRows); |
681 } else if (rows >= 3 && rows <= 90) { | 681 } else if (rows >= 3 && rows <= 90) { |
682 dimension = FX_NEW CFX_Int32Array; | 682 dimension = FX_NEW CFX_Int32Array; |
683 dimension->Add(m_minCols); | 683 dimension->Add(m_minCols); |
684 dimension->Add(rows); | 684 dimension->Add(rows); |
685 } | 685 } |
686 } | 686 } |
687 if (dimension == NULL) { | 687 if (dimension == NULL) { |
688 e = BCExceptionUnableToFitMessageInColumns; | 688 e = BCExceptionUnableToFitMessageInColumns; |
689 return NULL; | 689 return NULL; |
690 } | 690 } |
691 return dimension; | 691 return dimension; |
692 } | 692 } |
OLD | NEW |