Debian Codegraph @

d2x-rebirth/libmve/decoder16.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /* 16 bit decoding routines */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <algorithm>
13 #include <cstdint>
14
15 #include "decoders.h"
16 #include "console.h"
17
18 #include "dxxsconf.h"
19 #include <array>
20 #include <utility>
21
22 using namespace dcx;
23
24 namespace d2x {
25
26 namespace {
27
28 static void dispatchDecoder16(const uint16_t *backBuf1, const uint16_t *backBuf2, std::size_t width, std::size_t height, unsigned short **pFrame, unsigned char codeType, const unsigned char **pData, const unsigned char **pOffData, int *pDataRemain, int *curXb, int *curYb);
29
30 }
31
32 void decodeFrame16(const uint16_t *const backBuf2, const std::size_t width, const std::size_t height, unsigned char *pFrame, std::span<const uint8_t> pMap, const unsigned char *pData, int dataRemain)
33 {
34 const auto backBuf1 = reinterpret_cast<const uint16_t *>(pFrame);
35 unsigned short offset;
36 auto FramePtr = reinterpret_cast<uint16_t *>(pFrame);
37 int length;
38 int op;
39 int i, j;
40 int xb, yb;
41
42 xb = width >> 3;
43 yb = height >> 3;
44
45 offset = pData[0]|(pData[1]<<8);
46
47 auto pOffData = pData + offset;
48
49 pData += 2;
50
51 auto pOrig = pData;
52 length = offset - 2; /*dataRemain-2;*/
53
54 for (j=0; j<yb; j++)
55 {
56 for (i=0; i<xb/2; i++)
57 {
58 const auto m = pMap.front();
59 op = m & 0xf;
60 dispatchDecoder16(backBuf1, backBuf2, width, height, &FramePtr, op, &pData, &pOffData, &dataRemain, &i, &j);
61
62 /*
63 if (FramePtr < backBuf1)
64 con_printf(CON_CRITICAL, "danger! pointing out of bounds below after dispatch decoder: %d, %d (1) [%x]", i, j, (*pMap) & 0xf);
65 else if (FramePtr >= backBuf1 + width*height)
66 con_printf(CON_CRITICAL, "danger! pointing out of bounds above after dispatch decoder: %d, %d (1) [%x]", i, j, (*pMap) & 0xf);
67 */
68
69 op = (m >> 4) & 0xf;
70 dispatchDecoder16(backBuf1, backBuf2, width, height, &FramePtr, op, &pData, &pOffData, &dataRemain, &i, &j);
71
72 /*
73 if (FramePtr < backBuf1)
74 con_printf(CON_CRITICAL, "danger! pointing out of bounds below after dispatch decoder: %d, %d (2) [%x]", i, j, (*pMap) >> 4);
75 else if (FramePtr >= backBuf1 + width*height)
76 con_printf(CON_CRITICAL, "danger! pointing out of bounds above after dispatch decoder: %d, %d (2) [%x]", i, j, (*pMap) >> 4);
77 */
78
79 pMap = pMap.subspan<1>();
80 }
81 FramePtr += 7*width;
82 }
83
84 const std::ptrdiff_t remaining = (pData - pOrig);
85 if (const std::ptrdiff_t difference = length - remaining)
86 {
87 con_printf(CON_CRITICAL, "DEBUG: junk left over: %d,%d,%d", DXX_ptrdiff_cast_int(remaining), length, DXX_ptrdiff_cast_int(difference));
88 }
89 }
90
91 namespace {
92
93 static uint16_t GETPIXEL(const unsigned char **buf, int off)
94 {
95 unsigned short val = (*buf)[0+off] | ((*buf)[1+off] << 8);
96 return val;
97 }
98
99 static uint16_t GETPIXELI(const unsigned char **buf, int off)
100 {
101 unsigned short val = (*buf)[0+off] | ((*buf)[1+off] << 8);
102 (*buf) += 2;
103 return val;
104 }
105
106 struct position_t
107 {
108 int x, y;
109 };
110
111 static constexpr position_t relClose(int i)
112 {
113 return {(i & 0xf) - 8, (i >> 4) - 8};
114 }
115
116 static constexpr position_t relFar0(int i, int sign)
117 {
118 return {
119 sign * (8 + (i % 7)),
120 sign * (i / 7)
121 };
122 }
123
124 static constexpr position_t relFar56(int i, int sign)
125 {
126 return {
127 sign * (-14 + (i - 56) % 29),
128 sign * (8 + (i - 56) / 29)
129 };
130 }
131
132 static constexpr position_t relFar(int i, int sign)
133 {
134 return (i < 56) ? relFar0(i, sign) : relFar56(i, sign);
135 }
136
137 struct lookup_table_t
138 {
139 std::array<position_t, 256> close, far_p, far_n;
140 };
141
142 template <std::size_t... N>
143 static constexpr lookup_table_t genLoopkupTable(std::index_sequence<N...>)
144 {
145 return lookup_table_t{
146 {{relClose(N)...}},
147 {{relFar(N, 1)...}},
148 {{relFar(N, -1)...}}
149 };
150 }
151
152 constexpr lookup_table_t lookup_table = genLoopkupTable(std::make_index_sequence<256>());
153
154 static void copyFrame(const std::size_t width, uint16_t *pDest, const uint16_t *pSrc)
155 {
156 int i;
157
158 for (i=0; i<8; i++)
159 {
160 std::copy_n(pSrc, 8, pDest);
161 std::advance(pDest, width);
162 std::advance(pSrc, width);
163 }
164 }
165
166 static void patternRow4Pixels(unsigned short *pFrame,
167 unsigned char pat0, unsigned char pat1,
168 const std::array<uint16_t, 4> &p)
169 {
170 unsigned short mask=0x0003;
171 unsigned short shift=0;
172 unsigned short pattern = (pat1 << 8) | pat0;
173
174 while (mask != 0)
175 {
176 *pFrame++ = {p[(mask & pattern) >> shift]};
177 mask <<= 2;
178 shift += 2;
179 }
180 }
181
182 static void patternRow4Pixels2(const std::size_t width, unsigned short *pFrame,
183 unsigned char pat0,
184 const std::array<uint16_t, 4> &p)
185 {
186 unsigned char mask=0x03;
187 unsigned char shift=0;
188 /* ORIGINAL VERSION IS BUGGY
189 int skip{1};
190
191 while (mask != 0)
192 {
193 pel = p[(mask & pat0) >> shift];
194 pFrame[0] = pel;
195 pFrame[2] = pel;
196 pFrame[g_width + 0] = pel;
197 pFrame[g_width + 2] = pel;
198 pFrame += skip;
199 skip = 4 - skip;
200 mask <<= 2;
201 shift += 2;
202 }
203 */
204 while (mask != 0)
205 {
206 const auto pel{p[(mask & pat0) >> shift]};
207 pFrame[0] = {pel};
208 pFrame[1] = {pel};
209 pFrame[width + 0] = {pel};
210 pFrame[width + 1] = {pel};
211 pFrame += 2;
212 mask <<= 2;
213 shift += 2;
214 }
215 }
216
217 static void patternRow4Pixels2x1(unsigned short *pFrame, unsigned char pat,
218 const std::array<uint16_t, 4> &p)
219 {
220 unsigned char mask=0x03;
221 unsigned char shift=0;
222
223 while (mask != 0)
224 {
225 const auto pel{p[(mask & pat) >> shift]};
226 pFrame[0] = {pel};
227 pFrame[1] = {pel};
228 pFrame += 2;
229 mask <<= 2;
230 shift += 2;
231 }
232 }
233
234 static void patternQuadrant4Pixels(const std::size_t width, unsigned short *pFrame,
235 unsigned char pat0, unsigned char pat1, unsigned char pat2,
236 unsigned char pat3, const std::array<uint16_t, 4> &p)
237 {
238 unsigned long mask = 0x00000003UL;
239 int shift{0};
240 int i;
241 unsigned long pat = (pat3 << 24) | (pat2 << 16) | (pat1 << 8) | pat0;
242
243 for (i=0; i<16; i++)
244 {
245 pFrame[i & 3] = {p[(pat & mask) >> shift]};
246
247 if ((i&3) == 3)
248 std::advance(pFrame, width);
249
250 mask <<= 2;
251 shift += 2;
252 }
253 }
254
255
256 static void patternRow2Pixels(unsigned short *pFrame, unsigned char pat,
257 const std::array<uint16_t, 4> &p)
258 {
259 unsigned char mask=0x01;
260
261 while (mask != 0)
262 {
263 *pFrame++ = {p[(mask & pat) ? 1 : 0]};
264 mask <<= 1;
265 }
266 }
267
268 static void patternRow2Pixels2(const std::size_t width, unsigned short *pFrame, unsigned char pat,
269 const std::array<uint16_t, 4> &p)
270 {
271 unsigned char mask=0x1;
272
273 /* ORIGINAL VERSION IS BUGGY
274 int skip{1};
275 while (mask != 0x10)
276 {
277 pel = p[(mask & pat) ? 1 : 0];
278 pFrame[0] = pel;
279 pFrame[2] = pel;
280 pFrame[g_width + 0] = pel;
281 pFrame[g_width + 2] = pel;
282 pFrame += skip;
283 skip = 4 - skip;
284 mask <<= 1;
285 }
286 */
287 while (mask != 0x10) {
288 const auto pel{p[(mask & pat) ? 1 : 0]};
289
290 pFrame[0] = {pel};
291 pFrame[1] = {pel};
292 pFrame[width + 0] = {pel};
293 pFrame[width + 1] = {pel};
294 std::advance(pFrame, 2);
295
296 mask <<= 1;
297 }
298 }
299
300 static void patternQuadrant2Pixels(const std::size_t width, unsigned short *pFrame, unsigned char pat0,
301 unsigned char pat1, const std::array<uint16_t, 4> &p)
302 {
303 unsigned short mask = 0x0001;
304 int i;
305 unsigned short pat = (pat1 << 8) | pat0;
306
307 for (i=0; i<16; i++)
308 {
309 pFrame[i & 3] = {p[(pat & mask) ? 1 : 0]};
310
311 if ((i&3) == 3)
312 std::advance(pFrame, width);
313
314 mask <<= 1;
315 }
316 }
317
318 static void dispatchDecoder16(const uint16_t *const backBuf1, const uint16_t *const backBuf2, const std::size_t width, const std::size_t height, unsigned short **pFrame, unsigned char codeType, const unsigned char **pData, const unsigned char **pOffData, int *pDataRemain, int *curXb, int *curYb)
319 {
320 std::array<uint16_t, 4> p;
321 std::array<uint8_t, 4> pat;
322 int i, j, k;
323 int x, y;
324 auto pDstBak = *pFrame;
325
326 switch(codeType)
327 {
328 case 0x0:
329 copyFrame(width, *pFrame, *pFrame + (backBuf2 - backBuf1));
330 case 0x1:
331 break;
332 case 0x2: /*
333 relFar(*(*pOffData)++, 1, &x, &y);
334 */
335
336 k = *(*pOffData)++;
337 x = lookup_table.far_p[k].x;
338 y = lookup_table.far_p[k].y;
339
340 copyFrame(width, *pFrame, *pFrame + x + y*width);
341 --*pDataRemain;
342 break;
343 case 0x3: /*
344 relFar(*(*pOffData)++, -1, &x, &y);
345 */
346
347 k = *(*pOffData)++;
348 x = lookup_table.far_n[k].x;
349 y = lookup_table.far_n[k].y;
350
351 copyFrame(width, *pFrame, *pFrame + x + y*width);
352 --*pDataRemain;
353 break;
354 case 0x4: /*
355 relClose(*(*pOffData)++, &x, &y);
356 */
357
358 k = *(*pOffData)++;
359 x = lookup_table.close[k].x;
360 y = lookup_table.close[k].y;
361
362 copyFrame(width, *pFrame, *pFrame + (backBuf2 - backBuf1) + x + y*width);
363 --*pDataRemain;
364 break;
365 case 0x5:
366 x = static_cast<char>(*(*pData)++);
367 y = static_cast<char>(*(*pData)++);
368 copyFrame(width, *pFrame, *pFrame + (backBuf2 - backBuf1) + x + y*width);
369 *pDataRemain -= 2;
370 break;
371 case 0x6:
372 con_puts(CON_CRITICAL, "STUB: encoding 6 not tested");
373 for (i=0; i<2; i++)
374 {
375 *pFrame += 16;
376 if (++*curXb == (width >> 3))
377 {
378 *pFrame += 7*width;
379 *curXb = 0;
380 if (++*curYb == (height >> 3))
381 return;
382 }
383 }
384 break;
385
386 case 0x7:
387 p[0] = GETPIXELI(pData, 0);
388 p[1] = GETPIXELI(pData, 0);
389
390 if (!((p[0]/*|p[1]*/)&0x8000))
391 {
392 for (i=0; i<8; i++)
393 {
394 patternRow2Pixels(*pFrame, *(*pData), p);
395 (*pData)++;
396
397 *pFrame += width;
398 }
399 }
400 else
401 {
402 for (i=0; i<2; i++)
403 {
404 patternRow2Pixels2(width, *pFrame, *(*pData) & 0xf, p);
405 *pFrame += 2*width;
406 patternRow2Pixels2(width, *pFrame, *(*pData) >> 4, p);
407 (*pData)++;
408
409 *pFrame += 2*width;
410 }
411 }
412 break;
413
414 case 0x8:
415 p[0] = GETPIXEL(pData, 0);
416
417 if (!(p[0] & 0x8000))
418 {
419 for (i=0; i<4; i++)
420 {
421 p[0] = GETPIXELI(pData, 0);
422 p[1] = GETPIXELI(pData, 0);
423
424 pat[0] = (*pData)[0];
425 pat[1] = (*pData)[1];
426 (*pData) += 2;
427
428 patternQuadrant2Pixels(width, *pFrame, pat[0], pat[1], p);
429
430 if (i & 1)
431 *pFrame -= (4*width - 4);
432 else
433 *pFrame += 4*width;
434 }
435
436
437 } else {
438 p[2] = GETPIXEL(pData, 8);
439
440 if (!(p[2]&0x8000)) {
441 for (i=0; i<4; i++)
442 {
443 if ((i & 1) == 0)
444 {
445 p[0] = GETPIXELI(pData, 0);
446 p[1] = GETPIXELI(pData, 0);
447 }
448 pat[0] = *(*pData)++;
449 pat[1] = *(*pData)++;
450 patternQuadrant2Pixels(width, *pFrame, pat[0], pat[1], p);
451
452 if (i & 1)
453 *pFrame -= (4*width - 4);
454 else
455 *pFrame += 4*width;
456 }
457 } else {
458 for (i=0; i<8; i++)
459 {
460 if ((i & 3) == 0)
461 {
462 p[0] = GETPIXELI(pData, 0);
463 p[1] = GETPIXELI(pData, 0);
464 }
465 patternRow2Pixels(*pFrame, *(*pData), p);
466 (*pData)++;
467
468 *pFrame += width;
469 }
470 }
471 }
472 break;
473
474 case 0x9:
475 p[0] = GETPIXELI(pData, 0);
476 p[1] = GETPIXELI(pData, 0);
477 p[2] = GETPIXELI(pData, 0);
478 p[3] = GETPIXELI(pData, 0);
479
480 *pDataRemain -= 8;
481
482 if (!(p[0] & 0x8000))
483 {
484 if (!(p[2] & 0x8000))
485 {
486
487 for (i=0; i<8; i++)
488 {
489 pat[0] = (*pData)[0];
490 pat[1] = (*pData)[1];
491 (*pData) += 2;
492 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
493 *pFrame += width;
494 }
495 *pDataRemain -= 16;
496
497 }
498 else
499 {
500 patternRow4Pixels2(width, *pFrame, (*pData)[0], p);
501 *pFrame += 2*width;
502 patternRow4Pixels2(width, *pFrame, (*pData)[1], p);
503 *pFrame += 2*width;
504 patternRow4Pixels2(width, *pFrame, (*pData)[2], p);
505 *pFrame += 2*width;
506 patternRow4Pixels2(width, *pFrame, (*pData)[3], p);
507
508 (*pData) += 4;
509 *pDataRemain -= 4;
510
511 }
512 }
513 else
514 {
515 if (!(p[2] & 0x8000))
516 {
517 for (i=0; i<8; i++)
518 {
519 pat[0] = (*pData)[0];
520 (*pData) += 1;
521 patternRow4Pixels2x1(*pFrame, pat[0], p);
522 *pFrame += width;
523 }
524 *pDataRemain -= 8;
525 }
526 else
527 {
528 for (i=0; i<4; i++)
529 {
530 pat[0] = (*pData)[0];
531 pat[1] = (*pData)[1];
532
533 (*pData) += 2;
534
535 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
536 *pFrame += width;
537 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
538 *pFrame += width;
539 }
540 *pDataRemain -= 8;
541 }
542 }
543 break;
544
545 case 0xa:
546 p[0] = GETPIXEL(pData, 0);
547
548 if (!(p[0] & 0x8000))
549 {
550 for (i=0; i<4; i++)
551 {
552 p[0] = GETPIXELI(pData, 0);
553 p[1] = GETPIXELI(pData, 0);
554 p[2] = GETPIXELI(pData, 0);
555 p[3] = GETPIXELI(pData, 0);
556 pat[0] = (*pData)[0];
557 pat[1] = (*pData)[1];
558 pat[2] = (*pData)[2];
559 pat[3] = (*pData)[3];
560
561 (*pData) += 4;
562
563 patternQuadrant4Pixels(width, *pFrame, pat[0], pat[1], pat[2], pat[3], p);
564
565 if (i & 1)
566 *pFrame -= (4*width - 4);
567 else
568 *pFrame += 4*width;
569 }
570 }
571 else
572 {
573 p[0] = GETPIXEL(pData, 16);
574
575 if (!(p[0] & 0x8000))
576 {
577 for (i=0; i<4; i++)
578 {
579 if ((i&1) == 0)
580 {
581 p[0] = GETPIXELI(pData, 0);
582 p[1] = GETPIXELI(pData, 0);
583 p[2] = GETPIXELI(pData, 0);
584 p[3] = GETPIXELI(pData, 0);
585 }
586
587 pat[0] = (*pData)[0];
588 pat[1] = (*pData)[1];
589 pat[2] = (*pData)[2];
590 pat[3] = (*pData)[3];
591
592 (*pData) += 4;
593
594 patternQuadrant4Pixels(width, *pFrame, pat[0], pat[1], pat[2], pat[3], p);
595
596 if (i & 1)
597 *pFrame -= (4*width - 4);
598 else
599 *pFrame += 4*width;
600 }
601 }
602 else
603 {
604 for (i=0; i<8; i++)
605 {
606 if ((i&3) == 0)
607 {
608 p[0] = GETPIXELI(pData, 0);
609 p[1] = GETPIXELI(pData, 0);
610 p[2] = GETPIXELI(pData, 0);
611 p[3] = GETPIXELI(pData, 0);
612 }
613
614 pat[0] = (*pData)[0];
615 pat[1] = (*pData)[1];
616 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
617 *pFrame += width;
618
619 (*pData) += 2;
620 }
621 }
622 }
623 break;
624
625 case 0xb:
626 for (i=0; i<8; i++)
627 {
628 memcpy(*pFrame, *pData, 16);
629 *pFrame += width;
630 *pData += 16;
631 *pDataRemain -= 16;
632 }
633 break;
634
635 case 0xc:
636 for (i=0; i<4; i++)
637 {
638 p[0] = GETPIXEL(pData, 0);
639 p[1] = GETPIXEL(pData, 2);
640 p[2] = GETPIXEL(pData, 4);
641 p[3] = GETPIXEL(pData, 6);
642
643 for (j=0; j<2; j++)
644 {
645 for (k=0; k<4; k++)
646 {
647 const auto pel{p[k]};
648 (*pFrame)[j + 2 * k] = {pel};
649 (*pFrame)[width + j + 2 * k] = {pel};
650 }
651 std::advance(*pFrame, width);
652 }
653 *pData += 8;
654 *pDataRemain -= 8;
655 }
656 break;
657
658 case 0xd:
659 for (i=0; i<2; i++)
660 {
661 p[0] = GETPIXEL(pData, 0);
662 p[1] = GETPIXEL(pData, 2);
663 const auto p0{p[0]};
664 const auto p1{p[1]};
665
666 for (j=0; j<4; j++)
667 {
668 for (k=0; k<4; k++)
669 {
670 (*pFrame)[k * width + j] = {p0};
671 (*pFrame)[k * width + j + 4] = {p1};
672 }
673 }
674
675 *pFrame += 4*width;
676
677 *pData += 4;
678 *pDataRemain -= 4;
679 }
680 break;
681
682 case 0xe:
683 p[0] = GETPIXEL(pData, 0);
684 {
685 const auto p0{p[0]};
686
687 for (i = 0; i < 8; i++) {
688 for (j = 0; j < 8; j++) {
689 (*pFrame)[j] = {p0};
690 }
691
692 *pFrame += width;
693 }
694 }
695
696 *pData += 2;
697 *pDataRemain -= 2;
698
699 break;
700
701 case 0xf:
702 p[0] = GETPIXEL(pData, 0);
703 p[1] = GETPIXEL(pData, 1);
704
705 for (i=0; i<8; i++)
706 {
707 for (j=0; j<8; j++)
708 {
709 (*pFrame)[j] = {p[(i + j) & 1]};
710 }
711 *pFrame += width;
712 }
713
714 *pData += 4;
715 *pDataRemain -= 4;
716 break;
717
718 default:
719 break;
720 }
721
722 *pFrame = pDstBak+8;
723 }
724
725 }
726
727 }