dos33fsprogs/mode7/background.c
2017-12-18 15:26:31 -05:00

117 lines
2.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int runlen[4]={0,0,0,0},last_color[4]={-1,-1,-1,-1};
static int new_size=0;
static unsigned char row[4][256];
static int rle_compress(int color, int j) {
int need_comma=0;
if ((color==last_color[j]) && (runlen[j]<255)) {
runlen[j]++;
// printf("Match %d %d\n",color,runlen[j]);
}
else {
need_comma=1;
// printf("Run of color %d length %d\n",
// last_color[j],runlen[j]);
if (runlen[j]==0) { // first color, skip
need_comma=0;
}
else if (runlen[j]==1) {
printf("$%02X",last_color[j]);
new_size++;
}
else if (runlen[j]==2) {
printf("$%02X,$%02X",last_color[j],last_color[j]);
new_size+=2;
}
else if ((runlen[j]>2) && (runlen[j]<16)) {
printf("$%02X,$%02X",0xa0 | (runlen[j]),
last_color[j]);
new_size+=2;
}
/* We could in theory compress up to 272 */
/* but we leave it at 256 to make the decode easier */
else if ((runlen[j]>15) && (runlen[j]<256)) {
new_size+=3;
printf("$%02X,$%02X,$%02X",0xa0,
runlen[j],last_color[j]);
}
else if (runlen[j]>=256) {
printf("Too big!\n");
exit(1);
}
last_color[j]=color;
runlen[j]=1;
}
return need_comma;
}
void plot(int x, int y, int color) {
int half,odd;
half=y/2;
odd=y&1;
if (odd) {
row[half][x]=row[half][x]|(color<<4);
}
else {
row[half][x]=row[half][x]|(color);
}
}
int main(int argc, char **argv) {
int length=0,x,y;
int need_comma;
memset(row,0,sizeof(row));
length=255;
/* 0 - 20 */
plot(2,5,15); plot(5,2,15); plot(9,1,13);
plot(12,3,15); plot(13,5,15); plot(16,5,15); plot(17,2,15);
plot(19,1,5); plot(20,0,5); plot(20,2,5); plot(21,1,5);
plot(20,1,15);
printf("; Original size = %d bytes\n",length*4);
printf("sky_background:\n");
printf("; scroll_length:\n.byte %d\n",length);
for(y=0;y<4;y++) {
//printf("scroll_row%d:\n",y+1);
printf("\t.byte ");
for(x=0;x<256;x++) {
need_comma=rle_compress(row[y][x],y);
if ((need_comma)) {// && (x!=length-1))
printf(",");
}
//last_color[y]=row[y][x];
}
rle_compress(-1,y);
printf("\n");
}
printf("\t.byte $A1\n");
new_size++;
printf("; Compressed size = %d bytes\n",new_size);
return 0;
}