ReC98/Pipeline/bmp2arr.c

171 lines
4.5 KiB
C
Raw Normal View History

2020-06-26 03:31:51 +00:00
#if defined(REC98_PROJECT)
# include "platform.h" /* see also [https://github.com/nmlgc/ReC98/issues/8] */
#elif defined(_MSC_VER)/*Microsoft C++*/ || defined(__BORLANDC__) || defined(__TURBOC__)
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
2020-06-26 03:31:51 +00:00
#else
# include <stdint.h>
#endif
#if defined(_MSC_VER)/*Microsoft C++*/ || defined(__BORLANDC__) || defined(__TURBOC__)
# include <io.h>
#else
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
#endif
2020-06-26 03:31:51 +00:00
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <fcntl.h>
#if defined(__GNUC__)
# include <endian.h>
#else
# define le16toh(x) (x)
# define le32toh(x) (x)
2020-06-26 06:36:26 +00:00
# define htole16(x) (x)
# define htole32(x) (x)
2020-06-26 03:31:51 +00:00
#endif
#include "bmp2arrl.h"
2020-06-26 03:31:51 +00:00
/* O_BINARY is needed for MS-DOS, Windows, etc.
* Linux, Mac OS X, etc. do not have O_BINARY */
#ifndef O_BINARY
#define O_BINARY (0)
#endif
static int parse_argv(struct rec98_bmp2arr_task *tsk,int argc,char **argv) {
char *a;
int i;
for (i=1;i < argc;) {
a = argv[i++];
if (*a == '-') {
do { a++; } while (*a == '-');
if (!strcmp(a,"i")) {
a = argv[i++];
if (a == NULL) return -1;
cstr_set(&tsk->input_bmp,a);
}
else if (!strcmp(a,"o")) {
a = argv[i++];
if (a == NULL) return -1;
cstr_set(&tsk->output_file,a);
}
2020-06-26 22:41:45 +00:00
else if (!strcmp(a,"sym")) {
a = argv[i++];
if (a == NULL) return -1;
cstr_set(&tsk->output_symname,a);
}
2020-06-26 03:31:51 +00:00
else if (!strcmp(a,"of")) {
a = argv[i++];
if (a == NULL) return -1;
2020-06-28 00:17:52 +00:00
if (!strcmp(a,"asm"))
2020-06-26 03:31:51 +00:00
tsk->output_type = REC98_OUT_ASM;
2020-06-26 22:44:57 +00:00
else if (!strcmp(a,"bin"))
tsk->output_type = REC98_OUT_BIN;
else if (!strcmp(a,"bmp"))
tsk->output_type = REC98_OUT_BMP;
2020-06-26 03:31:51 +00:00
else if (!strcmp(a,"c"))
tsk->output_type = REC98_OUT_C;
else
return -1;
}
else if (!strcmp(a,"sw")) {
a = argv[i++];
if (a == NULL) return -1;
tsk->sprite_width = atoi(a);
if (!(tsk->sprite_width == 8 || tsk->sprite_width == 16)) return -1;
}
else if (!strcmp(a,"sh")) {
a = argv[i++];
if (a == NULL) return -1;
tsk->sprite_height = atoi(a);
if (tsk->sprite_height < 1 || tsk->sprite_height > 32) return -1;
}
else if (!strcmp(a,"u")) {
tsk->upsidedown = 1;
}
2020-06-26 06:33:06 +00:00
else if (!strcmp(a,"dbg-bmp")) {
tsk->debug_bmp_out = 1;
}
2020-06-26 03:31:51 +00:00
else if (!strcmp(a,"pshf")) {
a = argv[i++];
if (a == NULL) return -1;
tsk->preshift = 1;
if (!strcmp(a,"inner"))
tsk->preshift_inner = 1;
else if (!strcmp(a,"outer"))
tsk->preshift_inner = 0;
else
return -1;
}
else {
fprintf(stderr,"Unknown switch '%s'\n",a);
}
}
else {
fprintf(stderr,"Unexpected\n");
return -1;
}
}
/* input BMP is required */
if (tsk->input_bmp == NULL) {
fprintf(stderr,"Input BMP required (-i)\n");
return -1;
}
/* output file is required */
if (tsk->output_file == NULL) {
fprintf(stderr,"Output file required (-o)\n");
return -1;
}
if (tsk->sprite_width == 0 || tsk->sprite_height == 0) {
fprintf(stderr,"Sprite width/height required (-sw and -sh)\n");
return -1;
}
if (!(tsk->sprite_width == 8 || tsk->sprite_width == 16)) {
fprintf(stderr,"Sprite width must be 8 or 16\n");
return -1;
}
2020-06-26 03:31:51 +00:00
return 0; /* success */
}
int main(int argc,char **argv) {
struct rec98_bmp2arr_task tsk;
if (rec98_bmp2arr_task_init(&tsk))
return 1;
if (parse_argv(&tsk,argc,argv))
return 1;
if (rec98_bmp2arr_load_bitmap(&tsk))
return 1;
2020-06-26 06:33:06 +00:00
if (tsk.debug_bmp_out) {
if (rec98_bmp2arr_save_debug_bmp_out(&tsk))
return 1;
return 0;
}
2020-06-26 23:16:56 +00:00
if (rec98_bmp2arr_save_output(&tsk))
return 1;
2020-06-26 03:31:51 +00:00
rec98_bmp2arr_task_free(&tsk);
return 0;
}