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