diff options
author | Bad Diode <bd@badd10de.dev> | 2021-04-20 12:10:11 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-04-20 12:10:11 +0200 |
commit | 2315bacd456e1185516aa8dd232c8a2eb6aaee2b (patch) | |
tree | ba9d6543a5229ae70fc317f7fa29381c36bceea1 | |
parent | a1ba114f1cd89ceda63cc8d32e0f3facf401dc39 (diff) | |
download | gba-dev-tools-2315bacd456e1185516aa8dd232c8a2eb6aaee2b.tar.gz gba-dev-tools-2315bacd456e1185516aa8dd232c8a2eb6aaee2b.zip |
Clean warnings
-rw-r--r-- | src/main.c | 34 |
1 files changed, 19 insertions, 15 deletions
@@ -16,6 +16,11 @@ rgb15(u32 red, u32 green, u32 blue ) { | |||
16 | return (blue << 10) | (green << 5) | red; | 16 | return (blue << 10) | (green << 5) | red; |
17 | } | 17 | } |
18 | 18 | ||
19 | static inline | ||
20 | int quantize5(int color) { | ||
21 | return color * 31 / 255; | ||
22 | } | ||
23 | |||
19 | // TODO: Assuming 4bpp tiles. The tile is an index to a palette. | 24 | // TODO: Assuming 4bpp tiles. The tile is an index to a palette. |
20 | // TODO: Write more documentation about this. | 25 | // TODO: Write more documentation about this. |
21 | typedef u32 Tile[TILE_SIZE]; | 26 | typedef u32 Tile[TILE_SIZE]; |
@@ -42,7 +47,7 @@ typedef struct Image { | |||
42 | void | 47 | void |
43 | export_c_file(Tiles *tiles, Palette *palette, FILE *file) { | 48 | export_c_file(Tiles *tiles, Palette *palette, FILE *file) { |
44 | // Output tiles. | 49 | // Output tiles. |
45 | fprintf(file, "u32 tiles[%u][%u] = {\n", tiles->capacity, TILE_SIZE); | 50 | fprintf(file, "u32 tiles[%lu][%u] = {\n", tiles->capacity, TILE_SIZE); |
46 | for (size_t i = 0; i < tiles->capacity; ++i) { | 51 | for (size_t i = 0; i < tiles->capacity; ++i) { |
47 | fprintf(file, " {"); | 52 | fprintf(file, " {"); |
48 | for (size_t j = 0; j < TILE_SIZE; ++j) { | 53 | for (size_t j = 0; j < TILE_SIZE; ++j) { |
@@ -57,7 +62,7 @@ export_c_file(Tiles *tiles, Palette *palette, FILE *file) { | |||
57 | fprintf(file, "\n"); | 62 | fprintf(file, "\n"); |
58 | 63 | ||
59 | // Output palette. | 64 | // Output palette. |
60 | fprintf(file, "u16 palette[%u] = {\n", palette->capacity); | 65 | fprintf(file, "u16 palette[%lu] = {\n", palette->capacity); |
61 | fprintf(file, " "); | 66 | fprintf(file, " "); |
62 | size_t counter = 0; | 67 | size_t counter = 0; |
63 | for (size_t i = 0; i < palette->capacity; ++i, ++counter) { | 68 | for (size_t i = 0; i < palette->capacity; ++i, ++counter) { |
@@ -99,9 +104,9 @@ extract_tile(Tiles *tiles, Palette *palette, Image *img, | |||
99 | int blue = img->data[idx + 2]; | 104 | int blue = img->data[idx + 2]; |
100 | 105 | ||
101 | // Quantize to 5 bits per channel. | 106 | // Quantize to 5 bits per channel. |
102 | red = (red * 31 / 255); | 107 | red = quantize5(red); |
103 | green = (green * 31 / 255); | 108 | green = quantize5(green); |
104 | blue = (blue * 31 / 255); | 109 | blue = quantize5(blue); |
105 | 110 | ||
106 | Color clr = rgb15(red, green, blue); | 111 | Color clr = rgb15(red, green, blue); |
107 | bool found = false; | 112 | bool found = false; |
@@ -158,8 +163,6 @@ main(int argc, char *argv[]) { | |||
158 | int option; | 163 | int option; |
159 | while ((option = getopt(argc, argv, "hb:o:")) != -1) { | 164 | while ((option = getopt(argc, argv, "hb:o:")) != -1) { |
160 | switch (option) { | 165 | switch (option) { |
161 | // TODO: -o output file name. If none is given, the output will be | ||
162 | // redirected to stdout. | ||
163 | // -shape, -size, -s: either 2 options or one with fixed | 166 | // -shape, -size, -s: either 2 options or one with fixed |
164 | // number of parameters 8x8, 16x16, 16x8, etc. | 167 | // number of parameters 8x8, 16x16, 16x8, etc. |
165 | case 'b': { | 168 | case 'b': { |
@@ -183,9 +186,9 @@ main(int argc, char *argv[]) { | |||
183 | sscanf(b_str, "%x", &blue); | 186 | sscanf(b_str, "%x", &blue); |
184 | 187 | ||
185 | // Quantize and transform to rgb15. | 188 | // Quantize and transform to rgb15. |
186 | red = (red * 31 / 255); | 189 | red = quantize5(red); |
187 | green = (green * 31 / 255); | 190 | green = quantize5(green); |
188 | blue = (blue * 31 / 255); | 191 | blue = quantize5(blue); |
189 | background_color = rgb15(red, green, blue); | 192 | background_color = rgb15(red, green, blue); |
190 | } break; | 193 | } break; |
191 | case 'o': { | 194 | case 'o': { |
@@ -229,6 +232,7 @@ main(int argc, char *argv[]) { | |||
229 | return EXIT_FAILURE; | 232 | return EXIT_FAILURE; |
230 | } | 233 | } |
231 | img.data = stbi_load_from_file(input_file, &img.width, &img.height, &img.n_channels, 0); | 234 | img.data = stbi_load_from_file(input_file, &img.width, &img.height, &img.n_channels, 0); |
235 | fclose(input_file); | ||
232 | 236 | ||
233 | // TODO: Implement support for different file inputs. | 237 | // TODO: Implement support for different file inputs. |
234 | if (img.n_channels != 3) { | 238 | if (img.n_channels != 3) { |
@@ -236,9 +240,9 @@ main(int argc, char *argv[]) { | |||
236 | return EXIT_FAILURE; | 240 | return EXIT_FAILURE; |
237 | } | 241 | } |
238 | 242 | ||
239 | int n_tiles_x = img.width / TILE_SIZE; | 243 | size_t n_tiles_x = img.width / TILE_SIZE; |
240 | int n_tiles_y = img.height / TILE_SIZE; | 244 | size_t n_tiles_y = img.height / TILE_SIZE; |
241 | int n_tiles = n_tiles_x * n_tiles_y; | 245 | size_t n_tiles = n_tiles_x * n_tiles_y; |
242 | 246 | ||
243 | // Allocate memory for the tiles in this file, with zero-initialization. | 247 | // Allocate memory for the tiles in this file, with zero-initialization. |
244 | Tiles tiles = { | 248 | Tiles tiles = { |
@@ -247,7 +251,6 @@ main(int argc, char *argv[]) { | |||
247 | .capacity = n_tiles, | 251 | .capacity = n_tiles, |
248 | }; | 252 | }; |
249 | 253 | ||
250 | int pal_idx = 1; | ||
251 | int offset_x = 0; | 254 | int offset_x = 0; |
252 | int offset_y = 0; | 255 | int offset_y = 0; |
253 | for (size_t k = 0; k < n_tiles; ++k) { | 256 | for (size_t k = 0; k < n_tiles; ++k) { |
@@ -264,10 +267,11 @@ main(int argc, char *argv[]) { | |||
264 | } else { | 267 | } else { |
265 | FILE *file = fopen(out_file_path, "w"); | 268 | FILE *file = fopen(out_file_path, "w"); |
266 | if (!file) { | 269 | if (!file) { |
267 | fprintf(stderr, "%s: can't open output file: %s\n", out_file_path); | 270 | fprintf(stderr, "%s: can't open output file: %s\n", BIN_NAME, out_file_path); |
268 | return EXIT_FAILURE; | 271 | return EXIT_FAILURE; |
269 | } | 272 | } |
270 | export_c_file(&tiles, &palette, file); | 273 | export_c_file(&tiles, &palette, file); |
274 | fclose(file); | ||
271 | } | 275 | } |
272 | 276 | ||
273 | // Cleanup resources and exit. | 277 | // Cleanup resources and exit. |