-
Notifications
You must be signed in to change notification settings - Fork 0
/
mian.c
139 lines (114 loc) · 3.36 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
#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(BMP* input, BMP* output, UINT width, UINT height, UINT maxOrMin){
if ((maxOrMin != 0) && (maxOrMin != 1))return;
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);
if (maxOrMin) grey = max(r, g, b);
else grey = min(r, g, b);
BMP_SetPixelRGB(output, x, y, grey, grey, grey);
}
}
return;
}
void grayscale_scc(BMP* input, BMP* output, UINT width, UINT height, UINT color){
if ((color < 0) || (color>2))return;
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);
if (color == 0)grey = r;
else if (color == 1)grey = g;
else 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;
}