-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProgram.cs
343 lines (317 loc) · 14.3 KB
/
Program.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
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
using System;
using System.Threading.Tasks;
using System.Linq;
using WIC;
namespace heic2jpg
{
class Program
{
static async Task Main(string[] args)
{
if (!OperatingSystem.IsWindows())
{
Console.WriteLine("Sorry this tool is not supported on this operating system.");
return;
}
if (OperatingSystem.IsWindows())
AddRightClickMenu();
if (args == null || args.Length == 0)
{
Console.WriteLine("Usage: heic2jpg [filename]");
return;
}
foreach (var file in args)
{
await ConvertFile(file);
}
}
static async Task ConvertFile(string filename)
{
try
{
await Task.CompletedTask;
filename = System.IO.Path.GetFullPath(filename);
var imagingFactory = new WIC.WICImagingFactory();
var decoder = imagingFactory.CreateDecoderFromFilename(filename, Guid.Empty, WIC.StreamAccessMode.GENERIC_READ,
WIC.WICDecodeOptions.WICDecodeMetadataCacheOnLoad);
//ShowMetadata(decoder.GetFrame(0));
if (decoder.GetDecoderInfo().GetCLSID() == WIC.Decoder.Jpeg)
{
Console.WriteLine($"'{filename}' is already a JPEG file.");
return;
}
var output = imagingFactory.CreateStream();
output.InitializeFromFilename(System.IO.Path.ChangeExtension(filename, ".jpg"), WIC.StreamAccessMode.GENERIC_WRITE);
var encoder = imagingFactory.CreateEncoder(ContainerFormat.Jpeg);
encoder.Initialize(output, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);
for (int i = 0; i < decoder.GetFrameCount(); i++)
{
var frame = decoder.GetFrame(i);
encoder.CreateNewFrame(out var frameJpg, null);
frameJpg.Initialize(null);
frameJpg.SetSize(frame.GetSize());
frameJpg.SetResolution(frame.GetResolution());
frameJpg.SetPixelFormat(frame.GetPixelFormat());
var reader = frame.AsMetadataBlockReader();
var count = reader.GetCount();
//Get the EXIF data from the original photo.
var metadataReader = frame.GetMetadataQueryReader();
var metadataWriter = frameJpg.GetMetadataQueryWriter();
foreach (var name in metadataReader.GetNamesRecursive())
{
try {
var val = metadataReader.GetMetadataByName(name);
if (name.StartsWith("/ifd/"))
metadataWriter.SetMetadataByName("/app1" + name.Replace("/ifd/{ushort=34665}/", "/ifd/exif/").Replace("/ifd/{ushort=34853}/", "/ifd/gps/"), val);
else if (name.StartsWith("/xmp/"))
metadataWriter.SetMetadataByName(name, val);
}
catch {
System.Diagnostics.Trace.WriteLine($"Error setting '{name}'");
}
}
var photoProperties = SystemProperties.Concat(SystemPhotoProperties.Concat(SystemGpsProperties));
foreach (var photoProp in photoProperties)
{
var action = "getting";
try
{
var val = metadataReader.GetMetadataByName(photoProp);
//System.Diagnostics.Trace.WriteLine($"{photoProp} = {val}");
action = "setting";
metadataWriter.SetMetadataByName(photoProp, val);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine($"Error {action} '{photoProp}': " + ex.Message);
}
}
frameJpg.WriteSource(frame);
frameJpg.Commit();
frame = null;
frameJpg = null;
}
encoder.Commit();
output.Commit(WIC.STGC.STGC_DEFAULT);
encoder = null;
output = null;
}
catch (Exception ex)
{
Console.WriteLine($"Error converting file '{filename}': {ex.Message}");
System.Diagnostics.Trace.WriteLine($"Error converting file '{filename}': {ex.ToString()}");
}
}
static void ShowMetadata(IWICBitmapFrameDecode frame)
{
var metadataReader = frame.GetMetadataQueryReader();
foreach (var name in metadataReader.GetNamesRecursive())
{
var val = metadataReader.GetMetadataByName(name);
System.Diagnostics.Trace.WriteLine($"{name}: {GetValue(val)}");
}
}
static object GetValue(object val)
{
if (val is WICBlob)
return BitConverter.ToString(((WICBlob)val).Bytes);
if (val is Array)
return "[" + string.Join("][", ((Array)val).Cast<object>()) + "]";
return val;
}
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
static void AddRightClickMenu()
{
try
{
var path = Environment.ProcessPath; // System.Reflection.Assembly.GetEntryAssembly()?.Location;
var key = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("SystemFileAssociations\\.heic\\Shell\\Convert to JPG");
key.SetValue("NeverDefault", "", Microsoft.Win32.RegistryValueKind.String);
key = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("SystemFileAssociations\\.heic\\Shell\\Convert to JPG\\command");
key.SetValue(null, $"\"{path}\" \"%1\"", Microsoft.Win32.RegistryValueKind.String);
//var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Classes\\.heic", true);
//var heicClass = (string?)key.GetValue(null, string.Empty);
//if (string.IsNullOrEmpty(heicClass))
//{
// heicClass = "heicfile";
// key.SetValue(null, heicClass, Microsoft.Win32.RegistryValueKind.String);
//}
//key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Classes\\.heic\\OpenWithProgids", true);
//key.SetValue(heicClass, "", Microsoft.Win32.RegistryValueKind.String);
//key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey($"Software\\Classes\\{heicClass}\\Shell\\Convert to jpg\\command", true);
//key.SetValue(null, "\"heic2jpg.exe\" \"%1\"", Microsoft.Win32.RegistryValueKind.String);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine($"Error adding registry entries: {ex.Message}");
}
}
/// <summary>
/// A list of all System photo metadata properties
/// https://docs.microsoft.com/en-us/windows/win32/wic/system
/// </summary>
static string[] SystemProperties = {
"System.ApplicationName",
"System.Author",
"System.Comment",
"System.Copyright",
"System.DateAcquired",
"System.Keywords",
"System.Rating",
"System.SimpleRating",
"System.Subject",
"System.Title"
};
/// <summary>
/// A list of all the writable System.Photo.* properties.
/// Commented out properties are calculated or read-only.
/// </summary>
static string[] SystemPhotoProperties = {
//"System.Photo.Aperture",
"System.Photo.ApertureDenominator",
"System.Photo.ApertureNumerator",
//"System.Photo.Brightness",
"System.Photo.BrightnessDenominator",
"System.Photo.BrightnessNumerator",
"System.Photo.CameraManufacturer",
"System.Photo.CameraModel",
"System.Photo.CameraSerialNumber",
"System.Photo.Contrast",
//"System.Photo.ContrastText",
"System.Photo.DateTaken",
//"System.Photo.DigitalZoom",
"System.Photo.DigitalZoomDenominator",
"System.Photo.DigitalZoomNumerator",
"System.Photo.Event",
"System.Photo.EXIFVersion",
//"System.Photo.ExposureBias",
"System.Photo.ExposureBiasDenominator",
"System.Photo.ExposureBiasNumerator",
//"System.Photo.ExposureIndex",
"System.Photo.ExposureIndexDenominator",
"System.Photo.ExposureIndexNumerator",
"System.Photo.ExposureProgram",
//"System.Photo.ExposureProgramText",
//"System.Photo.ExposureTime",
"System.Photo.ExposureTimeDenominator",
"System.Photo.ExposureTimeNumerator",
"System.Photo.Flash",
//"System.Photo.FlashEnergy",
"System.Photo.FlashEnergyDenominator",
"System.Photo.FlashEnergyNumerator",
"System.Photo.FlashManufacturer",
"System.Photo.FlashModel",
//"System.Photo.FlashText",
//"System.Photo.FNumber",
"System.Photo.FNumberDenominator",
"System.Photo.FNumberNumerator",
//"System.Photo.FocalLength",
"System.Photo.FocalLengthDenominator",
"System.Photo.FocalLengthInFilm",
"System.Photo.FocalLengthNumerator",
//"System.Photo.FocalPlaneXResolution",
"System.Photo.FocalPlaneXResolutionDenominator",
"System.Photo.FocalPlaneXResolutionNumerator",
//"System.Photo.FocalPlaneYResolution",
"System.Photo.FocalPlaneYResolutionDenominator",
"System.Photo.FocalPlaneYResolutionNumerator",
//"System.Photo.GainControl",
"System.Photo.GainControlDenominator",
"System.Photo.GainControlNumerator",
//"System.Photo.GainControlText",
"System.Photo.ISOSpeed",
"System.Photo.LensManufacturer",
"System.Photo.LensModel",
"System.Photo.LightSource",
"System.Photo.MakerNote",
"System.Photo.MakerNoteOffset",
//"System.Photo.MaxAperture",
"System.Photo.MaxApertureDenominator",
"System.Photo.MaxApertureNumerator",
"System.Photo.MeteringMode",
//"System.Photo.MeteringModeText",
"System.Photo.Orientation",
//"System.Photo.OrientationText",
//"System.Photo.PeopleNames",
//"System.Photo.PhotometricInterpretation",
//"System.Photo.PhotometricInterpretationText",
"System.Photo.ProgramMode",
//"System.Photo.ProgramModeText",
//"System.Photo.RelatedSoundFile",
"System.Photo.Saturation",
//"System.Photo.SaturationText",
"System.Photo.Sharpness",
//"System.Photo.SharpnessText",
//"System.Photo.ShutterSpeed",
"System.Photo.ShutterSpeedDenominator",
"System.Photo.ShutterSpeedNumerator",
//"System.Photo.SubjectDistance",
"System.Photo.SubjectDistanceDenominator",
"System.Photo.SubjectDistanceNumerator",
//"System.Photo.TagViewAggregate",
"System.Photo.TranscodedForSync",
"System.Photo.WhiteBalance",
//"System.Photo.WhiteBalanceText"
};
/// <summary>
/// A list of all the writable System.Gps.* properties.
/// Commented out properties are calculated or read-only.
/// </summary>
static string[] SystemGpsProperties = {
//"System.GPS.Altitude",
"System.GPS.AltitudeDenominator",
"System.GPS.AltitudeNumerator",
"System.GPS.AltitudeRef",
"System.GPS.AreaInformation",
"System.GPS.Date",
//"System.GPS.DestBearing",
"System.GPS.DestBearingDenominator",
"System.GPS.DestBearingNumerator",
"System.GPS.DestBearingRef",
//"System.GPS.DestDistance",
"System.GPS.DestDistanceDenominator",
"System.GPS.DestDistanceNumerator",
"System.GPS.DestDistanceRef",
//"System.GPS.DestLatitude",
"System.GPS.DestLatitudeDenominator",
"System.GPS.DestLatitudeNumerator",
"System.GPS.DestLatitudeRef",
//"System.GPS.DestLongitude",
"System.GPS.DestLongitudeDenominator",
"System.GPS.DestLongitudeNumerator",
"System.GPS.DestLongitudeRef",
"System.GPS.Differential",
//"System.GPS.DOP",
"System.GPS.DOPDenominator",
"System.GPS.DOPNumerator",
//"System.GPS.ImgDirection",
"System.GPS.ImgDirectionDenominator",
"System.GPS.ImgDirectionNumerator",
"System.GPS.ImgDirectionRef",
//"System.GPS.Latitude",
//"System.GPS.LatitudeDecimal",
"System.GPS.LatitudeDenominator",
"System.GPS.LatitudeNumerator",
"System.GPS.LatitudeRef",
//"System.GPS.Longitude",
//"System.GPS.LongitudeDecimal",
"System.GPS.LongitudeDenominator",
"System.GPS.LongitudeNumerator",
"System.GPS.LongitudeRef",
"System.GPS.MapDatum",
"System.GPS.MeasureMode",
"System.GPS.ProcessingMethod",
"System.GPS.Satellites",
//"System.GPS.Speed",
"System.GPS.SpeedDenominator",
"System.GPS.SpeedNumerator",
"System.GPS.SpeedRef",
"System.GPS.Status",
//"System.GPS.Track",
"System.GPS.TrackDenominator",
"System.GPS.TrackNumerator",
"System.GPS.TrackRef",
"System.GPS.VersionID"
};
}
}