Subversion Repositories Scribus

Rev

Rev 16771 | Rev 19721 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15796 fschmid 1
/*
2
 * The Progressive Graphics File; http://www.libpgf.org
16771 fschmid 3
 *
15796 fschmid 4
 * $Date: 2006-06-04 22:05:59 +0200 (So, 04 Jun 2006) $
5
 * $Revision: 229 $
16771 fschmid 6
 *
15796 fschmid 7
 * This file Copyright (C) 2006 xeraina GmbH, Switzerland
16771 fschmid 8
 *
15796 fschmid 9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
11
 * as published by the Free Software Foundation; either version 2.1
12
 * of the License, or (at your option) any later version.
16771 fschmid 13
 *
15796 fschmid 14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
16771 fschmid 18
 *
15796 fschmid 19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
18122 mrdocs 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
15796 fschmid 22
 */
23
 
16771 fschmid 24
//////////////////////////////////////////////////////////////////////
25
/// @file Decoder.h
26
/// @brief PGF decoder class
27
/// @author C. Stamm, R. Spuler
28
 
15796 fschmid 29
#ifndef PGF_DECODER_H
30
#define PGF_DECODER_H
31
 
16771 fschmid 32
#include "PGFstream.h"
15796 fschmid 33
#include "BitStream.h"
34
#include "Subband.h"
35
#include "WaveletTransform.h"
36
 
37
/////////////////////////////////////////////////////////////////////
38
// Constants
39
#define BufferLen                       (BufferSize/WordWidth)  // number of words per buffer
40
 
41
/////////////////////////////////////////////////////////////////////
42
/// PGF decoder class.
43
/// @author C. Stamm, R. Spuler
16771 fschmid 44
/// @brief PGF decoder
15796 fschmid 45
class CDecoder {
16771 fschmid 46
        //////////////////////////////////////////////////////////////////////
47
        /// PGF decoder macro block class.
48
        /// @author C. Stamm, I. Bauersachs
49
        /// @brief A macro block is a decoding unit of fixed size (uncoded)
50
        class CMacroBlock {
51
        public:
52
                CMacroBlock(CDecoder *decoder)
53
                : m_header(0)
54
        , m_valuePos(0)
55
                , m_decoder(decoder)
56
                {
57
                        ASSERT(m_decoder);
58
                }
59
 
60
                void  BitplaneDecode();                                         // several macro blocks can be encoded in parallel
61
 
62
                ROIBlockHeader m_header;                                        // block header
63
                DataT  m_value[BufferSize];                                     // output buffer of values with index m_valuePos
64
                UINT32 m_codeBuffer[BufferSize];                        // input buffer for encoded bitstream
65
                UINT32 m_valuePos;                                                      // current position in m_value
66
 
67
        private:
68
                UINT32 ComposeBitplane(UINT32 bufferSize, DataT planeMask, UINT32* sigBits, UINT32* refBits, UINT32* signBits);
69
                UINT32 ComposeBitplaneRLD(UINT32 bufferSize, DataT planeMask, UINT32 sigPos, UINT32* refBits);
70
                UINT32 ComposeBitplaneRLD(UINT32 bufferSize, DataT planeMask, UINT32* sigBits, UINT32* refBits, UINT32* signBits);
71
                void  SetBitAtPos(UINT32 pos, DataT planeMask)                  { (m_value[pos] >= 0) ? m_value[pos] |= planeMask : m_value[pos] -= planeMask; }
72
                void  SetSign(UINT32 pos, bool sign)                                    { m_value[pos] = -m_value[pos]*sign + m_value[pos]*(!sign); }
73
 
74
                CDecoder *m_decoder;                                            // outer class
75
                bool m_sigFlagVector[BufferSize+1];                     // see paper from Malvar, Fast Progressive Wavelet Coder
76
        };
77
 
15796 fschmid 78
public:
79
        /////////////////////////////////////////////////////////////////////
80
        /// Constructor: Read pre-header, header, and levelLength at current stream position.
81
        /// It might throw an IOException.
82
        /// @param stream A PGF stream
83
        /// @param preHeader [out] A PGF pre-header
84
        /// @param header [out] A PGF header
85
        /// @param postHeader [out] A PGF post-header
86
        /// @param levelLength The location of the levelLength array. The array is allocated in this method. The caller has to delete this array.
16771 fschmid 87
        /// @param useOMP If true, then the decoder will use multi-threading based on openMP
88
        CDecoder(CPGFStream* stream, PGFPreHeader& preHeader, PGFHeader& header, PGFPostHeader& postHeader, UINT32*& levelLength, bool useOMP = true) THROW_; // throws IOException
15796 fschmid 89
 
90
        /////////////////////////////////////////////////////////////////////
91
        /// Destructor
92
        ~CDecoder();
93
 
94
        /////////////////////////////////////////////////////////////////////
95
        /// Unpartitions a rectangular region of a given subband.
96
        /// Partitioning scheme: The plane is partitioned in squares of side length LinBlockSize.
97
        /// Write wavelet coefficients into buffer.
98
        /// It might throw an IOException.
99
        /// @param band A subband
100
        /// @param quantParam Dequantization value
101
        /// @param width The width of the rectangle
102
        /// @param height The height of the rectangle
103
        /// @param startPos The buffer position of the top left corner of the rectangular region
104
        /// @param pitch The number of bytes in row of the subband
105
        void Partition(CSubband* band, int quantParam, int width, int height, int startPos, int pitch) THROW_;
106
 
107
        /////////////////////////////////////////////////////////////////////
108
        /// Deccoding and dequantization of HL and LH subband (interleaved) using partitioning scheme.
109
        /// Partitioning scheme: The plane is partitioned in squares of side length InterBlockSize.
110
        /// It might throw an IOException.
111
        /// @param wtChannel A wavelet transform channel containing the HL and HL band
112
        /// @param level Wavelet transform level
113
        /// @param quantParam Dequantization value
114
        void DecodeInterleaved(CWaveletTransform* wtChannel, int level, int quantParam) THROW_;
115
 
116
        //////////////////////////////////////////////////////////////////////
117
        /// Return the length of all encoded headers in bytes.
118
        /// @return The length of all encoded headers in bytes
119
        UINT32 GetEncodedHeaderLength() const                   { return m_encodedHeaderLength; }
120
 
121
        ////////////////////////////////////////////////////////////////////
122
        /// Reset stream position to beginning of PGF pre header
123
        void SetStreamPosToStart() THROW_                               { ASSERT(m_stream); m_stream->SetPos(FSFromStart, m_startPos); }
124
 
125
        ////////////////////////////////////////////////////////////////////
16771 fschmid 126
        /// Reset stream position to beginning of data block
15796 fschmid 127
        void SetStreamPosToData() THROW_                                { ASSERT(m_stream); m_stream->SetPos(FSFromStart, m_startPos + m_encodedHeaderLength); }
128
 
129
        ////////////////////////////////////////////////////////////////////
130
        /// Skip a given number of bytes in the open stream.
131
        /// It might throw an IOException.
132
        void Skip(UINT64 offset) THROW_;
133
 
134
        /////////////////////////////////////////////////////////////////////
135
        /// Dequantization of a single value at given position in subband.
136
        /// @param band A subband
137
        /// @param bandPos A valid position in subband band
138
        /// @param quantParam The quantization parameter
139
        void DequantizeValue(CSubband* band, UINT32 bandPos, int quantParam);
140
 
141
        //////////////////////////////////////////////////////////////////////
142
        /// Copies data from the open stream to a target buffer.
143
        /// It might throw an IOException.
144
        /// @param target The target buffer
145
        /// @param len The number of bytes to read
146
        /// @return The number of bytes copied to the target buffer
147
        UINT32 ReadEncodedData(UINT8* target, UINT32 len) const THROW_;
148
 
16771 fschmid 149
        /////////////////////////////////////////////////////////////////////
150
        /// Reads stream and decodes tile buffer
151
        /// It might throw an IOException.
152
        void DecodeBuffer() THROW_;
153
 
15796 fschmid 154
#ifdef __PGFROISUPPORT__
155
        /////////////////////////////////////////////////////////////////////
156
        /// Reads stream and decodes tile buffer
157
        /// It might throw an IOException.
16771 fschmid 158
        void DecodeTileBuffer() THROW_;
15796 fschmid 159
 
160
        /////////////////////////////////////////////////////////////////////
161
        /// Resets stream position to next tile.
162
        /// It might throw an IOException.
16771 fschmid 163
        void SkipTileBuffer() THROW_;
15796 fschmid 164
 
165
        /////////////////////////////////////////////////////////////////////
166
        /// Enables region of interest (ROI) status.
167
        void SetROI()                                   { m_roi = true; }
168
#endif
169
 
170
#ifdef TRACE
171
        void DumpBuffer();
172
#endif
173
 
174
private:
16771 fschmid 175
        void ReadMacroBlock(CMacroBlock* block) THROW_; // throws IOException
15796 fschmid 176
 
177
        CPGFStream *m_stream;                                           // input pgf stream
178
        UINT64 m_startPos;                                                      // stream position at the beginning of the PGF pre header
16771 fschmid 179
        UINT64 m_streamSizeEstimation;                          // estimation of stream size
15796 fschmid 180
        UINT32 m_encodedHeaderLength;                           // stream offset from startPos to the beginning of the data part (highest level)
181
 
16771 fschmid 182
        CMacroBlock **m_macroBlocks;                            // array of macroblocks
183
        int m_currentBlockIndex;                                        // index of current macro block
184
        int     m_macroBlockLen;                                                // array length
185
        int     m_macroBlocksAvailable;                                 // number of decoded macro blocks
186
        CMacroBlock *m_currentBlock;                            // current macro block (used by main thread)
15796 fschmid 187
 
188
#ifdef __PGFROISUPPORT__
189
        bool   m_roi;                                                           // true: ensures region of interest (ROI) decoding
190
#endif
191
};
192
 
193
#endif //PGF_DECODER_H