From c54e4efaec39482211e0f7058ad0aa2354a69281 Mon Sep 17 00:00:00 2001 From: Klemek Date: Wed, 24 Sep 2025 16:06:52 +0200 Subject: [PATCH] define vars at start of function --- src/args.c | 34 +++++++++++++++++++++------------- src/bmp.c | 41 +++++++++++++++++++++++++++-------------- src/generator.c | 22 +++++++++++++++------- src/main.c | 8 ++++++-- src/rand.c | 10 +++++++--- 5 files changed, 76 insertions(+), 39 deletions(-) diff --git a/src/args.c b/src/args.c index 66f1ce6..db5f6a3 100644 --- a/src/args.c +++ b/src/args.c @@ -69,27 +69,33 @@ static char *split_arg_value(char *arg) { static bool is_digit(char c) { return c >= '0' && c <= '9'; } static bool is_number(char *value) { + unsigned long value_len; + unsigned int i; + if (value == NULL) { return false; } - unsigned long value_len = strlen(value); - unsigned int i; + value_len = strlen(value); for (i = 0; i < value_len; i++) { if (!is_digit(value[i])) { return false; } } + return true; } static unsigned int parse_uint(char *arg, char *value) { + unsigned long long tmp_value; + if (!is_number(value)) { invalid_value(arg, value); } - unsigned long long tmp_value = (unsigned long long)atoll(value); + tmp_value = (unsigned long long)atoll(value); if (tmp_value >= UINT_MAX) { invalid_value(arg, value); } + return (unsigned int)tmp_value; } @@ -102,6 +108,7 @@ static unsigned long parse_ulong(char *arg, char *value) { static void parse_color(char *arg, char *value, unsigned int color[3]) { char *tmp; + tmp = strtok(value, ","); color[0] = parse_uint(arg, tmp); tmp = strtok(NULL, ","); @@ -112,6 +119,10 @@ static void parse_color(char *arg, char *value, unsigned int color[3]) { Parameters args_parse(int argc, char **argv) { Parameters params; + unsigned char var_range; + bool size_set, slope_set, start_set, var_set, rot_set; + int i; + char *arg, *value; params.quiet = false; params.seed = (unsigned long)time(NULL); @@ -120,22 +131,19 @@ Parameters args_parse(int argc, char **argv) { params.file_path = "output.bmp"; params.monochrome = false; - unsigned char var_range = 30; + var_range = 30; - bool size_set = false; - bool slope_set = false; - bool start_set = false; - bool var_set = false; - bool rot_set = false; + size_set = false; + slope_set = false; + start_set = false; + var_set = false; + rot_set = false; - int i; - char *arg; - char *value; for (i = 1; i < argc; i++) { arg = argv[i]; value = split_arg_value(arg); if (is_arg(arg, "--help")) { - print_help(0); + print_help(EXIT_SUCCESS); } else if (is_arg(arg, "-q") || is_arg(arg, "--quiet")) { params.quiet = true; } else if (is_arg(arg, "-v") || is_arg(arg, "--version")) { diff --git a/src/bmp.c b/src/bmp.c index 90079b8..dd12eb5 100644 --- a/src/bmp.c +++ b/src/bmp.c @@ -9,6 +9,7 @@ static void write_str(unsigned char *buffer, unsigned int offset, unsigned int size, unsigned char *value) { unsigned int i; + for (i = 0; i < size; i++) { buffer[offset + i] = (unsigned char)value[i]; } @@ -17,6 +18,7 @@ static void write_str(unsigned char *buffer, unsigned int offset, static void write_num(unsigned char *buffer, unsigned int offset, unsigned int size, unsigned int value) { unsigned int i; + for (i = 0; i < size; i++) { buffer[offset + i] = (unsigned char)((value >> (8 * i)) & 0xFFu); } @@ -25,6 +27,7 @@ static void write_num(unsigned char *buffer, unsigned int offset, static void write_nul(unsigned char *buffer, unsigned int offset, unsigned int size) { unsigned int i; + for (i = 0; i < size; i++) { buffer[offset + i] = 0; } @@ -32,8 +35,11 @@ static void write_nul(unsigned char *buffer, unsigned int offset, static unsigned char *bmp_header(unsigned int width, unsigned int height, unsigned int color_depth) { - unsigned char *output = (unsigned char *)malloc(HEADER_SIZE); - unsigned int data_length = width * height * color_depth; + unsigned char *output; + unsigned int data_length; + + output = malloc(HEADER_SIZE * sizeof(unsigned char)); + data_length = width * height * color_depth; // 0x00(2) BM write_str(output, 0x00, 0x02, (unsigned char *)"BM"); // 0x02(4) file size @@ -70,17 +76,22 @@ static unsigned char *bmp_header(unsigned int width, unsigned int height, static unsigned int bmp_data_line_length(unsigned int width, unsigned int color_depth) { - unsigned int data_length = width * color_depth; - unsigned int line_offset = data_length % 4; - unsigned int line_padding = line_offset > 0 ? 4 - line_offset : 0; + unsigned int data_length, line_offset, line_padding; + + data_length = width * color_depth; + line_offset = data_length % 4; + line_padding = line_offset > 0 ? 4 - line_offset : 0; + return data_length + line_padding; } static void bmp_data_line(unsigned char *buffer, unsigned int width, unsigned int color_depth, unsigned char *data) { - unsigned int data_length = width * color_depth; - unsigned int line_offset = data_length % 4; - unsigned int line_padding = line_offset > 0 ? 4 - line_offset : 0; + unsigned int data_length, line_offset, line_padding; + + data_length = width * color_depth; + line_offset = data_length % 4; + line_padding = line_offset > 0 ? 4 - line_offset : 0; write_str(buffer, 0, data_length, data); if (line_padding > 0) { write_nul(buffer, width * color_depth, line_padding); @@ -91,15 +102,17 @@ void bmp_generate(unsigned int width, unsigned int height, unsigned int color_depth, bool descending, char *file_path, line_fn generate_line) { FILE *fptr; + unsigned char *header, *data_buffer, *line_buffer; + unsigned int data_length, line_length, y; + fptr = fopen(file_path, "w"); - unsigned char *header = bmp_header(width, height, color_depth); + header = bmp_header(width, height, color_depth); fwrite(header, HEADER_SIZE, 1, fptr); free(header); - unsigned int data_length = width * color_depth; - unsigned int line_length = bmp_data_line_length(width, color_depth); - unsigned char *data_buffer = (unsigned char *)malloc(data_length); - unsigned char *line_buffer = (unsigned char *)malloc(line_length); - unsigned int y; + data_length = width * color_depth; + line_length = bmp_data_line_length(width, color_depth); + data_buffer = malloc(data_length * sizeof(unsigned char)); + line_buffer = malloc(line_length * sizeof(unsigned char)); for (y = 0; y < height; y++) { generate_line(y, data_buffer, data_length); bmp_data_line(line_buffer, width, color_depth, data_buffer); diff --git a/src/generator.c b/src/generator.c index 7cd6adc..55e89e9 100644 --- a/src/generator.c +++ b/src/generator.c @@ -19,14 +19,18 @@ static unsigned char *current_line; static unsigned char generate_pixel(unsigned char depth, unsigned char top_pixel, unsigned char left_pixel) { - int k = rand_uint(global_params.var[depth] + 1); - int v = (rand_uint(2) == 0 ? k : -k) + (left_pixel)*slope + - (top_pixel) * (1.0 - slope); + int k, v; + + k = rand_uint(global_params.var[depth] + 1); + v = (rand_uint(2) == 0 ? k : -k) + (left_pixel)*slope + + (top_pixel) * (1.0 - slope); + return (unsigned char)(v < 0 ? 0 : (v > 255 ? (unsigned char)255 : v)); } static void generate_line() { unsigned int i; + for (i = 0; i < line_width; i++) { last_line[i] = current_line[i]; } @@ -45,6 +49,7 @@ static void generate_bmp_line(unsigned int y, unsigned char *data_buffer, unsigned int len) { unsigned int i; unsigned int x; + if (y % global_params.size == 0) { generate_line(); } @@ -77,14 +82,15 @@ static void debug_parameters(Parameters params) { } static void init(Parameters params) { + unsigned int i; + global_params = params; slope = ((float)params.slope) / 255.0; color_depth = params.monochrome ? 1 : 3; debug_parameters(params); line_width = ((params.width / params.size) + 1) * color_depth; - last_line = (unsigned char *)malloc(line_width); - current_line = (unsigned char *)malloc(line_width); - unsigned int i = 0; + last_line = malloc(line_width * sizeof(unsigned char)); + current_line = malloc(line_width * sizeof(unsigned char)); for (i = 0; i < line_width; i++) { current_line[i] = params.start[i % color_depth]; } @@ -104,10 +110,12 @@ static void print_time(Parameters params, clock_t start) { } void generator_run(Parameters params) { + clock_t start; + if (!params.quiet) { puts(PACKAGE " " VERSION); } - clock_t start = clock(); + start = clock(); init(params); bmp_generate(params.width, params.height, BMP_COLOR_DEPTH, params.rotation % 2 == 1, params.file_path, generate_bmp_line); diff --git a/src/main.c b/src/main.c index de81285..bda81c4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,15 @@ -#include "main.h" +#include + #include "args.h" #include "generator.h" +#include "main.h" #include "types.h" int main(int argc, char **argv) { Parameters params; + params = args_parse(argc, argv); generator_run(params); - return 0; + + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/src/rand.c b/src/rand.c index e8bf80b..d5e6e0e 100644 --- a/src/rand.c +++ b/src/rand.c @@ -5,11 +5,15 @@ static unsigned long long const multiplier = 6364136223846793005u; // https://en.wikipedia.org/wiki/Permuted_congruential_generator static unsigned long rand(void) { - unsigned long long x = mcg_state; - unsigned count = (unsigned)(x >> 61); + unsigned long long x; + unsigned count; + + x = mcg_state; + count = (unsigned)(x >> 61); mcg_state = x * multiplier; x ^= x >> 22; + return (unsigned long)(x >> (22 + count)); } @@ -18,6 +22,6 @@ void rand_seed(unsigned long long seed) { (void)rand(); } -unsigned int rand_uint(const unsigned int max) { +unsigned int rand_uint(unsigned int max) { return max == 0 ? 0 : (unsigned int)(rand() % max); }