Compare commits

..
6 Commits
Author SHA1 Message Date
Jeremy Pritts cab5cabc4e update package info, add net 5 target, and bump version 2022-02-11 20:44:02 -05:00
Jeremy Pritts b0038a5184 bump version 2022-02-05 23:50:03 -05:00
Jeremy Pritts 6665fab8d9 fix overflow in RGBAHalfToBGRA32 2022-02-05 23:48:53 -05:00
Jeremy Pritts 6998127fe8 bump version 2022-02-05 04:17:33 -05:00
Jeremy Pritts 8f68931c40 xml stub documentation 2022-02-05 04:04:38 -05:00
Jeremy Pritts eb9536456e clamp bytes during rgb conversion 2022-02-05 03:51:25 -05:00
15 changed files with 608 additions and 48 deletions

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
using System;
using System.Drawing.Imaging;
using System.IO;
using TextureDecoder.Astc;
using AssetRipper.TextureDecoder.Astc;
namespace AstcTest
{
+1 -1
View File
@@ -2,7 +2,7 @@ using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using TextureDecoder.Atc;
using AssetRipper.TextureDecoder.Atc;
namespace AtcTest
{
+1 -1
View File
@@ -2,7 +2,7 @@ using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using TextureDecoder.Dxt;
using AssetRipper.TextureDecoder.Dxt;
namespace DxtTest
{
+1 -1
View File
@@ -2,7 +2,7 @@ using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using TextureDecoder.Etc;
using AssetRipper.TextureDecoder.Etc;
namespace EtcTest
{
+1 -1
View File
@@ -2,7 +2,7 @@ using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using TextureDecoder.Pvrtc;
using AssetRipper.TextureDecoder.Pvrtc;
namespace PvrtcTest
{
+1 -1
View File
@@ -2,7 +2,7 @@ using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using TextureDecoder.Rgb;
using AssetRipper.TextureDecoder.Rgb;
namespace RgbTest
{
+31 -3
View File
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace TextureDecoder.Astc
namespace AssetRipper.TextureDecoder.Astc
{
/// <summary>
///
/// </summary>
public static class AstcDecoder
{
private unsafe struct BlockData
@@ -31,6 +32,15 @@ namespace TextureDecoder.Astc
public int nonbits;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="blockWidth"></param>
/// <param name="blockHeight"></param>
/// <returns></returns>
public static byte[] DecodeASTC(byte[] input, int width, int height, int blockWidth, int blockHeight)
{
byte[] output = new byte[width * height * 4];
@@ -38,6 +48,15 @@ namespace TextureDecoder.Astc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="blockWidth"></param>
/// <param name="blockHeight"></param>
/// <param name="output"></param>
public unsafe static void DecodeASTC(byte[] input, int width, int height, int blockWidth, int blockHeight, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -49,6 +68,15 @@ namespace TextureDecoder.Astc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="blockWidth"></param>
/// <param name="blockHeight"></param>
/// <param name="output"></param>
public unsafe static void DecodeASTC(byte* input, int width, int height, int blockWidth, int blockHeight, byte* output)
{
int bcw = (width + blockWidth - 1) / blockWidth;
+46 -1
View File
@@ -1,10 +1,20 @@
using System;
using System.Runtime.CompilerServices;
namespace TextureDecoder.Atc
namespace AssetRipper.TextureDecoder.Atc
{
/// <summary>
///
/// </summary>
public static class AtcDecoder
{
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressAtcRgb4(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -12,6 +22,13 @@ namespace TextureDecoder.Atc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressAtcRgb4(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -23,6 +40,13 @@ namespace TextureDecoder.Atc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressAtcRgb4(byte* input, int width, int height, byte* output)
{
int bcw = (width + 3) / 4;
@@ -47,6 +71,13 @@ namespace TextureDecoder.Atc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressAtcRgba8(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -54,6 +85,13 @@ namespace TextureDecoder.Atc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressAtcRgba8(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -65,6 +103,13 @@ namespace TextureDecoder.Atc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressAtcRgba8(byte* input, int width, int height, byte* output)
{
int bcw = (width + 3) / 4;
+26 -3
View File
@@ -1,10 +1,20 @@
using System;
using System.Runtime.CompilerServices;
namespace TextureDecoder.Dxt
namespace AssetRipper.TextureDecoder.Dxt
{
/// <summary>
///
/// </summary>
public static class DxtDecoder
{
{
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void DecompressDXT1(byte[] input, int width, int height, byte[] output)
{
int offset = 0;
@@ -49,7 +59,13 @@ namespace TextureDecoder.Dxt
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void DecompressDXT3(byte[] input, int width, int height, byte[] output)
{
int offset = 0;
@@ -104,6 +120,13 @@ namespace TextureDecoder.Dxt
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void DecompressDXT5(byte[] input, int width, int height, byte[] output)
{
int offset = 0;
+172 -2
View File
@@ -1,10 +1,19 @@
using System;
using System.Runtime.CompilerServices;
namespace TextureDecoder.Etc
namespace AssetRipper.TextureDecoder.Etc
{
/// <summary>
///
/// </summary>
public static class EtcDecoder
{
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressETC(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -12,6 +21,13 @@ namespace TextureDecoder.Etc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressETC(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -23,6 +39,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressETC(byte* input, int width, int height, byte* output)
{
int bcw = (width + 3) / 4;
@@ -47,6 +70,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressETC2(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -54,6 +84,13 @@ namespace TextureDecoder.Etc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressETC2(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -65,6 +102,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressETC2(byte* input, int width, int height, byte* output)
{
int bcw = (width + 3) / 4;
@@ -89,6 +133,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressETC2A1(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -96,6 +147,13 @@ namespace TextureDecoder.Etc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressETC2A1(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -107,6 +165,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressETC2A1(byte* input, int width, int height, byte* output)
{
int width4 = width * 4;
@@ -132,6 +197,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressETC2A8(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -139,6 +211,13 @@ namespace TextureDecoder.Etc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressETC2A8(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -150,6 +229,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressETC2A8(byte* input, int width, int height, byte* output)
{
int bcw = (width + 3) / 4;
@@ -175,6 +261,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressEACRUnsigned(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -182,6 +275,13 @@ namespace TextureDecoder.Etc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressEACRUnsigned(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -193,6 +293,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressEACRUnsigned(byte* input, int width, int height, byte* output)
{
int bcw = (width + 3) / 4;
@@ -221,6 +328,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressEACRSigned(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -228,6 +342,13 @@ namespace TextureDecoder.Etc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressEACRSigned(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -239,6 +360,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressEACRSigned(byte* input, int width, int height, byte* output)
{
int bcw = (width + 3) / 4;
@@ -267,6 +395,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressEACRGUnsigned(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -274,6 +409,13 @@ namespace TextureDecoder.Etc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressEACRGUnsigned(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -285,6 +427,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressEACRGUnsigned(byte* input, int width, int height, byte* output)
{
int bcw = (width + 3) / 4;
@@ -314,6 +463,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressEACRGSigned(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -321,6 +477,13 @@ namespace TextureDecoder.Etc
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressEACRGSigned(byte[] input, int width, int height, byte[] output)
{
fixed (byte* inputPtr = input)
@@ -332,6 +495,13 @@ namespace TextureDecoder.Etc
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public unsafe static void DecompressEACRGSigned(byte* input, int width, int height, byte* output)
{
int bcw = (width + 3) / 4;
+4 -3
View File
@@ -2,12 +2,13 @@
#define ASSUME_IMAGE_TILING
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
namespace TextureDecoder.Pvrtc
namespace AssetRipper.TextureDecoder.Pvrtc
{
/// <summary>
///
/// </summary>
public static class PvrtcDecoder
{
private struct AmtcBlock
+299 -24
View File
@@ -1,9 +1,23 @@
using System;
using System.Runtime.CompilerServices;
#if NET5_0_OR_GREATER
using System.Buffers.Binary;
#endif
namespace TextureDecoder.Rgb
namespace AssetRipper.TextureDecoder.Rgb
{
/// <summary>
///
/// </summary>
public static class RgbConverter
{
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] A8ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -11,6 +25,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void A8ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -29,6 +50,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] ARGB16ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -36,6 +64,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void ARGB16ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -54,6 +89,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RGB24ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -61,6 +103,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RGB24ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -79,6 +128,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RGBA32ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -86,6 +142,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RGBA32ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -104,6 +167,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] ARGB32ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -111,6 +181,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void ARGB32ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -129,6 +206,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RGB16ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -136,6 +220,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RGB16ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -157,6 +248,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] R16ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -164,6 +262,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void R16ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -182,6 +287,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RGBA16ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -189,6 +301,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RGBA16ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -207,6 +326,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RG16ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -214,6 +340,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RG16ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -232,6 +365,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] R8ToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -239,6 +379,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void R8ToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -257,6 +404,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RHalfToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -264,6 +418,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RHalfToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -272,7 +433,7 @@ namespace TextureDecoder.Rgb
{
for (int j = 0; j < height; j++)
{
byte r = Convert.ToByte(Math.Round(ToHalf(input, io) * 255f));
byte r = ClampByte(ToHalf(input, io) * 255f);
output[oo + 0] = 0; // b
output[oo + 1] = 0; // g
output[oo + 2] = r; // r
@@ -283,6 +444,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RGHalfToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -290,6 +458,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RGHalfToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -298,8 +473,8 @@ namespace TextureDecoder.Rgb
{
for (int j = 0; j < height; j++)
{
byte r = Convert.ToByte(Math.Round(ToHalf(input, io + 0) * 255f));
byte g = Convert.ToByte(Math.Round(ToHalf(input, io + 2) * 255f));
byte r = ClampByte(ToHalf(input, io + 0) * 255f);
byte g = ClampByte(ToHalf(input, io + 2) * 255f);
output[oo + 0] = 0; // b
output[oo + 1] = g; // g
output[oo + 2] = r; // r
@@ -310,6 +485,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RGBAHalfToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -317,6 +499,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RGBAHalfToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -325,10 +514,10 @@ namespace TextureDecoder.Rgb
{
for (int j = 0; j < height; j++)
{
byte r = Convert.ToByte(Math.Round(ToHalf(input, io + 0) * 255f));
byte g = Convert.ToByte(Math.Round(ToHalf(input, io + 2) * 255f));
byte b = Convert.ToByte(Math.Round(ToHalf(input, io + 4) * 255f));
byte a = Convert.ToByte(Math.Round(ToHalf(input, io + 6) * 255f));
byte r = ClampByte(ToHalf(input, io + 0) * 255f);
byte g = ClampByte(ToHalf(input, io + 2) * 255f);
byte b = ClampByte(ToHalf(input, io + 4) * 255f);
byte a = ClampByte(ToHalf(input, io + 6) * 255f);
output[oo + 0] = b; // b
output[oo + 1] = g; // g
output[oo + 2] = r; // r
@@ -339,6 +528,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RFloatToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -346,6 +542,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RFloatToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -354,7 +557,7 @@ namespace TextureDecoder.Rgb
{
for (int j = 0; j < height; j++)
{
byte r = Convert.ToByte(Math.Round(BitConverter.ToSingle(input, io) * 255f));
byte r = ClampByte(ToSingle(input, io) * 255f);
output[oo + 0] = 0; // b
output[oo + 1] = 0; // g
output[oo + 2] = r; // r
@@ -365,6 +568,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RGFloatToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -372,6 +582,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RGFloatToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -380,8 +597,8 @@ namespace TextureDecoder.Rgb
{
for (int j = 0; j < height; j++)
{
byte r = Convert.ToByte(Math.Round(BitConverter.ToSingle(input, io + 0) * 255f));
byte g = Convert.ToByte(Math.Round(BitConverter.ToSingle(input, io + 4) * 255f));
byte r = ClampByte(ToSingle(input, io + 0) * 255f);
byte g = ClampByte(ToSingle(input, io + 4) * 255f);
output[oo + 0] = 0; // b
output[oo + 1] = g; // g
output[oo + 2] = r; // r
@@ -392,6 +609,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RGBAFloatToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -399,6 +623,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RGBAFloatToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -407,10 +638,10 @@ namespace TextureDecoder.Rgb
{
for (int j = 0; j < height; j++)
{
byte r = Convert.ToByte(Math.Round(BitConverter.ToSingle(input, io + 0) * 255f));
byte g = Convert.ToByte(Math.Round(BitConverter.ToSingle(input, io + 4) * 255f));
byte b = Convert.ToByte(Math.Round(BitConverter.ToSingle(input, io + 8) * 255f));
byte a = Convert.ToByte(Math.Round(BitConverter.ToSingle(input, io + 12) * 255f));
byte r = ClampByte(ToSingle(input, io + 0));
byte g = ClampByte(ToSingle(input, io + 4));
byte b = ClampByte(ToSingle(input, io + 8));
byte a = ClampByte(ToSingle(input, io + 12));
output[oo + 0] = b; // b
output[oo + 1] = g; // g
output[oo + 2] = r; // r
@@ -421,6 +652,13 @@ namespace TextureDecoder.Rgb
}
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] RGB9e5FloatToBGRA32(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * 4];
@@ -428,6 +666,13 @@ namespace TextureDecoder.Rgb
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void RGB9e5FloatToBGRA32(byte[] input, int width, int height, byte[] output)
{
int io = 0;
@@ -438,9 +683,9 @@ namespace TextureDecoder.Rgb
{
uint value = BitConverter.ToUInt32(input, io);
double scale = Math.Pow(2, unchecked((int)(value >> 27) - 24));
byte r = Convert.ToByte(Math.Round((value >> 0 & 0x1FF) * scale * 255.0));
byte g = Convert.ToByte(Math.Round((value >> 9 & 0x1FF) * scale * 255.0));
byte b = Convert.ToByte(Math.Round((value >> 18 & 0x1FF) * scale * 255.0));
byte r = ClampByte((value >> 0 & 0x1FF) * scale * 255.0);
byte g = ClampByte((value >> 9 & 0x1FF) * scale * 255.0);
byte b = ClampByte((value >> 18 & 0x1FF) * scale * 255.0);
output[oo + 0] = b; // b
output[oo + 1] = g; // g
output[oo + 2] = r; // r
@@ -454,15 +699,45 @@ namespace TextureDecoder.Rgb
private static float ToHalf(byte[] input, int offset)
{
#if NET6_0_OR_GREATER
return (float)System.Buffers.Binary.BinaryPrimitives.ReadHalfLittleEndian(input.AsSpan(offset, 2));
#elif NET5_0_OR_GREATER
if(BitConverter.IsLittleEndian)
return (float)BitConverter.ToHalf(input, offset);
else
return (float)BitConverter.ToHalf(new byte[] { input[offset + 1], input[offset] });
return (float)BinaryPrimitives.ReadHalfLittleEndian(input.AsSpan(offset, 2));
#elif NET5_0
return (float)ToHalf(BinaryPrimitives.ReadUInt16LittleEndian(input.AsSpan(offset, 2)));
#else
return (float)Half.ToHalf(input, offset);
#endif
}
#if NET5_0
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Half ToHalf(ushort bits)
{
return *((Half*)&bits);
}
#endif
private static float ToSingle(byte[] input, int offset)
{
#if NET5_0_OR_GREATER
return BinaryPrimitives.ReadSingleLittleEndian(input.AsSpan(offset, 4));
#else
if (BitConverter.IsLittleEndian)
return BitConverter.ToSingle(input, offset);
else
return BitConverter.ToSingle(new byte[] { input[offset + 3], input[offset + 2], input[offset + 1], input[offset] }, 0);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte ClampByte(float x)
{
return byte.MaxValue < x ? byte.MaxValue : (x > byte.MinValue ? (byte)x : byte.MinValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte ClampByte(double x)
{
return byte.MaxValue < x ? byte.MaxValue : (x > byte.MinValue ? (byte)x : byte.MinValue);
}
}
}
+5 -4
View File
@@ -1,17 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net5.0;net6.0</TargetFrameworks>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<RootNamespace>AssetRipper.TextureDecoder</RootNamespace>
<Authors>ds5678</Authors>
<Company>AssetRipper</Company>
<Version>1.0.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<PackageId>TextureDecoder</PackageId>
<Version>1.1.0.0</Version>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<PackageId>AssetRipper.TextureDecoder</PackageId>
<PackageTags>C# Texture</PackageTags>
<RepositoryUrl>https://github.com/AssetRipper/TextureDecoder</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
+18 -1
View File
@@ -1,9 +1,19 @@
using System.Runtime.CompilerServices;
namespace TextureDecoder.Yuy2
namespace AssetRipper.TextureDecoder.Yuy2
{
/// <summary>
///
/// </summary>
public static class Yuy2Decoder
{
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] DecompressYUY2(byte[] input, int width, int height)
{
byte[] output = new byte[width * height * sizeof(uint)];
@@ -11,6 +21,13 @@ namespace TextureDecoder.Yuy2
return output;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="output"></param>
public static void DecompressYUY2(byte[] input, int width, int height, byte[] output)
{
int p = 0;
+1 -1
View File
@@ -2,7 +2,7 @@ using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using TextureDecoder.Yuy2;
using AssetRipper.TextureDecoder.Yuy2;
namespace Yuy2Test
{