summaryrefslogtreecommitdiff
path: root/frontends/riscos/content-handlers/sprite.c
blob: 5cbade4e5af04028f67191f799b11dad93cae509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

/** \file
 * Content for image/x-riscos-sprite (RISC OS implementation).
 *
 * No conversion is necessary: we can render RISC OS sprites directly under
 * RISC OS.
 */

#include <string.h>
#include <stdlib.h>
#include "oslib/osspriteop.h"

#include "utils/config.h"
#include "utils/config.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "netsurf/plotters.h"
#include "netsurf/content.h"
#include "content/llcache.h"
#include "content/content_protected.h"

#include "riscos/gui.h"
#include "riscos/image.h"
#include "riscos/content-handlers/sprite.h"

#ifdef WITH_SPRITE

typedef struct sprite_content {
	struct content base;

	void *data;
} sprite_content;

static nserror sprite_create(const content_handler *handler,
		lwc_string *imime_type, const struct http_parameter *params,
		llcache_handle *llcache, const char *fallback_charset,
		bool quirks, struct content **c);
static bool sprite_convert(struct content *c);
static void sprite_destroy(struct content *c);
static bool sprite_redraw(struct content *c, struct content_redraw_data *data,
		const struct rect *clip, const struct redraw_context *ctx);
static nserror sprite_clone(const struct content *old, struct content **newc);
static content_type sprite_content_type(void);

static const content_handler sprite_content_handler = {
	.create = sprite_create,
	.data_complete = sprite_convert,
	.destroy = sprite_destroy,
	.redraw = sprite_redraw,
	.clone = sprite_clone,
	.type = sprite_content_type,
	.no_share = false,
};

static const char *sprite_types[] = {
	"image/x-riscos-sprite"
};

CONTENT_FACTORY_REGISTER_TYPES(sprite, sprite_types, sprite_content_handler)

nserror sprite_create(const content_handler *handler,
		lwc_string *imime_type, const struct http_parameter *params,
		llcache_handle *llcache, const char *fallback_charset,
		bool quirks, struct content **c)
{
	sprite_content *sprite;
	nserror error;

	sprite = calloc(1, sizeof(sprite_content));
	if (sprite == NULL)
		return NSERROR_NOMEM;

	error = content__init(&sprite->base, handler, imime_type, params,
			llcache, fallback_charset, quirks);
	if (error != NSERROR_OK) {
		free(sprite);
		return error;
	}

	*c = (struct content *) sprite;

	return NSERROR_OK;
}

/**
 * Convert a CONTENT_SPRITE for display.
 *
 * No conversion is necessary. We merely read the sprite dimensions.
 */

bool sprite_convert(struct content *c)
{
	sprite_content *sprite = (sprite_content *) c;
	os_error *error;
	int w, h;
	union content_msg_data msg_data;
	const char *source_data;
	unsigned long source_size;
	const void *sprite_data;
	char *title;

	source_data = content__get_source_data(c, &source_size);

	sprite_data = source_data - 4;
	osspriteop_area *area = (osspriteop_area*) sprite_data;
	sprite->data = area;

	/* check for bad data */
	if ((int)source_size + 4 != area->used) {
		msg_data.error = messages_get("BadSprite");
		content_broadcast(c, CONTENT_MSG_ERROR, &msg_data);
		return false;
	}

	error = xosspriteop_read_sprite_info(osspriteop_PTR,
			(osspriteop_area *)0x100,
			(osspriteop_id) ((char *) area + area->first),
			&w, &h, NULL, NULL);
	if (error) {
		LOG("xosspriteop_read_sprite_info: 0x%x: %s", error->errnum, error->errmess);
		msg_data.error = error->errmess;
		content_broadcast(c, CONTENT_MSG_ERROR, &msg_data);
		return false;
	}

	c->width = w;
	c->height = h;

	/* set title text */
	title = messages_get_buff("SpriteTitle",
			nsurl_access_leaf(llcache_handle_get_url(c->llcache)),
			c->width, c->height);
	if (title != NULL) {
		content__set_title(c, title);
		free(title);
	}
	content_set_ready(c);
	content_set_done(c);
	/* Done: update status bar */
	content_set_status(c, "");
	return true;
}


/**
 * Destroy a CONTENT_SPRITE and free all resources it owns.
 */

void sprite_destroy(struct content *c)
{
	/* do not free c->data.sprite.data at it is simply a pointer to
	 * 4 bytes beforec->source_data. */
}


/**
 * Redraw a CONTENT_SPRITE.
 */

bool sprite_redraw(struct content *c, struct content_redraw_data *data,
		const struct rect *clip, const struct redraw_context *ctx)
{
	sprite_content *sprite = (sprite_content *) c;

	if (ctx->plot->flush && (ctx->plot->flush(ctx) != NSERROR_OK))
		return false;

	return image_redraw(sprite->data,
			ro_plot_origin_x + data->x * 2,
			ro_plot_origin_y - data->y * 2,
			data->width, data->height,
			c->width,
			c->height,
			data->background_colour,
			false, false, false,
			IMAGE_PLOT_OS);
}

nserror sprite_clone(const struct content *old, struct content **newc)
{
	sprite_content *sprite;
	nserror error;

	sprite = calloc(1, sizeof(sprite_content));
	if (sprite == NULL)
		return NSERROR_NOMEM;

	error = content__clone(old, &sprite->base);
	if (error != NSERROR_OK) {
		content_destroy(&sprite->base);
		return error;
	}

	/* Simply rerun convert */
	if (old->status == CONTENT_STATUS_READY ||
			old->status == CONTENT_STATUS_DONE) {
		if (sprite_convert(&sprite->base) == false) {
			content_destroy(&sprite->base);
			return NSERROR_CLONE_FAILED;
		}
	}

	*newc = (struct content *) sprite;

	return NSERROR_OK;
}

content_type sprite_content_type(void)
{
	return CONTENT_IMAGE;
}

#endif


/**
 * Returns the bit depth of a sprite
 *
 * \param   s   sprite
 * \return  depth in bpp
 */

byte sprite_bpp(const osspriteop_header *s)
{
	/* bit 31 indicates the presence of a full alpha channel 
	 * rather than a binary mask */
	int type = ((unsigned)s->mode >> osspriteop_TYPE_SHIFT) & 15;
	byte bpp = 0;

	switch (type) {
		case osspriteop_TYPE_OLD:
		{
			bits psr;
			int val;
			if (!xos_read_mode_variable(s->mode, 
					os_MODEVAR_LOG2_BPP, &val, &psr) &&
					!(psr & _C))
				bpp = 1 << val;
		}
		break;
		case osspriteop_TYPE1BPP:  bpp = 1; break;
		case osspriteop_TYPE2BPP:  bpp = 2; break;
		case osspriteop_TYPE4BPP:  bpp = 4; break;
		case osspriteop_TYPE8BPP:  bpp = 8; break;
		case osspriteop_TYPE16BPP: bpp = 16; break;
		case osspriteop_TYPE32BPP: bpp = 32; break;
		case osspriteop_TYPE_CMYK: bpp = 32; break;
	}
	return bpp;
}