Revert SkiaSharp Changes to TPL

This commit is contained in:
Michael 2022-03-21 22:26:09 -05:00
parent 1d645800b9
commit 0d3295c032
Signed by: TheShadowEevee
GPG key ID: 7A8AA92B3BAFAB75
2 changed files with 56 additions and 78 deletions

View file

@ -1,9 +0,0 @@
MIT License
Copyright (c) 2015-2016 Xamarin, Inc.
Copyright (c) 2017-2018 Microsoft Corporation.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

125
TPL.cs
View file

@ -20,9 +20,10 @@
//Zetsubou by SquidMan was also a reference. //Zetsubou by SquidMan was also a reference.
//Thanks to the authors! //Thanks to the authors!
using SkiaSharp;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -146,11 +147,11 @@ namespace libWiiSharp
} }
public static TPL FromImage( public static TPL FromImage(
SKBitmap img, Image img,
TPL_TextureFormat tplFormat, TPL_TextureFormat tplFormat,
TPL_PaletteFormat paletteFormat = TPL_PaletteFormat.RGB5A3) TPL_PaletteFormat paletteFormat = TPL_PaletteFormat.RGB5A3)
{ {
return FromImages(new SKBitmap[1] { img }, new TPL_TextureFormat[1] return FromImages(new Image[1] { img }, new TPL_TextureFormat[1]
{ {
tplFormat tplFormat
}, new TPL_PaletteFormat[1] { paletteFormat }); }, new TPL_PaletteFormat[1] { paletteFormat });
@ -166,10 +167,10 @@ namespace libWiiSharp
throw new Exception("You must specify a format for each image!"); throw new Exception("You must specify a format for each image!");
} }
List<SKBitmap> imageList = new List<SKBitmap>(); List<Image> imageList = new List<Image>();
foreach (string imagePath in imagePaths) foreach (string imagePath in imagePaths)
{ {
imageList.Add(SKBitmap.Decode(imagePath)); imageList.Add(Image.FromFile(imagePath));
} }
TPL tpl = new TPL(); TPL tpl = new TPL();
@ -178,7 +179,7 @@ namespace libWiiSharp
} }
public static TPL FromImages( public static TPL FromImages(
SKBitmap[] images, Image[] images,
TPL_TextureFormat[] tplFormats, TPL_TextureFormat[] tplFormats,
TPL_PaletteFormat[] paletteFormats) TPL_PaletteFormat[] paletteFormats)
{ {
@ -239,11 +240,11 @@ namespace libWiiSharp
} }
public void CreateFromImage( public void CreateFromImage(
SKBitmap img, Image img,
TPL_TextureFormat tplFormat, TPL_TextureFormat tplFormat,
TPL_PaletteFormat paletteFormat = TPL_PaletteFormat.RGB5A3) TPL_PaletteFormat paletteFormat = TPL_PaletteFormat.RGB5A3)
{ {
PrivCreateFromImages(new SKBitmap[1] { img }, new TPL_TextureFormat[1] PrivCreateFromImages(new Image[1] { img }, new TPL_TextureFormat[1]
{ {
tplFormat tplFormat
}, new TPL_PaletteFormat[1] { paletteFormat }); }, new TPL_PaletteFormat[1] { paletteFormat });
@ -259,17 +260,17 @@ namespace libWiiSharp
throw new Exception("You must specify a format for each image!"); throw new Exception("You must specify a format for each image!");
} }
List<SKBitmap> imageList = new List<SKBitmap>(); List<Image> imageList = new List<Image>();
foreach (string imagePath in imagePaths) foreach (string imagePath in imagePaths)
{ {
imageList.Add(SKBitmap.Decode(imagePath)); imageList.Add(Image.FromFile(imagePath));
} }
PrivCreateFromImages(imageList.ToArray(), tplFormats, paletteFormats); PrivCreateFromImages(imageList.ToArray(), tplFormats, paletteFormats);
} }
public void CreateFromImages( public void CreateFromImages(
SKBitmap[] images, Image[] images,
TPL_TextureFormat[] tplFormats, TPL_TextureFormat[] tplFormats,
TPL_PaletteFormat[] paletteFormats) TPL_PaletteFormat[] paletteFormats)
{ {
@ -321,12 +322,12 @@ namespace libWiiSharp
return ToMemoryStream().ToArray(); return ToMemoryStream().ToArray();
} }
public SKBitmap ExtractTexture() public Image ExtractTexture()
{ {
return ExtractTexture(0); return ExtractTexture(0);
} }
public SKBitmap ExtractTexture(int index) public Image ExtractTexture(int index)
{ {
byte[] data = tplTextureHeaders[index].TextureFormat switch byte[] data = tplTextureHeaders[index].TextureFormat switch
{ {
@ -358,51 +359,32 @@ namespace libWiiSharp
File.Delete(savePath); File.Delete(savePath);
} }
SKBitmap texture = ExtractTexture(index); Image texture = ExtractTexture(index);
switch (Path.GetExtension(savePath).ToLower()) switch (Path.GetExtension(savePath).ToLower())
{ {
case ".webp": case ".tif":
SKData webp = SKImage.FromBitmap(texture).Encode(SKEncodedImageFormat.Webp, 100); case ".tiff":
using (var filestream = File.OpenWrite(savePath)) texture.Save(savePath, ImageFormat.Tiff);
{
webp.SaveTo(filestream);
}
break; break;
case ".bmp": case ".bmp":
SKData bmp = SKImage.FromBitmap(texture).Encode(SKEncodedImageFormat.Bmp, 100); texture.Save(savePath, ImageFormat.Bmp);
using (var filestream = File.OpenWrite(savePath))
{
bmp.SaveTo(filestream);
}
break; break;
case ".gif": case ".gif":
SKData gif = SKImage.FromBitmap(texture).Encode(SKEncodedImageFormat.Gif, 100); texture.Save(savePath, ImageFormat.Gif);
using (var filestream = File.OpenWrite(savePath))
{
gif.SaveTo(filestream);
}
break; break;
case ".jpg": case ".jpg":
case ".jpeg": case ".jpeg":
SKData jpeg = SKImage.FromBitmap(texture).Encode(SKEncodedImageFormat.Jpeg, 100); texture.Save(savePath, ImageFormat.Jpeg);
using (var filestream = File.OpenWrite(savePath))
{
jpeg.SaveTo(filestream);
}
break; break;
default: default:
SKData png = SKImage.FromBitmap(texture).Encode(SKEncodedImageFormat.Png, 100); texture.Save(savePath, ImageFormat.Png);
using (var filestream = File.OpenWrite(savePath))
{
png.SaveTo(filestream);
}
break; break;
} }
} }
public SKBitmap[] ExtractAllTextures() public Image[] ExtractAllTextures()
{ {
List<SKBitmap> imageList = new List<SKBitmap>(); List<Image> imageList = new List<Image>();
for (int index = 0; index < tplHeader.NumOfTextures; ++index) for (int index = 0; index < tplHeader.NumOfTextures; ++index)
{ {
imageList.Add(ExtractTexture(index)); imageList.Add(ExtractTexture(index));
@ -429,10 +411,10 @@ namespace libWiiSharp
TPL_TextureFormat tplFormat, TPL_TextureFormat tplFormat,
TPL_PaletteFormat paletteFormat = TPL_PaletteFormat.RGB5A3) TPL_PaletteFormat paletteFormat = TPL_PaletteFormat.RGB5A3)
{ {
AddTexture(SKBitmap.Decode(imagePath), tplFormat, paletteFormat); AddTexture(Image.FromFile(imagePath), tplFormat, paletteFormat);
} }
public void AddTexture(SKBitmap img, TPL_TextureFormat tplFormat, TPL_PaletteFormat paletteFormat = TPL_PaletteFormat.RGB5A3) public void AddTexture(Image img, TPL_TextureFormat tplFormat, TPL_PaletteFormat paletteFormat = TPL_PaletteFormat.RGB5A3)
{ {
TPL_TextureEntry tplTextureEntry = new TPL_TextureEntry(); TPL_TextureEntry tplTextureEntry = new TPL_TextureEntry();
TPL_TextureHeader tplTextureHeader = new TPL_TextureHeader(); TPL_TextureHeader tplTextureHeader = new TPL_TextureHeader();
@ -483,10 +465,10 @@ namespace libWiiSharp
return (TPL_PaletteFormat)tplPaletteHeaders[index].PaletteFormat; return (TPL_PaletteFormat)tplPaletteHeaders[index].PaletteFormat;
} }
/*public Size GetTextureSize(int index) public Size GetTextureSize(int index)
{ {
return new Size(tplTextureHeaders[index].TextureWidth, tplTextureHeaders[index].TextureHeight); return new Size(tplTextureHeaders[index].TextureWidth, tplTextureHeaders[index].TextureHeight);
}*/ }
private void WriteToStream(Stream writeStream) private void WriteToStream(Stream writeStream)
{ {
@ -675,7 +657,7 @@ namespace libWiiSharp
} }
private void PrivCreateFromImages( private void PrivCreateFromImages(
SKBitmap[] images, Image[] images,
TPL_TextureFormat[] tplFormats, TPL_TextureFormat[] tplFormats,
TPL_PaletteFormat[] paletteFormats) TPL_PaletteFormat[] paletteFormats)
{ {
@ -688,7 +670,7 @@ namespace libWiiSharp
tplHeader.NumOfTextures = (uint)images.Length; tplHeader.NumOfTextures = (uint)images.Length;
for (int index = 0; index < images.Length; ++index) for (int index = 0; index < images.Length; ++index)
{ {
SKBitmap image = images[index]; Image image = images[index];
TPL_TextureEntry tplTextureEntry = new TPL_TextureEntry(); TPL_TextureEntry tplTextureEntry = new TPL_TextureEntry();
TPL_TextureHeader tplTextureHeader = new TPL_TextureHeader(); TPL_TextureHeader tplTextureHeader = new TPL_TextureHeader();
TPL_PaletteHeader tplPaletteHeader = new TPL_PaletteHeader(); TPL_PaletteHeader tplPaletteHeader = new TPL_PaletteHeader();
@ -713,30 +695,33 @@ namespace libWiiSharp
} }
} }
private byte[] ImageToTpl(SKBitmap img, TPL_TextureFormat tplFormat) private byte[] ImageToTpl(Image img, TPL_TextureFormat tplFormat)
{ {
return tplFormat switch return tplFormat switch
{ {
TPL_TextureFormat.I4 => ToI4(img), TPL_TextureFormat.I4 => ToI4((Bitmap)img),
TPL_TextureFormat.I8 => ToI8(img), TPL_TextureFormat.I8 => ToI8((Bitmap)img),
TPL_TextureFormat.IA4 => ToIA4(img), TPL_TextureFormat.IA4 => ToIA4((Bitmap)img),
TPL_TextureFormat.IA8 => ToIA8(img), TPL_TextureFormat.IA8 => ToIA8((Bitmap)img),
TPL_TextureFormat.RGB565 => ToRGB565(img), TPL_TextureFormat.RGB565 => ToRGB565((Bitmap)img),
TPL_TextureFormat.RGB5A3 => ToRGB5A3(img), TPL_TextureFormat.RGB5A3 => ToRGB5A3((Bitmap)img),
TPL_TextureFormat.RGBA8 => ToRGBA8(img), TPL_TextureFormat.RGBA8 => ToRGBA8((Bitmap)img),
TPL_TextureFormat.CI4 or TPL_TextureFormat.CI8 or TPL_TextureFormat.CI14X2 => new byte[0], TPL_TextureFormat.CI4 or TPL_TextureFormat.CI8 or TPL_TextureFormat.CI14X2 => new byte[0],
_ => throw new FormatException("Format not supported!\nCurrently, images can only be converted to the following formats:\nI4, I8, IA4, IA8, RGB565, RGB5A3, RGBA8, CI4, CI8 , CI14X2."), _ => throw new FormatException("Format not supported!\nCurrently, images can only be converted to the following formats:\nI4, I8, IA4, IA8, RGB565, RGB5A3, RGBA8, CI4, CI8 , CI14X2."),
}; };
} }
private uint[] ImageToRgba(SKBitmap bitmap) private uint[] ImageToRgba(Image img)
{ {
byte[] numArray = new byte[bitmap.Height * (Math.Abs(bitmap.Width) * 4)]; Bitmap bitmap = (Bitmap)img;
Marshal.Copy(bitmap.GetAddress(0, 0), numArray, 0, numArray.Length); BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte[] numArray = new byte[bitmapdata.Height * Math.Abs(bitmapdata.Stride)];
Marshal.Copy(bitmapdata.Scan0, numArray, 0, numArray.Length);
bitmap.UnlockBits(bitmapdata);
return Shared.ByteArrayToUIntArray(numArray); return Shared.ByteArrayToUIntArray(numArray);
} }
private SKBitmap RgbaToImage(byte[] data, int width, int height) private Bitmap RgbaToImage(byte[] data, int width, int height)
{ {
if (width == 0) if (width == 0)
{ {
@ -748,10 +733,12 @@ namespace libWiiSharp
height = 1; height = 1;
} }
SKBitmap bitmap = new SKBitmap(width, height); Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
try try
{ {
Marshal.Copy(data, 0, bitmap.GetAddress(0, 0), data.Length); BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
Marshal.Copy(data, 0, bitmapdata.Scan0, data.Length);
bitmap.UnlockBits(bitmapdata);
return bitmap; return bitmap;
} }
catch catch
@ -867,7 +854,7 @@ namespace libWiiSharp
return Shared.UIntArrayToByteArray(array); return Shared.UIntArrayToByteArray(array);
} }
private byte[] ToRGBA8(SKBitmap img) private byte[] ToRGBA8(Bitmap img)
{ {
uint[] rgba = ImageToRgba(img); uint[] rgba = ImageToRgba(img);
int width = img.Width; int width = img.Width;
@ -972,7 +959,7 @@ namespace libWiiSharp
return Shared.UIntArrayToByteArray(array); return Shared.UIntArrayToByteArray(array);
} }
private byte[] ToRGB5A3(SKBitmap img) private byte[] ToRGB5A3(Bitmap img)
{ {
uint[] rgba = ImageToRgba(img); uint[] rgba = ImageToRgba(img);
int width = img.Width; int width = img.Width;
@ -1051,7 +1038,7 @@ namespace libWiiSharp
return Shared.UIntArrayToByteArray(array); return Shared.UIntArrayToByteArray(array);
} }
private byte[] ToRGB565(SKBitmap img) private byte[] ToRGB565(Bitmap img)
{ {
uint[] rgba = ImageToRgba(img); uint[] rgba = ImageToRgba(img);
int width = img.Width; int width = img.Width;
@ -1116,7 +1103,7 @@ namespace libWiiSharp
return Shared.UIntArrayToByteArray(array); return Shared.UIntArrayToByteArray(array);
} }
private byte[] ToI4(SKBitmap img) private byte[] ToI4(Bitmap img)
{ {
uint[] rgba = ImageToRgba(img); uint[] rgba = ImageToRgba(img);
int width = img.Width; int width = img.Width;
@ -1176,7 +1163,7 @@ namespace libWiiSharp
return Shared.UIntArrayToByteArray(array); return Shared.UIntArrayToByteArray(array);
} }
private byte[] ToI8(SKBitmap img) private byte[] ToI8(Bitmap img)
{ {
uint[] rgba = ImageToRgba(img); uint[] rgba = ImageToRgba(img);
int width = img.Width; int width = img.Width;
@ -1235,7 +1222,7 @@ namespace libWiiSharp
return Shared.UIntArrayToByteArray(array); return Shared.UIntArrayToByteArray(array);
} }
private byte[] ToIA4(SKBitmap img) private byte[] ToIA4(Bitmap img)
{ {
uint[] rgba = ImageToRgba(img); uint[] rgba = ImageToRgba(img);
int width = img.Width; int width = img.Width;
@ -1294,7 +1281,7 @@ namespace libWiiSharp
return Shared.UIntArrayToByteArray(array); return Shared.UIntArrayToByteArray(array);
} }
private byte[] ToIA8(SKBitmap img) private byte[] ToIA8(Bitmap img)
{ {
uint[] rgba = ImageToRgba(img); uint[] rgba = ImageToRgba(img);
int width = img.Width; int width = img.Width;