summaryrefslogtreecommitdiff
path: root/amiga/dt_picture.c
blob: 15fc30e6a199bff83a8c93728b9bc098b185141e (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
/*
 * Copyright 2011 - 2012 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 picture handler (implementation)
*/

#ifdef WITH_AMIGA_DATATYPES
#include "amiga/os3support.h"

#include "amiga/bitmap.h"
#include "amiga/filetype.h"
#include "amiga/datatypes.h"
#include "content/content_protected.h"
#include "desktop/plotters.h"
#include "image/bitmap.h"
#include "image/image_cache.h"
#include "utils/log.h"
#include "utils/messages.h"

#include <proto/datatypes.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <datatypes/pictureclass.h>
#include <intuition/classusr.h>

static nserror amiga_dt_picture_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_picture_convert(struct content *c);
static nserror amiga_dt_picture_clone(const struct content *old, struct content **newc);
static void amiga_dt_picture_destroy(struct content *c);

static const content_handler amiga_dt_picture_content_handler = {
	.create = amiga_dt_picture_create,
	.data_complete = amiga_dt_picture_convert,
	.destroy = amiga_dt_picture_destroy,
	.redraw = image_cache_redraw,
	.clone = amiga_dt_picture_clone,
	.get_internal = image_cache_get_internal,
	.type = image_cache_content_type,
	.no_share = false,
};

struct amiga_dt_picture_content {
	struct content c;
	Object *dto;
};

nserror amiga_dt_picture_init(void)
{
	struct DataType *dt, *prevdt = NULL;
	lwc_string *type;
	nserror error;
	struct Node *node = NULL;

	while((dt = ObtainDataType(DTST_RAM, NULL,
			DTA_DataType, prevdt,
			DTA_GroupID, GID_PICTURE, // we only support images for now
			TAG_DONE)) != NULL)
	{
		if(prevdt) ReleaseDataType(prevdt);
		prevdt = dt;

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

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

				if (error != NSERROR_OK)
					return error;
			}

		}while (node != NULL);

	}

	ReleaseDataType(prevdt);

	return NSERROR_OK;
}

nserror amiga_dt_picture_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)
{
	struct amiga_dt_picture_content *adt;
	nserror error;

	adt = calloc(1, sizeof(struct amiga_dt_picture_content));
	if (adt == NULL)
		return NSERROR_NOMEM;

	error = content__init((struct content *)adt, handler, imime_type, params,
			llcache, fallback_charset, quirks);
	if (error != NSERROR_OK) {
		free(adt);
		return error;
	}

	*c = (struct content *)adt;

	return NSERROR_OK;
}

static Object *amiga_dt_picture_newdtobject(struct amiga_dt_picture_content *adt)
{
	const uint8 *data;
	ULONG size;

	if(adt->dto == NULL) {
		data = (uint8 *)content__get_source_data((struct content *)adt, &size);

		adt->dto = NewDTObject(NULL,
					DTA_SourceType, DTST_MEMORY,
					DTA_SourceAddress, data,
					DTA_SourceSize, size,
					DTA_GroupID, GID_PICTURE,
					PDTA_DestMode, PMODE_V43,
					PDTA_PromoteMask, TRUE,
					TAG_DONE);
	}

	return adt->dto;
}

static char *amiga_dt_picture_datatype(struct content *c)
{
	const uint8 *data;
	ULONG size;
	struct DataType *dt;
	char *filetype = NULL;
	
	data = (uint8 *)content__get_source_data(c, &size);

	if((dt = ObtainDataType(DTST_MEMORY, NULL,
					DTA_SourceAddress, data,
					DTA_SourceSize, size,
					DTA_GroupID, GID_PICTURE,
					TAG_DONE))) {
		filetype = strdup(dt->dtn_Header->dth_Name);
		ReleaseDataType(dt);
	}
	
	if(filetype == NULL) filetype = strdup("DataTypes");
	return filetype;
}

static struct bitmap *amiga_dt_picture_cache_convert(struct content *c)
{
	LOG(("amiga_dt_picture_cache_convert"));

	union content_msg_data msg_data;
	UBYTE *bm_buffer;
	Object *dto;
	struct bitmap *bitmap;
	struct amiga_dt_picture_content *adt = (struct amiga_dt_picture_content *)c;

	if((dto = amiga_dt_picture_newdtobject(adt)))
	{
		bitmap = bitmap_create(c->width, c->height, BITMAP_NEW);
		if (!bitmap) {
			msg_data.error = messages_get("NoMemory");
			content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
			return NULL;
		}

		bm_buffer = bitmap_get_buffer(bitmap);

		IDoMethod(dto, PDTM_READPIXELARRAY,
			bm_buffer, PBPAFMT_RGBA, bitmap_get_rowstride(bitmap),
			0, 0, c->width, c->height);

		bitmap_set_opaque(bitmap, bitmap_test_opaque(bitmap));
		
		DisposeDTObject(dto);
		adt->dto = NULL;
	}
	else return NULL;

	return bitmap;
}

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

	int width, height;
	char *title;
	Object *dto;
	struct BitMapHeader *bmh;
	char *filetype;

	if((dto = amiga_dt_picture_newdtobject((struct amiga_dt_picture_content *)c))) {
		if(GetDTAttrs(dto, PDTA_BitMapHeader, &bmh, TAG_DONE)) {
			width = (int)bmh->bmh_Width;
			height = (int)bmh->bmh_Height;
		}
		else return false;
	}
	else return false;

	c->width = width;
	c->height = height;
	c->size = width * height * 4;

	/* set title text */
	if((filetype = amiga_dt_picture_datatype(c))) {
		title = messages_get_buff("DataTypesTitle",
			nsurl_access_leaf(llcache_handle_get_url(c->llcache)),
			filetype, c->width, c->height);
		if (title != NULL) {
			content__set_title(c, title);
			free(title);
		}
		free(filetype);
	}
	
	image_cache_add(c, NULL, amiga_dt_picture_cache_convert);

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

nserror amiga_dt_picture_clone(const struct content *old, struct content **newc)
{
	struct content *adt;
	nserror error;

	LOG(("amiga_dt_picture_clone"));

	adt = calloc(1, sizeof(struct content));
	if (adt == NULL)
		return NSERROR_NOMEM;

	error = content__clone(old, adt);
	if (error != NSERROR_OK) {
		content_destroy(adt);
		return error;
	}

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

	*newc = adt;

	return NSERROR_OK;
}

static void amiga_dt_picture_destroy(struct content *c)
{
	struct amiga_dt_picture_content *adt = (struct amiga_dt_picture_content *)c;

	DisposeDTObject(adt->dto);
	adt->dto = NULL;
	
	image_cache_destroy(c);
}

#endif