File: load_webp.c

package info (click to toggle)
wmaker 0.96.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,332 kB
  • sloc: ansic: 99,482; sh: 7,068; perl: 3,423; makefile: 1,647; lisp: 219; python: 34
file content (172 lines) | stat: -rw-r--r-- 4,776 bytes parent folder | download
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
/* load_webp.c - load WEBP image from file
 *
 * Raster graphics library
 *
 * Copyright (c) 2014-2021 Window Maker Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 *  MA 02110-1301, USA.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <webp/decode.h>

#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"


/*
 * webp_message_from_status
 *
 * return the text message from a VP8 status code
 */
static const char *webp_message_from_status(VP8StatusCode status)
{
	static const char *const known_message[] = {
		/* Known codes as per libWebP 0.4.1 */
		[VP8_STATUS_OUT_OF_MEMORY] = N_("out of memory"),
		[VP8_STATUS_INVALID_PARAM] = N_("invalid parameter"),
		[VP8_STATUS_BITSTREAM_ERROR] = N_("error in the bitstream"),
		[VP8_STATUS_UNSUPPORTED_FEATURE] = N_("feature is not supported"),
		[VP8_STATUS_SUSPENDED] = N_("operation suspended"),
		[VP8_STATUS_USER_ABORT] = N_("aborted by user"),
		[VP8_STATUS_NOT_ENOUGH_DATA] = N_("not enough data")
	};
	static char custom_message[128];

	if (status >= 0 && status < sizeof(known_message) / sizeof(known_message[0]))
		if (known_message[status] != NULL)
			return known_message[status];

	snprintf(custom_message, sizeof(custom_message),
	         _("unknow status code %d"), status);
	return custom_message;
}

RImage *RLoadWEBP(const char *file_name)
{
	FILE *file;
	RImage *image = NULL;
	char buffer[20];
	int raw_data_size;
	int r;
	uint8_t *raw_data;
	VP8StatusCode status;
	WebPBitstreamFeatures features;
	uint8_t *ret = NULL;

	file = fopen(file_name, "rb");
	if (!file) {
		RErrorCode = RERR_OPEN;
		return NULL;
	}

	if (!fread(buffer, sizeof(buffer), 1, file)) {
		RErrorCode = RERR_BADIMAGEFILE;
		fclose(file);
		return NULL;
	}

	if (!(buffer[ 0] == 'R' && buffer[ 1] == 'I' && buffer[ 2] == 'F' && buffer[ 3] == 'F' &&
	      buffer[ 8] == 'W' && buffer[ 9] == 'E' && buffer[10] == 'B' && buffer[11] == 'P' &&
	      buffer[12] == 'V' && buffer[13] == 'P' && buffer[14] == '8' &&
#if WEBP_DECODER_ABI_VERSION < 0x0003	/* old versions don't support WEBPVP8X and WEBPVP8L */
	      buffer[15] == ' ')) {
#else
	      (buffer[15] == ' ' || buffer[15] == 'X' || buffer[15] == 'L'))) {
#endif
		RErrorCode = RERR_BADFORMAT;
		fclose(file);
		return NULL;
	}

	fseek(file, 0L, SEEK_END);
	raw_data_size = ftell(file);

	if (raw_data_size <= 0) {
		fprintf(stderr, _("wrlib: could not get size of WebP file \"%s\", %s\n"),
		        file_name, strerror(errno));
		RErrorCode = RERR_BADIMAGEFILE;
		fclose(file);
		return NULL;
	}

	raw_data = (uint8_t *) malloc(raw_data_size);

	if (!raw_data) {
		RErrorCode = RERR_NOMEMORY;
		fclose(file);
		return NULL;
	}

	fseek(file, 0L, SEEK_SET);
	r = fread(raw_data, 1, raw_data_size, file);
	fclose(file);

	if (r != raw_data_size) {
		RErrorCode = RERR_READ;
		free(raw_data);
		return NULL;
	}

	status = WebPGetFeatures(raw_data, raw_data_size, &features);
	if (status != VP8_STATUS_OK) {
		fprintf(stderr, _("wrlib: could not get features from WebP file \"%s\", %s\n"),
		        file_name, webp_message_from_status(status));
		RErrorCode = RERR_BADIMAGEFILE;
		free(raw_data);
		return NULL;
	}

	if (features.has_alpha) {
		image = RCreateImage(features.width, features.height, True);
		if (!image) {
			RErrorCode = RERR_NOMEMORY;
			free(raw_data);
			return NULL;
		}
		ret = WebPDecodeRGBAInto(raw_data, raw_data_size, image->data,
		                         features.width * features.height * 4,
		                         features.width * 4);
	} else {
		image = RCreateImage(features.width, features.height, False);
		if (!image) {
			RErrorCode = RERR_NOMEMORY;
			free(raw_data);
			return NULL;
		}
		ret = WebPDecodeRGBInto(raw_data, raw_data_size, image->data,
		                        features.width * features.height * 3,
		                        features.width * 3);
	}

	free(raw_data);

	if (!ret) {
		fprintf(stderr, _("wrlib: failed to decode WebP from file \"%s\"\n"), file_name);
		RErrorCode = RERR_BADIMAGEFILE;
		RReleaseImage(image);
		return NULL;
	}

	return image;
}