-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConsoleSlicer.cs
121 lines (103 loc) · 5.12 KB
/
ConsoleSlicer.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using ImageProcessor;
using log4net;
using Newtonsoft.Json;
namespace AnimateSpriteSheetSlicer {
internal class ConsoleSlicer {
private static readonly ILog Log = LogManager.GetLogger("ConsoleSlicer");
private readonly string[] _args;
private ConsoleSlicer(string[] args) {
_args = args;
}
public static void Run(string[] args) {
ConsoleSlicer consoleSlicer = new ConsoleSlicer(args);
consoleSlicer.Run();
}
private void Run() {
if (_args.Length == 0) {
Log.Info("Usage: AnimateSpriteSheetSlicer <JSON-Array file path> <output directory>");
Environment.ExitCode = 1;
return;
}
string jsonFilePath = _args[0];
if (!File.Exists(jsonFilePath)) {
Log.FatalFormat("JSON file '{0}' not found", jsonFilePath);
Environment.ExitCode = 1;
return;
}
Log.InfoFormat("Reading JSON file '{0}'", jsonFilePath);
string jsonFile = File.ReadAllText(jsonFilePath);
dynamic spriteSheetData = JsonConvert.DeserializeObject(jsonFile);
string spriteSheetImageFileName = spriteSheetData["meta"]["image"];
dynamic spriteSheetFramesData = spriteSheetData["frames"];
List<FrameData> spriteSheetFrames = new List<FrameData>();
foreach (dynamic spriteSheetFrameData in spriteSheetFramesData) {
string name = spriteSheetFrameData["filename"];
// Clean up the name a bit
int tmp = name.IndexOf(" instance");
if (tmp != -1) {
name = name.Substring(0, tmp);
}
name = name.Trim();
spriteSheetFrames.Add(
new FrameData(
name,
new Rectangle(
(int) spriteSheetFrameData["frame"]["x"],
(int) spriteSheetFrameData["frame"]["y"],
(int) spriteSheetFrameData["frame"]["w"],
(int) spriteSheetFrameData["frame"]["h"]
)
));
}
Log.InfoFormat("Found {0} frames", spriteSheetFrames.Count);
Log.InfoFormat("Reading image file '{0}'", spriteSheetImageFileName);
string jsonFileDirectory = Path.GetDirectoryName(jsonFilePath);
spriteSheetImageFileName = Path.Combine(jsonFileDirectory, spriteSheetImageFileName);
string jsonFileName = Path.GetFileNameWithoutExtension(jsonFilePath);
string outputDirectory = _args.Length >= 2 ? _args[1] : Path.Combine(jsonFileDirectory, jsonFileName);
Directory.CreateDirectory(outputDirectory);
if (!File.Exists(spriteSheetImageFileName)) {
Log.FatalFormat("Image file '{0}' not found", spriteSheetImageFileName);
Environment.ExitCode = 1;
return;
}
byte[] spriteSheetImageBytes = File.ReadAllBytes(spriteSheetImageFileName);
using (MemoryStream inStream = new MemoryStream(spriteSheetImageBytes)) {
using (ImageFactory imageFactory = new ImageFactory()) {
using (ImageFactory spriteSheetImageSource = imageFactory.Load(inStream)) {
using (Image clonedSpriteSheetImage = (Image) imageFactory.Image.Clone()) {
for (int i = 0; i < spriteSheetFrames.Count; i++) {
FrameData spriteSheetFrame = spriteSheetFrames[i];
using (ImageFactory spriteSheetImage = new ImageFactory().Load(clonedSpriteSheetImage)) {
using (ImageFactory frameImage = spriteSheetImage.Crop(spriteSheetFrame.Rect)) {
string frameFilePath =
Path.Combine(
outputDirectory,
spriteSheetFrame.Name + "." + spriteSheetImageSource.CurrentImageFormat.DefaultExtension
);
Log.InfoFormat("Saving frame file {0}/{1} ('{2}')", i + 1, spriteSheetFrames.Count, spriteSheetFrame.Name);
frameImage
.Format(spriteSheetImageSource.CurrentImageFormat)
.Save(frameFilePath);
}
}
}
}
}
}
}
}
private struct FrameData {
public readonly string Name;
public readonly Rectangle Rect;
public FrameData(string name, Rectangle rect) {
Name = name;
Rect = rect;
}
}
}
}