|
@@ -0,0 +1,164 @@
|
|
1
|
+using System;
|
|
2
|
+using System.Collections.Generic;
|
|
3
|
+using System.Threading.Tasks;
|
|
4
|
+using System.Drawing;
|
|
5
|
+using System.Drawing.Imaging;
|
|
6
|
+using System.IO;
|
|
7
|
+using System.Linq;
|
|
8
|
+
|
|
9
|
+namespace agbidx.ImageLib
|
|
10
|
+{
|
|
11
|
+ public static class BitmapProcessor
|
|
12
|
+ {
|
|
13
|
+ public static async Task ProcessPokemonImage(string outDirectory, string inPath, bool verbose, bool scanDirectories, bool recursive)
|
|
14
|
+ {
|
|
15
|
+ //Create the original bitmap
|
|
16
|
+ if(Directory.Exists(inPath))
|
|
17
|
+ {
|
|
18
|
+ if(scanDirectories)
|
|
19
|
+ {
|
|
20
|
+ List<Task> subTasks = new List<Task>();
|
|
21
|
+ foreach(string s in Directory.GetFiles(inPath))
|
|
22
|
+ {
|
|
23
|
+ subTasks.Add(ProcessPokemonImage(outDirectory, s, verbose, recursive, recursive));
|
|
24
|
+ }
|
|
25
|
+ foreach(string s in Directory.GetDirectories(inPath))
|
|
26
|
+ {
|
|
27
|
+ subTasks.Add(ProcessPokemonImage(outDirectory, s, verbose, recursive, recursive));
|
|
28
|
+ }
|
|
29
|
+ await Task.WhenAll(subTasks);
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ }
|
|
33
|
+ else
|
|
34
|
+ {
|
|
35
|
+ await Task.Run(() => {
|
|
36
|
+ Bitmap input = new Bitmap(inPath);
|
|
37
|
+ ProcessPokemonImageFront(input, outDirectory, Path.GetFileNameWithoutExtension(inPath), verbose);
|
|
38
|
+ });
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ private static Color[] Quantize4bppColors(Bitmap bmp)
|
|
44
|
+ {
|
|
45
|
+ List<Color> colors = new List<Color>(16);
|
|
46
|
+ BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
|
|
47
|
+ int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
|
|
48
|
+ int heightInPixels = bitmapData.Height;
|
|
49
|
+ int widthInBytes = bitmapData.Width * bytesPerPixel;
|
|
50
|
+ int palCount = 0;
|
|
51
|
+ unsafe
|
|
52
|
+ {
|
|
53
|
+ byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
|
|
54
|
+ for (int y = 0; y < heightInPixels; y++)
|
|
55
|
+ {
|
|
56
|
+ byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
|
|
57
|
+ for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
|
|
58
|
+ {
|
|
59
|
+ int blue = currentLine[x];
|
|
60
|
+ int green = currentLine[x + 1];
|
|
61
|
+ int red = currentLine[x + 2];
|
|
62
|
+ if (!colors.Any(c => c.R == red && c.G == green && c.B == blue))
|
|
63
|
+ {
|
|
64
|
+ colors.Add(Color.FromArgb(red,green,blue));
|
|
65
|
+ palCount++;
|
|
66
|
+ if(palCount > 16)
|
|
67
|
+ throw new ArgumentException("The image has too many colors", "bmp");
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+ }
|
|
73
|
+ bmp.UnlockBits(bitmapData);
|
|
74
|
+ return colors.ToArray();
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ private static Bitmap RenderBitmapFromPalette(Bitmap original, Color[] palette)
|
|
78
|
+ {
|
|
79
|
+ Bitmap output = new Bitmap(original.Width,original.Height, PixelFormat.Format4bppIndexed);
|
|
80
|
+ BitmapData targetData = output.LockBits(new Rectangle(0, 0, original.Width,original.Height),
|
|
81
|
+ ImageLockMode.ReadWrite, PixelFormat.Format4bppIndexed);
|
|
82
|
+ BitmapData sourceData = original.LockBits(new Rectangle(0, 0, original.Width,original.Height),
|
|
83
|
+ ImageLockMode.ReadOnly, original.PixelFormat);
|
|
84
|
+
|
|
85
|
+ int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(original.PixelFormat) / 8;
|
|
86
|
+ int heightInPixels = sourceData.Height;
|
|
87
|
+ int widthInBytes = sourceData.Width * bytesPerPixel;
|
|
88
|
+ unsafe
|
|
89
|
+ {
|
|
90
|
+ byte* ptrFirstPixel = (byte*)sourceData.Scan0;
|
|
91
|
+ for (int y = 0; y < heightInPixels; y++)
|
|
92
|
+ {
|
|
93
|
+ byte* currentLine = ptrFirstPixel + (y * sourceData.Stride);
|
|
94
|
+ byte* pTarget = (byte*) (targetData.Scan0 + y*targetData.Stride);
|
|
95
|
+ byte prevValue = 0;
|
|
96
|
+ for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
|
|
97
|
+ {
|
|
98
|
+ int blue = currentLine[x];
|
|
99
|
+ int green = currentLine[x + 1];
|
|
100
|
+ int red = currentLine[x + 2];
|
|
101
|
+ if (!palette.Any(c => c.R == red && c.G == green && c.B == blue))
|
|
102
|
+ throw new ArgumentException($"The color palette does not contain the required entry {red}:{green}:{blue}.","palette");
|
|
103
|
+ else
|
|
104
|
+ {
|
|
105
|
+ byte colorIndex = (byte)Array.IndexOf(palette, Color.FromArgb(red,green,blue));
|
|
106
|
+ if((x/bytesPerPixel) % 2 == 0)
|
|
107
|
+ prevValue = colorIndex;
|
|
108
|
+ else
|
|
109
|
+ {
|
|
110
|
+ *pTarget = (byte)((prevValue<<4) | (colorIndex));
|
|
111
|
+ pTarget++;
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ }
|
|
115
|
+ }
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ output.UnlockBits(targetData);
|
|
120
|
+ original.UnlockBits(sourceData);
|
|
121
|
+
|
|
122
|
+ ColorPalette pal = output.Palette;
|
|
123
|
+ for(int i = 0; i < ((palette.Length > 16) ? 16 : palette.Length); ++i)
|
|
124
|
+ {
|
|
125
|
+ pal.Entries[i] = palette[i];
|
|
126
|
+ }
|
|
127
|
+ output.Palette = pal;
|
|
128
|
+ return output;
|
|
129
|
+ }
|
|
130
|
+ private static void ProcessPokemonImageFront(Bitmap inFile, string outDirectory, string name, bool verbose)
|
|
131
|
+ {
|
|
132
|
+ Bitmap normal = new Bitmap(128,64, PixelFormat.Format32bppRgb);
|
|
133
|
+ Bitmap shiny = new Bitmap(128,64, PixelFormat.Format32bppRgb);
|
|
134
|
+ using(Graphics g = Graphics.FromImage(normal))
|
|
135
|
+ {
|
|
136
|
+ g.DrawImage(inFile,new Rectangle(0,0,64,64),new Rectangle(0,0,64,64), GraphicsUnit.Pixel);
|
|
137
|
+ g.DrawImage(inFile,new Rectangle(64,0,64,64),new Rectangle(2*64,0,64,64), GraphicsUnit.Pixel);
|
|
138
|
+ }
|
|
139
|
+ using(Graphics g = Graphics.FromImage(shiny))
|
|
140
|
+ {
|
|
141
|
+ g.DrawImage(inFile ,new Rectangle(0,0,64,64),new Rectangle(1*64,0,64,64), GraphicsUnit.Pixel);
|
|
142
|
+ g.DrawImage(inFile ,new Rectangle(64,0,64,64),new Rectangle(3*64,0,64,64), GraphicsUnit.Pixel);
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ Color[] normalPalette = Quantize4bppColors(normal);
|
|
146
|
+ Color[] shinyPalette = Quantize4bppColors(shiny);
|
|
147
|
+
|
|
148
|
+ Bitmap normalIndexed = RenderBitmapFromPalette(normal, normalPalette);
|
|
149
|
+ Bitmap shinyIndexed = RenderBitmapFromPalette(shiny, shinyPalette);
|
|
150
|
+
|
|
151
|
+ Bitmap frontNormalIndexed = normalIndexed.Clone(new Rectangle(0,0,64,64), PixelFormat.Format4bppIndexed);
|
|
152
|
+ Bitmap backShinyIndexed = shinyIndexed.Clone(new Rectangle(64,0,64,64), PixelFormat.Format4bppIndexed);
|
|
153
|
+
|
|
154
|
+ frontNormalIndexed.Save(Path.Combine(outDirectory, name+"_frontnormal.png"),ImageFormat.Png);
|
|
155
|
+ backShinyIndexed.Save(Path.Combine(outDirectory,name+"_backshiny.png"),ImageFormat.Png);
|
|
156
|
+
|
|
157
|
+ if(verbose)
|
|
158
|
+ {
|
|
159
|
+ Console.WriteLine("Processed " + Path.Combine(outDirectory, name+"_frontnormal.png"));
|
|
160
|
+ Console.WriteLine("Processed " + Path.Combine(outDirectory,name+"_backshiny.png"));
|
|
161
|
+ }
|
|
162
|
+ }
|
|
163
|
+ }
|
|
164
|
+}
|