Debian Codegraph @

src/cdogs/cwolfmap/expand.c

raw symbols SCIP
1 /*
2 ** Adapted from ecwolf/wolfmapcommon.cpp
3 ** https://bitbucket.org/ecwolf/ecwolf/src/c7615c4744d6143ad72fc27cb73db9a80b5ba153/src/resourcefiles/wolfmapcommon.cpp?at=master#wolfmapcommon.cpp-40,51,58,63,144,147
4 **
5 **---------------------------------------------------------------------------
6 ** Copyright 2013 Braden Obrzut
7 ** All rights reserved.
8 **
9 ** Redistribution and use in source and binary forms, with or without
10 ** modification, are permitted provided that the following conditions
11 ** are met:
12 **
13 ** 1. Redistributions of source code must retain the above copyright
14 ** notice, this list of conditions and the following disclaimer.
15 ** 2. Redistributions in binary form must reproduce the above copyright
16 ** notice, this list of conditions and the following disclaimer in the
17 ** documentation and/or other materials provided with the distribution.
18 ** 3. The name of the author may not be used to endorse or promote products
19 ** derived from this software without specific prior written permission.
20 **
21 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 **---------------------------------------------------------------------------
32 **
33 **
34 */
35 #include "expand.h"
36
37 #define CARMACK_NEARTAG 0xA7
38 #define CARMACK_FARTAG 0xA8
39
40 static WORD ReadLittleShort(const BYTE *const ptr)
41 {
42 return (WORD)((BYTE)*ptr) | ((WORD)(BYTE)(*(ptr + 1)) << 8);
43 }
44 static inline void WriteLittleShort(BYTE *const ptr, WORD value)
45 {
46 ptr[0] = value & 0xFF;
47 ptr[1] = (value >> 8) & 0xFF;
48 }
49
50 // http://www.shikadi.net/moddingwiki/Carmack_compression
51 void ExpandCarmack(const unsigned char *in, unsigned char *out)
52 {
53 const unsigned char *const end = out + ReadLittleShort((const BYTE *)in);
54 const unsigned char *const start = out;
55 in += 2;
56
57 const unsigned char *copy;
58 BYTE length;
59 while (out < end)
60 {
61 length = *in++;
62 if (length == 0 && (*in == CARMACK_NEARTAG || *in == CARMACK_FARTAG))
63 {
64 *out++ = in[1];
65 *out++ = in[0];
66 in += 2;
67 continue;
68 }
69 else if (*in == CARMACK_NEARTAG)
70 {
71 copy = out - (in[1] * 2);
72 in += 2;
73 }
74 else if (*in == CARMACK_FARTAG)
75 {
76 copy = start + (ReadLittleShort((const BYTE *)(in + 1)) * 2);
77 in += 3;
78 }
79 else
80 {
81 *out++ = length;
82 *out++ = *in++;
83 continue;
84 }
85 if (out + (length * 2) > end)
86 break;
87 while (length-- > 0)
88 {
89 *out++ = *copy++;
90 *out++ = *copy++;
91 }
92 }
93 }
94
95 // https://moddingwiki.shikadi.net/wiki/Id_Software_RLEW_compression
96 void ExpandRLEW(
97 const unsigned char *in, unsigned char *out, const WORD rlewTag,
98 const bool hasFinalLength)
99 {
100 DWORD length = 64 * 64 * 2; // width*height*<bytes in 16bit>
101 if (hasFinalLength)
102 {
103 length = ReadLittleShort((const BYTE *)in);
104 in += 2;
105 }
106 const unsigned char *const end = out + length;
107
108 while (out < end)
109 {
110 if (ReadLittleShort((const BYTE *)in) != rlewTag)
111 {
112 *out++ = *in++;
113 *out++ = *in++;
114 }
115 else
116 {
117 WORD count = ReadLittleShort((const BYTE *)(in + 2));
118 WORD input = ReadLittleShort((const BYTE *)(in + 4));
119 in += 6;
120 for (int i = 0; i < count; i++)
121 {
122 WriteLittleShort((BYTE *)out, input);
123 out += 2;
124 }
125 }
126 }
127 }