├── .github
└── FUNDING.yml
├── DigimonColorSpriteTool
├── ImageType.cs
├── ImageInfo.cs
├── DigimonColorSpriteTool.csproj
├── ImageConverter.cs
├── FirmwareInfo.cs
├── Program.cs
└── ImageImportExport.cs
├── DigimonColorSpriteTool.sln
├── .gitattributes
├── README.md
└── .gitignore
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | ko_fi: caralynx
2 |
--------------------------------------------------------------------------------
/DigimonColorSpriteTool/ImageType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace DigimonColorSpriteTool
8 | {
9 | internal enum ImageType
10 | {
11 | CharacterSprite,
12 | Cutin,
13 | Name,
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/DigimonColorSpriteTool/ImageInfo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace DigimonColorSpriteTool
8 | {
9 | internal class ImageInfo
10 | {
11 | public ushort Width { get; set; }
12 | public ushort Height { get; set; }
13 | public int DataOffset { get; set; }
14 | public string? FilePath { get; set; }
15 | public byte[]? OverrideData { get; set; } // Override data takes precedence over FilePath
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/DigimonColorSpriteTool/DigimonColorSpriteTool.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net8.0
6 | enable
7 | enable
8 | Digimon Color Sprite Import/Export Tool
9 | cyanic
10 | 2.2.0
11 | https://github.com/GMMan/DigimonColorSpriteTool
12 | README.md
13 | $(Title)
14 | $(RepositoryUrl)
15 |
16 |
17 |
18 |
19 | True
20 | \
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/DigimonColorSpriteTool.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.4.33213.308
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigimonColorSpriteTool", "DigimonColorSpriteTool\DigimonColorSpriteTool.csproj", "{078EDDD0-7B1C-4822-A0F2-D2659D1217CC}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9E2D1D13-2F35-40F9-986F-E98CEF9F7B90}"
9 | ProjectSection(SolutionItems) = preProject
10 | README.md = README.md
11 | EndProjectSection
12 | EndProject
13 | Global
14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
15 | Debug|Any CPU = Debug|Any CPU
16 | Release|Any CPU = Release|Any CPU
17 | EndGlobalSection
18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
19 | {078EDDD0-7B1C-4822-A0F2-D2659D1217CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20 | {078EDDD0-7B1C-4822-A0F2-D2659D1217CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
21 | {078EDDD0-7B1C-4822-A0F2-D2659D1217CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
22 | {078EDDD0-7B1C-4822-A0F2-D2659D1217CC}.Release|Any CPU.Build.0 = Release|Any CPU
23 | EndGlobalSection
24 | GlobalSection(SolutionProperties) = preSolution
25 | HideSolutionNode = FALSE
26 | EndGlobalSection
27 | GlobalSection(ExtensibilityGlobals) = postSolution
28 | SolutionGuid = {691A1CA8-9943-4CEB-8548-3A703C49D354}
29 | EndGlobalSection
30 | EndGlobal
31 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/DigimonColorSpriteTool/ImageConverter.cs:
--------------------------------------------------------------------------------
1 | using SixLabors.ImageSharp;
2 | using SixLabors.ImageSharp.PixelFormats;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace DigimonColorSpriteTool
10 | {
11 | public static class ImageConverter
12 | {
13 | public static Image ConvertRgb565ToImage(byte[] pixels, int width, int height, bool useGreenAsAlpha)
14 | {
15 | if (pixels == null) throw new ArgumentNullException(nameof(pixels));
16 | if (width < 0) throw new ArgumentOutOfRangeException(nameof(width), "Width cannot be negative.");
17 | if (height < 0) throw new ArgumentOutOfRangeException(nameof(height), "Height cannot be negative.");
18 | if (pixels.Length != width * height * 2) throw new ArgumentException("Image size is incorrect.");
19 |
20 | // Modified ConvertImage565 with alpha
21 | int i = 0;
22 | var img = new Image(width, height);
23 | img.ProcessPixelRows(proc =>
24 | {
25 | for (int y = 0; y < proc.Height; ++y)
26 | {
27 | var row = proc.GetRowSpan(y);
28 | for (int x = 0; x < proc.Width; ++x)
29 | {
30 | ushort c = (ushort)(pixels[i++] | (pixels[i++] << 8));
31 | float b = ((c >> 0) & 0x1f) / 31f;
32 | float g = ((c >> 5) & 0x3f) / 63f;
33 | float r = ((c >> 11) & 0x1f) / 31f;
34 | float a = 1;
35 | if (!useGreenAsAlpha && b == 0 && g == 1 && r == 0)
36 | a = 0;
37 | row[x] = new Rgba32(r, g, b, a);
38 | }
39 | }
40 | });
41 | return img;
42 | }
43 |
44 | public static byte[] ConvertImageToRgb565(Image img, bool useGreenAsAlpha)
45 | {
46 | if (img == null) throw new ArgumentNullException(nameof(img));
47 |
48 | var rgbImg = img.CloneAs();
49 | using (MemoryStream ms = new MemoryStream())
50 | {
51 | BinaryWriter bw = new BinaryWriter(ms);
52 | rgbImg.ProcessPixelRows(proc =>
53 | {
54 | for (int y = 0; y < proc.Height; ++y)
55 | {
56 | var row = proc.GetRowSpan(y);
57 | for (int x = 0; x < proc.Width; ++x)
58 | {
59 | var pixel = row[x].ToScaledVector4();
60 | int r = (int)Math.Round(pixel.X * 31);
61 | int g = (int)Math.Round(pixel.Y * 63);
62 | int b = (int)Math.Round(pixel.Z * 31);
63 | if (pixel.W == 0)
64 | {
65 | r = 0;
66 | g = 63;
67 | b = 0;
68 | }
69 | else if (!useGreenAsAlpha && r == 0 && g == 63 && b == 0)
70 | {
71 | --g;
72 | }
73 |
74 | ushort c = (ushort)((r << 11) | (g << 5) | b);
75 | bw.Write(c);
76 | }
77 | }
78 | });
79 | ms.Flush();
80 | return ms.ToArray();
81 | }
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Digimon Color Sprite Import/Export Tool
2 | =======================================
3 |
4 | This tool allows you to easily extract and reimport sprites for Digimon Color
5 | and Pendulum Color. It also supports exporting/importing as sprite sheets for
6 | character sprites, and also accepts sprites with different dimensions than the
7 | original.
8 |
9 | [](https://ko-fi.com/C0C81P4PX)
10 |
11 | Usage
12 | -----
13 |
14 | ### Export sprites
15 |
16 | ```
17 | DigimonColorSpriteTool.exe export-preset [options]
18 | ```
19 |
20 | Where:
21 | - `romPath`: Path to flash dump
22 | - `version`: Flash dump version, one of `dmc1`, `dmc2`, `dmc3`, `dmc4`, `dmc5`, `dmcmh`, `dmcgz`
23 | for Digimon Color versions, and `penc1`, `penc2`, `penc3`, `penc4`, `penc5`,
24 | `penc0` for Pendulum Color versions
25 | - `outDir`: The directory to export into
26 |
27 | Files are exported as PNG files into `outDir` with transparent background.
28 | You can export with green background by using the `-g` option, and to BMP with
29 | the `--bmp` option.
30 |
31 | ### Import sprites
32 |
33 | ```
34 | DigimonColorSpriteTool.exe import-preset [options] []
35 | ```
36 |
37 | `inDir` is a path to the folder that contains the sprites to import. You
38 | only need to provide the sprites you want to overwrite, named with the correct
39 | index. `outFile` is the path to the repacked ROM file. If omitted, the
40 | original file will be overwritten. Use the `-g` option if your backgrounds are
41 | green. Otherwise, any pure green pixels will be slightly adjusted so they show
42 | up as green on-device. BMPs will be imported with the `-g` option applied.
43 |
44 | ### Export sprite sheet
45 |
46 | ```
47 | DigimonColorSpriteTool.exe export-sheets-preset [options]
48 | ```
49 |
50 | Sprite sheets consist of a vertical column of character sprites. You can edit
51 | and reimport sheets. Same options as previous apply. If the characters have name
52 | sprites, it will be stored under the same name and extension as the sprite sheet
53 | except with `_name` appended to the name part, i.e. `4.png` would have a name
54 | counterpart named `4_name.png`.
55 |
56 |
57 | ### Import sprite sheet
58 |
59 | ```
60 | DigimonColorSpriteTool.exe import-sheets-preset [options] []
61 | ```
62 |
63 | Additional options:
64 | - `-g`: Same as before
65 | - `-sr`: Number of rows in sprite sheet
66 | - `-sc`: Number of columns in sprite sheet
67 | - `--tortoiseshel`: Sets columns and rows to 3x4
68 |
69 | When importing sprite sheets, by default it uses 1 column and the same number of
70 | rows as there are sprite frames per character. You can change this with `-sr`
71 | and `-sc` options. Note that there needs to be enough frames for each character,
72 | and each frame must be smaller or equal to 48x48, and can be scaled up to 48x48
73 | by whole multiples. For example, 16x16 frames can be scaled up, but 20x20 frames
74 | cannot be, and 64x64 frames are too large. All sheets in the folder must use the
75 | same layout. You can omit sheets for characters that you do not want to replace.
76 | If name sprites are supported, make sure they are named in the way described in
77 | the previous section. If the name sprite is omitted or improperly named, it will
78 | not be updated in the resulting file.
79 |
80 | ### Special characters
81 |
82 | Certain Digimon Color variants may have characters with additional sprites.
83 | These are designated as "special characters" and their indexes may be specified
84 | with one or more `-sp` options followed by the index. For example, if index 0,
85 | 2, and 9 are special, you would add the options `-sp 0 -sp 2 -sp 9`. This option
86 | is available for sprite sheet related commands: `export-sheets-preset`,
87 | `import-sheets-preset`, `export-sheets`, `import-sheets`.
88 |
89 | ### Cut-ins
90 |
91 | For devices that support cut-ins, they are placed after regular sprites and
92 | also after special sprites. If the character is not special, the exported name
93 | for sprite sheets will have the character index plus `_cutin` appended for the
94 | name. If the character is special, the above applies, plus an additional
95 | 0-based index is added to the name, which corresponds to its location within
96 | the sprite order.
97 |
98 | Example: for a special character with index `0`, you will get `0.png`,
99 | `0_cutin0.png`, and `0_cutin1.png`.
100 |
101 | When counting number of sprites for specifying on the command line, do not
102 | include cut-ins or names in the count.
103 |
104 | ### Non-preset commands
105 |
106 | Each command is also available in non-preset versions. See usage help in-program
107 | for the commands and their arguments. An explanation of the arguments is as
108 | follows:
109 |
110 | - `spritePackBase`: Flash offset of the beginning of the sprite data. It is
111 | `524288` for DMC, and `4194304` for PenC.
112 | - `sizeTableOffset`: Flash offset of the sprite sizes table
113 | - `numImages`: Total number of images in sprite data
114 | - `numCharas`: Number of characters in sprite data. This is the number of full
115 | sprite frame sets, not including single-frame jogress characters
116 | - `numFramesPerChara`: Number of frames per character. `15` for DMC, `12` for
117 | PenC
118 | - `charaStartIndex`: Start index of character sprites. `210` for DMC, `240` for
119 | PenC
120 | - `-j`: Number of jogress characters. If jogress character images are
121 | interspersed before full set characters, indicate the number of such jogress
122 | characters. Do not include jogress characters after full set characters
123 | - `--has-name`: Each character has a name sprite following the character frames
124 | - `-nfs`: Number of frames for special characters
125 | - `-sp`: Specifies the index of a special character. Can be repeated multiple
126 | times
127 |
128 | You can print the values for those arguments from presets using the
129 | `show-preset` command:
130 |
131 | ```
132 | DigimonColorSpriteTool.exe show-preset
133 | ```
134 |
135 | For special character indexes, specify each separately using the `-sp` option.
136 |
--------------------------------------------------------------------------------
/DigimonColorSpriteTool/FirmwareInfo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace DigimonColorSpriteTool
8 | {
9 | public class FirmwareInfo
10 | {
11 | public uint SpritePackBase { get; set; }
12 | public uint CharaSpriteWidth { get; set; } = 48;
13 | public uint CharaSpriteHeight { get; set; } = 48;
14 | public uint SizeTableOffset { get; set; }
15 | public uint NumImages { get; set; }
16 | public uint NumCharas { get; set; }
17 | public uint NumFramesPerChara { get; set; }
18 | public uint CharasStartIndex { get; set; }
19 | public uint NumJogressCharas { get; set; }
20 | public bool HasName { get; set; }
21 | public bool HasCutin { get; set; }
22 | public uint NumFramesPerSpecialChara { get; set; }
23 | public uint[] SpecialCharaIndexes { get; set; } = [];
24 |
25 | public static readonly Dictionary Presets = new()
26 | {
27 | { "dmc1", new FirmwareInfo
28 | {
29 | SpritePackBase = 0x80000,
30 | SizeTableOffset = 38296,
31 | NumImages = 597,
32 | NumCharas = 18,
33 | NumFramesPerChara = 15,
34 | CharasStartIndex = 210,
35 | NumJogressCharas = 0,
36 | }
37 | },
38 | { "dmc2", new FirmwareInfo
39 | {
40 | SpritePackBase = 0x80000,
41 | SizeTableOffset = 40346,
42 | NumImages = 597,
43 | NumCharas = 18,
44 | NumFramesPerChara = 15,
45 | CharasStartIndex = 210,
46 | NumJogressCharas = 1,
47 | }
48 | },
49 | { "dmc3", new FirmwareInfo
50 | {
51 | SpritePackBase = 0x80000,
52 | SizeTableOffset = 38632,
53 | NumImages = 628,
54 | NumCharas = 20,
55 | NumFramesPerChara = 15,
56 | CharasStartIndex = 210,
57 | NumJogressCharas = 0,
58 | }
59 | },
60 | { "dmc4", new FirmwareInfo
61 | {
62 | SpritePackBase = 0x80000,
63 | SizeTableOffset = 41032,
64 | NumImages = 613,
65 | NumCharas = 19,
66 | NumFramesPerChara = 15,
67 | CharasStartIndex = 210,
68 | NumJogressCharas = 1,
69 | }
70 | },
71 | { "dmc5", new FirmwareInfo
72 | {
73 | SpritePackBase = 0x80000,
74 | SizeTableOffset = 38592,
75 | NumImages = 613,
76 | NumCharas = 19,
77 | NumFramesPerChara = 15,
78 | CharasStartIndex = 210,
79 | NumJogressCharas = 2,
80 | }
81 | },
82 | { "dmcmh", new FirmwareInfo
83 | {
84 | SpritePackBase = 0x400000,
85 | SizeTableOffset = 49662,
86 | NumImages = 938,
87 | NumCharas = 38,
88 | NumFramesPerChara = 15,
89 | CharasStartIndex = 200,
90 | NumJogressCharas = 0,
91 | HasName = true,
92 | NumFramesPerSpecialChara = 20,
93 | SpecialCharaIndexes = [29, 31, 34]
94 | }
95 | },
96 | { "dmcgz", new FirmwareInfo
97 | {
98 | SpritePackBase = 0x400000,
99 | SizeTableOffset = 49148,
100 | NumImages = 853,
101 | NumCharas = 34,
102 | NumFramesPerChara = 13,
103 | CharasStartIndex = 219,
104 | NumJogressCharas = 0,
105 | HasCutin = true,
106 | NumFramesPerSpecialChara = 18,
107 | SpecialCharaIndexes = [6, 15, 16, 25, 29]
108 | }
109 | },
110 | { "penc1", new FirmwareInfo
111 | {
112 | SpritePackBase = 0x400000,
113 | SizeTableOffset = 64796,
114 | NumImages = 759,
115 | NumCharas = 32,
116 | NumFramesPerChara = 12,
117 | CharasStartIndex = 240,
118 | NumJogressCharas = 0,
119 | }
120 | },
121 | { "penc2", new FirmwareInfo
122 | {
123 | SpritePackBase = 0x400000,
124 | SizeTableOffset = 64730,
125 | NumImages = 759,
126 | NumCharas = 32,
127 | NumFramesPerChara = 12,
128 | CharasStartIndex = 240,
129 | NumJogressCharas = 0,
130 | }
131 | },
132 | { "penc3", new FirmwareInfo
133 | {
134 | SpritePackBase = 0x400000,
135 | SizeTableOffset = 64736,
136 | NumImages = 759,
137 | NumCharas = 32,
138 | NumFramesPerChara = 12,
139 | CharasStartIndex = 240,
140 | NumJogressCharas = 0,
141 | }
142 | },
143 | { "penc4", new FirmwareInfo
144 | {
145 | SpritePackBase = 0x400000,
146 | SizeTableOffset = 65932,
147 | NumImages = 759,
148 | NumCharas = 32,
149 | NumFramesPerChara = 12,
150 | CharasStartIndex = 240,
151 | NumJogressCharas = 0,
152 | }
153 | },
154 | { "penc5", new FirmwareInfo
155 | {
156 | SpritePackBase = 0x400000,
157 | SizeTableOffset = 65944,
158 | NumImages = 759,
159 | NumCharas = 32,
160 | NumFramesPerChara = 12,
161 | CharasStartIndex = 240,
162 | NumJogressCharas = 0,
163 | }
164 | },
165 | { "penc0", new FirmwareInfo
166 | {
167 | SpritePackBase = 0x400000,
168 | SizeTableOffset = 66158,
169 | NumImages = 771,
170 | NumCharas = 33,
171 | NumFramesPerChara = 12,
172 | CharasStartIndex = 240,
173 | NumJogressCharas = 0,
174 | }
175 | },
176 | };
177 | }
178 | }
179 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Ww][Ii][Nn]32/
27 | [Aa][Rr][Mm]/
28 | [Aa][Rr][Mm]64/
29 | bld/
30 | [Bb]in/
31 | [Oo]bj/
32 | [Oo]ut/
33 | [Ll]og/
34 | [Ll]ogs/
35 |
36 | # Visual Studio 2015/2017 cache/options directory
37 | .vs/
38 | # Uncomment if you have tasks that create the project's static files in wwwroot
39 | #wwwroot/
40 |
41 | # Visual Studio 2017 auto generated files
42 | Generated\ Files/
43 |
44 | # MSTest test Results
45 | [Tt]est[Rr]esult*/
46 | [Bb]uild[Ll]og.*
47 |
48 | # NUnit
49 | *.VisualState.xml
50 | TestResult.xml
51 | nunit-*.xml
52 |
53 | # Build Results of an ATL Project
54 | [Dd]ebugPS/
55 | [Rr]eleasePS/
56 | dlldata.c
57 |
58 | # Benchmark Results
59 | BenchmarkDotNet.Artifacts/
60 |
61 | # .NET Core
62 | project.lock.json
63 | project.fragment.lock.json
64 | artifacts/
65 |
66 | # ASP.NET Scaffolding
67 | ScaffoldingReadMe.txt
68 |
69 | # StyleCop
70 | StyleCopReport.xml
71 |
72 | # Files built by Visual Studio
73 | *_i.c
74 | *_p.c
75 | *_h.h
76 | *.ilk
77 | *.meta
78 | *.obj
79 | *.iobj
80 | *.pch
81 | *.pdb
82 | *.ipdb
83 | *.pgc
84 | *.pgd
85 | *.rsp
86 | *.sbr
87 | *.tlb
88 | *.tli
89 | *.tlh
90 | *.tmp
91 | *.tmp_proj
92 | *_wpftmp.csproj
93 | *.log
94 | *.vspscc
95 | *.vssscc
96 | .builds
97 | *.pidb
98 | *.svclog
99 | *.scc
100 |
101 | # Chutzpah Test files
102 | _Chutzpah*
103 |
104 | # Visual C++ cache files
105 | ipch/
106 | *.aps
107 | *.ncb
108 | *.opendb
109 | *.opensdf
110 | *.sdf
111 | *.cachefile
112 | *.VC.db
113 | *.VC.VC.opendb
114 |
115 | # Visual Studio profiler
116 | *.psess
117 | *.vsp
118 | *.vspx
119 | *.sap
120 |
121 | # Visual Studio Trace Files
122 | *.e2e
123 |
124 | # TFS 2012 Local Workspace
125 | $tf/
126 |
127 | # Guidance Automation Toolkit
128 | *.gpState
129 |
130 | # ReSharper is a .NET coding add-in
131 | _ReSharper*/
132 | *.[Rr]e[Ss]harper
133 | *.DotSettings.user
134 |
135 | # TeamCity is a build add-in
136 | _TeamCity*
137 |
138 | # DotCover is a Code Coverage Tool
139 | *.dotCover
140 |
141 | # AxoCover is a Code Coverage Tool
142 | .axoCover/*
143 | !.axoCover/settings.json
144 |
145 | # Coverlet is a free, cross platform Code Coverage Tool
146 | coverage*.json
147 | coverage*.xml
148 | coverage*.info
149 |
150 | # Visual Studio code coverage results
151 | *.coverage
152 | *.coveragexml
153 |
154 | # NCrunch
155 | _NCrunch_*
156 | .*crunch*.local.xml
157 | nCrunchTemp_*
158 |
159 | # MightyMoose
160 | *.mm.*
161 | AutoTest.Net/
162 |
163 | # Web workbench (sass)
164 | .sass-cache/
165 |
166 | # Installshield output folder
167 | [Ee]xpress/
168 |
169 | # DocProject is a documentation generator add-in
170 | DocProject/buildhelp/
171 | DocProject/Help/*.HxT
172 | DocProject/Help/*.HxC
173 | DocProject/Help/*.hhc
174 | DocProject/Help/*.hhk
175 | DocProject/Help/*.hhp
176 | DocProject/Help/Html2
177 | DocProject/Help/html
178 |
179 | # Click-Once directory
180 | publish/
181 |
182 | # Publish Web Output
183 | *.[Pp]ublish.xml
184 | *.azurePubxml
185 | # Note: Comment the next line if you want to checkin your web deploy settings,
186 | # but database connection strings (with potential passwords) will be unencrypted
187 | *.pubxml
188 | *.publishproj
189 |
190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
191 | # checkin your Azure Web App publish settings, but sensitive information contained
192 | # in these scripts will be unencrypted
193 | PublishScripts/
194 |
195 | # NuGet Packages
196 | *.nupkg
197 | # NuGet Symbol Packages
198 | *.snupkg
199 | # The packages folder can be ignored because of Package Restore
200 | **/[Pp]ackages/*
201 | # except build/, which is used as an MSBuild target.
202 | !**/[Pp]ackages/build/
203 | # Uncomment if necessary however generally it will be regenerated when needed
204 | #!**/[Pp]ackages/repositories.config
205 | # NuGet v3's project.json files produces more ignorable files
206 | *.nuget.props
207 | *.nuget.targets
208 |
209 | # Microsoft Azure Build Output
210 | csx/
211 | *.build.csdef
212 |
213 | # Microsoft Azure Emulator
214 | ecf/
215 | rcf/
216 |
217 | # Windows Store app package directories and files
218 | AppPackages/
219 | BundleArtifacts/
220 | Package.StoreAssociation.xml
221 | _pkginfo.txt
222 | *.appx
223 | *.appxbundle
224 | *.appxupload
225 |
226 | # Visual Studio cache files
227 | # files ending in .cache can be ignored
228 | *.[Cc]ache
229 | # but keep track of directories ending in .cache
230 | !?*.[Cc]ache/
231 |
232 | # Others
233 | ClientBin/
234 | ~$*
235 | *~
236 | *.dbmdl
237 | *.dbproj.schemaview
238 | *.jfm
239 | *.pfx
240 | *.publishsettings
241 | orleans.codegen.cs
242 |
243 | # Including strong name files can present a security risk
244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
245 | #*.snk
246 |
247 | # Since there are multiple workflows, uncomment next line to ignore bower_components
248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
249 | #bower_components/
250 |
251 | # RIA/Silverlight projects
252 | Generated_Code/
253 |
254 | # Backup & report files from converting an old project file
255 | # to a newer Visual Studio version. Backup files are not needed,
256 | # because we have git ;-)
257 | _UpgradeReport_Files/
258 | Backup*/
259 | UpgradeLog*.XML
260 | UpgradeLog*.htm
261 | ServiceFabricBackup/
262 | *.rptproj.bak
263 |
264 | # SQL Server files
265 | *.mdf
266 | *.ldf
267 | *.ndf
268 |
269 | # Business Intelligence projects
270 | *.rdl.data
271 | *.bim.layout
272 | *.bim_*.settings
273 | *.rptproj.rsuser
274 | *- [Bb]ackup.rdl
275 | *- [Bb]ackup ([0-9]).rdl
276 | *- [Bb]ackup ([0-9][0-9]).rdl
277 |
278 | # Microsoft Fakes
279 | FakesAssemblies/
280 |
281 | # GhostDoc plugin setting file
282 | *.GhostDoc.xml
283 |
284 | # Node.js Tools for Visual Studio
285 | .ntvs_analysis.dat
286 | node_modules/
287 |
288 | # Visual Studio 6 build log
289 | *.plg
290 |
291 | # Visual Studio 6 workspace options file
292 | *.opt
293 |
294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
295 | *.vbw
296 |
297 | # Visual Studio LightSwitch build output
298 | **/*.HTMLClient/GeneratedArtifacts
299 | **/*.DesktopClient/GeneratedArtifacts
300 | **/*.DesktopClient/ModelManifest.xml
301 | **/*.Server/GeneratedArtifacts
302 | **/*.Server/ModelManifest.xml
303 | _Pvt_Extensions
304 |
305 | # Paket dependency manager
306 | .paket/paket.exe
307 | paket-files/
308 |
309 | # FAKE - F# Make
310 | .fake/
311 |
312 | # CodeRush personal settings
313 | .cr/personal
314 |
315 | # Python Tools for Visual Studio (PTVS)
316 | __pycache__/
317 | *.pyc
318 |
319 | # Cake - Uncomment if you are using it
320 | # tools/**
321 | # !tools/packages.config
322 |
323 | # Tabs Studio
324 | *.tss
325 |
326 | # Telerik's JustMock configuration file
327 | *.jmconfig
328 |
329 | # BizTalk build output
330 | *.btp.cs
331 | *.btm.cs
332 | *.odx.cs
333 | *.xsd.cs
334 |
335 | # OpenCover UI analysis results
336 | OpenCover/
337 |
338 | # Azure Stream Analytics local run output
339 | ASALocalRun/
340 |
341 | # MSBuild Binary and Structured Log
342 | *.binlog
343 |
344 | # NVidia Nsight GPU debugger configuration file
345 | *.nvuser
346 |
347 | # MFractors (Xamarin productivity tool) working folder
348 | .mfractor/
349 |
350 | # Local History for Visual Studio
351 | .localhistory/
352 |
353 | # BeatPulse healthcheck temp database
354 | healthchecksdb
355 |
356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
357 | MigrationBackup/
358 |
359 | # Ionide (cross platform F# VS Code tools) working folder
360 | .ionide/
361 |
362 | # Fody - auto-generated XML schema
363 | FodyWeavers.xsd
--------------------------------------------------------------------------------
/DigimonColorSpriteTool/Program.cs:
--------------------------------------------------------------------------------
1 | // See https://aka.ms/new-console-template for more information
2 |
3 | using DigimonColorSpriteTool;
4 | using System.CommandLine;
5 | using System.CommandLine.Parsing;
6 |
7 | var rootCommand = new RootCommand("Digimon Color Sprite Import/Export Tool");
8 |
9 | // Options
10 | var useGreenAsAlphaOption = new Option("--green-transparency", "Use full green pixel as transparency");
11 | useGreenAsAlphaOption.AddAlias("-g");
12 | var useBmpOption = new Option("--bmp", "Output as BMP");
13 | var numJogressOption = new Option("--num-jogress", () => 0, "Number of jogresses");
14 | numJogressOption.AddAlias("-j");
15 | var sheetRowsOption = new Option("--sheet-rows", "Number of rows in sprite sheet");
16 | sheetRowsOption.ArgumentHelpName = "row";
17 | sheetRowsOption.AddAlias("-sr");
18 | var sheetColsOption = new Option("--sheet-cols", "Number of columns in sprite sheet");
19 | sheetColsOption.ArgumentHelpName = "cols";
20 | sheetColsOption.AddAlias("-sc");
21 | var tortoiseshelOption = new Option("--tortoiseshel", "Use Tortoiseshel sprite sheet layout");
22 | var hasNameOption = new Option("--has-name", "Each character has a name sprite at the end");
23 | var numFramesPerSpecialCharaOption = new Option("--num-frames-per-special-chara", () => 0, "Number of frames for special characters");
24 | numFramesPerSpecialCharaOption.ArgumentHelpName = "frames";
25 | numFramesPerSpecialCharaOption.AddAlias("-nfs");
26 | var specialCharaIndexesOption = new Option("--special-chara", "List of special character indexes");
27 | specialCharaIndexesOption.ArgumentHelpName = "index";
28 | specialCharaIndexesOption.AddAlias("-sp");
29 |
30 | // Arguments
31 | var presetNameArgument = new Argument("presetName", "Device firmware preset name")
32 | .FromAmong(FirmwareInfo.Presets.Keys.ToArray());
33 | var spritePackBaseArgument = new Argument("spritePackBase", "Sprite pack offset in flash");
34 | var sizeTableOffsetArgument = new Argument("sizeTableOffset", "Size table offset");
35 | var numImagesArgument = new Argument("numImages", "Number of images in ROM");
36 | var numCharasArgument = new Argument("numCharas", "Number of characters in ROM");
37 | var numFramesPerCharaArgument = new Argument("numFramesPerChara", "Number of frames per character");
38 | var charaStartIndexArgument = new Argument("charaStartIndex", "Index of first character sprite");
39 |
40 | var romPathArgument = new Argument("romPath", "Path to flash dump").ExistingOnly();
41 | var outDirArgument = new Argument("outDir", "Output directory").LegalFilePathsOnly();
42 | var outFileArgument = new Argument("outFile", "Output file").LegalFilePathsOnly();
43 | outFileArgument.Arity = ArgumentArity.ZeroOrOne;
44 | var inDirArgument = new Argument("inDir", "Input directory").ExistingOnly();
45 |
46 | #region Command handlers
47 | void DoExport(FileInfo romFile, FirmwareInfo fwInfo, DirectoryInfo outDir, bool useBmp, bool useGreenAsAlpha)
48 | {
49 | if (useBmp) useGreenAsAlpha = true;
50 | using var impExp = new ImageImportExport(romFile.OpenRead(), fwInfo);
51 | outDir.Create();
52 | impExp.ExportAllImages(outDir.FullName, useBmp ? ".bmp" : ".png", useGreenAsAlpha);
53 | }
54 |
55 | void DoImport(FileInfo romFile, FirmwareInfo fwInfo, DirectoryInfo inDir, FileInfo? outFile, bool useGreenAsAlpha)
56 | {
57 | bool needCopyOverSrc = outFile == null || outFile.FullName == romFile.FullName; // Not foolproof
58 | if (needCopyOverSrc) outFile = new FileInfo(Path.GetTempFileName());
59 | using (var impExp = new ImageImportExport(romFile.OpenRead(), fwInfo))
60 | {
61 | impExp.SetOverridesByFolder(inDir.FullName);
62 | impExp.Rebuild(outFile.FullName, useGreenAsAlpha);
63 | }
64 | if (needCopyOverSrc)
65 | {
66 | outFile.CopyTo(romFile.FullName, true);
67 | outFile.Delete();
68 | }
69 | }
70 |
71 | void DoExportSheets(FileInfo romFile, FirmwareInfo fwInfo, DirectoryInfo outDir, bool useGreenAsAlpha, bool useBmp)
72 | {
73 | if (useBmp) useGreenAsAlpha = true;
74 | using var impExp = new ImageImportExport(romFile.OpenRead(), fwInfo);
75 | outDir.Create();
76 | impExp.ExportSpriteSheetFolder(outDir.FullName, useBmp ? ".bmp" : ".png", useGreenAsAlpha);
77 | }
78 |
79 | void DoImportSheets(FileInfo romFile, FirmwareInfo fwInfo, DirectoryInfo inDir, FileInfo? outFile, bool useGreenAsAlpha, uint? rows, uint? cols)
80 | {
81 | bool needCopyOverSrc = outFile == null || outFile.FullName == romFile.FullName; // Not foolproof
82 | if (needCopyOverSrc) outFile = new FileInfo(Path.GetTempFileName());
83 | using (var impExp = new ImageImportExport(romFile.OpenRead(), fwInfo))
84 | {
85 | impExp.ImportSpriteSheetFolder(inDir.FullName, useGreenAsAlpha, rows, cols);
86 | impExp.Rebuild(outFile.FullName, useGreenAsAlpha);
87 | }
88 | if (needCopyOverSrc)
89 | {
90 | outFile.CopyTo(romFile.FullName, true);
91 | outFile.Delete();
92 | }
93 | }
94 | #endregion
95 |
96 | #region Commands with presets
97 | var exportPresetCmd = new Command("export-preset", "Export all sprites using firmware preset")
98 | {
99 | useGreenAsAlphaOption, useBmpOption, romPathArgument, presetNameArgument, outDirArgument
100 | };
101 | rootCommand.AddCommand(exportPresetCmd);
102 |
103 | exportPresetCmd.SetHandler(context =>
104 | {
105 | var pr = context.ParseResult;
106 | var useGreenAsAlpha = pr.GetValueForOption(useGreenAsAlphaOption);
107 | var useBmp = pr.GetValueForOption(useBmpOption);
108 | var romPath = pr.GetValueForArgument(romPathArgument);
109 | var presetName = pr.GetValueForArgument(presetNameArgument);
110 | var outDir = pr.GetValueForArgument(outDirArgument);
111 |
112 | var fwInfo = FirmwareInfo.Presets[presetName];
113 |
114 | DoExport(romPath, fwInfo, outDir, useBmp, useGreenAsAlpha);
115 | });
116 |
117 | var importPresetCmd = new Command("import-preset", "Import sprites using firmware preset")
118 | {
119 | useGreenAsAlphaOption, romPathArgument, presetNameArgument, inDirArgument, outFileArgument
120 | };
121 | rootCommand.AddCommand(importPresetCmd);
122 |
123 | importPresetCmd.SetHandler(context =>
124 | {
125 | var pr = context.ParseResult;
126 | var useGreenAsAlpha = pr.GetValueForOption(useGreenAsAlphaOption);
127 | var romPath = pr.GetValueForArgument(romPathArgument);
128 | var presetName = pr.GetValueForArgument(presetNameArgument);
129 | var inDir = pr.GetValueForArgument(inDirArgument);
130 | var outFile = pr.GetValueForArgument(outFileArgument);
131 |
132 | var fwInfo = FirmwareInfo.Presets[presetName];
133 |
134 | DoImport(romPath, fwInfo, inDir, outFile, useGreenAsAlpha);
135 | });
136 |
137 | var exportSheetsPresetCmd = new Command("export-sheets-preset", "Export all character sprite sheets using firmware preset")
138 | {
139 | useGreenAsAlphaOption, useBmpOption, specialCharaIndexesOption, romPathArgument, presetNameArgument, outDirArgument
140 | };
141 | rootCommand.Add(exportSheetsPresetCmd);
142 |
143 | exportSheetsPresetCmd.SetHandler(context =>
144 | {
145 | var pr = context.ParseResult;
146 | var useGreenAsAlpha = pr.GetValueForOption(useGreenAsAlphaOption);
147 | var useBmp = pr.GetValueForOption(useBmpOption);
148 | var romPath = pr.GetValueForArgument(romPathArgument);
149 | var presetName = pr.GetValueForArgument(presetNameArgument);
150 | var outDir = pr.GetValueForArgument(outDirArgument);
151 |
152 | var fwInfo = FirmwareInfo.Presets[presetName];
153 | if (pr.HasOption(specialCharaIndexesOption))
154 | {
155 | fwInfo.SpecialCharaIndexes = pr.GetValueForOption(specialCharaIndexesOption)!;
156 | }
157 |
158 | DoExportSheets(romPath, fwInfo, outDir, useGreenAsAlpha, useBmp);
159 | });
160 |
161 | var importSheetsPresetCmd = new Command("import-sheets-preset", "Import character sprite sheets using firmware preset")
162 | {
163 | useGreenAsAlphaOption, specialCharaIndexesOption, sheetRowsOption, sheetColsOption, tortoiseshelOption,
164 | romPathArgument, presetNameArgument, inDirArgument, outFileArgument
165 | };
166 | rootCommand.AddCommand(importSheetsPresetCmd);
167 |
168 | importSheetsPresetCmd.SetHandler(context =>
169 | {
170 | var pr = context.ParseResult;
171 | var useGreenAsAlpha = pr.GetValueForOption(useGreenAsAlphaOption);
172 | var sheetRows = pr.GetValueForOption(sheetRowsOption);
173 | var sheetCols = pr.GetValueForOption(sheetColsOption);
174 | var isTortoiseshel = pr.GetValueForOption(tortoiseshelOption);
175 | var romPath = pr.GetValueForArgument(romPathArgument);
176 | var presetName = pr.GetValueForArgument(presetNameArgument);
177 | var inDir = pr.GetValueForArgument(inDirArgument);
178 | var outFile = pr.GetValueForArgument(outFileArgument);
179 |
180 | var fwInfo = FirmwareInfo.Presets[presetName];
181 | if (pr.HasOption(specialCharaIndexesOption))
182 | {
183 | fwInfo.SpecialCharaIndexes = pr.GetValueForOption(specialCharaIndexesOption)!;
184 | }
185 | if (isTortoiseshel)
186 | {
187 | sheetRows = 4;
188 | sheetCols = 3;
189 | }
190 |
191 | DoImportSheets(romPath, fwInfo, inDir, outFile, useGreenAsAlpha, sheetRows, sheetCols);
192 | });
193 |
194 | var showPresetCmd = new Command("show-preset", "Show the parameters of a preset")
195 | {
196 | presetNameArgument
197 | };
198 | rootCommand.Add(showPresetCmd);
199 |
200 | showPresetCmd.SetHandler(presetName =>
201 | {
202 | var fwInfo = FirmwareInfo.Presets[presetName];
203 | Console.WriteLine($"Name: {presetName}");
204 | Console.WriteLine($"{nameof(fwInfo.SpritePackBase)}: {fwInfo.SpritePackBase}");
205 | Console.WriteLine($"{nameof(fwInfo.CharaSpriteWidth)}: {fwInfo.CharaSpriteWidth}");
206 | Console.WriteLine($"{nameof(fwInfo.CharaSpriteHeight)}: {fwInfo.CharaSpriteHeight}");
207 | Console.WriteLine($"{nameof(fwInfo.SizeTableOffset)}: {fwInfo.SizeTableOffset}");
208 | Console.WriteLine($"{nameof(fwInfo.NumImages)}: {fwInfo.NumImages}");
209 | Console.WriteLine($"{nameof(fwInfo.NumCharas)}: {fwInfo.NumCharas}");
210 | Console.WriteLine($"{nameof(fwInfo.NumFramesPerChara)}: {fwInfo.NumFramesPerChara}");
211 | Console.WriteLine($"{nameof(fwInfo.CharasStartIndex)}: {fwInfo.CharasStartIndex}");
212 | Console.WriteLine($"{nameof(fwInfo.NumJogressCharas)}: {fwInfo.NumJogressCharas}");
213 | Console.WriteLine($"{nameof(fwInfo.HasName)}: {fwInfo.HasName}");
214 | Console.WriteLine($"{nameof(fwInfo.NumFramesPerSpecialChara)}: {fwInfo.NumFramesPerSpecialChara}");
215 | Console.WriteLine($"{nameof(fwInfo.SpecialCharaIndexes)}: [{string.Join(", ", fwInfo.SpecialCharaIndexes)}]");
216 | }, presetNameArgument);
217 | #endregion
218 |
219 | #region Commands without presets
220 | var exportCmd = new Command("export", "Export all sprites")
221 | {
222 | useGreenAsAlphaOption, useBmpOption, romPathArgument, spritePackBaseArgument, sizeTableOffsetArgument,
223 | numImagesArgument, outDirArgument
224 | };
225 | rootCommand.AddCommand(exportCmd);
226 |
227 | exportCmd.SetHandler(context =>
228 | {
229 | var pr = context.ParseResult;
230 | var useGreenAsAlpha = pr.GetValueForOption(useGreenAsAlphaOption);
231 | var useBmp = pr.GetValueForOption(useBmpOption);
232 | var romPath = pr.GetValueForArgument(romPathArgument);
233 | var spritePackBase = pr.GetValueForArgument(spritePackBaseArgument);
234 | var sizeTableOffset = pr.GetValueForArgument(sizeTableOffsetArgument);
235 | var numImages = pr.GetValueForArgument(numImagesArgument);
236 | var outDir = pr.GetValueForArgument(outDirArgument);
237 |
238 | var fwInfo = new FirmwareInfo
239 | {
240 | SpritePackBase = spritePackBase,
241 | SizeTableOffset = sizeTableOffset,
242 | NumImages = numImages,
243 | };
244 |
245 | DoExport(romPath, fwInfo, outDir, useBmp, useGreenAsAlpha);
246 | });
247 |
248 | var importCmd = new Command("import", "Import sprites")
249 | {
250 | useGreenAsAlphaOption, romPathArgument, spritePackBaseArgument, sizeTableOffsetArgument,
251 | numImagesArgument, inDirArgument, outFileArgument
252 | };
253 | rootCommand.AddCommand(importCmd);
254 |
255 | importCmd.SetHandler(context =>
256 | {
257 | var pr = context.ParseResult;
258 | var useGreenAsAlpha = pr.GetValueForOption(useGreenAsAlphaOption);
259 | var romPath = pr.GetValueForArgument(romPathArgument);
260 | var spritePackBase = pr.GetValueForArgument(spritePackBaseArgument);
261 | var sizeTableOffset = pr.GetValueForArgument(sizeTableOffsetArgument);
262 | var numImages = pr.GetValueForArgument(numImagesArgument);
263 | var inDir = pr.GetValueForArgument(inDirArgument);
264 | var outFile = pr.GetValueForArgument(outFileArgument);
265 |
266 | var fwInfo = new FirmwareInfo
267 | {
268 | SpritePackBase = spritePackBase,
269 | SizeTableOffset = sizeTableOffset,
270 | NumImages = numImages,
271 | };
272 |
273 | DoImport(romPath, fwInfo, inDir, outFile, useGreenAsAlpha);
274 | });
275 |
276 | var exportSheetsCmd = new Command("export-sheets", "Export all character sprite sheets")
277 | {
278 | useGreenAsAlphaOption, useBmpOption, numJogressOption, hasNameOption, numFramesPerSpecialCharaOption,
279 | specialCharaIndexesOption, romPathArgument, outDirArgument, spritePackBaseArgument,
280 | sizeTableOffsetArgument, numImagesArgument, numCharasArgument, numFramesPerCharaArgument,
281 | charaStartIndexArgument
282 | };
283 | rootCommand.Add(exportSheetsCmd);
284 |
285 | exportSheetsCmd.SetHandler(context =>
286 | {
287 | var pr = context.ParseResult;
288 | var useGreenAsAlpha = pr.GetValueForOption(useGreenAsAlphaOption);
289 | var useBmp = pr.GetValueForOption(useBmpOption);
290 | var numJogresses = pr.GetValueForOption(numJogressOption);
291 | var romPath = pr.GetValueForArgument(romPathArgument);
292 | var outDir = pr.GetValueForArgument(outDirArgument);
293 | var spritePackBase = pr.GetValueForArgument(spritePackBaseArgument);
294 | var sizeTableOffset = pr.GetValueForArgument(sizeTableOffsetArgument);
295 | var numImages = pr.GetValueForArgument(numImagesArgument);
296 | var numCharas = pr.GetValueForArgument(numCharasArgument);
297 | var numFramesPerChara = pr.GetValueForArgument(numFramesPerCharaArgument);
298 | var charaStartIndex = pr.GetValueForArgument(charaStartIndexArgument);
299 | var hasName = pr.GetValueForOption(hasNameOption);
300 | var numFramesPerSpecialChara = pr.GetValueForOption(numFramesPerSpecialCharaOption);
301 | var specialCharaIndexes = pr.GetValueForOption(specialCharaIndexesOption);
302 |
303 | var fwInfo = new FirmwareInfo
304 | {
305 | SpritePackBase = spritePackBase,
306 | SizeTableOffset = sizeTableOffset,
307 | NumImages = numImages,
308 | NumCharas = numCharas,
309 | NumFramesPerChara = numFramesPerChara,
310 | CharasStartIndex = charaStartIndex,
311 | NumJogressCharas = numJogresses,
312 | HasName = hasName,
313 | NumFramesPerSpecialChara = numFramesPerSpecialChara,
314 | SpecialCharaIndexes = specialCharaIndexes ?? [],
315 | };
316 |
317 | DoExportSheets(romPath, fwInfo, outDir, useGreenAsAlpha, useBmp);
318 | });
319 |
320 | var importSheetsCmd = new Command("import-sheets", "Import character sprite sheets")
321 | {
322 | useGreenAsAlphaOption, numJogressOption, hasNameOption, numFramesPerSpecialCharaOption,
323 | specialCharaIndexesOption, sheetRowsOption, sheetColsOption, tortoiseshelOption,
324 | romPathArgument, spritePackBaseArgument, sizeTableOffsetArgument, numImagesArgument,
325 | numCharasArgument, numFramesPerCharaArgument, charaStartIndexArgument, inDirArgument,
326 | outFileArgument
327 | };
328 | rootCommand.AddCommand(importSheetsCmd);
329 |
330 | importSheetsCmd.SetHandler(context =>
331 | {
332 | var pr = context.ParseResult;
333 | var useGreenAsAlpha = pr.GetValueForOption(useGreenAsAlphaOption);
334 | var numJogresses = pr.GetValueForOption(numJogressOption);
335 | var sheetRows = pr.GetValueForOption(sheetRowsOption);
336 | var sheetCols = pr.GetValueForOption(sheetColsOption);
337 | var isTortoiseshel = pr.GetValueForOption(tortoiseshelOption);
338 | var romPath = pr.GetValueForArgument(romPathArgument);
339 | var spritePackBase = pr.GetValueForArgument(spritePackBaseArgument);
340 | var sizeTableOffset = pr.GetValueForArgument(sizeTableOffsetArgument);
341 | var numImages = pr.GetValueForArgument(numImagesArgument);
342 | var numCharas = pr.GetValueForArgument(numCharasArgument);
343 | var numFramesPerChara = pr.GetValueForArgument(numFramesPerCharaArgument);
344 | var charaStartIndex = pr.GetValueForArgument(charaStartIndexArgument);
345 | var inDir = pr.GetValueForArgument(inDirArgument);
346 | var outFile = pr.GetValueForArgument(outFileArgument);
347 | var hasName = pr.GetValueForOption(hasNameOption);
348 | var numFramesPerSpecialChara = pr.GetValueForOption(numFramesPerSpecialCharaOption);
349 | var specialCharaIndexes = pr.GetValueForOption(specialCharaIndexesOption);
350 |
351 | var fwInfo = new FirmwareInfo
352 | {
353 | SpritePackBase = spritePackBase,
354 | SizeTableOffset = sizeTableOffset,
355 | NumImages = numImages,
356 | NumCharas = numCharas,
357 | NumFramesPerChara = numFramesPerChara,
358 | CharasStartIndex = charaStartIndex,
359 | NumJogressCharas = numJogresses,
360 | HasName = hasName,
361 | NumFramesPerSpecialChara = numFramesPerSpecialChara,
362 | SpecialCharaIndexes = specialCharaIndexes ?? [],
363 | };
364 | if (isTortoiseshel)
365 | {
366 | sheetRows = 4;
367 | sheetCols = 3;
368 | }
369 |
370 | DoImportSheets(romPath, fwInfo, inDir, outFile, useGreenAsAlpha, sheetRows, sheetCols);
371 | });
372 | #endregion
373 |
374 | return rootCommand.Invoke(args);
375 |
--------------------------------------------------------------------------------
/DigimonColorSpriteTool/ImageImportExport.cs:
--------------------------------------------------------------------------------
1 | using SixLabors.ImageSharp;
2 | using SixLabors.ImageSharp.PixelFormats;
3 | using SixLabors.ImageSharp.Processing;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace DigimonColorSpriteTool
11 | {
12 | public class ImageImportExport : IDisposable
13 | {
14 | static readonly string[] FILE_NAME_PATTERNS =
15 | [
16 | "{0}", "{0:d3}", "{0}_0x{0:x}"
17 | ];
18 | static readonly string[] FILE_EXTENSIONS =
19 | [
20 | ".png", ".bmp"
21 | ];
22 | static readonly string NAME_SUFFIX = "_name";
23 | static readonly string CUTIN_SUFFIX = "_cutin";
24 |
25 | Stream fwStream;
26 | BinaryReader br;
27 | List imageInfos = new List();
28 | FirmwareInfo firmwareInfo;
29 | private bool disposedValue;
30 | List imageTypes = new();
31 | List specialImageTypes = new();
32 |
33 | public int NumImages => imageInfos.Count;
34 |
35 | public ImageImportExport(Stream fwStream, FirmwareInfo firmwareInfo)
36 | {
37 | this.fwStream = fwStream ?? throw new ArgumentNullException(nameof(fwStream));
38 | this.firmwareInfo = firmwareInfo ?? throw new ArgumentNullException(nameof(firmwareInfo));
39 | br = new BinaryReader(fwStream);
40 | ReadMetadata();
41 | BuildImageTypes();
42 | }
43 |
44 | void ReadMetadata()
45 | {
46 | // Read size table
47 | fwStream.Seek(firmwareInfo.SizeTableOffset, SeekOrigin.Begin);
48 | for (int i = 0; i < firmwareInfo.NumImages; i++)
49 | {
50 | imageInfos.Add(new ImageInfo
51 | {
52 | Width = br.ReadUInt16(),
53 | Height = br.ReadUInt16(),
54 | });
55 | }
56 |
57 | // Read offsets
58 | fwStream.Seek(firmwareInfo.SpritePackBase, SeekOrigin.Begin);
59 | foreach (var info in imageInfos)
60 | {
61 | info.DataOffset = br.ReadInt32();
62 | }
63 | }
64 |
65 | void BuildImageTypes()
66 | {
67 | int i;
68 | for (i = 0; i < firmwareInfo.NumFramesPerChara; ++i)
69 | {
70 | imageTypes.Add(ImageType.CharacterSprite);
71 | }
72 | if (firmwareInfo.HasCutin)
73 | {
74 | imageTypes.Add(ImageType.Cutin);
75 | }
76 | if (firmwareInfo.NumFramesPerSpecialChara > firmwareInfo.NumFramesPerChara)
77 | {
78 | specialImageTypes.AddRange(imageTypes);
79 | for (; i < firmwareInfo.NumFramesPerSpecialChara; ++i)
80 | {
81 | specialImageTypes.Add(ImageType.CharacterSprite);
82 | }
83 | if (firmwareInfo.HasCutin)
84 | {
85 | specialImageTypes.Add(ImageType.Cutin);
86 | }
87 | if (firmwareInfo.HasName)
88 | {
89 | specialImageTypes.Add(ImageType.Name);
90 | }
91 | }
92 | if (firmwareInfo.HasName)
93 | {
94 | imageTypes.Add(ImageType.Name);
95 | }
96 | }
97 |
98 | void CheckDisposed()
99 | {
100 | if (disposedValue) throw new ObjectDisposedException(GetType().FullName);
101 | }
102 |
103 | static string? FindFile(string folderPath, int index)
104 | {
105 | foreach (var extension in FILE_EXTENSIONS)
106 | {
107 | foreach (var pattern in FILE_NAME_PATTERNS)
108 | {
109 | string filePath = Path.Combine(folderPath, string.Format(pattern, index) + extension);
110 | if (File.Exists(filePath)) return filePath;
111 | }
112 | }
113 | return null;
114 | }
115 |
116 | public void SetOverridesByFolder(string folderPath)
117 | {
118 | CheckDisposed();
119 | if (string.IsNullOrEmpty(folderPath)) throw new ArgumentNullException(nameof(folderPath));
120 | for (int i = 0; i < imageInfos.Count; ++i)
121 | {
122 | string? filePath = FindFile(folderPath, i);
123 | imageInfos[i].FilePath = filePath;
124 | imageInfos[i].OverrideData = null;
125 | }
126 | }
127 |
128 | Image GetImage(ImageInfo info, bool useGreenAsAlpha)
129 | {
130 | fwStream.Seek(firmwareInfo.SpritePackBase + info.DataOffset, SeekOrigin.Begin);
131 | return ImageConverter.ConvertRgb565ToImage(br.ReadBytes(info.Width * info.Height * 2), info.Width, info.Height, useGreenAsAlpha);
132 | }
133 |
134 | public void ExportImage(int index, string destPath, bool useGreenAsAlpha)
135 | {
136 | CheckDisposed();
137 | if (index < 0 || index >= imageInfos.Count) throw new ArgumentOutOfRangeException(nameof(index));
138 | if (string.IsNullOrEmpty(destPath)) throw new ArgumentNullException(nameof(destPath));
139 | using var img = GetImage(imageInfos[index], useGreenAsAlpha);
140 | img.Save(destPath);
141 | }
142 |
143 | public void ExportAllImages(string destFolder, string extension, bool useGreenAsAlpha)
144 | {
145 | CheckDisposed();
146 | if (string.IsNullOrEmpty(destFolder)) throw new ArgumentNullException(nameof(destFolder));
147 | if (string.IsNullOrEmpty(extension)) throw new ArgumentNullException(nameof(extension));
148 | for (int i = 0; i < imageInfos.Count; ++i)
149 | {
150 | ExportImage(i, Path.Combine(destFolder, $"{i}{extension}"), useGreenAsAlpha);
151 | }
152 | }
153 |
154 | public void Rebuild(string destPath, bool useGreenAsAlpha)
155 | {
156 | CheckDisposed();
157 | if (string.IsNullOrEmpty(destPath)) throw new ArgumentNullException(nameof(destPath));
158 | using FileStream newStream = File.Create(destPath);
159 | BinaryWriter bw = new(newStream);
160 |
161 | // Copy firmware
162 | fwStream.Seek(0, SeekOrigin.Begin);
163 | fwStream.CopyTo(newStream);
164 |
165 | // Write image data
166 | List offsets = new();
167 | newStream.Seek(firmwareInfo.SpritePackBase + imageInfos.Count * 4, SeekOrigin.Begin);
168 | int i = 0;
169 | foreach (var info in imageInfos)
170 | {
171 | offsets.Add((int)(newStream.Position - firmwareInfo.SpritePackBase));
172 | if (info.OverrideData != null)
173 | {
174 | if (info.OverrideData.Length != info.Width * info.Height * 2)
175 | Console.Error.WriteLine($"Warning: New image data {i} has different size ({info.OverrideData.Length}) than original ({info.Width * info.Height * 2}).");
176 | bw.Write(info.OverrideData);
177 | }
178 | else if (info.FilePath != null)
179 | {
180 | using (var img = Image.Load(info.FilePath))
181 | {
182 | if (img.Width != info.Width || img.Height != info.Height)
183 | {
184 | Console.Error.WriteLine($"Warning: New file {i} has different dimension ({img.Width}x{img.Height}) compared to original ({info.Width}x{info.Height}).");
185 | info.Width = (ushort)img.Width;
186 | info.Height = (ushort)img.Height;
187 | }
188 | var convertedImg = ImageConverter.ConvertImageToRgb565(img, useGreenAsAlpha);
189 | bw.Write(convertedImg);
190 | }
191 | }
192 | else
193 | {
194 | fwStream.Seek(firmwareInfo.SpritePackBase + info.DataOffset, SeekOrigin.Begin);
195 | byte[] data = br.ReadBytes(info.Width * info.Height * 2);
196 | bw.Write(data);
197 | }
198 |
199 | ++i;
200 | }
201 |
202 | // Write image offsets
203 | newStream.Seek(firmwareInfo.SpritePackBase, SeekOrigin.Begin);
204 | foreach (var offset in offsets)
205 | {
206 | bw.Write(offset);
207 | }
208 |
209 | // Write size table
210 | newStream.Seek(firmwareInfo.SizeTableOffset, SeekOrigin.Begin);
211 | foreach (var info in imageInfos)
212 | {
213 | bw.Write(info.Width);
214 | bw.Write(info.Height);
215 | }
216 | }
217 |
218 | public void ImportSpriteSheet(string path, int startImageIndex, bool useGreenAsAlpha, uint? rows, uint? cols, bool isSpecial)
219 | {
220 | CheckDisposed();
221 | if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path");
222 | uint numFrames = isSpecial ? firmwareInfo.NumFramesPerSpecialChara : firmwareInfo.NumFramesPerChara;
223 | if (startImageIndex < 0 || startImageIndex + numFrames >= imageInfos.Count)
224 | throw new ArgumentOutOfRangeException(nameof(startImageIndex), "Invalid start image index.");
225 | if (!rows.HasValue) rows = numFrames;
226 | if (!cols.HasValue) cols = 1;
227 | if (rows * cols < numFrames)
228 | throw new ArgumentException("Not enough rows and cols for number of frame per character.");
229 |
230 | using var sheetImg = Image.Load(path);
231 | int sheetFrameWidth = (int)(sheetImg.Width / cols);
232 | int sheetFrameHeight = (int)(sheetImg.Height / rows);
233 | // Do not allow oversized sprites, even if it technically would work
234 | if (sheetFrameWidth > firmwareInfo.CharaSpriteWidth || sheetFrameHeight > firmwareInfo.CharaSpriteHeight)
235 | throw new ArgumentException("Sheet frame is too large for device character frame.", nameof(path));
236 | if (firmwareInfo.CharaSpriteWidth % sheetFrameWidth != 0)
237 | throw new ArgumentException("Sheet frame width cannot be scaled by an integral factor.", nameof(path));
238 | if (firmwareInfo.CharaSpriteHeight % sheetFrameHeight != 0)
239 | throw new ArgumentException("Sheet frame height cannot be scaled by an integral factor.", nameof(path));
240 | int scaleFactorX = (int)(firmwareInfo.CharaSpriteWidth / sheetFrameWidth);
241 | int scaleFactorY = (int)(firmwareInfo.CharaSpriteHeight / sheetFrameHeight);
242 |
243 | List allImageTypes = isSpecial ? specialImageTypes : imageTypes;
244 | int currRow = 0;
245 | int currCol = 0;
246 | int cutinIndex = 0;
247 | for (int i = 0; i < allImageTypes.Count; ++i)
248 | {
249 | switch (allImageTypes[i])
250 | {
251 | case ImageType.CharacterSprite:
252 | {
253 | using var frameImg = new Image(sheetFrameWidth, sheetFrameHeight);
254 | frameImg.Mutate(x => x.DrawImage(sheetImg, new Point(-sheetFrameWidth * currCol, -sheetFrameHeight * currRow), 1.0f));
255 | if (scaleFactorX != 1 || scaleFactorY != 1)
256 | {
257 | frameImg.Mutate(x => x.Resize(sheetFrameWidth * scaleFactorX, sheetFrameHeight * scaleFactorY, KnownResamplers.NearestNeighbor));
258 | }
259 | byte[] pixels = ImageConverter.ConvertImageToRgb565(frameImg, useGreenAsAlpha);
260 | imageInfos[startImageIndex + i].OverrideData = pixels;
261 | ++currCol;
262 | if (currCol >= cols)
263 | {
264 | ++currRow;
265 | currCol = 0;
266 | }
267 | break;
268 |
269 | }
270 | case ImageType.Cutin:
271 | {
272 | string cutinName = CUTIN_SUFFIX;
273 | if (isSpecial)
274 | {
275 | cutinName += cutinIndex++;
276 | }
277 | string cutinPath = $"{Path.ChangeExtension(path, null)}{cutinName}{Path.GetExtension(path)}";
278 | if (File.Exists(cutinPath))
279 | {
280 | using var cutinImg = Image.Load(cutinPath);
281 | byte[] pixels = ImageConverter.ConvertImageToRgb565(cutinImg, useGreenAsAlpha);
282 | var nameInfo = imageInfos[startImageIndex + i];
283 | nameInfo.OverrideData = pixels;
284 | nameInfo.Width = (ushort)cutinImg.Width;
285 | nameInfo.Height = (ushort)cutinImg.Height;
286 | }
287 | }
288 | break;
289 | case ImageType.Name:
290 | {
291 | string namePath = $"{Path.ChangeExtension(path, null)}{NAME_SUFFIX}{Path.GetExtension(path)}";
292 | if (File.Exists(namePath))
293 | {
294 | using var nameImg = Image.Load(namePath);
295 | byte[] pixels = ImageConverter.ConvertImageToRgb565(nameImg, useGreenAsAlpha);
296 | var nameInfo = imageInfos[startImageIndex + i];
297 | nameInfo.OverrideData = pixels;
298 | nameInfo.Width = (ushort)nameImg.Width;
299 | nameInfo.Height = (ushort)nameImg.Height;
300 | }
301 | }
302 | break;
303 | }
304 | }
305 | }
306 |
307 | public void ImportSpriteSheetFolder(string folderPath, bool useGreenAsAlpha, uint? rows, uint? cols)
308 | {
309 | CheckDisposed();
310 | if (string.IsNullOrEmpty(folderPath)) throw new ArgumentNullException(nameof(folderPath));
311 | int startImageIndex = (int)firmwareInfo.CharasStartIndex;
312 | for (int i = 0; i < firmwareInfo.NumCharas; ++i)
313 | {
314 | string? filePath = FindFile(folderPath, i);
315 | bool isSpecial = Array.IndexOf(firmwareInfo.SpecialCharaIndexes, (uint)i) != -1;
316 | if (i < firmwareInfo.NumJogressCharas) ++startImageIndex;
317 | if (filePath != null)
318 | {
319 | ImportSpriteSheet(filePath, startImageIndex, useGreenAsAlpha, rows, cols, isSpecial);
320 | }
321 | startImageIndex += isSpecial ? specialImageTypes.Count : imageTypes.Count;
322 | }
323 | }
324 |
325 | public void ExportSpriteSheet(string basePath, string extension, int startImageIndex, bool useGreenAsAlpha, bool isSpecial)
326 | {
327 | CheckDisposed();
328 | if (string.IsNullOrEmpty(basePath)) throw new ArgumentNullException(nameof(basePath));
329 | if (string.IsNullOrEmpty(extension)) throw new ArgumentNullException(nameof(extension));
330 | uint numFrames = isSpecial ? firmwareInfo.NumFramesPerSpecialChara : firmwareInfo.NumFramesPerChara;
331 | if (startImageIndex < 0 || startImageIndex + numFrames >= imageInfos.Count)
332 | throw new ArgumentOutOfRangeException(nameof(startImageIndex), "Invalid start image index.");
333 | using var sheetImg = new Image((int)firmwareInfo.CharaSpriteWidth, (int)(firmwareInfo.CharaSpriteHeight * numFrames));
334 |
335 | List allImageTypes = isSpecial ? specialImageTypes : imageTypes;
336 | int cutinIndex = 0;
337 | for (int i = 0; i < allImageTypes.Count; ++i)
338 | {
339 | switch (allImageTypes[i])
340 | {
341 | case ImageType.CharacterSprite:
342 | {
343 | using var frameImg = GetImage(imageInfos[startImageIndex + i], useGreenAsAlpha);
344 | sheetImg.Mutate(x => x.DrawImage(frameImg, new Point(0, (i - cutinIndex) * (int)firmwareInfo.CharaSpriteHeight), 1.0f));
345 | }
346 | break;
347 | case ImageType.Cutin:
348 | {
349 | string cutinName = CUTIN_SUFFIX;
350 | if (isSpecial)
351 | {
352 | cutinName += cutinIndex++;
353 | }
354 | using var cutinSprite = GetImage(imageInfos[startImageIndex + i], useGreenAsAlpha);
355 | cutinSprite.Save($"{basePath}{cutinName}{extension}");
356 | }
357 | break;
358 | case ImageType.Name:
359 | {
360 | using var nameSprite = GetImage(imageInfos[startImageIndex + i], useGreenAsAlpha);
361 | nameSprite.Save($"{basePath}{NAME_SUFFIX}{extension}");
362 | }
363 | break;
364 | }
365 | }
366 | sheetImg.Save(basePath + extension);
367 | }
368 |
369 | public void ExportSpriteSheetFolder(string folderPath, string extension, bool useGreenAsAlpha)
370 | {
371 | CheckDisposed();
372 | if (string.IsNullOrEmpty(folderPath)) throw new ArgumentNullException(nameof(folderPath));
373 | int startImageIndex = (int)firmwareInfo.CharasStartIndex;
374 | for (int i = 0; i < firmwareInfo.NumCharas; ++i)
375 | {
376 | string filePath = Path.Combine(folderPath, $"{i}");
377 | bool isSpecial = Array.IndexOf(firmwareInfo.SpecialCharaIndexes, (uint)i) != -1;
378 | if (i < firmwareInfo.NumJogressCharas) ++startImageIndex;
379 | ExportSpriteSheet(filePath, extension, startImageIndex, useGreenAsAlpha, isSpecial);
380 | startImageIndex += isSpecial ? specialImageTypes.Count : imageTypes.Count;
381 | }
382 | }
383 |
384 | protected virtual void Dispose(bool disposing)
385 | {
386 | if (!disposedValue)
387 | {
388 | if (disposing)
389 | {
390 | fwStream.Close();
391 | imageInfos.Clear();
392 | }
393 |
394 | disposedValue = true;
395 | }
396 | }
397 |
398 | public void Dispose()
399 | {
400 | // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
401 | Dispose(disposing: true);
402 | GC.SuppressFinalize(this);
403 | }
404 | }
405 | }
406 |
--------------------------------------------------------------------------------