-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
100 lines (81 loc) · 2.69 KB
/
test.c
File metadata and controls
100 lines (81 loc) · 2.69 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//including the library
#include "src/bmp.c"
unsigned char clamp(int value, int min, int max) {
if (value < min)
return (unsigned char)min;
else if (value > max)
return (unsigned char)max;
else
return (unsigned char)value;
}
//testing Bitmap library
// taking image as input and grayscaling it
void grayscale(BMPImage* img) {
for (int y = 0; y < img->infoHeader.height; y++) {
for (int x = 0; x < img->infoHeader.width; x++) {
Pixel p = getPixel(img,x,y);
unsigned char pixavg = (p.r+p.g+p.b)/3;
// average rgb values
p.r = pixavg;
p.g = pixavg;
p.b = pixavg;
setPixel(img,x,y,p);
}
}
}
// taking image as input and inverting each pixel
void invert(BMPImage* img) {
for (int y = 0; y < img->infoHeader.height; y++) {
for (int x = 0; x < img->infoHeader.width; x++) {
Pixel p = getPixel(img,x,y);
// invert rgb values
p.r = 255 - p.r;
p.g = 255 - p.g;
p.b = 255 - p.b;
setPixel(img,x,y,p);
}
}
}
// taking image as input and decreasing blue and green channels on each pixel
void vintage(BMPImage* img) {
for (int y = 0; y < img->infoHeader.height; y++) {
for (int x = 0; x < img->infoHeader.width; x++) {
Pixel p = getPixel(img, x, y);
// decrease blue and green channels
p.b = (unsigned char)(p.b * 0.5);
p.g = (unsigned char)(p.g * 0.7);
setPixel(img, x, y, p);
}
}
}
// taking image as input and adding random noise to each pixel
void noise(BMPImage* img,int intensity) {
srand(time(NULL)); // random number seed
for (int y = 0; y < img->infoHeader.height; y++) {
for (int x = 0; x < img->infoHeader.width; x++) {
Pixel p = getPixel(img, x, y);
// Add random noise to each channel
int noiseR = rand() % (intensity * 2 + 1) - intensity; // random noise between -intensity to +intensity
int noiseG = rand() % (intensity * 2 + 1) - intensity;
int noiseB = rand() % (intensity * 2 + 1) - intensity;
p.r = clamp(p.r + noiseR, 0, 255); // clamp to 0-255
p.g = clamp(p.g + noiseG, 0, 255);
p.b = clamp(p.b + noiseB, 0, 255);
setPixel(img, x, y, p);
}
}
}
int main() {
// load image from path
BMPImage img = readBMP("tests/0.bmp");
// add filters defined above
noise(&img, 100);
grayscale(&img);
invert(&img);
vintage(&img);
// writing output to a new file
writeBMP("tests/0out.bmp", img);
}