yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
snes_tile.cc
Go to the documentation of this file.
1#include "snes_tile.h"
2
3#include <cassert>
4#include <cstdint>
5#include <stdexcept>
6#include <vector>
7
8namespace yaze {
9namespace gfx {
10
11// Bit set for object priority
12constexpr uint16_t TilePriorityBit = 0x2000;
13
14// Bit set for object hflip
15constexpr uint16_t TileHFlipBit = 0x4000;
16
17// Bit set for object vflip
18constexpr uint16_t TileVFlipBit = 0x8000;
19
20// Bits used for tile name
21constexpr uint16_t TileNameMask = 0x03FF;
22
23snes_tile8 UnpackBppTile(std::span<uint8_t> data, const uint32_t offset,
24 const uint32_t bpp) {
25 snes_tile8 tile = {}; // Initialize to zero
26 assert(bpp >= 1 && bpp <= 8);
27 unsigned int bpp_pos[8]; // More for conveniance and readibility
28 for (int row = 0; row < 8; row++) { // Process rows first (Y coordinate)
29 for (int col = 0; col < 8; col++) { // Then columns (X coordinate)
30 if (bpp == 1) {
31 tile.data[row * 8 + col] = (data[offset + row] >> (7 - col)) & 0x01;
32 continue;
33 }
34 /* SNES bpp format interlace each byte of the first 2 bitplanes.
35 * | byte 1 of first bitplane | byte 1 of second bitplane |
36 * | byte 2 of first bitplane | byte 2 of second bitplane | ..
37 */
38 bpp_pos[0] = offset + row * 2;
39 bpp_pos[1] = offset + row * 2 + 1;
40 char mask = 1 << (7 - col);
41 tile.data[row * 8 + col] = (data[bpp_pos[0]] & mask) ? 1 : 0;
42 tile.data[row * 8 + col] |= ((data[bpp_pos[1]] & mask) ? 1 : 0) << 1;
43 if (bpp == 3) {
44 // When we have 3 bitplanes, the bytes for the third bitplane are after
45 // the 16 bytes of the 2 bitplanes.
46 bpp_pos[2] = offset + 16 + row;
47 tile.data[row * 8 + col] |= ((data[bpp_pos[2]] & mask) ? 1 : 0) << 2;
48 }
49 if (bpp >= 4) {
50 // For 4 bitplanes, the 2 added bitplanes are interlaced like the first
51 // two.
52 bpp_pos[2] = offset + 16 + row * 2;
53 bpp_pos[3] = offset + 16 + row * 2 + 1;
54 tile.data[row * 8 + col] |= ((data[bpp_pos[2]] & mask) ? 1 : 0) << 2;
55 tile.data[row * 8 + col] |= ((data[bpp_pos[3]] & mask) ? 1 : 0) << 3;
56 }
57 if (bpp == 8) {
58 bpp_pos[4] = offset + 32 + row * 2;
59 bpp_pos[5] = offset + 32 + row * 2 + 1;
60 bpp_pos[6] = offset + 48 + row * 2;
61 bpp_pos[7] = offset + 48 + row * 2 + 1;
62 tile.data[row * 8 + col] |= ((data[bpp_pos[4]] & mask) ? 1 : 0) << 4;
63 tile.data[row * 8 + col] |= ((data[bpp_pos[5]] & mask) ? 1 : 0) << 5;
64 tile.data[row * 8 + col] |= ((data[bpp_pos[6]] & mask) ? 1 : 0) << 6;
65 tile.data[row * 8 + col] |= ((data[bpp_pos[7]] & mask) ? 1 : 0) << 7;
66 }
67 }
68 }
69 return tile;
70}
71
72std::vector<uint8_t> PackBppTile(const snes_tile8& tile, const uint32_t bpp) {
73 // Allocate memory for output data
74 std::vector<uint8_t> output(bpp * 8, 0); // initialized with 0
75 unsigned maxcolor = 1 << bpp; // Fix: should be 1 << bpp, not 2 << bpp
76
77 // Iterate over all rows and columns of the tile
78 for (unsigned int row = 0; row < 8; row++) {
79 for (unsigned int col = 0; col < 8; col++) {
80 uint8_t color = tile.data[row * 8 + col];
81 if (color >= maxcolor) {
82 throw std::invalid_argument("Invalid color value.");
83 }
84
85 // 1bpp format
86 if (bpp == 1)
87 output[row] += (uint8_t)((color & 1) << (7 - col));
88
89 // 2bpp format
90 if (bpp >= 2) {
91 output[row * 2] += ((color & 1) << (7 - col));
92 output[row * 2 + 1] += (((color & 2) == 2) << (7 - col));
93 }
94
95 // 3bpp format
96 if (bpp == 3)
97 output[16 + row] += (((color & 4) == 4) << (7 - col));
98
99 // 4bpp format
100 if (bpp >= 4) {
101 output[16 + row * 2] += (((color & 4) == 4) << (7 - col));
102 output[16 + row * 2 + 1] += (((color & 8) == 8) << (7 - col));
103 }
104
105 // 8bpp format
106 if (bpp == 8) {
107 output[32 + row * 2] += (((color & 16) == 16) << (7 - col));
108 output[32 + row * 2 + 1] += (((color & 32) == 32) << (7 - col));
109 output[48 + row * 2] += (((color & 64) == 64) << (7 - col));
110 output[48 + row * 2 + 1] += (((color & 128) == 128) << (7 - col));
111 }
112 }
113 }
114 return output;
115}
116
117std::vector<uint8_t> ConvertBpp(std::span<uint8_t> tiles, uint32_t from_bpp,
118 uint32_t to_bpp) {
119 unsigned int nb_tile = tiles.size() / (from_bpp * 8);
120 std::vector<uint8_t> converted(nb_tile * to_bpp * 8);
121
122 for (unsigned int i = 0; i < nb_tile; i++) {
123 snes_tile8 tile = UnpackBppTile(tiles, i * from_bpp * 8, from_bpp);
124 std::vector<uint8_t> packed_tile = PackBppTile(tile, to_bpp);
125 std::memcpy(converted.data() + i * to_bpp * 8, packed_tile.data(),
126 to_bpp * 8);
127 }
128 return converted;
129}
130
131std::vector<uint8_t> SnesTo8bppSheet(std::span<uint8_t> sheet, int bpp,
132 int num_sheets) {
133 int xx = 0; // positions where we are at on the sheet
134 int yy = 0;
135 int pos = 0;
136 int ypos = 0;
137 int num_tiles = 64;
138 int buffer_size = 0x1000;
139
140 if (bpp == 2) {
141 bpp = 16;
142 num_tiles = 128;
143 buffer_size = 0x2000;
144 } else if (bpp == 3) {
145 bpp = 24;
146 } else if (bpp == 4) {
147 bpp = 32;
148 buffer_size = 0x4000;
149 } else if (bpp == 8) {
150 bpp = 64;
151 }
152
153 if (num_sheets != 1) {
154 num_tiles *= num_sheets;
155 buffer_size *= num_sheets;
156 }
157
158 // Safety check: Ensure input data is sufficient for requested tiles
159 if (static_cast<size_t>(num_tiles * bpp) > sheet.size()) {
160 // Clamp number of tiles to what we can read from input
161 // This prevents SIGSEGV if decompression returned truncated data
162 if (bpp > 0) {
163 num_tiles = static_cast<int>(sheet.size()) / bpp;
164 } else {
165 num_tiles = 0;
166 }
167 }
168
169 std::vector<uint8_t> sheet_buffer_out(buffer_size); // Zero initialized
170
171 for (int i = 0; i < num_tiles; i++) { // for each tiles, 16 per line
172 for (int y = 0; y < 8; y++) { // for each line
173 for (int x = 0; x < 8; x++) { //[0] + [1] + [16]
174 auto b1 = (sheet[(y * 2) + (bpp * pos)] & (kGraphicsBitmap[x]));
175 auto b2 = (sheet[((y * 2) + (bpp * pos)) + 1] & (kGraphicsBitmap[x]));
176 auto b3 = (sheet[(16 + y) + (bpp * pos)] & (kGraphicsBitmap[x]));
177 unsigned char b = 0;
178 if (b1 != 0) {
179 b |= 1;
180 }
181 if (b2 != 0) {
182 b |= 2;
183 }
184 if (b3 != 0 && bpp != 16) {
185 b |= 4;
186 }
187 sheet_buffer_out[x + xx + (y * 128) + (yy * 1024)] = b;
188 }
189 }
190 pos++;
191 ypos++;
192 xx += 8;
193 if (ypos >= 16) {
194 yy++;
195 xx = 0;
196 ypos = 0;
197 }
198 }
199 return sheet_buffer_out;
200}
201
202std::vector<uint8_t> Bpp8SnesToIndexed(std::vector<uint8_t> data,
203 uint64_t bpp) {
204 // 3BPP
205 // [r0,bp1],[r0,bp2],[r1,bp1],[r1,bp2],[r2,bp1],[r2,bp2],[r3,bp1],[r3,bp2]
206 // [r4,bp1],[r4,bp2],[r5,bp1],[r5,bp2],[r6,bp1],[r6,bp2],[r7,bp1],[r7,bp2]
207 // [r0,bp3],[r0,bp4],[r1,bp3],[r1,bp4],[r2,bp3],[r2,bp4],[r3,bp3],[r3,bp4]
208 // [r4,bp3],[r4,bp4],[r5,bp3],[r5,bp4],[r6,bp3],[r6,bp4],[r7,bp3],[r7,bp4]
209 // [r0,bp5],[r0,bp6],[r1,bp5],[r1,bp6],[r2,bp5],[r2,bp6],[r3,bp5],[r3,bp6]
210 // [r4,bp5],[r4,bp6],[r5,bp5],[r5,bp6],[r6,bp5],[r6,bp6],[r7,bp5],[r7,bp6]
211 // [r0,bp7],[r0,bp8],[r1,bp7],[r1,bp8],[r2,bp7],[r2,bp8],[r3,bp7],[r3,bp8]
212 // [r4,bp7],[r4,bp8],[r5,bp7],[r5,bp8],[r6,bp7],[r6,bp8],[r7,bp7],[r7,bp8]
213
214 // 16 tiles = 1024 bytes
215 auto buffer = std::vector<uint8_t>(data.size());
216 std::vector<std::vector<uint8_t>> bitmap_data;
217 bitmap_data.resize(0x80);
218 for (auto& each : bitmap_data) {
219 each.reserve(0x800);
220 }
221 int yy = 0;
222 int xx = 0;
223 int pos = 0;
224
225 const uint16_t sheet_width = 128;
226
227 // 64 = 4096 bytes
228 // 16 = 1024?
229 int ypos = 0;
230 // for each tiles //16 per lines
231 for (int i = 0; i < 4096; i++) {
232 // for each lines
233 for (int y = 0; y < 8; y++) {
234 //[0] + [1] + [16]
235 for (int x = 0; x < 8; x++) {
236 const uint16_t bitmask[] = {0x80, 0x40, 0x20, 0x10,
237 0x08, 0x04, 0x02, 0x01};
238 auto b1 = (data[(y * 2) + ((bpp * 8) * pos)] & (bitmask[x]));
239 auto b2 = (data[((y * 2) + ((bpp * 8) * pos)) + 1] & (bitmask[x]));
240 auto b3 = (data[(y * 2) + ((bpp * 8) * pos) + 16] & (bitmask[x]));
241 auto b4 = (data[(y * 2) + ((bpp * 8) * pos) + 17] & (bitmask[x]));
242 auto b5 = (data[(y * 2) + ((bpp * 8) * pos) + 32] & (bitmask[x]));
243 auto b6 = (data[(y * 2) + ((bpp * 8) * pos) + 33] & (bitmask[x]));
244 auto b7 = (data[(y * 2) + ((bpp * 8) * pos) + 48] & (bitmask[x]));
245 auto b8 = (data[(y * 2) + ((bpp * 8) * pos) + 49] & (bitmask[x]));
246
247 auto b = 0;
248 if (b1 != 0) {
249 b |= 1;
250 }
251 if (b2 != 0) {
252 b |= 2;
253 }
254 if (bpp >= 4) {
255 if (b3 != 0) {
256 b |= 4;
257 }
258 if (b4 != 0) {
259 b |= 8;
260 }
261 }
262 if (bpp >= 8) {
263 if (b5 != 0) {
264 b |= 0x10;
265 }
266 if (b6 != 0) {
267 b |= 0x20;
268 }
269 if (b7 != 0) {
270 b |= 0x40;
271 }
272 if (b8 != 0) {
273 b |= 0x80;
274 }
275 }
276 // bitmap_data[((x + xx) * sheet_width) + y + (yy * 8)] = b;
277 bitmap_data[x + xx][y + (yy * 8)] = b;
278 }
279 }
280 pos++;
281 ypos++;
282 xx += 8;
283 if (ypos >= 16) {
284 yy++;
285 xx = 0;
286 ypos = 0;
287 }
288 }
289
290 int n = 0;
291
292 for (int y = 0; y < (data.size() / 64); y++) {
293 for (int x = 0; x < sheet_width; x++) { // 128 assumption
294 if (n < data.size()) {
295 // buffer[n] = bitmap_data[(x * sheet_width) + y];
296 buffer[n] = bitmap_data[x][y];
297 n++;
298 }
299 }
300 }
301 return buffer;
302}
303
304uint16_t TileInfoToWord(TileInfo tile_info) {
305 uint16_t result = 0;
306
307 // Copy the id_ value
308 result |= tile_info.id_ & 0x3FF; // ids are 10 bits
309
310 // Set the vertical_mirror_, horizontal_mirror_, and over_ flags
311 result |= (tile_info.vertical_mirror_ ? 1 : 0) << 15;
312 result |= (tile_info.horizontal_mirror_ ? 1 : 0) << 14;
313 result |= (tile_info.over_ ? 1 : 0) << 13;
314
315 // Set the palette_
316 result |= (tile_info.palette_ & 0x07) << 10; // palettes are 3 bits
317
318 return result;
319}
320
321TileInfo WordToTileInfo(uint16_t word) {
322 // Extract the id_ value
323 uint16_t id = word & 0x3FF; // ids are 10 bits
324
325 // Extract the vertical_mirror_, horizontal_mirror_, and over_ flags
326 bool vertical_mirror = (word >> 15) & 0x01;
327 bool horizontal_mirror = (word >> 14) & 0x01;
328 bool over = (word >> 13) & 0x01;
329
330 // Extract the palette_
331 uint8_t palette = (word >> 10) & 0x07; // palettes are 3 bits
332
333 return TileInfo(id, palette, vertical_mirror, horizontal_mirror, over);
334}
335
336uint16_t TileInfoToShort(TileInfo tile_info) {
337 // uint16_t result = 0;
338
339 // Copy the id_ value
340 // result |= tile_info.id_ & 0x3FF; // ids are 10 bits
341
342 // Set the vertical_mirror_, horizontal_mirror_, and over_ flags
343 // result |= (tile_info.vertical_mirror_ ? 1 : 0) << 10;
344 // result |= (tile_info.horizontal_mirror_ ? 1 : 0) << 11;
345 // result |= (tile_info.over_ ? 1 : 0) << 12;
346
347 // Set the palette_
348 // result |= (tile_info.palette_ & 0x07) << 13; // palettes are 3 bits
349
350 uint16_t value = 0;
351 // vhopppcc cccccccc
352 if (tile_info.over_) {
353 value |= TilePriorityBit;
354 }
355 if (tile_info.horizontal_mirror_) {
356 value |= TileHFlipBit;
357 }
358 if (tile_info.vertical_mirror_) {
359 value |= TileVFlipBit;
360 }
361 value |= (uint16_t)((tile_info.palette_ << 10) & 0x1C00);
362 value |= (uint16_t)(tile_info.id_ & TileNameMask);
363
364 return value;
365}
366
367TileInfo GetTilesInfo(uint16_t tile) {
368 // vhopppcc cccccccc
369 uint16_t tid = (uint16_t)(tile & TileNameMask);
370 uint8_t p = (uint8_t)((tile >> 10) & 0x07);
371
372 bool o = ((tile & TilePriorityBit) == TilePriorityBit);
373 bool h = ((tile & TileHFlipBit) == TileHFlipBit);
374 bool v = ((tile & TileVFlipBit) == TileVFlipBit);
375
376 return TileInfo(tid, p, v, h, o);
377}
378
379void CopyTile8bpp16(int x, int y, int tile, std::vector<uint8_t>& bitmap,
380 std::vector<uint8_t>& blockset) {
381 int src_pos =
382 ((tile - ((tile / 0x08) * 0x08)) * 0x10) + ((tile / 0x08) * 2048);
383 int dest_pos = (x + (y * 0x200));
384 for (int yy = 0; yy < 0x10; yy++) {
385 for (int xx = 0; xx < 0x10; xx++) {
386 bitmap[dest_pos + xx + (yy * 0x200)] =
387 blockset[src_pos + xx + (yy * 0x80)];
388 }
389 }
390}
391
393 std::span<uint8_t> src) {
394 std::vector<uint8_t> dest;
395 uint8_t b0;
396 uint8_t b1;
397 uint8_t b2;
398 uint8_t b3;
399 int res;
400 int mul;
401 int y_adder = 0;
402 int src_index;
403 int dest_x;
404 int dest_y;
405 int dest_index;
406 int main_index_limit = src.size() / 32;
407 for (int main_index = 0; main_index <= main_index_limit; main_index += 32) {
408 src_index = (main_index << 5);
409 if (src_index + 31 >= src.size()) {
410 throw std::invalid_argument("src_index + 31 >= src.size()");
411 }
412 dest_x = main_index & 0x0F;
413 dest_y = main_index >> 4;
414 dest_index = ((dest_y << 7) + dest_x) << 3;
415 if (dest_index + 903 >= dest.size()) {
416 throw std::invalid_argument("dest_index + 903 >= dest.size()");
417 }
418 for (int i = 0; i < 16; i += 2) {
419 mul = 1;
420 b0 = src[src_index + i];
421 b1 = src[src_index + i + 1];
422 b2 = src[src_index + i + 16];
423 b3 = src[src_index + i + 17];
424 for (int j = 0; j < 8; j++) {
425 res = ((b0 & mul) | ((b1 & mul) << 1) | ((b2 & mul) << 2) |
426 ((b3 & mul) << 3)) >>
427 j;
428 dest[dest_index + (7 - j) + y_adder] = res;
429 mul <<= 1;
430 }
431 y_adder += 128;
432 }
433 }
434 return dest;
435}
436
437} // namespace gfx
438} // namespace yaze
SNES 16-bit tile metadata container.
Definition snes_tile.h:50
std::vector< uint8_t > LoadSNES4bppGFXToIndexedColorMatrix(std::span< uint8_t > src)
Definition snes_tile.cc:392
constexpr uint16_t TileNameMask
Definition snes_tile.cc:21
constexpr uint16_t TileVFlipBit
Definition snes_tile.cc:18
void CopyTile8bpp16(int x, int y, int tile, std::vector< uint8_t > &bitmap, std::vector< uint8_t > &blockset)
Definition snes_tile.cc:379
uint16_t TileInfoToWord(TileInfo tile_info)
Definition snes_tile.cc:304
uint16_t TileInfoToShort(TileInfo tile_info)
Definition snes_tile.cc:336
std::vector< uint8_t > PackBppTile(const snes_tile8 &tile, const uint32_t bpp)
Definition snes_tile.cc:72
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
Definition snes_tile.cc:202
constexpr uint16_t TilePriorityBit
Definition snes_tile.cc:12
std::vector< uint8_t > SnesTo8bppSheet(std::span< uint8_t > sheet, int bpp, int num_sheets)
Definition snes_tile.cc:131
snes_tile8 UnpackBppTile(std::span< uint8_t > data, const uint32_t offset, const uint32_t bpp)
Definition snes_tile.cc:23
TileInfo GetTilesInfo(uint16_t tile)
Definition snes_tile.cc:367
constexpr uint8_t kGraphicsBitmap[8]
Definition snes_tile.h:20
TileInfo WordToTileInfo(uint16_t word)
Definition snes_tile.cc:321
std::vector< uint8_t > ConvertBpp(std::span< uint8_t > tiles, uint32_t from_bpp, uint32_t to_bpp)
Definition snes_tile.cc:117
constexpr uint16_t TileHFlipBit
Definition snes_tile.cc:15
8x8 SNES tile data
Definition yaze.h:287
uint8_t data[64]
Definition yaze.h:290