-
Notifications
You must be signed in to change notification settings - Fork 0
/
mian.c
173 lines (142 loc) · 4.05 KB
/
mian.c
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
#include "qdbmp.h"
UINT max(UINT r, UINT g, UINT b){
if ((r >= g) && (r >= b)) return r;
else if ((g >= r) && (g >= b)) return g;
return b;
}
UINT min(UINT r, UINT g, UINT b){
if ((r <= g) && (r <= b)) return r;
else if ((g <= r) && (g <= b)) return g;
return b;
}
void negative(BMP* input, BMP* output, UINT width, UINT height){
UCHAR r, g, b;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
BMP_SetPixelRGB(output, x, y, 255 - r, 255 - g, 255 - b);
}
}
return;
}
void grayscale_avg(BMP* input, BMP* output, UINT width, UINT height){
UCHAR r, g, b, grey;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
grey = (r + g + b) / 3;
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void grayscale_luma(BMP* input, BMP* output, UINT width, UINT height){
UCHAR r, g, b, grey;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
grey = r*0.2126 + g*0.7152 + b*0.0722;
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void grayscale_desaturation(BMP* input, BMP* output, UINT width, UINT height){
UCHAR r, g, b, grey;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
grey = (max(r, g, b) + min(r, g, b)) / 2;
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void grayscale_decomposition_max(BMP* input, BMP* output, UINT width, UINT height){
UCHAR r, g, b, grey;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
grey = max(r, g, b);
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void grayscale_decomposition_min(BMP* input, BMP* output, UINT width, UINT height){
UCHAR r, g, b, grey;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
grey = min(r, g, b);
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void grayscale_scc_red(BMP* input, BMP* output, UINT width, UINT height){
UCHAR r, g, b, grey;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
grey = r;
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void grayscale_scc_green(BMP* input, BMP* output, UINT width, UINT height){
UCHAR r, g, b, grey;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
grey = g;
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void grayscale_scc_blue(BMP* input, BMP* output, UINT width, UINT height){
UCHAR r, g, b, grey;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
grey = b;
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void grayscale_custom_shades_of_grey(BMP* input, BMP* output, UINT width, UINT height, UINT noOfShades){
if (noOfShades < 2 || noOfShades >256) return;
UCHAR r, g, b, grey, ConversionFactor, AverageValue;
for (UINT x = 0; x < width; ++x){
for (UINT y = 0; y < height; ++y){
BMP_GetPixelRGB(input, x, y, &r, &g, &b);
ConversionFactor = 255 / (noOfShades - 1);
AverageValue = (r + g + b) / 3;
grey = (UINT)((AverageValue / ConversionFactor) + 0.5) * ConversionFactor;
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void main(){
BMP *input, *output;
UCHAR r, g, b, grey;
UINT width, height;
UINT x, y;
USHORT depth;
input = BMP_ReadFile("Inputs//fish.bmp");
BMP_CHECK_ERROR(stderr, -1);
width = BMP_GetWidth(input);
height = BMP_GetHeight(input);
depth = BMP_GetDepth(input);
output = BMP_Create(width, height, depth);
grayscale_custom_shades_of_grey(input, output, width, height, 256);
BMP_WriteFile(output, "Grayscale//fish_256_shades_of_grey.bmp");
BMP_CHECK_ERROR(stderr, -2);
BMP_Free(input);
BMP_Free(output);
return;
}