aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 01dfda83e3bbba8fa1be48d4942225012bbba5ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>

#include "bitmap.h"
#include "character.h"
#include "m.h"
#include "color.h"

#ifdef _DEBUG
#warning "Compiling with DEBUG"
#define DEBUG_PRINTF(...) { printf(__VA_ARGS__); }
#else
#define DEBUG_PRINTF(...) {	}
#endif

#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;
	char *character_map;
	unsigned int charsize_x;
	unsigned int charsize_y;
	uint8_t color;
	uint8_t use_stdin;
	uint8_t use_whitespace;
	uint8_t fit_width;
	uint8_t dynamic_range;
};

struct prog_param parse_args(int argc, char *argv[]); 

void print_help( void );

int main(int argc, char *argv[])
{
	struct prog_param args = parse_args(argc, argv);

	//Stores a luminance array
	uint8_t	**ascii_buff;
	//Stores a color array
	char*	**col_buff;

	uint8_t b_max = 0x00;
	uint8_t b_min = 0xff;

	struct bitmap_pixel_data bitmap;

	bitmap = bitmap_read(args.filename);

	if(bitmap.error) {
		printf("Error reading file\n");
		return 1;
	}

	//x and y size of ASCII-image
	unsigned int size_x,size_y;

	if(args.fit_width > 0) {
		args.charsize_x = (unsigned int)((float)bitmap.x / (float)args.fit_width);
		args.charsize_y = (unsigned int)(((float)bitmap.y / (float)bitmap.x) * (float)args.charsize_x * 2);
	}
	size_x = bitmap.x / args.charsize_x;
	size_y = bitmap.y / args.charsize_y;

	DEBUG_PRINTF("Output size: %u x %u\n", size_x, size_y);

	//Allocate character sotrage
	ascii_buff = malloc(sizeof(*ascii_buff) * size_x);
	for (int i = 0; i < size_x; i++) 
		ascii_buff[i] = malloc(sizeof(ascii_buff[i]) * size_y);

	//Allocate color storage if color enabled
	if(args.color) {
		col_buff = malloc(sizeof(*col_buff) * size_x);
		for (int i = 0; i < size_x; i++)
			col_buff[i] = malloc(sizeof(col_buff[i]) * size_y);
	}

	//Nest thine Lööps
	//Very not optimal Variable names!!!!!!!!!!!!!
	//
	//For every size_x * size_y block: calculate average values of pixel blocks
	for(unsigned int x = 0; x < size_x; x++) {
		for(unsigned int y = 0; y < size_y; y++) {
			uint8_t brightness [ args.charsize_x ][ args.charsize_y ]; //Average brightness of every pixel
			uint8_t cc[ 3 ][ args.charsize_x * args.charsize_y ]; //RGB Values of Pixels
			unsigned int cc_counter = 0;

			//Iterate through Pixel block
			for(unsigned int row_c = 0; row_c < args.charsize_y; row_c++) {
				unsigned int row = y * args.charsize_y + row_c; //Actual position in Bitmap

				for(unsigned int col_c = 0; col_c < args.charsize_x; col_c++) {
					unsigned int col = x * args.charsize_x + col_c; //Actual position in bitmap

					brightness[col_c][row_c] = rgb_avg(
							bitmap.R[col][row],
							bitmap.G[col][row],
							bitmap.B[col][row]);

					if(args.color) {
						cc[0][cc_counter] = bitmap.R[col][row];
						cc[1][cc_counter] = bitmap.G[col][row];
						cc[2][cc_counter] = bitmap.B[col][row];
						cc_counter++;
					}//if
				}//for col_c
			}//for row_c

			//Calculate average color / brightness
			ascii_buff[x][y] = avg(args.charsize_x * args.charsize_y, *brightness);
			if(args.color) {
				if(args.use_whitespace)
					col_buff[x][y] = calc_bg_col_ansi(
						(uint8_t)avg(args.charsize_x * args.charsize_y, cc[0]),
						(uint8_t)avg(args.charsize_x * args.charsize_y, cc[1]),
						(uint8_t)avg(args.charsize_x * args.charsize_y, cc[2]));
				else
					col_buff[x][y] = calc_col_ansi(
						(uint8_t)avg(args.charsize_x * args.charsize_y, cc[0]),
						(uint8_t)avg(args.charsize_x * args.charsize_y, cc[1]),
						(uint8_t)avg(args.charsize_x * args.charsize_y, cc[2]));
			}

			if((uint8_t)ascii_buff[x][y] < b_min)
				b_min = ascii_buff[x][y];
			if((uint8_t)ascii_buff[x][y] > b_max)
				b_max = ascii_buff[x][y];
		}//for y
	}//for x


	if(args.color)
		printf("\e[0m");//Default colors
	
	if(! args.dynamic_range) {
		b_min = 0;
		b_max = 255;
	} else {
		DEBUG_PRINTF("Dynamic Range: Brightness Values: Min: %u Max: %u\n", b_min, b_max);
	}

	//Print buffer
	for(int y = 0; y<size_y; y++) {
		for(int x = 0; x < size_x; x++) {
			if(args.color) 
				printf("\e[%sm", col_buff[x][y]);
			if(args.use_whitespace)
				printf(" ");
			else
				printf("%c", calc_char(ascii_buff[x][y], b_min, b_max, args.character_map));
		}
		printf("\e[0m\n");
	}

	if(args.color)
		printf("\e[0m");//Default colors

	DEBUG_PRINTF("Finished!\n");

	//Cleanup
	for(int i = 0; i < size_x; i++)
		free (ascii_buff[i]);
	free(ascii_buff);

	for(int i = 0; i < bitmap.x; i++) {
		free(bitmap.R[i]);
		free(bitmap.G[i]);
		free(bitmap.B[i]);
	}
	free(bitmap.R);
	free(bitmap.G);
	free(bitmap.B);
/*
 * 	NOTE Memory leaks bc every pixel-color is allocated as a single string.
 * 	Since program is intended to terminate after a single run, 'free()' is left
 * 	out for performance reasons. 
	if(args.color) {
		for(int i = 0; i < size_x; i++)
			free(col_buff[i]);
		free(col_buff);
	}*/

	return 0;
}//main

struct prog_param parse_args(int argc, char *argv[])
{
	struct prog_param ret;

	memset(&ret, 0, sizeof ret);

	ret.charsize_x = CHAR_SIZE_X;

	for (int i = 1; i < argc; i++) {
		if(argv[i][0] == '-') {
			int icpy = i;
			for(int o = 1; o < strlen(argv[icpy]); o++) {
				switch(argv[icpy][o]) {
					case 'h':
						print_help();
						exit(0);
						break;
					case 'x':
						ret.charsize_x = atoi(argv[++i]);
						break;
					case 'y':
						ret.charsize_y = atoi(argv[++i]);
						break;
					case 'c':
						ret.color = 1;
						break;
					case 'i':
						ret.use_stdin = 1;
						break;
					case 'w':
						ret.use_whitespace = 1;
						break;
					case 's':
						ret.fit_width = atoi(argv[++i]);
						break;
					case 'd':
						ret.dynamic_range = 1;
						break;
					case 'm':
						ret.character_map = argv[++i];
						break;
					default:
						printf("Unrecognized Option '%c'\n", argv[icpy][o]);
						print_help();
						exit(1);
				};//switch
			}//for o
		}//if
		else if(ret.filename == NULL && !ret.use_stdin ) {
			ret.filename = argv[i];
		} else {
			printf("Wrong number of arguments\n");
			print_help();
			exit(1);
		}
	}//for i

	if(ret.filename == NULL && !ret.use_stdin )
	{
		printf("No input file. Use -i to read from stdin\n");
		print_help();
		exit(1);
	}

	if(!ret.charsize_y)
		ret.charsize_y = 2 * ret.charsize_x;

	return ret;
}

void print_help( void )
{
	printf("ASCIIMap\n(c) 2019 Jonas Gunz, github.com/kompetenzbolzen/AsciiMap\n");
	printf("ASCIIMap prints a ASCII representation of a bitmap image\n\nUsage: [OPTIONS] FILENAME\n");
	printf("Options:\n	-h: Print this help message\n	-x VAL: set the width of block wich makes up one character. Default: %i\n", CHAR_SIZE_X);
	printf("	-y VAL: set the height of block wich makes up one character. Default: 2*x\n	-c: Print in ANSI color mode. Default: OFF\n");
	printf("	-i: Read from STDIN instead of file.\n	-w: print only whitespaces with background color\n");
	printf("	-d: Dynamic brightness range\n	-m PALETTE: specify custom character palette from darkest to brightest\n");
}