-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.axaml.cs
119 lines (97 loc) · 3.95 KB
/
MainWindow.axaml.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
using Avalonia.Controls;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Drawing.Drawing2D;
using NetVips;
using System;
using System.Collections.Generic;
using Avalonia.Animation;
namespace NetVipsAvalonia {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
GenBtn.Click += async (s, e) => {
var openFolderWindow = new OpenFolderDialog();
var folder = await openFolderWindow.ShowAsync(this);
if (folder is null) {
return;
}
StateLabel.Content = "Started...";
var imageFiles = Directory.GetFiles(folder);
var pngs = imageFiles.Where(filename => filename.EndsWith("png"))
.Select(filename => NetVips.Image.NewFromFile(filename))
.ToArray();
var framesCount = 0;
var gifFiles = imageFiles.Where(filename => filename.EndsWith("gif"));
List<NetVips.Image[]> gifsList = new();
foreach (var filename in gifFiles) {
var gif = NetVips.Image.Gifload(filename, -1);
Debug.WriteLine("Gif bands ==> " + gif.Bands);
var pages = (int)(gif.Height / gif.PageHeight);
var frames = new List<NetVips.Image>();
for (var i = 0; i < pages; i++) {
var frame =
gif.Crop(0, i * gif.PageHeight, gif.Width, gif.PageHeight);
frames.Add(frame);
}
if (framesCount == 0) {
framesCount = frames.Count;
} else {
framesCount = Math.Min(framesCount, frames.Count);
}
gifsList.Add(frames.ToArray());
}
var gifs = gifsList.ToArray();
DateTimeOffset now = (DateTimeOffset)DateTime.UtcNow;
var compositModes =
Enum.GetValues(typeof(Enums.BlendMode)).Cast<Enums.BlendMode>();
// var compositModes = new List<Enums.BlendMode>() { Enums.BlendMode.Over
// };
foreach (var mode in compositModes) {
var delays = new List<int>();
var generatedFramesList = new List<NetVips.Image>();
for (var i = 0; i < framesCount; i++) {
delays.Add(100);
var baseImage = pngs[0];
var forLayering = new List<NetVips.Image>();
var blendModes = new List<Enums.BlendMode>();
for (var j = 1; j < pngs.Length; j++) {
forLayering.Add(pngs[j]);
blendModes.Add(Enums.BlendMode.Over);
}
foreach (var gif in gifs) {
forLayering.Add(gif[i]);
blendModes.Add(mode);
}
var blendedFrame =
baseImage.Composite(forLayering.ToArray(), blendModes.ToArray());
generatedFramesList.Add(blendedFrame);
}
var generatedFrames = generatedFramesList.ToArray();
var framesJoined = NetVips.Image.Arrayjoin(generatedFrames, 1);
var outputFileName = now.ToUnixTimeMilliseconds() + "-" + mode + ".gif";
var filepath = Path.Join(folder, "out", outputFileName);
var filepathJoined = filepath + "-joined.png";
framesJoined.Pngsave(filepathJoined);
var height = generatedFrames[0].Height;
var framesWithMeta = framesJoined.Mutate(mutable => {
// Set the number of loops, libvips uses iterations like this:
// 0 - set 0 loops (infinite)
// 1 - loop once
// 2 - loop twice etc.
mutable.Set(GValue.GIntType, "loop", 0);
// Set the frame delay(s)
// We use a 1000ms delay for all frames in this example
mutable.Set(GValue.ArrayIntType, "delay", delays.ToArray());
// Set page height
mutable.Set(GValue.GIntType, "page-height", height);
});
framesWithMeta.Gifsave(filepath, dither: 0.5, effort: 1,
bitdepth: 8, reoptimise: true);
}
StateLabel.Content = "Wrote to folder: " + Path.Join(folder, "out");
};
}
}
}