ImfRle.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <string.h>
  35. #include "ImfRle.h"
  36. #include "ImfNamespace.h"
  37. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  38. namespace {
  39. const int MIN_RUN_LENGTH = 3;
  40. const int MAX_RUN_LENGTH = 127;
  41. }
  42. //
  43. // Compress an array of bytes, using run-length encoding,
  44. // and return the length of the compressed data.
  45. //
  46. int
  47. rleCompress (int inLength, const char in[], signed char out[])
  48. {
  49. const char *inEnd = in + inLength;
  50. const char *runStart = in;
  51. const char *runEnd = in + 1;
  52. signed char *outWrite = out;
  53. while (runStart < inEnd)
  54. {
  55. while (runEnd < inEnd &&
  56. *runStart == *runEnd &&
  57. runEnd - runStart - 1 < MAX_RUN_LENGTH)
  58. {
  59. ++runEnd;
  60. }
  61. if (runEnd - runStart >= MIN_RUN_LENGTH)
  62. {
  63. //
  64. // Compressable run
  65. //
  66. *outWrite++ = (runEnd - runStart) - 1;
  67. *outWrite++ = *(signed char *) runStart;
  68. runStart = runEnd;
  69. }
  70. else
  71. {
  72. //
  73. // Uncompressable run
  74. //
  75. while (runEnd < inEnd &&
  76. ((runEnd + 1 >= inEnd ||
  77. *runEnd != *(runEnd + 1)) ||
  78. (runEnd + 2 >= inEnd ||
  79. *(runEnd + 1) != *(runEnd + 2))) &&
  80. runEnd - runStart < MAX_RUN_LENGTH)
  81. {
  82. ++runEnd;
  83. }
  84. *outWrite++ = runStart - runEnd;
  85. while (runStart < runEnd)
  86. {
  87. *outWrite++ = *(signed char *) (runStart++);
  88. }
  89. }
  90. ++runEnd;
  91. }
  92. return outWrite - out;
  93. }
  94. //
  95. // Uncompress an array of bytes compressed with rleCompress().
  96. // Returns the length of the oncompressed data, or 0 if the
  97. // length of the uncompressed data would be more than maxLength.
  98. //
  99. int
  100. rleUncompress (int inLength, int maxLength, const signed char in[], char out[])
  101. {
  102. char *outStart = out;
  103. while (inLength > 0)
  104. {
  105. if (*in < 0)
  106. {
  107. int count = -((int)*in++);
  108. inLength -= count + 1;
  109. if (0 > (maxLength -= count))
  110. return 0;
  111. memcpy(out, in, count);
  112. out += count;
  113. in += count;
  114. }
  115. else
  116. {
  117. int count = *in++;
  118. inLength -= 2;
  119. if (0 > (maxLength -= count + 1))
  120. return 0;
  121. memset(out, *(char*)in, count+1);
  122. out += count+1;
  123. in++;
  124. }
  125. }
  126. return out - outStart;
  127. }
  128. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT