Debian Codegraph @

nall/mosaic/context.hpp

raw
1 #ifdef NALL_MOSAIC_INTERNAL_HPP
2
3 namespace nall {
4 namespace mosaic {
5
6 struct context {
7 unsigned offset;
8 unsigned width;
9 unsigned height;
10 unsigned count;
11
12 bool endian; //0 = lsb, 1 = msb
13 bool order; //0 = linear, 1 = planar
14 unsigned depth; //1 - 24bpp
15
16 unsigned blockWidth;
17 unsigned blockHeight;
18 unsigned blockStride;
19 unsigned blockOffset;
20 vector<unsigned> block;
21
22 unsigned tileWidth;
23 unsigned tileHeight;
24 unsigned tileStride;
25 unsigned tileOffset;
26 vector<unsigned> tile;
27
28 unsigned mosaicWidth;
29 unsigned mosaicHeight;
30 unsigned mosaicStride;
31 unsigned mosaicOffset;
32 vector<unsigned> mosaic;
33
34 unsigned paddingWidth;
35 unsigned paddingHeight;
36 unsigned paddingColor;
37 vector<unsigned> palette;
38
39 unsigned objectWidth() const { return blockWidth * tileWidth * mosaicWidth + paddingWidth; }
40 unsigned objectHeight() const { return blockHeight * tileHeight * mosaicHeight + paddingHeight; }
41 unsigned objectSize() const {
42 unsigned size = blockStride * tileWidth * tileHeight * mosaicWidth * mosaicHeight
43 + blockOffset * tileHeight * mosaicWidth * mosaicHeight
44 + tileStride * mosaicWidth * mosaicHeight
45 + tileOffset * mosaicHeight;
46 return max(1u, size);
47 }
48
49 unsigned eval(const string& expression) {
50 if(auto result = Eval::integer(expression)) return result();
51 return 0u;
52 }
53
54 void eval(vector<unsigned>& buffer, const string& expression_) {
55 string expression = expression_;
56 bool function = false;
57 for(auto& c : expression) {
58 if(c == '(') function = true;
59 if(c == ')') function = false;
60 if(c == ',' && function == true) c = ';';
61 }
62
63 lstring list = expression.split(",");
64 for(auto& item : list) {
65 item.trim();
66 if(item.match("f(?*) ?*")) {
67 item.ltrim<1>("f(");
68 lstring part = item.split<1>(") ");
69 lstring args = part[0].split<3>(";");
70 for(auto &item : args) item.trim();
71
72 unsigned length = eval(args(0, "0"));
73 unsigned offset = eval(args(1, "0"));
74 unsigned stride = eval(args(2, "0"));
75 if(args.size() < 2) offset = buffer.size();
76 if(args.size() < 3) stride = 1;
77
78 for(unsigned n = 0; n < length; n++) {
79 string fn = part[1];
80 fn.replace("n", string{n});
81 fn.replace("o", string{offset});
82 fn.replace("p", string{buffer.size()});
83 buffer.resize(offset + 1);
84 buffer[offset] = eval(fn);
85 offset += stride;
86 }
87 } else if(item.match("base64*")) {
88 unsigned offset = 0;
89 item.ltrim<1>("base64");
90 if(item.match("(?*) *")) {
91 item.ltrim<1>("(");
92 lstring part = item.split<1>(") ");
93 offset = eval(part[0]);
94 item = part(1, "");
95 }
96 item.trim();
97 for(auto& c : item) {
98 if(c >= 'A' && c <= 'Z') buffer.append(offset + c - 'A' + 0);
99 if(c >= 'a' && c <= 'z') buffer.append(offset + c - 'a' + 26);
100 if(c >= '0' && c <= '9') buffer.append(offset + c - '0' + 52);
101 if(c == '-') buffer.append(offset + 62);
102 if(c == '_') buffer.append(offset + 63);
103 }
104 } else if(item.match("file *")) {
105 item.ltrim<1>("file ");
106 item.trim();
107 //...
108 } else if(item.empty() == false) {
109 buffer.append(eval(item));
110 }
111 }
112 }
113
114 void parse(const string& data) {
115 reset();
116
117 lstring lines = data.split("\n");
118 for(auto& line : lines) {
119 lstring part = line.split<1>(":");
120 if(part.size() != 2) continue;
121 part[0].trim();
122 part[1].trim();
123
124 if(part[0] == "offset") offset = eval(part[1]);
125 if(part[0] == "width") width = eval(part[1]);
126 if(part[0] == "height") height = eval(part[1]);
127 if(part[0] == "count") count = eval(part[1]);
128
129 if(part[0] == "endian") endian = eval(part[1]);
130 if(part[0] == "order") order = eval(part[1]);
131 if(part[0] == "depth") depth = eval(part[1]);
132
133 if(part[0] == "blockWidth") blockWidth = eval(part[1]);
134 if(part[0] == "blockHeight") blockHeight = eval(part[1]);
135 if(part[0] == "blockStride") blockStride = eval(part[1]);
136 if(part[0] == "blockOffset") blockOffset = eval(part[1]);
137 if(part[0] == "block") eval(block, part[1]);
138
139 if(part[0] == "tileWidth") tileWidth = eval(part[1]);
140 if(part[0] == "tileHeight") tileHeight = eval(part[1]);
141 if(part[0] == "tileStride") tileStride = eval(part[1]);
142 if(part[0] == "tileOffset") tileOffset = eval(part[1]);
143 if(part[0] == "tile") eval(tile, part[1]);
144
145 if(part[0] == "mosaicWidth") mosaicWidth = eval(part[1]);
146 if(part[0] == "mosaicHeight") mosaicHeight = eval(part[1]);
147 if(part[0] == "mosaicStride") mosaicStride = eval(part[1]);
148 if(part[0] == "mosaicOffset") mosaicOffset = eval(part[1]);
149 if(part[0] == "mosaic") eval(mosaic, part[1]);
150
151 if(part[0] == "paddingWidth") paddingWidth = eval(part[1]);
152 if(part[0] == "paddingHeight") paddingHeight = eval(part[1]);
153 if(part[0] == "paddingColor") paddingColor = eval(part[1]);
154 if(part[0] == "palette") eval(palette, part[1]);
155 }
156
157 sanitize();
158 }
159
160 bool load(const string& filename) {
161 string filedata = string::read(filename);
162 if(filedata.empty()) return false;
163 parse(filedata);
164 return true;
165 }
166
167 void sanitize() {
168 if(depth < 1) depth = 1;
169 if(depth > 24) depth = 24;
170
171 if(blockWidth < 1) blockWidth = 1;
172 if(blockHeight < 1) blockHeight = 1;
173
174 if(tileWidth < 1) tileWidth = 1;
175 if(tileHeight < 1) tileHeight = 1;
176
177 if(mosaicWidth < 1) mosaicWidth = 1;
178 if(mosaicHeight < 1) mosaicHeight = 1;
179
180 //set alpha to full opacity
181 paddingColor |= 255u << 24;
182 for(auto& color : palette) color |= 255u << 24;
183 }
184
185 void reset() {
186 offset = 0;
187 width = 0;
188 height = 0;
189 count = 0;
190
191 endian = 1;
192 order = 0;
193 depth = 1;
194
195 blockWidth = 1;
196 blockHeight = 1;
197 blockStride = 0;
198 blockOffset = 0;
199 block.reset();
200
201 tileWidth = 1;
202 tileHeight = 1;
203 tileStride = 0;
204 tileOffset = 0;
205 tile.reset();
206
207 mosaicWidth = 1;
208 mosaicHeight = 1;
209 mosaicStride = 0;
210 mosaicOffset = 0;
211 mosaic.reset();
212
213 paddingWidth = 0;
214 paddingHeight = 0;
215 paddingColor = 0;
216 palette.reset();
217 }
218
219 context() {
220 reset();
221 }
222 };
223
224 }
225 }
226
227 #endif