summaryrefslogtreecommitdiff
path: root/amiga/dt_anim.c
blob: f5e20e958a923e99adeb0280639f3b2f60d7a8fc (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * Copyright 2011 Chris Young <chris@unsatisfactorysoftware.co.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
 * DataTypes animation handler (implementation)
*/

#ifdef WITH_AMIGA_DATATYPES
#include "amiga/filetype.h"
#include "amiga/datatypes.h"
#include "amiga/plotters.h"
#include "content/content_protected.h"
#include "desktop/plotters.h"
#include "image/bitmap.h"
#include "render/box.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/talloc.h"

#include <proto/datatypes.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <datatypes/animationclass.h>
#include <datatypes/pictureclass.h>
#include <graphics/blitattr.h>
#include <intuition/classusr.h>

typedef struct amiga_dt_anim_content {
	struct content base;

	struct bitmap *bitmap;	/**< Created NetSurf bitmap */

	Object *dto;
	int x;
	int y;
	int w;
	int h;
} amiga_dt_anim_content;

APTR ami_colormap_to_clut(struct ColorMap *cmap);

static nserror amiga_dt_anim_create(const content_handler *handler,
		lwc_string *imime_type, const http_parameter *params,
		llcache_handle *llcache, const char *fallback_charset,
		bool quirks, struct content **c);
static bool amiga_dt_anim_convert(struct content *c);
static void amiga_dt_anim_reformat(struct content *c, int width, int height);
static void amiga_dt_anim_destroy(struct content *c);
static bool amiga_dt_anim_redraw(struct content *c,
		struct content_redraw_data *data, const struct rect *clip,
		const struct redraw_context *ctx);
static void amiga_dt_anim_open(struct content *c, struct browser_window *bw,
		struct content *page, struct box *box,
		struct object_params *params);
static void amiga_dt_anim_close(struct content *c);
static nserror amiga_dt_anim_clone(const struct content *old, struct content **newc);
static content_type amiga_dt_anim_content_type(lwc_string *mime_type);

static void *amiga_dt_anim_get_internal(const struct content *c, void *context)
{
	amiga_dt_anim_content *adta_c = (amiga_dt_anim_content *)c;

	return adta_c->bitmap;
}

static const content_handler amiga_dt_anim_content_handler = {
	.create = amiga_dt_anim_create,
	.data_complete = amiga_dt_anim_convert,
	.reformat = amiga_dt_anim_reformat,
	.destroy = amiga_dt_anim_destroy,
	.redraw = amiga_dt_anim_redraw,
	.open = amiga_dt_anim_open,
	.close = amiga_dt_anim_close,
	.clone = amiga_dt_anim_clone,
	.get_internal = amiga_dt_anim_get_internal,
	.type = amiga_dt_anim_content_type,
	.no_share = false,
};

nserror amiga_dt_anim_init(void)
{
	char dt_mime[50];
	struct DataType *dt, *prevdt = NULL;
	lwc_string *type;
	lwc_error lerror;
	nserror error;
	BPTR fh = 0;
	struct Node *node = NULL;

	while((dt = ObtainDataType(DTST_RAM, NULL,
			DTA_DataType, prevdt,
			DTA_GroupID, GID_ANIMATION,
			TAG_DONE)) != NULL)
	{
		ReleaseDataType(prevdt);
		prevdt = dt;

		do {
			node = ami_mime_from_datatype(dt, &type, node);

			if(node)
			{
				error = content_factory_register_handler(type, 
					&amiga_dt_anim_content_handler);

				if (error != NSERROR_OK)
					return error;
			}

		}while (node != NULL);

	}

	ReleaseDataType(prevdt);

	return NSERROR_OK;
}

void amiga_dt_anim_fini(void)
{
	/* Nothing to do */
}

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

	plugin = talloc_zero(0, amiga_dt_anim_content);
	if (plugin == NULL)
		return NSERROR_NOMEM;

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

	*c = (struct content *) plugin;

	return NSERROR_OK;
}

bool amiga_dt_anim_convert(struct content *c)
{
	LOG(("amiga_dt_anim_convert"));

	amiga_dt_anim_content *plugin = (amiga_dt_anim_content *) c;
	union content_msg_data msg_data;
	int width, height;
	char title[100];
	const uint8 *data;
	UBYTE *bm_buffer;
	ULONG size;
	Object *dto;
	struct BitMapHeader *bmh;
	unsigned int bm_flags = BITMAP_NEW | BITMAP_OPAQUE;
	int bm_format = PBPAFMT_RGBA;
	struct adtFrame adt_frame;
	APTR clut;

	data = (uint8 *)content__get_source_data(c, &size);

	if(plugin->dto = NewDTObject(NULL,
					DTA_SourceType, DTST_MEMORY,
					DTA_SourceAddress, data,
					DTA_SourceSize, size,
					DTA_GroupID, GID_ANIMATION,
					TAG_DONE))
	{
		if(GetDTAttrs(plugin->dto, PDTA_BitMapHeader, &bmh, TAG_DONE))
		{
			width = (int)bmh->bmh_Width;
			height = (int)bmh->bmh_Height;

			plugin->bitmap = bitmap_create(width, height, bm_flags);
			if (!plugin->bitmap) {
				msg_data.error = messages_get("NoMemory");
				content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
				return false;
			}

			bm_buffer = bitmap_get_buffer(plugin->bitmap);

			adt_frame.MethodID = ADTM_LOADFRAME;
			adt_frame.alf_TimeStamp = 0;
			IDoMethodA(plugin->dto, &adt_frame);

			clut = ami_colormap_to_clut(adt_frame.alf_CMap);

			BltBitMapTags(
				BLITA_Width, width,
				BLITA_Height, height,
				BLITA_Source, adt_frame.alf_BitMap,
				BLITA_SrcType, BLITT_BITMAP,
				BLITA_Dest, bm_buffer,
				BLITA_DestType, BLITT_RGB24,
				BLITA_DestBytesPerRow, width,
				BLITA_CLUT, clut,
				TAG_DONE);

				FreeVec(clut);

				adt_frame.MethodID = ADTM_UNLOADFRAME;
				IDoMethodA(plugin->dto, &adt_frame);
		}
		else return false;
	}
	else return false;

	c->width = width;
	c->height = height;

/*
	snprintf(title, sizeof(title), "image (%lux%lu, %lu bytes)",
		width, height, size);
	content__set_title(c, title);
*/

	bitmap_modified(plugin->bitmap);

	content_set_ready(c);
	content_set_done(c);

	content_set_status(c, "");
	return true;
}

void amiga_dt_anim_destroy(struct content *c)
{
	amiga_dt_anim_content *plugin = (amiga_dt_anim_content *) c;

	LOG(("amiga_dt_anim_destroy"));

	if (plugin->bitmap != NULL)
		bitmap_destroy(plugin->bitmap);

	DisposeDTObject(plugin->dto);

	return;
}

bool amiga_dt_anim_redraw(struct content *c,
		struct content_redraw_data *data, const struct rect *clip,
		const struct redraw_context *ctx)
{
	amiga_dt_anim_content *plugin = (amiga_dt_anim_content *) c;
	bitmap_flags_t flags = BITMAPF_NONE;

	LOG(("amiga_dt_anim_redraw"));

	if (data->repeat_x)
		flags |= BITMAPF_REPEAT_X;
	if (data->repeat_y)
		flags |= BITMAPF_REPEAT_Y;

	return ctx->plot->bitmap(data->x, data->y, data->width, data->height,
			plugin->bitmap, data->background_colour, flags);
}

/**
 * Handle a window containing a CONTENT_PLUGIN being opened.
 *
 * \param  c       content that has been opened
 * \param  bw      browser window containing the content
 * \param  page    content of type CONTENT_HTML containing c, or 0 if not an
 *                 object within a page
 * \param  box     box containing c, or 0 if not an object
 * \param  params  object parameters, or 0 if not an object
 */
void amiga_dt_anim_open(struct content *c, struct browser_window *bw,
	struct content *page, struct box *box,
	struct object_params *params)
{
	LOG(("amiga_dt_anim_open"));

	return;
}

void amiga_dt_anim_close(struct content *c)
{
	LOG(("amiga_dt_anim_close"));
	return;
}

void amiga_dt_anim_reformat(struct content *c, int width, int height)
{
	LOG(("amiga_dt_anim_reformat"));
	return;
}

nserror amiga_dt_anim_clone(const struct content *old, struct content **newc)
{
	amiga_dt_anim_content *plugin;
	nserror error;

	LOG(("amiga_dt_anim_clone"));

	plugin = talloc_zero(0, amiga_dt_anim_content);
	if (plugin == NULL)
		return NSERROR_NOMEM;

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

	/* We "clone" the old content by replaying conversion */
	if (old->status == CONTENT_STATUS_READY || 
			old->status == CONTENT_STATUS_DONE) {
		if (amiga_dt_anim_convert(&plugin->base) == false) {
			content_destroy(&plugin->base);
			return NSERROR_CLONE_FAILED;
		}
	}

	*newc = (struct content *) plugin;

	return NSERROR_OK;
}

content_type amiga_dt_anim_content_type(lwc_string *mime_type)
{
	return CONTENT_IMAGE;
}

APTR ami_colormap_to_clut(struct ColorMap *cmap)
{
	int i;
	UBYTE *clut = AllocVec(256 * 4, MEMF_CLEAR);
	ULONG colour[3 * 256];

	if(!clut) return NULL;

	/* Get the palette from the ColorMap */
	GetRGB32(cmap, 0, 256, (ULONG *)&colour);

	/* convert it to a table of ARGB values */
	for(i = 0; i < 1024; i += 4)
	{
		clut[i] = (0xff << 24) |
				((colour[i] & 0xff000000) >> 8) |
				((colour[i + 1] & 0xff000000) >> 16) |
				((colour[i + 2] & 0xff000000) >> 24);
	}

	return clut;
}

#endif