/src/freeimage-svn/FreeImage/trunk/Source/OpenEXR/IlmImf/ImfZip.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "ImfZip.h" |
36 | | #include "ImfCheckedArithmetic.h" |
37 | | #include "ImfNamespace.h" |
38 | | #include "Iex.h" |
39 | | |
40 | | #include <math.h> |
41 | | #include <zlib.h> |
42 | | |
43 | | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER |
44 | | |
45 | | Imf::Zip::Zip(size_t maxRawSize): |
46 | | _maxRawSize(maxRawSize), |
47 | | _tmpBuffer(0) |
48 | 0 | { |
49 | 0 | _tmpBuffer = new char[_maxRawSize]; |
50 | 0 | } |
51 | | |
52 | | Imf::Zip::Zip(size_t maxScanLineSize, size_t numScanLines): |
53 | | _maxRawSize(0), |
54 | | _tmpBuffer(0) |
55 | 0 | { |
56 | 0 | _maxRawSize = uiMult (maxScanLineSize, numScanLines); |
57 | 0 | _tmpBuffer = new char[_maxRawSize]; |
58 | 0 | } |
59 | | |
60 | | Imf::Zip::~Zip() |
61 | 0 | { |
62 | 0 | if (_tmpBuffer) delete[] _tmpBuffer; |
63 | 0 | } |
64 | | |
65 | | size_t |
66 | | Imf::Zip::maxRawSize() |
67 | 0 | { |
68 | 0 | return _maxRawSize; |
69 | 0 | } |
70 | | |
71 | | size_t |
72 | | Imf::Zip::maxCompressedSize() |
73 | 0 | { |
74 | 0 | return uiAdd (uiAdd (_maxRawSize, |
75 | 0 | size_t (ceil (_maxRawSize * 0.01))), |
76 | 0 | size_t (100)); |
77 | 0 | } |
78 | | |
79 | | int |
80 | | Imf::Zip::compress(const char *raw, int rawSize, char *compressed) |
81 | 0 | { |
82 | | // |
83 | | // Reorder the pixel data. |
84 | | // |
85 | |
|
86 | 0 | { |
87 | 0 | char *t1 = _tmpBuffer; |
88 | 0 | char *t2 = _tmpBuffer + (rawSize + 1) / 2; |
89 | 0 | const char *stop = raw + rawSize; |
90 | |
|
91 | 0 | while (true) |
92 | 0 | { |
93 | 0 | if (raw < stop) |
94 | 0 | *(t1++) = *(raw++); |
95 | 0 | else |
96 | 0 | break; |
97 | | |
98 | 0 | if (raw < stop) |
99 | 0 | *(t2++) = *(raw++); |
100 | 0 | else |
101 | 0 | break; |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | | // |
106 | | // Predictor. |
107 | | // |
108 | |
|
109 | 0 | { |
110 | 0 | unsigned char *t = (unsigned char *) _tmpBuffer + 1; |
111 | 0 | unsigned char *stop = (unsigned char *) _tmpBuffer + rawSize; |
112 | 0 | int p = t[-1]; |
113 | |
|
114 | 0 | while (t < stop) |
115 | 0 | { |
116 | 0 | int d = int (t[0]) - p + (128 + 256); |
117 | 0 | p = t[0]; |
118 | 0 | t[0] = d; |
119 | 0 | ++t; |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | | // |
124 | | // Compress the data using zlib |
125 | | // |
126 | |
|
127 | 0 | uLongf outSize = int(ceil(rawSize * 1.01)) + 100; |
128 | |
|
129 | 0 | if (Z_OK != ::compress ((Bytef *)compressed, &outSize, |
130 | 0 | (const Bytef *) _tmpBuffer, rawSize)) |
131 | 0 | { |
132 | 0 | throw Iex::BaseExc ("Data compression (zlib) failed."); |
133 | 0 | } |
134 | | |
135 | 0 | return outSize; |
136 | 0 | } |
137 | | |
138 | | int |
139 | | Imf::Zip::uncompress(const char *compressed, int compressedSize, |
140 | | char *raw) |
141 | 0 | { |
142 | | // |
143 | | // Decompress the data using zlib |
144 | | // |
145 | |
|
146 | 0 | uLongf outSize = _maxRawSize; |
147 | |
|
148 | 0 | if (Z_OK != ::uncompress ((Bytef *)_tmpBuffer, &outSize, |
149 | 0 | (const Bytef *) compressed, compressedSize)) |
150 | 0 | { |
151 | 0 | throw Iex::InputExc ("Data decompression (zlib) failed."); |
152 | 0 | } |
153 | | |
154 | | // |
155 | | // Predictor. |
156 | | // |
157 | 0 | { |
158 | 0 | unsigned char *t = (unsigned char *) _tmpBuffer + 1; |
159 | 0 | unsigned char *stop = (unsigned char *) _tmpBuffer + outSize; |
160 | |
|
161 | 0 | while (t < stop) |
162 | 0 | { |
163 | 0 | int d = int (t[-1]) + int (t[0]) - 128; |
164 | 0 | t[0] = d; |
165 | 0 | ++t; |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | | // |
170 | | // Reorder the pixel data. |
171 | | // |
172 | |
|
173 | 0 | { |
174 | 0 | const char *t1 = _tmpBuffer; |
175 | 0 | const char *t2 = _tmpBuffer + (outSize + 1) / 2; |
176 | 0 | char *s = raw; |
177 | 0 | char *stop = s + outSize; |
178 | |
|
179 | 0 | while (true) |
180 | 0 | { |
181 | 0 | if (s < stop) |
182 | 0 | *(s++) = *(t1++); |
183 | 0 | else |
184 | 0 | break; |
185 | | |
186 | 0 | if (s < stop) |
187 | 0 | *(s++) = *(t2++); |
188 | 0 | else |
189 | 0 | break; |
190 | 0 | } |
191 | 0 | } |
192 | |
|
193 | 0 | return outSize; |
194 | 0 | } |
195 | | |
196 | | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT |