/src/FreeImage/Source/OpenEXR/IlmImf/ImfRleCompressor.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 331 lines · 188 code · 63 blank · 80 comment · 28 complexity · fbfe52074b696f954c806d52581a3227 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, 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. // class RleCompressor
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include <ImfRleCompressor.h>
  40. #include <ImfCheckedArithmetic.h>
  41. #include "Iex.h"
  42. namespace Imf {
  43. namespace {
  44. const int MIN_RUN_LENGTH = 3;
  45. const int MAX_RUN_LENGTH = 127;
  46. //
  47. // Compress an array of bytes, using run-length encoding,
  48. // and return the length of the compressed data.
  49. //
  50. int
  51. rleCompress (int inLength, const char in[], signed char out[])
  52. {
  53. const char *inEnd = in + inLength;
  54. const char *runStart = in;
  55. const char *runEnd = in + 1;
  56. signed char *outWrite = out;
  57. while (runStart < inEnd)
  58. {
  59. while (runEnd < inEnd &&
  60. *runStart == *runEnd &&
  61. runEnd - runStart - 1 < MAX_RUN_LENGTH)
  62. {
  63. ++runEnd;
  64. }
  65. if (runEnd - runStart >= MIN_RUN_LENGTH)
  66. {
  67. //
  68. // Compressable run
  69. //
  70. *outWrite++ = (runEnd - runStart) - 1;
  71. *outWrite++ = *(signed char *) runStart;
  72. runStart = runEnd;
  73. }
  74. else
  75. {
  76. //
  77. // Uncompressable run
  78. //
  79. while (runEnd < inEnd &&
  80. ((runEnd + 1 >= inEnd ||
  81. *runEnd != *(runEnd + 1)) ||
  82. (runEnd + 2 >= inEnd ||
  83. *(runEnd + 1) != *(runEnd + 2))) &&
  84. runEnd - runStart < MAX_RUN_LENGTH)
  85. {
  86. ++runEnd;
  87. }
  88. *outWrite++ = runStart - runEnd;
  89. while (runStart < runEnd)
  90. {
  91. *outWrite++ = *(signed char *) (runStart++);
  92. }
  93. }
  94. ++runEnd;
  95. }
  96. return outWrite - out;
  97. }
  98. //
  99. // Uncompress an array of bytes compressed with rleCompress().
  100. // Returns the length of the oncompressed data, or 0 if the
  101. // length of the uncompressed data would be more than maxLength.
  102. //
  103. int
  104. rleUncompress (int inLength, int maxLength, const signed char in[], char out[])
  105. {
  106. char *outStart = out;
  107. while (inLength > 0)
  108. {
  109. if (*in < 0)
  110. {
  111. int count = -((int)*in++);
  112. inLength -= count + 1;
  113. if (0 > (maxLength -= count))
  114. return 0;
  115. while (count-- > 0)
  116. *out++ = *(char *) (in++);
  117. }
  118. else
  119. {
  120. int count = *in++;
  121. inLength -= 2;
  122. if (0 > (maxLength -= count + 1))
  123. return 0;
  124. while (count-- >= 0)
  125. *out++ = *(char *) in;
  126. in++;
  127. }
  128. }
  129. return out - outStart;
  130. }
  131. } // namespace
  132. RleCompressor::RleCompressor (const Header &hdr, size_t maxScanLineSize):
  133. Compressor (hdr),
  134. _maxScanLineSize (maxScanLineSize),
  135. _tmpBuffer (0),
  136. _outBuffer (0)
  137. {
  138. _tmpBuffer = new char [maxScanLineSize];
  139. _outBuffer = new char [uiMult (maxScanLineSize, size_t (3)) / 2];
  140. }
  141. RleCompressor::~RleCompressor ()
  142. {
  143. delete [] _tmpBuffer;
  144. delete [] _outBuffer;
  145. }
  146. int
  147. RleCompressor::numScanLines () const
  148. {
  149. //
  150. // This compressor compresses individual scan lines.
  151. //
  152. return 1;
  153. }
  154. int
  155. RleCompressor::compress (const char *inPtr,
  156. int inSize,
  157. int minY,
  158. const char *&outPtr)
  159. {
  160. //
  161. // Special case ­- empty input buffer
  162. //
  163. if (inSize == 0)
  164. {
  165. outPtr = _outBuffer;
  166. return 0;
  167. }
  168. //
  169. // Reorder the pixel data.
  170. //
  171. {
  172. char *t1 = _tmpBuffer;
  173. char *t2 = _tmpBuffer + (inSize + 1) / 2;
  174. const char *stop = inPtr + inSize;
  175. while (true)
  176. {
  177. if (inPtr < stop)
  178. *(t1++) = *(inPtr++);
  179. else
  180. break;
  181. if (inPtr < stop)
  182. *(t2++) = *(inPtr++);
  183. else
  184. break;
  185. }
  186. }
  187. //
  188. // Predictor.
  189. //
  190. {
  191. unsigned char *t = (unsigned char *) _tmpBuffer + 1;
  192. unsigned char *stop = (unsigned char *) _tmpBuffer + inSize;
  193. int p = t[-1];
  194. while (t < stop)
  195. {
  196. int d = int (t[0]) - p + (128 + 256);
  197. p = t[0];
  198. t[0] = d;
  199. ++t;
  200. }
  201. }
  202. //
  203. // Run-length encode the data.
  204. //
  205. outPtr = _outBuffer;
  206. return rleCompress (inSize, _tmpBuffer, (signed char *) _outBuffer);
  207. }
  208. int
  209. RleCompressor::uncompress (const char *inPtr,
  210. int inSize,
  211. int minY,
  212. const char *&outPtr)
  213. {
  214. //
  215. // Special case ­- empty input buffer
  216. //
  217. if (inSize == 0)
  218. {
  219. outPtr = _outBuffer;
  220. return 0;
  221. }
  222. //
  223. // Decode the run-length encoded data
  224. //
  225. int outSize;
  226. if (0 == (outSize = rleUncompress (inSize, _maxScanLineSize,
  227. (const signed char *) inPtr,
  228. _tmpBuffer)))
  229. {
  230. throw Iex::InputExc ("Data decoding (rle) failed.");
  231. }
  232. //
  233. // Predictor.
  234. //
  235. {
  236. unsigned char *t = (unsigned char *) _tmpBuffer + 1;
  237. unsigned char *stop = (unsigned char *) _tmpBuffer + outSize;
  238. while (t < stop)
  239. {
  240. int d = int (t[-1]) + int (t[0]) - 128;
  241. t[0] = d;
  242. ++t;
  243. }
  244. }
  245. //
  246. // Reorder the pixel data.
  247. //
  248. {
  249. const char *t1 = _tmpBuffer;
  250. const char *t2 = _tmpBuffer + (outSize + 1) / 2;
  251. char *s = _outBuffer;
  252. char *stop = s + outSize;
  253. while (true)
  254. {
  255. if (s < stop)
  256. *(s++) = *(t1++);
  257. else
  258. break;
  259. if (s < stop)
  260. *(s++) = *(t2++);
  261. else
  262. break;
  263. }
  264. }
  265. outPtr = _outBuffer;
  266. return outSize;
  267. }
  268. } // namespace Imf