- for (y = 0; y < 8; y += 2) {
- for (x = 0; x < 8; x += 2) {
- pix = *s->stream_ptr++;
- *(s->pixel_ptr + x) = pix;
- *(s->pixel_ptr + x + 1) = pix;
- *(s->pixel_ptr + s->stride + x) = pix;
- *(s->pixel_ptr + s->stride + x + 1) = pix;
+ /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
+ * either top and bottom or left and right halves */
+ if (!(P[0] & 0x8000)) {
+
+ /* 4-color encoding for each quadrant */
+ for (y = 0; y < 16; y++) {
+ // new values for each 4x4 block
+ if (!(y & 3)) {
+ if (y)
+ for (x = 0; x < 4; x++)
+ P[x] = bytestream2_get_le16(&s->stream_ptr);
+ flags = bytestream2_get_le32(&s->stream_ptr);
+ }
+
+ for (x = 0; x < 4; x++, flags >>= 2)
+ *pixel_ptr++ = P[flags & 0x03];
+
+ pixel_ptr += s->stride - 4;
+ // switch to right half
+ if (y == 7) pixel_ptr -= 8 * s->stride - 4;
+ }
+
+ } else {
+ // vertical split?
+ int vert;
+ uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
+
+ for (x = 4; x < 8; x++)
+ P[x] = bytestream2_get_le16(&s->stream_ptr);
+ vert = !(P[4] & 0x8000);
+
+ /* 4-color encoding for either left and right or top and bottom
+ * halves */
+
+ for (y = 0; y < 16; y++) {
+ for (x = 0; x < 4; x++, flags >>= 2)
+ *pixel_ptr++ = P[flags & 0x03];
+
+ if (vert) {
+ pixel_ptr += s->stride - 4;
+ // switch to right half
+ if (y == 7) pixel_ptr -= 8 * s->stride - 4;
+ } else if (y & 1) pixel_ptr += s->line_inc;
+
+ // load values for second half
+ if (y == 7) {
+ memcpy(P, P + 4, 8);
+ flags = bytestream2_get_le64(&s->stream_ptr);
+ }