working types and random
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
TARGET ?= margen
|
||||
INSTALL_DIR ?= $(HOME)/.local/bin
|
||||
TEST_ARGS ?= -w=100 -h=100
|
||||
TEST_ARGS ?= -w=100
|
||||
SHELL := /bin/bash
|
||||
|
||||
.PHONY: build
|
||||
@@ -9,7 +9,7 @@ clean:
|
||||
|
||||
build:
|
||||
@mkdir -p build
|
||||
gcc -Wall src/*.c src/*.h -o build/$(TARGET)
|
||||
gcc -Wall src/*.c src/*.h -lm -o build/$(TARGET)
|
||||
|
||||
.PHONY: install
|
||||
install: build
|
||||
|
||||
+108
-82
@@ -1,16 +1,18 @@
|
||||
#include "args.h"
|
||||
#include "rand.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
void print_help(int status_code)
|
||||
{
|
||||
void print_help(int status_code) {
|
||||
puts("usage: margen "
|
||||
"[--help] "
|
||||
"[-q] "
|
||||
"[-w=WIDTH] "
|
||||
"[-h=HEIGHT] "
|
||||
"[-o=PATH] "
|
||||
"[--seed=SEED]"
|
||||
"[-p=PIXEL_SIZE] "
|
||||
"[-s=SLOPE] "
|
||||
"[-c=R,G,B] "
|
||||
@@ -22,6 +24,7 @@ void print_help(int status_code)
|
||||
" -w, --width image width (default: 1920)\n"
|
||||
" -h, --height image height (default: 1080)\n"
|
||||
" -o, --output output file (default: output.bmp)\n"
|
||||
" -seed random seed (default: time based)\n"
|
||||
" -p, --pixel pixel size (default: random)\n"
|
||||
" -s, --slope slope [0-255] (default: random)\n"
|
||||
" -c, --color base color [0-255,0-255,0-255] (default: random)\n"
|
||||
@@ -30,20 +33,15 @@ void print_help(int status_code)
|
||||
exit(status_code);
|
||||
}
|
||||
|
||||
void invalid_arg(char *arg)
|
||||
{
|
||||
void invalid_arg(char *arg) {
|
||||
fprintf(stderr, "invalid argument: '%s'\n\n", arg);
|
||||
print_help(1);
|
||||
}
|
||||
|
||||
void invalid_value(char *arg, char *subarg, char *value)
|
||||
{
|
||||
if (subarg == NULL)
|
||||
{
|
||||
void invalid_value(char *arg, char *subarg, char *value) {
|
||||
if (subarg == NULL) {
|
||||
fprintf(stderr, "invalid value for argument '%s': '%s'\n\n", arg, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
fprintf(stderr, "invalid value for argument '%s' %s: '%s'\n\n", arg, subarg,
|
||||
value);
|
||||
}
|
||||
@@ -52,129 +50,157 @@ void invalid_value(char *arg, char *subarg, char *value)
|
||||
|
||||
bool is_arg(char *arg, char *ref) { return strcoll(arg, ref) == 0; }
|
||||
|
||||
char *split_arg_value(char *arg)
|
||||
{
|
||||
char *split_arg_value(char *arg) {
|
||||
strtok(arg, "=");
|
||||
return strtok(NULL, "=");
|
||||
}
|
||||
|
||||
bool is_digit(char c) { return c >= '0' && c <= '9'; }
|
||||
|
||||
bool is_number(char *value)
|
||||
{
|
||||
if (value == NULL)
|
||||
{
|
||||
bool is_number(char *value, char *max) {
|
||||
if (value == NULL) {
|
||||
return false;
|
||||
}
|
||||
unsigned long value_len = strlen(value);
|
||||
int i;
|
||||
for (i = 0; i < value_len; i++)
|
||||
{
|
||||
if (!is_digit(value[i]))
|
||||
{
|
||||
for (i = 0; i < value_len; i++) {
|
||||
if (!is_digit(value[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
return strcmp(value, max) <= 0;
|
||||
}
|
||||
|
||||
long parse_number(char *arg, char *value)
|
||||
{
|
||||
if (!is_number(value))
|
||||
{
|
||||
unsigned char parse_char(char *arg, char *value) {
|
||||
if (!is_number(value, "255")) {
|
||||
invalid_value(arg, NULL, value);
|
||||
}
|
||||
return atol(value);
|
||||
return (char)atoi(value);
|
||||
}
|
||||
|
||||
void parse_color(char *arg, char *value, unsigned char color[3])
|
||||
{
|
||||
unsigned short parse_ushort(char *arg, char *value) {
|
||||
if (!is_number(value, "65535")) {
|
||||
invalid_value(arg, NULL, value);
|
||||
}
|
||||
return (unsigned short)atoi(value);
|
||||
}
|
||||
|
||||
unsigned short parse_ulong(char *arg, char *value) {
|
||||
if (!is_number(value, "18446744073709551615")) {
|
||||
invalid_value(arg, NULL, value);
|
||||
}
|
||||
return (unsigned long)atoll(value);
|
||||
}
|
||||
|
||||
void parse_color(char *arg, char *value, unsigned char color[3]) {
|
||||
char *tmp;
|
||||
tmp = strtok(value, ",");
|
||||
if (!is_number(tmp))
|
||||
{
|
||||
if (!is_number(tmp, "255")) {
|
||||
invalid_value(arg, "(R)", tmp);
|
||||
}
|
||||
color[0] = (unsigned char)atoi(tmp);
|
||||
color[0] = (char)atoi(tmp);
|
||||
tmp = strtok(NULL, ",");
|
||||
if (!is_number(tmp))
|
||||
{
|
||||
if (!is_number(tmp, "255")) {
|
||||
invalid_value(arg, "(G)", tmp);
|
||||
}
|
||||
color[1] = (unsigned char)atoi(tmp);
|
||||
color[1] = (char)atoi(tmp);
|
||||
tmp = strtok(NULL, ",");
|
||||
if (!is_number(tmp))
|
||||
{
|
||||
if (!is_number(tmp, "255")) {
|
||||
invalid_value(arg, "(B)", tmp);
|
||||
}
|
||||
color[2] = (unsigned char)atoi(tmp);
|
||||
color[2] = (char)atoi(tmp);
|
||||
}
|
||||
|
||||
parameters parse_args(int argc, char **argv)
|
||||
{
|
||||
parameters parse_args(int argc, char **argv) {
|
||||
parameters params;
|
||||
|
||||
params.quiet = false;
|
||||
params.width = 1920;
|
||||
params.height = 1080;
|
||||
params.seed = (unsigned long)time(NULL);
|
||||
params.width = 0;
|
||||
params.height = 0;
|
||||
params.file_path = "output.bmp";
|
||||
params.size = 1; // TODO random
|
||||
params.slope = 128; // TODO random
|
||||
params.start[0] = 128; // TODO random
|
||||
params.start[1] = 128; // TODO random
|
||||
params.start[2] = 128; // TODO random
|
||||
params.var[0] = 20; // TODO random
|
||||
params.var[1] = 20; // TODO random
|
||||
params.var[2] = 20; // TODO random
|
||||
|
||||
bool size_set = false;
|
||||
bool slope_set = false;
|
||||
bool start_set = false;
|
||||
bool var_set = false;
|
||||
|
||||
int i;
|
||||
char *arg;
|
||||
char *value;
|
||||
for (i = 1; i < argc; i++)
|
||||
{
|
||||
for (i = 1; i < argc; i++) {
|
||||
arg = argv[i];
|
||||
value = split_arg_value(arg);
|
||||
if (is_arg(arg, "--help"))
|
||||
{
|
||||
if (is_arg(arg, "--help")) {
|
||||
print_help(0);
|
||||
}
|
||||
else if (is_arg(arg, "-q") || is_arg(arg, "--quiet"))
|
||||
{
|
||||
} else if (is_arg(arg, "-q") || is_arg(arg, "--quiet")) {
|
||||
params.quiet = true;
|
||||
} else if (is_arg(arg, "-w") || is_arg(arg, "--width")) {
|
||||
params.width = parse_ushort(arg, value);
|
||||
if (params.width == 0) {
|
||||
invalid_value(arg, NULL, value);
|
||||
}
|
||||
else if (is_arg(arg, "-w") || is_arg(arg, "--width"))
|
||||
{
|
||||
params.width = (unsigned long)parse_number(arg, value);
|
||||
if (params.height == 0) {
|
||||
params.height = params.width;
|
||||
}
|
||||
else if (is_arg(arg, "-h") || is_arg(arg, "--height"))
|
||||
{
|
||||
params.height = (unsigned long)parse_number(arg, value);
|
||||
} else if (is_arg(arg, "-h") || is_arg(arg, "--height")) {
|
||||
params.height = parse_ushort(arg, value);
|
||||
if (params.height == 0) {
|
||||
invalid_value(arg, NULL, value);
|
||||
}
|
||||
else if (is_arg(arg, "-o") || is_arg(arg, "--output"))
|
||||
{
|
||||
if (params.width == 0) {
|
||||
params.width = params.height;
|
||||
}
|
||||
} else if (is_arg(arg, "--seed")) {
|
||||
params.seed = parse_ulong(arg, value);
|
||||
} else if (is_arg(arg, "-o") || is_arg(arg, "--output")) {
|
||||
params.file_path = value;
|
||||
} else if (is_arg(arg, "-p") || is_arg(arg, "--pixel")) {
|
||||
params.size = parse_ushort(arg, value);
|
||||
if (params.size == 0) {
|
||||
invalid_value(arg, NULL, value);
|
||||
}
|
||||
else if (is_arg(arg, "-p") || is_arg(arg, "--pixel"))
|
||||
{
|
||||
params.size = (unsigned char)parse_number(arg, value);
|
||||
}
|
||||
else if (is_arg(arg, "-s") || is_arg(arg, "--slope"))
|
||||
{
|
||||
params.size = (unsigned char)parse_number(arg, value);
|
||||
}
|
||||
else if (is_arg(arg, "-c") || is_arg(arg, "--color"))
|
||||
{
|
||||
size_set = true;
|
||||
} else if (is_arg(arg, "-s") || is_arg(arg, "--slope")) {
|
||||
params.slope = parse_char(arg, value);
|
||||
slope_set = true;
|
||||
} else if (is_arg(arg, "-c") || is_arg(arg, "--color")) {
|
||||
parse_color(arg, value, params.start);
|
||||
}
|
||||
else if (is_arg(arg, "-v") || is_arg(arg, "--variation"))
|
||||
{
|
||||
parse_color(arg, value, params.start);
|
||||
}
|
||||
else
|
||||
{
|
||||
start_set = true;
|
||||
} else if (is_arg(arg, "-v") || is_arg(arg, "--variation")) {
|
||||
parse_color(arg, value, params.var);
|
||||
var_set = true;
|
||||
} else {
|
||||
invalid_arg(arg);
|
||||
}
|
||||
}
|
||||
|
||||
if (params.width == 0 && params.height == 0) {
|
||||
params.width = 1920;
|
||||
params.height = 1080;
|
||||
}
|
||||
|
||||
set_seed(params.seed);
|
||||
|
||||
if (!size_set) {
|
||||
params.size = rand_ushort(10) + 3;
|
||||
}
|
||||
|
||||
if (!slope_set) {
|
||||
params.slope = rand_uchar(50) + 100;
|
||||
}
|
||||
|
||||
if (!start_set) {
|
||||
params.start[0] = rand_uchar(256);
|
||||
params.start[1] = rand_uchar(256);
|
||||
params.start[2] = rand_uchar(256);
|
||||
}
|
||||
|
||||
if (!var_set) {
|
||||
params.var[0] = rand_uchar(30);
|
||||
params.var[1] = rand_uchar(30);
|
||||
params.var[2] = rand_uchar(30);
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
@@ -4,32 +4,35 @@
|
||||
|
||||
#define HEADER_SIZE 54
|
||||
|
||||
void write_str(char *buffer, int offset, int size, char *value) {
|
||||
void write_str(unsigned char *buffer, unsigned short offset,
|
||||
unsigned short size, unsigned char *value) {
|
||||
int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
buffer[offset + i] = value[i];
|
||||
buffer[offset + i] = (unsigned char)value[i];
|
||||
}
|
||||
}
|
||||
|
||||
void write_num(char *buffer, int offset, int size, unsigned long value) {
|
||||
void write_num(unsigned char *buffer, unsigned short offset,
|
||||
unsigned short size, unsigned short value) {
|
||||
int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
buffer[offset + i] = (char)((value >> (8 * i)) & 0xFFu);
|
||||
buffer[offset + i] = (unsigned char)((value >> (8 * i)) & 0xFFu);
|
||||
}
|
||||
}
|
||||
|
||||
void write_nul(char *buffer, int offset, int size) {
|
||||
void write_nul(unsigned char *buffer, unsigned short offset,
|
||||
unsigned short size) {
|
||||
int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
buffer[offset + i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
char *bmp_header(unsigned long width, unsigned long height,
|
||||
unsigned int color_depth) {
|
||||
char *output = (char *)malloc(HEADER_SIZE);
|
||||
unsigned char *bmp_header(unsigned short width, unsigned short height,
|
||||
unsigned char color_depth) {
|
||||
unsigned char *output = (unsigned char *)malloc(HEADER_SIZE);
|
||||
|
||||
write_str(output, 0x00, 0x02, "BM"); // 0x00(2) BM
|
||||
write_str(output, 0x00, 0x02, (unsigned char *)"BM"); // 0x00(2) BM
|
||||
write_num(output, 0x02, 0x04,
|
||||
HEADER_SIZE + width * height * color_depth); // 0x02(4) file size
|
||||
write_nul(output, 0x06, 0x04); // 0x06(4) application reserved
|
||||
@@ -50,34 +53,34 @@ char *bmp_header(unsigned long width, unsigned long height,
|
||||
return output;
|
||||
}
|
||||
|
||||
unsigned long bmp_data_line_length(unsigned long width,
|
||||
unsigned int color_depth) {
|
||||
unsigned long line_offset = (width * color_depth) % 4;
|
||||
unsigned long line_padding = line_offset > 0 ? 4 - line_offset : 0;
|
||||
unsigned short bmp_data_line_length(unsigned short width,
|
||||
unsigned char color_depth) {
|
||||
unsigned short line_offset = (width * color_depth) % 4;
|
||||
unsigned short line_padding = line_offset > 0 ? 4 - line_offset : 0;
|
||||
return width * color_depth + line_padding;
|
||||
}
|
||||
|
||||
void bmp_data_line(char *buffer, unsigned long width, unsigned int color_depth,
|
||||
char *data) {
|
||||
unsigned long line_offset = (width * color_depth) % 4;
|
||||
unsigned long line_padding = line_offset > 0 ? 4 - line_offset : 0;
|
||||
void bmp_data_line(unsigned char *buffer, unsigned short width,
|
||||
unsigned char color_depth, unsigned char *data) {
|
||||
unsigned short line_offset = (width * color_depth) % 4;
|
||||
unsigned short line_padding = line_offset > 0 ? 4 - line_offset : 0;
|
||||
write_str(buffer, 0, width * color_depth, data);
|
||||
if (line_padding > 0) {
|
||||
write_nul(buffer, width * color_depth, line_padding);
|
||||
}
|
||||
}
|
||||
|
||||
void bmp_generate(unsigned long width, unsigned long height,
|
||||
unsigned int color_depth, char *file_path,
|
||||
void bmp_generate(unsigned short width, unsigned short height,
|
||||
unsigned char color_depth, char *file_path,
|
||||
line_fn generate_line) {
|
||||
FILE *fptr;
|
||||
fptr = fopen(file_path, "w");
|
||||
char *header = bmp_header(width, height, color_depth);
|
||||
unsigned char *header = bmp_header(width, height, color_depth);
|
||||
fwrite(header, HEADER_SIZE, 1, fptr);
|
||||
free(header);
|
||||
int y;
|
||||
char *data_buffer = malloc(width * color_depth);
|
||||
char *line_buffer = malloc(bmp_data_line_length(width, color_depth));
|
||||
unsigned short y;
|
||||
unsigned char *data_buffer = malloc(width * color_depth);
|
||||
unsigned char *line_buffer = malloc(bmp_data_line_length(width, color_depth));
|
||||
for (y = 0; y < height; y++) {
|
||||
generate_line(height - y - 1, data_buffer);
|
||||
bmp_data_line(line_buffer, width, color_depth, data_buffer);
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
#ifndef BMP_H
|
||||
#define BMP_H
|
||||
|
||||
void bmp_generate(unsigned long width, unsigned long height,
|
||||
unsigned int color_depth, char *file_path,
|
||||
void bmp_generate(unsigned short width, unsigned short height,
|
||||
unsigned char color_depth, char *file_path,
|
||||
line_fn generate_line);
|
||||
|
||||
#endif
|
||||
+16
-1
@@ -1,17 +1,32 @@
|
||||
#include "args.h"
|
||||
#include "bmp.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define COLOR_DEPTH 3
|
||||
|
||||
parameters global_params;
|
||||
|
||||
void generate_line(unsigned long y, char *data_buffer) {
|
||||
void generate_line(unsigned short y, unsigned char *data_buffer) {
|
||||
memset(data_buffer, y, global_params.width * COLOR_DEPTH);
|
||||
}
|
||||
|
||||
void debug_parameters(parameters params) {
|
||||
if (!params.quiet) {
|
||||
printf("output %s\n", params.file_path);
|
||||
printf("seed %ld\n", params.seed);
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
int generate(parameters params) {
|
||||
global_params = params;
|
||||
debug_parameters(params);
|
||||
bmp_generate(params.width, params.height, COLOR_DEPTH, params.file_path,
|
||||
generate_line);
|
||||
return 0;
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
#include <math.h>
|
||||
|
||||
float seed;
|
||||
|
||||
void set_seed(unsigned long new_seed) { seed = (float)(new_seed % 1000000); }
|
||||
|
||||
float rand(float seed) {
|
||||
float v = powf(fabs(seed), 6. / 7.);
|
||||
v *= sinf(v) + 1.;
|
||||
return v - floorf(v);
|
||||
}
|
||||
|
||||
unsigned char rand_uchar(unsigned int max) {
|
||||
return (unsigned char)(rand(seed++) * max);
|
||||
}
|
||||
|
||||
unsigned short rand_ushort(unsigned int max) {
|
||||
return (unsigned short)(rand(seed++) * max);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
#ifndef RAND_H
|
||||
#define RAND_H
|
||||
|
||||
void set_seed(float new_seed);
|
||||
unsigned char rand_uchar(unsigned int max);
|
||||
unsigned short rand_ushort(unsigned int max);
|
||||
|
||||
#endif
|
||||
+5
-4
@@ -5,10 +5,11 @@
|
||||
|
||||
struct Parameters {
|
||||
bool quiet;
|
||||
unsigned long width;
|
||||
unsigned long height;
|
||||
unsigned long seed;
|
||||
unsigned short width;
|
||||
unsigned short height;
|
||||
char *file_path;
|
||||
unsigned char size;
|
||||
unsigned short size;
|
||||
unsigned char slope;
|
||||
unsigned char start[3];
|
||||
unsigned char var[3];
|
||||
@@ -16,6 +17,6 @@ struct Parameters {
|
||||
|
||||
typedef struct Parameters parameters;
|
||||
|
||||
typedef void line_fn(unsigned long y, char *data_buffer);
|
||||
typedef void line_fn(unsigned short y, unsigned char *data_buffer);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user