@@ -425,39 +425,111 @@ void oilpaint(int height, int width, RGBTRIPLE image[height][width]){
425425}
426426
427427
428+ // Pixelate (mosaic) filter: average color in blocks
429+ void pixelate (int height , int width , RGBTRIPLE image [height ][width ])
430+ {
431+ int block = 10 ; // block size (pixels)
432+ for (int by = 0 ; by < height ; by += block )
433+ {
434+ for (int bx = 0 ; bx < width ; bx += block )
435+ {
436+ long sumR = 0 , sumG = 0 , sumB = 0 ;
437+ int count = 0 ;
438+ for (int y = by ; y < by + block && y < height ; y ++ )
439+ {
440+ for (int x = bx ; x < bx + block && x < width ; x ++ )
441+ {
442+ sumR += image [y ][x ].rgbtRed ;
443+ sumG += image [y ][x ].rgbtGreen ;
444+ sumB += image [y ][x ].rgbtBlue ;
445+ count ++ ;
446+ }
447+ }
448+ if (count == 0 ) continue ;
449+ uint8_t avgR = (uint8_t )(sumR / count );
450+ uint8_t avgG = (uint8_t )(sumG / count );
451+ uint8_t avgB = (uint8_t )(sumB / count );
452+
453+ for (int y = by ; y < by + block && y < height ; y ++ )
454+ {
455+ for (int x = bx ; x < bx + block && x < width ; x ++ )
456+ {
457+ image [y ][x ].rgbtRed = avgR ;
458+ image [y ][x ].rgbtGreen = avgG ;
459+ image [y ][x ].rgbtBlue = avgB ;
460+ }
461+ }
462+ }
463+ }
464+ }
465+
466+
428467void spiral (int height , int width , RGBTRIPLE image [height ][width ])
429468{
430469 RGBTRIPLE (* output )[width ] = calloc (height , width * sizeof (RGBTRIPLE ));
431470 double cx = width / 2.0 ;
432471 double cy = height / 2.0 ;
433472 double max_r = sqrt (cx * cx + cy * cy );
434473 double k = 4.0 ;
435- for (int y = 0 ;y < height ;y ++ )
474+
475+ for (int y = 0 ; y < height ; y ++ )
436476 {
437- for (int x = 0 ; x < width ;x ++ )
477+ for (int x = 0 ; x < width ; x ++ )
438478 {
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 ];
452- }
453- else
454- {
455- output [y ][x ]= (RGBTRIPLE ){0 , 0 , 0 };
456- }
479+ double dx = x - cx ;
480+ double dy = y - cy ;
481+ double r = sqrt (dx * dx + dy * dy );
482+ double theta = atan2 (dy , dx );
483+ double factor = (max_r - r ) / max_r ;
484+ if (factor < 0.0 ) factor = 0.0 ;
485+ double theta_new = theta + k * factor ;
486+
487+ double x_new = cx + r * cos (theta_new );
488+ double y_new = cy + r * sin (theta_new );
489+
490+ /* Clamp coordinates to source image to avoid empty/black pixels */
491+ if (x_new < 0.0 ) x_new = 0.0 ;
492+ if (x_new > (double )(width - 1 )) x_new = (double )(width - 1 );
493+ if (y_new < 0.0 ) y_new = 0.0 ;
494+ if (y_new > (double )(height - 1 )) y_new = (double )(height - 1 );
495+
496+ /* Bilinear interpolation for smooth sampling */
497+ int x0 = (int )floor (x_new );
498+ int x1 = x0 + 1 ;
499+ if (x1 >= width ) x1 = x0 ;
500+ int y0 = (int )floor (y_new );
501+ int y1 = y0 + 1 ;
502+ if (y1 >= height ) y1 = y0 ;
503+
504+ double wx = x_new - x0 ;
505+ double wy = y_new - y0 ;
506+
507+ RGBTRIPLE p00 = image [y0 ][x0 ];
508+ RGBTRIPLE p10 = image [y0 ][x1 ];
509+ RGBTRIPLE p01 = image [y1 ][x0 ];
510+ RGBTRIPLE p11 = image [y1 ][x1 ];
511+
512+ double r_val = (1 - wx ) * (1 - wy ) * p00 .rgbtRed
513+ + wx * (1 - wy ) * p10 .rgbtRed
514+ + (1 - wx ) * wy * p01 .rgbtRed
515+ + wx * wy * p11 .rgbtRed ;
516+ double g_val = (1 - wx ) * (1 - wy ) * p00 .rgbtGreen
517+ + wx * (1 - wy ) * p10 .rgbtGreen
518+ + (1 - wx ) * wy * p01 .rgbtGreen
519+ + wx * wy * p11 .rgbtGreen ;
520+ double b_val = (1 - wx ) * (1 - wy ) * p00 .rgbtBlue
521+ + wx * (1 - wy ) * p10 .rgbtBlue
522+ + (1 - wx ) * wy * p01 .rgbtBlue
523+ + wx * wy * p11 .rgbtBlue ;
524+
525+ output [y ][x ].rgbtRed = (uint8_t )round (r_val );
526+ output [y ][x ].rgbtGreen = (uint8_t )round (g_val );
527+ output [y ][x ].rgbtBlue = (uint8_t )round (b_val );
457528 }
458529 }
459- for (int y = 0 ;y < height ;y ++ )
460- for (int x = 0 ; x < width ; x ++ )
530+
531+ for (int y = 0 ; y < height ; y ++ )
532+ for (int x = 0 ; x < width ; x ++ )
461533 image [y ][x ] = output [y ][x ];
462534
463535 free (output );
0 commit comments