NetSurf
sprite.c
Go to the documentation of this file.
1/*
2 * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * Content for image/x-riscos-sprite (RISC OS implementation).
22 *
23 * No conversion is necessary: we can render RISC OS sprites directly under
24 * RISC OS.
25 */
26
27#include <string.h>
28#include <stdlib.h>
29#include "oslib/osspriteop.h"
30
31#include "utils/config.h"
32#include "utils/config.h"
33#include "utils/log.h"
34#include "utils/messages.h"
35#include "utils/utils.h"
36#include "netsurf/plotters.h"
37#include "netsurf/content.h"
38#include "content/llcache.h"
39#include "content/content.h"
42
43#include "riscos/gui.h"
44#include "riscos/image.h"
46
47#ifdef WITH_SPRITE
48
49typedef struct sprite_content {
50 struct content base;
51
52 void *data;
53} sprite_content;
54
55static nserror sprite_create(const content_handler *handler,
56 lwc_string *imime_type, const struct http_parameter *params,
58 bool quirks, struct content **c);
59static bool sprite_convert(struct content *c);
60static void sprite_destroy(struct content *c);
61static bool sprite_redraw(struct content *c, struct content_redraw_data *data,
62 const struct rect *clip, const struct redraw_context *ctx);
63static nserror sprite_clone(const struct content *old, struct content **newc);
64static content_type sprite_content_type(void);
65
66static const content_handler sprite_content_handler = {
67 .create = sprite_create,
68 .data_complete = sprite_convert,
69 .destroy = sprite_destroy,
70 .redraw = sprite_redraw,
71 .clone = sprite_clone,
72 .type = sprite_content_type,
73 .no_share = false,
74};
75
76static const char *sprite_types[] = {
77 "image/x-riscos-sprite"
78};
79
80CONTENT_FACTORY_REGISTER_TYPES(sprite, sprite_types, sprite_content_handler)
81
82nserror sprite_create(const content_handler *handler,
83 lwc_string *imime_type, const struct http_parameter *params,
85 bool quirks, struct content **c)
86{
87 sprite_content *sprite;
88 nserror error;
89
90 sprite = calloc(1, sizeof(sprite_content));
91 if (sprite == NULL)
92 return NSERROR_NOMEM;
93
94 error = content__init(&sprite->base, handler, imime_type, params,
96 if (error != NSERROR_OK) {
97 free(sprite);
98 return error;
99 }
100
101 *c = (struct content *) sprite;
102
103 return NSERROR_OK;
104}
105
106/**
107 * Convert a CONTENT_SPRITE for display.
108 *
109 * No conversion is necessary. We merely read the sprite dimensions.
110 */
111
112bool sprite_convert(struct content *c)
113{
114 sprite_content *sprite = (sprite_content *) c;
115 os_error *error;
116 int w, h;
117 union content_msg_data msg_data;
118 const uint8_t *source_data;
119 size_t source_size;
120 const void *sprite_data;
121 char *title;
122
123 source_data = content__get_source_data(c, &source_size);
124
125 sprite_data = source_data - 4;
126 osspriteop_area *area = (osspriteop_area*) sprite_data;
127 sprite->data = area;
128
129 /* check for bad data */
130 if ((int)source_size + 4 != area->used) {
131 msg_data.errordata.errorcode = NSERROR_UNKNOWN;
132 msg_data.errordata.errormsg = messages_get("BadSprite");
134 return false;
135 }
136
137 error = xosspriteop_read_sprite_info(osspriteop_PTR,
138 (osspriteop_area *)0x100,
139 (osspriteop_id) ((char *) area + area->first),
140 &w, &h, NULL, NULL);
141 if (error) {
142 NSLOG(netsurf, INFO,
143 "xosspriteop_read_sprite_info: 0x%x: %s",
144 error->errnum,
145 error->errmess);
146 msg_data.errordata.errorcode = NSERROR_UNKNOWN;
147 msg_data.errordata.errormsg = error->errmess;
149 return false;
150 }
151
152 c->width = w;
153 c->height = h;
154
155 /* set title text */
156 title = messages_get_buff("SpriteTitle",
158 c->width, c->height);
159 if (title != NULL) {
161 free(title);
162 }
165 /* Done: update status bar */
166 content_set_status(c, "");
167 return true;
168}
169
170
171/**
172 * Destroy a CONTENT_SPRITE and free all resources it owns.
173 */
174
175void sprite_destroy(struct content *c)
176{
177 /* do not free c->data.sprite.data at it is simply a pointer to
178 * 4 bytes beforec->source_data. */
179}
180
181
182/**
183 * Redraw a CONTENT_SPRITE.
184 */
185
186bool sprite_redraw(struct content *c, struct content_redraw_data *data,
187 const struct rect *clip, const struct redraw_context *ctx)
188{
189 sprite_content *sprite = (sprite_content *) c;
190
191 if (ctx->plot->flush && (ctx->plot->flush(ctx) != NSERROR_OK))
192 return false;
193
194 return image_redraw(sprite->data,
195 ro_plot_origin_x + data->x * 2,
196 ro_plot_origin_y - data->y * 2,
197 data->width, data->height,
198 c->width,
199 c->height,
200 data->background_colour,
201 false, false, false,
203}
204
205nserror sprite_clone(const struct content *old, struct content **newc)
206{
207 sprite_content *sprite;
208 nserror error;
209
210 sprite = calloc(1, sizeof(sprite_content));
211 if (sprite == NULL)
212 return NSERROR_NOMEM;
213
214 error = content__clone(old, &sprite->base);
215 if (error != NSERROR_OK) {
216 content_destroy(&sprite->base);
217 return error;
218 }
219
220 /* Simply rerun convert */
221 if (old->status == CONTENT_STATUS_READY ||
222 old->status == CONTENT_STATUS_DONE) {
223 if (sprite_convert(&sprite->base) == false) {
224 content_destroy(&sprite->base);
226 }
227 }
228
229 *newc = (struct content *) sprite;
230
231 return NSERROR_OK;
232}
233
234content_type sprite_content_type(void)
235{
236 return CONTENT_IMAGE;
237}
238
239#endif
240
241
242/**
243 * Returns the bit depth of a sprite
244 *
245 * \param s sprite
246 * \return depth in bpp
247 */
248
249byte sprite_bpp(const osspriteop_header *s)
250{
251 /* bit 31 indicates the presence of a full alpha channel
252 * rather than a binary mask */
253 int type = ((unsigned)s->mode >> osspriteop_TYPE_SHIFT) & 15;
254 byte bpp = 0;
255
256 switch (type) {
257 case osspriteop_TYPE_OLD:
258 {
259 bits psr;
260 int val;
261 if (!xos_read_mode_variable(s->mode,
262 os_MODEVAR_LOG2_BPP, &val, &psr) &&
263 !(psr & _C))
264 bpp = 1 << val;
265 }
266 break;
267 case osspriteop_TYPE1BPP: bpp = 1; break;
268 case osspriteop_TYPE2BPP: bpp = 2; break;
269 case osspriteop_TYPE4BPP: bpp = 4; break;
270 case osspriteop_TYPE8BPP: bpp = 8; break;
271 case osspriteop_TYPE16BPP: bpp = 16; break;
272 case osspriteop_TYPE32BPP: bpp = 32; break;
273 case osspriteop_TYPE_CMYK: bpp = 32; break;
274 }
275 return bpp;
276}
Content handling interface.
void content_destroy(struct content *c)
Destroy and free a content.
Definition: content.c:354
void content_broadcast(struct content *c, content_msg msg, const union content_msg_data *data)
Send a message to all users.
Definition: content.c:752
void content_set_done(struct content *c)
Put a content in status CONTENT_STATUS_DONE.
Definition: content.c:299
bool content__set_title(struct content *c, const char *title)
Set title associated with content.
Definition: content.c:1090
nserror content__init(struct content *c, const content_handler *handler, lwc_string *imime_type, const struct http_parameter *params, llcache_handle *llcache, const char *fallback_charset, bool quirks)
Definition: content.c:190
const uint8_t * content__get_source_data(struct content *c, size_t *size)
Retrieve source of content.
Definition: content.c:1216
nserror content__clone(const struct content *c, struct content *nc)
Clone a content's data members.
Definition: content.c:1382
void content_set_ready(struct content *c)
Put a content in status CONTENT_STATUS_READY and unlock the content.
Definition: content.c:285
void content_set_status(struct content *c, const char *status_message)
Updates content with new status.
Definition: content.c:270
#define CONTENT_FACTORY_REGISTER_TYPES(HNAME, HTYPELIST, HHANDLER)
Protected interface to Content handling.
@ CONTENT_STATUS_READY
Some parts of content still being loaded, but can be displayed.
Definition: content_type.h:92
@ CONTENT_STATUS_DONE
Content has completed all processing.
Definition: content_type.h:95
content_type
The type of a content.
Definition: content_type.h:53
@ CONTENT_IMAGE
All images.
Definition: content_type.h:67
@ CONTENT_MSG_ERROR
error occurred
Definition: content_type.h:122
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_UNKNOWN
Unknown error - DO NOT USE.
Definition: errors.h:31
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_CLONE_FAILED
Failed to clone handle.
Definition: errors.h:37
@ NSERROR_OK
No error.
Definition: errors.h:30
const char * type
Definition: filetype.cpp:44
bool image_redraw(osspriteop_area *area, int x, int y, int req_width, int req_height, int width, int height, colour background_colour, bool repeatx, bool repeaty, bool background, image_type type)
Plot an image at the given coordinates using the method specified.
Definition: image.c:209
@ IMAGE_PLOT_OS
Definition: image.h:31
Public content interface.
Target independent plotting interface.
static struct llcache_s * llcache
low level cache state
Definition: llcache.c:267
nsurl * llcache_handle_get_url(const llcache_handle *handle)
Retrieve the post-redirect URL of a low-level cache object.
Definition: llcache.c:4195
Low-level resource cache (interface)
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:241
char * messages_get_buff(const char *key,...)
Formatted message from a key in the global message hash.
Definition: messages.c:205
Localised message support (interface).
const char * nsurl_access_leaf(const nsurl *url)
Access a URL's path leaf as a string.
@ base
Definition: punycode.c:19
int ro_plot_origin_x
Definition: plotters.c:40
int ro_plot_origin_y
Definition: plotters.c:41
byte sprite_bpp(const osspriteop_header *s)
Returns the bit depth of a sprite.
Definition: sprite.c:249
Content for image/x-riscos-sprite (RISC OS interface).
Interface to utility string handling.
Content operation function table.
nserror(* create)(const struct content_handler *handler, lwc_string *imime_type, const struct http_parameter *params, struct llcache_handle *llcache, const char *fallback_charset, bool quirks, struct content **c)
parameters to content redraw
Definition: content.h:40
int height
vertical dimension
Definition: content.h:48
int y
coordinate for top-left of redraw
Definition: content.h:42
int x
coordinate for top-left of redraw
Definition: content.h:41
colour background_colour
The background colour.
Definition: content.h:51
int width
dimensions to render content at (for scaling contents with intrinsic dimensions)
Definition: content.h:47
Content which corresponds to a single URL.
bool quirks
Content is in quirks mode.
int height
Height dimension, if applicable.
char * fallback_charset
Fallback charset, or NULL.
int width
Width dimension, if applicable.
const struct content_handler * handler
Handler for content.
struct llcache_handle * llcache
Low-level cache object.
content_status status
Current status.
Representation of an HTTP parameter.
Definition: parameter.c:31
Handle to low-level cache object.
Definition: llcache.c:76
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
Extra data for some content_msg messages.
Definition: content.h:60
const char * title
Definition: content.h:186
void * ctx
context passed to browser_window_search()
Definition: content.h:277
Interface to a number of general purpose functionality.
static nserror clip(const struct redraw_context *ctx, const struct rect *clip)
Sets a clip rectangle for subsequent plot operations.
Definition: plot.c:357