From 8ca37b89eb2a82b5cffd4e15ab063ba1dadc36ae Mon Sep 17 00:00:00 2001 From: Klemek Date: Wed, 4 Jun 2025 14:32:09 +0200 Subject: [PATCH] working generator --- src/args.c | 9 +++++++-- src/generator.c | 49 +++++++++++++++++++++++++++---------------------- src/types.h | 1 + 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/args.c b/src/args.c index bb17e75..da0d4a3 100644 --- a/src/args.c +++ b/src/args.c @@ -16,7 +16,8 @@ void print_help(int status_code) { "[-p=PIXEL_SIZE] " "[-s=SLOPE] " "[-c=R,G,B] " - "[-v=R,G,B]\n\n" + "[-v=R,G,B] " + "[-m]\n\n" "generates a marble-like pattern bitmap image.\n\n" "options:\n" " --help show this help message and exit\n" @@ -29,7 +30,8 @@ void print_help(int status_code) { " -s, --slope slope [0-255] (default: random)\n" " -c, --color base color [0-255,0-255,0-255] (default: random)\n" " -v, --variation base variation [0-255,0-255,0-255] (default: " - "random)"); + "random)" + " -m, --monochrome black & white generation"); exit(status_code); } @@ -119,6 +121,7 @@ parameters parse_args(int argc, char **argv) { params.width = 0; params.height = 0; params.file_path = "output.bmp"; + params.monochrome = false; bool size_set = false; bool slope_set = false; @@ -170,6 +173,8 @@ parameters parse_args(int argc, char **argv) { } else if (is_arg(arg, "-v") || is_arg(arg, "--variation")) { parse_color(arg, value, params.var); var_set = true; + } else if (is_arg(arg, "-m") || is_arg(arg, "--monochrome")) { + params.monochrome = true; } else { invalid_arg(arg); } diff --git a/src/generator.c b/src/generator.c index e48b071..c14d5c2 100644 --- a/src/generator.c +++ b/src/generator.c @@ -5,10 +5,9 @@ #include #include -#define COLOR_DEPTH 3 - parameters global_params; -float global_fslope; +float slope; +unsigned char color_depth; unsigned int line_width; unsigned char *last_line; unsigned char *current_line; @@ -17,8 +16,8 @@ unsigned char generate_pixel(unsigned char depth, unsigned char top_pixel, unsigned char left_pixel) { float v = fminf( 255.0, fmaxf(0.0, (rand_float(2.0) - 1.0) * global_params.var[depth] + - ((float)left_pixel) * global_fslope + - ((float)top_pixel) * (1.0 - global_fslope))); + ((float)left_pixel) * slope + + ((float)top_pixel) * (1.0 - slope))); return (unsigned char)v; } @@ -28,12 +27,12 @@ void generate_line() { last_line[i] = current_line[i]; } for (i = 0; i < line_width; i++) { - if (i < COLOR_DEPTH) { - current_line[i] = generate_pixel(i % COLOR_DEPTH, last_line[i], - global_params.start[i % COLOR_DEPTH]); + if (i < color_depth) { + current_line[i] = generate_pixel(i % color_depth, last_line[i], + global_params.start[i % color_depth]); } else { - current_line[i] = generate_pixel(i % COLOR_DEPTH, last_line[i], - current_line[i - COLOR_DEPTH]); + current_line[i] = generate_pixel(i % color_depth, last_line[i], + current_line[i - color_depth]); } } } @@ -42,12 +41,12 @@ void generate_bmp_line(unsigned short y, unsigned char *data_buffer, unsigned int len) { unsigned int i; unsigned int x; + if (y > 0 && (y % global_params.size) == 0) { + generate_line(); + } for (i = 0; i < len; i++) { - if (y > 0 && y % global_params.size == 0) { - generate_line(); - } - x = i / (COLOR_DEPTH * global_params.size); - data_buffer[i] = current_line[x * COLOR_DEPTH + (i % COLOR_DEPTH)]; + x = i / (color_depth * global_params.size); + data_buffer[i] = current_line[x * color_depth + (i % color_depth)]; } } @@ -58,22 +57,28 @@ void debug_parameters(parameters params) { printf("width %d\n", params.width); printf("height %d\n", params.height); printf("pixel %d\n", params.size); - printf("color %u,%u,%u\n", params.start[0], params.start[1], - params.start[2]); - printf("var. %u,%u,%u\n", params.var[0], params.var[1], params.var[2]); + if (params.monochrome) { + printf("color %u\n", params.start[0]); + printf("var. %u\n", params.var[0]); + } else { + printf("color %u,%u,%u\n", params.start[0], params.start[1], + params.start[2]); + printf("var. %u,%u,%u\n", params.var[0], params.var[1], params.var[2]); + } } } void init(parameters params) { global_params = params; - global_fslope = ((float)params.slope) / 255.0; + slope = ((float)params.slope) / 255.0; + color_depth = params.monochrome ? 1 : 3; debug_parameters(params); - line_width = ((params.width / params.size) + 1) * COLOR_DEPTH; + 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; for (i = 0; i < line_width; i++) { - current_line[i] = params.start[i % COLOR_DEPTH]; + current_line[i] = params.start[i % color_depth]; } set_seed(params.seed); generate_line(); @@ -86,7 +91,7 @@ void clean() { void generate(parameters params) { init(params); - bmp_generate(params.width, params.height, COLOR_DEPTH, params.file_path, + bmp_generate(params.width, params.height, color_depth, params.file_path, generate_bmp_line); clean(); } \ No newline at end of file diff --git a/src/types.h b/src/types.h index b3a94c7..ad67a24 100644 --- a/src/types.h +++ b/src/types.h @@ -5,6 +5,7 @@ struct Parameters { bool quiet; + bool monochrome; unsigned long seed; unsigned short width; unsigned short height;