Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | |
| 7 | #include <algorithm> |
| 8 | |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 9 | #include "core/fxge/cfx_graphstatedata.h" |
| 10 | #include "core/fxge/cfx_pathdata.h" |
| 11 | #include "core/fxge/cfx_renderdevice.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 12 | #include "fxbarcode/BC_TwoDimWriter.h" |
| 13 | #include "fxbarcode/BC_Writer.h" |
| 14 | #include "fxbarcode/common/BC_CommonBitMatrix.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 15 | #include "third_party/base/numerics/safe_math.h" |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 16 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 17 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 18 | CBC_TwoDimWriter::CBC_TwoDimWriter() : m_iCorrectLevel(1), m_bFixedSize(true) {} |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 19 | |
| 20 | CBC_TwoDimWriter::~CBC_TwoDimWriter() {} |
| 21 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 22 | void CBC_TwoDimWriter::RenderDeviceResult(CFX_RenderDevice* device, |
| 23 | const CFX_Matrix* matrix) { |
| 24 | CFX_GraphStateData stateData; |
| 25 | CFX_PathData path; |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 26 | path.AppendRect(0, 0, (float)m_Width, (float)m_Height); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 27 | device->DrawPath(&path, matrix, &stateData, m_backgroundColor, |
| 28 | m_backgroundColor, FXFILL_ALTERNATE); |
| 29 | int32_t leftPos = 0; |
| 30 | int32_t topPos = 0; |
| 31 | if (m_bFixedSize) { |
| 32 | leftPos = (m_Width - m_output->GetWidth()) / 2; |
| 33 | topPos = (m_Height - m_output->GetHeight()) / 2; |
| 34 | } |
| 35 | CFX_Matrix matri = *matrix; |
| 36 | if (m_Width < m_output->GetWidth() && m_Height < m_output->GetHeight()) { |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 37 | CFX_Matrix matriScale((float)m_Width / (float)m_output->GetWidth(), 0.0, |
| 38 | 0.0, (float)m_Height / (float)m_output->GetHeight(), |
| 39 | 0.0, 0.0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 40 | matriScale.Concat(*matrix); |
| 41 | matri = matriScale; |
| 42 | } |
| 43 | for (int32_t x = 0; x < m_output->GetWidth(); x++) { |
| 44 | for (int32_t y = 0; y < m_output->GetHeight(); y++) { |
| 45 | CFX_PathData rect; |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 46 | rect.AppendRect((float)leftPos + x, (float)topPos + y, |
| 47 | (float)(leftPos + x + 1), (float)(topPos + y + 1)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 48 | if (m_output->Get(x, y)) { |
weili | db444d2 | 2016-06-02 15:48:15 -0700 | [diff] [blame] | 49 | CFX_GraphStateData data; |
| 50 | device->DrawPath(&rect, &matri, &data, m_barColor, 0, FXFILL_WINDING); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
thestig | 495bda1 | 2016-04-28 17:29:19 -0700 | [diff] [blame] | 55 | |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 56 | int32_t CBC_TwoDimWriter::GetErrorCorrectionLevel() const { |
weili | 29b8ad0 | 2016-06-14 18:20:04 -0700 | [diff] [blame] | 57 | return m_iCorrectLevel; |
| 58 | } |
| 59 | |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 60 | bool CBC_TwoDimWriter::RenderResult(uint8_t* code, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 61 | int32_t codeWidth, |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 62 | int32_t codeHeight) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 63 | int32_t inputWidth = codeWidth; |
| 64 | int32_t inputHeight = codeHeight; |
| 65 | int32_t tempWidth = inputWidth + 2; |
| 66 | int32_t tempHeight = inputHeight + 2; |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 67 | float moduleHSize = std::min(m_ModuleWidth, m_ModuleHeight); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 68 | moduleHSize = std::min(moduleHSize, 8.0f); |
| 69 | moduleHSize = std::max(moduleHSize, 1.0f); |
| 70 | pdfium::base::CheckedNumeric<int32_t> scaledWidth = tempWidth; |
| 71 | pdfium::base::CheckedNumeric<int32_t> scaledHeight = tempHeight; |
| 72 | scaledWidth *= moduleHSize; |
| 73 | scaledHeight *= moduleHSize; |
| 74 | |
| 75 | int32_t outputWidth = scaledWidth.ValueOrDie(); |
| 76 | int32_t outputHeight = scaledHeight.ValueOrDie(); |
| 77 | if (m_bFixedSize) { |
| 78 | if (m_Width < outputWidth || m_Height < outputHeight) { |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 79 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 80 | } |
| 81 | } else { |
| 82 | if (m_Width > outputWidth || m_Height > outputHeight) { |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 83 | outputWidth = |
| 84 | (int32_t)(outputWidth * ceil((float)m_Width / (float)outputWidth)); |
| 85 | outputHeight = |
| 86 | (int32_t)(outputHeight * ceil((float)m_Height / (float)outputHeight)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 89 | int32_t multiX = (int32_t)ceil((float)outputWidth / (float)tempWidth); |
| 90 | int32_t multiY = (int32_t)ceil((float)outputHeight / (float)tempHeight); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 91 | if (m_bFixedSize) { |
| 92 | multiX = std::min(multiX, multiY); |
| 93 | multiY = multiX; |
| 94 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 95 | int32_t leftPadding = std::max((outputWidth - (inputWidth * multiX)) / 2, 0); |
| 96 | int32_t topPadding = std::max((outputHeight - (inputHeight * multiY)) / 2, 0); |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 97 | m_output = pdfium::MakeUnique<CBC_CommonBitMatrix>(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 98 | m_output->Init(outputWidth, outputHeight); |
| 99 | for (int32_t inputY = 0, outputY = topPadding; |
| 100 | (inputY < inputHeight) && (outputY < outputHeight - multiY); |
| 101 | inputY++, outputY += multiY) { |
| 102 | for (int32_t inputX = 0, outputX = leftPadding; |
| 103 | (inputX < inputWidth) && (outputX < outputWidth - multiX); |
| 104 | inputX++, outputX += multiX) { |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 105 | if (code[inputX + inputY * inputWidth] == 1 && |
| 106 | !m_output->SetRegion(outputX, outputY, multiX, multiY)) { |
| 107 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 111 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 112 | } |