Skip to content

Commit 5927a37

Browse files
feat(filter): add pixelate (-P flag) for mosaic effect
1 parent 2f1ef2b commit 5927a37

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@ Gx
181181

182182
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.
183183

184+
185+
186+
### Pixelate Filter
187+
188+
The “Pixelate” filter divides the image into small square blocks (default 5x5) and replaces each block with the average color of its pixels. This creates a mosaic-like effect, useful for artistic rendering or obscuring parts of an image for privacy.
189+
190+
Algorithm Steps:
191+
192+
1. Divide the image into blocks of size blockSize x blockSize.
193+
2. For each block:
194+
a. Compute the average Red, Green, and Blue values of all pixels in the block.
195+
b. Replace all pixels in the block with the computed average color.
196+
3. Repeat until all blocks are processed.
197+
198+
184199
---
185200

186201
### Usage

filter.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
int main(int argc, char *argv[])
88
{
99
// Define allowable filters
10-
char *filters = "bgrsivtd";
10+
char *filters = "bgrsivtdp";
1111

1212

1313

@@ -134,6 +134,12 @@ while ((opt = getopt(argc, argv, filters)) != -1)
134134
detect_edges(height, width, image);
135135
break;
136136

137+
138+
139+
case 'p':
140+
apply_pixelate(height, width, image, 5); // default block size = 5
141+
break;
142+
137143
default:
138144
printf("Unknown filter: %c\n", filterArr[i]);
139145
free(image);

helpers.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,59 @@ void detect_edges(int height, int width, RGBTRIPLE image[height][width])
270270
}
271271
}
272272

273+
274+
275+
// Pixelate Filter
276+
void apply_pixelate(int height, int width, RGBTRIPLE image[height][width], int blockSize)
277+
{
278+
for (int i = 0; i < height; i += blockSize)
279+
{
280+
for (int j = 0; j < width; j += blockSize)
281+
{
282+
int sumRed = 0, sumGreen = 0, sumBlue = 0;
283+
int count = 0;
284+
285+
// Compute average color in the block
286+
for (int bi = 0; bi < blockSize; bi++)
287+
{
288+
for (int bj = 0; bj < blockSize; bj++)
289+
{
290+
int ni = i + bi;
291+
int nj = j + bj;
292+
if (ni < height && nj < width)
293+
{
294+
sumRed += image[ni][nj].rgbtRed;
295+
sumGreen += image[ni][nj].rgbtGreen;
296+
sumBlue += image[ni][nj].rgbtBlue;
297+
count++;
298+
}
299+
}
300+
}
301+
302+
int avgRed = sumRed / count;
303+
int avgGreen = sumGreen / count;
304+
int avgBlue = sumBlue / count;
305+
306+
// Set all pixels in the block to the average color
307+
for (int bi = 0; bi < blockSize; bi++)
308+
{
309+
for (int bj = 0; bj < blockSize; bj++)
310+
{
311+
int ni = i + bi;
312+
int nj = j + bj;
313+
if (ni < height && nj < width)
314+
{
315+
image[ni][nj].rgbtRed = avgRed;
316+
image[ni][nj].rgbtGreen = avgGreen;
317+
image[ni][nj].rgbtBlue = avgBlue;
318+
}
319+
}
320+
}
321+
}
322+
}
323+
}
324+
325+
273326
// Free temporary array
274327
for (int i = 0; i < height; i++)
275328
free(copy[i]);

helpers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ void threshold(int height, int width, RGBTRIPLE image[height][width]);
2929
// **New: Edge Detection filter**
3030
void detect_edges(int height, int width, RGBTRIPLE image[height][width]);
3131

32+
// Pixelate filter
33+
void apply_pixelate(int height, int width, RGBTRIPLE image[height][width], int blockSize);
34+
35+
3236
#endif

0 commit comments

Comments
 (0)