big refactor ready for pathway fadeout

This commit is contained in:
alpine9000
2016-04-25 13:40:02 +10:00
parent 4e5a5ded19
commit 1deae4565b
23 changed files with 724 additions and 151 deletions
+6
View File
@@ -0,0 +1,6 @@
PROGRAM=./out/fade
OBJS=out/fade.o out/file.o
LIBS=
EXTRA_CFLAGS=
include ../../shared/tools.mk
+179
View File
@@ -0,0 +1,179 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <getopt.h>
#include <stdarg.h>
#include "fade.h"
#include "file.h"
typedef struct {
int r, g, b, a;
} rgba_t;
config_t config = {
.numColors = 16
};
#define MAX_COLORS 32
rgba_t original[MAX_COLORS];
rgba_t from[MAX_COLORS];
rgba_t progress[MAX_COLORS];
void
abort_(const char * s, ...)
{
fprintf(stderr, "%s: ", config.argv[0]);
va_list args;
va_start(args, s);
vfprintf(stderr, s, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
void
usage()
{
fprintf(stderr,
"%s: --output <output>\n"\
"options:\n"\
" --to <file.pal>\n"\
" --from-grey\n"\
" --colors <num colors> (default: 16)\n"\
" --verbose\n\n"\
"Exactly one 'to' and 'from' option must be specified\n", config.argv[0]);
exit(1);
}
int
main(int argc, char** argv)
{
int c;
config.argv = argv;
while (1) {
static struct option long_options[] = {
{"verbose", no_argument, &config.verbose, 1},
{"from-grey", no_argument, &config.fromGrey, 1},
{"from-black", no_argument, &config.fromBlack, 1},
{"to", required_argument, 0, 't'},
{"from", required_argument, 0, 'f'},
{"output", required_argument, 0, 'o'},
{"colors", required_argument, 0, 'c'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "t:c:o:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
break;
case 't':
config.toFile = optarg;
break;
case 'f':
config.fromFile = optarg;
break;
case 'o':
config.output = optarg;
break;
case 'c':
if (sscanf(optarg, "%d", &config.numColors) != 1) {
abort_("invalid number of colors");
}
break;
case '?':
usage();
break;
default:
usage();
break;
}
}
if (config.toFile == 0 || config.output == 0) {
usage();
abort();
}
if (config.fromFile == 0 && (config.fromGrey == 0 && config.fromBlack == 0 && config.fromFile == 0)) {
usage();
abort();
}
if (config.toFile) {
FILE* fp = file_openRead(config.toFile);
rgba_t* p = original;
while (fscanf(fp, "%d %d %d %d", &p->r, &p->g, &p->b, &p->a) == 4) {
p++;
}
fclose(fp);
}
if (config.fromFile) {
FILE* fp = file_openRead(config.fromFile);
rgba_t* p = from;
while (fscanf(fp, "%d %d %d %d", &p->r, &p->g, &p->b, &p->a) == 4) {
p++;
}
fclose(fp);
}
if (config.fromGrey) {
rgba_t* g = from;
rgba_t* p = original;
for (int i = 0; i < config.numColors; i++) {
g->r = g->g = g->b = (p->r + p->g + p->b) / 3;
g++;
p++;
}
}
if (config.fromBlack) {
rgba_t* g = from;
for (int i = 0; i < config.numColors; i++) {
g->r = g->g = g->b = 0;
g++;
}
}
for (int s = 0; s < 16; s++) {
printf(".step%d\n", s);
for (int i = 0; i < config.numColors; i++) {
int dr = ((((float)original[i].r)-(float)from[i].r)/16.0)*s;
int dg = ((((float)original[i].g)-(float)from[i].g)/16.0)*s;
int db = ((((float)original[i].b)-(float)from[i].b)/16.0)*s;
#if 0
printf("r:%d %d %d -> %d\n"
"g:%d %d %d -> %d\n"
"b:%d %d %d -> %d\n",
original[i].r,from[i].r, dr, from[i].r+dr,
original[i].g,from[i].g, dg, from[i].g+dg,
original[i].b,from[i].b, db, from[i].b+db);
#endif
printf("\tdc.w\t$%03x\n",
((from[i].r+dr)>>4)<<8|
((from[i].g+dg)>>4)<<4|
((from[i].b+db)>>4));
}
}
printf("%sFadeComplete:\n", config.output);
}
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <getopt.h>
#include <stdarg.h>
typedef struct {
int verbose;
int numColors;
int fromGrey;
int fromBlack;
char* toFile;
char* fromFile;
char* output;
char** argv;
} config_t;
extern config_t config;
extern void
abort_(const char * s, ...);
+39
View File
@@ -0,0 +1,39 @@
#include "fade.h"
FILE *
file_openWrite(const char * s, ...)
{
char buffer[4096];
va_list args;
va_start(args, s);
vsprintf(buffer, s, args);
va_end(args);
if (config.verbose) {
printf("Opening %s for writing\n", buffer);
}
FILE* fp = fopen(buffer, "w+");
if (!fp) {
abort_("Failed to open %s for writing\n", buffer);
}
return fp;
}
FILE *
file_openRead(const char * s, ...)
{
char buffer[4096];
va_list args;
va_start(args, s);
vsprintf(buffer, s, args);
va_end(args);
FILE* fp = fopen(buffer, "r");
if (!fp) {
abort_("Failed to open %s for reading\n", buffer);
}
return fp;
}
+7
View File
@@ -0,0 +1,7 @@
#pragma once
FILE *
file_openWrite(const char * s, ...);
FILE *
file_openRead(const char * s, ...);
+7
View File
@@ -59,6 +59,7 @@ palette_output(imagecon_image_t* ic, char* outFilename)
FILE* paletteGreyFP = 0;
FILE* paletteTableFP = 0;
FILE* paletteGreyTableFP = 0;
FILE* paletteGreyCopperFP = 0;
if (config.outputCopperList) {
fp = file_openWrite("%s-copper-list.s", outFilename);
@@ -72,6 +73,7 @@ palette_output(imagecon_image_t* ic, char* outFilename)
if (config.outputPaletteGrey) {
paletteGreyFP = file_openWrite("%s-grey.s", outFilename);
paletteGreyTableFP = file_openWrite("%s-grey-table.s", outFilename);
paletteGreyCopperFP = file_openWrite("%s-grey-copper.s", outFilename);
fprintf(paletteGreyFP, "\tmovem.l d0-a6,-(sp)\n\tlea CUSTOM,a6\n");
}
@@ -118,6 +120,11 @@ palette_output(imagecon_image_t* ic, char* outFilename)
if (fp) {
fprintf(fp, "\tdc.w $%x,$%x\n", 0x180+((i+config.paletteOffset)*2), RGB24TORGB12(ic->palette[i].r) << 8 | RGB24TORGB12(ic->palette[i].g) << 4 | RGB24TORGB12(ic->palette[i].b));
}
if (paletteGreyCopperFP) {
unsigned grey = ((RGB24TORGB12(ic->palette[i].r) + RGB24TORGB12(ic->palette[i].g) + RGB24TORGB12(ic->palette[i].b))/3);
fprintf(paletteGreyCopperFP, "\tdc.w $%x,$%03x\n", 0x180+((i+config.paletteOffset)*2), grey << 8 | grey << 4 | grey);
}
}
if (paletteGreyTableFP) {