Skip to content

Commit b03c48b

Browse files
issues fixed
1 parent 5927a37 commit b03c48b

File tree

5 files changed

+40
-120
lines changed

5 files changed

+40
-120
lines changed

filter.c

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

1212

1313

14-
15-
char filterArr[argc - 3];
16-
int filterCount = 0;
17-
18-
while ((opt = getopt(argc, argv, filters)) != -1)
19-
{
20-
if (opt == '?')
21-
{
22-
printf("Invalid filter option\n");
23-
return 2;
24-
}
25-
filterArr[filterCount++] = (char)opt;
14+
char filterArr[argc-3];
15+
// gets all filter flags and checks validity
16+
for(int i=0; i<argc; i++){
17+
char temp = getopt(argc,argv,filters);
18+
if(temp == -1) break;
19+
filterArr[i]= temp;
20+
if(filterArr[i] == '?') {
21+
printf("Invalid filter option");
22+
return 1;
23+
}
2624
}
27-
2825

2926

3027

@@ -130,15 +127,13 @@ while ((opt = getopt(argc, argv, filters)) != -1)
130127
threshold(height, width, image);
131128
break;
132129

133-
case 'd': // Edge Detection
134-
detect_edges(height, width, image);
135-
break;
130+
136131

137132

138133

139-
case 'p':
140-
apply_pixelate(height, width, image, 5); // default block size = 5
141-
break;
134+
case 'p':
135+
apply_pixelate(height, width, image, 5);
136+
break;
142137

143138
default:
144139
printf("Unknown filter: %c\n", filterArr[i]);

filter.exe

53.1 KB
Binary file not shown.

helpers.c

Lines changed: 25 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -202,129 +202,56 @@ void threshold(int height, int width, RGBTRIPLE image[height][width])
202202
}
203203

204204

205-
// ----------------------
206-
// EDGE DETECTION (SOBEL)
207-
// ----------------------
208-
void detect_edges(int height, int width, RGBTRIPLE image[height][width])
205+
206+
// Pixelate filter — separate function, not nested
207+
void apply_pixelate(int height, int width, RGBTRIPLE image[height][width], int blockSize)
209208
{
210-
// Temporary copy of the image
209+
211210
RGBTRIPLE **copy = malloc(height * sizeof(RGBTRIPLE *));
212211
for (int i = 0; i < height; i++)
213212
copy[i] = malloc(width * sizeof(RGBTRIPLE));
214213

215-
for (int i = 0; i < height; i++)
216-
for (int j = 0; j < width; j++)
217-
copy[i][j] = image[i][j];
218-
219-
// Sobel kernels
220-
int Gx[3][3] = {
221-
{-1, 0, 1},
222-
{-2, 0, 2},
223-
{-1, 0, 1}
224-
};
225-
int Gy[3][3] = {
226-
{-1, -2, -1},
227-
{ 0, 0, 0},
228-
{ 1, 2, 1}
229-
};
230-
231-
for (int i = 0; i < height; i++)
232-
{
233-
for (int j = 0; j < width; j++)
234-
{
235-
int sumRx = 0, sumGx = 0, sumBx = 0;
236-
int sumRy = 0, sumGy = 0, sumBy = 0;
237-
238-
for (int di = -1; di <= 1; di++)
239-
{
240-
for (int dj = -1; dj <= 1; dj++)
241-
{
242-
int ni = i + di;
243-
int nj = j + dj;
244-
245-
if (ni >= 0 && ni < height && nj >= 0 && nj < width)
246-
{
247-
RGBTRIPLE pixel = copy[ni][nj];
248-
int kx = Gx[di + 1][dj + 1];
249-
int ky = Gy[di + 1][dj + 1];
250-
251-
sumRx += pixel.rgbtRed * kx;
252-
sumGx += pixel.rgbtGreen * kx;
253-
sumBx += pixel.rgbtBlue * kx;
254-
255-
sumRy += pixel.rgbtRed * ky;
256-
sumGy += pixel.rgbtGreen * ky;
257-
sumBy += pixel.rgbtBlue * ky;
258-
}
259-
}
260-
}
261-
262-
// Calculate gradient magnitude and clamp
263-
int red = min(max((int)round(sqrt(sumRx*sumRx + sumRy*sumRy)), 0), 255);
264-
int green = min(max((int)round(sqrt(sumGx*sumGx + sumGy*sumGy)), 0), 255);
265-
int blue = min(max((int)round(sqrt(sumBx*sumBx + sumBy*sumBy)), 0), 255);
266-
267-
image[i][j].rgbtRed = red;
268-
image[i][j].rgbtGreen = green;
269-
image[i][j].rgbtBlue = blue;
270-
}
271-
}
272-
273-
274-
275-
// Pixelate Filter
276-
void apply_pixelate(int height, int width, RGBTRIPLE image[height][width], int blockSize)
277-
{
278214
for (int i = 0; i < height; i += blockSize)
279215
{
280216
for (int j = 0; j < width; j += blockSize)
281217
{
282218
int sumRed = 0, sumGreen = 0, sumBlue = 0;
283219
int count = 0;
284220

285-
// Compute average color in the block
286-
for (int bi = 0; bi < blockSize; bi++)
221+
for (int bi = 0; bi < blockSize && (i + bi) < height; bi++)
287222
{
288-
for (int bj = 0; bj < blockSize; bj++)
223+
for (int bj = 0; bj < blockSize && (j + bj) < width; bj++)
289224
{
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-
}
225+
sumRed += image[i + bi][j + bj].rgbtRed;
226+
sumGreen += image[i + bi][j + bj].rgbtGreen;
227+
sumBlue += image[i + bi][j + bj].rgbtBlue;
228+
count++;
299229
}
300230
}
301231

302-
int avgRed = sumRed / count;
303-
int avgGreen = sumGreen / count;
304-
int avgBlue = sumBlue / count;
232+
unsigned char avgRed = sumRed / count;
233+
unsigned char avgGreen = sumGreen / count;
234+
unsigned char avgBlue = sumBlue / count;
305235

306-
// Set all pixels in the block to the average color
307-
for (int bi = 0; bi < blockSize; bi++)
236+
for (int bi = 0; bi < blockSize && (i + bi) < height; bi++)
308237
{
309-
for (int bj = 0; bj < blockSize; bj++)
238+
for (int bj = 0; bj < blockSize && (j + bj) < width; bj++)
310239
{
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-
}
240+
image[i + bi][j + bj].rgbtRed = avgRed;
241+
image[i + bi][j + bj].rgbtGreen = avgGreen;
242+
image[i + bi][j + bj].rgbtBlue = avgBlue;
319243
}
320244
}
321245
}
246+
247+
322248
}
323-
}
324249

325250

326-
// Free temporary array
327251
for (int i = 0; i < height; i++)
328-
free(copy[i]);
329-
free(copy);
252+
free(copy[i]);
253+
free(copy);
330254
}
255+
256+
// Free temporary array
257+

helpers.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ void vignette(int height, int width, RGBTRIPLE image[height][width]);
2626
//Threshold Filter(Black & White)
2727
void threshold(int height, int width, RGBTRIPLE image[height][width]);
2828

29-
// **New: Edge Detection filter**
30-
void detect_edges(int height, int width, RGBTRIPLE image[height][width]);
3129

3230
// Pixelate filter
3331
void apply_pixelate(int height, int width, RGBTRIPLE image[height][width], int blockSize);

output.bmp

68.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)