From 9402e110e904b4644ebc467e7f8e78ba4e91333c Mon Sep 17 00:00:00 2001 From: James Shaw Date: Sat, 29 Mar 2008 13:30:04 +0000 Subject: Implement sprite support for GTK using librosprite svn path=/trunk/netsurf/; revision=4051 --- Makefile | 5 +- Makefile.sources | 2 +- content/content.c | 11 ++++- content/content.h | 6 +++ content/content_type.h | 2 +- image/nssprite.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ image/nssprite.h | 41 ++++++++++++++++ render/box_construct.c | 2 +- utils/config.h | 5 ++ 9 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 image/nssprite.c create mode 100644 image/nssprite.h diff --git a/Makefile b/Makefile index ef46cb8c7..ceffcbae7 100644 --- a/Makefile +++ b/Makefile @@ -112,11 +112,12 @@ GTKCFLAGS := -std=c99 -Dgtk -Dnsgtk \ -D_XOPEN_SOURCE=600 \ -D_POSIX_C_SOURCE=200112L \ -D_NETBSD_SOURCE \ - $(WARNFLAGS) -I. -g -O \ + $(WARNFLAGS) -I. -I../../libsprite/trunk/ -g -O \ $(shell $(PKG_CONFIG) --cflags libglade-2.0 gtk+-2.0 librsvg-2.0) \ + $(shell $(PKG_CONFIG) --cflags librosprite) \ $(shell xml2-config --cflags) -GTKLDFLAGS := $(shell $(PKG_CONFIG) --cflags --libs libglade-2.0 gtk+-2.0 gthread-2.0 gmodule-2.0 librsvg-2.0) +GTKLDFLAGS := $(shell $(PKG_CONFIG) --cflags --libs libglade-2.0 gtk+-2.0 gthread-2.0 gmodule-2.0 librsvg-2.0 librosprite) CFLAGS += $(GTKCFLAGS) LDFLAGS += $(GTKLDFLAGS) $(shell $(PKG_CONFIG) --libs lcms) diff --git a/Makefile.sources b/Makefile.sources index bc780f7e6..ff23ce64f 100644 --- a/Makefile.sources +++ b/Makefile.sources @@ -23,7 +23,7 @@ S_COMMON := $(addprefix content/,$(S_CONTENT)) \ $(addprefix desktop/,$(S_DESKTOP)) S_IMAGE := bmp.c bmpread.c gif.c gifread.c ico.c jpeg.c \ - mng.c svg.c rsvg.c + mng.c nssprite.c svg.c rsvg.c # S_IMAGE are sources related to image management S_IMAGE := $(addprefix image/,$(S_IMAGE)) diff --git a/content/content.c b/content/content.c index 19bb45b0a..6d8ed22e7 100644 --- a/content/content.c +++ b/content/content.c @@ -63,6 +63,9 @@ #ifdef WITH_SPRITE #include "riscos/sprite.h" #endif +#ifdef WITH_NSSPRITE +#include "image/nssprite.h" +#endif #ifdef WITH_DRAW #include "riscos/draw.h" #endif @@ -168,7 +171,7 @@ static const struct mime_entry mime_map[] = { #ifdef WITH_BMP {"image/x-ms-bmp", CONTENT_BMP}, #endif -#ifdef WITH_SPRITE +#if defined(WITH_SPRITE) || defined(WITH_NSSPRITE) {"image/x-riscos-sprite", CONTENT_SPRITE}, #endif #ifdef WITH_BMP @@ -205,7 +208,7 @@ const char *content_type_name[] = { "JNG", "MNG", #endif -#ifdef WITH_SPRITE +#if defined(WITH_SPRITE) || defined(WITH_NSSPRITE) "SPRITE", #endif #ifdef WITH_DRAW @@ -301,6 +304,10 @@ static const struct handler_entry handler_map[] = { {0, 0, sprite_convert, 0, sprite_destroy, 0, sprite_redraw, 0, 0, 0, false}, #endif +#ifdef WITH_NSSPRITE + {0, 0, nssprite_convert, + 0, nssprite_destroy, 0, nssprite_redraw, 0, 0, 0, false}, +#endif #ifdef WITH_DRAW {0, 0, draw_convert, 0, draw_destroy, 0, draw_redraw, 0, 0, 0, false}, diff --git a/content/content.h b/content/content.h index 4f2fa0ed6..5e130e138 100644 --- a/content/content.h +++ b/content/content.h @@ -51,6 +51,9 @@ #ifdef WITH_SPRITE #include "riscos/sprite.h" #endif +#ifdef WITH_NSSPRITE +#include "image/nssprite.h" +#endif #ifdef WITH_DRAW #include "riscos/draw.h" #endif @@ -173,6 +176,9 @@ struct content { #ifdef WITH_SPRITE struct content_sprite_data sprite; #endif +#ifdef WITH_NSSPRITE + struct content_nssprite_data nssprite; +#endif #ifdef WITH_DRAW struct content_draw_data draw; #endif diff --git a/content/content_type.h b/content/content_type.h index 51d25858f..a7c59e85a 100644 --- a/content/content_type.h +++ b/content/content_type.h @@ -48,7 +48,7 @@ typedef enum { CONTENT_JNG, CONTENT_MNG, #endif -#ifdef WITH_SPRITE +#if defined(WITH_SPRITE) || defined(WITH_NSSPRITE) CONTENT_SPRITE, #endif #ifdef WITH_DRAW diff --git a/image/nssprite.c b/image/nssprite.c new file mode 100644 index 000000000..e482336d0 --- /dev/null +++ b/image/nssprite.c @@ -0,0 +1,125 @@ + /* + * Copyright 2008 James Shaw + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf 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; version 2 of the License. + * + * NetSurf 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, see . + */ + +/** \file + * Content for image/x-riscos-sprite (librosprite implementation). + * + */ + +#include +#include +#include +#include +#include "utils/config.h" +#include "desktop/plotters.h" +#include "content/content.h" +#include "utils/log.h" +#include "utils/messages.h" +#include "utils/utils.h" + +#ifdef WITH_NSSPRITE + +#define ERRCHK(x) do { \ + rosprite_error err = x; \ + if (err == ROSPRITE_EOF) { \ + LOG(("Got ROSPRITE_EOF when loading sprite file")); \ + return false; \ + } else if (err == ROSPRITE_BADMODE) { \ + LOG(("Got ROSPRITE_BADMODE when loading sprite file")); \ + return false; \ + } else if (err == ROSPRITE_OK) { \ + } else { \ + return false; \ + } \ +} while(0) + +/** + * Convert a CONTENT_SPRITE for display. + * + * No conversion is necessary. We merely read the sprite dimensions. + */ + +bool nssprite_convert(struct content *c, int width, int height) +{ + struct rosprite_mem_context* ctx; + ERRCHK(rosprite_create_mem_context(c->source_data, c->total_size, &ctx)); + + struct rosprite_area* sprite_area; + ERRCHK(rosprite_load(rosprite_mem_reader, ctx, &sprite_area)); + rosprite_destroy_mem_context(ctx); + c->data.nssprite.sprite_area = sprite_area; + + assert(sprite_area->sprite_count > 0); + + struct rosprite* sprite = sprite_area->sprites[0]; + + c->bitmap = bitmap_create(sprite->width, sprite->height, BITMAP_NEW); + if (!c->bitmap) { + return false; + } + char* imagebuf = bitmap_get_buffer(c->bitmap); + unsigned int row_width = bitmap_get_rowstride(c->bitmap); + + memcpy(imagebuf, sprite->image, row_width * sprite->height); // TODO: avoid copying entire image buffer + + /* reverse byte order of each word */ + for (uint32_t y = 0; y < sprite->height; y++) { + for (uint32_t x = 0; x < sprite->width; x++) { + int offset = 4 * (y * sprite->width + x); + uint32_t r = imagebuf[offset+3]; + uint32_t g = imagebuf[offset+2]; + uint32_t b = imagebuf[offset+1]; + uint32_t a = imagebuf[offset]; + imagebuf[offset] = r; + imagebuf[offset+1] = g; + imagebuf[offset+2] = b; + imagebuf[offset+3] = a; + } + } + + c->width = sprite->width; + c->height = sprite->height; + c->status = CONTENT_STATUS_DONE; + return true; +} + + +/** + * Destroy a CONTENT_SPRITE and free all resources it owns. + */ + +void nssprite_destroy(struct content *c) +{ + rosprite_destroy_sprite_area(c->data.nssprite.sprite_area); +} + + +/** + * Redraw a CONTENT_SPRITE. + */ + +bool nssprite_redraw(struct content *c, int x, int y, + int width, int height, + int clip_x0, int clip_y0, int clip_x1, int clip_y1, + float scale, colour background_colour) +{ + return plot.bitmap(x, y, width, height, + c->bitmap, background_colour); +} + +#endif diff --git a/image/nssprite.h b/image/nssprite.h new file mode 100644 index 000000000..06e0a366e --- /dev/null +++ b/image/nssprite.h @@ -0,0 +1,41 @@ +/* + * Copyright 2008 James Shaw + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf 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; version 2 of the License. + * + * NetSurf 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, see . + */ + +/** \file + * Content for image/x-riscos-sprite (librosprite interface). + */ + +#ifndef _NETSURF_NS_SPRITE_H_ +#define _NETSURF_NS_SPRITE_H_ + +#include + +struct content; + +struct content_nssprite_data { + struct rosprite_area* sprite_area; +}; + +bool nssprite_convert(struct content *c, int width, int height); +void nssprite_destroy(struct content *c); +bool nssprite_redraw(struct content *c, int x, int y, + int width, int height, + int clip_x0, int clip_y0, int clip_x1, int clip_y1, + float scale, colour background_colour); + +#endif diff --git a/render/box_construct.c b/render/box_construct.c index 58eff2878..08d5999af 100644 --- a/render/box_construct.c +++ b/render/box_construct.c @@ -66,7 +66,7 @@ static const content_type image_types[] = { CONTENT_JNG, CONTENT_MNG, #endif -#ifdef WITH_SPRITE +#if defined(WITH_SPRITE) || defined(WITH_NSSPRITE) CONTENT_SPRITE, #endif #ifdef WITH_DRAW diff --git a/utils/config.h b/utils/config.h index 9200fe73f..ce23fc0aa 100644 --- a/utils/config.h +++ b/utils/config.h @@ -73,6 +73,7 @@ char *strndup(const char *s, size_t n); #else /* We're likely to have a working mmap() */ #define WITH_MMAP + #define WITH_NSSPRITE #if !defined(DEBUG_BUILD) /* Use librsvg and Cairo for rendering SVG */ #define WITH_RSVG @@ -83,6 +84,10 @@ char *strndup(const char *s, size_t n); #error Cannot build WITH_NS_SVG and WITH_RSVG both enabled #endif +#if defined(WITH_NSSPRITE) && defined(WITH_SPRITE) + #error Cannot build WITH_NSSPRITE and WITH_SPRITE both enabled +#endif + #if defined(riscos) || defined(DEBUG_BUILD) /* Export modules */ #define WITH_SAVE_COMPLETE -- cgit v1.2.3