From a1ba114f1cd89ceda63cc8d32e0f3facf401dc39 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 20 Apr 2021 11:55:27 +0200 Subject: Add option to output to file directly instead of stdout --- src/main.c | 60 ++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/src/main.c b/src/main.c index ae4cf76..dcc9075 100644 --- a/src/main.c +++ b/src/main.c @@ -40,38 +40,38 @@ typedef struct Image { } Image; void -export_c_file(Tiles *tiles, Palette *palette) { +export_c_file(Tiles *tiles, Palette *palette, FILE *file) { // Output tiles. - printf("u32 tiles[%u][%u] = {\n", tiles->capacity, TILE_SIZE); + fprintf(file, "u32 tiles[%u][%u] = {\n", tiles->capacity, TILE_SIZE); for (size_t i = 0; i < tiles->capacity; ++i) { - printf(" {"); + fprintf(file, " {"); for (size_t j = 0; j < TILE_SIZE; ++j) { - printf("0x%08x", tiles->data[i][j]); + fprintf(file, "0x%08x", tiles->data[i][j]); if (j != (TILE_SIZE - 1)) { - printf(", "); + fprintf(file, ", "); } } - printf("},\n"); + fprintf(file, "},\n"); } - printf("};\n"); - printf("\n"); + fprintf(file, "};\n"); + fprintf(file, "\n"); // Output palette. - printf("u16 palette[%u] = {\n", palette->capacity); - printf(" "); + fprintf(file, "u16 palette[%u] = {\n", palette->capacity); + fprintf(file, " "); size_t counter = 0; for (size_t i = 0; i < palette->capacity; ++i, ++counter) { if (counter == 4) { counter = 0; - printf("\n "); + fprintf(file, "\n "); } - printf("0x%04x,", palette->data[i]); + fprintf(file, "0x%04x,", palette->data[i]); if (counter < 3) { - printf(" "); + fprintf(file, " "); } } - printf("\n"); - printf("};\n"); + fprintf(file, "\n"); + fprintf(file, "};\n"); } void @@ -79,8 +79,8 @@ print_usage(void) { printf("Usage: %s [options] \n", BIN_NAME); printf("\n"); printf("\t-b \tSelects the background color used for transparency.\n"); + printf("\t-o \tPath to the output file. If blank, stdout will be used.\n"); printf("\n"); - // TODO: Print valid options as a suggestion. } void @@ -153,9 +153,10 @@ int main(int argc, char *argv[]) { u16 background_color = DEFAULT_BG_COLOR; u16 palette_size = DEFAULT_PALETTE_SIZE; + char *out_file_path = NULL; int option; - while ((option = getopt(argc, argv, "hb:")) != -1) { + while ((option = getopt(argc, argv, "hb:o:")) != -1) { switch (option) { // TODO: -o output file name. If none is given, the output will be // redirected to stdout. @@ -187,6 +188,9 @@ main(int argc, char *argv[]) { blue = (blue * 31 / 255); background_color = rgb15(red, green, blue); } break; + case 'o': { + out_file_path = optarg; + } break; case 'h': { print_usage(); return EXIT_SUCCESS; @@ -219,11 +223,12 @@ main(int argc, char *argv[]) { Image img = {0}; // Open the given file. - img.data = stbi_load(file_name, &img.width, &img.height, &img.n_channels, 0); - if (img.data == NULL) { - fprintf(stderr, "Error: can't open the given file.\n"); + FILE *input_file = fopen(file_name, "rb"); + if (input_file == NULL) { + fprintf(stderr, "%s: can't open input file: %s\n", BIN_NAME, file_name); return EXIT_FAILURE; } + img.data = stbi_load_from_file(input_file, &img.width, &img.height, &img.n_channels, 0); // TODO: Implement support for different file inputs. if (img.n_channels != 3) { @@ -254,9 +259,20 @@ main(int argc, char *argv[]) { } } - export_c_file(&tiles, &palette); + if (out_file_path == NULL) { + export_c_file(&tiles, &palette, stdout); + } else { + FILE *file = fopen(out_file_path, "w"); + if (!file) { + fprintf(stderr, "%s: can't open output file: %s\n", out_file_path); + return EXIT_FAILURE; + } + export_c_file(&tiles, &palette, file); + } - // TODO: Cleanup other resources. + // Cleanup resources and exit. + free(tiles.data); + free(palette.data); stbi_image_free(img.data); return EXIT_SUCCESS; } -- cgit v1.2.1