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