Skip to content

Commit a93600f

Browse files
feat: first iteration of spiral filter
1 parent 56be611 commit a93600f

File tree

4 files changed

+44
-42
lines changed

4 files changed

+44
-42
lines changed

filter.c

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

13-
// Allocate filter array
14-
char *filterArr = (char *)malloc((argc - 2) * sizeof(char));
15-
if (!filterArr) {
16-
printf("Memory allocation error.\n");
17-
return 1;
18-
}
13+
14+
char filterArr[argc-3];
1915
int filterCount = 0;
2016
int brightness_value = 0;
2117

@@ -147,7 +143,9 @@ int main(int argc, char *argv[])
147143
case 'o':
148144
oilpaint(height, width, image);
149145
break;
150-
146+
case 'S':
147+
spiral(height, width, image);
148+
break;
151149
case 'P': // Pixelate
152150
pixelate(height, width, image);
153151
break;

helpers.c

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -424,41 +424,41 @@ void oilpaint(int height, int width, RGBTRIPLE image[height][width]){
424424
free(copy);
425425
}
426426

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-
}
427+
428+
void spiral(int height, int width, RGBTRIPLE image[height][width])
429+
{
430+
RGBTRIPLE (*output)[width] = calloc(height, width * sizeof(RGBTRIPLE));
431+
double cx = width / 2.0;
432+
double cy = height / 2.0;
433+
double max_r = sqrt(cx * cx + cy * cy);
434+
double k = 4.0;
435+
for (int y=0;y<height;y++)
436+
{
437+
for (int x=0;x<width;x++)
438+
{
439+
double dx=x-cx;
440+
double dy=y-cy;
441+
double r=sqrt(dx*dx+dy*dy);
442+
double theta=atan2(dy,dx);
443+
double factor=(max_r-r)/max_r;
444+
double theta_new=theta+k*factor;
445+
446+
double x_new=cx+r*cos(theta_new);
447+
double y_new=cy+r*sin(theta_new);
448+
449+
if (x_new>=0&&x_new<width&&y_new>=0&&y_new<height)
450+
{
451+
output[y][x]=image[(int)y_new][(int)x_new];
450452
}
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-
}
453+
else
454+
{
455+
output[y][x]=(RGBTRIPLE){0, 0, 0};
461456
}
462457
}
463458
}
464-
}
459+
for (int y=0;y<height;y++)
460+
for (int x=0; x<width; x++)
461+
image[y][x] = output[y][x];
462+
463+
free(output);
464+
}

helpers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ 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+
41+
// Spiral / Swirl filter
42+
void spiral(int height, int width, RGBTRIPLE image[height][width]);
43+
4044
// Pixelate filter
4145
void pixelate(int height, int width, RGBTRIPLE image[height][width]);
4246
#endif

out.bmp

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)