define vars at start of function

This commit is contained in:
2025-09-24 16:06:52 +02:00
parent e65cf7e37c
commit c54e4efaec
5 changed files with 76 additions and 39 deletions
+21 -13
View File
@@ -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_digit(char c) { return c >= '0' && c <= '9'; }
static bool is_number(char *value) { static bool is_number(char *value) {
unsigned long value_len;
unsigned int i;
if (value == NULL) { if (value == NULL) {
return false; return false;
} }
unsigned long value_len = strlen(value); value_len = strlen(value);
unsigned int i;
for (i = 0; i < value_len; i++) { for (i = 0; i < value_len; i++) {
if (!is_digit(value[i])) { if (!is_digit(value[i])) {
return false; return false;
} }
} }
return true; return true;
} }
static unsigned int parse_uint(char *arg, char *value) { static unsigned int parse_uint(char *arg, char *value) {
unsigned long long tmp_value;
if (!is_number(value)) { if (!is_number(value)) {
invalid_value(arg, 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) { if (tmp_value >= UINT_MAX) {
invalid_value(arg, value); invalid_value(arg, value);
} }
return (unsigned int)tmp_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]) { static void parse_color(char *arg, char *value, unsigned int color[3]) {
char *tmp; char *tmp;
tmp = strtok(value, ","); tmp = strtok(value, ",");
color[0] = parse_uint(arg, tmp); color[0] = parse_uint(arg, tmp);
tmp = strtok(NULL, ","); 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 args_parse(int argc, char **argv) {
Parameters params; 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.quiet = false;
params.seed = (unsigned long)time(NULL); params.seed = (unsigned long)time(NULL);
@@ -120,22 +131,19 @@ Parameters args_parse(int argc, char **argv) {
params.file_path = "output.bmp"; params.file_path = "output.bmp";
params.monochrome = false; params.monochrome = false;
unsigned char var_range = 30; var_range = 30;
bool size_set = false; size_set = false;
bool slope_set = false; slope_set = false;
bool start_set = false; start_set = false;
bool var_set = false; var_set = false;
bool rot_set = false; rot_set = false;
int i;
char *arg;
char *value;
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
arg = argv[i]; arg = argv[i];
value = split_arg_value(arg); value = split_arg_value(arg);
if (is_arg(arg, "--help")) { if (is_arg(arg, "--help")) {
print_help(0); print_help(EXIT_SUCCESS);
} else if (is_arg(arg, "-q") || is_arg(arg, "--quiet")) { } else if (is_arg(arg, "-q") || is_arg(arg, "--quiet")) {
params.quiet = true; params.quiet = true;
} else if (is_arg(arg, "-v") || is_arg(arg, "--version")) { } else if (is_arg(arg, "-v") || is_arg(arg, "--version")) {
+27 -14
View File
@@ -9,6 +9,7 @@
static void write_str(unsigned char *buffer, unsigned int offset, static void write_str(unsigned char *buffer, unsigned int offset,
unsigned int size, unsigned char *value) { unsigned int size, unsigned char *value) {
unsigned int i; unsigned int i;
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
buffer[offset + i] = (unsigned char)value[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, static void write_num(unsigned char *buffer, unsigned int offset,
unsigned int size, unsigned int value) { unsigned int size, unsigned int value) {
unsigned int i; unsigned int i;
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
buffer[offset + i] = (unsigned char)((value >> (8 * i)) & 0xFFu); 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, static void write_nul(unsigned char *buffer, unsigned int offset,
unsigned int size) { unsigned int size) {
unsigned int i; unsigned int i;
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
buffer[offset + i] = 0; 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, static unsigned char *bmp_header(unsigned int width, unsigned int height,
unsigned int color_depth) { unsigned int color_depth) {
unsigned char *output = (unsigned char *)malloc(HEADER_SIZE); unsigned char *output;
unsigned int data_length = width * height * color_depth; unsigned int data_length;
output = malloc(HEADER_SIZE * sizeof(unsigned char));
data_length = width * height * color_depth;
// 0x00(2) BM // 0x00(2) BM
write_str(output, 0x00, 0x02, (unsigned char *)"BM"); write_str(output, 0x00, 0x02, (unsigned char *)"BM");
// 0x02(4) file size // 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, static unsigned int bmp_data_line_length(unsigned int width,
unsigned int color_depth) { unsigned int color_depth) {
unsigned int data_length = width * color_depth; unsigned int data_length, line_offset, line_padding;
unsigned int line_offset = data_length % 4;
unsigned int line_padding = line_offset > 0 ? 4 - line_offset : 0; data_length = width * color_depth;
line_offset = data_length % 4;
line_padding = line_offset > 0 ? 4 - line_offset : 0;
return data_length + line_padding; return data_length + line_padding;
} }
static void bmp_data_line(unsigned char *buffer, unsigned int width, static void bmp_data_line(unsigned char *buffer, unsigned int width,
unsigned int color_depth, unsigned char *data) { unsigned int color_depth, unsigned char *data) {
unsigned int data_length = width * color_depth; unsigned int data_length, line_offset, line_padding;
unsigned int line_offset = data_length % 4;
unsigned int line_padding = line_offset > 0 ? 4 - line_offset : 0; 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); write_str(buffer, 0, data_length, data);
if (line_padding > 0) { if (line_padding > 0) {
write_nul(buffer, width * color_depth, line_padding); 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, unsigned int color_depth, bool descending, char *file_path,
line_fn generate_line) { line_fn generate_line) {
FILE *fptr; FILE *fptr;
unsigned char *header, *data_buffer, *line_buffer;
unsigned int data_length, line_length, y;
fptr = fopen(file_path, "w"); 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); fwrite(header, HEADER_SIZE, 1, fptr);
free(header); free(header);
unsigned int data_length = width * color_depth; data_length = width * color_depth;
unsigned int line_length = bmp_data_line_length(width, color_depth); line_length = bmp_data_line_length(width, color_depth);
unsigned char *data_buffer = (unsigned char *)malloc(data_length); data_buffer = malloc(data_length * sizeof(unsigned char));
unsigned char *line_buffer = (unsigned char *)malloc(line_length); line_buffer = malloc(line_length * sizeof(unsigned char));
unsigned int y;
for (y = 0; y < height; y++) { for (y = 0; y < height; y++) {
generate_line(y, data_buffer, data_length); generate_line(y, data_buffer, data_length);
bmp_data_line(line_buffer, width, color_depth, data_buffer); bmp_data_line(line_buffer, width, color_depth, data_buffer);
+14 -6
View File
@@ -19,14 +19,18 @@ static unsigned char *current_line;
static unsigned char generate_pixel(unsigned char depth, static unsigned char generate_pixel(unsigned char depth,
unsigned char top_pixel, unsigned char top_pixel,
unsigned char left_pixel) { unsigned char left_pixel) {
int k = rand_uint(global_params.var[depth] + 1); int k, v;
int v = (rand_uint(2) == 0 ? k : -k) + (left_pixel)*slope +
k = rand_uint(global_params.var[depth] + 1);
v = (rand_uint(2) == 0 ? k : -k) + (left_pixel)*slope +
(top_pixel) * (1.0 - slope); (top_pixel) * (1.0 - slope);
return (unsigned char)(v < 0 ? 0 : (v > 255 ? (unsigned char)255 : v)); return (unsigned char)(v < 0 ? 0 : (v > 255 ? (unsigned char)255 : v));
} }
static void generate_line() { static void generate_line() {
unsigned int i; unsigned int i;
for (i = 0; i < line_width; i++) { for (i = 0; i < line_width; i++) {
last_line[i] = current_line[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 len) {
unsigned int i; unsigned int i;
unsigned int x; unsigned int x;
if (y % global_params.size == 0) { if (y % global_params.size == 0) {
generate_line(); generate_line();
} }
@@ -77,14 +82,15 @@ static void debug_parameters(Parameters params) {
} }
static void init(Parameters params) { static void init(Parameters params) {
unsigned int i;
global_params = params; global_params = params;
slope = ((float)params.slope) / 255.0; slope = ((float)params.slope) / 255.0;
color_depth = params.monochrome ? 1 : 3; color_depth = params.monochrome ? 1 : 3;
debug_parameters(params); 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); last_line = malloc(line_width * sizeof(unsigned char));
current_line = (unsigned char *)malloc(line_width); current_line = malloc(line_width * sizeof(unsigned char));
unsigned int i = 0;
for (i = 0; i < line_width; i++) { for (i = 0; i < line_width; i++) {
current_line[i] = params.start[i % color_depth]; 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) { void generator_run(Parameters params) {
clock_t start;
if (!params.quiet) { if (!params.quiet) {
puts(PACKAGE " " VERSION); puts(PACKAGE " " VERSION);
} }
clock_t start = clock(); start = clock();
init(params); init(params);
bmp_generate(params.width, params.height, BMP_COLOR_DEPTH, bmp_generate(params.width, params.height, BMP_COLOR_DEPTH,
params.rotation % 2 == 1, params.file_path, generate_bmp_line); params.rotation % 2 == 1, params.file_path, generate_bmp_line);
+6 -2
View File
@@ -1,11 +1,15 @@
#include "main.h" #include <stdlib.h>
#include "args.h" #include "args.h"
#include "generator.h" #include "generator.h"
#include "main.h"
#include "types.h" #include "types.h"
int main(int argc, char **argv) { int main(int argc, char **argv) {
Parameters params; Parameters params;
params = args_parse(argc, argv); params = args_parse(argc, argv);
generator_run(params); generator_run(params);
return 0;
return EXIT_SUCCESS;
} }
+7 -3
View File
@@ -5,11 +5,15 @@ static unsigned long long const multiplier = 6364136223846793005u;
// https://en.wikipedia.org/wiki/Permuted_congruential_generator // https://en.wikipedia.org/wiki/Permuted_congruential_generator
static unsigned long rand(void) { static unsigned long rand(void) {
unsigned long long x = mcg_state; unsigned long long x;
unsigned count = (unsigned)(x >> 61); unsigned count;
x = mcg_state;
count = (unsigned)(x >> 61);
mcg_state = x * multiplier; mcg_state = x * multiplier;
x ^= x >> 22; x ^= x >> 22;
return (unsigned long)(x >> (22 + count)); return (unsigned long)(x >> (22 + count));
} }
@@ -18,6 +22,6 @@ void rand_seed(unsigned long long seed) {
(void)rand(); (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); return max == 0 ? 0 : (unsigned int)(rand() % max);
} }