-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWebPImageCodec.cs
More file actions
114 lines (73 loc) · 3.51 KB
/
Copy pathWebPImageCodec.cs
File metadata and controls
114 lines (73 loc) · 3.51 KB
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
#if NETFRAMEWORK
using Gsemac.Drawing.Imaging.Properties;
using Gsemac.IO;
using Gsemac.IO.Extensions;
using Gsemac.IO.FileFormats;
using Gsemac.Reflection.Plugins;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using WebPWrapper;
namespace Gsemac.Drawing.Imaging {
[RequiresAssemblies("libwebp", "libwebpdemux", "libwebpdecoder")]
[RequiresAssemblyOrTypes("WebPWrapper", "WebPWrapper.WebP")]
public class WebPImageCodec :
PluginBase,
IImageCodec {
// Public members
public IEnumerable<ICodecCapabilities> GetSupportedFileFormats() {
return GetSupportedImageFormats();
}
public void Encode(IImage image, Stream stream, IImageEncoderOptions encoderOptions) {
using (Bitmap bitmap = image.ToBitmap())
EncodeWebPBitmap(bitmap, stream, encoderOptions);
}
public IImage Decode(Stream stream, IImageDecoderOptions options) {
byte[] webPData = stream.ToArray();
using (WebP decoder = new WebP())
using (Image decodedWebPBitmap = DecodeWebPBitmap(decoder, webPData)) {
IImage image = ImageFactory.Default.FromBitmap(decodedWebPBitmap, format: ImageFormat.WebP, codec: this);
int frameCount = 1;
int animationIterations = 0;
TimeSpan animationDelay = TimeSpan.Zero;
// Decode the animation data.
decoder.GetInfo(webPData, out _, out _, out _, out bool hasAnimation, out string _);
if (hasAnimation) {
// TODO: WebPWrapper doesn't support decoding animated images
// Fail on animated images if we're attempting to read anything more than metadata.
if (options.Mode != ImageDecoderMode.Metadata)
throw new FileFormatException(ExceptionMessages.CannotDecodeAnimatedWebPImage);
using (IWebPDemuxer demuxer = new WebPDemuxer(webPData)) {
frameCount = demuxer.GetI(WebPFormatFeature.FrameCount);
animationIterations = demuxer.GetI(WebPFormatFeature.LoopCount);
IWebPFrame frame = demuxer.GetFrame(1); // WebP frame indices are 1-based
animationDelay = frame.Duration;
}
}
return new WebPImage(image, frameCount, animationIterations, animationDelay);
}
}
// Private members
private IEnumerable<ICodecCapabilities> GetSupportedImageFormats() {
return new[] {
ImageFormat.WebP,
}
.OrderBy(f => f.Extensions.First())
.Distinct()
.Select(f => new CodecCapabilities(f, canRead: true, canWrite: true));
}
private void EncodeWebPBitmap(Bitmap bitmap, Stream stream, IImageEncoderOptions encoderOptions) {
bool useLosslessEncoding = encoderOptions.Quality >= 100 ||
encoderOptions.OptimizationMode == OptimizationMode.Lossless;
using (WebP encoder = new WebP())
using (MemoryStream webPStream = new MemoryStream(useLosslessEncoding ? encoder.EncodeLossless(bitmap) : encoder.EncodeLossy(bitmap, encoderOptions.Quality)))
webPStream.CopyTo(stream);
}
private Image DecodeWebPBitmap(WebP decoder, byte[] webPData) {
return decoder.Decode(webPData);
}
}
}
#endif