/src/freeimage-svn/FreeImage/trunk/Source/OpenEXR/IlmImf/ImfRle.cpp
Line  | Count  | Source  | 
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  | 
54  |  | rleCompress (int inLength, const char in[], signed char out[])  | 
55  | 0  | { | 
56  | 0  |     const char *inEnd = in + inLength;  | 
57  | 0  |     const char *runStart = in;  | 
58  | 0  |     const char *runEnd = in + 1;  | 
59  | 0  |     signed char *outWrite = out;  | 
60  |  | 
  | 
61  | 0  |     while (runStart < inEnd)  | 
62  | 0  |     { | 
63  | 0  |   while (runEnd < inEnd &&  | 
64  | 0  |          *runStart == *runEnd &&  | 
65  | 0  |          runEnd - runStart - 1 < MAX_RUN_LENGTH)  | 
66  | 0  |   { | 
67  | 0  |       ++runEnd;  | 
68  | 0  |   }  | 
69  |  | 
  | 
70  | 0  |   if (runEnd - runStart >= MIN_RUN_LENGTH)  | 
71  | 0  |   { | 
72  |  |       //  | 
73  |  |       // Compressable run  | 
74  |  |       //  | 
75  |  | 
  | 
76  | 0  |       *outWrite++ = (runEnd - runStart) - 1;  | 
77  | 0  |       *outWrite++ = *(signed char *) runStart;  | 
78  | 0  |       runStart = runEnd;  | 
79  | 0  |   }  | 
80  | 0  |   else  | 
81  | 0  |   { | 
82  |  |       //  | 
83  |  |       // Uncompressable run  | 
84  |  |       //  | 
85  |  | 
  | 
86  | 0  |       while (runEnd < inEnd &&  | 
87  | 0  |        ((runEnd + 1 >= inEnd ||  | 
88  | 0  |          *runEnd != *(runEnd + 1)) ||  | 
89  | 0  |         (runEnd + 2 >= inEnd ||  | 
90  | 0  |          *(runEnd + 1) != *(runEnd + 2))) &&  | 
91  | 0  |        runEnd - runStart < MAX_RUN_LENGTH)  | 
92  | 0  |       { | 
93  | 0  |     ++runEnd;  | 
94  | 0  |       }  | 
95  |  | 
  | 
96  | 0  |       *outWrite++ = runStart - runEnd;  | 
97  |  | 
  | 
98  | 0  |       while (runStart < runEnd)  | 
99  | 0  |       { | 
100  | 0  |     *outWrite++ = *(signed char *) (runStart++);  | 
101  | 0  |       }  | 
102  | 0  |   }  | 
103  |  | 
  | 
104  | 0  |   ++runEnd;  | 
105  | 0  |     }  | 
106  |  | 
  | 
107  | 0  |     return outWrite - out;  | 
108  | 0  | }  | 
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  | 
118  |  | rleUncompress (int inLength, int maxLength, const signed char in[], char out[])  | 
119  | 0  | { | 
120  | 0  |     char *outStart = out;  | 
121  |  | 
  | 
122  | 0  |     while (inLength > 0)  | 
123  | 0  |     { | 
124  | 0  |   if (*in < 0)  | 
125  | 0  |   { | 
126  | 0  |       int count = -((int)*in++);  | 
127  | 0  |       inLength -= count + 1;  | 
128  |  | 
  | 
129  | 0  |       if (0 > (maxLength -= count))  | 
130  | 0  |     return 0;  | 
131  |  |  | 
132  | 0  |         memcpy(out, in, count);  | 
133  | 0  |         out += count;  | 
134  | 0  |         in  += count;  | 
135  | 0  |   }  | 
136  | 0  |   else  | 
137  | 0  |   { | 
138  | 0  |       int count = *in++;  | 
139  | 0  |       inLength -= 2;  | 
140  |  | 
  | 
141  | 0  |       if (0 > (maxLength -= count + 1))  | 
142  | 0  |     return 0;  | 
143  |  |  | 
144  | 0  |         memset(out, *(char*)in, count+1);  | 
145  | 0  |         out += count+1;  | 
146  |  | 
  | 
147  | 0  |       in++;  | 
148  | 0  |   }  | 
149  | 0  |     }  | 
150  |  |  | 
151  | 0  |     return out - outStart;  | 
152  | 0  | }  | 
153  |  |  | 
154  |  |  | 
155  |  |  | 
156  |  |  | 
157  |  | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT  |