@@ -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+
0 commit comments