1                                                 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 
7 //-----------------------------------------------------------------------------
8 //
9 //	class Compressor
10 //
11 //-----------------------------------------------------------------------------
12 
13 #include "ImfCompressor.h"
14 #include "ImfRleCompressor.h"
15 #include "ImfZipCompressor.h"
16 #include "ImfPizCompressor.h"
17 #include "ImfPxr24Compressor.h"
18 #include "ImfB44Compressor.h"
19 #include "ImfDwaCompressor.h"
20 #include "ImfCheckedArithmetic.h"
21 #include "ImfNamespace.h"
22 
23 OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
24 
25 using IMATH_NAMESPACE::Box2i;
26 
27 
Compressor(const Header & hdr)28 Compressor::Compressor (const Header &hdr): _header (hdr) {}
29 
30 
~Compressor()31 Compressor::~Compressor () {}
32 
33 
34 Compressor::Format
format() const35 Compressor::format () const
36 {
37     return XDR;
38 }
39 
40 
41 int
compressTile(const char * inPtr,int inSize,Box2i range,const char * & outPtr)42 Compressor::compressTile (const char *inPtr,
43 			  int inSize,
44 			  Box2i range,
45 			  const char *&outPtr)
46 {
47     return compress (inPtr, inSize, range.min.y, outPtr);
48 }
49 
50 
51 int
uncompressTile(const char * inPtr,int inSize,Box2i range,const char * & outPtr)52 Compressor::uncompressTile (const char *inPtr,
53 			    int inSize,
54 			    Box2i range,
55 			    const char *&outPtr)
56 {
57     return uncompress (inPtr, inSize, range.min.y, outPtr);
58 }
59 
60 
61 bool
isValidCompression(Compression c)62 isValidCompression (Compression c)
63 {
64     switch (c)
65     {
66       case NO_COMPRESSION:
67       case RLE_COMPRESSION:
68       case ZIPS_COMPRESSION:
69       case ZIP_COMPRESSION:
70       case PIZ_COMPRESSION:
71       case PXR24_COMPRESSION:
72       case B44_COMPRESSION:
73       case B44A_COMPRESSION:
74       case DWAA_COMPRESSION:
75       case DWAB_COMPRESSION:
76 
77 	return true;
78 
79       default:
80 
81 	return false;
82     }
83 }
84 
isLossyCompression(Compression c)85 bool isLossyCompression(Compression c)
86 {
87     switch (c)
88     {
89       case B44_COMPRESSION:
90       case B44A_COMPRESSION:
91       case DWAA_COMPRESSION:
92       case DWAB_COMPRESSION:
93 	return true;
94       default:
95 	return false;
96     }
97 }
98 
isValidDeepCompression(Compression c)99 bool isValidDeepCompression(Compression c)
100 {
101   switch(c)
102   {
103       case NO_COMPRESSION:
104       case RLE_COMPRESSION:
105       case ZIPS_COMPRESSION:
106           return true;
107       default :
108           return false;
109   }
110 }
111 
112 
113 Compressor *
newCompressor(Compression c,size_t maxScanLineSize,const Header & hdr)114 newCompressor (Compression c, size_t maxScanLineSize, const Header &hdr)
115 {
116     switch (c)
117     {
118       case RLE_COMPRESSION:
119 
120 	return new RleCompressor (hdr, maxScanLineSize);
121 
122       case ZIPS_COMPRESSION:
123 
124 	return new ZipCompressor (hdr, maxScanLineSize, 1);
125 
126       case ZIP_COMPRESSION:
127 
128 	return new ZipCompressor (hdr, maxScanLineSize, 16);
129 
130       case PIZ_COMPRESSION:
131 
132 	return new PizCompressor (hdr, maxScanLineSize, 32);
133 
134       case PXR24_COMPRESSION:
135 
136 	return new Pxr24Compressor (hdr, maxScanLineSize, 16);
137 
138       case B44_COMPRESSION:
139 
140 	return new B44Compressor (hdr, maxScanLineSize, 32, false);
141 
142       case B44A_COMPRESSION:
143 
144 	return new B44Compressor (hdr, maxScanLineSize, 32, true);
145 
146       case DWAA_COMPRESSION:
147 
148 	return new DwaCompressor (hdr, static_cast<int>(maxScanLineSize), 32,
149                                DwaCompressor::STATIC_HUFFMAN);
150 
151       case DWAB_COMPRESSION:
152 
153 	return new DwaCompressor (hdr, static_cast<int>(maxScanLineSize), 256,
154                                DwaCompressor::STATIC_HUFFMAN);
155 
156       default:
157 
158 	return 0;
159     }
160 }
161 
162 
163 // for a given compression type, return the number of scanlines
164 // compressed into a single chunk
165 // TODO add to API and move to ImfCompressor.cpp
166 int
numLinesInBuffer(Compression comp)167 numLinesInBuffer(Compression comp)
168 {
169     switch(comp)
170     {
171         case NO_COMPRESSION :
172         case RLE_COMPRESSION:
173         case ZIPS_COMPRESSION:
174             return 1;
175         case ZIP_COMPRESSION:
176             return 16;
177         case PIZ_COMPRESSION:
178             return 32;
179         case PXR24_COMPRESSION:
180             return 16;
181         case B44_COMPRESSION:
182         case B44A_COMPRESSION:
183         case DWAA_COMPRESSION:
184             return 32;
185         case DWAB_COMPRESSION:
186             return 256;
187 
188         default:
189 	        throw IEX_NAMESPACE::ArgExc ("Unknown compression type");
190     }
191 }
192 
193 
194 Compressor *
newTileCompressor(Compression c,size_t tileLineSize,size_t numTileLines,const Header & hdr)195 newTileCompressor (Compression c,
196 		   size_t tileLineSize,
197 		   size_t numTileLines,
198 		   const Header &hdr)
199 {
200     switch (c)
201     {
202       case RLE_COMPRESSION:
203 
204 	return new RleCompressor (hdr, uiMult (tileLineSize, numTileLines));
205 
206       case ZIPS_COMPRESSION:
207       case ZIP_COMPRESSION:
208 
209 	return new ZipCompressor (hdr, tileLineSize, numTileLines);
210 
211       case PIZ_COMPRESSION:
212 
213 	return new PizCompressor (hdr, tileLineSize, numTileLines);
214 
215       case PXR24_COMPRESSION:
216 
217 	return new Pxr24Compressor (hdr, tileLineSize, numTileLines);
218 
219       case B44_COMPRESSION:
220 
221 	return new B44Compressor (hdr, tileLineSize, numTileLines, false);
222 
223       case B44A_COMPRESSION:
224 
225 	return new B44Compressor (hdr, tileLineSize, numTileLines, true);
226 
227       case DWAA_COMPRESSION:
228 
229 	return new DwaCompressor (hdr, static_cast<int>(tileLineSize), static_cast<int>(numTileLines),
230                                DwaCompressor::DEFLATE);
231 
232       case DWAB_COMPRESSION:
233 
234 	return new DwaCompressor (hdr, static_cast<int>(tileLineSize), static_cast<int>(numTileLines),
235                                DwaCompressor::STATIC_HUFFMAN);
236 
237       default:
238 
239 	return 0;
240     }
241 }
242 
243 
244 OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
245 
246