From 2315bacd456e1185516aa8dd232c8a2eb6aaee2b Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 20 Apr 2021 12:10:11 +0200 Subject: Clean warnings --- src/main.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index dcc9075..87c4b73 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,11 @@ rgb15(u32 red, u32 green, u32 blue ) { return (blue << 10) | (green << 5) | red; } +static inline +int quantize5(int color) { + return color * 31 / 255; +} + // TODO: Assuming 4bpp tiles. The tile is an index to a palette. // TODO: Write more documentation about this. typedef u32 Tile[TILE_SIZE]; @@ -42,7 +47,7 @@ typedef struct Image { void export_c_file(Tiles *tiles, Palette *palette, FILE *file) { // Output tiles. - fprintf(file, "u32 tiles[%u][%u] = {\n", tiles->capacity, TILE_SIZE); + fprintf(file, "u32 tiles[%lu][%u] = {\n", tiles->capacity, TILE_SIZE); for (size_t i = 0; i < tiles->capacity; ++i) { fprintf(file, " {"); for (size_t j = 0; j < TILE_SIZE; ++j) { @@ -57,7 +62,7 @@ export_c_file(Tiles *tiles, Palette *palette, FILE *file) { fprintf(file, "\n"); // Output palette. - fprintf(file, "u16 palette[%u] = {\n", palette->capacity); + fprintf(file, "u16 palette[%lu] = {\n", palette->capacity); fprintf(file, " "); size_t counter = 0; for (size_t i = 0; i < palette->capacity; ++i, ++counter) { @@ -99,9 +104,9 @@ extract_tile(Tiles *tiles, Palette *palette, Image *img, int blue = img->data[idx + 2]; // Quantize to 5 bits per channel. - red = (red * 31 / 255); - green = (green * 31 / 255); - blue = (blue * 31 / 255); + red = quantize5(red); + green = quantize5(green); + blue = quantize5(blue); Color clr = rgb15(red, green, blue); bool found = false; @@ -158,8 +163,6 @@ main(int argc, char *argv[]) { int option; 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. // -shape, -size, -s: either 2 options or one with fixed // number of parameters 8x8, 16x16, 16x8, etc. case 'b': { @@ -183,9 +186,9 @@ main(int argc, char *argv[]) { sscanf(b_str, "%x", &blue); // Quantize and transform to rgb15. - red = (red * 31 / 255); - green = (green * 31 / 255); - blue = (blue * 31 / 255); + red = quantize5(red); + green = quantize5(green); + blue = quantize5(blue); background_color = rgb15(red, green, blue); } break; case 'o': { @@ -229,6 +232,7 @@ main(int argc, char *argv[]) { return EXIT_FAILURE; } img.data = stbi_load_from_file(input_file, &img.width, &img.height, &img.n_channels, 0); + fclose(input_file); // TODO: Implement support for different file inputs. if (img.n_channels != 3) { @@ -236,9 +240,9 @@ main(int argc, char *argv[]) { return EXIT_FAILURE; } - int n_tiles_x = img.width / TILE_SIZE; - int n_tiles_y = img.height / TILE_SIZE; - int n_tiles = n_tiles_x * n_tiles_y; + size_t n_tiles_x = img.width / TILE_SIZE; + size_t n_tiles_y = img.height / TILE_SIZE; + size_t n_tiles = n_tiles_x * n_tiles_y; // Allocate memory for the tiles in this file, with zero-initialization. Tiles tiles = { @@ -247,7 +251,6 @@ main(int argc, char *argv[]) { .capacity = n_tiles, }; - int pal_idx = 1; int offset_x = 0; int offset_y = 0; for (size_t k = 0; k < n_tiles; ++k) { @@ -264,10 +267,11 @@ main(int argc, char *argv[]) { } else { FILE *file = fopen(out_file_path, "w"); if (!file) { - fprintf(stderr, "%s: can't open output file: %s\n", out_file_path); + fprintf(stderr, "%s: can't open output file: %s\n", BIN_NAME, out_file_path); return EXIT_FAILURE; } export_c_file(&tiles, &palette, file); + fclose(file); } // Cleanup resources and exit. -- cgit v1.2.1