NetSurf
dt_picture.c
Go to the documentation of this file.
1/*
2 * Copyright 2011 - 2012 Chris Young <chris@unsatisfactorysoftware.co.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/** \file
20 * DataTypes picture handler (implementation)
21*/
22
23#ifdef WITH_AMIGA_DATATYPES
24#include "amiga/os3support.h"
25
26#include <stdbool.h>
27#include <stdlib.h>
28#include <string.h>
29#include <proto/datatypes.h>
30#include <proto/dos.h>
31#include <proto/intuition.h>
32#include <datatypes/pictureclass.h>
33#include <intuition/classusr.h>
34
35#include "utils/log.h"
36#include "utils/messages.h"
37#include "netsurf/plotters.h"
38#include "netsurf/bitmap.h"
39#include "content/llcache.h"
40#include "content/content.h"
44
45#include "amiga/bitmap.h"
46#include "amiga/filetype.h"
47#include "amiga/datatypes.h"
48
49
50static nserror amiga_dt_picture_create(const content_handler *handler,
51 lwc_string *imime_type, const struct http_parameter *params,
52 llcache_handle *llcache, const char *fallback_charset,
53 bool quirks, struct content **c);
54static bool amiga_dt_picture_convert(struct content *c);
55static nserror amiga_dt_picture_clone(const struct content *old, struct content **newc);
56static void amiga_dt_picture_destroy(struct content *c);
57
58static const content_handler amiga_dt_picture_content_handler = {
59 .create = amiga_dt_picture_create,
60 .data_complete = amiga_dt_picture_convert,
61 .destroy = amiga_dt_picture_destroy,
62 .redraw = image_cache_redraw,
63 .clone = amiga_dt_picture_clone,
64 .get_internal = image_cache_get_internal,
66 .no_share = false,
67};
68
69struct amiga_dt_picture_content {
70 struct content c;
71 Object *dto;
72};
73
74nserror amiga_dt_picture_init(void)
75{
76 struct DataType *dt, *prevdt = NULL;
77 lwc_string *type;
78 nserror error;
79 struct Node *node = NULL;
80
81 while((dt = ObtainDataType(DTST_RAM, NULL,
82 DTA_DataType, prevdt,
83 DTA_GroupID, GID_PICTURE, // we only support images for now
84 TAG_DONE)) != NULL)
85 {
86 if(prevdt) ReleaseDataType(prevdt);
87 prevdt = dt;
88
89 do {
90 node = ami_mime_from_datatype(dt, &type, node);
91
92 if(node)
93 {
95 lwc_string_data(type),
96 &amiga_dt_picture_content_handler);
97
98 if (error != NSERROR_OK)
99 return error;
100 }
101
102 }while (node != NULL);
103
104 }
105
106 ReleaseDataType(prevdt);
107
108 return NSERROR_OK;
109}
110
111nserror amiga_dt_picture_create(const content_handler *handler,
112 lwc_string *imime_type, const struct http_parameter *params,
113 llcache_handle *llcache, const char *fallback_charset,
114 bool quirks, struct content **c)
115{
116 struct amiga_dt_picture_content *adt;
117 nserror error;
118
119 adt = calloc(1, sizeof(struct amiga_dt_picture_content));
120 if (adt == NULL)
121 return NSERROR_NOMEM;
122
123 error = content__init((struct content *)adt, handler, imime_type, params,
124 llcache, fallback_charset, quirks);
125 if (error != NSERROR_OK) {
126 free(adt);
127 return error;
128 }
129
130 *c = (struct content *)adt;
131
132 return NSERROR_OK;
133}
134
135static Object *amiga_dt_picture_newdtobject(struct amiga_dt_picture_content *adt)
136{
137 const uint8_t *data;
138 size_t size;
139
140 if(adt->dto == NULL) {
141 data = content__get_source_data((struct content *)adt, &size);
142
143 adt->dto = NewDTObject(NULL,
144 DTA_SourceType, DTST_MEMORY,
145 DTA_SourceAddress, data,
146 DTA_SourceSize, size,
147 DTA_GroupID, GID_PICTURE,
148 PDTA_DestMode, PMODE_V43,
149 PDTA_PromoteMask, TRUE,
150 TAG_DONE);
151 }
152
153 return adt->dto;
154}
155
156static char *amiga_dt_picture_datatype(struct content *c)
157{
158 const uint8_t *data;
159 size_t size;
160 struct DataType *dt;
161 char *filetype = NULL;
162
163 data = content__get_source_data(c, &size);
164
165 if((dt = ObtainDataType(DTST_MEMORY, NULL,
166 DTA_SourceAddress, data,
167 DTA_SourceSize, size,
168 DTA_GroupID, GID_PICTURE,
169 TAG_DONE))) {
170 filetype = strdup(dt->dtn_Header->dth_Name);
171 ReleaseDataType(dt);
172 }
173
174 if(filetype == NULL) filetype = strdup("DataTypes");
175 return filetype;
176}
177
178static struct bitmap *amiga_dt_picture_cache_convert(struct content *c)
179{
180 NSLOG(netsurf, INFO, "amiga_dt_picture_cache_convert");
181
182 union content_msg_data msg_data;
183 UBYTE *bm_buffer;
184 Object *dto;
185 struct bitmap *bitmap;
186 struct amiga_dt_picture_content *adt = (struct amiga_dt_picture_content *)c;
187
188 if((dto = amiga_dt_picture_newdtobject(adt)))
189 {
191 if (!bitmap) {
192 msg_data.errordata.errorcode = NSERROR_NOMEM;
193 msg_data.errordata.errormsg = messages_get("NoMemory");
195 return NULL;
196 }
197
198 bm_buffer = amiga_bitmap_get_buffer(bitmap);
199
200 IDoMethod(dto, PDTM_READPIXELARRAY,
201 bm_buffer, PBPAFMT_RGBA,
203 0, 0, c->width, c->height);
204
206
207 DisposeDTObject(dto);
208 adt->dto = NULL;
209 }
210 else return NULL;
211
212 return bitmap;
213}
214
215bool amiga_dt_picture_convert(struct content *c)
216{
217 NSLOG(netsurf, INFO, "amiga_dt_picture_convert");
218
219 int width, height;
220 char *title;
221 Object *dto;
222 struct BitMapHeader *bmh;
223 char *filetype;
224
225 if((dto = amiga_dt_picture_newdtobject((struct amiga_dt_picture_content *)c))) {
226 if(GetDTAttrs(dto, PDTA_BitMapHeader, &bmh, TAG_DONE)) {
227 width = (int)bmh->bmh_Width;
228 height = (int)bmh->bmh_Height;
229 }
230 else return false;
231 }
232 else return false;
233
234 c->width = width;
235 c->height = height;
236 c->size = width * height * 4;
237
238 /* set title text */
239 if((filetype = amiga_dt_picture_datatype(c))) {
240 title = messages_get_buff("DataTypesTitle",
242 filetype, c->width, c->height);
243 if (title != NULL) {
244 content__set_title(c, title);
245 free(title);
246 }
247 free(filetype);
248 }
249
250 image_cache_add(c, NULL, amiga_dt_picture_cache_convert);
251
254 content_set_status(c, "");
255 return true;
256}
257
258nserror amiga_dt_picture_clone(const struct content *old, struct content **newc)
259{
260 struct content *adt;
261 nserror error;
262
263 NSLOG(netsurf, INFO, "amiga_dt_picture_clone");
264
265 adt = calloc(1, sizeof(struct content));
266 if (adt == NULL)
267 return NSERROR_NOMEM;
268
269 error = content__clone(old, adt);
270 if (error != NSERROR_OK) {
271 content_destroy(adt);
272 return error;
273 }
274
275 /* We "clone" the old content by replaying conversion */
276 if ((old->status == CONTENT_STATUS_READY) ||
277 (old->status == CONTENT_STATUS_DONE)) {
278 if (amiga_dt_picture_convert(adt) == false) {
279 content_destroy(adt);
281 }
282 }
283
284 *newc = adt;
285
286 return NSERROR_OK;
287}
288
289static void amiga_dt_picture_destroy(struct content *c)
290{
291 struct amiga_dt_picture_content *adt = (struct amiga_dt_picture_content *)c;
292
293 DisposeDTObject(adt->dto);
294 adt->dto = NULL;
295
297}
298
299#endif
struct Node * ami_mime_from_datatype(struct DataType *dt, lwc_string **mimetype, struct Node *start_node)
Return a MIME Type matching a DataType.
Definition: filetype.c:455
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
nserror content_factory_register_handler(const char *mime_type, const content_handler *handler)
Register a handler with the content factory.
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_MSG_ERROR
error occurred
Definition: content_type.h:122
nserror
Enumeration of error codes.
Definition: errors.h:29
@ 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
size_t amiga_bitmap_get_rowstride(void *bitmap)
Find the width of a pixel row in bytes.
Definition: bitmap.c:197
void * amiga_bitmap_create(int width, int height, enum gui_bitmap_flags flags)
Create a bitmap.
Definition: bitmap.c:111
void amiga_bitmap_set_opaque(void *bitmap, bool opaque)
Sets whether a bitmap should be plotted opaque.
Definition: bitmap.c:300
unsigned char * amiga_bitmap_get_buffer(void *bitmap)
Return a pointer to the pixel data in a bitmap.
Definition: bitmap.c:174
content_type image_cache_content_type(void)
Definition: image_cache.c:873
void * image_cache_get_internal(const struct content *c, void *context)
Definition: image_cache.c:856
bool image_cache_redraw(struct content *c, struct content_redraw_data *data, const struct rect *clip, const struct redraw_context *ctx)
Generic content redraw callback.
Definition: image_cache.c:798
void image_cache_destroy(struct content *content)
Definition: image_cache.c:841
nserror image_cache_add(struct content *content, struct bitmap *bitmap, image_cache_convert_fn *convert)
adds an image content to be cached.
Definition: image_cache.c:510
The image content handler intermediate image cache.
Generic bitmap handling interface.
bool bitmap_test_opaque(void *bitmap)
Test whether a bitmap is completely opaque (no transparency).
Definition: bitmap.c:316
@ BITMAP_NONE
Definition: bitmap.h:37
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.
Minimal compatibility header for AmigaOS 3.
#define PDTA_PromoteMask
Definition: os3support.h:75
#define IDoMethod
Definition: os3support.h:169
int width
Definition: gui.c:160
int height
Definition: gui.c:161
Interface to utility string handling.
RISC OS wimp toolkit bitmap.
Definition: bitmap.c:68
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)
Content which corresponds to a single URL.
int height
Height dimension, if applicable.
int width
Width dimension, if applicable.
content_status status
Current status.
unsigned int size
Estimated size of all data associated with this content.
Representation of an HTTP parameter.
Definition: parameter.c:31
Handle to low-level cache object.
Definition: llcache.c:76
Extra data for some content_msg messages.
Definition: content.h:60
static nserror bitmap(const struct redraw_context *ctx, struct bitmap *bitmap, int x, int y, int width, int height, colour bg, bitmap_flags_t flags)
Plot a bitmap.
Definition: plot.c:857