| 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 |
/* 8 bit decoding routines */ |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <cstdint> |
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "decoders.h" |
| 15 |
#include "console.h" |
| 16 |
|
| 17 |
#include "dxxsconf.h" |
| 18 |
#include "compiler-range_for.h" |
| 19 |
#include "d_range.h" |
| 20 |
#include <array> |
| 21 |
|
| 22 |
using namespace dcx; |
| 23 |
|
| 24 |
namespace d2x { |
| 25 |
|
| 26 |
static void dispatchDecoder(const uint8_t *vBackBuf1, const uint8_t *vBackBuf2, std::size_t width, std::size_t height, unsigned char **pFrame, unsigned char codeType, const unsigned char **pData, int *pDataRemain, int *curXb, int *curYb); |
| 27 |
|
| 28 |
void decodeFrame8(const uint8_t *const vBackBuf2, 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) |
| 29 |
{ |
| 30 |
const auto vBackBuf1{pFrame}; |
| 31 |
int xb, yb; |
| 32 |
|
| 33 |
xb = width >> 3; |
| 34 |
yb = height >> 3; |
| 35 |
for (int j=0; j<yb; j++) |
| 36 |
{ |
| 37 |
for (int i=0; i<xb/2; i++) |
| 38 |
{ |
| 39 |
const auto m = pMap.front(); |
| 40 |
dispatchDecoder(vBackBuf1, vBackBuf2, width, height, &pFrame, m & 0xf, &pData, &dataRemain, &i, &j); |
| 41 |
if (pFrame < vBackBuf1) |
| 42 |
con_printf(CON_CRITICAL, "danger! pointing out of bounds below after dispatch decoder: %d, %d (1) [%x]", i, j, m & 0xf); |
| 43 |
else if (pFrame >= vBackBuf1 + width*height) |
| 44 |
con_printf(CON_CRITICAL, "danger! pointing out of bounds above after dispatch decoder: %d, %d (1) [%x]", i, j, m & 0xf); |
| 45 |
dispatchDecoder(vBackBuf1, vBackBuf2, width, height, &pFrame, m >> 4, &pData, &dataRemain, &i, &j); |
| 46 |
if (pFrame < vBackBuf1) |
| 47 |
con_printf(CON_CRITICAL, "danger! pointing out of bounds below after dispatch decoder: %d, %d (2) [%x]", i, j, m >> 4); |
| 48 |
else if (pFrame >= vBackBuf1 + width*height) |
| 49 |
con_printf(CON_CRITICAL, "danger! pointing out of bounds above after dispatch decoder: %d, %d (2) [%x]", i, j, m >> 4); |
| 50 |
|
| 51 |
pMap = pMap.subspan<1>(); |
| 52 |
} |
| 53 |
pFrame += 7*width; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static void relClose(int i, int *x, int *y) |
| 58 |
{ |
| 59 |
int ma, mi; |
| 60 |
|
| 61 |
ma = i >> 4; |
| 62 |
mi = i & 0xf; |
| 63 |
|
| 64 |
*x = mi - 8; |
| 65 |
*y = ma - 8; |
| 66 |
} |
| 67 |
|
| 68 |
static void relFar(int i, int sign, int *x, int *y) |
| 69 |
{ |
| 70 |
if (i < 56) |
| 71 |
{ |
| 72 |
*x = sign * (8 + (i % 7)); |
| 73 |
*y = sign * (i / 7); |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
*x = sign * (-14 + (i - 56) % 29); |
| 78 |
*y = sign * (8 + (i - 56) / 29); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/* copies an 8x8 block from pSrc to pDest. |
| 83 |
pDest and pSrc are both g_width bytes wide */ |
| 84 |
static void copyFrame(const std::size_t width, uint8_t *pDest, const uint8_t *pSrc) |
| 85 |
{ |
| 86 |
range_for (const int i, xrange(8u)) |
| 87 |
{ |
| 88 |
(void)i; |
| 89 |
std::copy_n(pSrc, 8, pDest); |
| 90 |
std::advance(pDest, width); |
| 91 |
std::advance(pSrc, width); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Fill in the next eight bytes with p[0], p[1], p[2], or p[3], |
| 96 |
// depending on the corresponding two-bit value in pat0 and pat1 |
| 97 |
static void patternRow4Pixels(unsigned char *pFrame, |
| 98 |
unsigned char pat0, unsigned char pat1, |
| 99 |
const std::array<uint8_t, 4> &p) |
| 100 |
{ |
| 101 |
unsigned short mask=0x0003; |
| 102 |
unsigned short shift=0; |
| 103 |
unsigned short pattern = (pat1 << 8) | pat0; |
| 104 |
|
| 105 |
while (mask != 0) |
| 106 |
{ |
| 107 |
*pFrame++ = {p[(mask & pattern) >> shift]}; |
| 108 |
mask <<= 2; |
| 109 |
shift += 2; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Fill in the next four 2x2 pixel blocks with p[0], p[1], p[2], or p[3], |
| 114 |
// depending on the corresponding two-bit value in pat0. |
| 115 |
static void patternRow4Pixels2(const std::size_t width, unsigned char *pFrame, |
| 116 |
const uint8_t pat0, |
| 117 |
const std::array<uint8_t, 4> &p) |
| 118 |
{ |
| 119 |
unsigned char mask=0x03; |
| 120 |
unsigned char shift=0; |
| 121 |
|
| 122 |
while (mask != 0) |
| 123 |
{ |
| 124 |
const auto pel{p[(mask & pat0) >> shift]}; |
| 125 |
pFrame[0] = {pel}; |
| 126 |
pFrame[1] = {pel}; |
| 127 |
pFrame[width + 0] = {pel}; |
| 128 |
pFrame[width + 1] = {pel}; |
| 129 |
std::advance(pFrame, 2); |
| 130 |
mask <<= 2; |
| 131 |
shift += 2; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// Fill in the next four 2x1 pixel blocks with p[0], p[1], p[2], or p[3], |
| 136 |
// depending on the corresponding two-bit value in pat. |
| 137 |
static void patternRow4Pixels2x1(unsigned char *pFrame, const uint8_t pat, const std::array<uint8_t, 4> &p) |
| 138 |
{ |
| 139 |
unsigned char mask=0x03; |
| 140 |
unsigned char shift=0; |
| 141 |
|
| 142 |
while (mask != 0) |
| 143 |
{ |
| 144 |
const auto pel{p[(mask & pat) >> shift]}; |
| 145 |
pFrame[0] = {pel}; |
| 146 |
pFrame[1] = {pel}; |
| 147 |
std::advance(pFrame, 2); |
| 148 |
mask <<= 2; |
| 149 |
shift += 2; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// Fill in the next 4x4 pixel block with p[0], p[1], p[2], or p[3], |
| 154 |
// depending on the corresponding two-bit value in pat0, pat1, pat2, and pat3. |
| 155 |
static void patternQuadrant4Pixels(const std::size_t width, unsigned char *pFrame, unsigned char pat0, unsigned char pat1, unsigned char pat2, unsigned char pat3, const std::array<uint8_t, 4> &p) |
| 156 |
{ |
| 157 |
unsigned long mask = 0x00000003UL; |
| 158 |
int shift{0}; |
| 159 |
unsigned long pat = (pat3 << 24) | (pat2 << 16) | (pat1 << 8) | pat0; |
| 160 |
|
| 161 |
range_for (const int i, xrange(16u)) |
| 162 |
{ |
| 163 |
pFrame[i & 3] = {p[(pat & mask) >> shift]}; |
| 164 |
|
| 165 |
if ((i&3) == 3) |
| 166 |
std::advance(pFrame, width); |
| 167 |
|
| 168 |
mask <<= 2; |
| 169 |
shift += 2; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// fills the next 8 pixels with either p[0] or p[1], depending on pattern |
| 174 |
static void patternRow2Pixels(unsigned char *pFrame, const uint8_t pat, const std::array<uint8_t, 4> &p) |
| 175 |
{ |
| 176 |
unsigned char mask=0x01; |
| 177 |
|
| 178 |
while (mask != 0) |
| 179 |
{ |
| 180 |
*pFrame++ = {p[(mask & pat) ? 1 : 0]}; |
| 181 |
mask <<= 1; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// fills the next four 2 x 2 pixel boxes with either p[0] or p[1], depending on pattern |
| 186 |
static void patternRow2Pixels2(const std::size_t width, unsigned char *pFrame, const uint8_t pat, const std::array<uint8_t, 4> &p) |
| 187 |
{ |
| 188 |
unsigned char mask=0x1; |
| 189 |
while (mask != 0x10) |
| 190 |
{ |
| 191 |
const auto pel{p[(mask & pat) ? 1 : 0]}; |
| 192 |
pFrame[0] = {pel}; // upper-left |
| 193 |
pFrame[1] = {pel}; // upper-right |
| 194 |
pFrame[width + 0] = {pel}; // lower-left |
| 195 |
pFrame[width + 1] = {pel}; // lower-right |
| 196 |
std::advance(pFrame, 2); |
| 197 |
|
| 198 |
mask <<= 1; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// fills pixels in the next 4 x 4 pixel boxes with either p[0] or p[1], depending on pat0 and pat1. |
| 203 |
static void patternQuadrant2Pixels(const std::size_t width, unsigned char *pFrame, const uint8_t pat0, unsigned char pat1, const std::array<uint8_t, 4> &p) |
| 204 |
{ |
| 205 |
uint16_t mask{0x0001}; |
| 206 |
const uint16_t pat = (pat1 << 8) | pat0; |
| 207 |
range_for (const int i, xrange(4u)) |
| 208 |
{ |
| 209 |
range_for (const int j, xrange(4u)) |
| 210 |
{ |
| 211 |
pFrame[j + i * width] = {p[(pat & mask) ? 1 : 0]}; |
| 212 |
mask <<= 1; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
static void dispatchDecoder(const uint8_t *const vBackBuf1, const uint8_t *const vBackBuf2, const std::size_t width, const std::size_t height, unsigned char **pFrame, unsigned char codeType, const unsigned char **pData, int *pDataRemain, int *curXb, int *curYb) |
| 218 |
{ |
| 219 |
std::array<uint8_t, 4> p, pat; |
| 220 |
int x, y; |
| 221 |
|
| 222 |
/* Data is processed in 8x8 pixel blocks. |
| 223 |
There are 16 ways to encode each block. |
| 224 |
*/ |
| 225 |
|
| 226 |
switch(codeType) |
| 227 |
{ |
| 228 |
case 0x0: |
| 229 |
/* block is copied from block in current frame */ |
| 230 |
copyFrame(width, *pFrame, *pFrame + (vBackBuf2 - vBackBuf1)); |
| 231 |
[[fallthrough]]; |
| 232 |
case 0x1: |
| 233 |
/* block is unchanged from two frames ago */ |
| 234 |
*pFrame += 8; |
| 235 |
break; |
| 236 |
|
| 237 |
case 0x2: |
| 238 |
/* Block is copied from nearby (below and/or to the right) within the |
| 239 |
new frame. The offset within the buffer from which to grab the |
| 240 |
patch of 8 pixels is given by grabbing a byte B from the data |
| 241 |
stream, which is broken into a positive x and y offset according |
| 242 |
to the following mapping: |
| 243 |
|
| 244 |
if B < 56: |
| 245 |
x = 8 + (B % 7) |
| 246 |
y = B / 7 |
| 247 |
else |
| 248 |
x = -14 + ((B - 56) % 29) |
| 249 |
y = 8 + ((B - 56) / 29) |
| 250 |
*/ |
| 251 |
relFar(*(*pData)++, 1, &x, &y); |
| 252 |
copyFrame(width, *pFrame, *pFrame + x + y*width); |
| 253 |
*pFrame += 8; |
| 254 |
--*pDataRemain; |
| 255 |
break; |
| 256 |
|
| 257 |
case 0x3: |
| 258 |
/* Block is copied from nearby (above and/or to the left) within the |
| 259 |
new frame. |
| 260 |
|
| 261 |
if B < 56: |
| 262 |
x = -(8 + (B % 7)) |
| 263 |
y = -(B / 7) |
| 264 |
else |
| 265 |
x = -(-14 + ((B - 56) % 29)) |
| 266 |
y = -( 8 + ((B - 56) / 29)) |
| 267 |
*/ |
| 268 |
relFar(*(*pData)++, -1, &x, &y); |
| 269 |
copyFrame(width, *pFrame, *pFrame + x + y*width); |
| 270 |
*pFrame += 8; |
| 271 |
--*pDataRemain; |
| 272 |
break; |
| 273 |
|
| 274 |
case 0x4: |
| 275 |
/* Similar to 0x2 and 0x3, except this method copies from the |
| 276 |
"current" frame, rather than the "new" frame, and instead of the |
| 277 |
lopsided mapping they use, this one uses one which is symmetric |
| 278 |
and centered around the top-left corner of the block. This uses |
| 279 |
only 1 byte still, though, so the range is decreased, since we |
| 280 |
have to encode all directions in a single byte. The byte we pull |
| 281 |
from the data stream, I'll call B. Call the highest 4 bits of B |
| 282 |
BH and the lowest 4 bytes BL. Then the offset from which to copy |
| 283 |
the data is: |
| 284 |
|
| 285 |
x = -8 + BL |
| 286 |
y = -8 + BH |
| 287 |
*/ |
| 288 |
relClose(*(*pData)++, &x, &y); |
| 289 |
copyFrame(width, *pFrame, *pFrame + (vBackBuf2 - vBackBuf1) + x + y*width); |
| 290 |
*pFrame += 8; |
| 291 |
--*pDataRemain; |
| 292 |
break; |
| 293 |
|
| 294 |
case 0x5: |
| 295 |
/* Similar to 0x4, but instead of one byte for the offset, this uses |
| 296 |
two bytes to encode a larger range, the first being the x offset |
| 297 |
as a signed 8-bit value, and the second being the y offset as a |
| 298 |
signed 8-bit value. |
| 299 |
*/ |
| 300 |
x = static_cast<int8_t>(*(*pData)++); |
| 301 |
y = static_cast<int8_t>(*(*pData)++); |
| 302 |
copyFrame(width, *pFrame, *pFrame + (vBackBuf2 - vBackBuf1) + x + y*width); |
| 303 |
*pFrame += 8; |
| 304 |
*pDataRemain -= 2; |
| 305 |
break; |
| 306 |
|
| 307 |
case 0x6: |
| 308 |
/* I can't figure out how any file containing a block of this type |
| 309 |
could still be playable, since it appears that it would leave the |
| 310 |
internal bookkeeping in an inconsistent state in the BG player |
| 311 |
code. Ahh, well. Perhaps it was a bug in the BG player code that |
| 312 |
just didn't happen to be exposed by any of the included movies. |
| 313 |
Anyway, this skips the next two blocks, doing nothing to them. |
| 314 |
Note that if you've reached the end of a row, this means going on |
| 315 |
to the next row. |
| 316 |
*/ |
| 317 |
range_for (const int i, xrange(2u)) |
| 318 |
{ |
| 319 |
(void)i; |
| 320 |
*pFrame += 16; |
| 321 |
if (++*curXb == (width >> 3)) |
| 322 |
{ |
| 323 |
*pFrame += 7 * width; |
| 324 |
*curXb = 0; |
| 325 |
if (++*curYb == (height >> 3)) |
| 326 |
return; |
| 327 |
} |
| 328 |
} |
| 329 |
break; |
| 330 |
|
| 331 |
case 0x7: |
| 332 |
/* Ok, here's where it starts to get really...interesting. This is, |
| 333 |
incidentally, the part where they started using self-modifying |
| 334 |
code. So, most of the following encodings are "patterned" blocks, |
| 335 |
where we are given a number of pixel values and then bitmapped |
| 336 |
values to specify which pixel values belong to which squares. For |
| 337 |
this encoding, we are given the following in the data stream: |
| 338 |
|
| 339 |
P0 P1 |
| 340 |
|
| 341 |
These are pixel values (i.e. 8-bit indices into the palette). If |
| 342 |
P0 <= P1, we then get 8 more bytes from the data stream, one for |
| 343 |
each row in the block: |
| 344 |
|
| 345 |
B0 B1 B2 B3 B4 B5 B6 B7 |
| 346 |
|
| 347 |
For each row, the leftmost pixel is represented by the low-order |
| 348 |
bit, and the rightmost by the high-order bit. Use your imagination |
| 349 |
in between. If a bit is set, the pixel value is P1 and if it is |
| 350 |
unset, the pixel value is P0. |
| 351 |
|
| 352 |
So, for example, if we had: |
| 353 |
|
| 354 |
11 22 fe 83 83 83 83 83 83 fe |
| 355 |
|
| 356 |
This would represent the following layout: |
| 357 |
|
| 358 |
11 22 22 22 22 22 22 22 ; fe == 11111110 |
| 359 |
22 22 11 11 11 11 11 22 ; 83 == 10000011 |
| 360 |
22 22 11 11 11 11 11 22 ; 83 == 10000011 |
| 361 |
22 22 11 11 11 11 11 22 ; 83 == 10000011 |
| 362 |
22 22 11 11 11 11 11 22 ; 83 == 10000011 |
| 363 |
22 22 11 11 11 11 11 22 ; 83 == 10000011 |
| 364 |
22 22 11 11 11 11 11 22 ; 83 == 10000011 |
| 365 |
11 22 22 22 22 22 22 22 ; fe == 11111110 |
| 366 |
|
| 367 |
If, on the other hand, P0 > P1, we get two more bytes from the |
| 368 |
data stream: |
| 369 |
|
| 370 |
B0 B1 |
| 371 |
|
| 372 |
Each of these bytes contains two 4-bit patterns. These patterns |
| 373 |
work like the patterns above with 8 bytes, except each bit |
| 374 |
represents a 2x2 pixel region. |
| 375 |
|
| 376 |
B0 contains the pattern for the top two rows and B1 contains |
| 377 |
the pattern for the bottom two rows. Note that the low-order |
| 378 |
nibble of each byte contains the pattern for the upper of the |
| 379 |
two rows that that byte controls. |
| 380 |
|
| 381 |
So if we had: |
| 382 |
|
| 383 |
22 11 7e 83 |
| 384 |
|
| 385 |
The output would be: |
| 386 |
|
| 387 |
11 11 22 22 22 22 22 22 ; e == 1 1 1 0 |
| 388 |
11 11 22 22 22 22 22 22 ; |
| 389 |
22 22 22 22 22 22 11 11 ; 7 == 0 1 1 1 |
| 390 |
22 22 22 22 22 22 11 11 ; |
| 391 |
11 11 11 11 11 11 22 22 ; 3 == 1 0 0 0 |
| 392 |
11 11 11 11 11 11 22 22 ; |
| 393 |
22 22 22 22 11 11 11 11 ; 8 == 0 0 1 1 |
| 394 |
22 22 22 22 11 11 11 11 ; |
| 395 |
*/ |
| 396 |
p[0] = *(*pData)++; |
| 397 |
p[1] = *(*pData)++; |
| 398 |
if (p[0] <= p[1]) |
| 399 |
{ |
| 400 |
range_for (const int i, xrange(8u)) |
| 401 |
{ |
| 402 |
(void)i; |
| 403 |
patternRow2Pixels(*pFrame, *(*pData)++, p); |
| 404 |
*pFrame += width; |
| 405 |
} |
| 406 |
} |
| 407 |
else |
| 408 |
{ |
| 409 |
const auto width2 = 2 * width; |
| 410 |
range_for (const int i, xrange(2u)) |
| 411 |
{ |
| 412 |
(void)i; |
| 413 |
patternRow2Pixels2(width, *pFrame, *(*pData) & 0xf, p); |
| 414 |
*pFrame += width2; |
| 415 |
patternRow2Pixels2(width, *pFrame, *(*pData)++ >> 4, p); |
| 416 |
*pFrame += width2; |
| 417 |
} |
| 418 |
} |
| 419 |
*pFrame -= (8 * width - 8); |
| 420 |
break; |
| 421 |
|
| 422 |
case 0x8: |
| 423 |
/* Ok, this one is basically like encoding 0x7, only more |
| 424 |
complicated. Again, we start out by getting two bytes on the data |
| 425 |
stream: |
| 426 |
|
| 427 |
P0 P1 |
| 428 |
|
| 429 |
if P0 <= P1 then we get the following from the data stream: |
| 430 |
|
| 431 |
B0 B1 |
| 432 |
P2 P3 B2 B3 |
| 433 |
P4 P5 B4 B5 |
| 434 |
P6 P7 B6 B7 |
| 435 |
|
| 436 |
P0 P1 and B0 B1 are used for the top-left corner, P2 P3 B2 B3 for |
| 437 |
the bottom-left corner, P4 P5 B4 B5 for the top-right, P6 P7 B6 B7 |
| 438 |
for the bottom-right. (So, each codes for a 4x4 pixel array.) |
| 439 |
Since we have 16 bits in B0 B1, there is one bit for each pixel in |
| 440 |
the array. The convention for the bit-mapping is, again, left to |
| 441 |
right and top to bottom. |
| 442 |
|
| 443 |
So, basically, the top-left quarter of the block is an arbitrary |
| 444 |
pattern with 2 pixels, the bottom-left a different arbitrary |
| 445 |
pattern with 2 different pixels, and so on. |
| 446 |
|
| 447 |
For example if the next 16 bytes were: |
| 448 |
|
| 449 |
00 22 f9 9f 44 55 aa 55 11 33 cc 33 66 77 01 ef |
| 450 |
|
| 451 |
We'd draw: |
| 452 |
|
| 453 |
22 22 22 22 | 11 11 33 33 ; f = 1111, c = 1100 |
| 454 |
22 00 00 22 | 11 11 33 33 ; 9 = 1001, c = 1100 |
| 455 |
22 00 00 22 | 33 33 11 11 ; 9 = 1001, 3 = 0011 |
| 456 |
22 22 22 22 | 33 33 11 11 ; f = 1111, 3 = 0011 |
| 457 |
------------+------------ |
| 458 |
44 55 44 55 | 66 66 66 66 ; a = 1010, 0 = 0000 |
| 459 |
44 55 44 55 | 77 66 66 66 ; a = 1010, 1 = 0001 |
| 460 |
55 44 55 44 | 66 77 77 77 ; 5 = 0101, e = 1110 |
| 461 |
55 44 55 44 | 77 77 77 77 ; 5 = 0101, f = 1111 |
| 462 |
|
| 463 |
I've added a dividing line in the above to clearly delineate the |
| 464 |
quadrants. |
| 465 |
|
| 466 |
|
| 467 |
Now, if P0 > P1 then we get 10 more bytes from the data stream: |
| 468 |
|
| 469 |
B0 B1 B2 B3 P2 P3 B4 B5 B6 B7 |
| 470 |
|
| 471 |
Now, if P2 <= P3, then the first six bytes [P0 P1 B0 B1 B2 B3] |
| 472 |
represent the left half of the block and the latter six bytes |
| 473 |
[P2 P3 B4 B5 B6 B7] represent the right half. |
| 474 |
|
| 475 |
For example: |
| 476 |
|
| 477 |
22 00 01 37 f7 31 11 66 8c e6 73 31 |
| 478 |
|
| 479 |
yeilds: |
| 480 |
|
| 481 |
22 22 22 22 | 11 11 11 66 ; 0: 0000 | 8: 1000 |
| 482 |
00 22 22 22 | 11 11 66 66 ; 1: 0001 | C: 1100 |
| 483 |
00 00 22 22 | 11 66 66 66 ; 3: 0011 | e: 1110 |
| 484 |
00 00 00 22 | 11 66 11 66 ; 7: 0111 | 6: 0101 |
| 485 |
00 00 00 00 | 66 66 66 11 ; f: 1111 | 7: 0111 |
| 486 |
00 00 00 22 | 66 66 11 11 ; 7: 0111 | 3: 0011 |
| 487 |
00 00 22 22 | 66 66 11 11 ; 3: 0011 | 3: 0011 |
| 488 |
00 22 22 22 | 66 11 11 11 ; 1: 0001 | 1: 0001 |
| 489 |
|
| 490 |
|
| 491 |
On the other hand, if P0 > P1 and P2 > P3, then |
| 492 |
[P0 P1 B0 B1 B2 B3] represent the top half of the |
| 493 |
block and [P2 P3 B4 B5 B6 B7] represent the bottom half. |
| 494 |
|
| 495 |
For example: |
| 496 |
|
| 497 |
22 00 cc 66 33 19 66 11 18 24 42 81 |
| 498 |
|
| 499 |
yeilds: |
| 500 |
|
| 501 |
22 22 00 00 22 22 00 00 ; cc: 11001100 |
| 502 |
22 00 00 22 22 00 00 22 ; 66: 01100110 |
| 503 |
00 00 22 22 00 00 22 22 ; 33: 00110011 |
| 504 |
00 22 22 00 00 22 22 22 ; 19: 00011001 |
| 505 |
----------------------- |
| 506 |
66 66 66 11 11 66 66 66 ; 18: 00011000 |
| 507 |
66 66 11 66 66 11 66 66 ; 24: 00100100 |
| 508 |
66 11 66 66 66 66 11 66 ; 42: 01000010 |
| 509 |
11 66 66 66 66 66 66 11 ; 81: 10000001 |
| 510 |
*/ |
| 511 |
if ( (*pData)[0] <= (*pData)[1]) |
| 512 |
{ |
| 513 |
// four quadrant case |
| 514 |
range_for (const int i, xrange(4u)) |
| 515 |
{ |
| 516 |
p[0] = *(*pData)++; |
| 517 |
p[1] = *(*pData)++; |
| 518 |
pat[0] = *(*pData)++; |
| 519 |
pat[1] = *(*pData)++; |
| 520 |
patternQuadrant2Pixels(width, *pFrame, pat[0], pat[1], p); |
| 521 |
|
| 522 |
// alternate between moving down and moving up and right |
| 523 |
if (i & 1) |
| 524 |
*pFrame += 4 - 4 * width; // up and right |
| 525 |
else |
| 526 |
*pFrame += 4 * width; // down |
| 527 |
} |
| 528 |
} |
| 529 |
else if ( (*pData)[6] <= (*pData)[7]) |
| 530 |
{ |
| 531 |
// split horizontal |
| 532 |
range_for (const int i, xrange(4u)) |
| 533 |
{ |
| 534 |
if ((i & 1) == 0) |
| 535 |
{ |
| 536 |
p[0] = *(*pData)++; |
| 537 |
p[1] = *(*pData)++; |
| 538 |
} |
| 539 |
pat[0] = *(*pData)++; |
| 540 |
pat[1] = *(*pData)++; |
| 541 |
patternQuadrant2Pixels(width, *pFrame, pat[0], pat[1], p); |
| 542 |
|
| 543 |
if (i & 1) |
| 544 |
*pFrame -= (4 * width - 4); |
| 545 |
else |
| 546 |
*pFrame += 4 * width; |
| 547 |
} |
| 548 |
} |
| 549 |
else |
| 550 |
{ |
| 551 |
// split vertical |
| 552 |
range_for (const int i, xrange(8u)) |
| 553 |
{ |
| 554 |
if ((i & 3) == 0) |
| 555 |
{ |
| 556 |
p[0] = *(*pData)++; |
| 557 |
p[1] = *(*pData)++; |
| 558 |
} |
| 559 |
patternRow2Pixels(*pFrame, *(*pData)++, p); |
| 560 |
*pFrame += width; |
| 561 |
} |
| 562 |
*pFrame -= (8 * width - 8); |
| 563 |
} |
| 564 |
break; |
| 565 |
|
| 566 |
case 0x9: |
| 567 |
/* Similar to the previous 2 encodings, only more complicated. And |
| 568 |
it will get worse before it gets better. No longer are we dealing |
| 569 |
with patterns over two pixel values. Now we are dealing with |
| 570 |
patterns over 4 pixel values with 2 bits assigned to each pixel |
| 571 |
(or block of pixels). |
| 572 |
|
| 573 |
So, first on the data stream are our 4 pixel values: |
| 574 |
|
| 575 |
P0 P1 P2 P3 |
| 576 |
|
| 577 |
Now, if P0 <= P1 AND P2 <= P3, we get 16 bytes of pattern, each |
| 578 |
2 bits representing a 1x1 pixel (00=P0, 01=P1, 10=P2, 11=P3). The |
| 579 |
ordering is again left to right and top to bottom. The most |
| 580 |
significant bits represent the left side at the top, and so on. |
| 581 |
|
| 582 |
If P0 <= P1 AND P2 > P3, we get 4 bytes of pattern, each 2 bits |
| 583 |
representing a 2x2 pixel. Ordering is left to right and top to |
| 584 |
bottom. |
| 585 |
|
| 586 |
if P0 > P1 AND P2 <= P3, we get 8 bytes of pattern, each 2 bits |
| 587 |
representing a 2x1 pixel (i.e. 2 pixels wide, and 1 high). |
| 588 |
|
| 589 |
if P0 > P1 AND P2 > P3, we get 8 bytes of pattern, each 2 bits |
| 590 |
representing a 1x2 pixel (i.e. 1 pixel wide, and 2 high). |
| 591 |
*/ |
| 592 |
if ( (*pData)[0] <= (*pData)[1]) |
| 593 |
{ |
| 594 |
if ( (*pData)[2] <= (*pData)[3]) |
| 595 |
{ |
| 596 |
p[0] = *(*pData)++; |
| 597 |
p[1] = *(*pData)++; |
| 598 |
p[2] = *(*pData)++; |
| 599 |
p[3] = *(*pData)++; |
| 600 |
|
| 601 |
range_for (const int i, xrange(8u)) |
| 602 |
{ |
| 603 |
(void)i; |
| 604 |
pat[0] = *(*pData)++; |
| 605 |
pat[1] = *(*pData)++; |
| 606 |
patternRow4Pixels(*pFrame, pat[0], pat[1], p); |
| 607 |
*pFrame += width; |
| 608 |
} |
| 609 |
|
| 610 |
*pFrame -= (8 * width - 8); |
| 611 |
} |
| 612 |
else |
| 613 |
{ |
| 614 |
p[0] = *(*pData)++; |
| 615 |
p[1] = *(*pData)++; |
| 616 |
p[2] = *(*pData)++; |
| 617 |
p[3] = *(*pData)++; |
| 618 |
|
| 619 |
patternRow4Pixels2(width, *pFrame, *(*pData)++, p); |
| 620 |
*pFrame += 2 * width; |
| 621 |
patternRow4Pixels2(width, *pFrame, *(*pData)++, p); |
| 622 |
*pFrame += 2 * width; |
| 623 |
patternRow4Pixels2(width, *pFrame, *(*pData)++, p); |
| 624 |
*pFrame += 2 * width; |
| 625 |
patternRow4Pixels2(width, *pFrame, *(*pData)++, p); |
| 626 |
*pFrame -= (6 * width - 8); |
| 627 |
} |
| 628 |
} |
| 629 |
else |
| 630 |
{ |
| 631 |
if ( (*pData)[2] <= (*pData)[3]) |
| 632 |
{ |
| 633 |
// draw 2x1 strips |
| 634 |
p[0] = *(*pData)++; |
| 635 |
p[1] = *(*pData)++; |
| 636 |
p[2] = *(*pData)++; |
| 637 |
p[3] = *(*pData)++; |
| 638 |
|
| 639 |
range_for (const int i, xrange(8u)) |
| 640 |
{ |
| 641 |
(void)i; |
| 642 |
pat[0] = *(*pData)++; |
| 643 |
patternRow4Pixels2x1(*pFrame, pat[0], p); |
| 644 |
*pFrame += width; |
| 645 |
} |
| 646 |
} |
| 647 |
else |
| 648 |
{ |
| 649 |
// draw 1x2 strips |
| 650 |
p[0] = *(*pData)++; |
| 651 |
p[1] = *(*pData)++; |
| 652 |
p[2] = *(*pData)++; |
| 653 |
p[3] = *(*pData)++; |
| 654 |
|
| 655 |
range_for (const int i, xrange(4u)) |
| 656 |
{ |
| 657 |
(void)i; |
| 658 |
pat[0] = *(*pData)++; |
| 659 |
pat[1] = *(*pData)++; |
| 660 |
patternRow4Pixels(*pFrame, pat[0], pat[1], p); |
| 661 |
*pFrame += width; |
| 662 |
patternRow4Pixels(*pFrame, pat[0], pat[1], p); |
| 663 |
*pFrame += width; |
| 664 |
} |
| 665 |
} |
| 666 |
*pFrame -= (8 * width - 8); |
| 667 |
} |
| 668 |
break; |
| 669 |
|
| 670 |
case 0xa: |
| 671 |
/* Similar to the previous, only a little more complicated. |
| 672 |
|
| 673 |
We are still dealing with patterns over 4 pixel values with 2 bits |
| 674 |
assigned to each pixel (or block of pixels). |
| 675 |
|
| 676 |
So, first on the data stream are our 4 pixel values: |
| 677 |
|
| 678 |
P0 P1 P2 P3 |
| 679 |
|
| 680 |
Now, if P0 <= P1, the block is divided into 4 quadrants, ordered |
| 681 |
(as with opcode 0x8) TL, BL, TR, BR. In this case the next data |
| 682 |
in the data stream should be: |
| 683 |
|
| 684 |
B0 B1 B2 B3 |
| 685 |
P4 P5 P6 P7 B4 B5 B6 B7 |
| 686 |
P8 P9 P10 P11 B8 B9 B10 B11 |
| 687 |
P12 P13 P14 P15 B12 B13 B14 B15 |
| 688 |
|
| 689 |
Each 2 bits represent a 1x1 pixel (00=P0, 01=P1, 10=P2, 11=P3). |
| 690 |
The ordering is again left to right and top to bottom. The most |
| 691 |
significant bits represent the right side at the top, and so on. |
| 692 |
|
| 693 |
If P0 > P1 then the next data on the data stream is: |
| 694 |
|
| 695 |
B0 B1 B2 B3 B4 B5 B6 B7 |
| 696 |
P4 P5 P6 P7 B8 B9 B10 B11 B12 B13 B14 B15 |
| 697 |
|
| 698 |
Now, in this case, if P4 <= P5, |
| 699 |
[P0 P1 P2 P3 B0 B1 B2 B3 B4 B5 B6 B7] represent the left half of |
| 700 |
the block and the other bytes represent the right half. If P4 > |
| 701 |
P5, then [P0 P1 P2 P3 B0 B1 B2 B3 B4 B5 B6 B7] represent the top |
| 702 |
half of the block and the other bytes represent the bottom half. |
| 703 |
*/ |
| 704 |
if ( (*pData)[0] <= (*pData)[1]) |
| 705 |
{ |
| 706 |
range_for (const int i, xrange(4u)) |
| 707 |
{ |
| 708 |
p[0] = *(*pData)++; |
| 709 |
p[1] = *(*pData)++; |
| 710 |
p[2] = *(*pData)++; |
| 711 |
p[3] = *(*pData)++; |
| 712 |
pat[0] = *(*pData)++; |
| 713 |
pat[1] = *(*pData)++; |
| 714 |
pat[2] = *(*pData)++; |
| 715 |
pat[3] = *(*pData)++; |
| 716 |
|
| 717 |
patternQuadrant4Pixels(width, *pFrame, pat[0], pat[1], pat[2], pat[3], p); |
| 718 |
|
| 719 |
if (i & 1) |
| 720 |
*pFrame -= (4 * width - 4); |
| 721 |
else |
| 722 |
*pFrame += 4 * width; |
| 723 |
} |
| 724 |
} |
| 725 |
else |
| 726 |
{ |
| 727 |
if ( (*pData)[12] <= (*pData)[13]) |
| 728 |
{ |
| 729 |
// split vertical |
| 730 |
range_for (const int i, xrange(4u)) |
| 731 |
{ |
| 732 |
if ((i&1) == 0) |
| 733 |
{ |
| 734 |
p[0] = *(*pData)++; |
| 735 |
p[1] = *(*pData)++; |
| 736 |
p[2] = *(*pData)++; |
| 737 |
p[3] = *(*pData)++; |
| 738 |
} |
| 739 |
|
| 740 |
pat[0] = *(*pData)++; |
| 741 |
pat[1] = *(*pData)++; |
| 742 |
pat[2] = *(*pData)++; |
| 743 |
pat[3] = *(*pData)++; |
| 744 |
|
| 745 |
patternQuadrant4Pixels(width, *pFrame, pat[0], pat[1], pat[2], pat[3], p); |
| 746 |
|
| 747 |
if (i & 1) |
| 748 |
*pFrame -= (4 * width - 4); |
| 749 |
else |
| 750 |
*pFrame += 4 * width; |
| 751 |
} |
| 752 |
} |
| 753 |
else |
| 754 |
{ |
| 755 |
// split horizontal |
| 756 |
range_for (const int i, xrange(8u)) |
| 757 |
{ |
| 758 |
if ((i&3) == 0) |
| 759 |
{ |
| 760 |
p[0] = *(*pData)++; |
| 761 |
p[1] = *(*pData)++; |
| 762 |
p[2] = *(*pData)++; |
| 763 |
p[3] = *(*pData)++; |
| 764 |
} |
| 765 |
|
| 766 |
pat[0] = *(*pData)++; |
| 767 |
pat[1] = *(*pData)++; |
| 768 |
patternRow4Pixels(*pFrame, pat[0], pat[1], p); |
| 769 |
*pFrame += width; |
| 770 |
} |
| 771 |
|
| 772 |
*pFrame -= (8 * width - 8); |
| 773 |
} |
| 774 |
} |
| 775 |
break; |
| 776 |
|
| 777 |
case 0xb: |
| 778 |
/* In this encoding we get raw pixel data in the data stream -- 64 |
| 779 |
bytes of pixel data. 1 byte for each pixel, and in the standard |
| 780 |
order (l->r, t->b). |
| 781 |
*/ |
| 782 |
range_for (const int i, xrange(8u)) |
| 783 |
{ |
| 784 |
(void)i; |
| 785 |
std::copy_n(*pData, 8, *pFrame); |
| 786 |
*pFrame += width; |
| 787 |
*pData += 8; |
| 788 |
*pDataRemain -= 8; |
| 789 |
} |
| 790 |
*pFrame -= (8 * width - 8); |
| 791 |
break; |
| 792 |
|
| 793 |
case 0xc: |
| 794 |
/* In this encoding we get raw pixel data in the data stream -- 16 |
| 795 |
bytes of pixel data. 1 byte for each block of 2x2 pixels, and in |
| 796 |
the standard order (l->r, t->b). |
| 797 |
*/ |
| 798 |
range_for (const int i, xrange(4u)) |
| 799 |
{ |
| 800 |
(void)i; |
| 801 |
range_for (const int j, xrange(2u)) |
| 802 |
{ |
| 803 |
(void)j; |
| 804 |
range_for (const int k, xrange(4u)) |
| 805 |
{ |
| 806 |
(*pFrame)[2 * k] = {(*pData)[k]}; |
| 807 |
(*pFrame)[2 * k + 1] = {(*pData)[k]}; |
| 808 |
} |
| 809 |
*pFrame += width; |
| 810 |
} |
| 811 |
*pData += 4; |
| 812 |
*pDataRemain -= 4; |
| 813 |
} |
| 814 |
*pFrame -= (8 * width - 8); |
| 815 |
break; |
| 816 |
|
| 817 |
case 0xd: |
| 818 |
/* In this encoding we get raw pixel data in the data stream -- 4 |
| 819 |
bytes of pixel data. 1 byte for each block of 4x4 pixels, and in |
| 820 |
the standard order (l->r, t->b). |
| 821 |
*/ |
| 822 |
range_for (const int i, xrange(2u)) |
| 823 |
{ |
| 824 |
(void)i; |
| 825 |
range_for (const int j, xrange(4u)) |
| 826 |
{ |
| 827 |
range_for (const int k, xrange(4u)) |
| 828 |
{ |
| 829 |
(*pFrame)[k * width + j] = {(*pData)[0]}; |
| 830 |
(*pFrame)[k * width + j + 4] = {(*pData)[1]}; |
| 831 |
} |
| 832 |
} |
| 833 |
*pFrame += 4 * width; |
| 834 |
*pData += 2; |
| 835 |
*pDataRemain -= 2; |
| 836 |
} |
| 837 |
*pFrame -= (8 * width - 8); |
| 838 |
break; |
| 839 |
|
| 840 |
case 0xe: |
| 841 |
/* This encoding represents a solid 8x8 frame. We get 1 byte of pixel |
| 842 |
data from the data stream. |
| 843 |
*/ |
| 844 |
{ |
| 845 |
const auto pel{**pData}; |
| 846 |
range_for (const int i, xrange(8u)) |
| 847 |
{ |
| 848 |
(void)i; |
| 849 |
std::fill_n(*pFrame, 8, pel); |
| 850 |
*pFrame += width; |
| 851 |
} |
| 852 |
++*pData; |
| 853 |
--*pDataRemain; |
| 854 |
*pFrame -= (8 * width - 8); |
| 855 |
} |
| 856 |
break; |
| 857 |
|
| 858 |
case 0xf: |
| 859 |
/* This encoding represents a "dithered" frame, which is |
| 860 |
checkerboarded with alternate pixels of two colors. We get 2 |
| 861 |
bytes of pixel data from the data stream, and these bytes are |
| 862 |
alternated: |
| 863 |
|
| 864 |
P0 P1 P0 P1 P0 P1 P0 P1 |
| 865 |
P1 P0 P1 P0 P1 P0 P1 P0 |
| 866 |
... |
| 867 |
P0 P1 P0 P1 P0 P1 P0 P1 |
| 868 |
P1 P0 P1 P0 P1 P0 P1 P0 |
| 869 |
*/ |
| 870 |
range_for (const int i, xrange(8u)) |
| 871 |
{ |
| 872 |
range_for (const int j, xrange(8u)) |
| 873 |
{ |
| 874 |
(*pFrame)[j] = {(*pData)[(i + j) & 1]}; |
| 875 |
} |
| 876 |
*pFrame += width; |
| 877 |
} |
| 878 |
*pData += 2; |
| 879 |
*pDataRemain -= 2; |
| 880 |
*pFrame -= (8 * width - 8); |
| 881 |
break; |
| 882 |
|
| 883 |
default: |
| 884 |
break; |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
} |