From ed99cc649822dde14de66a2bc7c203eb6376ae78 Mon Sep 17 00:00:00 2001 From: jonas Date: Sun, 28 Apr 2019 12:11:53 +0200 Subject: Removed print to file, added args, help --- main.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 102 insertions(+), 19 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 2aaf11c..436f209 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "bitmap.h" @@ -32,6 +33,17 @@ #define CHAR_SIZE_X 2 //How many pixels should form one ASCII char? #define CHAR_SIZE_Y (2 * CHAR_SIZE_X) +struct prog_param +{ + char *filename; + unsigned int charsize_x; + unsigned int charsize_y; +}; + +struct prog_param parse_args(int argc, char *argv[]); + +void print_help( void ); + const char map[] = {' ', ' ', '.', ',', '`', '-', '~', '"', '*', ':', ';', '<', '!', '/', '?', '%', '&', '=', '$', '#'}; //const char map[] = {' ', '`', '.', ',', ':', ';', '\"', '+', '#', '@'}; @@ -47,6 +59,8 @@ char calc_char(uint8_t _c, uint8_t _min, uint8_t _max); int main(int argc, char *argv[]) { + struct prog_param args = parse_args(argc, argv); + char **ascii_buff; uint8_t b_max = 0x00; @@ -54,13 +68,8 @@ int main(int argc, char *argv[]) struct bitmap_pixel_data bitmap; - if(argc != 3) - { - printf("Usage: %s \n", argv[0]); - return 1; - } - bitmap = bitmap_read(argv[1]); + bitmap = bitmap_read(args.filename); if(bitmap.error) { @@ -70,8 +79,8 @@ int main(int argc, char *argv[]) //Calculate Averages of CHAR_SIZE x CHAR_SIZE blocks unsigned int size_x,size_y; - size_x = bitmap.x / CHAR_SIZE_X; - size_y = bitmap.y / CHAR_SIZE_Y; + size_x = bitmap.x / args.charsize_x; + size_y = bitmap.y / args.charsize_y; DEBUG_PRINTF("Creating ASCII File %u x %u\n", size_x, size_y); @@ -86,20 +95,20 @@ int main(int argc, char *argv[]) { for(int y = 0; y < size_y; y++) { - char b[CHAR_SIZE_X][CHAR_SIZE_Y]; + char b[args.charsize_x][args.charsize_y]; - for(int r = 0; r < CHAR_SIZE_Y; r++) + for(int r = 0; r < args.charsize_y; r++) { - int row = y * CHAR_SIZE_Y + r; - for(int c = 0; c < CHAR_SIZE_X; c++) + int row = y * args.charsize_y + r; + for(int c = 0; c < args.charsize_x; c++) { - int col = x * CHAR_SIZE_X + c; + int col = x * args.charsize_x + c; //b[c][r] = avg(3, (char*)&bitmap_buff[row][col]); b[c][r] = rgb_avg(bitmap.R[col][row],bitmap.G[col][row],bitmap.B[col][row]); } } - ascii_buff[x][y] = avg(CHAR_SIZE_X * CHAR_SIZE_Y, (char*)&b); + ascii_buff[x][y] = avg(args.charsize_x * args.charsize_y, (char*)&b); if((uint8_t)ascii_buff[x][y] < b_min) b_min = ascii_buff[x][y]; @@ -111,23 +120,25 @@ int main(int argc, char *argv[]) DEBUG_PRINTF("Brightness Values: Min: %u Max: %u\n", b_min, b_max); //Write Output - DEBUG_PRINTF("Opening %s for writing.\n", argv[2]); + /*DEBUG_PRINTF("Opening %s for writing.\n", argv[2]); FILE *out = fopen(argv[2], "w"); if(!out) { printf("Error opening output File. Check writing permissions.\n"); return 1; - } + }*/ for(int y = 0; y