Delete trailing whitespace in cabi.c

This commit is contained in:
Scott Allen 2020-07-02 16:10:36 -04:00
parent 3345198d3d
commit 566557eab3
1 changed files with 46 additions and 46 deletions

View File

@ -44,12 +44,12 @@ void draw_sprite_ascii(const uint8_t *dat, unsigned w, unsigned h)
{ {
unsigned x, y; unsigned x, y;
unsigned row, bit; unsigned row, bit;
for (y = 0; y < h; y ++) for (y = 0; y < h; y ++)
{ {
row = y/8; row = y/8;
bit = y&7; bit = y&7;
for (x = 0; x < w; x++) for (x = 0; x < w; x++)
{ {
if (dat[x + (row*w)] & (1 << bit)) if (dat[x + (row*w)] & (1 << bit))
@ -111,33 +111,33 @@ void draw_compressed_sprite_ascii(const uint8_t *src)
unsigned w, h; unsigned w, h;
unsigned x, y; unsigned x, y;
unsigned total = 0; unsigned total = 0;
memset(&cs, 0, sizeof(cs)); memset(&cs, 0, sizeof(cs));
cs.src = src; cs.src = src;
cs.bit = 0x100; cs.bit = 0x100;
cs.src_pos = 0; cs.src_pos = 0;
// header // header
w = getval(8) + 1; w = getval(8) + 1;
h = getval(8) + 1; h = getval(8) + 1;
col = getval(1); // starting colour col = getval(1); // starting colour
x = y = 0; x = y = 0;
while (y < h) while (y < h)
{ {
bl = 1; bl = 1;
while (!getval(1)) while (!getval(1))
bl += 2; bl += 2;
len = getval(bl)+1; // span length len = getval(bl)+1; // span length
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
//if ((x%8) == 0) // every 8th bit (format test) //if ((x%8) == 0) // every 8th bit (format test)
printf("%s", col ? "#":"."); printf("%s", col ? "#":".");
if (col) total++; if (col) total++;
x++; x++;
if (x >= w) if (x >= w)
@ -146,11 +146,11 @@ void draw_compressed_sprite_ascii(const uint8_t *src)
y ++; y ++;
x = 0; x = 0;
} }
//if ((x+y*w)%(w*8) == 0) printf("\n"); // print every 8th line (format test) //if ((x+y*w)%(w*8) == 0) printf("\n"); // print every 8th line (format test)
} }
col = 1-col; // toggle col = 1-col; // toggle
} }
printf("\ntotal: %u\n", total); printf("\ntotal: %u\n", total);
@ -164,13 +164,13 @@ void draw_compressed_sprite_ascii(const uint8_t *src)
/* /*
getcol getcol
pos is the index of the pixel: 0 .. w*h-1 pos is the index of the pixel: 0 .. w*h-1
*/ */
static unsigned getcol(unsigned pos) static unsigned getcol(unsigned pos)
{ {
unsigned x, y; unsigned x, y;
// display order // display order
if (reading_order == 0) if (reading_order == 0)
@ -178,7 +178,7 @@ static unsigned getcol(unsigned pos)
if (cs.src[pos/8] & (1 << (pos&7))) return 1; if (cs.src[pos/8] & (1 << (pos&7))) return 1;
return 0; return 0;
} }
// reading order (compresses slightly better but harder to optimize sprite blit) // reading order (compresses slightly better but harder to optimize sprite blit)
// or use this after loading png into display order (no need for extra conversion) // or use this after loading png into display order (no need for extra conversion)
@ -189,17 +189,17 @@ static unsigned getcol(unsigned pos)
} }
static unsigned find_rlen(unsigned pos, unsigned plen) static unsigned find_rlen(unsigned pos, unsigned plen)
{ {
unsigned col; unsigned col;
unsigned pos0; unsigned pos0;
col = getcol(pos); col = getcol(pos);
pos0 = pos; pos0 = pos;
while(getcol(pos) == col && pos < plen) while(getcol(pos) == col && pos < plen)
pos ++; pos ++;
return pos-pos0; return pos-pos0;
} }
@ -214,11 +214,11 @@ static void putbit(unsigned val)
if (cs.out_pos != 0) printf(","); if (cs.out_pos != 0) printf(",");
if (cs.out_pos % 16 == 0) printf("\n"); if (cs.out_pos % 16 == 0) printf("\n");
printf("0x%02x", cs.byte); printf("0x%02x", cs.byte);
cs.out_pos ++; cs.out_pos ++;
cs.bit = 0x1; cs.bit = 0x1;
cs.byte = 0; cs.byte = 0;
} }
} }
@ -226,7 +226,7 @@ static void putbit(unsigned val)
static void putval(unsigned val, unsigned bits) static void putval(unsigned val, unsigned bits)
{ {
unsigned i; unsigned i;
if (bits <= 0) return; if (bits <= 0) return;
for (i = 0; i < bits; i++) for (i = 0; i < bits; i++)
putbit(val & (1 << i)); putbit(val & (1 << i));
@ -250,46 +250,46 @@ static void putsplen(unsigned len)
/* /*
comp comp
compress plen 1-bit pixels from src to dest compress plen 1-bit pixels from src to dest
*/ */
unsigned compress_rle(const uint8_t *src, unsigned w, unsigned h, char *prefix, char *suffix) unsigned compress_rle(const uint8_t *src, unsigned w, unsigned h, char *prefix, char *suffix)
{ {
unsigned pos; unsigned pos;
unsigned rlen; unsigned rlen;
printf("const PROGMEM uint8_t %s%s[] = {", prefix, suffix); printf("const PROGMEM uint8_t %s%s[] = {", prefix, suffix);
fflush(stdout); fflush(stdout);
memset(&cs, 0, sizeof(cs)); memset(&cs, 0, sizeof(cs));
cs.src = src; cs.src = src;
cs.bit = 1; cs.bit = 1;
cs.w = w; cs.w = w;
cs.h = h; cs.h = h;
// header // header
putval(w-1, 8); putval(w-1, 8);
putval(h-1, 8); putval(h-1, 8);
putval(getcol(0), 1); // first colour putval(getcol(0), 1); // first colour
pos = 0; pos = 0;
// span data // span data
while (pos < w*h) while (pos < w*h)
{ {
rlen = find_rlen(pos, w*h); rlen = find_rlen(pos, w*h);
pos += rlen; pos += rlen;
putsplen(rlen-1); putsplen(rlen-1);
} }
// pad with zeros and flush // pad with zeros and flush
while (cs.bit != 0x1) while (cs.bit != 0x1)
putbit(0); putbit(0);
printf("\n};\n"); printf("\n};\n");
return cs.out_pos; // bytes return cs.out_pos; // bytes
} }
@ -308,8 +308,8 @@ int main(int argc, char **argv)
unsigned row, bit; unsigned row, bit;
char default_prefix[] = "compressed_image"; char default_prefix[] = "compressed_image";
char *prefix = default_prefix; char *prefix = default_prefix;
if (argc < 2) if (argc < 2)
{ {
printf("cabi - Compress Arduboy Image\n"); printf("cabi - Compress Arduboy Image\n");
@ -323,24 +323,24 @@ int main(int argc, char **argv)
if (argc >= 3) { if (argc >= 3) {
prefix = argv[2]; prefix = argv[2];
} }
result = lodepng_decode32_file(&bmp, &w, &h, argv[1]); result = lodepng_decode32_file(&bmp, &w, &h, argv[1]);
if (result != 0) { if (result != 0) {
printf("error %u: file %s: %s\n", result, argv[1], lodepng_error_text(result)); printf("error %u: file %s: %s\n", result, argv[1], lodepng_error_text(result));
free(bmp); free(bmp);
exit(result); exit(result);
} }
// generate sprite and mask // generate sprite and mask
rawlen = w * (h+7) / 8; rawlen = w * (h+7) / 8;
bmp0 = malloc(rawlen); memset(bmp0, 0, rawlen); bmp0 = malloc(rawlen); memset(bmp0, 0, rawlen);
bmp1 = malloc(rawlen); memset(bmp1, 0, rawlen); bmp1 = malloc(rawlen); memset(bmp1, 0, rawlen);
printf("// %s width: %u height: %u\n", argv[1], w, h); printf("// %s width: %u height: %u\n", argv[1], w, h);
for (y = 0; y < h; y++) for (y = 0; y < h; y++)
{ {
for (x = 0; x < w; x++) for (x = 0; x < w; x++)
@ -360,20 +360,20 @@ int main(int argc, char **argv)
// set mask // set mask
bmp1[x + (row*w)] |= (1 << bit); bmp1[x + (row*w)] |= (1 << bit);
} }
} }
} }
compressed_len = compress_rle(bmp0, w, h, prefix, ""); compressed_len = compress_rle(bmp0, w, h, prefix, "");
printf("// bytes:%u ratio: %3.3f\n\n", compressed_len, (float)(compressed_len * 8)/ (float)(w*h)); printf("// bytes:%u ratio: %3.3f\n\n", compressed_len, (float)(compressed_len * 8)/ (float)(w*h));
compressed_len = compress_rle(bmp1, w, h, prefix, "_mask"); compressed_len = compress_rle(bmp1, w, h, prefix, "_mask");
printf("// bytes:%u ratio: %3.3f\n\n", compressed_len, (float)(compressed_len * 8)/ (float)(w*h)); printf("// bytes:%u ratio: %3.3f\n\n", compressed_len, (float)(compressed_len * 8)/ (float)(w*h));
free(bmp); free(bmp);
free(bmp0); free(bmp0);
free(bmp1); free(bmp1);
return 0; return 0;
} }