ImfRleCompressor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "ImfRle.h"
  42. #include "Iex.h"
  43. #include "ImfNamespace.h"
  44. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  45. RleCompressor::RleCompressor (const Header &hdr, size_t maxScanLineSize):
  46. Compressor (hdr),
  47. _maxScanLineSize (maxScanLineSize),
  48. _tmpBuffer (0),
  49. _outBuffer (0)
  50. {
  51. _tmpBuffer = new char [maxScanLineSize];
  52. _outBuffer = new char [uiMult (maxScanLineSize, size_t (3)) / 2];
  53. }
  54. RleCompressor::~RleCompressor ()
  55. {
  56. delete [] _tmpBuffer;
  57. delete [] _outBuffer;
  58. }
  59. int
  60. RleCompressor::numScanLines () const
  61. {
  62. //
  63. // This compressor compresses individual scan lines.
  64. //
  65. return 1;
  66. }
  67. int
  68. RleCompressor::compress (const char *inPtr,
  69. int inSize,
  70. int minY,
  71. const char *&outPtr)
  72. {
  73. //
  74. // Special case �- empty input buffer
  75. //
  76. if (inSize == 0)
  77. {
  78. outPtr = _outBuffer;
  79. return 0;
  80. }
  81. //
  82. // Reorder the pixel data.
  83. //
  84. {
  85. char *t1 = _tmpBuffer;
  86. char *t2 = _tmpBuffer + (inSize + 1) / 2;
  87. const char *stop = inPtr + inSize;
  88. while (true)
  89. {
  90. if (inPtr < stop)
  91. *(t1++) = *(inPtr++);
  92. else
  93. break;
  94. if (inPtr < stop)
  95. *(t2++) = *(inPtr++);
  96. else
  97. break;
  98. }
  99. }
  100. //
  101. // Predictor.
  102. //
  103. {
  104. unsigned char *t = (unsigned char *) _tmpBuffer + 1;
  105. unsigned char *stop = (unsigned char *) _tmpBuffer + inSize;
  106. int p = t[-1];
  107. while (t < stop)
  108. {
  109. int d = int (t[0]) - p + (128 + 256);
  110. p = t[0];
  111. t[0] = d;
  112. ++t;
  113. }
  114. }
  115. //
  116. // Run-length encode the data.
  117. //
  118. outPtr = _outBuffer;
  119. return rleCompress (inSize, _tmpBuffer, (signed char *) _outBuffer);
  120. }
  121. int
  122. RleCompressor::uncompress (const char *inPtr,
  123. int inSize,
  124. int minY,
  125. const char *&outPtr)
  126. {
  127. //
  128. // Special case �- empty input buffer
  129. //
  130. if (inSize == 0)
  131. {
  132. outPtr = _outBuffer;
  133. return 0;
  134. }
  135. //
  136. // Decode the run-length encoded data
  137. //
  138. int outSize;
  139. if (0 == (outSize = rleUncompress (inSize, _maxScanLineSize,
  140. (const signed char *) inPtr,
  141. _tmpBuffer)))
  142. {
  143. throw IEX_NAMESPACE::InputExc ("Data decoding (rle) failed.");
  144. }
  145. //
  146. // Predictor.
  147. //
  148. {
  149. unsigned char *t = (unsigned char *) _tmpBuffer + 1;
  150. unsigned char *stop = (unsigned char *) _tmpBuffer + outSize;
  151. while (t < stop)
  152. {
  153. int d = int (t[-1]) + int (t[0]) - 128;
  154. t[0] = d;
  155. ++t;
  156. }
  157. }
  158. //
  159. // Reorder the pixel data.
  160. //
  161. {
  162. const char *t1 = _tmpBuffer;
  163. const char *t2 = _tmpBuffer + (outSize + 1) / 2;
  164. char *s = _outBuffer;
  165. char *stop = s + outSize;
  166. while (true)
  167. {
  168. if (s < stop)
  169. *(s++) = *(t1++);
  170. else
  171. break;
  172. if (s < stop)
  173. *(s++) = *(t2++);
  174. else
  175. break;
  176. }
  177. }
  178. outPtr = _outBuffer;
  179. return outSize;
  180. }
  181. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT