Skip to content

Commit 7ae89a1

Browse files
committed
Resolve merge conflicts, retain all filter functions
1 parent 63b0705 commit 7ae89a1

File tree

2 files changed

+101
-44
lines changed

2 files changed

+101
-44
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,30 @@ For a pixel along the edge or corner, like pixel 15, we would still look for all
114114
If you apply the above algorithm to each pixel in the image, the result should look like a blurry, out-of-focus version of the original.
115115

116116
### 5.) Threshold Filter (Black & White)
117-
118117
- **Flag:** `-t`
119118
- **Description:** Converts each pixel in the image to pure black or white based on its intensity. If the average of red, green, and blue is greater than or equal to 128, the pixel becomes white (255,255,255); otherwise, it becomes black (0,0,0).
120119
- **Usage example:**
121-
122120
```sh
123121
./filter -t input.bmp output.bmp
124122
```
125123

126124
### 6.) Brightness Adjustment Filter
127-
128125
- **Flag:** `-B <value>`
129126
- **Description:** Increases or decreases the brightness of the image by adding a fixed value to each pixel's R, G, and B channels. The value should be an integer—positive to increase, negative to decrease.
130-
- **Usage example:**
131-
127+
- **Usage examples:**
132128
```sh
133129
./filter -B 40 input.bmp output.bmp # Increase brightness by 40
134130
./filter -B -30 input.bmp output.bmp # Decrease brightness by 30
135131
```
136132

133+
### 7.) Vignette Filter
134+
- **Flag:** `-v`
135+
- **Description:** Applies a vignette effect that darkens the corners of the image to focus attention towards the center.
136+
- **Usage example:**
137+
```sh
138+
./filter -v input.bmp output.bmp
139+
```
140+
137141
You should not modify any of the function signatures, nor should you modify any other files other than helpers.c.
138142

139143
Consider the following grid of pixels, where we’ve numbered each pixel.

helpers.c

Lines changed: 92 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,110 @@
11
#include "helpers.h"
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
#include <math.h>
5+
#include "bmp.h"
26

3-
int min(int a,int b){
4-
if(a<b) return a;
5-
return b;
7+
int min(int a, int b) {
8+
return (a < b) ? a : b;
69
}
7-
int max(int a,int b){
8-
if(a>b) return a;
9-
return b;
10+
int max(int a, int b) {
11+
return (a > b) ? a : b;
1012
}
11-
void grayscale(int height, int width, RGBTRIPLE image[height][width]){
12-
return;
13-
14-
// Convert image to grayscale
1513

14+
void grayscale(int height, int width, RGBTRIPLE image[height][width]) {
15+
for (int i = 0; i < height; i++) {
16+
for (int j = 0; j < width; j++) {
17+
int red = image[i][j].rgbtRed;
18+
int green = image[i][j].rgbtGreen;
19+
int blue = image[i][j].rgbtBlue;
20+
int avg = (red + green + blue) / 3;
21+
image[i][j].rgbtRed = avg;
22+
image[i][j].rgbtGreen = avg;
23+
image[i][j].rgbtBlue = avg;
24+
}
25+
}
1626
}
1727

18-
void invert(int height, int width, RGBTRIPLE image[height][width]){
19-
for(int i = 0; i<height; i++){
20-
for(int j = 0; j<width; j++){
21-
//Original RGB
28+
void invert(int height, int width, RGBTRIPLE image[height][width]) {
29+
for (int i = 0; i < height; i++) {
30+
for (int j = 0; j < width; j++) {
2231
int red = image[i][j].rgbtRed;
2332
int green = image[i][j].rgbtGreen;
2433
int blue = image[i][j].rgbtBlue;
2534

26-
//Normal inversion
2735
int invRed = 255 - red;
2836
int invGreen = 255 - green;
2937
int invBlue = 255 - blue;
3038

31-
//for maintain average brightness
32-
int originalBrightness = 0.299*red + 0.587*green + 0.114*blue;
33-
int invertedBrightness = 0.299*invRed + 0.587*invGreen + 0.114*invBlue;
39+
int originalBrightness = 0.299 * red + 0.587 * green + 0.114 * blue;
40+
int invertedBrightness = 0.299 * invRed + 0.587 * invGreen + 0.114 * invBlue;
3441
int brightnessDiff = originalBrightness - invertedBrightness;
3542

3643
invRed = min(max(invRed + brightnessDiff, 0), 255);
3744
invGreen = min(max(invGreen + brightnessDiff, 0), 255);
3845
invBlue = min(max(invBlue + brightnessDiff, 0), 255);
3946

40-
//Assign
4147
image[i][j].rgbtRed = invRed;
4248
image[i][j].rgbtGreen = invGreen;
4349
image[i][j].rgbtBlue = invBlue;
4450
}
4551
}
46-
return;
47-
48-
// Convert image to grayscale
49-
5052
}
51-
void sepia(int height, int width, RGBTRIPLE image[height][width]){
52-
53-
// Convert image to sepia
5453

54+
void sepia(int height, int width, RGBTRIPLE image[height][width]) {
55+
// TODO: Implement sepia filter (currently empty)
5556
}
5657

57-
58-
void reflect(int height, int width, RGBTRIPLE image[height][width]){
59-
60-
// Reflect image horizontally
61-
58+
void reflect(int height, int width, RGBTRIPLE image[height][width]) {
59+
for (int i = 0; i < height; i++) {
60+
for (int j = 0; j < width / 2; j++) {
61+
RGBTRIPLE temp = image[i][j];
62+
image[i][j] = image[i][width - 1 - j];
63+
image[i][width - 1 - j] = temp;
64+
}
65+
}
6266
}
6367

64-
65-
void blur(int height, int width, RGBTRIPLE image[height][width]){
66-
67-
// Blur image
68-
68+
void blur(int height, int width, RGBTRIPLE image[height][width]) {
69+
RGBTRIPLE **temp = malloc(height * sizeof(RGBTRIPLE *));
70+
for (int i = 0; i < height; i++)
71+
temp[i] = malloc(width * sizeof(RGBTRIPLE));
72+
73+
int kernelSize = 21;
74+
int offset = kernelSize / 2;
75+
for (int repeat = 0; repeat < 3; repeat++) {
76+
for (int i = 0; i < height; i++) {
77+
for (int j = 0; j < width; j++) {
78+
int sumRed = 0, sumGreen = 0, sumBlue = 0, count = 0;
79+
for (int ki = -offset; ki <= offset; ki++) {
80+
for (int kj = -offset; kj <= offset; kj++) {
81+
int ni = i + ki;
82+
int nj = j + kj;
83+
if (ni >= 0 && ni < height && nj >= 0 && nj < width) {
84+
sumRed += image[ni][nj].rgbtRed;
85+
sumGreen += image[ni][nj].rgbtGreen;
86+
sumBlue += image[ni][nj].rgbtBlue;
87+
count++;
88+
}
89+
}
90+
}
91+
temp[i][j].rgbtRed = (uint8_t)(sumRed / count);
92+
temp[i][j].rgbtGreen = (uint8_t)(sumGreen / count);
93+
temp[i][j].rgbtBlue = (uint8_t)(sumBlue / count);
94+
}
95+
}
96+
for (int i = 0; i < height; i++)
97+
for (int j = 0; j < width; j++)
98+
image[i][j] = temp[i][j];
99+
}
100+
for (int i = 0; i < height; i++)
101+
free(temp[i]);
102+
free(temp);
69103
}
70104

71-
void threshold(int height, int width, RGBTRIPLE image[height][width]){
72-
for(int i = 0; i < height; i++){
73-
for(int j = 0; j < width; j++){
105+
void threshold(int height, int width, RGBTRIPLE image[height][width]) {
106+
for (int i = 0; i < height; i++) {
107+
for (int j = 0; j < width; j++) {
74108
int avg = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;
75109
if (avg >= 128) {
76110
image[i][j].rgbtRed = 255;
@@ -102,4 +136,23 @@ void brightness(int height, int width, RGBTRIPLE image[height][width], int value
102136
image[i][j].rgbtBlue = b;
103137
}
104138
}
139+
}
140+
141+
void vignette(int height, int width, RGBTRIPLE image[height][width]) {
142+
float cx = width / 2.0;
143+
float cy = height / 2.0;
144+
float max_dis = sqrt(cx * cx + cy * cy);
145+
for (int i = 0; i < height; i++) {
146+
for (int j = 0; j < width; j++) {
147+
float disx = j - cx;
148+
float disy = i - cy;
149+
float dist = sqrt(disx * disx + disy * disy);
150+
float vig = 1.0 - (dist / max_dis);
151+
if (vig < 0.0) vig = 0.0;
152+
if (vig > 1.0) vig = 1.0;
153+
image[i][j].rgbtRed = (int)(image[i][j].rgbtRed * vig);
154+
image[i][j].rgbtGreen = (int)(image[i][j].rgbtGreen * vig);
155+
image[i][j].rgbtBlue = (int)(image[i][j].rgbtBlue * vig);
156+
}
157+
}
105158
}

0 commit comments

Comments
 (0)