Skip to content

Commit 2ee64f4

Browse files
Merge pull request #40 from ishantofficial24-gif/main
docs: update README and add algorithm section
2 parents 87bf611 + 52ad465 commit 2ee64f4

File tree

1 file changed

+114
-58
lines changed

1 file changed

+114
-58
lines changed

README.md

Lines changed: 114 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -113,81 +113,138 @@ For a pixel along the edge or corner, like pixel 15, we would still look for all
113113

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

116-
### 6.) Brightness Adjustment Filter
117-
- **Flag:** `-B <value>`
118-
- **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.
119-
- **Usage examples:**
120-
```sh
121-
./filter -B 40 input.bmp output.bmp # Increase brightness by 40
122-
./filter -B -30 input.bmp output.bmp # Decrease brightness by 30
123-
```
116+
### 5.) The Invert Algorithm
124117

125-
### 6.) The Threshold Algorithm
118+
The “invert” filter creates a photographic negative of an image by reversing the RGB values.
119+
If a pixel has a value of 0 (no light), its inverted value becomes 255 (full light), and vice-versa.
126120

127-
The “threshold” filter converts a colorful image into a pure black-and-white one based on pixel intensity.
121+
To compute the inverted colors, subtract each channel from 255:
128122

129-
For each pixel, the intensity is calculated as the average of its red, green, and blue values:
123+
invertedRed = 255 - originalRed
124+
invertedGreen = 255 - originalGreen
125+
invertedBlue = 255 - originalBlue
130126

131-
\[
132-
\text{intensity} = \frac{(R + G + B)}{3}
133-
\]
127+
However, simply inverting can sometimes change brightness unnaturally.
128+
To preserve the original brightness, we compute brightness for both original and inverted pixels, then apply a correction to match brightness levels.
129+
Finally, each pixel’s red, green, and blue values are set to the brightness-corrected inverted values.
130+
Applying this to every pixel produces a clean color-negative effect.
134131

135-
If the intensity is **greater than or equal to 128**, the pixel is set to **white** (`R = G = B = 255`).
136-
Otherwise, it is set to **black** (`R = G = B = 0`).
132+
137133

138-
This results in a high-contrast, two-tone image where all intermediate shades are eliminated — essentially a hard binary “black-and-white” conversion.
134+
### 6.) The Brightness Adjustment Algorithm
139135

136+
Brightness adjustment increases or decreases the light intensity of each pixel.
140137

141-
### 7.) The Edge Detection (Sobel) Algorithm
138+
To brighten or darken, a fixed value is added to each color channel:
142139

143-
The “edge detection” filter highlights sharp changes in pixel intensity, producing a sketch-like outline of the image.
140+
newRed = originalRed + value
141+
newGreen = originalGreen + value
142+
newBlue = originalBlue + value
144143

145-
For each pixel, the horizontal and vertical gradients are calculated using a 3×3 Sobel kernel applied to the surrounding pixels:
146144

147-
Horizontal (Gx):
148-
-1 0 1
149-
-2 0 2
150-
-1 0 1
145+
To ensure values stay valid, the results are clamped between 0 and 255:
151146

152-
Vertical (Gy):
153-
-1 -2 -1
154-
0 0 0
155-
1 2 1
147+
if newValue > 255 -> 255
148+
if newValue < 0 -> 0
156149

157-
The gradient magnitude for each color channel is then computed as:
158150

159-
value
160-
=
161-
𝐺
162-
𝑥
163-
2
164-
+
165-
𝐺
166-
𝑦
167-
2
168-
value=
169-
Gx
170-
2
171-
+Gy
172-
2
173-
151+
A positive value increases brightness; a negative value decreases it.
152+
Applied across the image, this produces a uniformly brighter or darker picture.
174153

154+
### 7.) The Vignette Algorithm
175155

176-
The result is clamped between 0 and 255 and replaces the original pixel value. This produces a monochrome image where edges are highlighted, giving a pencil-sketch effect.
156+
The vignette filter darkens the edges of an image to draw attention toward the center.
177157

178-
---
158+
We compute the distance of each pixel from the image center:
179159

180-
### 8.) The Glow Algorithm
160+
distance = sqrt((x - centerX)² + (y - centerY)²)
181161

182-
The “Glow” filter gives bright regions of an image a soft, luminous halo, similar to a cinematic bloom effect. This filter works by blending the original image with a blurred version of itself.
183162

184-
The algorithm first creates a copy of the image and applies a mild box blur (using a smaller kernel than the main blur filter) to it. Then, for each pixel, the final color values are calculated by combining the original pixel's color with the new blurred pixel's color using a weighted average:
163+
Then calculate a scaling factor based on distance:
185164

186-
- `finalRed = 0.7 * originalRed + 0.3 * blurredRed`
187-
- `finalGreen = 0.7 * originalGreen + 0.3 * blurredGreen`
188-
- `finalBlue = 0.7 * originalBlue + 0.3 * blurredBlue`
165+
vignetteStrength = 1 - (distance / maximumDistance)
189166

190-
The resulting values are rounded to the nearest integer and capped at 255. This process brightens the image and causes the light to "bleed" from bright areas into darker ones, creating a soft, luminous effect.
167+
168+
Each color channel is multiplied by this factor, so pixels farther from the center get darker.
169+
170+
The result is a cinematic, spotlight-like fade toward the image edges.
171+
172+
### 8.) The Threshold (Black & White) Algorithm
173+
174+
Thresholding converts an image into pure black-and-white.
175+
176+
First, compute the pixel’s grayscale average:
177+
178+
avg = (Red + Green + Blue) / 3
179+
180+
181+
If the result is 128 or greater, the pixel becomes white.
182+
Otherwise, it becomes black:
183+
184+
if avg >= 128 -> (255, 255, 255)
185+
else -> (0, 0, 0)
186+
187+
188+
This filter produces a strong high-contrast binary image, similar to printed text or stencil art.
189+
190+
### 9.) The Edge Detection Algorithm (Sobel Operator)
191+
192+
Edge detection highlights boundaries and sharp transitions in an image.
193+
194+
We apply the Sobel Kernel in both X and Y directions:
195+
196+
Gx = horizontal edge kernel
197+
Gy = vertical edge kernel
198+
199+
200+
For each pixel, we compute weighted sums for both kernels for each color channel:
201+
202+
gradient = sqrt((Gx²) + (Gy²))
203+
204+
205+
The result is then clamped between 0 and 255.
206+
207+
Pixels with strong intensity changes become bright (edges), while uniform areas appear dark — producing a clean outline/edge-map of the image.
208+
209+
### 10.) The Glow Algorithm
210+
211+
Glow enhances bright areas and gives a soft-highlight aura.
212+
213+
Steps:
214+
215+
Make a copy of the original image
216+
217+
Apply a mild blur to the copy
218+
219+
Blend the blurred and original pixels:
220+
221+
newPixel = (1 - blend) * original + blend * blurred
222+
223+
224+
A small blend value (e.g., 0.3) produces a natural glow effect without washing out details.
225+
226+
This creates a dreamy, halo-like enhancement around highlights — similar to portrait photography glow effects.
227+
228+
### 11.) The Oil-Paint Algorithm
229+
230+
The oil-paint effect simulates brush-stroke texture by grouping pixels based on intensity bins.
231+
232+
For each pixel:
233+
234+
Look at neighboring pixels within a radius
235+
236+
Compute each neighbor’s intensity bucket:
237+
238+
intensity = (R + G + B) / 3
239+
240+
241+
Count how many pixels fall into each intensity level
242+
243+
Select the most common intensity bin
244+
245+
Set the pixel’s final color to the average color of that bin
246+
247+
This produces thick, paint-style textures and smoothed color patches, mimicking traditional oil-painting strokes.
191248

192249
### Usage
193250

@@ -202,20 +259,19 @@ To apply a filter via command-line:
202259
- `t`: threshold
203260
- `d`: edge detection
204261
- `B <value>`: brightness
262+
- `o`: oilpaint
205263

264+
206265
Example for glow:
207-
./filter -G input.bmp output.bmp
266+
filter -G input.bmp output.bmp
208267

209-
For vignette:
210-
```
211-
./filter v input.bmp output.bmp
212-
```
213268
You can also chain multiple filters by supplying multiple tags (e.g., `./filter vg input.bmp output.bmp` for vignette then grayscale).
214269

215270
You should not modify any of the function signatures, nor should you modify any other files other than helpers.c.
216271

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

274+
219275
<img width="528" height="492" alt="Screenshot 2025-10-16 041628" src="https://github.com/user-attachments/assets/5695cd6f-c535-42fc-8f8f-87bfbb362364" />
220276

221277
## Team and Credits

0 commit comments

Comments
 (0)