diff --git a/compress.cpp b/compress.cpp index 0abc068..6f751e4 100644 --- a/compress.cpp +++ b/compress.cpp @@ -2,6 +2,80 @@ #include #include +#include + +char *base64_encode(const unsigned char *data, int len) +{ + static const char table[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + int raw_len = 4 * ((len + 2) / 3); // base64 chars without newlines + int lines = raw_len / 80; // number of full 80-char lines + int out_len = raw_len + lines; // add newline per full line + + char *out = (char*)malloc(out_len + 1); // +1 for '\0' + if (!out) return NULL; + + int i = 0, j = 0, line_count = 0; + + while (i < len) { + unsigned int a = i < len ? data[i++] : 0; + unsigned int b = i < len ? data[i++] : 0; + unsigned int c = i < len ? data[i++] : 0; + + unsigned int triple = (a << 16) | (b << 8) | c; + + out[j++] = table[(triple >> 18) & 0x3F]; + out[j++] = table[(triple >> 12) & 0x3F]; + out[j++] = (i > len + 1) ? '=' : table[(triple >> 6) & 0x3F]; + out[j++] = (i > len) ? '=' : table[(triple) & 0x3F]; + + line_count += 4; + if (line_count == 80) { + out[j++] = '\n'; + line_count = 0; + } + } + + out[j] = '\0'; + return out; +} + +uint8_t *compress(uint8_t *map, int *size) +{ + bool on_zeros = false; + int zero_count = 0; + int &total_size = *size; + uint8_t *compressed = (uint8_t*)malloc(1440); + uint8_t *p = compressed; + for (int i=0;i<1440;++i) { + if (map[i]==0) { + if (on_zeros) { + zero_count++; + if (zero_count==255) { + on_zeros=false; + *(p++) = 255; + total_size++; + } + } else { + on_zeros=true; + zero_count=0; + *(p++) = 0; + total_size++; + } + } else { + if (on_zeros) { + on_zeros=false; + *(p++) = zero_count; + total_size++; + } + *(p++) = map[i]; + total_size++; + } + } + return compressed; +} + struct actor_t { uint8_t x, y, z, t; @@ -17,57 +91,61 @@ int main(int argc, char *argv[]) fread(buffer, 33, 1, f); size = buffer[0]; for (int j=0; j