forked from 0xC0000054/pdn-webp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebPFileType.cs
275 lines (237 loc) · 11.9 KB
/
WebPFileType.cs
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
////////////////////////////////////////////////////////////////////////
//
// This file is part of pdn-webp, a FileType plugin for Paint.NET
// that loads and saves WebP images.
//
// Copyright (c) 2011-2020 Nicholas Hayes
//
// This file is licensed under the MIT License.
// See LICENSE.txt for complete licensing and attribution information.
//
////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using PaintDotNet;
using PaintDotNet.IndirectUI;
using PaintDotNet.IO;
using PaintDotNet.PropertySystem;
using WebPFileType.Exif;
namespace WebPFileType
{
[PluginSupportInfo(typeof(PluginSupportInfo))]
public sealed class WebPFileType :
PropertyBasedFileType
{
private enum PropertyNames
{
Preset,
Quality,
KeepMetadata, // Obsolete, but retained to keep the value from being reused for a different property.
ForumLink,
GitHubLink
}
private readonly IWebPStringResourceManager strings;
#if PDN_3_5_X
public WebPFileType()
: base("WebP",
FileTypeFlags.SupportsLoading | FileTypeFlags.SupportsSaving | FileTypeFlags.SavesWithProgress,
new string[] { ".webp" })
{
strings = new BuiltinStringResourceManager();
}
#else
public WebPFileType(IFileTypeHost host)
: base("WebP",
new FileTypeOptions
{
LoadExtensions = new string[] { ".webp" },
SaveExtensions = new string[] { ".webp" }
})
{
if (host != null)
{
strings = new PdnLocalizedStringResourceManager(host.Services.GetService<PaintDotNet.WebP.IWebPFileTypeStrings>());
}
else
{
strings = new BuiltinStringResourceManager();
}
}
#endif
private string GetString(string name)
{
return strings.GetString(name);
}
private static Document GetOrientedDocument(byte[] bytes, out ExifValueCollection exifMetadata)
{
exifMetadata = null;
Document doc = null;
// Load the image into a Bitmap so the EXIF orientation transform can be applied.
using (Bitmap image = WebPFile.Load(bytes))
{
byte[] exifBytes = WebPFile.GetExifBytes(bytes);
if (exifBytes != null)
{
exifMetadata = ExifParser.Parse(exifBytes);
if (exifMetadata.Count > 0)
{
MetadataEntry orientationProperty = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.Orientation);
if (orientationProperty != null)
{
RotateFlipType transform = MetadataHelpers.GetOrientationTransform(orientationProperty);
if (transform != RotateFlipType.RotateNoneFlipNone)
{
image.RotateFlip(transform);
}
}
MetadataEntry xResProperty = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.XResolution);
MetadataEntry yResProperty = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.YResolution);
MetadataEntry resUnitProperty = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.ResolutionUnit);
if (xResProperty != null && yResProperty != null && resUnitProperty != null)
{
if (MetadataHelpers.TryDecodeRational(xResProperty, out double xRes) &&
MetadataHelpers.TryDecodeRational(yResProperty, out double yRes) &&
MetadataHelpers.TryDecodeShort(resUnitProperty, out ushort resUnit))
{
if (xRes > 0.0 && yRes > 0.0)
{
double dpiX, dpiY;
switch ((MeasurementUnit)resUnit)
{
case MeasurementUnit.Centimeter:
dpiX = Document.DotsPerCmToDotsPerInch(xRes);
dpiY = Document.DotsPerCmToDotsPerInch(yRes);
break;
case MeasurementUnit.Inch:
dpiX = xRes;
dpiY = yRes;
break;
default:
// Unknown ResolutionUnit value.
dpiX = 0.0;
dpiY = 0.0;
break;
}
if (dpiX > 0.0 && dpiY > 0.0)
{
try
{
image.SetResolution((float)dpiX, (float)dpiY);
}
catch
{
// Ignore any errors when setting the resolution.
}
}
}
}
}
}
}
doc = Document.FromGdipImage(image);
}
return doc;
}
protected override Document OnLoad(Stream input)
{
byte[] bytes = new byte[input.Length];
input.ProperRead(bytes, 0, (int)input.Length);
Document doc = GetOrientedDocument(bytes, out ExifValueCollection exifMetadata);
byte[] colorProfileBytes = WebPFile.GetColorProfileBytes(bytes);
if (colorProfileBytes != null)
{
#if PDN_3_5_X
System.Drawing.Imaging.PropertyItem colorProfileItem = PaintDotNet.SystemLayer.PdnGraphics.CreatePropertyItem();
colorProfileItem.Id = unchecked((ushort)ExifTagID.IccProfileData);
colorProfileItem.Type = (short)ExifTagType.Undefined;
colorProfileItem.Len = colorProfileBytes.Length;
colorProfileItem.Value = colorProfileBytes.CloneT();
doc.Metadata.AddExifValues(new System.Drawing.Imaging.PropertyItem[] { colorProfileItem });
#else
doc.Metadata.AddExifPropertyItem(PaintDotNet.Imaging.ExifSection.Image,
unchecked((ushort)ExifTagID.IccProfileData),
new PaintDotNet.Imaging.ExifValue(PaintDotNet.Imaging.ExifValueType.Undefined,
colorProfileBytes.CloneT()));
#endif
}
if (exifMetadata != null)
{
foreach (MetadataEntry entry in exifMetadata.Distinct())
{
#if PDN_3_5_X
System.Drawing.Imaging.PropertyItem propertyItem = entry.TryCreateGdipPropertyItem();
if (propertyItem != null)
{
doc.Metadata.AddExifValues(new System.Drawing.Imaging.PropertyItem[] { propertyItem });
}
#else
doc.Metadata.AddExifPropertyItem(entry.CreateExifPropertyItem());
#endif
}
}
byte[] xmpBytes = WebPFile.GetXmpBytes(bytes);
if (xmpBytes != null)
{
#if PDN_3_5_X
doc.Metadata.SetUserValue(WebPMetadataNames.XMP, Convert.ToBase64String(xmpBytes, Base64FormattingOptions.None));
#else
PaintDotNet.Imaging.XmpPacket xmpPacket = PaintDotNet.Imaging.XmpPacket.TryParse(xmpBytes);
if (xmpPacket != null)
{
doc.Metadata.SetXmpPacket(xmpPacket);
}
#endif
}
return doc;
}
public override PropertyCollection OnCreateSavePropertyCollection()
{
List<Property> props = new List<Property>
{
StaticListChoiceProperty.CreateForEnum(PropertyNames.Preset, WebPPreset.Photo, false),
new Int32Property(PropertyNames.Quality, 95, 0, 100, false),
#if !PDN_3_5_X
new UriProperty(PropertyNames.ForumLink, new Uri("https://forums.getpaint.net/topic/21773-webp-filetype/")),
new UriProperty(PropertyNames.GitHubLink, new Uri("https://github.com/0xC0000054/pdn-webp"))
#endif
};
return new PropertyCollection(props);
}
public override ControlInfo OnCreateSaveConfigUI(PropertyCollection props)
{
ControlInfo info = CreateDefaultSaveConfigUI(props);
PropertyControlInfo presetPCI = info.FindControlForPropertyName(PropertyNames.Preset);
presetPCI.ControlProperties[ControlInfoPropertyNames.DisplayName].Value = GetString("Preset_DisplayName");
presetPCI.SetValueDisplayName(WebPPreset.Default, GetString("Preset_Default_DisplayName"));
presetPCI.SetValueDisplayName(WebPPreset.Drawing, GetString("Preset_Drawing_DisplayName"));
presetPCI.SetValueDisplayName(WebPPreset.Icon, GetString("Preset_Icon_DisplayName"));
presetPCI.SetValueDisplayName(WebPPreset.Photo, GetString("Preset_Photo_DisplayName"));
presetPCI.SetValueDisplayName(WebPPreset.Picture, GetString("Preset_Picture_DisplayName"));
presetPCI.SetValueDisplayName(WebPPreset.Text, GetString("Preset_Text_DisplayName"));
info.SetPropertyControlValue(PropertyNames.Quality, ControlInfoPropertyNames.DisplayName, GetString("Quality_DisplayName"));
#if !PDN_3_5_X
PropertyControlInfo forumLinkPCI = info.FindControlForPropertyName(PropertyNames.ForumLink);
forumLinkPCI.ControlProperties[ControlInfoPropertyNames.DisplayName].Value = GetString("ForumLink_DisplayName");
forumLinkPCI.ControlProperties[ControlInfoPropertyNames.Description].Value = GetString("ForumLink_Description");
PropertyControlInfo githubLinkPCI = info.FindControlForPropertyName(PropertyNames.GitHubLink);
githubLinkPCI.ControlProperties[ControlInfoPropertyNames.DisplayName].Value = string.Empty;
githubLinkPCI.ControlProperties[ControlInfoPropertyNames.Description].Value = "GitHub"; // GitHub is a brand name that should not be localized.
#endif
return info;
}
protected override bool IsReflexive(PropertyBasedSaveConfigToken token)
{
int quality = token.GetProperty<Int32Property>(PropertyNames.Quality).Value;
return quality == 100;
}
protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
{
int quality = token.GetProperty<Int32Property>(PropertyNames.Quality).Value;
WebPPreset preset = (WebPPreset)token.GetProperty(PropertyNames.Preset).Value;
WebPFile.Save(input, output, quality, preset, scratchSurface, progressCallback);
}
}
}