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