1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // *       Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // *       Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // *       Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 //-----------------------------------------------------------------------------
38 //
39 //	class ZipCompressor
40 //
41 //-----------------------------------------------------------------------------
42 
43 #include "ImfZipCompressor.h"
44 #include "ImfCheckedArithmetic.h"
45 #include "Iex.h"
46 #include <zlib.h>
47 #include "ImfNamespace.h"
48 
49 OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
50 
51 
ZipCompressor(const Header & hdr,size_t maxScanLineSize,size_t numScanLines)52 ZipCompressor::ZipCompressor
53     (const Header &hdr,
54      size_t maxScanLineSize,
55      size_t numScanLines)
56 :
57     Compressor (hdr),
58     _maxScanLineSize (maxScanLineSize),
59     _numScanLines (numScanLines),
60     _outBuffer (0),
61     _zip(maxScanLineSize, numScanLines)
62 {
63     _outBuffer = new char[_zip.maxCompressedSize()];
64 }
65 
66 
~ZipCompressor()67 ZipCompressor::~ZipCompressor ()
68 {
69     delete [] _outBuffer;
70 }
71 
72 
73 int
numScanLines() const74 ZipCompressor::numScanLines () const
75 {
76     return _numScanLines;
77 }
78 
79 
80 int
compress(const char * inPtr,int inSize,int minY,const char * & outPtr)81 ZipCompressor::compress (const char *inPtr,
82 			 int inSize,
83 			 int minY,
84 			 const char *&outPtr)
85 {
86     //
87     // Special case �- empty input buffer
88     //
89 
90     if (inSize == 0)
91     {
92 	outPtr = _outBuffer;
93 	return 0;
94     }
95 
96     int outSize = _zip.compress(inPtr, inSize, _outBuffer);
97 
98     outPtr = _outBuffer;
99     return outSize;
100 }
101 
102 
103 int
uncompress(const char * inPtr,int inSize,int minY,const char * & outPtr)104 ZipCompressor::uncompress (const char *inPtr,
105 			   int inSize,
106 			   int minY,
107 			   const char *&outPtr)
108 {
109     //
110     // Special case �- empty input buffer
111     //
112 
113     if (inSize == 0)
114     {
115 	outPtr = _outBuffer;
116 	return 0;
117     }
118 
119     int outSize = _zip.uncompress(inPtr, inSize, _outBuffer);
120 
121     outPtr = _outBuffer;
122     return outSize;
123 }
124 
125 
126 OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
127 
128