diff --git a/src/bmp.c b/src/bmp.c index 62e7669..1b4b045 100644 --- a/src/bmp.c +++ b/src/bmp.c @@ -85,8 +85,9 @@ void bmp_generate(unsigned short width, unsigned short height, unsigned int data_length = ((unsigned int)width) * ((unsigned int)color_depth); unsigned short y; - unsigned char *data_buffer = malloc(data_length); - unsigned char *line_buffer = malloc(bmp_data_line_length(width, color_depth)); + unsigned char *data_buffer = (unsigned char *)malloc(data_length); + unsigned char *line_buffer = + (unsigned char *)malloc(bmp_data_line_length(width, color_depth)); for (y = 0; y < height; y++) { generate_line(height - y - 1, 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 d451776..e48b071 100644 --- a/src/generator.c +++ b/src/generator.c @@ -3,22 +3,18 @@ #include "rand.h" #include #include +#include #define COLOR_DEPTH 3 parameters global_params; float global_fslope; +unsigned int line_width; +unsigned char *last_line; +unsigned char *current_line; -void generate_line(unsigned short y, unsigned char *data_buffer, - unsigned int len) { - unsigned int i; - for (i = 0; i < len; i++) { - data_buffer[i] = rand_uchar(256); - } -} - -unsigned char pixel_gen(unsigned char depth, unsigned char top_pixel, - unsigned char left_pixel) { +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 + @@ -26,6 +22,35 @@ unsigned char pixel_gen(unsigned char depth, unsigned char top_pixel, return (unsigned char)v; } +void generate_line() { + unsigned int i; + for (i = 0; i < line_width; i++) { + 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]); + } else { + current_line[i] = generate_pixel(i % COLOR_DEPTH, last_line[i], + current_line[i - COLOR_DEPTH]); + } + } +} + +void generate_bmp_line(unsigned short y, unsigned char *data_buffer, + unsigned int len) { + unsigned int i; + unsigned int x; + 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)]; + } +} + void debug_parameters(parameters params) { if (!params.quiet) { printf("output %s\n", params.file_path); @@ -39,12 +64,29 @@ void debug_parameters(parameters params) { } } -int generate(parameters params) { +void init(parameters params) { global_params = params; global_fslope = ((float)params.slope) / 255.0; 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; + for (i = 0; i < line_width; i++) { + current_line[i] = params.start[i % COLOR_DEPTH]; + } set_seed(params.seed); + generate_line(); +} + +void clean() { + free(last_line); + free(current_line); +} + +void generate(parameters params) { + init(params); bmp_generate(params.width, params.height, COLOR_DEPTH, params.file_path, - generate_line); - return 0; + generate_bmp_line); + clean(); } \ No newline at end of file diff --git a/src/generator.h b/src/generator.h index 677293e..49712c2 100644 --- a/src/generator.h +++ b/src/generator.h @@ -3,6 +3,6 @@ #ifndef GENERATOR_H #define GENERATOR_H -int generate(parameters params); +void generate(parameters params); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 5fe0fcf..f35de5c 100644 --- a/src/main.c +++ b/src/main.c @@ -4,5 +4,6 @@ int main(int argc, char **argv) { parameters params; params = parse_args(argc, argv); - return generate(params); + generate(params); + return 0; } \ No newline at end of file