From a4a817abcda267d79189f0c53f30fddcc6958440 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 31 Dec 2017 18:51:42 -0800 Subject: [PATCH] Cleanup dis codebase like jenga --- pk3DS.Core/CTR/BCLIM.cs | 66 +++++++++++++++++------------------------ pk3DS/Tools/ToolsUI.cs | 9 +----- 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/pk3DS.Core/CTR/BCLIM.cs b/pk3DS.Core/CTR/BCLIM.cs index 61dcdfb..9a5801b 100644 --- a/pk3DS.Core/CTR/BCLIM.cs +++ b/pk3DS.Core/CTR/BCLIM.cs @@ -1,6 +1,5 @@ using System; using System.Drawing; -using System.Drawing.Imaging; using System.IO; namespace pk3DS.Core.CTR @@ -84,48 +83,37 @@ public static Image makeBMP(string path, bool autosave = false, bool crop = true CLIM bclim = analyze(path); if (bclim.Magic != 0x4D494C43) { - System.Media.SystemSounds.Beep.Play(); + System.Media.SystemSounds.Beep.Play(); return null; } - // Interpret data. - int f = bclim.FileFormat; - if (f > 13) - { - System.Media.SystemSounds.Exclamation.Play(); - return null; - } - Bitmap img; - if (f == 7 && BitConverter.ToUInt16(bclim.Data, 0) == 2) - // PKM XY Format 7 (Color Palette) - img = getIMG_XY7(bclim); - else if (f == 10 || f == 11) - img = ImageUtil.GetBitmapETC(bclim); - else - img = getIMG(bclim); - - if (img == null) return null; - Rectangle cropRect = new Rectangle(0, 0, bclim.Width, bclim.Height); - Bitmap CropBMP = new Bitmap(cropRect.Width, cropRect.Height); - using (Graphics g = Graphics.FromImage(CropBMP)) - { - g.DrawImage(img, - new Rectangle(0, 0, CropBMP.Width, CropBMP.Height), - cropRect, - GraphicsUnit.Pixel); - } - if (!autosave) return !crop ? img : CropBMP; - - using (MemoryStream ms = new MemoryStream()) - { - //error will throw from here - CropBMP.Save(ms, ImageFormat.Png); - byte[] data = ms.ToArray(); - File.WriteAllBytes(bclim.FilePath + "\\" + bclim.FileName + ".png", data); - } - return !crop ? img : CropBMP; + Bitmap img = GetBCLIMImage(bclim); + if (img == null) + return null; + if (crop) + img = ImageUtil.CropBMP(bclim, img); + if (autosave) + img.Save(Path.Combine(bclim.FilePath, $"{bclim.FileName}.png")); + return img; } + + private static Bitmap GetBCLIMImage(CLIM bclim) + { + int f = bclim.FileFormat; + switch (f) + { + case 7 when BitConverter.ToUInt16(bclim.Data, 0) == 2: // PKM XY Format 7 (Color Palette) + return getIMG_XY7(bclim); + case 0xA: case 0xB: + return ImageUtil.GetBitmapETC(bclim); + default: + if (f > 0xD) // unsupported formats + return null; + return getIMG(bclim); + } + } + // Bitmap Data Writing public static Bitmap getIMG(int width, int height, byte[] bytes, int f) { @@ -272,7 +260,7 @@ public static int write16BitColorPalette(Bitmap img, ref MemoryStream ms) int h = gcm(img.Height, 8); w = Math.Max(nlpo2(w), nlpo2(h)); h = w; - byte[] pixelarray = new Byte[w * h]; + byte[] pixelarray = new byte[w * h]; const int colorformat = 2; int ctr = 1; diff --git a/pk3DS/Tools/ToolsUI.cs b/pk3DS/Tools/ToolsUI.cs index 7f4f84d..0773dbf 100644 --- a/pk3DS/Tools/ToolsUI.cs +++ b/pk3DS/Tools/ToolsUI.cs @@ -99,14 +99,7 @@ private void openIMG(string path) var dir = Path.GetDirectoryName(path); var fn = Path.GetFileNameWithoutExtension(path); var outpath = Path.Combine(dir, $"{fn}.png"); - - using (MemoryStream ms = new MemoryStream()) - { - //error will throw from here - ((Bitmap)img).Save(ms, ImageFormat.Png); - byte[] data = ms.ToArray(); - File.WriteAllBytes(outpath, data); - } + img.Save(outpath); } } PB_BCLIM.Size = new Size(img.Width + 2, img.Height + 2);