static private functions and public functions starting with file name
This commit is contained in:
+13
-13
@@ -9,7 +9,7 @@
|
|||||||
#include "rand.h"
|
#include "rand.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
void print_help(int status_code) {
|
static void print_help(int status_code) {
|
||||||
puts(PACKAGE
|
puts(PACKAGE
|
||||||
" " VERSION "\n\n"
|
" " VERSION "\n\n"
|
||||||
"usage: " PACKAGE " "
|
"usage: " PACKAGE " "
|
||||||
@@ -48,26 +48,26 @@ void print_help(int status_code) {
|
|||||||
exit(status_code);
|
exit(status_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
void invalid_arg(char *arg) {
|
static void invalid_arg(char *arg) {
|
||||||
fprintf(stderr, "invalid argument: '%s'\n\n", arg);
|
fprintf(stderr, "invalid argument: '%s'\n\n", arg);
|
||||||
print_help(EXIT_FAILURE);
|
print_help(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void invalid_value(char *arg, char *value) {
|
static void invalid_value(char *arg, char *value) {
|
||||||
fprintf(stderr, "invalid value for argument '%s': '%s'\n\n", arg, value);
|
fprintf(stderr, "invalid value for argument '%s': '%s'\n\n", arg, value);
|
||||||
print_help(EXIT_FAILURE);
|
print_help(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_arg(char *arg, char *ref) { return strcoll(arg, ref) == 0; }
|
static bool is_arg(char *arg, char *ref) { return strcoll(arg, ref) == 0; }
|
||||||
|
|
||||||
char *split_arg_value(char *arg) {
|
static char *split_arg_value(char *arg) {
|
||||||
strtok(arg, "=");
|
strtok(arg, "=");
|
||||||
return strtok(NULL, "=");
|
return strtok(NULL, "=");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_digit(char c) { return c >= '0' && c <= '9'; }
|
static bool is_digit(char c) { return c >= '0' && c <= '9'; }
|
||||||
|
|
||||||
bool is_number(char *value) {
|
static bool is_number(char *value) {
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ bool is_number(char *value) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char parse_char(char *arg, char *value) {
|
static unsigned char parse_char(char *arg, char *value) {
|
||||||
if (!is_number(value)) {
|
if (!is_number(value)) {
|
||||||
invalid_value(arg, value);
|
invalid_value(arg, value);
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,7 @@ unsigned char parse_char(char *arg, char *value) {
|
|||||||
return (unsigned char)tmp_value;
|
return (unsigned char)tmp_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned short parse_ushort(char *arg, char *value) {
|
static unsigned short parse_ushort(char *arg, char *value) {
|
||||||
if (!is_number(value)) {
|
if (!is_number(value)) {
|
||||||
invalid_value(arg, value);
|
invalid_value(arg, value);
|
||||||
}
|
}
|
||||||
@@ -103,14 +103,14 @@ unsigned short parse_ushort(char *arg, char *value) {
|
|||||||
return (unsigned short)tmp_value;
|
return (unsigned short)tmp_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long parse_ulong(char *arg, char *value) {
|
static unsigned long parse_ulong(char *arg, char *value) {
|
||||||
if (!is_number(value)) {
|
if (!is_number(value)) {
|
||||||
invalid_value(arg, value);
|
invalid_value(arg, value);
|
||||||
}
|
}
|
||||||
return (unsigned long)atoll(value);
|
return (unsigned long)atoll(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_color(char *arg, char *value, unsigned char color[3]) {
|
static void parse_color(char *arg, char *value, unsigned char color[3]) {
|
||||||
char *tmp;
|
char *tmp;
|
||||||
tmp = strtok(value, ",");
|
tmp = strtok(value, ",");
|
||||||
color[0] = parse_char(arg, tmp);
|
color[0] = parse_char(arg, tmp);
|
||||||
@@ -120,7 +120,7 @@ void parse_color(char *arg, char *value, unsigned char color[3]) {
|
|||||||
color[2] = parse_char(arg, tmp);
|
color[2] = parse_char(arg, tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
Parameters parse_args(int argc, char **argv) {
|
Parameters args_parse(int argc, char **argv) {
|
||||||
Parameters params;
|
Parameters params;
|
||||||
|
|
||||||
params.quiet = false;
|
params.quiet = false;
|
||||||
@@ -203,7 +203,7 @@ Parameters parse_args(int argc, char **argv) {
|
|||||||
params.height = 1080;
|
params.height = 1080;
|
||||||
}
|
}
|
||||||
|
|
||||||
set_seed(params.seed);
|
rand_seed(params.seed);
|
||||||
|
|
||||||
if (!size_set) {
|
if (!size_set) {
|
||||||
params.size = rand_ushort(6) + 6;
|
params.size = rand_ushort(6) + 6;
|
||||||
|
|||||||
+1
-1
@@ -3,6 +3,6 @@
|
|||||||
#ifndef ARGS_H
|
#ifndef ARGS_H
|
||||||
#define ARGS_H
|
#define ARGS_H
|
||||||
|
|
||||||
Parameters parse_args(int argc, char **argv);
|
Parameters args_parse(int argc, char **argv);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -6,30 +6,31 @@
|
|||||||
|
|
||||||
#define HEADER_SIZE 54
|
#define HEADER_SIZE 54
|
||||||
|
|
||||||
void write_str(unsigned char *buffer, unsigned int offset, unsigned int size,
|
static void write_str(unsigned char *buffer, unsigned int offset,
|
||||||
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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_num(unsigned char *buffer, unsigned int offset, unsigned int size,
|
static void write_num(unsigned char *buffer, unsigned int offset,
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_nul(unsigned char *buffer, unsigned int offset, unsigned int size) {
|
static void write_nul(unsigned char *buffer, unsigned int offset,
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *bmp_header(unsigned short width, unsigned short height,
|
static unsigned char *bmp_header(unsigned short width, unsigned short height,
|
||||||
unsigned char color_depth) {
|
unsigned char color_depth) {
|
||||||
unsigned char *output = (unsigned char *)malloc(HEADER_SIZE);
|
unsigned char *output = (unsigned char *)malloc(HEADER_SIZE);
|
||||||
unsigned int data_length = ((unsigned int)width) * ((unsigned int)height) *
|
unsigned int data_length = ((unsigned int)width) * ((unsigned int)height) *
|
||||||
@@ -68,7 +69,7 @@ unsigned char *bmp_header(unsigned short width, unsigned short height,
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int bmp_data_line_length(unsigned short width,
|
static unsigned int bmp_data_line_length(unsigned short width,
|
||||||
unsigned char color_depth) {
|
unsigned char color_depth) {
|
||||||
unsigned int data_length =
|
unsigned int data_length =
|
||||||
((unsigned int)width) * ((unsigned int)color_depth);
|
((unsigned int)width) * ((unsigned int)color_depth);
|
||||||
@@ -77,7 +78,7 @@ unsigned int bmp_data_line_length(unsigned short width,
|
|||||||
return data_length + line_padding;
|
return data_length + line_padding;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bmp_data_line(unsigned char *buffer, unsigned short width,
|
static void bmp_data_line(unsigned char *buffer, unsigned short width,
|
||||||
unsigned char color_depth, unsigned char *data) {
|
unsigned char color_depth, unsigned char *data) {
|
||||||
unsigned int data_length =
|
unsigned int data_length =
|
||||||
((unsigned int)width) * ((unsigned int)color_depth);
|
((unsigned int)width) * ((unsigned int)color_depth);
|
||||||
|
|||||||
+16
-15
@@ -9,14 +9,15 @@
|
|||||||
|
|
||||||
#define BMP_COLOR_DEPTH 3
|
#define BMP_COLOR_DEPTH 3
|
||||||
|
|
||||||
Parameters global_params;
|
static Parameters global_params;
|
||||||
float slope;
|
static float slope;
|
||||||
unsigned char color_depth;
|
static unsigned char color_depth;
|
||||||
unsigned int line_width;
|
static unsigned int line_width;
|
||||||
unsigned char *last_line;
|
static unsigned char *last_line;
|
||||||
unsigned char *current_line;
|
static unsigned char *current_line;
|
||||||
|
|
||||||
unsigned char generate_pixel(unsigned char depth, unsigned char top_pixel,
|
static unsigned char generate_pixel(unsigned char depth,
|
||||||
|
unsigned char top_pixel,
|
||||||
unsigned char left_pixel) {
|
unsigned char left_pixel) {
|
||||||
short k = rand_uchar(global_params.var[depth] + 1);
|
short k = rand_uchar(global_params.var[depth] + 1);
|
||||||
short v = (rand_uchar(2) == 0 ? k : -k) + (left_pixel)*slope +
|
short v = (rand_uchar(2) == 0 ? k : -k) + (left_pixel)*slope +
|
||||||
@@ -24,7 +25,7 @@ unsigned char generate_pixel(unsigned char depth, unsigned char top_pixel,
|
|||||||
return (unsigned char)(v < 0 ? 0 : (v > 255 ? (unsigned char)255 : v));
|
return (unsigned char)(v < 0 ? 0 : (v > 255 ? (unsigned char)255 : v));
|
||||||
}
|
}
|
||||||
|
|
||||||
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];
|
||||||
@@ -40,7 +41,7 @@ void generate_line() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void generate_bmp_line(unsigned short y, unsigned char *data_buffer,
|
static void generate_bmp_line(unsigned short y, unsigned char *data_buffer,
|
||||||
unsigned int len) {
|
unsigned int len) {
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
unsigned int x;
|
unsigned int x;
|
||||||
@@ -54,7 +55,7 @@ void generate_bmp_line(unsigned short y, unsigned char *data_buffer,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void debug_parameters(Parameters params) {
|
static void debug_parameters(Parameters params) {
|
||||||
if (!params.quiet) {
|
if (!params.quiet) {
|
||||||
printf(" output %s\n", params.file_path);
|
printf(" output %s\n", params.file_path);
|
||||||
printf(" seed %ld\n", params.seed);
|
printf(" seed %ld\n", params.seed);
|
||||||
@@ -75,7 +76,7 @@ void debug_parameters(Parameters params) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void init(Parameters params) {
|
static void init(Parameters params) {
|
||||||
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;
|
||||||
@@ -87,22 +88,22 @@ void init(Parameters params) {
|
|||||||
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];
|
||||||
}
|
}
|
||||||
set_seed(params.seed);
|
rand_seed(params.seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
void clean() {
|
static void clean() {
|
||||||
free(last_line);
|
free(last_line);
|
||||||
free(current_line);
|
free(current_line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_time(Parameters params, clock_t start) {
|
static void print_time(Parameters params, clock_t start) {
|
||||||
if (!params.quiet) {
|
if (!params.quiet) {
|
||||||
clock_t now = clock();
|
clock_t now = clock();
|
||||||
printf("time: %.3fs\n", (float)(now - start) / CLOCKS_PER_SEC);
|
printf("time: %.3fs\n", (float)(now - start) / CLOCKS_PER_SEC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void generate(Parameters params) {
|
void generator_run(Parameters params) {
|
||||||
if (!params.quiet) {
|
if (!params.quiet) {
|
||||||
puts(PACKAGE " " VERSION);
|
puts(PACKAGE " " VERSION);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -3,6 +3,6 @@
|
|||||||
#ifndef GENERATOR_H
|
#ifndef GENERATOR_H
|
||||||
#define GENERATOR_H
|
#define GENERATOR_H
|
||||||
|
|
||||||
void generate(Parameters params);
|
void generator_run(Parameters params);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+3
-2
@@ -1,10 +1,11 @@
|
|||||||
|
#include "main.h"
|
||||||
#include "args.h"
|
#include "args.h"
|
||||||
#include "generator.h"
|
#include "generator.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
Parameters params;
|
Parameters params;
|
||||||
params = parse_args(argc, argv);
|
params = args_parse(argc, argv);
|
||||||
generate(params);
|
generator_run(params);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#ifndef MAIN_H
|
#ifndef MAIN_H
|
||||||
#define MAIN_H
|
#define MAIN_H
|
||||||
|
|
||||||
|
int main(int argc, char **argv);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+2
-2
@@ -4,7 +4,7 @@ static unsigned long long mcg_state = 0xcafef00dd15ea5e5u; // Must be odd
|
|||||||
static unsigned long long const multiplier = 6364136223846793005u;
|
static unsigned long long const multiplier = 6364136223846793005u;
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Permuted_congruential_generator
|
// https://en.wikipedia.org/wiki/Permuted_congruential_generator
|
||||||
unsigned long rand(void) {
|
static unsigned long rand(void) {
|
||||||
unsigned long long x = mcg_state;
|
unsigned long long x = mcg_state;
|
||||||
unsigned count = (unsigned)(x >> 61);
|
unsigned count = (unsigned)(x >> 61);
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ unsigned long rand(void) {
|
|||||||
return (unsigned long)(x >> (22 + count));
|
return (unsigned long)(x >> (22 + count));
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_seed(unsigned long long seed) {
|
void rand_seed(unsigned long long seed) {
|
||||||
mcg_state = 2 * seed + 1;
|
mcg_state = 2 * seed + 1;
|
||||||
(void)rand();
|
(void)rand();
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
#ifndef RAND_H
|
#ifndef RAND_H
|
||||||
#define RAND_H
|
#define RAND_H
|
||||||
|
|
||||||
void set_seed(unsigned long long seed);
|
void rand_seed(unsigned long long seed);
|
||||||
unsigned char rand_uchar(unsigned int max);
|
unsigned char rand_uchar(unsigned int max);
|
||||||
unsigned short rand_ushort(unsigned int max);
|
unsigned short rand_ushort(unsigned int max);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user