From 663afc6460f00cc9db966688a0b3e5568d5f8441 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 22 Sep 2006 00:27:13 +0000 Subject: [PATCH] * Autocrop toilet output. --- test/toilet.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/test/toilet.c b/test/toilet.c index 0591db1..8922b39 100644 --- a/test/toilet.c +++ b/test/toilet.c @@ -34,7 +34,8 @@ static cucul_canvas_t *cuculize_big(uint32_t const *, unsigned int); static cucul_canvas_t *cuculize_tiny(uint32_t const *, unsigned int); /* Canvas special effects */ -static void make_gay(cucul_canvas_t *); +static void filter_autocrop(cucul_canvas_t *); +static void filter_gay(cucul_canvas_t *); int main(int argc, char *argv[]) { @@ -148,8 +149,9 @@ int main(int argc, char *argv[]) /* Do gay stuff with our string (léopard) */ cv = cuculize_big(string, length); + filter_autocrop(cv); if(flag_gay) - make_gay(cv); + filter_gay(cv); /* Output char */ buffer = cucul_export_canvas(cv, export); @@ -229,7 +231,38 @@ static cucul_canvas_t *cuculize_tiny(uint32_t const *string, return cv; } -static void make_gay(cucul_canvas_t *cv) +static void filter_autocrop(cucul_canvas_t *cv) +{ + unsigned int x, y, w, h; + unsigned int xmin, xmax, ymin, ymax; + + xmin = w = cucul_get_canvas_width(cv); + xmax = 0; + ymin = h = cucul_get_canvas_height(cv); + ymax = 0; + + for(y = 0; y < h; y++) + for(x = 0; x < w; x++) + { + unsigned long int ch = cucul_getchar(cv, x, y); + if(ch != (unsigned char)' ') + { + if(x < xmin) + xmin = x; + if(x > xmax) + xmax = x; + if(y < ymin) + ymin = y; + if(y > ymax) + ymax = y; + } + } + + cucul_set_canvas_boundaries(cv, xmin, ymin, + xmax - xmin + 1, ymax - ymin + 1); +} + +static void filter_gay(cucul_canvas_t *cv) { static unsigned char const rainbow[] = {