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_ean13.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_OnedEAN13Reader.h" |
| 28 | #include "xfa/fxbarcode/oned/BC_OnedEAN13Writer.h" |
| 29 | |
| 30 | CBC_EAN13::CBC_EAN13() { |
| 31 | m_pBCReader = (CBC_Reader*)new (CBC_OnedEAN13Reader); |
| 32 | m_pBCWriter = (CBC_Writer*)new (CBC_OnedEAN13Writer); |
| 33 | } |
| 34 | |
| 35 | CBC_EAN13::~CBC_EAN13() { |
| 36 | delete (m_pBCReader); |
| 37 | delete (m_pBCWriter); |
| 38 | } |
| 39 | |
| 40 | CFX_WideString CBC_EAN13::Preprocess(const CFX_WideStringC& contents) { |
| 41 | CFX_WideString encodeContents = |
| 42 | ((CBC_OnedEAN13Writer*)m_pBCWriter)->FilterContents(contents); |
| 43 | int32_t length = encodeContents.GetLength(); |
| 44 | if (length <= 12) { |
| 45 | for (int32_t i = 0; i < 12 - length; i++) |
| 46 | encodeContents = FX_WCHAR('0') + encodeContents; |
| 47 | |
| 48 | CFX_ByteString byteString = encodeContents.UTF8Encode(); |
| 49 | int32_t checksum = |
| 50 | ((CBC_OnedEAN13Writer*)m_pBCWriter)->CalcChecksum(byteString); |
| 51 | byteString += checksum - 0 + '0'; |
| 52 | encodeContents = byteString.UTF8Decode(); |
| 53 | } |
| 54 | if (length > 13) |
| 55 | encodeContents = encodeContents.Mid(0, 13); |
| 56 | |
| 57 | return encodeContents; |
| 58 | } |
| 59 | |
| 60 | FX_BOOL CBC_EAN13::Encode(const CFX_WideStringC& contents, |
| 61 | FX_BOOL isDevice, |
| 62 | int32_t& e) { |
| 63 | if (contents.IsEmpty()) { |
| 64 | e = BCExceptionNoContents; |
| 65 | return FALSE; |
| 66 | } |
| 67 | BCFORMAT format = BCFORMAT_EAN_13; |
| 68 | int32_t outWidth = 0; |
| 69 | int32_t outHeight = 0; |
| 70 | CFX_WideString encodeContents = Preprocess(contents); |
| 71 | CFX_ByteString byteString = encodeContents.UTF8Encode(); |
| 72 | m_renderContents = encodeContents; |
| 73 | uint8_t* data = static_cast<CBC_OnedEAN13Writer*>(m_pBCWriter) |
| 74 | ->Encode(byteString, format, outWidth, outHeight, e); |
| 75 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 76 | ((CBC_OneDimWriter*)m_pBCWriter) |
| 77 | ->RenderResult(encodeContents, data, outWidth, isDevice, e); |
| 78 | FX_Free(data); |
| 79 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 80 | return TRUE; |
| 81 | } |
| 82 | |
| 83 | FX_BOOL CBC_EAN13::RenderDevice(CFX_RenderDevice* device, |
| 84 | const CFX_Matrix* matirx, |
| 85 | int32_t& e) { |
| 86 | ((CBC_OneDimWriter*)m_pBCWriter) |
| 87 | ->RenderDeviceResult(device, matirx, m_renderContents, e); |
| 88 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 89 | return TRUE; |
| 90 | } |
| 91 | |
| 92 | FX_BOOL CBC_EAN13::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) { |
| 93 | ((CBC_OneDimWriter*)m_pBCWriter) |
| 94 | ->RenderBitmapResult(pOutBitmap, m_renderContents, e); |
| 95 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 96 | return TRUE; |
| 97 | } |
| 98 | |
| 99 | CFX_WideString CBC_EAN13::Decode(uint8_t* buf, |
| 100 | int32_t width, |
| 101 | int32_t hight, |
| 102 | int32_t& e) { |
| 103 | CFX_WideString str; |
| 104 | return str; |
| 105 | } |
| 106 | |
| 107 | CFX_WideString CBC_EAN13::Decode(CFX_DIBitmap* pBitmap, int32_t& e) { |
| 108 | CBC_BufferedImageLuminanceSource source(pBitmap); |
| 109 | CBC_GlobalHistogramBinarizer binarizer(&source); |
| 110 | CBC_BinaryBitmap bitmap(&binarizer); |
| 111 | CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e); |
| 112 | BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L"")); |
| 113 | return CFX_WideString::FromUTF8(str, str.GetLength()); |
| 114 | } |