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 Encoder.h
26
/// @brief PGF encoder class
27
/// @author C. Stamm, R. Spuler
28
 
15796 fschmid 29
#ifndef PGF_ENCODER_H
30
#define PGF_ENCODER_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 encoder class.
16771 fschmid 43
/// @author C. Stamm
44
/// @brief PGF encoder
15796 fschmid 45
class CEncoder {
16771 fschmid 46
        //////////////////////////////////////////////////////////////////////
47
        /// PGF encoder macro block class.
48
        /// @author C. Stamm, I. Bauersachs
49
        /// @brief A macro block is an encoding unit of fixed size (uncoded)
50
        class CMacroBlock {
51
        public:
52
                CMacroBlock(CEncoder *encoder)
53
                : m_header(0)
54
        , m_encoder(encoder)
55
                {
56
                        ASSERT(m_encoder);
57
                        Init(-1);
58
                }
59
 
60
                DataT   m_value[BufferSize];                            // input buffer of values with index m_valuePos
61
                UINT32  m_codeBuffer[BufferSize];                       // output buffer for encoded bitstream
62
 
63
                ROIBlockHeader m_header;                                        // block header
64
                UINT32  m_valuePos;                                                     // current buffer position
65
                UINT32  m_maxAbsValue;                                          // maximum absolute coefficient in each buffer
66
                UINT32  m_codePos;                                                      // current position in encoded bitstream
67
                int             m_lastLevelIndex;                                       // index of last encoded level: [0, nLevels); used because a level-end can occur before a buffer is full
68
 
69
                void Init(int lastLevelIndex) {                         // initialize for reusage
70
                        m_valuePos = 0;
71
                        m_maxAbsValue = 0;
72
                        m_codePos = 0;
73
                        m_lastLevelIndex = lastLevelIndex;
74
                }
75
                void BitplaneEncode();                                          // several macro blocks can be encoded in parallel
76
        private:
77
                UINT32 RLESigns(UINT32 codePos, UINT32* signBits, UINT32 signLen);
78
                UINT32 DecomposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32* sigBits, UINT32* refBits, UINT32* signBits, UINT32& signLen, UINT32& codeLen);
79
                UINT8  NumberOfBitplanes();
80
                bool   GetBitAtPos(UINT32 pos, UINT32 planeMask) const  { return (abs(m_value[pos]) & planeMask) > 0; }
81
 
82
                CEncoder *m_encoder;                                            // encoder instance
83
                bool    m_sigFlagVector[BufferSize+1];          // see paper from Malvar, Fast Progressive Wavelet Coder
84
        };
85
 
15796 fschmid 86
public:
87
        /////////////////////////////////////////////////////////////////////
88
        /// Write pre-header, header, postHeader, and levelLength.
89
        /// It might throw an IOException.
90
        /// @param stream A PGF stream
91
        /// @param preHeader A already filled in PGF pre header
92
        /// @param header An already filled in PGF header
93
        /// @param postHeader [in] A already filled in PGF post header (containing color table, user data, ...)
94
        /// @param levelLength A reference to an integer array, large enough to save the relative file positions of all PGF levels
16771 fschmid 95
        /// @param useOMP If true, then the encoder will use multi-threading based on openMP
96
        CEncoder(CPGFStream* stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader& postHeader, UINT32*& levelLength, bool useOMP = true) THROW_; // throws IOException
15796 fschmid 97
 
98
        /////////////////////////////////////////////////////////////////////
99
        /// Destructor
100
        ~CEncoder();
101
 
102
        /////////////////////////////////////////////////////////////////////
16771 fschmid 103
        /// Encoder favors speed over compression size
104
        void FavorSpeedOverSize() { m_favorSpeed = true; }
105
 
106
        /////////////////////////////////////////////////////////////////////
107
        /// Pad buffer with zeros and encode buffer.
15796 fschmid 108
        /// It might throw an IOException.
16771 fschmid 109
        void Flush() THROW_;
15796 fschmid 110
 
111
        /////////////////////////////////////////////////////////////////////
16771 fschmid 112
        /// Write levelLength into header.
113
        /// @return number of bytes written into stream
114
        /// It might throw an IOException.
115
        UINT32 WriteLevelLength() THROW_;
116
 
117
        /////////////////////////////////////////////////////////////////////
15796 fschmid 118
        /// Partitions a rectangular region of a given subband.
119
        /// Partitioning scheme: The plane is partitioned in squares of side length LinBlockSize.
120
        /// Write wavelet coefficients into buffer.
121
        /// It might throw an IOException.
122
        /// @param band A subband
123
        /// @param width The width of the rectangle
124
        /// @param height The height of the rectangle
125
        /// @param startPos The buffer position of the top left corner of the rectangular region
126
        /// @param pitch The number of bytes in row of the subband
127
        void Partition(CSubband* band, int width, int height, int startPos, int pitch) THROW_;
128
 
129
        /////////////////////////////////////////////////////////////////////
16771 fschmid 130
        /// Informs the encoder about the encoded level.
131
        /// @param currentLevel encoded level [0, nLevels)
132
        void SetEncodedLevel(int currentLevel) { ASSERT(currentLevel >= 0); m_currentBlock->m_lastLevelIndex = m_nLevels - currentLevel - 1; m_forceWriting = true; }
15796 fschmid 133
 
134
        /////////////////////////////////////////////////////////////////////
135
        /// Write a single value into subband at given position.
136
        /// It might throw an IOException.
137
        /// @param band A subband
138
        /// @param bandPos A valid position in subband band
139
        void WriteValue(CSubband* band, int bandPos) THROW_;
140
 
16771 fschmid 141
        /////////////////////////////////////////////////////////////////////
142
        /// Compute stream length of header.
143
        /// @return header length
144
        UINT32 ComputeHeaderLength() const { return UINT32(m_bufferStartPos - m_startPosition); }
145
 
146
        /////////////////////////////////////////////////////////////////////
147
        /// Compute stream length of encoded buffer.
148
        /// @return encoded buffer length
149
        UINT32 ComputeBufferLength() const { return UINT32(m_stream->GetPos() - m_bufferStartPos); }
150
 
151
        /////////////////////////////////////////////////////////////////////
152
        /// Save current stream position as beginning of current level.
153
        void SetBufferStartPos() { m_bufferStartPos = m_stream->GetPos(); }
154
 
15796 fschmid 155
#ifdef __PGFROISUPPORT__
156
        /////////////////////////////////////////////////////////////////////
157
        /// Encodes tile buffer and writes it into stream
158
        /// It might throw an IOException.
16771 fschmid 159
        void EncodeTileBuffer() THROW_  { ASSERT(m_currentBlock && m_currentBlock->m_valuePos >= 0 && m_currentBlock->m_valuePos <= BufferSize); EncodeBuffer(ROIBlockHeader(m_currentBlock->m_valuePos, true)); }
15796 fschmid 160
 
161
        /////////////////////////////////////////////////////////////////////
162
        /// Enables region of interest (ROI) status.
163
        void SetROI()                                   { m_roi = true; }
164
#endif
165
 
166
#ifdef TRACE
167
        void DumpBuffer() const;
168
#endif
169
 
170
private:
16771 fschmid 171
        void EncodeBuffer(ROIBlockHeader h) THROW_; // throws IOException
172
        void WriteMacroBlock(CMacroBlock* block) THROW_; // throws IOException
15796 fschmid 173
 
174
        CPGFStream *m_stream;
175
        UINT64  m_startPosition;                                        // file position of PGF start (PreHeader)
176
        UINT64  m_levelLengthPos;                                       // file position of Metadata
16771 fschmid 177
        UINT64  m_bufferStartPos;                                       // file position of encoded buffer
15796 fschmid 178
 
16771 fschmid 179
        CMacroBlock **m_macroBlocks;                            // array of macroblocks
180
        int             m_macroBlockLen;                                        // array length
181
        int             m_lastMacroBlock;                                       // array index of the last created macro block
182
        CMacroBlock *m_currentBlock;                            // current macro block (used by main thread)
15796 fschmid 183
 
184
        UINT32* m_levelLength;                                          // temporary saves the level index
185
        int     m_currLevelIndex;                                       // counts where (=index) to save next value
16771 fschmid 186
        UINT8   m_nLevels;                                                      // number of levels
187
        bool    m_favorSpeed;                                           // favor speed over size
188
        bool    m_forceWriting;                                         // all macro blocks have to be written into the stream
15796 fschmid 189
#ifdef __PGFROISUPPORT__
190
        bool    m_roi;                                                          // true: ensures region of interest (ROI) encoding
191
#endif
192
};
193
 
194
#endif //PGF_ENCODER