1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_FILTER_SOURCE_GRAPHICFILTER_ITIFF_CCIDECOM_HXX
21 #define INCLUDED_FILTER_SOURCE_GRAPHICFILTER_ITIFF_CCIDECOM_HXX
22 
23 #include <sal/types.h>
24 #include <array>
25 #include <memory>
26 
27 #define CCI_OPTION_2D               1       // 2D compression (instead of 1D)
28 #define CCI_OPTION_EOL              2       // There are EOL-Codes at the end of each line.
29 #define CCI_OPTION_BYTEALIGNEOL     4       // Filling bits before each EOL-Code, so that
30                                             // the end of EOL is bytes aligned
31 #define CCI_OPTION_BYTEALIGNROW     8       // Rows always start byte aligned
32 #define CCI_OPTION_INVERSEBITORDER  16
33 
34 // Entry in the Huffman table:
35 struct CCIHuffmanTableEntry {
36     sal_uInt16 nValue;    // The data value.
37     sal_uInt16 nCode;     // The code through which the data value is represented.
38     sal_uInt16 nCodeBits; // Size of the code in bits.
39 };
40 
41 // Entry in a hash table for daft decoding.
42 struct CCILookUpTableEntry {
43     sal_uInt16 nValue;
44     sal_uInt16 nCodeBits;
45 };
46 
47 class SvStream;
48 
49 struct DecompressStatus
50 {
51     bool m_bSuccess;
52     bool m_bBufferUnchanged;
DecompressStatusDecompressStatus53     DecompressStatus(bool bSuccess, bool bBufferUnchanged)
54         : m_bSuccess(bSuccess), m_bBufferUnchanged(bBufferUnchanged)
55     {
56     }
57 };
58 
59 class CCIDecompressor {
60 
61 public:
62 
63     CCIDecompressor( sal_uInt32 nOptions, sal_uInt32 nImageWidth );
64     ~CCIDecompressor();
65 
66     void StartDecompression( SvStream & rIStream );
67 
68     DecompressStatus DecompressScanline(sal_uInt8 * pTarget, sal_uInt64 nTargetBits, bool bLastLine);
69 
70 private:
71 
72     void MakeLookUp(const CCIHuffmanTableEntry * pHufTab,
73                     const CCIHuffmanTableEntry * pHufTabSave,
74                     CCILookUpTableEntry * pLookUp,
75                     sal_uInt16 nHuffmanTableSize,
76                     sal_uInt16 nMaxCodeBits);
77 
78     bool ReadEOL();
79 
80     bool Read2DTag();
81 
82     sal_uInt8 ReadBlackOrWhite();
83 
84     sal_uInt16 ReadCodeAndDecode(const CCILookUpTableEntry * pLookUp,
85                              sal_uInt16 nMaxCodeBits);
86 
87     static void FillBits(sal_uInt8 * pTarget, sal_uInt16 nTargetBits,
88                   sal_uInt16 nBitPos, sal_uInt16 nNumBits,
89                   sal_uInt8 nBlackOrWhite);
90 
91     static sal_uInt16 CountBits(const sal_uInt8 * pData, sal_uInt16 nDataSizeBits,
92                      sal_uInt16 nBitPos, sal_uInt8 nBlackOrWhite);
93 
94     //returns true if pTarget was unmodified
95     bool Read1DScanlineData(sal_uInt8 * pTarget, sal_uInt16 nTargetBits);
96     bool Read2DScanlineData(sal_uInt8 * pTarget, sal_uInt16 nTargetBits);
97 
98     bool bTableBad;
99 
100     bool bStatus;
101 
102     std::unique_ptr<sal_uInt8[]> pByteSwap;
103 
104     SvStream * pIStream;
105 
106     sal_uInt32 nEOLCount;
107 
108     sal_uInt32 nWidth;
109 
110     sal_uInt32 nOptions;
111 
112     bool bFirstEOL;
113 
114     std::array<CCILookUpTableEntry, 1<<13> pWhiteLookUp;
115     std::array<CCILookUpTableEntry, 1<<13> pBlackLookUp;
116     std::array<CCILookUpTableEntry, 1<<10> p2DModeLookUp;
117     std::array<CCILookUpTableEntry, 1<<11> pUncompLookUp;
118 
119     sal_uInt32 nInputBitsBuf;
120     sal_uInt16 nInputBitsBufSize;
121 
122     std::unique_ptr<sal_uInt8[]> pLastLine;
123     sal_uInt64 nLastLineSize;
124 };
125 
126 
127 #endif
128 
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
130