Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 1 | // Copyright 2016 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 | * Copyright 2011 ZXing authors |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | */ |
| 21 | |
| 22 | #include "xfa/fxbarcode/cbc_upca.h" |
| 23 | |
| 24 | #include "xfa/fxbarcode/BC_BinaryBitmap.h" |
| 25 | #include "xfa/fxbarcode/BC_BufferedImageLuminanceSource.h" |
| 26 | #include "xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.h" |
| 27 | #include "xfa/fxbarcode/oned/BC_OnedUPCAReader.h" |
| 28 | #include "xfa/fxbarcode/oned/BC_OnedUPCAWriter.h" |
| 29 | |
| 30 | CBC_UPCA::CBC_UPCA() { |
| 31 | m_pBCReader = (CBC_Reader*)new (CBC_OnedUPCAReader); |
| 32 | ((CBC_OnedUPCAReader*)m_pBCReader)->Init(); |
| 33 | m_pBCWriter = (CBC_Writer*)new (CBC_OnedUPCAWriter); |
| 34 | } |
| 35 | |
| 36 | CBC_UPCA::~CBC_UPCA() { |
| 37 | delete (m_pBCReader); |
| 38 | delete (m_pBCWriter); |
| 39 | } |
| 40 | |
| 41 | CFX_WideString CBC_UPCA::Preprocess(const CFX_WideStringC& contents) { |
| 42 | CFX_WideString encodeContents = |
| 43 | ((CBC_OnedUPCAWriter*)m_pBCWriter)->FilterContents(contents); |
| 44 | int32_t length = encodeContents.GetLength(); |
| 45 | if (length <= 11) { |
| 46 | for (int32_t i = 0; i < 11 - length; i++) |
| 47 | encodeContents = FX_WCHAR('0') + encodeContents; |
| 48 | |
| 49 | CFX_ByteString byteString = encodeContents.UTF8Encode(); |
| 50 | int32_t checksum = |
| 51 | ((CBC_OnedUPCAWriter*)m_pBCWriter)->CalcChecksum(byteString); |
| 52 | byteString += checksum - 0 + '0'; |
| 53 | encodeContents = byteString.UTF8Decode(); |
| 54 | } |
| 55 | if (length > 12) |
| 56 | encodeContents = encodeContents.Mid(0, 12); |
| 57 | |
| 58 | return encodeContents; |
| 59 | } |
| 60 | |
| 61 | FX_BOOL CBC_UPCA::Encode(const CFX_WideStringC& contents, |
| 62 | FX_BOOL isDevice, |
| 63 | int32_t& e) { |
| 64 | if (contents.IsEmpty()) { |
| 65 | e = BCExceptionNoContents; |
| 66 | return FALSE; |
| 67 | } |
| 68 | BCFORMAT format = BCFORMAT_UPC_A; |
| 69 | int32_t outWidth = 0; |
| 70 | int32_t outHeight = 0; |
| 71 | CFX_WideString encodeContents = Preprocess(contents); |
| 72 | CFX_ByteString byteString = encodeContents.UTF8Encode(); |
| 73 | m_renderContents = encodeContents; |
| 74 | ((CBC_OnedUPCAWriter*)m_pBCWriter)->Init(); |
| 75 | uint8_t* data = static_cast<CBC_OnedUPCAWriter*>(m_pBCWriter) |
| 76 | ->Encode(byteString, format, outWidth, outHeight, e); |
| 77 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 78 | ((CBC_OneDimWriter*)m_pBCWriter) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 79 | ->RenderResult(encodeContents.AsStringC(), data, outWidth, isDevice, e); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 80 | FX_Free(data); |
| 81 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 82 | return TRUE; |
| 83 | } |
| 84 | |
| 85 | FX_BOOL CBC_UPCA::RenderDevice(CFX_RenderDevice* device, |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame^] | 86 | const CFX_Matrix* matrix, |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 87 | int32_t& e) { |
| 88 | ((CBC_OneDimWriter*)m_pBCWriter) |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame^] | 89 | ->RenderDeviceResult(device, matrix, m_renderContents.AsStringC(), e); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 90 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 91 | return TRUE; |
| 92 | } |
| 93 | |
| 94 | FX_BOOL CBC_UPCA::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) { |
| 95 | ((CBC_OneDimWriter*)m_pBCWriter) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 96 | ->RenderBitmapResult(pOutBitmap, m_renderContents.AsStringC(), e); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 97 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 98 | return TRUE; |
| 99 | } |
| 100 | |
| 101 | CFX_WideString CBC_UPCA::Decode(uint8_t* buf, |
| 102 | int32_t width, |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame^] | 103 | int32_t height, |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 104 | int32_t& e) { |
| 105 | CFX_WideString str; |
| 106 | return str; |
| 107 | } |
| 108 | |
| 109 | CFX_WideString CBC_UPCA::Decode(CFX_DIBitmap* pBitmap, int32_t& e) { |
| 110 | CBC_BufferedImageLuminanceSource source(pBitmap); |
| 111 | CBC_GlobalHistogramBinarizer binarizer(&source); |
| 112 | CBC_BinaryBitmap bitmap(&binarizer); |
| 113 | CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e); |
| 114 | BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L"")); |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 115 | return CFX_WideString::FromUTF8(str.AsStringC()); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 116 | } |