-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.h
180 lines (156 loc) · 4.5 KB
/
image.h
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define BMP_HEADER_SIZE 54
typedef struct {
unsigned char b, g, r;
} pixel;
typedef struct {
int width, height;
pixel** pixels;
} image;
//initialize black image
static image *init_image(int w, int h) {
image* new_image = (image*)malloc(sizeof(image));
if (!new_image) {
printf("Error allocating memory for image\n");
return 0;
}
new_image->pixels = (pixel**)malloc(h * sizeof(pixel *));
if (!new_image->pixels) {
printf("Error allocating memory for image\n");
return 0;
}
new_image->height = h;
new_image->width = w;
for (int i = 0; i < h; i++) {
new_image->pixels[i] = (pixel*)malloc(w * sizeof(pixel));
if (!new_image->pixels[i]) {
printf("Error allocating memory for image\n");
return 0;
}
}
return new_image;
}
image* load_image_from_file(char* filename) {
FILE *file = fopen(filename, "rb");
if (!file) {
printf("Error reading file %s, be sure you have read permissions and that the filename was typed correctly\n", filename);
return 0;
}
unsigned char info[BMP_HEADER_SIZE];
fread(info, sizeof(unsigned char), BMP_HEADER_SIZE, file);
int width = *(int*)&info[18], height = *(int*)&info[22];
printf("%d %d\n", width, height);
image* new_image = init_image(width, height);
printf("Loaded garbage image with address %p\n", new_image);
int padding = (width%4 == 0) ? width%4 : 4 - width%4;
char temp[4];
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
fread(&(new_image->pixels[i][j]), 3, 1, file);
}
if(padding) {
fread(&temp, padding, 1, file);
}
}
fclose(file);
return new_image;
}
static char get_pixel_char(pixel p) {
char* chars = "$@B%%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,\"^`\'. ";
int length = strlen(chars);
double gray_scale = 0.21* p.r + 0.72 * p.g + 0.07 * p.b;
return chars[(int)ceil((length - 1) * gray_scale/255.0f)];
}
static double absolute(double value) {
if (value < 0)
return -value;
return value;
}
static int get_pixel_color(pixel p) {
double luminosity = 0.5 * (fmax(fmax(p.r / 255.0f, p.g / 255.0f), p.b / 255.0f) + fmin(fmin(p.r / 255.0f, p.g / 255.0f), p.b / 255.0f));
double saturation = (luminosity == 1) ? 0 : (fmax(fmax(p.r / 255.0f, p.g / 255.0f), p.b / 255.0f) - fmin(fmin(p.r / 255.0f, p.g / 255.0f), p.b / 255.0f)) / (1 - absolute(2 * luminosity - 1));
if (saturation < 0.2 || luminosity > 0.9 || luminosity < 0.1) {
return 0;//white
}
double hue;
if (p.r >= p.g && p.g >= p.b) {
hue = 60 * (p.g / 255.0f - p.b / 255.0f) / ((p.r / 255.0f - p.b / 255.0f));
}
else if (p.g > p.r && p.r >= p.b) {
hue = 60 * (2 - (p.r / 255.0f - p.b / 255.0f) / (p.g / 255.0f - p.b / 255.0f));
}
else if (p.g >= p.b && p.b > p.r) {
hue = 60 * (2 + (p.b / 255.0f - p.r / 255.0f) / (p.g / 255.0f - p.r / 255.0f));
}
else if(p.b > p.g && p.g > p.r) {
hue = 60 * (4 - (p.g / 255.0f - p.r / 255.0f)/(p.b / 255.0f - p.r / 255.0f));
}
else if(p.b > p.r && p.r >= p.g) {
hue = 60 * (4 + (p.r / 255.0f - p.g / 255.0f)/(p.b / 255.0f - p.g / 255.0f));
}
else {
hue = 60 * (6 * (p.b / 255.0f - p.g / 255.0f)/(p.r - p.g / 255.0f));
}
//hue = floor(hue);
if(hue >= 330 || hue < 16) {
return 1;//red
}
else if(hue >=16 && hue < 90) {
return 2;//yellow
}
else if(hue >=90 && hue < 160) {
return 3;//green
}
else if(hue >=160 && hue < 210) {
return 4;// cyan
}
else if(hue >=210 && hue < 270) {
return 5;//blue
}
else {
return 6; //magenta
}
}
void print_image(image *img) {
printf("Printing image with width %d and height %d\n", img->width, img->height);
for(int y = img->height-1; y >=0 ; y--) {
for(int x = 0; x < img->width ; x++) {
char c = get_pixel_char(img->pixels[y][x]);
int col = get_pixel_color(img->pixels[y][x]);
switch(col) {
case 0:
printf("\033[97m%c\033[0m", c);
break;
case 1:
printf("\033[31m%c\033[0m", c);
break;
case 2:
printf("\033[33m%c\033[0m", c);
break;
case 3:
printf("\033[32m%c\033[0m", c);
break;
case 4:
printf("\033[36m%c\033[0m", c);
break;
case 5:
printf("\033[34m%c\033[0m", c);
break;
case 6:
printf("\033[95m%c\033[0m", c);
break;
}
}
printf("\n");
}
}
void destroy_image(image* img) {
for (int i = 0; i < img->height; i++) {
free(img->pixels[i]);
}
free(img->pixels);
free(img);
}