diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2016-03-14 14:14:16 -0400 |
---|---|---|
committer | Dan Sinclair <dsinclair@chromium.org> | 2016-03-14 14:14:16 -0400 |
commit | 1770c021cf998ff1b33855b1397f6ea8ff9f7cd7 (patch) | |
tree | 285e39abd4b5872d8cd632b9e331b0667fdc3eae /xfa/fxbarcode/common | |
parent | f766ad219f66543654520f6a1955836f519e26d1 (diff) | |
download | pdfium-1770c021cf998ff1b33855b1397f6ea8ff9f7cd7.tar.xz |
Move xfa/src up to xfa/.
This CL moves the xfa/src files up to the xfa/ directory and fixes the includes,
include guards, and build files.
R=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1803723002 .
Diffstat (limited to 'xfa/fxbarcode/common')
30 files changed, 2512 insertions, 0 deletions
diff --git a/xfa/fxbarcode/common/BC_CommonBitArray.cpp b/xfa/fxbarcode/common/BC_CommonBitArray.cpp new file mode 100644 index 0000000000..b39e4acd88 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonBitArray.cpp @@ -0,0 +1,113 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/utils.h" + +CBC_CommonBitArray::CBC_CommonBitArray(CBC_CommonBitArray* array) { + m_size = array->GetSize(); + m_bits.Copy(array->GetBits()); +} +CBC_CommonBitArray::CBC_CommonBitArray() { + m_bits.SetSize(1); + m_size = 0; +} +CBC_CommonBitArray::CBC_CommonBitArray(int32_t size) { + m_bits.SetSize((size + 31) >> 5); + m_size = size; +} +CBC_CommonBitArray::~CBC_CommonBitArray() { + m_size = 0; +} +int32_t CBC_CommonBitArray::GetSize() { + return m_size; +} +CFX_Int32Array& CBC_CommonBitArray::GetBits() { + return m_bits; +} +int32_t CBC_CommonBitArray::GetSizeInBytes() { + return (m_size + 7) >> 3; +} +FX_BOOL CBC_CommonBitArray::Get(int32_t i) { + return (m_bits[i >> 5] & (1 << (i & 0x1f))) != 0; +} +void CBC_CommonBitArray::Set(int32_t i) { + m_bits[i >> 5] |= 1 << (i & 0x1F); +} +void CBC_CommonBitArray::Flip(int32_t i) { + m_bits[i >> 5] ^= 1 << (i & 0x1F); +} +void CBC_CommonBitArray::SetBulk(int32_t i, int32_t newBits) { + m_bits[i >> 5] = newBits; +} +void CBC_CommonBitArray::Clear() { + FXSYS_memset(&m_bits[0], 0x00, m_bits.GetSize() * sizeof(int32_t)); +} +FX_BOOL CBC_CommonBitArray::IsRange(int32_t start, + int32_t end, + FX_BOOL value, + int32_t& e) { + if (end < start) { + e = BCExceptionEndLessThanStart; + return FALSE; + } + if (end == start) { + return TRUE; + } + end--; + int32_t firstInt = start >> 5; + int32_t lastInt = end >> 5; + int32_t i; + for (i = firstInt; i <= lastInt; i++) { + int32_t firstBit = i > firstInt ? 0 : start & 0x1F; + int32_t lastBit = i < lastInt ? 31 : end & 0x1F; + int32_t mask; + if (firstBit == 0 && lastBit == 31) { + mask = -1; + } else { + mask = 0; + for (int32_t j = firstBit; j <= lastBit; j++) { + mask |= 1 << j; + } + } + if ((m_bits[i] & mask) != (value ? mask : 0)) { + return FALSE; + } + } + return TRUE; +} +int32_t* CBC_CommonBitArray::GetBitArray() { + return &m_bits[0]; +} +void CBC_CommonBitArray::Reverse() { + int32_t* newBits = FX_Alloc(int32_t, m_bits.GetSize()); + FXSYS_memset(newBits, 0x00, m_bits.GetSize() * sizeof(int32_t)); + int32_t size = m_size; + int32_t i; + for (i = 0; i < size; i++) { + if (Get(size - i - 1)) { + newBits[i >> 5] |= 1 << (i & 0x1F); + } + } + FXSYS_memcpy(&m_bits[0], newBits, m_bits.GetSize() * sizeof(int32_t)); + FX_Free(newBits); +} diff --git a/xfa/fxbarcode/common/BC_CommonBitArray.h b/xfa/fxbarcode/common/BC_CommonBitArray.h new file mode 100644 index 0000000000..6ddd789480 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonBitArray.h @@ -0,0 +1,35 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_COMMONBITARRAY_H_ +#define XFA_FXBARCODE_COMMON_BC_COMMONBITARRAY_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_CommonBitArray { + public: + CBC_CommonBitArray(CBC_CommonBitArray* array); + CBC_CommonBitArray(int32_t size); + CBC_CommonBitArray(); + virtual ~CBC_CommonBitArray(); + int32_t GetSize(); + CFX_Int32Array& GetBits(); + int32_t GetSizeInBytes(); + FX_BOOL Get(int32_t i); + void Set(int32_t i); + void Flip(int32_t i); + void SetBulk(int32_t i, int32_t newBits); + FX_BOOL IsRange(int32_t start, int32_t end, FX_BOOL value, int32_t& e); + int32_t* GetBitArray(); + void Reverse(); + void Clear(); + + private: + int32_t m_size; + CFX_Int32Array m_bits; +}; + +#endif // XFA_FXBARCODE_COMMON_BC_COMMONBITARRAY_H_ diff --git a/xfa/fxbarcode/common/BC_CommonBitMatrix.cpp b/xfa/fxbarcode/common/BC_CommonBitMatrix.cpp new file mode 100644 index 0000000000..a8f87e7ec4 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonBitMatrix.cpp @@ -0,0 +1,146 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/utils.h" + +CBC_CommonBitMatrix::CBC_CommonBitMatrix() { + m_width = 0; + m_height = 0; + m_rowSize = 0; + m_bits = NULL; +} +void CBC_CommonBitMatrix::Init(int32_t dimension) { + m_width = dimension; + m_height = dimension; + int32_t rowSize = (m_height + 31) >> 5; + m_rowSize = rowSize; + m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); + FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); +} +void CBC_CommonBitMatrix::Init(int32_t width, int32_t height) { + m_width = width; + m_height = height; + int32_t rowSize = (width + 31) >> 5; + m_rowSize = rowSize; + m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); + FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); +} +CBC_CommonBitMatrix::~CBC_CommonBitMatrix() { + FX_Free(m_bits); +} +FX_BOOL CBC_CommonBitMatrix::Get(int32_t x, int32_t y) { + int32_t offset = y * m_rowSize + (x >> 5); + if (offset >= m_rowSize * m_height || offset < 0) { + return false; + } + return ((((FX_DWORD)m_bits[offset]) >> (x & 0x1f)) & 1) != 0; +} +int32_t* CBC_CommonBitMatrix::GetBits() { + return m_bits; +} +void CBC_CommonBitMatrix::Set(int32_t x, int32_t y) { + int32_t offset = y * m_rowSize + (x >> 5); + if (offset >= m_rowSize * m_height || offset < 0) { + return; + } + m_bits[offset] |= 1 << (x & 0x1f); +} +void CBC_CommonBitMatrix::Flip(int32_t x, int32_t y) { + int32_t offset = y * m_rowSize + (x >> 5); + m_bits[offset] ^= 1 << (x & 0x1f); +} +void CBC_CommonBitMatrix::Clear() { + FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); +} +void CBC_CommonBitMatrix::SetRegion(int32_t left, + int32_t top, + int32_t width, + int32_t height, + int32_t& e) { + if (top < 0 || left < 0) { + e = BCExceptionLeftAndTopMustBeNonnegative; + return; + } + if (height < 1 || width < 1) { + e = BCExceptionHeightAndWidthMustBeAtLeast1; + return; + } + int32_t right = left + width; + int32_t bottom = top + height; + if (m_height < bottom || m_width < right) { + e = BCExceptionRegionMustFitInsideMatrix; + return; + } + int32_t y; + for (y = top; y < bottom; y++) { + int32_t offset = y * m_rowSize; + int32_t x; + for (x = left; x < right; x++) { + m_bits[offset + (x >> 5)] |= 1 << (x & 0x1f); + } + } +} +CBC_CommonBitArray* CBC_CommonBitMatrix::GetRow(int32_t y, + CBC_CommonBitArray* row) { + CBC_CommonBitArray* rowArray = NULL; + if (row == NULL || row->GetSize() < m_width) { + rowArray = new CBC_CommonBitArray(m_width); + } else { + rowArray = new CBC_CommonBitArray(row); + } + int32_t offset = y * m_rowSize; + int32_t x; + for (x = 0; x < m_rowSize; x++) { + rowArray->SetBulk(x << 5, m_bits[offset + x]); + } + return rowArray; +} +void CBC_CommonBitMatrix::SetRow(int32_t y, CBC_CommonBitArray* row) { + int32_t l = y * m_rowSize; + for (int32_t i = 0; i < m_rowSize; i++) { + m_bits[l] = row->GetBitArray()[i]; + l++; + } +} +void CBC_CommonBitMatrix::SetCol(int32_t y, CBC_CommonBitArray* col) { + for (int32_t i = 0; i < col->GetBits().GetSize(); i++) { + m_bits[i * m_rowSize + y] = col->GetBitArray()[i]; + } +} +int32_t CBC_CommonBitMatrix::GetWidth() { + return m_width; +} +int32_t CBC_CommonBitMatrix::GetHeight() { + return m_height; +} +int32_t CBC_CommonBitMatrix::GetRowSize() { + return m_rowSize; +} +int32_t CBC_CommonBitMatrix::GetDimension(int32_t& e) { + if (m_width != m_height) { + e = BCExceptionCanNotCallGetDimensionOnNonSquareMatrix; + return 0; + } + return m_width; +} diff --git a/xfa/fxbarcode/common/BC_CommonBitMatrix.h b/xfa/fxbarcode/common/BC_CommonBitMatrix.h new file mode 100644 index 0000000000..78b42641dd --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonBitMatrix.h @@ -0,0 +1,46 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_COMMONBITMATRIX_H_ +#define XFA_FXBARCODE_COMMON_BC_COMMONBITMATRIX_H_ + +#include "core/include/fxcrt/fx_system.h" + +class CBC_CommonBitArray; + +class CBC_CommonBitMatrix { + public: + CBC_CommonBitMatrix(); + virtual ~CBC_CommonBitMatrix(); + FX_BOOL Get(int32_t x, int32_t y); + void Set(int32_t x, int32_t y); + void Flip(int32_t x, int32_t y); + void Clear(); + void SetRegion(int32_t left, + int32_t top, + int32_t width, + int32_t height, + int32_t& e); + CBC_CommonBitArray* GetRow(int32_t y, CBC_CommonBitArray* row); + void SetRow(int32_t y, CBC_CommonBitArray* row); + CBC_CommonBitArray* GetCol(int32_t y, CBC_CommonBitArray* row); + void SetCol(int32_t y, CBC_CommonBitArray* col); + int32_t GetWidth(); + int32_t GetHeight(); + int32_t GetRowSize(); + int32_t GetDimension(int32_t& e); + virtual void Init(int32_t dimension); + virtual void Init(int32_t width, int32_t height); + int32_t* GetBits(); + + private: + int32_t m_width; + int32_t m_height; + int32_t m_rowSize; + int32_t* m_bits; +}; + +#endif // XFA_FXBARCODE_COMMON_BC_COMMONBITMATRIX_H_ diff --git a/xfa/fxbarcode/common/BC_CommonBitSource.cpp b/xfa/fxbarcode/common/BC_CommonBitSource.cpp new file mode 100644 index 0000000000..af91d5e0bd --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonBitSource.cpp @@ -0,0 +1,71 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/BC_CommonBitSource.h" + +CBC_CommonBitSource::CBC_CommonBitSource(CFX_ByteArray* bytes) { + m_bytes.Copy((*bytes)); + m_bitOffset = 0; + m_byteOffset = 0; +} +CBC_CommonBitSource::~CBC_CommonBitSource() {} +int32_t CBC_CommonBitSource::ReadBits(int32_t numBits, int32_t& e) { + if (numBits < 1 || numBits > 32) { + e = BCExceptionIllegalArgument; + return 0; + } + int32_t result = 0; + if (m_bitOffset > 0) { + int32_t bitsLeft = 8 - m_bitOffset; + int32_t toRead = numBits < bitsLeft ? numBits : bitsLeft; + int32_t bitsToNotRead = bitsLeft - toRead; + int32_t mask = (0xff >> (8 - toRead)) << bitsToNotRead; + result = (m_bytes[m_byteOffset] & mask) >> bitsToNotRead; + numBits -= toRead; + m_bitOffset += toRead; + if (m_bitOffset == 8) { + m_bitOffset = 0; + m_byteOffset++; + } + } + if (numBits > 0) { + while (numBits >= 8) { + result = (result << 8) | (m_bytes[m_byteOffset] & 0xff); + m_byteOffset++; + numBits -= 8; + } + if (numBits > 0) { + int32_t bitsToNotRead = 8 - numBits; + int32_t mask = (0xff >> bitsToNotRead) << bitsToNotRead; + result = (result << numBits) | + ((m_bytes[m_byteOffset] & mask) >> bitsToNotRead); + m_bitOffset += numBits; + } + } + return result; +} +int32_t CBC_CommonBitSource::Available() { + return 8 * (m_bytes.GetSize() - m_byteOffset) - m_bitOffset; +} +int32_t CBC_CommonBitSource::getByteOffset() { + return m_byteOffset; +} diff --git a/xfa/fxbarcode/common/BC_CommonBitSource.h b/xfa/fxbarcode/common/BC_CommonBitSource.h new file mode 100644 index 0000000000..7617a6f19d --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonBitSource.h @@ -0,0 +1,27 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_COMMONBITSOURCE_H_ +#define XFA_FXBARCODE_COMMON_BC_COMMONBITSOURCE_H_ + +#include "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/utils.h" + +class CBC_CommonBitSource { + public: + CBC_CommonBitSource(CFX_ByteArray* bytes); + virtual ~CBC_CommonBitSource(); + int32_t ReadBits(int32_t numBits, int32_t& e); + int32_t Available(); + int32_t getByteOffset(); + + private: + CFX_ByteArray m_bytes; + int32_t m_byteOffset; + int32_t m_bitOffset; +}; + +#endif // XFA_FXBARCODE_COMMON_BC_COMMONBITSOURCE_H_ diff --git a/xfa/fxbarcode/common/BC_CommonByteArray.cpp b/xfa/fxbarcode/common/BC_CommonByteArray.cpp new file mode 100644 index 0000000000..1fd6e46420 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonByteArray.cpp @@ -0,0 +1,99 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <algorithm> + +#include "xfa/fxbarcode/common/BC_CommonByteArray.h" + +CBC_CommonByteArray::CBC_CommonByteArray() { + m_bytes = NULL; + m_size = 0; + m_index = 0; +} +CBC_CommonByteArray::CBC_CommonByteArray(int32_t size) { + m_size = size; + m_bytes = FX_Alloc(uint8_t, size); + FXSYS_memset(m_bytes, 0, size); + m_index = 0; +} +CBC_CommonByteArray::CBC_CommonByteArray(uint8_t* byteArray, int32_t size) { + m_size = size; + m_bytes = FX_Alloc(uint8_t, size); + FXSYS_memcpy(m_bytes, byteArray, size); + m_index = size; +} +CBC_CommonByteArray::~CBC_CommonByteArray() { + FX_Free(m_bytes); +} +int32_t CBC_CommonByteArray::At(int32_t index) { + return m_bytes[index] & 0xff; +} +void CBC_CommonByteArray::Set(int32_t index, int32_t value) { + m_bytes[index] = (uint8_t)value; +} +int32_t CBC_CommonByteArray::Size() { + return m_size; +} +FX_BOOL CBC_CommonByteArray::IsEmpty() { + return m_size == 0; +} +void CBC_CommonByteArray::AppendByte(int32_t value) { + if (m_size == 0 || m_index >= m_size) { + int32_t newSize = std::max(32, m_size << 1); + Reserve(newSize); + } + m_bytes[m_index] = (uint8_t)value; + m_index++; +} +void CBC_CommonByteArray::Reserve(int32_t capacity) { + if (m_bytes == NULL || m_size < capacity) { + uint8_t* newArray = FX_Alloc(uint8_t, capacity); + if (m_bytes) { + FXSYS_memcpy(newArray, m_bytes, m_size); + FXSYS_memset(newArray + m_size, 0, capacity - m_size); + } else { + FXSYS_memset(newArray, 0, capacity); + } + FX_Free(m_bytes); + m_bytes = newArray; + m_size = capacity; + } +} +void CBC_CommonByteArray::Set(uint8_t* source, int32_t offset, int32_t count) { + FX_Free(m_bytes); + m_bytes = FX_Alloc(uint8_t, count); + m_size = count; + FXSYS_memcpy(m_bytes, source + offset, count); + m_index = count; +} +void CBC_CommonByteArray::Set(CFX_ByteArray* source, + int32_t offset, + int32_t count) { + FX_Free(m_bytes); + m_bytes = FX_Alloc(uint8_t, count); + m_size = count; + int32_t i; + for (i = 0; i < count; i++) { + m_bytes[i] = source->operator[](i + offset); + } + m_index = m_size; +} diff --git a/xfa/fxbarcode/common/BC_CommonByteArray.h b/xfa/fxbarcode/common/BC_CommonByteArray.h new file mode 100644 index 0000000000..69d470b0e5 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonByteArray.h @@ -0,0 +1,33 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_COMMONBYTEARRAY_H_ +#define XFA_FXBARCODE_COMMON_BC_COMMONBYTEARRAY_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_CommonByteArray { + private: + int32_t m_size; + int32_t m_index; + uint8_t* m_bytes; + + public: + CBC_CommonByteArray(); + CBC_CommonByteArray(int32_t size); + CBC_CommonByteArray(uint8_t* byteArray, int32_t size); + virtual ~CBC_CommonByteArray(); + int32_t At(int32_t index); + void Set(int32_t index, int32_t value); + int32_t Size(); + FX_BOOL IsEmpty(); + void AppendByte(int32_t value); + void Reserve(int32_t capacity); + void Set(uint8_t* source, int32_t offset, int32_t count); + void Set(CFX_ByteArray* source, int32_t offset, int32_t count); +}; + +#endif // XFA_FXBARCODE_COMMON_BC_COMMONBYTEARRAY_H_ diff --git a/xfa/fxbarcode/common/BC_CommonByteMatrix.cpp b/xfa/fxbarcode/common/BC_CommonByteMatrix.cpp new file mode 100644 index 0000000000..c9ed7495c0 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonByteMatrix.cpp @@ -0,0 +1,64 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/include/fxcrt/fx_memory.h" +#include "xfa/fxbarcode/common/BC_CommonByteMatrix.h" + +CBC_CommonByteMatrix::CBC_CommonByteMatrix(int32_t width, int32_t height) { + m_height = height; + m_width = width; + m_bytes = NULL; +} +void CBC_CommonByteMatrix::Init() { + m_bytes = FX_Alloc2D(uint8_t, m_height, m_width); + FXSYS_memset(m_bytes, 0xff, m_height * m_width); +} +CBC_CommonByteMatrix::~CBC_CommonByteMatrix() { + FX_Free(m_bytes); +} +int32_t CBC_CommonByteMatrix::GetHeight() { + return m_height; +} +int32_t CBC_CommonByteMatrix::GetWidth() { + return m_width; +} +uint8_t CBC_CommonByteMatrix::Get(int32_t x, int32_t y) { + return m_bytes[y * m_width + x]; +} +void CBC_CommonByteMatrix::Set(int32_t x, int32_t y, int32_t value) { + m_bytes[y * m_width + x] = (uint8_t)value; +} +void CBC_CommonByteMatrix::Set(int32_t x, int32_t y, uint8_t value) { + m_bytes[y * m_width + x] = value; +} +void CBC_CommonByteMatrix::clear(uint8_t value) { + int32_t y; + for (y = 0; y < m_height; y++) { + int32_t x; + for (x = 0; x < m_width; x++) { + m_bytes[y * m_width + x] = value; + } + } +} +uint8_t* CBC_CommonByteMatrix::GetArray() { + return m_bytes; +} diff --git a/xfa/fxbarcode/common/BC_CommonByteMatrix.h b/xfa/fxbarcode/common/BC_CommonByteMatrix.h new file mode 100644 index 0000000000..271feef34f --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonByteMatrix.h @@ -0,0 +1,34 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_COMMONBYTEMATRIX_H_ +#define XFA_FXBARCODE_COMMON_BC_COMMONBYTEMATRIX_H_ + +#include <stdint.h> + +#include "core/include/fxcrt/fx_system.h" + +class CBC_CommonByteMatrix { + public: + CBC_CommonByteMatrix(int32_t width, int32_t height); + virtual ~CBC_CommonByteMatrix(); + int32_t GetHeight(); + int32_t GetWidth(); + uint8_t Get(int32_t x, int32_t y); + uint8_t* GetArray(); + + void Set(int32_t x, int32_t y, int32_t value); + void Set(int32_t x, int32_t y, uint8_t value); + void clear(uint8_t value); + virtual void Init(); + + private: + uint8_t* m_bytes; + int32_t m_width; + int32_t m_height; +}; + +#endif // XFA_FXBARCODE_COMMON_BC_COMMONBYTEMATRIX_H_ diff --git a/xfa/fxbarcode/common/BC_CommonCharacterSetECI.cpp b/xfa/fxbarcode/common/BC_CommonCharacterSetECI.cpp new file mode 100644 index 0000000000..d6fa9e472e --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonCharacterSetECI.cpp @@ -0,0 +1,44 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/BC_CommonCharacterSetECI.h" +#include "xfa/fxbarcode/common/BC_CommonECI.h" + +void CBC_CommonCharacterSetECI::initialize() {} +CBC_CommonCharacterSetECI::CBC_CommonCharacterSetECI( + int32_t value, + CFX_ByteString encodingName) + : CBC_CommonECI(value), m_encodingName(encodingName) {} +CBC_CommonCharacterSetECI::~CBC_CommonCharacterSetECI() {} +CFX_ByteString CBC_CommonCharacterSetECI::GetEncodingName() { + return m_encodingName; +} +void CBC_CommonCharacterSetECI::AddCharacterSet(int32_t value, + CFX_ByteString encodingName) {} +CBC_CommonCharacterSetECI* CBC_CommonCharacterSetECI::GetCharacterSetECIByValue( + int32_t value) { + return NULL; +} +CBC_CommonCharacterSetECI* CBC_CommonCharacterSetECI::GetCharacterSetECIByName( + const CFX_ByteString& name) { + return NULL; +} diff --git a/xfa/fxbarcode/common/BC_CommonCharacterSetECI.h b/xfa/fxbarcode/common/BC_CommonCharacterSetECI.h new file mode 100644 index 0000000000..18a676a449 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonCharacterSetECI.h @@ -0,0 +1,29 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_COMMONCHARACTERSETECI_H_ +#define XFA_FXBARCODE_COMMON_BC_COMMONCHARACTERSETECI_H_ + +#include "core/include/fxcrt/fx_string.h" +#include "xfa/fxbarcode/common/BC_CommonECI.h" + +class CBC_CommonCharacterSetECI : public CBC_CommonECI { + public: + CBC_CommonCharacterSetECI(int32_t value, CFX_ByteString encodingName); + virtual ~CBC_CommonCharacterSetECI(); + CFX_ByteString GetEncodingName(); + static void AddCharacterSet(int32_t value, CFX_ByteString encodingName); + int32_t GetValue(); + static CBC_CommonCharacterSetECI* GetCharacterSetECIByValue(int32_t value); + static CBC_CommonCharacterSetECI* GetCharacterSetECIByName( + const CFX_ByteString& name); + + private: + CFX_ByteString m_encodingName; + static void initialize(); +}; + +#endif // XFA_FXBARCODE_COMMON_BC_COMMONCHARACTERSETECI_H_ diff --git a/xfa/fxbarcode/common/BC_CommonDecoderResult.cpp b/xfa/fxbarcode/common/BC_CommonDecoderResult.cpp new file mode 100644 index 0000000000..0c545c4eba --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonDecoderResult.cpp @@ -0,0 +1,77 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/common/BC_CommonDecoderResult.h" +#include "xfa/fxbarcode/pdf417/BC_PDF417ResultMetadata.h" +#include "xfa/fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h" +#include "xfa/fxbarcode/utils.h" + +CBC_CommonDecoderResult::CBC_CommonDecoderResult() {} +void CBC_CommonDecoderResult::Init(const CFX_ByteArray& rawBytes, + const CFX_ByteString& text, + const CFX_Int32Array& byteSegments, + CBC_QRCoderErrorCorrectionLevel* ecLevel, + int32_t& e) { + if (text.IsEmpty()) { + e = BCExceptionIllegalArgument; + return; + } + m_rawBytes.Copy(rawBytes); + m_text = text; + m_byteSegments.Copy(byteSegments); + m_ecLevel = ecLevel; + m_other = NULL; +} +void CBC_CommonDecoderResult::Init(const CFX_ByteArray& rawBytes, + const CFX_ByteString& text, + const CFX_PtrArray& byteSegments, + const CFX_ByteString& ecLevel, + int32_t& e) { + if (text.IsEmpty()) { + e = BCExceptionIllegalArgument; + return; + } + m_rawBytes.Copy(rawBytes); + m_text = text; + m_pdf417byteSegments.Copy(byteSegments); + m_pdf417ecLevel = ecLevel; + m_other = NULL; +} +void CBC_CommonDecoderResult::setOther(CBC_PDF417ResultMetadata* other) { + m_other = other; +} +CBC_CommonDecoderResult::~CBC_CommonDecoderResult() { + delete m_other; +} +const CFX_ByteArray& CBC_CommonDecoderResult::GetRawBytes() { + return m_rawBytes; +} +const CFX_Int32Array& CBC_CommonDecoderResult::GetByteSegments() { + return m_byteSegments; +} +const CFX_ByteString& CBC_CommonDecoderResult::GetText() { + return m_text; +} +CBC_QRCoderErrorCorrectionLevel* CBC_CommonDecoderResult::GetECLevel() { + return m_ecLevel; +} diff --git a/xfa/fxbarcode/common/BC_CommonDecoderResult.h b/xfa/fxbarcode/common/BC_CommonDecoderResult.h new file mode 100644 index 0000000000..dbdb9d34d4 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonDecoderResult.h @@ -0,0 +1,45 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_COMMONDECODERRESULT_H_ +#define XFA_FXBARCODE_COMMON_BC_COMMONDECODERRESULT_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_QRCoderErrorCorrectionLevel; +class CBC_PDF417ResultMetadata; + +class CBC_CommonDecoderResult { + public: + CBC_CommonDecoderResult(); + virtual ~CBC_CommonDecoderResult(); + const CFX_ByteArray& GetRawBytes(); + const CFX_ByteString& GetText(); + const CFX_Int32Array& GetByteSegments(); + CBC_QRCoderErrorCorrectionLevel* GetECLevel(); + virtual void Init(const CFX_ByteArray& rawBytes, + const CFX_ByteString& text, + const CFX_Int32Array& byteSegments, + CBC_QRCoderErrorCorrectionLevel* ecLevel, + int32_t& e); + virtual void Init(const CFX_ByteArray& rawBytes, + const CFX_ByteString& text, + const CFX_PtrArray& byteSegments, + const CFX_ByteString& ecLevel, + int32_t& e); + void setOther(CBC_PDF417ResultMetadata* other); + + private: + CFX_ByteArray m_rawBytes; + CFX_ByteString m_text; + CFX_Int32Array m_byteSegments; + CFX_PtrArray m_pdf417byteSegments; + CBC_QRCoderErrorCorrectionLevel* m_ecLevel; + CFX_ByteString m_pdf417ecLevel; + CBC_PDF417ResultMetadata* m_other; +}; + +#endif // XFA_FXBARCODE_COMMON_BC_COMMONDECODERRESULT_H_ diff --git a/xfa/fxbarcode/common/BC_CommonECI.cpp b/xfa/fxbarcode/common/BC_CommonECI.cpp new file mode 100644 index 0000000000..4e37aec566 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonECI.cpp @@ -0,0 +1,42 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/BC_CommonCharacterSetECI.h" +#include "xfa/fxbarcode/common/BC_CommonECI.h" +#include "xfa/fxbarcode/utils.h" + +CBC_CommonECI::CBC_CommonECI(int32_t value) { + m_value = value; +} +CBC_CommonECI::~CBC_CommonECI() {} +int32_t CBC_CommonECI::GetValue() { + return m_value; +} +CBC_CommonECI* CBC_CommonECI::GetEICByValue(int32_t value, int32_t& e) { + if (value < 0 || value > 999999) { + e = BCExceptionBadECI; + return NULL; + } + if (value < 900) { + } + return NULL; +} diff --git a/xfa/fxbarcode/common/BC_CommonECI.h b/xfa/fxbarcode/common/BC_CommonECI.h new file mode 100644 index 0000000000..20b3e76bb3 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonECI.h @@ -0,0 +1,24 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_COMMONECI_H_ +#define XFA_FXBARCODE_COMMON_BC_COMMONECI_H_ + +#include <stdint.h> + +class CBC_CommonECI { + public: + CBC_CommonECI(int32_t value); + virtual ~CBC_CommonECI(); + + int32_t GetValue(); + static CBC_CommonECI* GetEICByValue(int32_t value, int32_t& e); + + private: + int32_t m_value; +}; + +#endif // XFA_FXBARCODE_COMMON_BC_COMMONECI_H_ diff --git a/xfa/fxbarcode/common/BC_CommonPerspectiveTransform.cpp b/xfa/fxbarcode/common/BC_CommonPerspectiveTransform.cpp new file mode 100644 index 0000000000..daa6189e56 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonPerspectiveTransform.cpp @@ -0,0 +1,152 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/BC_CommonPerspectiveTransform.h" + +#include <memory> + +#include "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/utils.h" + +CBC_CommonPerspectiveTransform::CBC_CommonPerspectiveTransform(FX_FLOAT a11, + FX_FLOAT a21, + FX_FLOAT a31, + FX_FLOAT a12, + FX_FLOAT a22, + FX_FLOAT a32, + FX_FLOAT a13, + FX_FLOAT a23, + FX_FLOAT a33) + : m_a11(a11), + m_a12(a12), + m_a13(a13), + m_a21(a21), + m_a22(a22), + m_a23(a23), + m_a31(a31), + m_a32(a32), + m_a33(a33) {} +CBC_CommonPerspectiveTransform::~CBC_CommonPerspectiveTransform() {} +CBC_CommonPerspectiveTransform* +CBC_CommonPerspectiveTransform::QuadrilateralToQuadrilateral(FX_FLOAT x0, + FX_FLOAT y0, + FX_FLOAT x1, + FX_FLOAT y1, + FX_FLOAT x2, + FX_FLOAT y2, + FX_FLOAT x3, + FX_FLOAT y3, + FX_FLOAT x0p, + FX_FLOAT y0p, + FX_FLOAT x1p, + FX_FLOAT y1p, + FX_FLOAT x2p, + FX_FLOAT y2p, + FX_FLOAT x3p, + FX_FLOAT y3p) { + std::unique_ptr<CBC_CommonPerspectiveTransform> qToS( + QuadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3)); + std::unique_ptr<CBC_CommonPerspectiveTransform> sToQ( + SquareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p)); + return sToQ->Times(*(qToS.get())); +} +void CBC_CommonPerspectiveTransform::TransformPoints(CFX_FloatArray* points) { + int32_t max = points->GetSize(); + FX_FLOAT a11 = m_a11; + FX_FLOAT a12 = m_a12; + FX_FLOAT a13 = m_a13; + FX_FLOAT a21 = m_a21; + FX_FLOAT a22 = m_a22; + FX_FLOAT a23 = m_a23; + FX_FLOAT a31 = m_a31; + FX_FLOAT a32 = m_a32; + FX_FLOAT a33 = m_a33; + int32_t i; + for (i = 0; i < max; i += 2) { + FX_FLOAT x = (*points)[i]; + FX_FLOAT y = (*points)[i + 1]; + FX_FLOAT denominator = a13 * x + a23 * y + a33; + (*points)[i] = (a11 * x + a21 * y + a31) / denominator; + (*points)[i + 1] = (a12 * x + a22 * y + a32) / denominator; + } +} +CBC_CommonPerspectiveTransform* +CBC_CommonPerspectiveTransform::SquareToQuadrilateral(FX_FLOAT x0, + FX_FLOAT y0, + FX_FLOAT x1, + FX_FLOAT y1, + FX_FLOAT x2, + FX_FLOAT y2, + FX_FLOAT x3, + FX_FLOAT y3) { + FX_FLOAT dy2 = y3 - y2; + FX_FLOAT dy3 = y0 - y1 + y2 - y3; + if ((dy2 == 0.0f) && (dy3 == 0.0f)) { + return new CBC_CommonPerspectiveTransform(x1 - x0, x2 - x1, x0, y1 - y0, + y2 - y1, y0, 0.0f, 0.0f, 1.0f); + } else { + FX_FLOAT dx1 = x1 - x2; + FX_FLOAT dx2 = x3 - x2; + FX_FLOAT dx3 = x0 - x1 + x2 - x3; + FX_FLOAT dy1 = y1 - y2; + FX_FLOAT denominator = dx1 * dy2 - dx2 * dy1; + FX_FLOAT a13 = (dx3 * dy2 - dx2 * dy3) / denominator; + FX_FLOAT a23 = (dx1 * dy3 - dx3 * dy1) / denominator; + return new CBC_CommonPerspectiveTransform( + x1 - x0 + a13 * x1, x3 - x0 + a23 * x3, x0, y1 - y0 + a13 * y1, + y3 - y0 + a23 * y3, y0, a13, a23, 1.0f); + } +} +CBC_CommonPerspectiveTransform* +CBC_CommonPerspectiveTransform::QuadrilateralToSquare(FX_FLOAT x0, + FX_FLOAT y0, + FX_FLOAT x1, + FX_FLOAT y1, + FX_FLOAT x2, + FX_FLOAT y2, + FX_FLOAT x3, + FX_FLOAT y3) { + std::unique_ptr<CBC_CommonPerspectiveTransform> temp1( + SquareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3)); + return temp1->BuildAdjoint(); +} +CBC_CommonPerspectiveTransform* CBC_CommonPerspectiveTransform::BuildAdjoint() { + return new CBC_CommonPerspectiveTransform( + m_a22 * m_a33 - m_a23 * m_a32, m_a23 * m_a31 - m_a21 * m_a33, + m_a21 * m_a32 - m_a22 * m_a31, m_a13 * m_a32 - m_a12 * m_a33, + m_a11 * m_a33 - m_a13 * m_a31, m_a12 * m_a31 - m_a11 * m_a32, + m_a12 * m_a23 - m_a13 * m_a22, m_a13 * m_a21 - m_a11 * m_a23, + m_a11 * m_a22 - m_a12 * m_a21); +} +CBC_CommonPerspectiveTransform* CBC_CommonPerspectiveTransform::Times( + CBC_CommonPerspectiveTransform& other) { + return new CBC_CommonPerspectiveTransform( + m_a11 * other.m_a11 + m_a21 * other.m_a12 + m_a31 * other.m_a13, + m_a11 * other.m_a21 + m_a21 * other.m_a22 + m_a31 * other.m_a23, + m_a11 * other.m_a31 + m_a21 * other.m_a32 + m_a31 * other.m_a33, + m_a12 * other.m_a11 + m_a22 * other.m_a12 + m_a32 * other.m_a13, + m_a12 * other.m_a21 + m_a22 * other.m_a22 + m_a32 * other.m_a23, + m_a12 * other.m_a31 + m_a22 * other.m_a32 + m_a32 * other.m_a33, + m_a13 * other.m_a11 + m_a23 * other.m_a12 + m_a33 * other.m_a13, + m_a13 * other.m_a21 + m_a23 * other.m_a22 + m_a33 * other.m_a23, + m_a13 * other.m_a31 + m_a23 * other.m_a32 + m_a33 * other.m_a33); +} diff --git a/xfa/fxbarcode/common/BC_CommonPerspectiveTransform.h b/xfa/fxbarcode/common/BC_CommonPerspectiveTransform.h new file mode 100644 index 0000000000..36defd8146 --- /dev/null +++ b/xfa/fxbarcode/common/BC_CommonPerspectiveTransform.h @@ -0,0 +1,65 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_COMMONPERSPECTIVETRANSFORM_H_ +#define XFA_FXBARCODE_COMMON_BC_COMMONPERSPECTIVETRANSFORM_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_CommonPerspectiveTransform { + public: + CBC_CommonPerspectiveTransform(FX_FLOAT a11, + FX_FLOAT a21, + FX_FLOAT a31, + FX_FLOAT a12, + FX_FLOAT a22, + FX_FLOAT a32, + FX_FLOAT a13, + FX_FLOAT a23, + FX_FLOAT a33); + virtual ~CBC_CommonPerspectiveTransform(); + static CBC_CommonPerspectiveTransform* QuadrilateralToQuadrilateral( + FX_FLOAT x0, + FX_FLOAT y0, + FX_FLOAT x1, + FX_FLOAT y1, + FX_FLOAT x2, + FX_FLOAT y2, + FX_FLOAT x3, + FX_FLOAT y3, + FX_FLOAT x0p, + FX_FLOAT y0p, + FX_FLOAT x1p, + FX_FLOAT y1p, + FX_FLOAT x2p, + FX_FLOAT y2p, + FX_FLOAT x3p, + FX_FLOAT y3p); + static CBC_CommonPerspectiveTransform* SquareToQuadrilateral(FX_FLOAT x0, + FX_FLOAT y0, + FX_FLOAT x1, + FX_FLOAT y1, + FX_FLOAT x2, + FX_FLOAT y2, + FX_FLOAT x3, + FX_FLOAT y3); + static CBC_CommonPerspectiveTransform* QuadrilateralToSquare(FX_FLOAT x0, + FX_FLOAT y0, + FX_FLOAT x1, + FX_FLOAT y1, + FX_FLOAT x2, + FX_FLOAT y2, + FX_FLOAT x3, + FX_FLOAT y3); + CBC_CommonPerspectiveTransform* BuildAdjoint(); + CBC_CommonPerspectiveTransform* Times(CBC_CommonPerspectiveTransform& other); + void TransformPoints(CFX_FloatArray* points); + + private: + FX_FLOAT m_a11, m_a12, m_a13, m_a21, m_a22, m_a23, m_a31, m_a32, m_a33; +}; + +#endif // XFA_FXBARCODE_COMMON_BC_COMMONPERSPECTIVETRANSFORM_H_ diff --git a/xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.cpp b/xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.cpp new file mode 100644 index 0000000000..d4a33229cf --- /dev/null +++ b/xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.cpp @@ -0,0 +1,173 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2009 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.h" + +#include <memory> + +#include "xfa/fxbarcode/BC_Binarizer.h" +#include "xfa/fxbarcode/BC_LuminanceSource.h" +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/utils.h" + +const int32_t LUMINANCE_BITS = 5; +const int32_t LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; +const int32_t LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; + +CBC_GlobalHistogramBinarizer::CBC_GlobalHistogramBinarizer( + CBC_LuminanceSource* source) + : CBC_Binarizer(source) {} +CBC_GlobalHistogramBinarizer::~CBC_GlobalHistogramBinarizer() {} +CBC_CommonBitArray* CBC_GlobalHistogramBinarizer::GetBlackRow( + int32_t y, + CBC_CommonBitArray* row, + int32_t& e) { + CBC_LuminanceSource* source = GetLuminanceSource(); + int32_t width = source->GetWidth(); + std::unique_ptr<CBC_CommonBitArray> result(new CBC_CommonBitArray(width)); + InitArrays(width); + CFX_ByteArray* localLuminances = source->GetRow(y, m_luminance, e); + if (e != BCExceptionNO) { + return result.release(); + } + CFX_Int32Array localBuckets; + localBuckets.Copy(m_buckets); + int32_t x; + for (x = 0; x < width; x++) { + int32_t pixel = (*localLuminances)[x] & 0xff; + localBuckets[pixel >> LUMINANCE_SHIFT]++; + } + int32_t blackPoint = EstimateBlackPoint(localBuckets, e); + if (e != BCExceptionNO) { + return result.release(); + } + int32_t left = (*localLuminances)[0] & 0xff; + int32_t center = (*localLuminances)[1] & 0xff; + for (x = 1; x < width - 1; x++) { + int32_t right = (*localLuminances)[x + 1] & 0xff; + int32_t luminance = ((center << 2) - left - right) >> 1; + if (luminance < blackPoint) { + result->Set(x); + } + left = center; + center = right; + } + return result.release(); +} +CBC_CommonBitMatrix* CBC_GlobalHistogramBinarizer::GetBlackMatrix(int32_t& e) { + CBC_LuminanceSource* source = GetLuminanceSource(); + int32_t width = source->GetWidth(); + int32_t height = source->GetHeight(); + std::unique_ptr<CBC_CommonBitMatrix> matrix(new CBC_CommonBitMatrix()); + matrix->Init(width, height); + InitArrays(width); + CFX_Int32Array localBuckets; + localBuckets.Copy(m_buckets); + int32_t y; + for (y = 1; y < 5; y++) { + int32_t row = height * y / 5; + CFX_ByteArray* localLuminances = source->GetRow(row, m_luminance, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + int32_t right = (width << 2) / 5; + int32_t x; + for (x = width / 5; x < right; x++) { + int32_t pixel = (*localLuminances)[x] & 0xff; + localBuckets[pixel >> LUMINANCE_SHIFT]++; + } + } + int32_t blackPoint = EstimateBlackPoint(localBuckets, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CFX_ByteArray> localLuminances(source->GetMatrix()); + for (y = 0; y < height; y++) { + int32_t offset = y * width; + for (int32_t x = 0; x < width; x++) { + int32_t pixel = (*localLuminances)[offset + x] & 0xff; + if (pixel < blackPoint) { + matrix->Set(x, y); + } + } + } + return matrix.release(); +} +void CBC_GlobalHistogramBinarizer::InitArrays(int32_t luminanceSize) { + if (m_luminance.GetSize() < luminanceSize) { + m_luminance.SetSize(luminanceSize); + } + if (m_buckets.GetSize() <= 0) { + m_buckets.SetSize(LUMINANCE_BUCKETS); + } else { + int32_t x; + for (x = 0; x < LUMINANCE_BUCKETS; x++) { + m_buckets[x] = 0; + } + } +} +int32_t CBC_GlobalHistogramBinarizer::EstimateBlackPoint( + CFX_Int32Array& buckets, + int32_t& e) { + int32_t numBuckets = buckets.GetSize(); + int32_t maxBucketCount = 0; + int32_t firstPeak = 0; + int32_t firstPeakSize = 0; + int32_t x; + for (x = 0; x < numBuckets; x++) { + if (buckets[x] > firstPeakSize) { + firstPeak = x; + firstPeakSize = buckets[x]; + } + if (buckets[x] > maxBucketCount) { + maxBucketCount = buckets[x]; + } + } + int32_t secondPeak = 0; + int32_t secondPeakScore = 0; + for (x = 0; x < numBuckets; x++) { + int32_t distanceToBiggest = x - firstPeak; + int32_t score = buckets[x] * distanceToBiggest * distanceToBiggest; + if (score > secondPeakScore) { + secondPeak = x; + secondPeakScore = score; + } + } + if (firstPeak > secondPeak) { + int32_t temp = firstPeak; + firstPeak = secondPeak; + secondPeak = temp; + } + if (secondPeak - firstPeak <= numBuckets >> 4) { + e = BCExceptionRead; + return 0; + } + int32_t bestValley = secondPeak - 1; + int32_t bestValleyScore = -1; + for (x = secondPeak - 1; x > firstPeak; x--) { + int32_t fromFirst = x - firstPeak; + int32_t score = fromFirst * fromFirst * (secondPeak - x) * + (maxBucketCount - buckets[x]); + if (score > bestValleyScore) { + bestValley = x; + bestValleyScore = score; + } + } + return bestValley << LUMINANCE_SHIFT; +} diff --git a/xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.h b/xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.h new file mode 100644 index 0000000000..da13cac402 --- /dev/null +++ b/xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.h @@ -0,0 +1,34 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_GLOBALHISTOGRAMBINARIZER_H_ +#define XFA_FXBARCODE_COMMON_BC_GLOBALHISTOGRAMBINARIZER_H_ + +#include "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/BC_Binarizer.h" + +class CBC_CommonBitArray; +class CBC_CommonBitMatrix; +class CBC_LuminanceSource; + +class CBC_GlobalHistogramBinarizer : public CBC_Binarizer { + public: + CBC_GlobalHistogramBinarizer(CBC_LuminanceSource* source); + virtual ~CBC_GlobalHistogramBinarizer(); + + void InitArrays(int32_t luminanceSize); + CBC_CommonBitMatrix* GetBlackMatrix(int32_t& e); + CBC_CommonBitArray* GetBlackRow(int32_t y, + CBC_CommonBitArray* row, + int32_t& e); + static int32_t EstimateBlackPoint(CFX_Int32Array& buckets, int32_t& e); + + private: + CFX_ByteArray m_luminance; + CFX_Int32Array m_buckets; +}; + +#endif // XFA_FXBARCODE_COMMON_BC_GLOBALHISTOGRAMBINARIZER_H_ diff --git a/xfa/fxbarcode/common/BC_WhiteRectangleDetector.cpp b/xfa/fxbarcode/common/BC_WhiteRectangleDetector.cpp new file mode 100644 index 0000000000..2aeacd1399 --- /dev/null +++ b/xfa/fxbarcode/common/BC_WhiteRectangleDetector.cpp @@ -0,0 +1,254 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2010 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/BC_WhiteRectangleDetector.h" + +#include <memory> + +#include "xfa/fxbarcode/BC_ResultPoint.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/utils.h" + +CBC_WhiteRectangleDetector::CBC_WhiteRectangleDetector( + CBC_CommonBitMatrix* image) { + m_image = image; + m_height = image->GetHeight(); + m_width = image->GetWidth(); + m_leftInit = (m_width - INIT_SIZE) >> 1; + m_rightInit = (m_width + INIT_SIZE) >> 1; + m_upInit = (m_height - INIT_SIZE) >> 1; + m_downInit = (m_height + INIT_SIZE) >> 1; +} +void CBC_WhiteRectangleDetector::Init(int32_t& e) { + if (m_upInit < 0 || m_leftInit < 0 || m_downInit >= m_height || + m_rightInit >= m_width) { + e = BCExceptionNotFound; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } +} +CBC_WhiteRectangleDetector::CBC_WhiteRectangleDetector( + CBC_CommonBitMatrix* image, + int32_t initSize, + int32_t x, + int32_t y) { + m_image = image; + m_height = image->GetHeight(); + m_width = image->GetWidth(); + int32_t halfsize = initSize >> 1; + m_leftInit = x - halfsize; + m_rightInit = x + halfsize; + m_upInit = y - halfsize; + m_downInit = y + halfsize; +} +CBC_WhiteRectangleDetector::~CBC_WhiteRectangleDetector() {} +CFX_PtrArray* CBC_WhiteRectangleDetector::Detect(int32_t& e) { + int32_t left = m_leftInit; + int32_t right = m_rightInit; + int32_t up = m_upInit; + int32_t down = m_downInit; + FX_BOOL sizeExceeded = FALSE; + FX_BOOL aBlackPointFoundOnBorder = TRUE; + FX_BOOL atLeastOneBlackPointFoundOnBorder = FALSE; + while (aBlackPointFoundOnBorder) { + aBlackPointFoundOnBorder = FALSE; + FX_BOOL rightBorderNotWhite = TRUE; + while (rightBorderNotWhite && right < m_width) { + rightBorderNotWhite = ContainsBlackPoint(up, down, right, FALSE); + if (rightBorderNotWhite) { + right++; + aBlackPointFoundOnBorder = TRUE; + } + } + if (right >= m_width) { + sizeExceeded = TRUE; + break; + } + FX_BOOL bottomBorderNotWhite = TRUE; + while (bottomBorderNotWhite && down < m_height) { + bottomBorderNotWhite = ContainsBlackPoint(left, right, down, TRUE); + if (bottomBorderNotWhite) { + down++; + aBlackPointFoundOnBorder = TRUE; + } + } + if (down >= m_height) { + sizeExceeded = TRUE; + break; + } + FX_BOOL leftBorderNotWhite = TRUE; + while (leftBorderNotWhite && left >= 0) { + leftBorderNotWhite = ContainsBlackPoint(up, down, left, FALSE); + if (leftBorderNotWhite) { + left--; + aBlackPointFoundOnBorder = TRUE; + } + } + if (left < 0) { + sizeExceeded = TRUE; + break; + } + FX_BOOL topBorderNotWhite = TRUE; + while (topBorderNotWhite && up >= 0) { + topBorderNotWhite = ContainsBlackPoint(left, right, up, TRUE); + if (topBorderNotWhite) { + up--; + aBlackPointFoundOnBorder = TRUE; + } + } + if (up < 0) { + sizeExceeded = TRUE; + break; + } + if (aBlackPointFoundOnBorder) { + atLeastOneBlackPointFoundOnBorder = TRUE; + } + } + if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) { + int32_t maxSize = right - left; + std::unique_ptr<CBC_ResultPoint> z; + for (int32_t i = 1; i < maxSize; i++) { + z.reset(GetBlackPointOnSegment((FX_FLOAT)left, (FX_FLOAT)(down - i), + (FX_FLOAT)(left + i), (FX_FLOAT)(down))); + if (z) + break; + } + if (z.get() == NULL) { + e = BCExceptionNotFound; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + std::unique_ptr<CBC_ResultPoint> t; + for (int32_t j = 1; j < maxSize; j++) { + t.reset(GetBlackPointOnSegment((FX_FLOAT)left, (FX_FLOAT)(up + j), + (FX_FLOAT)(left + j), (FX_FLOAT)up)); + if (t) + break; + } + if (t.get() == NULL) { + e = BCExceptionNotFound; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + std::unique_ptr<CBC_ResultPoint> x; + for (int32_t k = 1; k < maxSize; k++) { + x.reset(GetBlackPointOnSegment((FX_FLOAT)right, (FX_FLOAT)(up + k), + (FX_FLOAT)(right - k), (FX_FLOAT)up)); + if (x) + break; + } + if (x.get() == NULL) { + e = BCExceptionNotFound; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + std::unique_ptr<CBC_ResultPoint> y; + for (int32_t m = 1; m < maxSize; m++) { + y.reset(GetBlackPointOnSegment((FX_FLOAT)right, (FX_FLOAT)(down - m), + (FX_FLOAT)(right - m), (FX_FLOAT)down)); + if (y) + break; + } + if (y.get() == NULL) { + e = BCExceptionNotFound; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + return CenterEdges(y.get(), z.get(), x.get(), t.get()); + } else { + e = BCExceptionNotFound; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + return NULL; +} +int32_t CBC_WhiteRectangleDetector::Round(FX_FLOAT d) { + return (int32_t)(d + 0.5f); +} +CBC_ResultPoint* CBC_WhiteRectangleDetector::GetBlackPointOnSegment( + FX_FLOAT aX, + FX_FLOAT aY, + FX_FLOAT bX, + FX_FLOAT bY) { + int32_t dist = DistanceL2(aX, aY, bX, bY); + float xStep = (bX - aX) / dist; + float yStep = (bY - aY) / dist; + for (int32_t i = 0; i < dist; i++) { + int32_t x = Round(aX + i * xStep); + int32_t y = Round(aY + i * yStep); + if (m_image->Get(x, y)) { + return new CBC_ResultPoint((FX_FLOAT)x, (FX_FLOAT)y); + } + } + return NULL; +} +int32_t CBC_WhiteRectangleDetector::DistanceL2(FX_FLOAT aX, + FX_FLOAT aY, + FX_FLOAT bX, + FX_FLOAT bY) { + float xDiff = aX - bX; + float yDiff = aY - bY; + return Round((float)sqrt(xDiff * xDiff + yDiff * yDiff)); +} +CFX_PtrArray* CBC_WhiteRectangleDetector::CenterEdges(CBC_ResultPoint* y, + CBC_ResultPoint* z, + CBC_ResultPoint* x, + CBC_ResultPoint* t) { + float yi = y->GetX(); + float yj = y->GetY(); + float zi = z->GetX(); + float zj = z->GetY(); + float xi = x->GetX(); + float xj = x->GetY(); + float ti = t->GetX(); + float tj = t->GetY(); + if (yi < m_width / 2) { + CFX_PtrArray* result = new CFX_PtrArray; + result->SetSize(4); + (*result)[0] = new CBC_ResultPoint(ti - CORR, tj + CORR); + (*result)[1] = new CBC_ResultPoint(zi + CORR, zj + CORR); + (*result)[2] = new CBC_ResultPoint(xi - CORR, xj - CORR); + (*result)[3] = new CBC_ResultPoint(yi + CORR, yj - CORR); + return result; + } else { + CFX_PtrArray* result = new CFX_PtrArray; + result->SetSize(4); + (*result)[0] = new CBC_ResultPoint(ti + CORR, tj + CORR); + (*result)[1] = new CBC_ResultPoint(zi + CORR, zj - CORR); + (*result)[2] = new CBC_ResultPoint(xi - CORR, xj + CORR); + (*result)[3] = new CBC_ResultPoint(yi - CORR, yj - CORR); + return result; + } +} +FX_BOOL CBC_WhiteRectangleDetector::ContainsBlackPoint(int32_t a, + int32_t b, + int32_t fixed, + FX_BOOL horizontal) { + if (horizontal) { + for (int32_t x = a; x <= b; x++) { + if (m_image->Get(x, fixed)) { + return TRUE; + } + } + } else { + for (int32_t y = a; y <= b; y++) { + if (m_image->Get(fixed, y)) { + return TRUE; + } + } + } + return FALSE; +} diff --git a/xfa/fxbarcode/common/BC_WhiteRectangleDetector.h b/xfa/fxbarcode/common/BC_WhiteRectangleDetector.h new file mode 100644 index 0000000000..3d6c01ceff --- /dev/null +++ b/xfa/fxbarcode/common/BC_WhiteRectangleDetector.h @@ -0,0 +1,54 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_BC_WHITERECTANGLEDETECTOR_H_ +#define XFA_FXBARCODE_COMMON_BC_WHITERECTANGLEDETECTOR_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_CommonBitMatrix; +class CBC_ResultPoint; + +class CBC_WhiteRectangleDetector { + public: + CBC_WhiteRectangleDetector(CBC_CommonBitMatrix* image); + CBC_WhiteRectangleDetector(CBC_CommonBitMatrix* image, + int32_t initSize, + int32_t x, + int32_t y); + virtual ~CBC_WhiteRectangleDetector(); + CFX_PtrArray* Detect(int32_t& e); + virtual void Init(int32_t& e); + + private: + int32_t Round(float d); + CBC_ResultPoint* GetBlackPointOnSegment(FX_FLOAT aX, + FX_FLOAT aY, + FX_FLOAT bX, + FX_FLOAT bY); + int32_t DistanceL2(FX_FLOAT aX, FX_FLOAT aY, FX_FLOAT bX, FX_FLOAT bY); + CFX_PtrArray* CenterEdges(CBC_ResultPoint* y, + CBC_ResultPoint* z, + CBC_ResultPoint* x, + CBC_ResultPoint* t); + FX_BOOL ContainsBlackPoint(int32_t a, + int32_t b, + int32_t fixed, + FX_BOOL horizontal); + + static const int32_t INIT_SIZE = 30; + static const int32_t CORR = 1; + + CBC_CommonBitMatrix* m_image; + int32_t m_height; + int32_t m_width; + int32_t m_leftInit; + int32_t m_rightInit; + int32_t m_downInit; + int32_t m_upInit; +}; + +#endif // XFA_FXBARCODE_COMMON_BC_WHITERECTANGLEDETECTOR_H_ diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp new file mode 100644 index 0000000000..4d986cca26 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp @@ -0,0 +1,104 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.h" + +#include <memory> + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" + +CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field) { + m_field = field; +} +void CBC_ReedSolomonEncoder::Init() { + m_cachedGenerators.Add(new CBC_ReedSolomonGF256Poly(m_field, 1)); +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(int32_t degree, + int32_t& e) { + if (degree >= m_cachedGenerators.GetSize()) { + CBC_ReedSolomonGF256Poly* lastGenerator = + (CBC_ReedSolomonGF256Poly*)(m_cachedGenerators + [m_cachedGenerators.GetSize() - 1]); + for (int32_t d = m_cachedGenerators.GetSize(); d <= degree; d++) { + CFX_Int32Array temp; + temp.Add(1); + temp.Add(m_field->Exp(d - 1)); + CBC_ReedSolomonGF256Poly temp_poly; + temp_poly.Init(m_field, &temp, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_ReedSolomonGF256Poly* nextGenerator = + lastGenerator->Multiply(&temp_poly, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + m_cachedGenerators.Add(nextGenerator); + lastGenerator = nextGenerator; + } + } + return (CBC_ReedSolomonGF256Poly*)(m_cachedGenerators[degree]); +} +void CBC_ReedSolomonEncoder::Encode(CFX_Int32Array* toEncode, + int32_t ecBytes, + int32_t& e) { + if (ecBytes == 0) { + e = BCExceptionNoCorrectionBytes; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + int32_t dataBytes = toEncode->GetSize() - ecBytes; + if (dataBytes <= 0) { + e = BCExceptionNoDataBytesProvided; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CFX_Int32Array infoCoefficients; + infoCoefficients.SetSize(dataBytes); + for (int32_t x = 0; x < dataBytes; x++) { + infoCoefficients[x] = toEncode->operator[](x); + } + CBC_ReedSolomonGF256Poly info; + info.Init(m_field, &infoCoefficients, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CBC_ReedSolomonGF256Poly> infoTemp( + info.MultiplyByMonomial(ecBytes, 1, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CFX_PtrArray> temp(infoTemp->Divide(generator, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_ReedSolomonGF256Poly* remainder = + (CBC_ReedSolomonGF256Poly*)(temp->operator[](1)); + CFX_Int32Array* coefficients = remainder->GetCoefficients(); + int32_t numZeroCoefficients = ecBytes - coefficients->GetSize(); + for (int32_t i = 0; i < numZeroCoefficients; i++) { + (*toEncode)[dataBytes + i] = 0; + } + for (int32_t y = 0; y < coefficients->GetSize(); y++) { + (*toEncode)[dataBytes + numZeroCoefficients + y] = + coefficients->operator[](y); + } + for (int32_t k = 0; k < temp->GetSize(); k++) { + delete (CBC_ReedSolomonGF256Poly*)(*temp)[k]; + } +} +CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() { + for (int32_t i = 0; i < m_cachedGenerators.GetSize(); i++) { + delete (CBC_ReedSolomonGF256Poly*)m_cachedGenerators[i]; + } +} diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.h b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.h new file mode 100644 index 0000000000..6f6aef7e7d --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.h @@ -0,0 +1,29 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMON_H_ +#define XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMON_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_ReedSolomonGF256; +class CBC_ReedSolomonGF256Poly; + +class CBC_ReedSolomonEncoder { + private: + CBC_ReedSolomonGF256* m_field; + CFX_PtrArray m_cachedGenerators; + CBC_ReedSolomonGF256Poly* BuildGenerator(int32_t degree, int32_t& e); + + public: + CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field); + virtual ~CBC_ReedSolomonEncoder(); + + void Encode(CFX_Int32Array* toEncode, int32_t ecBytes, int32_t& e); + virtual void Init(); +}; + +#endif // XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMON_H_ diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp new file mode 100644 index 0000000000..ff91c9ddc3 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp @@ -0,0 +1,221 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h" + +#include <memory> +#include <utility> + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" + +CBC_ReedSolomonDecoder::CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field) { + m_field = field; +} +CBC_ReedSolomonDecoder::~CBC_ReedSolomonDecoder() {} +void CBC_ReedSolomonDecoder::Decode(CFX_Int32Array* received, + int32_t twoS, + int32_t& e) { + CBC_ReedSolomonGF256Poly poly; + poly.Init(m_field, received, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CFX_Int32Array syndromeCoefficients; + syndromeCoefficients.SetSize(twoS); + FX_BOOL dataMatrix = FALSE; + FX_BOOL noError = TRUE; + for (int32_t i = 0; i < twoS; i++) { + int32_t eval = poly.EvaluateAt(m_field->Exp(dataMatrix ? i + 1 : i)); + syndromeCoefficients[twoS - 1 - i] = eval; + if (eval != 0) { + noError = FALSE; + } + } + if (noError) { + return; + } + CBC_ReedSolomonGF256Poly syndrome; + syndrome.Init(m_field, &syndromeCoefficients, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CBC_ReedSolomonGF256Poly> temp( + m_field->BuildMonomial(twoS, 1, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CFX_PtrArray> sigmaOmega( + RunEuclideanAlgorithm(temp.get(), &syndrome, twoS, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CBC_ReedSolomonGF256Poly> sigma( + (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[0]); + std::unique_ptr<CBC_ReedSolomonGF256Poly> omega( + (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[1]); + std::unique_ptr<CFX_Int32Array> errorLocations( + FindErrorLocations(sigma.get(), e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CFX_Int32Array> errorMagnitudes( + FindErrorMagnitudes(omega.get(), errorLocations.get(), dataMatrix, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + for (int32_t k = 0; k < errorLocations->GetSize(); k++) { + int32_t position = + received->GetSize() - 1 - m_field->Log((*errorLocations)[k], e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + if (position < 0) { + e = BCExceptionBadErrorLocation; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + (*received)[position] = CBC_ReedSolomonGF256::AddOrSubtract( + (*received)[position], (*errorMagnitudes)[k]); + } +} +CFX_PtrArray* CBC_ReedSolomonDecoder::RunEuclideanAlgorithm( + CBC_ReedSolomonGF256Poly* a, + CBC_ReedSolomonGF256Poly* b, + int32_t R, + int32_t& e) { + if (a->GetDegree() < b->GetDegree()) { + CBC_ReedSolomonGF256Poly* temp = a; + a = b; + b = temp; + } + std::unique_ptr<CBC_ReedSolomonGF256Poly> rLast(a->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> r(b->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> sLast(m_field->GetOne()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> s(m_field->GetZero()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> tLast(m_field->GetZero()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> t(m_field->GetOne()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + while (r->GetDegree() >= R / 2) { + std::unique_ptr<CBC_ReedSolomonGF256Poly> rLastLast = std::move(rLast); + std::unique_ptr<CBC_ReedSolomonGF256Poly> sLastLast = std::move(sLast); + std::unique_ptr<CBC_ReedSolomonGF256Poly> tLastlast = std::move(tLast); + rLast = std::move(r); + sLast = std::move(s); + tLast = std::move(t); + if (rLast->IsZero()) { + e = BCExceptionR_I_1IsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + r.reset(rLastLast->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> q(m_field->GetZero()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + int32_t denominatorLeadingTerm = rLast->GetCoefficients(rLast->GetDegree()); + int32_t dltInverse = m_field->Inverse(denominatorLeadingTerm, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + while (r->GetDegree() >= rLast->GetDegree() && !(r->IsZero())) { + int32_t degreeDiff = r->GetDegree() - rLast->GetDegree(); + int32_t scale = + m_field->Multiply(r->GetCoefficients(r->GetDegree()), dltInverse); + std::unique_ptr<CBC_ReedSolomonGF256Poly> build( + m_field->BuildMonomial(degreeDiff, scale, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + q.reset(q->AddOrSubtract(build.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> multiply( + rLast->MultiplyByMonomial(degreeDiff, scale, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + r.reset(r->AddOrSubtract(multiply.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + std::unique_ptr<CBC_ReedSolomonGF256Poly> temp1( + q->Multiply(sLast.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + s.reset(temp1->AddOrSubtract(sLastLast.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> temp5( + q->Multiply(tLast.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + t.reset(temp5->AddOrSubtract(tLastlast.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + int32_t sigmaTildeAtZero = t->GetCoefficients(0); + if (sigmaTildeAtZero == 0) { + e = BCExceptionIsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + int32_t inverse = m_field->Inverse(sigmaTildeAtZero, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> sigma(t->Multiply(inverse, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> omega(r->Multiply(inverse, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CFX_PtrArray* temp = new CFX_PtrArray; + temp->Add(sigma.release()); + temp->Add(omega.release()); + return temp; +} +CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorLocations( + CBC_ReedSolomonGF256Poly* errorLocator, + int32_t& e) { + int32_t numErrors = errorLocator->GetDegree(); + if (numErrors == 1) { + std::unique_ptr<CFX_Int32Array> temp(new CFX_Int32Array); + temp->Add(errorLocator->GetCoefficients(1)); + return temp.release(); + } + CFX_Int32Array* tempT = new CFX_Int32Array; + tempT->SetSize(numErrors); + std::unique_ptr<CFX_Int32Array> result(tempT); + int32_t ie = 0; + for (int32_t i = 1; i < 256 && ie < numErrors; i++) { + if (errorLocator->EvaluateAt(i) == 0) { + (*result)[ie] = m_field->Inverse(i, ie); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + ie++; + } + } + if (ie != numErrors) { + e = BCExceptionDegreeNotMatchRoots; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + return result.release(); +} +CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorMagnitudes( + CBC_ReedSolomonGF256Poly* errorEvaluator, + CFX_Int32Array* errorLocations, + FX_BOOL dataMatrix, + int32_t& e) { + int32_t s = errorLocations->GetSize(); + CFX_Int32Array* temp = new CFX_Int32Array; + temp->SetSize(s); + std::unique_ptr<CFX_Int32Array> result(temp); + for (int32_t i = 0; i < s; i++) { + int32_t xiInverse = m_field->Inverse(errorLocations->operator[](i), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + int32_t denominator = 1; + for (int32_t j = 0; j < s; j++) { + if (i != j) { + denominator = m_field->Multiply( + denominator, CBC_ReedSolomonGF256::AddOrSubtract( + 1, m_field->Multiply(errorLocations->operator[](j), + xiInverse))); + } + } + int32_t temp = m_field->Inverse(denominator, temp); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + (*result)[i] = + m_field->Multiply(errorEvaluator->EvaluateAt(xiInverse), temp); + } + return result.release(); +} diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h new file mode 100644 index 0000000000..ec14344fe0 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h @@ -0,0 +1,35 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONDECODER_H_ +#define XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONDECODER_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_ReedSolomonGF256; +class CBC_ReedSolomonGF256Poly; + +class CBC_ReedSolomonDecoder { + private: + CBC_ReedSolomonGF256* m_field; + + public: + CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field); + virtual ~CBC_ReedSolomonDecoder(); + void Decode(CFX_Int32Array* received, int32_t twoS, int32_t& e); + CFX_PtrArray* RunEuclideanAlgorithm(CBC_ReedSolomonGF256Poly* a, + CBC_ReedSolomonGF256Poly* b, + int32_t R, + int32_t& e); + CFX_Int32Array* FindErrorLocations(CBC_ReedSolomonGF256Poly* errorLocator, + int32_t& e); + CFX_Int32Array* FindErrorMagnitudes(CBC_ReedSolomonGF256Poly* errorEvaluator, + CFX_Int32Array* errorLocations, + FX_BOOL dataMatrix, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONDECODER_H_ diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp new file mode 100644 index 0000000000..5e9fd33ac4 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp @@ -0,0 +1,124 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" + +CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeFild = NULL; +CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = NULL; +void CBC_ReedSolomonGF256::Initialize() { + QRCodeFild = new CBC_ReedSolomonGF256(0x011D); + QRCodeFild->Init(); + DataMatrixField = new CBC_ReedSolomonGF256(0x012D); + DataMatrixField->Init(); +} +void CBC_ReedSolomonGF256::Finalize() { + if (QRCodeFild) { + delete QRCodeFild; + } + QRCodeFild = NULL; + if (DataMatrixField) { + delete DataMatrixField; + } + DataMatrixField = NULL; +} +CBC_ReedSolomonGF256::CBC_ReedSolomonGF256(int32_t primitive) { + int32_t x = 1; + for (int32_t j = 0; j < 256; j++) { + m_expTable[j] = x; + x <<= 1; + if (x >= 0x100) { + x ^= primitive; + } + } + for (int32_t i = 0; i < 255; i++) { + m_logTable[m_expTable[i]] = i; + } + m_logTable[0] = 0; +} +void CBC_ReedSolomonGF256::Init() { + m_zero = new CBC_ReedSolomonGF256Poly(this, 0); + m_one = new CBC_ReedSolomonGF256Poly(this, 1); +} +CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() { + delete m_zero; + delete m_one; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() { + return m_zero; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() { + return m_one; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial( + int32_t degree, + int32_t coefficient, + int32_t& e) { + if (degree < 0) { + e = BCExceptionDegreeIsNegative; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + if (coefficient == 0) { + CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; + } + CFX_Int32Array coefficients; + coefficients.SetSize(degree + 1); + coefficients[0] = coefficient; + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(this, &coefficients, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) { + return a ^ b; +} +int32_t CBC_ReedSolomonGF256::Exp(int32_t a) { + return m_expTable[a]; +} +int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) { + if (a == 0) { + e = BCExceptionAIsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + } + return m_logTable[a]; +} +int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) { + if (a == 0) { + e = BCExceptionAIsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + } + return m_expTable[255 - m_logTable[a]]; +} +int32_t CBC_ReedSolomonGF256::Multiply(int32_t a, int32_t b) { + if (a == 0 || b == 0) { + return 0; + } + if (a == 1) { + return b; + } + if (b == 1) { + return a; + } + return m_expTable[(m_logTable[a] + m_logTable[b]) % 255]; +} diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h new file mode 100644 index 0000000000..099cab57d5 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h @@ -0,0 +1,41 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256_H_ +#define XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256_H_ + +#include "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/utils.h" + +class CBC_ReedSolomonGF256Poly; +class CBC_ReedSolomonGF256 { + public: + static void Initialize(); + static void Finalize(); + static CBC_ReedSolomonGF256* QRCodeFild; + static CBC_ReedSolomonGF256* DataMatrixField; + CBC_ReedSolomonGF256(int32_t primitive); + virtual ~CBC_ReedSolomonGF256(); + CBC_ReedSolomonGF256Poly* GetZero(); + CBC_ReedSolomonGF256Poly* GetOne(); + CBC_ReedSolomonGF256Poly* BuildMonomial(int32_t degree, + int32_t coefficient, + int32_t& e); + static int32_t AddOrSubtract(int32_t a, int32_t b); + int32_t Exp(int32_t a); + int32_t Log(int32_t a, int32_t& e); + int32_t Inverse(int32_t a, int32_t& e); + int32_t Multiply(int32_t a, int32_t b); + virtual void Init(); + + private: + int32_t m_expTable[256]; + int32_t m_logTable[256]; + CBC_ReedSolomonGF256Poly* m_zero; + CBC_ReedSolomonGF256Poly* m_one; +}; + +#endif // XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256_H_ diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp new file mode 100644 index 0000000000..ee22390df6 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp @@ -0,0 +1,254 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" + +#include <memory> + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" + +CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field, + int32_t coefficients) { + if (field == NULL) { + return; + } + m_field = field; + m_coefficients.Add(coefficients); +} +CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly() { + m_field = NULL; +} +void CBC_ReedSolomonGF256Poly::Init(CBC_ReedSolomonGF256* field, + CFX_Int32Array* coefficients, + int32_t& e) { + if (coefficients == NULL || coefficients->GetSize() == 0) { + e = BCExceptionCoefficientsSizeIsNull; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + m_field = field; + int32_t coefficientsLength = coefficients->GetSize(); + if ((coefficientsLength > 1 && (*coefficients)[0] == 0)) { + int32_t firstNonZero = 1; + while ((firstNonZero < coefficientsLength) && + ((*coefficients)[firstNonZero] == 0)) { + firstNonZero++; + } + if (firstNonZero == coefficientsLength) { + m_coefficients.Copy(*(m_field->GetZero()->GetCoefficients())); + } else { + m_coefficients.SetSize(coefficientsLength - firstNonZero); + for (int32_t i = firstNonZero, j = 0; i < coefficientsLength; i++, j++) { + m_coefficients[j] = coefficients->operator[](i); + } + } + } else { + m_coefficients.Copy(*coefficients); + } +} +CFX_Int32Array* CBC_ReedSolomonGF256Poly::GetCoefficients() { + return &m_coefficients; +} +int32_t CBC_ReedSolomonGF256Poly::GetDegree() { + return m_coefficients.GetSize() - 1; +} +FX_BOOL CBC_ReedSolomonGF256Poly::IsZero() { + return m_coefficients[0] == 0; +} +int32_t CBC_ReedSolomonGF256Poly::GetCoefficients(int32_t degree) { + return m_coefficients[m_coefficients.GetSize() - 1 - degree]; +} +int32_t CBC_ReedSolomonGF256Poly::EvaluateAt(int32_t a) { + if (a == 0) { + return GetCoefficients(0); + } + int32_t size = m_coefficients.GetSize(); + if (a == 1) { + int32_t result = 0; + for (int32_t i = 0; i < size; i++) { + result = CBC_ReedSolomonGF256::AddOrSubtract(result, m_coefficients[i]); + } + return result; + } + int32_t result = m_coefficients[0]; + for (int32_t j = 1; j < size; j++) { + result = CBC_ReedSolomonGF256::AddOrSubtract(m_field->Multiply(a, result), + m_coefficients[j]); + } + return result; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Clone(int32_t& e) { + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &m_coefficients, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::AddOrSubtract( + CBC_ReedSolomonGF256Poly* other, + int32_t& e) { + if (IsZero()) { + return other->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + if (other->IsZero()) { + return Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + CFX_Int32Array smallerCoefficients; + smallerCoefficients.Copy(m_coefficients); + CFX_Int32Array largerCoefficients; + largerCoefficients.Copy(*(other->GetCoefficients())); + if (smallerCoefficients.GetSize() > largerCoefficients.GetSize()) { + CFX_Int32Array temp; + temp.Copy(smallerCoefficients); + smallerCoefficients.Copy(largerCoefficients); + largerCoefficients.Copy(temp); + } + CFX_Int32Array sumDiff; + sumDiff.SetSize(largerCoefficients.GetSize()); + int32_t lengthDiff = + largerCoefficients.GetSize() - smallerCoefficients.GetSize(); + for (int32_t i = 0; i < lengthDiff; i++) { + sumDiff[i] = largerCoefficients[i]; + } + for (int32_t j = lengthDiff; j < largerCoefficients.GetSize(); j++) { + sumDiff[j] = (CBC_ReedSolomonGF256::AddOrSubtract( + smallerCoefficients[j - lengthDiff], largerCoefficients[j])); + } + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &sumDiff, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply( + CBC_ReedSolomonGF256Poly* other, + int32_t& e) { + if (IsZero() || other->IsZero()) { + CBC_ReedSolomonGF256Poly* temp = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; + } + CFX_Int32Array aCoefficients; + aCoefficients.Copy(m_coefficients); + int32_t aLength = m_coefficients.GetSize(); + CFX_Int32Array bCoefficients; + bCoefficients.Copy(*(other->GetCoefficients())); + int32_t bLength = other->GetCoefficients()->GetSize(); + CFX_Int32Array product; + product.SetSize(aLength + bLength - 1); + for (int32_t i = 0; i < aLength; i++) { + int32_t aCoeff = m_coefficients[i]; + for (int32_t j = 0; j < bLength; j++) { + product[i + j] = CBC_ReedSolomonGF256::AddOrSubtract( + product[i + j], + m_field->Multiply(aCoeff, other->GetCoefficients()->operator[](j))); + } + } + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &product, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply(int32_t scalar, + int32_t& e) { + if (scalar == 0) { + CBC_ReedSolomonGF256Poly* temp = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; + } + if (scalar == 1) { + return Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + int32_t size = m_coefficients.GetSize(); + CFX_Int32Array product; + product.SetSize(size); + for (int32_t i = 0; i < size; i++) { + product[i] = m_field->Multiply(m_coefficients[i], scalar); + } + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &product, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::MultiplyByMonomial( + int32_t degree, + int32_t coefficient, + int32_t& e) { + if (degree < 0) { + e = BCExceptionDegreeIsNegative; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + if (coefficient == 0) { + CBC_ReedSolomonGF256Poly* temp = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; + } + int32_t size = m_coefficients.GetSize(); + CFX_Int32Array product; + product.SetSize(size + degree); + for (int32_t i = 0; i < size; i++) { + product[i] = (m_field->Multiply(m_coefficients[i], coefficient)); + } + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &product, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CFX_PtrArray* CBC_ReedSolomonGF256Poly::Divide(CBC_ReedSolomonGF256Poly* other, + int32_t& e) { + if (other->IsZero()) { + e = BCExceptionDivideByZero; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + std::unique_ptr<CBC_ReedSolomonGF256Poly> quotient( + m_field->GetZero()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> remainder(Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + int32_t denominatorLeadingTerm = other->GetCoefficients(other->GetDegree()); + int32_t inverseDenominatorLeadingTeam = + m_field->Inverse(denominatorLeadingTerm, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + while (remainder->GetDegree() >= other->GetDegree() && !remainder->IsZero()) { + int32_t degreeDifference = remainder->GetDegree() - other->GetDegree(); + int32_t scale = + m_field->Multiply(remainder->GetCoefficients((remainder->GetDegree())), + inverseDenominatorLeadingTeam); + std::unique_ptr<CBC_ReedSolomonGF256Poly> term( + other->MultiplyByMonomial(degreeDifference, scale, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> iteratorQuotient( + m_field->BuildMonomial(degreeDifference, scale, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + quotient.reset(quotient->AddOrSubtract(iteratorQuotient.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + remainder.reset(remainder->AddOrSubtract(term.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + CFX_PtrArray* tempPtrA = new CFX_PtrArray; + tempPtrA->Add(quotient.release()); + tempPtrA->Add(remainder.release()); + return tempPtrA; +} +CBC_ReedSolomonGF256Poly::~CBC_ReedSolomonGF256Poly() { + m_coefficients.RemoveAll(); +} diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h new file mode 100644 index 0000000000..16a1f9066e --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h @@ -0,0 +1,43 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256POLY_H_ +#define XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256POLY_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_ReedSolomonGF256; + +class CBC_ReedSolomonGF256Poly { + public: + CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field, int32_t coefficients); + CBC_ReedSolomonGF256Poly(); + virtual ~CBC_ReedSolomonGF256Poly(); + int32_t GetCoefficients(int32_t degree); + CFX_Int32Array* GetCoefficients(); + int32_t GetDegree(); + FX_BOOL IsZero(); + int32_t EvaluateAt(int32_t a); + CBC_ReedSolomonGF256Poly* AddOrSubtract(CBC_ReedSolomonGF256Poly* other, + int32_t& e); + CBC_ReedSolomonGF256Poly* Multiply(CBC_ReedSolomonGF256Poly* other, + int32_t& e); + CBC_ReedSolomonGF256Poly* Multiply(int32_t scalar, int32_t& e); + CBC_ReedSolomonGF256Poly* MultiplyByMonomial(int32_t degree, + int32_t coefficient, + int32_t& e); + CFX_PtrArray* Divide(CBC_ReedSolomonGF256Poly* other, int32_t& e); + CBC_ReedSolomonGF256Poly* Clone(int32_t& e); + virtual void Init(CBC_ReedSolomonGF256* field, + CFX_Int32Array* coefficients, + int32_t& e); + + private: + CBC_ReedSolomonGF256* m_field; + CFX_Int32Array m_coefficients; +}; + +#endif // XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256POLY_H_ |