Skip to content

Commit 56be611

Browse files
Merge pull request #44 from indrasuthar07/png-support
pixalate filter
2 parents 200432a + ba617ec commit 56be611

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

filter.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
int main(int argc, char *argv[])
99
{
1010
// Define allowable filters
11-
char *filters = "bgrsivtmdGoB:";
11+
char *filters = "bgrsivtmdGoPB:";
1212

1313
// Allocate filter array
1414
char *filterArr = (char *)malloc((argc - 2) * sizeof(char));
@@ -49,6 +49,7 @@ int main(int argc, char *argv[])
4949
printf(" -B <value> Adjust brightness\n");
5050
printf(" -G Glow\n");
5151
printf(" -o Oil paint\n");
52+
printf(" -P Pixelate (mosaic effect)\n");
5253
printf(" -m Show this menu\n\n");
5354
free(filterArr);
5455
return 0;
@@ -60,7 +61,7 @@ int main(int argc, char *argv[])
6061
if (argc < optind + 2)
6162
{
6263
printf("Usage: ./filter [flag] infile outfile\n");
63-
printf("Filters: -g (grayscale), -s (sepia), -r (reflect), -b (blur), -i (invert), -v (vignette), -G (glow), -t (threshold), -d (edge detection), -o (oil paint), -B <value> (brightness)\n");
64+
printf("Filters: -g (grayscale), -s (sepia), -r (reflect), -b (blur), -i (invert), -v (vignette), -G (glow), -t (threshold), -d (edge detection), -o (oil paint), -P (pixelate), -B <value> (brightness)\n");
6465
free(filterArr);
6566
return 3;
6667
}
@@ -147,6 +148,9 @@ int main(int argc, char *argv[])
147148
oilpaint(height, width, image);
148149
break;
149150

151+
case 'P': // Pixelate
152+
pixelate(height, width, image);
153+
break;
150154
default:
151155
printf("Unknown filter: %c\n", filterArr[i]);
152156
free_image(&img);

helpers.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,42 @@ void oilpaint(int height, int width, RGBTRIPLE image[height][width]){
423423
free(copy[i]);
424424
free(copy);
425425
}
426+
427+
// Pixelate filter
428+
void pixelate(int height, int width, RGBTRIPLE image[height][width]){
429+
int blockSize = 8;
430+
if (width > 1000 || height > 1000) {
431+
blockSize = 16;
432+
} else if (width > 500 || height > 500) {
433+
blockSize = 12;
434+
}
435+
for (int blockY = 0; blockY < height; blockY += blockSize){
436+
for (int blockX = 0; blockX < width; blockX += blockSize){
437+
int blockEndY = min(blockY + blockSize, height);
438+
int blockEndX = min(blockX + blockSize, width);
439+
440+
long sumRed = 0, sumGreen = 0, sumBlue = 0;
441+
int pixelCount = 0;
442+
443+
for (int y = blockY; y < blockEndY; y++){
444+
for (int x = blockX; x < blockEndX; x++){
445+
sumRed += image[y][x].rgbtRed;
446+
sumGreen += image[y][x].rgbtGreen;
447+
sumBlue += image[y][x].rgbtBlue;
448+
pixelCount++;
449+
}
450+
}
451+
uint8_t avgRed = (uint8_t)(sumRed / pixelCount);
452+
uint8_t avgGreen = (uint8_t)(sumGreen / pixelCount);
453+
uint8_t avgBlue = (uint8_t)(sumBlue / pixelCount);
454+
455+
for (int y = blockY; y < blockEndY; y++){
456+
for (int x = blockX; x < blockEndX; x++){
457+
image[y][x].rgbtRed = avgRed;
458+
image[y][x].rgbtGreen = avgGreen;
459+
image[y][x].rgbtBlue = avgBlue;
460+
}
461+
}
462+
}
463+
}
464+
}

helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ void vignette(int height, int width, RGBTRIPLE image[height][width]);
3737
void glow(int height, int width, RGBTRIPLE image[height][width]);
3838
// Oil Paint filter
3939
void oilpaint(int height, int width, RGBTRIPLE image[height][width]);
40+
// Pixelate filter
41+
void pixelate(int height, int width, RGBTRIPLE image[height][width]);
4042
#endif

0 commit comments

Comments
 (0)