summaryrefslogtreecommitdiff
path: root/content/handlers/image/webp.c
blob: 66a86c9f44afd8a26440788b9ad382410592e4ce (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
/*
 * Copyright 2019 Vincent Sanders <vince@netsurf-browser.org>
 *
 * 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
 * implementation of content handling for image/webp
 *
 * This implementation uses the google webp library.
 * Image cache handling is performed by the generic NetSurf handler.
 */

#include <stdbool.h>
#include <stdlib.h>
#include <setjmp.h>

#include <webp/decode.h>

#include "utils/utils.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "netsurf/bitmap.h"
#include "content/llcache.h"
#include "content/content_protected.h"
#include "desktop/gui_internal.h"

#include "image/image_cache.h"

#include "webp.h"

/**
 * Content create entry point.
 *
 * create a content object for the webp
 */
static nserror
webp_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)
{
	struct content *webp_c; /* webp content object */
	nserror res;

	webp_c = calloc(1, sizeof(struct content));
	if (webp_c == NULL) {
		return NSERROR_NOMEM;
	}

	res = content__init(webp_c,
			    handler,
			    imime_type,
			    params,
			    llcache,
			    fallback_charset,
			    quirks);
	if (res != NSERROR_OK) {
		free(webp_c);
		return res;
	}

	*c = webp_c;

	return NSERROR_OK;
}

/**
 * create a bitmap from webp content.
 */
static struct bitmap *
webp_cache_convert(struct content *c)
{
	const uint8_t *source_data; /* webp source data */
	unsigned long source_size; /* length of webp source data */
	VP8StatusCode webpres;
	WebPBitstreamFeatures webpfeatures;
	unsigned int bmap_flags;
	uint8_t *pixels = NULL;
	uint8_t *decoded;
	size_t rowstride;
	struct bitmap *bitmap = NULL;

	source_data = (uint8_t *)content__get_source_data(c, &source_size);

	webpres = WebPGetFeatures(source_data, source_size, &webpfeatures);

	if (webpres != VP8_STATUS_OK) {
		return NULL;
	}

	if (webpfeatures.has_alpha == 0) {
		bmap_flags = BITMAP_NEW | BITMAP_OPAQUE;
	} else {
		bmap_flags = BITMAP_NEW;
	}

	/* create bitmap */
	bitmap = guit->bitmap->create(webpfeatures.width,
				      webpfeatures.height,
				      bmap_flags);
	if (bitmap == NULL) {
		/* empty bitmap could not be created */
		return NULL;
	}

	pixels = guit->bitmap->get_buffer(bitmap);
	if (pixels == NULL) {
		/* bitmap with no buffer available */
		guit->bitmap->destroy(bitmap);
		return NULL;
	}

	rowstride = guit->bitmap->get_rowstride(bitmap);

	decoded = WebPDecodeBGRAInto(source_data,
				     source_size,
				     pixels,
				     webpfeatures.width * webpfeatures.height * 4,
				     rowstride);
	if (decoded == NULL) {
		/* decode failed */
		guit->bitmap->destroy(bitmap);
		return NULL;
	}

	return bitmap;
}

/**
 * Convert the webp source data content.
 *
 * This ensures there is valid webp source data in the content object
 *  and then adds it to the image cache ready to be converted on
 *  demand.
 *
 * \param c The webp content object
 * \return true on successful processing of teh webp content else false
 */
static bool webp_convert(struct content *c)
{
	int res;
	unsigned long data_size;
	const uint8_t* data;
	int width;
	int height;

	data = (uint8_t *)content__get_source_data(c, &data_size);

	res = WebPGetInfo(data, data_size, &width, &height);
	if (res == 0) {
		NSLOG(netsurf, INFO, "WebPGetInfo failed:%p", c);
		return false;
	}

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

	image_cache_add(c, NULL, webp_cache_convert);

	content_set_ready(c);
	content_set_done(c);

	return true;
}

/**
 * Clone content.
 */
static nserror webp_clone(const struct content *old, struct content **new_c)
{
	struct content *webp_c; /* cloned webp content */
	nserror res;

	webp_c = calloc(1, sizeof(struct content));
	if (webp_c == NULL) {
		return NSERROR_NOMEM;
	}

	res = content__clone(old, webp_c);
	if (res != NSERROR_OK) {
		content_destroy(webp_c);
		return res;
	}

	/* re-convert if the content is ready */
	if ((old->status == CONTENT_STATUS_READY) ||
	    (old->status == CONTENT_STATUS_DONE)) {
		if (webp_convert(webp_c) == false) {
			content_destroy(webp_c);
			return NSERROR_CLONE_FAILED;
		}
	}

	*new_c = webp_c;

	return NSERROR_OK;
}

static const content_handler webp_content_handler = {
	.create = webp_create,
	.data_complete = webp_convert,
	.destroy = image_cache_destroy,
	.redraw = image_cache_redraw,
	.clone = webp_clone,
	.get_internal = image_cache_get_internal,
	.type = image_cache_content_type,
	.no_share = false,
};

static const char *webp_types[] = {
	"image/webp"
};

CONTENT_FACTORY_REGISTER_TYPES(nswebp, webp_types, webp_content_handler);