1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2009-2014 DreamWorks Animation LLC.
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 // *       Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // *       Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 // *       Neither the name of DreamWorks Animation nor the names of
17 // its contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 //
32 ///////////////////////////////////////////////////////////////////////////
33 
34 #ifndef INCLUDED_IMF_DWA_COMRESSOR_H
35 #define INCLUDED_IMF_DWA_COMRESSOR_H
36 
37 //------------------------------------------------------------------------------
38 //
39 // class DwaCompressor -- Store lossy RGB data by quantizing DCT components.
40 //
41 //------------------------------------------------------------------------------
42 
43 #include <vector>
44 #include <half.h>
45 
46 #include "ImfInt64.h"
47 #include "ImfZip.h"
48 #include "ImfChannelList.h"
49 #include "ImfCompressor.h"
50 #include "ImfNamespace.h"
51 
52 OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
53 
54 class DwaCompressor: public Compressor
55 {
56   public:
57 
58     enum AcCompression
59     {
60         STATIC_HUFFMAN,
61         DEFLATE,
62     };
63 
64 
65     DwaCompressor (const Header &hdr,
66                    int           maxScanLineSize,
67                    int           numScanLines,    // ideally is a multiple of 8
68                    AcCompression acCompression);
69 
70     virtual ~DwaCompressor ();
71 
72     virtual int numScanLines () const;
73 
74     virtual Imf::Compressor::Format format () const;
75 
76     virtual int compress (const char *inPtr,
77                           int         inSize,
78                           int         minY,
79                           const char *&outPtr);
80 
81     virtual int compressTile (const char   *inPtr,
82                               int           inSize,
83                               Imath::Box2i  range,
84                               const char  *&outPtr);
85 
86     virtual int uncompress (const char *inPtr,
87                             int         inSize,
88                             int         minY,
89                             const char *&outPtr);
90 
91     virtual int uncompressTile (const char  *inPtr,
92                                 int          inSize,
93                                 Imath::Box2i range,
94                                 const char *&outPtr);
95 
96     static void initializeFuncs ();
97 
98   private:
99 
100     struct ChannelData;
101     struct CscChannelSet;
102     struct Classifier;
103 
104     class LossyDctDecoderBase;
105     class LossyDctDecoder;
106     class LossyDctDecoderCsc;
107 
108     class LossyDctEncoderBase;
109     class LossyDctEncoder;
110     class LossyDctEncoderCsc;
111 
112     enum CompressorScheme
113     {
114         UNKNOWN = 0,
115         LOSSY_DCT,
116         RLE,
117 
118         NUM_COMPRESSOR_SCHEMES
119     };
120 
121     //
122     // Per-chunk compressed data sizes, one value per chunk
123     //
124 
125     enum DataSizesSingle
126     {
127         VERSION = 0,                  // Version number:
128                                       //   0: classic
129                                       //   1: adds "end of block" to the AC RLE
130 
131         UNKNOWN_UNCOMPRESSED_SIZE,    // Size of leftover data, uncompressed.
132         UNKNOWN_COMPRESSED_SIZE,      // Size of leftover data, zlib compressed.
133 
134         AC_COMPRESSED_SIZE,           // AC RLE + Huffman size
135         DC_COMPRESSED_SIZE,           // DC + Deflate size
136         RLE_COMPRESSED_SIZE,          // RLE + Deflate data size
137         RLE_UNCOMPRESSED_SIZE,        // RLE'd data size
138         RLE_RAW_SIZE,                 // Un-RLE'd data size
139 
140         AC_UNCOMPRESSED_COUNT,        // AC RLE number of elements
141         DC_UNCOMPRESSED_COUNT,        // DC number of elements
142 
143         AC_COMPRESSION,               // AC compression strategy
144         NUM_SIZES_SINGLE
145     };
146 
147     AcCompression     _acCompression;
148 
149     int               _maxScanLineSize;
150     int               _numScanLines;
151     int               _min[2], _max[2];
152 
153     ChannelList                _channels;
154     std::vector<ChannelData>   _channelData;
155     std::vector<CscChannelSet> _cscSets;
156     std::vector<Classifier>    _channelRules;
157 
158     char             *_packedAcBuffer;
159     size_t            _packedAcBufferSize;
160     char             *_packedDcBuffer;
161     size_t            _packedDcBufferSize;
162     char             *_rleBuffer;
163     size_t            _rleBufferSize;
164     char             *_outBuffer;
165     size_t            _outBufferSize;
166     char             *_planarUncBuffer[NUM_COMPRESSOR_SCHEMES];
167     size_t            _planarUncBufferSize[NUM_COMPRESSOR_SCHEMES];
168 
169     Zip              *_zip;
170     float             _dwaCompressionLevel;
171 
172     int compress (const char   *inPtr,
173                   int           inSize,
174                   Imath::Box2i  range,
175                   const char  *&outPtr);
176 
177     int uncompress (const char   *inPtr,
178                     int           inSize,
179                     Imath::Box2i  range,
180                     const char  *&outPtr);
181 
182     void initializeBuffers (size_t&);
183     void initializeDefaultChannelRules ();
184     void initializeLegacyChannelRules ();
185 
186     void relevantChannelRules( std::vector<Classifier> &) const;
187 
188     //
189     // Populate our cached version of the channel data with
190     // data from the real channel list. We want to
191     // copy over attributes, determine compression schemes
192     // releveant for the channel type, and find sets of
193     // channels to be compressed from Y'CbCr data instead
194     // of R'G'B'.
195     //
196 
197     void classifyChannels (ChannelList                  channels,
198                            std::vector<ChannelData>    &chanData,
199                            std::vector<CscChannelSet>  &cscData);
200 
201     //
202     // Compute various buffer pointers for each channel
203     //
204 
205     void setupChannelData (int minX, int minY, int maxX, int maxY);
206 };
207 
208 OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
209 
210 #endif
211