From fd91332a8bb3fb80c8764e9cc5bac6e130655d63 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 24 Nov 2003 17:50:14 +0000 Subject: [PATCH] * examples/: + Simple image viewer, cacaview, based on libcaca and imlib2. + Renamed caca-demo into cacademo. * doc/: + Renamed doc/caca-demo.1 into doc/cacademo.1. --- doc/Makefile.am | 2 +- doc/{caca-demo.1 => cacademo.1} | 0 doc/cacaview.1 | 0 examples/Makefile.am | 15 +-- examples/view.c | 181 ++++++++++++++++++++++++++++++++ 5 files changed, 191 insertions(+), 7 deletions(-) rename doc/{caca-demo.1 => cacademo.1} (100%) create mode 100644 doc/cacaview.1 create mode 100644 examples/view.c diff --git a/doc/Makefile.am b/doc/Makefile.am index 30dbacd..2553c6a 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -1,6 +1,6 @@ EXTRA_DIST = doxygen.cfg footer.html header.html $(man_MANS) -man_MANS = caca-config.1 caca-demo.1 caca-spritedit.1 +man_MANS = caca-config.1 cacademo.1 caca-spritedit.1 cacaview.1 all: stamp-doxygen stamp-latex diff --git a/doc/caca-demo.1 b/doc/cacademo.1 similarity index 100% rename from doc/caca-demo.1 rename to doc/cacademo.1 diff --git a/doc/cacaview.1 b/doc/cacaview.1 new file mode 100644 index 0000000..e69de29 diff --git a/examples/Makefile.am b/examples/Makefile.am index 0849b1f..5411d55 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -13,15 +13,18 @@ if USE_NCURSES LDFLAGS_ncurses = -lncurses endif -bin_PROGRAMS = caca-demo caca-spritedit +bin_PROGRAMS = cacademo caca-spritedit cacaview -caca_demo_SOURCES = demo.c -caca_demo_LDADD = ../src/libcaca.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm -caca_demo_CPPFLAGS = -I$(top_srcdir)/src -DDATADIR=\"$(pkgdatadir)\" -#caca_demo_CFLAGS = `pkg-config --cflags gtk+-2.0` -#caca_demo_LDFLAGS = `pkg-config --libs gtk+-2.0` +cacademo_SOURCES = demo.c +cacademo_LDADD = ../src/libcaca.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm +cacademo_CPPFLAGS = -I$(top_srcdir)/src -DDATADIR=\"$(pkgdatadir)\" caca_spritedit_SOURCES = spritedit.c caca_spritedit_LDADD = ../src/libcaca.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm caca_spritedit_CPPFLAGS = -I$(top_srcdir)/src +cacaview_SOURCES = view.c +cacaview_LDADD = ../src/libcaca.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm +cacaview_CPPFLAGS = -I$(top_srcdir)/src +cacaview_CFLAGS = `imlib2-config --cflags` +cacaview_LDFLAGS = `imlib2-config --libs` diff --git a/examples/view.c b/examples/view.c new file mode 100644 index 0000000..86ede73 --- /dev/null +++ b/examples/view.c @@ -0,0 +1,181 @@ +/* + * view image viewer for libcaca + * Copyright (c) 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 Lesser 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ + +#include "config.h" + +#include + +#define X_DISPLAY_MISSING 1 +#include + +#include "caca.h" + +Imlib_Image image = NULL; +char *pixels = NULL; +struct caca_bitmap *bitmap = NULL; + +int dithering = 0; +const enum caca_dithering dithering_list[] = + { CACA_DITHER_NONE, CACA_DITHER_ORDERED, CACA_DITHER_RANDOM }; + +int main(int argc, char **argv) +{ + int quit = 0, update = 1; + int x, y, w, h, zoom = 0; + + if(argc != 2) + { + fprintf(stderr, "usage: %s \n", argv[0]); + return 1; + } + + image = imlib_load_image(argv[1]); + + if(!image) + { + fprintf(stderr, "%s: unable to open `%s'\n", argv[0], argv[1]); + return 1; + } + + imlib_context_set_image(image); + pixels = (char *)imlib_image_get_data_for_reading_only(); + w = imlib_image_get_width(); + h = imlib_image_get_height(); + x = w / 2; + y = h / 2; + + if(caca_init()) + { + fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]); + return 1; + } + + /* Create the libcaca bitmap */ + bitmap = caca_create_bitmap(32, w, h, 4 * w, + 0x00ff0000, 0x0000ff00, 0x000000ff); + + if(!image) + { + fprintf(stderr, "%s: unable to create libcaca bitmap\n", argv[0]); + caca_end(); + return 1; + } + + /* Go ! */ + while(!quit) + { + int event; + + while((event = caca_get_event())) + { + switch(event) + { + case CACA_EVENT_KEY_PRESS | 'd': + case CACA_EVENT_KEY_PRESS | 'D': + dithering = (dithering + 1) % 3; + update = 1; + break; + case CACA_EVENT_KEY_PRESS | '+': + zoom++; + update = 1; + break; + case CACA_EVENT_KEY_PRESS | '-': + zoom--; + update = 1; + break; + case CACA_EVENT_KEY_PRESS | 'x': + case CACA_EVENT_KEY_PRESS | 'X': + zoom = 0; + update = 1; + break; + case CACA_EVENT_KEY_PRESS | CACA_KEY_UP: + if(zoom > 0) y -= h / (2 + zoom) / 8; + update = 1; + break; + case CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN: + if(zoom > 0) y += h / (2 + zoom) / 8; + update = 1; + break; + case CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT: + if(zoom > 0) x -= w / (2 + zoom) / 8; + update = 1; + break; + case CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT: + if(zoom > 0) x += w / (2 + zoom) / 8; + update = 1; + break; + case CACA_EVENT_KEY_PRESS | 'q': + case CACA_EVENT_KEY_PRESS | 'Q': + quit = 1; + break; + } + } + + if(update) + { + caca_clear(); + caca_set_dithering(dithering_list[dithering]); + if(zoom < 0) + { + int xo = (caca_get_width() - 1) / 2; + int yo = (caca_get_height() - 1) / 2; + int xn = (caca_get_width() - 1) / (2 - zoom); + int yn = (caca_get_height() - 1) / (2 - zoom); + caca_draw_bitmap(xo - xn, yo - yn, xo + xn, yo + yn, + bitmap, pixels); + } + else if(zoom > 0) + { + struct caca_bitmap *newbitmap; + int xn = w / (2 + zoom); + int yn = h / (2 + zoom); + if(x < xn) x = xn; + if(y < yn) y = yn; + if(xn + x > w) x = w - xn; + if(yn + y > h) y = h - yn; + newbitmap = caca_create_bitmap(32, 2 * xn, 2 * yn, 4 * w, + 0x00ff0000, 0x0000ff00, 0x000000ff); + caca_draw_bitmap(0, 0, caca_get_width() - 1, + caca_get_height() - 1, newbitmap, + pixels + 4 * (x - xn) + 4 * w * (y - yn)); + caca_free_bitmap(newbitmap); + } + else + { + caca_draw_bitmap(0, 0, caca_get_width() - 1, + caca_get_height() - 1, bitmap, pixels); + } + caca_refresh(); + update = 0; + } + } + + caca_free_bitmap(bitmap); + imlib_free_image(); + + /* Clean up */ + caca_end(); + + return 0; +} +