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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
From: Jens Georg <mail@jensge.org>
Date: Wed, 30 Aug 2017 21:46:55 +0200
Subject: Support reading WEBP
https://bugzilla.gnome.org/show_bug.cgi?id=717880
Requires a gexiv2 linked against exiv2 0.26 which currently works in the
flatpak and on F28, but NOT on Debian/Ubuntu 18.04
(cherry picked from commit f032a58dca391b1833c6ea70785bb3b63abc68c7)
---
meson.build | 3 +
src/meson.build | 3 +-
src/photos/PhotoFileFormat.vala | 18 ++-
src/photos/WebPSupport.vala | 240 ++++++++++++++++++++++++++++++++++++++++
vapi/libwebp.vapi | 5 +
vapi/libwebpdemux.vapi | 43 +++++++
6 files changed, 309 insertions(+), 3 deletions(-)
create mode 100644 src/photos/WebPSupport.vala
create mode 100644 vapi/libwebp.vapi
create mode 100644 vapi/libwebpdemux.vapi
diff --git a/meson.build b/meson.build
index 5d08d30..2316377 100644
--- a/meson.build
+++ b/meson.build
@@ -66,6 +66,9 @@ libexif = dependency('libexif', version : '>= 0.6.16')
unity = dependency('unity', required : false)
portal = [ dependency('libportal', version: '>= 0.5'), dependency('libportal-gtk3', version: '>= 0.5')]
+webpdemux = dependency('libwebpdemux')
+webp = dependency('libwebp')
+
unity_available = false
if unity.found() and get_option('unity-support')
unity_available = true
diff --git a/src/meson.build b/src/meson.build
index a532eec..8cab77d 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -29,7 +29,7 @@ face_sources = (['faces/FacesBranch.vala',
shotwell_deps = [gio, gee, sqlite, gtk, sqlite, posix, gphoto2,
gstreamer_pbu, gio_unix, gudev, gexiv2, gmodule,
- libraw, libexif, sw_plugin, portal, version]
+ libraw, libexif, sw_plugin, portal, version, webpdemux, webp]
if unity_available
shotwell_deps += [unity]
endif
@@ -73,6 +73,7 @@ executable('shotwell',
'photos/RawSupport.vala',
'photos/PngSupport.vala',
'photos/TiffSupport.vala',
+ 'photos/WebPSupport.vala',
'plugins/Plugins.vala',
'plugins/StandardHostInterface.vala',
'plugins/ManifestWidget.vala',
diff --git a/src/photos/PhotoFileFormat.vala b/src/photos/PhotoFileFormat.vala
index e642008..94ca752 100644
--- a/src/photos/PhotoFileFormat.vala
+++ b/src/photos/PhotoFileFormat.vala
@@ -58,12 +58,13 @@ public enum PhotoFileFormat {
TIFF,
BMP,
GIF,
+ WEBP,
UNKNOWN;
// This is currently listed in the order of detection, that is, the file is examined from
// left to right. (See PhotoFileInterrogator.)
public static PhotoFileFormat[] get_supported() {
- return { JFIF, RAW, PNG, TIFF, BMP, GIF };
+ return { JFIF, RAW, PNG, TIFF, BMP, GIF, WEBP };
}
public static PhotoFileFormat[] get_writeable() {
@@ -141,7 +142,10 @@ public enum PhotoFileFormat {
case GIF:
return 5;
-
+
+ case WEBP:
+ return 6;
+
case UNKNOWN:
default:
return -1;
@@ -169,6 +173,9 @@ public enum PhotoFileFormat {
case 5:
return GIF;
+ case 6:
+ return WEBP;
+
default:
return UNKNOWN;
}
@@ -249,6 +256,10 @@ public enum PhotoFileFormat {
Photos.GifFileFormatDriver.init();
break;
+ case WEBP:
+ Photos.WebpFileFormatDriver.init();
+ break;
+
default:
error("Unsupported file format %s", this.to_string());
}
@@ -274,6 +285,9 @@ public enum PhotoFileFormat {
case GIF:
return Photos.GifFileFormatDriver.get_instance();
+ case WEBP:
+ return Photos.WebpFileFormatDriver.get_instance();
+
default:
error("Unsupported file format %s", this.to_string());
}
diff --git a/src/photos/WebPSupport.vala b/src/photos/WebPSupport.vala
new file mode 100644
index 0000000..093f196
--- /dev/null
+++ b/src/photos/WebPSupport.vala
@@ -0,0 +1,240 @@
+/* Copyright 2016 Software Freedom Conservancy Inc.
+ *
+ * This software is licensed under the GNU LGPL (version 2.1 or later).
+ * See the COPYING file in this distribution.
+ */
+
+namespace Photos {
+
+public class WebpFileFormatDriver : PhotoFileFormatDriver {
+ private static WebpFileFormatDriver instance = null;
+
+ public static void init() {
+ instance = new WebpFileFormatDriver();
+ WebpFileFormatProperties.init();
+ }
+
+ public static WebpFileFormatDriver get_instance() {
+ return instance;
+ }
+
+ public override PhotoFileFormatProperties get_properties() {
+ return WebpFileFormatProperties.get_instance();
+ }
+
+ public override PhotoFileReader create_reader(string filepath) {
+ return new WebpReader(filepath);
+ }
+
+ public override PhotoMetadata create_metadata() {
+ return new PhotoMetadata();
+ }
+
+ public override bool can_write_image() {
+ return false;
+ }
+
+ public override bool can_write_metadata() {
+ return true;
+ }
+
+ public override PhotoFileWriter? create_writer(string filepath) {
+ return null;
+ }
+
+ public override PhotoFileMetadataWriter? create_metadata_writer(string filepath) {
+ return new WebpMetadataWriter(filepath);
+ }
+
+ public override PhotoFileSniffer create_sniffer(File file, PhotoFileSniffer.Options options) {
+ return new WebpSniffer(file, options);
+ }
+}
+
+private class WebpFileFormatProperties : PhotoFileFormatProperties {
+ private static string[] KNOWN_EXTENSIONS = {
+ "webp"
+ };
+
+ private static string[] KNOWN_MIME_TYPES = {
+ "image/webp"
+ };
+
+ private static WebpFileFormatProperties instance = null;
+
+ public static void init() {
+ instance = new WebpFileFormatProperties();
+ }
+
+ public static WebpFileFormatProperties get_instance() {
+ return instance;
+ }
+
+ public override PhotoFileFormat get_file_format() {
+ return PhotoFileFormat.WEBP;
+ }
+
+ public override PhotoFileFormatFlags get_flags() {
+ return PhotoFileFormatFlags.NONE;
+ }
+
+ public override string get_default_extension() {
+ return "webp";
+ }
+
+ public override string get_user_visible_name() {
+ return _("WebP");
+ }
+
+ public override string[] get_known_extensions() {
+ return KNOWN_EXTENSIONS;
+ }
+
+ public override string get_default_mime_type() {
+ return KNOWN_MIME_TYPES[0];
+ }
+
+ public override string[] get_mime_types() {
+ return KNOWN_MIME_TYPES;
+ }
+}
+
+private class WebpSniffer : PhotoFileSniffer {
+ private DetectedPhotoInformation detected = null;
+
+ public WebpSniffer(File file, PhotoFileSniffer.Options options) {
+ base (file, options);
+ detected = new DetectedPhotoInformation();
+ }
+
+ public override DetectedPhotoInformation? sniff(out bool is_corrupted) throws Error {
+ is_corrupted = false;
+
+ if (!is_webp(file))
+ return null;
+
+ // valac chokes on the ternary operator here
+ Checksum? md5_checksum = null;
+ if (calc_md5)
+ md5_checksum = new Checksum(ChecksumType.MD5);
+
+ detected.metadata = new PhotoMetadata();
+ try {
+ detected.metadata.read_from_file(file);
+ } catch (Error err) {
+ debug("Failed to load meta-data from file: %s", err.message);
+ // no metadata detected
+ detected.metadata = null;
+ }
+
+ if (calc_md5 && detected.metadata != null) {
+ detected.exif_md5 = detected.metadata.exif_hash();
+ detected.thumbnail_md5 = detected.metadata.thumbnail_hash();
+ }
+
+ // if no MD5, don't read as much, as the needed info will probably be gleaned
+ // in the first 8K to 16K
+ uint8[] buffer = calc_md5 ? new uint8[64 * 1024] : new uint8[8 * 1024];
+ size_t count = 0;
+
+ // loop through until all conditions we're searching for are met
+ FileInputStream fins = file.read(null);
+ var ba = new ByteArray();
+ for (;;) {
+ size_t bytes_read = fins.read(buffer, null);
+ if (bytes_read <= 0)
+ break;
+
+ ba.append(buffer[0:bytes_read]);
+
+ count += bytes_read;
+
+ if (calc_md5)
+ md5_checksum.update(buffer, bytes_read);
+
+ WebP.Data d = WebP.Data();
+ d.bytes = ba.data;
+
+ WebP.ParsingState state;
+ var demux = new WebP.Demuxer.partial(d, out state);
+
+ if (state == WebP.ParsingState.PARSE_ERROR) {
+ is_corrupted = true;
+ break;
+ }
+
+ if (state > WebP.ParsingState.PARSED_HEADER) {
+ detected.file_format = PhotoFileFormat.WEBP;
+ detected.format_name = "WebP";
+ detected.channels = 4;
+ detected.bits_per_channel = 8;
+ detected.image_dim.width = (int) demux.get(WebP.FormatFeature.CANVAS_WIDTH);
+ detected.image_dim.height = (int) demux.get(WebP.FormatFeature.CANVAS_HEIGHT);
+
+ // if not searching for anything else, exit
+ if (!calc_md5)
+ break;
+ }
+ }
+
+ if (fins != null)
+ fins.close(null);
+
+ if (calc_md5)
+ detected.md5 = md5_checksum.get_string();
+
+ return detected;
+ }
+}
+
+private class WebpReader : PhotoFileReader {
+ public WebpReader(string filepath) {
+ base (filepath, PhotoFileFormat.WEBP);
+ }
+
+ public override PhotoMetadata read_metadata() throws Error {
+ PhotoMetadata metadata = new PhotoMetadata();
+ metadata.read_from_file(get_file());
+
+ return metadata;
+ }
+
+ public override Gdk.Pixbuf unscaled_read() throws Error {
+ uint8[] buffer;
+
+ FileUtils.get_data(this.get_filepath(), out buffer);
+ int width, height;
+ var pixdata = WebP.DecodeRGBA(buffer, out width, out height);
+ pixdata.length = width * height * 4;
+
+ return new Gdk.Pixbuf.from_data(pixdata, Gdk.Colorspace.RGB, true, 8, width, height, width * 4);
+ }
+}
+
+private class WebpMetadataWriter : PhotoFileMetadataWriter {
+ public WebpMetadataWriter(string filepath) {
+ base (filepath, PhotoFileFormat.TIFF);
+ }
+
+ public override void write_metadata(PhotoMetadata metadata) throws Error {
+ metadata.write_to_file(get_file());
+ }
+}
+
+public bool is_webp(File file, Cancellable? cancellable = null) throws Error {
+ var ins = file.read();
+
+ uint8 buffer[12];
+ try {
+ ins.read(buffer, 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')
+ return true;
+ } catch (Error error) {
+ debug ("Failed to read from file %s: %s", file.get_path (), error.message);
+ }
+
+ return false;
+}
+
+}
diff --git a/vapi/libwebp.vapi b/vapi/libwebp.vapi
new file mode 100644
index 0000000..a19fbcf
--- /dev/null
+++ b/vapi/libwebp.vapi
@@ -0,0 +1,5 @@
+[CCode (cheader_filename = "webp/decode.h")]
+namespace WebP {
+ [CCode (array_length = false, cname="WebPDecodeRGBA")]
+ public static uint8[] DecodeRGBA([CCode (array_length_pos=1)]uint8[] data, out int width, out int height);
+}
diff --git a/vapi/libwebpdemux.vapi b/vapi/libwebpdemux.vapi
new file mode 100644
index 0000000..7612b42
--- /dev/null
+++ b/vapi/libwebpdemux.vapi
@@ -0,0 +1,43 @@
+namespace WebP {
+ [CCode (has_type_id = false)]
+ public struct Data {
+ [CCode (array_length_cname = "size")]
+ public unowned uint8[] bytes;
+
+ public size_t size;
+
+ [CCode (cname = "WebPDataClear")]
+ public void clear();
+ }
+
+ [CCode (cprefix = "WEBP_DEMUX_", cname = "WebPDemuxState")]
+ public enum ParsingState {
+ PARSE_ERROR,
+ PARSING_HEADER,
+ PARSED_HEADER,
+ DONE
+ }
+
+ [CCode (cprefix = "WEBP_FF_")]
+ public enum FormatFeature {
+ FORMAT_FLAGS,
+ CANVAS_WIDTH,
+ CANVAS_HEIGHT,
+ LOOP_COUNT,
+ BACKGROUND_COLOR,
+ FRAME_COUNT
+ }
+
+ [Compact]
+ [CCode (free_function = "WebPDemuxDelete", cname = "WebPDemuxer", cheader_filename = "webp/demux.h", has_type_id = false)]
+ public class Demuxer {
+ [CCode (cname="WebPDemux")]
+ public Demuxer(Data data);
+
+ [CCode (cname="WebPDemuxPartial")]
+ public Demuxer.partial(Data data, out ParsingState state);
+
+ [CCode (cname="WebPDemuxGetI")]
+ public uint32 get(FormatFeature feature);
+ }
+}
|