1 /* load_webp.c - load WEBP image from file
3 * Raster graphics library
5 * Copyright (c) 2014 Window Maker Team
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
29 #include <webp/decode.h>
32 #include "imgformat.h"
35 RImage
*RLoadWEBP(const char *file_name
)
43 WebPBitstreamFeatures features
;
46 file
= fopen(file_name
, "rb");
48 RErrorCode
= RERR_OPEN
;
52 if (!fread(buffer
, sizeof(buffer
), 1, file
)) {
53 RErrorCode
= RERR_BADIMAGEFILE
;
58 if (!(buffer
[ 0] == 'R' && buffer
[ 1] == 'I' && buffer
[ 2] == 'F' && buffer
[ 3] == 'F' &&
59 buffer
[ 8] == 'W' && buffer
[ 9] == 'E' && buffer
[10] == 'B' && buffer
[11] == 'P' &&
60 buffer
[12] == 'V' && buffer
[13] == 'P' && buffer
[14] == '8' &&
61 #if WEBP_DECODER_ABI_VERSION < 0x0003 /* old versions don't support WEBPVP8X and WEBPVP8L */
64 (buffer
[15] == ' ' || buffer
[15] == 'X' || buffer
[15] == 'L'))) {
66 RErrorCode
= RERR_BADFORMAT
;
71 fseek(file
, 0L, SEEK_END
);
72 raw_data_size
= ftell(file
);
74 if (raw_data_size
<= 0) {
75 fprintf(stderr
, "wrlib: Failed to find the WEBP file size for \"%s\"\n", file_name
);
76 RErrorCode
= RERR_BADIMAGEFILE
;
81 raw_data
= (uint8_t *) malloc(raw_data_size
);
84 RErrorCode
= RERR_NOMEMORY
;
89 fseek(file
, 0L, SEEK_SET
);
90 r
= fread(raw_data
, 1, raw_data_size
, file
);
93 if (r
!= raw_data_size
) {
94 RErrorCode
= RERR_READ
;
99 if (WebPGetFeatures(raw_data
, raw_data_size
, &features
) != VP8_STATUS_OK
) {
100 fprintf(stderr
, "wrlib: WebPGetFeatures has failed on \"%s\"\n", file_name
);
101 RErrorCode
= RERR_BADIMAGEFILE
;
106 if (features
.has_alpha
) {
107 image
= RCreateImage(features
.width
, features
.height
, True
);
109 RErrorCode
= RERR_NOMEMORY
;
113 ret
= WebPDecodeRGBAInto(raw_data
, raw_data_size
, image
->data
,
114 features
.width
* features
.height
* 4,
117 image
= RCreateImage(features
.width
, features
.height
, False
);
119 RErrorCode
= RERR_NOMEMORY
;
123 ret
= WebPDecodeRGBInto(raw_data
, raw_data_size
, image
->data
,
124 features
.width
* features
.height
* 3,
131 fprintf(stderr
, "wrlib: Failed to decode WEBP from file \"%s\"\n", file_name
);
132 RErrorCode
= RERR_BADIMAGEFILE
;
133 RReleaseImage(image
);