Refactor pkmncompress.c to use common.h

Fixes #349
This commit is contained in:
Rangi 2022-03-17 20:36:13 -04:00
parent 570d83b73c
commit 07df4a5f88
2 changed files with 151 additions and 369 deletions

View file

@ -1,7 +1,7 @@
.PHONY: all clean .PHONY: all clean
CC := gcc CC := gcc
CFLAGS := -O3 -flto -std=c11 -Wall -Wextra -pedantic -Wno-missing-field-initializers CFLAGS := -O3 -flto -std=c11 -Wall -Wextra -pedantic
tools := \ tools := \
gfx \ gfx \

View file

@ -14,79 +14,46 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <stdlib.h> #define PROGRAM_NAME "pkmncompress"
#include <stdio.h> #define USAGE_OPTS "infile.2bpp outfile.pic"
#include <stdint.h>
#include <string.h>
typedef uint8_t u8; #include "common.h"
u8 *compressed = NULL; uint8_t compressed[15 * 15 * 0x10];
int xrows = 0; int cur_bit;
int xwidth = 0; int cur_byte;
int curbit = 0;
int curbyte = 0;
void writebit(int bit) void write_bit(int bit) {
{ if (++cur_bit == 8) {
if (++curbit == 8) cur_byte++;
{ cur_bit = 0;
curbyte++;
curbit = 0;
} }
compressed[curbyte] |= bit << (7 - curbit); compressed[cur_byte] |= bit << (7 - cur_bit);
} }
void method_1(u8 *RAM) void compress_plane(uint8_t *plane, int width) {
{ static int nybble_lookup[2][0x10] = {
int i;
int j;
int nibble_1;
int nibble_2 = 0;
int code_1;
int code_2;
int table;
static int method_1[2][0x10] = {
{0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9, 0x8}, {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9, 0x8},
{0x8, 0x9, 0xB, 0xA, 0xE, 0xF, 0xD, 0xC, 0x4, 0x5, 0x7, 0x6, 0x2, 0x3, 0x1, 0x0} {0x8, 0x9, 0xB, 0xA, 0xE, 0xF, 0xD, 0xC, 0x4, 0x5, 0x7, 0x6, 0x2, 0x3, 0x1, 0x0},
}; };
int ram_size = width * width * 8;
for (i = 0; i < xrows * xwidth * 8; i++) for (int i = 0, nybble_lo = 0; i < ram_size; i++) {
{ int m = i % width;
j = i / xrows; if (!m) {
j += i % xrows * xwidth * 8; nybble_lo = 0;
if (!(i % xrows))
{
nibble_2 = 0;
} }
nibble_1 = (RAM[j] >> 4) & 0x0F; int j = i / width + m * width * 8;
table = 0; int nybble_hi = (plane[j] >> 4) & 0xF;
if (nibble_2 & 1) int code_1 = nybble_lookup[nybble_lo & 1][nybble_hi];
{ nybble_lo = plane[j] & 0xF;
table = 1; int code_2 = nybble_lookup[nybble_hi & 1][nybble_lo];
} plane[j] = (code_1 << 4) | code_2;
code_1 = method_1[table][nibble_1];
nibble_2 = RAM[j] & 0x0F;
table = 0;
if (nibble_1 & 1)
{
table = 1;
}
code_2 = method_1[table][nibble_2];
RAM[j] = (code_1 << 4) | code_2;
} }
} }
// "Get the previous power of 2. Deriving the bitcount from that seems to be faster on average than using the lookup table." void rle_encode_number(int n) {
void RLE(int nums) int bit_count = -1;
{ int v = ++n;
int v;
int j;
int bitcount;
int number;
bitcount = -1;
v = ++nums;
v++; v++;
v |= v >> 1; v |= v >> 1;
v |= v >> 2; v |= v >> 2;
@ -95,357 +62,172 @@ void RLE(int nums)
v |= v >> 16; v |= v >> 16;
v -= v >> 1; v -= v >> 1;
v--; v--;
int number = n - v;
number = nums - v; while (v) {
while(v) {
v >>= 1; v >>= 1;
bitcount++; bit_count++;
} }
for(j = 0; j < bitcount; j++) { for (int j = 0; j < bit_count; j++) {
writebit(1); write_bit(1);
} }
writebit(0); write_bit(0);
for(j = bitcount; j >= 0; j--) { for (int j = bit_count; j >= 0; j--) {
writebit((number >> j) & 1); write_bit((number >> j) & 1);
} }
} }
void RLE_old(int nums) void write_data_packet(uint8_t *bit_groups, int n) {
{ for (int i = 0; i < n; i++) {
int search; write_bit((bit_groups[i] >> 1) & 1);
int i; write_bit(bit_groups[i] & 1);
int j; }
int bitcount; }
int number;
static int RLE[0x10] = {0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF};
bitcount = -1; int interpret_compress(uint8_t *plane1, uint8_t *plane2, int mode, int order, int width) {
search = ++nums; int ram_size = width * width * 8;
while (search > 0) uint8_t *_plane1 = xmalloc(ram_size);
{ uint8_t *_plane2 = xmalloc(ram_size);
for (i = 0; i < 0xF; i++) if (order) {
{ memcpy(_plane1, plane2, ram_size);
if (RLE[i] == search) memcpy(_plane2, plane1, ram_size);
{ } else {
bitcount = i; memcpy(_plane1, plane1, ram_size);
break; memcpy(_plane2, plane2, ram_size);
} }
if (mode != 1) {
for (int i = 0; i < ram_size; i++) {
_plane2[i] ^= _plane1[i];
} }
if (bitcount != -1)
{
break;
}
search--;
} }
number = nums - RLE[bitcount]; compress_plane(_plane1, width);
for (j = 0; j < bitcount; j++) if (mode != 2) {
{ compress_plane(_plane2, width);
writebit(1);
} }
writebit(0); cur_bit = 7;
for (j = bitcount; j >= 0; j--) cur_byte = 0;
{ memset(compressed, 0, sizeof(compressed) / sizeof(*compressed));
writebit((number >> j) & 1); compressed[0] = (width << 4) | width;
} write_bit(order);
} uint8_t bit_groups[0x1000] = {0};
int index = 0;
void data_packet(u8 *bitgroups, int bgi) for (int plane = 0; plane < 2; plane++) {
{ int type = 0;
int i; int nums = 0;
for (i = 0; i < bgi; i++) memset(bit_groups, 0, sizeof(bit_groups) / sizeof(*bit_groups));
{ for (int x = 0; x < width; x++) {
writebit((bitgroups[i] >> 1) & 1); for (int bit = 0; bit < 8; bit += 2) {
writebit(bitgroups[i] & 1); for (int y = 0, byte = x * width * 8; y < width * 8; y++, byte++) {
} int bit_group = ((plane ? _plane2 : _plane1)[byte] >> (6 - bit)) & 3;
} if (!bit_group) {
if (!type) {
int interpret_compress(u8 *RAM_1, u8 *RAM_2, int interpretation, int switchram) write_bit(0);
{ } else if (type == 1) {
u8 *_1_RAM;
u8 *_2_RAM;
int i;
int ram;
int type;
int nums;
u8 *bitgroups;
int x;
int y;
int byte;
int bit;
int bitgroup;
int bgi = 0;
int ram_size = xrows * xwidth * 8;
_1_RAM = (u8 *)calloc(ram_size, 1);
_2_RAM = (u8 *)calloc(ram_size, 1);
if (switchram)
{
memcpy(_1_RAM, RAM_2, ram_size);
memcpy(_2_RAM, RAM_1, ram_size);
}
else
{
memcpy(_1_RAM, RAM_1, ram_size);
memcpy(_2_RAM, RAM_2, ram_size);
}
switch(interpretation)
{
case 1:
method_1(_1_RAM);
method_1(_2_RAM);
break;
case 2:
case 3:
for (i = 0; i < xrows * xwidth * 8; i++)
{
_2_RAM[i] ^= _1_RAM[i];
}
method_1(_1_RAM);
break;
}
if (interpretation == 3)
{
method_1(_2_RAM);
}
curbit = 7;
curbyte = 0;
compressed = (u8 *)calloc(0x310, 1);
compressed[0] = (xrows << 4) | xwidth;
writebit(switchram);
for (ram = 0; ram < 2; ram++)
{
type = 0;
nums = 0;
bitgroups = (u8 *)calloc(0x1000, 1);
for (x = 0; x < xwidth; x++)
{
for (bit = 0; bit < 8; bit += 2)
{
byte = x * xrows * 8;
for (y=0; y < xrows * 8; y++)
{
if (ram)
{
bitgroup = (_2_RAM[byte] >> (6 - bit)) & 3;
}
else
{
bitgroup = (_1_RAM[byte] >> (6 - bit)) & 3;
}
if (!bitgroup)
{
if (!type)
{
writebit(0);
}
else if (type == 1)
{
nums++; nums++;
} } else {
else write_data_packet(bit_groups, index);
{ write_bit(0);
data_packet(bitgroups, bgi); write_bit(0);
writebit(0);
writebit(0);
} }
type = 1; type = 1;
free(bitgroups); memset(bit_groups, 0, sizeof(bit_groups) / sizeof(*bit_groups));
bitgroups = (u8 *)calloc(0x1000, 1); index = 0;
bgi = 0; } else {
} if (!type) {
else write_bit(1);
{ } else if (type == 1) {
if (!type) rle_encode_number(nums);
{
writebit(1);
} }
else if (type == 1) type = 2;
{ bit_groups[index++] = bit_group;
RLE(nums);
}
type = -1;
bitgroups[bgi++] = bitgroup;
nums = 0; nums = 0;
} }
byte++;
} }
} }
} }
if (type == 1) if (type == 1) {
{ rle_encode_number(nums);
RLE(nums); } else {
write_data_packet(bit_groups, index);
} }
else if (!plane) {
{ if (mode < 2) {
data_packet(bitgroups, bgi); write_bit(0);
} } else {
if (!ram) write_bit(1);
{ write_bit(mode - 2);
if (interpretation < 2)
{
writebit(0);
}
else
{
writebit(1);
writebit(interpretation - 2);
} }
} }
} }
free(bitgroups); free(_plane1);
free(_1_RAM); free(_plane2);
free(_2_RAM); return (cur_byte + 1) * 8 + cur_bit;
return (curbyte + 1) * 8 + curbit;
} }
int compress(u8 *data, int width, int height) int compress(uint8_t *data, int width) {
{ int ram_size = width * width * 8;
u8 *RAM_1; uint8_t *plane1 = xmalloc(ram_size);
u8 *RAM_2; uint8_t *plane2 = xmalloc(ram_size);
int i; for (int i = 0; i < ram_size; i++) {
int mode; plane1[i] = data[i * 2];
int order; plane2[i] = data[i * 2 + 1];
int newsize;
int compressedsize;
int size = -1;
u8 *current = NULL;
int ram_size;
xrows = height;
xwidth = width;
ram_size = xrows * xwidth * 8;
RAM_1 = (u8 *)calloc(ram_size, 1);
RAM_2 = (u8 *)calloc(ram_size, 1);
for (i = 0; i < xrows * xwidth * 8; i++)
{
RAM_1[i] = data[(i << 1)];
RAM_2[i] = data[(i << 1) | 1];
} }
uint8_t current[sizeof(compressed) / sizeof(*compressed)] = {0};
for (mode = 1; mode < 4; mode++) int compressed_size = -1;
{ for (int mode = 1; mode < 4; mode++) {
for (order = 0; order < 2; order++) for (int order = 0; order < 2; order++) {
{ if (mode == 1 && order == 0) {
if (!(mode == 1 && order == 0)) continue;
{ }
newsize = interpret_compress(RAM_1, RAM_2, mode, order); int new_size = interpret_compress(plane1, plane2, mode, order, width);
if (size == -1 || newsize < size) if (compressed_size == -1 || new_size < compressed_size) {
{ compressed_size = new_size;
if (current != NULL) memset(current, 0, sizeof(current) / sizeof(*current));
{ memcpy(current, compressed, compressed_size / 8);
free(current);
}
current = (u8 *)calloc(0x310, 1);
memcpy(current, compressed, newsize / 8);
free(compressed);
size = newsize;
}
} }
} }
} }
compressed = (u8 *)calloc(0x310, 1); memset(compressed, 0, sizeof(compressed) / sizeof(*compressed));
compressedsize = size / 8; memcpy(compressed, current, compressed_size / 8);
memcpy(compressed, current, compressedsize); free(plane1);
free(current); free(plane2);
return compressed_size / 8;
free(RAM_1);
free(RAM_2);
return compressedsize;
} }
uint8_t *transpose_tiles(uint8_t *data, int width, int height) uint8_t *transpose_tiles(uint8_t *data, int width) {
{ int size = width * width * 0x10;
int i; uint8_t *transposed = xmalloc(size);
int j; for (int i = 0; i < size; i++) {
int tile_size = 0x10; int j = (i / 0x10) * width * 0x10;
int size = width * height * tile_size;
u8 *transposed = calloc(size, 1);
for (i = 0; i < size; i++)
{
j = (i / 0x10) * width * 0x10;
j = (j % size) + 0x10 * (j / size) + (i % 0x10); j = (j % size) + 0x10 * (j / size) + (i % 0x10);
transposed[j] = data[i]; transposed[j] = data[i];
} }
free(data); free(data);
return transposed; return transposed;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{ if (argc != 3) {
usage_exit(1);
}
long filesize;
uint8_t *data = read_u8(argv[1], &filesize);
int width = 0; int width = 0;
int height = 0; for (int w = 1; w < 16; w++) {
int transpose = 1; if (filesize == w * w * 0x10) {
width = w;
if (argc != 3)
{
fputs("Usage: pkmncompress infile.2bpp outfile.pic\n", stderr);
return EXIT_FAILURE;
}
char *infile = argv[1];
char *outfile = argv[2];
FILE *f = fopen(infile, "rb");
if (!f) {
fprintf(stderr, "failed to open for reading: '%s'\n", infile);
return EXIT_FAILURE;
}
fseek(f, 0, SEEK_END);
int filesize = ftell(f);
for (int i = 0; i < 32; i++) {
width = i;
height = i;
if (width * height * 16 >= filesize) {
break; break;
} }
} }
if (width * height * 16 < filesize) { if (!width) {
fprintf(stderr, "file too big: '%s' (%x)\n", infile, filesize); error_exit("Image is not a square, or is larger than 15x15 tiles");
return EXIT_FAILURE;
}
if (width * height * 16 > filesize) {
fprintf(stderr, "wrong filesize for '%s' (%x). must be a square image of 16-byte tiles\n", infile, filesize);
return EXIT_FAILURE;
} }
u8 *data = (u8 *)calloc(filesize, 1); data = transpose_tiles(data, width);
fseek(f, 0, SEEK_SET); int compressed_size = compress(data, width);
int size = fread(data, 1, filesize, f); write_u8(argv[2], compressed, compressed_size);
fclose(f);
if (size != filesize) {
fprintf(stderr, "failed to read: '%s'\n", infile);
return EXIT_FAILURE;
}
if (transpose) {
data = transpose_tiles(data, width, height);
}
int compressed_size = compress(data, width, height);
free(data); free(data);
return 0;
f = fopen(outfile, "wb");
if (!f) {
fprintf(stderr, "failed to open for writing: '%s'\n", outfile);
return EXIT_FAILURE;
}
fwrite(compressed, 1, compressed_size, f);
fclose(f);
free(compressed);
return EXIT_SUCCESS;
} }