-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrayscale.cpp
62 lines (45 loc) · 1.66 KB
/
grayscale.cpp
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
//BMP RGB TO GRAYSCALE
//09.09.24
#include "include/bitmapRW.h"
#include <math.h>
#include <ctime>
//https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
byte RGBtoGRayscale(byte r, byte g, byte b) {
float r_rgb = (float)r;
float g_rgb = (float)g;
float b_rgb = (float)b;
float r_lin = (r_rgb <= 0.04045) ? (r_rgb/12.92): (pow(((r_rgb+0.055)/1.055), 2.4));
float g_lin = (g_rgb <= 0.04045) ? (g_rgb/12.92): (pow(((g_rgb+0.055)/1.055), 2.4));
float b_lin = (b_rgb <= 0.04045) ? (b_rgb/12.92): (pow(((b_rgb+0.055)/1.055), 2.4));
float y_lin = (0.2126*r_lin+0.7152*g_lin+0.0722*b_lin);
byte y_rgb = (y_lin <= 0.0031308) ? ((byte)(12.92*y_lin)) : ((byte)(1.055*pow(y_lin, (1/2.4)) - 0.055));
return y_rgb;
//return (byte)((r+g+b)/3); //lmao bruh
}
int main(int argc, char *argv[]) {
byte *pixels;
int32_t width;
int32_t height;
uint32_t bytesPerPixel;
clock_t start, end;
start = clock();
char* input = argv[1];
char* output = argv[2];
ReadImage(input, &pixels, &width, &height, &bytesPerPixel);
//do stuff
for (int64_t i = 0; i < (uint64_t) width*height*bytesPerPixel; i+=bytesPerPixel) {
byte r, g, b;
b = pixels[i];
g = pixels[i+1];
r = pixels[i+2];
byte y_lin = RGBtoGRayscale(r, g, b);
pixels[i] = y_lin;
pixels[i+1] = y_lin;
pixels[i+2] = y_lin;
}
WriteImage(output, pixels, width, height, bytesPerPixel);
end = clock();
double runtime = double(end - start) / double(CLOCKS_PER_SEC);
printf("%f\n", runtime);
free(pixels);
}