From b176de43abfdcfe4e0b8a7b24575c9e1c1ea2550 Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 16 Nov 2003 00:09:09 +0000 Subject: [PATCH] * Very ugly ee_blit function. Will be polished in a while. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/ttyvaders/trunk@184 92316355-f0b4-4df1-b90c-862c8a59935f --- libee/Makefile.am | 1 + libee/blit.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++ libee/ee.h | 2 + test/Makefile.am | 2 + test/demo.c | 38 +++++++++++++++-- 5 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 libee/blit.c diff --git a/libee/Makefile.am b/libee/Makefile.am index 82b4e2a..2d2e130 100644 --- a/libee/Makefile.am +++ b/libee/Makefile.am @@ -15,5 +15,6 @@ libee_a_SOURCES = \ conic.c \ triangle.c \ sprite.c \ + blit.c \ $(NULL) diff --git a/libee/blit.c b/libee/blit.c new file mode 100644 index 0000000..7584b09 --- /dev/null +++ b/libee/blit.c @@ -0,0 +1,102 @@ +/* + * libee ASCII-Art library + * Copyright (c) 2002, 2003 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "config.h" + +#ifdef HAVE_INTTYPES_H +# include +#else +typedef unsigned char uint8_t; +#endif + +#include + +#include "ee.h" +#include "ee_internals.h" + +#include +void ee_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h) +{ + char foo[] = { ' ', '.', ':', ';', '=', '$', '%', '@', '#', '8', 'W' }; + int x, y, pitch; + + if(x1 > x2) + { + int tmp = x2; x2 = x1; x1 = tmp; + } + + if(y1 > y2) + { + int tmp = y2; y2 = y1; y1 = tmp; + } + + pitch = (3 * w + 3) / 4 * 4; + + for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= ee_get_height(); y++) + for(x = x1 > 0 ? x1 : 0; x <= x2 && x <= ee_get_width(); x++) + { + int fromx = w * (x - x1) / (x2 - x1 + 1); + int fromy = h * (y - y1) / (y2 - y1 + 1); + int r = ((unsigned char *)pixels)[3 * fromx + pitch * fromy]; + int g = ((unsigned char *)pixels)[3 * fromx + 1 + pitch * fromy]; + int b = ((unsigned char *)pixels)[3 * fromx + 2 + pitch * fromy]; + + if(r == g && g == b) + { + ee_set_color(EE_LIGHTGRAY); + } + else + { + static int foo_colors[6] = {EE_LIGHTRED, EE_YELLOW, EE_LIGHTGREEN, EE_LIGHTCYAN, EE_LIGHTBLUE, EE_LIGHTMAGENTA}; + float min = r, max = r, delta, hue, sat; + if(min > g) min = g; if(max < g) max = g; + if(min > b) min = b; if(max < b) max = b; + + delta = max - min; + + sat = max / delta; + + if(delta > 20) + { + if( r == max ) + hue = (g - b) / delta; // between yellow & magenta + else if( g == max ) + hue = 2 + (b - r) / delta; // between cyan & yellow + else + hue = 4 + (r - g) / delta; // between magenta & cyan + + hue *= 60; // degrees + if( hue < 0 ) + hue += 360; + + ee_set_color(foo_colors[(int)(hue + 30) / 60]); + } + else + { + ee_set_color(EE_LIGHTGRAY); + } + } + + ee_putchar(x, y, foo[(r + g + b) / 3 / 25]); + } +} + diff --git a/libee/ee.h b/libee/ee.h index bc6f462..a8c2211 100644 --- a/libee/ee.h +++ b/libee/ee.h @@ -107,6 +107,8 @@ int ee_get_sprite_dy(struct ee_sprite *, int); void ee_draw_sprite(int, int, struct ee_sprite *, int); void ee_free_sprite(struct ee_sprite *); +void ee_blit(int, int, int, int, void *, int, int); + #ifdef __cplusplus } #endif diff --git a/test/Makefile.am b/test/Makefile.am index 6be723f..580e646 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -15,6 +15,8 @@ bin_PROGRAMS = demo spritedit demo_SOURCES = demo.c demo_LDADD = ../libee/libee.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm +demo_CFLAGS = `pkg-config --cflags gtk+-2.0` +demo_LDFLAGS = `pkg-config --libs gtk+-2.0` spritedit_SOURCES = spritedit.c spritedit_LDADD = ../libee/libee.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm diff --git a/test/demo.c b/test/demo.c index 7123dcc..fa2a585 100644 --- a/test/demo.c +++ b/test/demo.c @@ -26,6 +26,9 @@ #include #include +#include +#include + #include "ee.h" static void display_menu(void); @@ -39,11 +42,16 @@ static void demo_boxes(void); static void demo_ellipses(void); static void demo_triangles(void); static void demo_sprites(void); +static void demo_blit(void); int bounds = 0; int outline = 0; struct ee_sprite *sprite = NULL; +GdkPixbuf *pixbuf; +char *pixels; +int bufx, bufy, bufpitch; + int main(int argc, char **argv) { void (*demo)(void) = NULL; @@ -59,6 +67,19 @@ int main(int argc, char **argv) /* Initialize data */ sprite = ee_load_sprite("data/barboss.txt"); +gdk_init (&argc, &argv); + //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gally4.jpeg", NULL); + //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/badge1.jpeg", NULL); + //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/union.png", NULL); + pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/pikachu.jpeg", NULL); +if(!pixbuf) return -2; + pixels = gdk_pixbuf_get_pixels(pixbuf); + bufx = gdk_pixbuf_get_width(pixbuf); + bufy = gdk_pixbuf_get_height(pixbuf); + bufpitch = gdk_pixbuf_get_rowstride(pixbuf); +fprintf(stderr, "bits: %i\n", gdk_pixbuf_get_bits_per_sample(pixbuf)); +fprintf(stderr, "w %i, h %i, stride %i\n", bufx, bufy, bufpitch); + /* Main menu */ display_menu(); ee_refresh(); @@ -120,6 +141,10 @@ int main(int argc, char **argv) case 'S': demo = demo_sprites; break; + case 'i': + case 'I': + demo = demo_blit; + break; } if(demo) @@ -173,11 +198,12 @@ static void display_menu(void) ee_putstr(4, 12, "'5': ellipses"); ee_putstr(4, 13, "'s': sprites"); ee_putstr(4, 14, "'c': color"); + ee_putstr(4, 15, "'i': image blit"); - ee_putstr(4, 16, "settings:"); - ee_printf(4, 17, "'o': outline: %s", + ee_putstr(4, 17, "settings:"); + ee_printf(4, 18, "'o': outline: %s", outline == 0 ? "none" : outline == 1 ? "solid" : "thin"); - ee_printf(4, 18, "'b': drawing boundaries: %s", + ee_printf(4, 19, "'b': drawing boundaries: %s", bounds == 0 ? "screen" : "infinite"); ee_putstr(4, yo - 2, "'q': quit"); @@ -428,3 +454,9 @@ static void demo_sprites(void) ee_rand(0, ee_get_height() - 1), sprite, 0); } +static void demo_blit(void) +{ +ee_set_color(EE_LIGHTGRAY); + ee_blit(6, 4, ee_get_width() - 6, ee_get_height() - 4, pixels, bufx, bufy); +} +