Files
PCK-Studio/PckStudio.Core/IO/3DST/TextureCodec.cs
Miku-666 8dfe9cf5b0 3d skin renderer (#50)
* Add 'Validate Skin Dimension' setting

* AddNewSkin - Fix cape box not showing after skin selection

* Extended ResourceLocation for better atlas handling

* ModelImporter - Add block bench export for models inside models.bin

* ModelIporter - Rename 'GetPivot' to 'GetSkinBoxPivot'

* ModelImporter - Fix ANIM2BOX to properly support slim skin models

* ModelImporter - Update BedrockModel json class

* ModelImporter - Update 'FixTexture' to be more generic

* Update ModelContainer API inside OMI-Lib

* Update skin vertex shader to not swap yz

* Update CubeMesh class

* SkinRenderer - Move framebuffer and error checking function to SceneViewport

* SceneViewport - Change OnUpdate parameter

* SkinRenderer - Fix Highlight part having wrong transform

* SkinRenderer - Move call to 'SwapBuffers' into SceneViewport.OnUpdate

* AppSettingsForm - Update API to be more flexible

* SkinBOXExtensions - Update 'GetUVGraphicsPath'

* SkinRenderer - Update 'OnUpdate' function

* SkinRenderer - Add 'LockMousePosition' option

* CustomSkinEditor - Update HighlightlingColor when selecting a part

* CustomSkinEditor - Add render settings

* CustomSkinEditor - Small non-technical changes

* SkinRenderer - Small non-technical changes

* CustomSkinEditor - Load render settings when 'OnLoad' is called

* SkinRenderer - Fix centering leg0/1

* SkinRenderer - Update 'ReInitialzeSkinData' to upload new data to shader

* Rename 'ModelImporter' -> 'SkinModelImporter' and add api interface to add custom import/export providers

* CubeGroupMesh - Fix overlay parts not showing proerly

* SkinRenderer - Fix part highlighting respecting inflate

* Split up model and skin importer into seperate classes and improved api

* IModelImportProvider - Add 'SupportImport' and 'SupportExport' property fields

* ModelImporter - Rename 'SimpleSkinImportProvider' to 'InternalImportProvider'

* modelTextureLocations.json - Add todo

* SkinModelImporter - Move 'ModelTextureLocations' to GameModelImporter

* CustomSkinEditor - Add SettingsManager for RenderSettings

* ModelImporter::Import - Check if file exists

* Rename 'modelTextureLocations' to 'modelMetaData'

* GameModelImporter - Change blockbench name when exporting

* SettingsManager - Add functionality to create internal settings object and add settings to it

* GameModelImporter - Fully implemented game-model export to block bench

* AppSettingsForm - Fix re-adding settings description to default settings

* AppSettingsForm - Add settings description to 'ValidateImageDimension'

* GameModelImporter - Add copyright notice and remove unnecessary using statements

* ModelImporter - Add copyright notice and remove unnecessary using statements

* BlockBenchModel - Fix Texture class json deserialization

* SkinModelImporter - Add 'TryConvertToSkinBoxType' function

* modelMetaData - Remove comments

* SkinModelImporter - Fix 'GetSkinBoxPivot' function

* SkinModelImporter - Add null check in 'FixTexture' function

* SkinModelImporter - Add offset detection when importing skin model

* CustomSkinEditor - Add 'export template' button

* GameModelImporter - Rename 'ModelTextureLocations' -> 'ModelMetaData'

* ModelImporter - Add summary to 'SupportedModelFileFormatsFilter' property

* GameModelImporter - Change function signature of 'CreateElement'

* GameModelImporter - Add options to create root outline

* GameModelImporter - Update Debug message in 'TraverseChildren'

* MainForm - Small code refactor

* Rename class 'Meta' ->'BlockBenchFormatInfo' and update BlockBenchModel.Create function signature

* MainForm - Update 'GetModelTextures' local function

* GameModelImporter - Check model metadata before conversion

* GameModelInfo - Mark class as sealed

* SkinModelImporter - Check if blockbench model uses box uv

* BlockBenchModel - Add export property to class 'Element'

* CustomSkinEditor - Remove unused 'PreviewImage' property

* CustomSkinEditor - Change highlight color on texture

* SkinModelImporter - Fix Block Bench Model import

* modelMetaData - Add meta data for 1.14 models

* SkinModelImporter - Update 'TryConvertToSkinBoxType' function

* SkinModelExporter - Fix model export for bbmodel and bedrock model

* SkinRenderer - Fix order of applying anim animations to match the game

* SkinModelImporter - Fix exception thrown in 'FixTexture'

* CustomSkinEditor - Add Anim editor button and fix anim not being updated when exporting

* SkinModelImporter - Fix offset detection when importing

* SkinModelImporter - Swap box bottom texture when texture is available

* GameModelImporter - Sort using statements

* SkinModelImporter - Small code clean up inside 'ImportBedrockJson'

* SkinModelImporter - Update 'AddBone' function inside 'ExportBedrockJson'

* SkinModelImporter - Fix bottom texture swaping being done bofre parts where imported

* SkinMoelImporter - Rename 'GetSkinBoxPivot' to 'GetSkinPartPivot'

* SceneViewport - Rename 'Init' to 'Initialize'

* SkinModelImporter - Add texture import in 'ImportBedrockJson'

* SkinModelImporter - Fix becrock model import

* Skin-/GameModelImporter - Rename 'fileName' parameter to 'filepath'

* Add ModelEditor

* modelMetaData - Add cavespider texture location

* GameModelImporter - Update 'ExportBlockBenchModel' function

* GameModelImporter - Mark 'ModelExportSettings' as sealed

* ModelEditor -Add Save tool menu item & add TrySetTexture Delegate

* ModelEditor - Add model node icons

* Update CubeMesh & rename CubeGroupMesh to CubeMeshCollection

* ModelEditor - Rename 'GetModelNodes' & 'GetModelPartNodeChildren'

* Update GenericMesh & mesh rendering

* Move Cube conversion into SkinBOXExtensions

* GenericMesh - Made 'Transform' property abstract

* SceneViewport - Add shaderLibrary and api to it

* Rename 'skin...' shaders to 'texturedCube...'

* Update modelMetaData part hierarchy structure

* ShaderProgram - Add 'SetUniform2' overload for System.Drawing.Size

* ModelEditor - Create factory methods for custom model treenodes

* modelMetaData - Add 'slime.armor' texture location & pattern texture locations for 'tropicalfish_-a/-b'

* Move Debug & Camera control into SceneViewport

* Update BoundingBox

* Add ModelRenderer

* ShaderProgram - Update GetUniformLocation to retrive all active uniforms when linking program

* ModelEditor - Add option to show bounding box of the model

* SceneViewport - Add OnPaint override to clear color and depth buffer and enable depth testing

* Update OMI submodule

* Update  Texture base class to accept slot when calling Bind

* Plain color fragment shader - Update uniform names to be PascalCase

* SceneViewport - Add 'ResetCamera' virtual function

* CustomSkinEditor - Add missing render setting 'Show Armor'

* ModelRenderer - Fix centering model after selecting

* Move 'SceneViewport.GetBounds' to 'BoundingBox.GetEnclosingBoundingBox'

* CubeMeshCollection - Implemented 'GetBounds'

* SkinRenderer - Add option to show skins bounding box

* ModelEditor - Update 'GetModelImageIndex'

* SceneViewport - Disable blend when rendering debug graphics

* ModelEditor - Remove 'Model' property in favor to 'LoadModel' function

* JsonModelMetaData - Initialize 'RootParts' to empty array

* BoundingBox - Fix exception when empty enumerable was passed

* CubeMesh - Remove 'SetName' and add constructor with 'name' parameter

* SkinBOX - Change class to record & make member properties getter only

* BoundingBox - Move 'Abs' function into extension class

* SceneViewport - Change 'Transform' to 'GetTransform'

* BoundingBox - Make 'GetVertices' static & add GetTransform

* SkinRenderer - Fix bounds calculation when offset is set & fix part highlighting

* CubeMesh - Move translation & scaling into 'GetTransform'

* CubeMeshCollection - Update 'Contains' overload function & 'SetVisible'

* ModelRenderer - Fix pivot point rendering

* ModelRenderer - Add part highlighting

* modelMetaData - Add missing part to dolphin

* modelMetaData - Add missing parts to dragon

* CubeMesh - Fix 'GetTransform' function

* ModelRenderer - Fix model rotation, pivot & translation issues

* ModelRenderer - Add offset to render transform & camera

* ModelRenderer - Tried fixing alpha rendering issues

* modelMetaData - Add missing part to dragon & add comment

* Add 'ITryGetSet.cs' and useful wrappers for it

* ModelRenderer - Rename 'HighlightInfo.Pivot' to 'HighlightInfo.Translation'

* ITryGetSet - mark classes and interfaces public

* ModelEditor - Add material render support

* ModelRenderer - Add 'TryGetModelMetaData' method

* Fix rendering invisible vertecies

* ModelRenderer - Simplefied populating 'metaData.RootParts' property

* ModelRenderer - Add 'modelOffset' field

* ModelRenderer - Update 'SetModelMaterial'

* ModelRenderer - Add simple way of rendering a 2nd layer of a model(the bed model only for now)

* ModelRenderer - Fix pivot points not working on horse model properly

* ResourceLocation - Add 'Unknown' ResourceLocation instance & improved 'ResourceLocation.GetFromPath'

* ResourceCategory - Add 'MobEntityTextures' & 'ItemEntityTextures'

* Add default model handling (defaults unfinished)

* Add Default Bed model

* Add default chicken model

* Add default cow model

* AddSkinPrompt - Fix Custom skin editor not having anim flag properly set

* SceneViewport - Fix Designer crashing when trying to call 'OnPaint'

* Update OMI submodule

* SceneViewport - Call 'base.OnMouseUp' before our own code

* BlockBenchModel - Fix 'Texture.Name' being null

* ItemSelectionPopUp - Fix 'okBtn_Click' condition

* MainForm - Add export function for default models

* MainForm - Fix model selector ignoring cancel button

* MainForm - Remove unnecessary wrapper for 'entityMaterials.TryGetValue'

* ModelEditor - Add remove model to context menu

* ModelEditor - Add 'GetModelContainer' function

* GameModelImporter - Add import functionality

* MainForm - Add texture when exporting default models

* Add default model for: redcow, pig, snowgolem & dragon head

* Add SkinModel & SkinIdentifier class

* Refactor Skin.cs
- Move texture from 'SkinModel' to Skin.cs
- Move 'Id' from SkinMetaData into it's own class(SkinIdentifier.cs)
- Create SkinModelInfo class for keeping skin conversion simple

* Skin.cs - Rename 'ANIM' property to 'Anim'

* Move 'hasInvalidEntries' into 'MaterialExtensions.HasInvalidEntries'

* Add ISaveContext

* PckAssetExt - Rename parameter names for 'GetSkin'

* Add Editor.cs

* Update most editors to use new Editor class and save context

* CustomSkinEditor - Use Editor as base class

* SkinMetaData - Change to Immutable data type

* PckAssetExtension - [SetSkin] Change adding loc key to setting loc key

* ImageDeserializer - Add format check when deserializing

* MainForm - [HandleSkinFile] Rename some varibale names

* ModelEditor - Use Editor as base class

* Move static variables from 'ModelPartSpecifics' to 'GameConstants'

* Texture.cs - Add IDisposable interface

* PckAssetExtensions - [SetSkin] Add null check for loc file

* AnimationEditor - Fix auto save check

* TextureAtlasEditor - Refactor animation access control

* TextureAtlasEditor - Sort using directives

* MainForm - [HandleTextureFile] Add Debug message when animation has no frames to save

* AddSkinPrompt - Update save context for custom skin editor

* Editor - Move autosave check in 'OnFormClosing'

* ModelRenderer - Update designer specifics

* Merge 'multi-pck-files-feature' into '3dSkinRenderer'

* [WIP] Sub-pck in new tab with savecontext etc.

* SceneViewport - Change base refresh rate to 60 fps

* CustomSkinEditor - Move max offset value into a constant

* ModelEditor - Add highlighting of sinfgle model boxes

* MainForm - Add constant for max pck id value

* CustomSkinEditor - Remove fps slider and re-ordered ui

* EditorForm - Remove abstract from class declaration

* EditorControl - Made virtual funtion throw `NotImplementedException`

* CustomSkinEditor - Fix naming violations

* CustomSkinEditor - Move initialization of render settings into a seperate funtion & remove `show armor` setting

* Move Common functionality to Core project & rendering and Model support as well

* Change namespace of EditorForm & EditorControl

* Add Constant 'NDEBUG' to Core, Rendering & ModelSupport project

* PckStudio.csproj - Remove `defaultModels.json` & `modelMetaData.json`
- files were moved to PckStuido.ModelSupport

* PckStudio.csproj - Remove unused `ApplicationBuildInfo.cs`

* PckStudio.Core - Add NamedData.cs

* PckStudio - Move some Resources to Core

* Add Altas class & refactored Atlas editor

* Update OMI Submodule

* TextureAtlasEditor - Fix clear button not reseting color

* Fix PackInfo.cs - OMI.Endianess -> OMI.ByteOrder

* TextureAtlas - Impl extraction&import of large tiles

* PckStudio.Core - Remove duplicated resources

* LOCEditor - Added menu item for copying loc id

* Core - Move 'MAX_PACK_ID' into GameConstants

* TextureAtlasEditor - small refactor + TODOs

* Update OMI submodule ref
2025-11-11 21:53:32 +01:00

636 lines
26 KiB
C#

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace PckStudio.Core.IO._3DST
{
/// <summary>
/// Format of the texture used on the PICA200.
/// </summary>
public enum _3DSTextureFormat
{
argb8 = 0,
rgb8 = 1,
rgba5551 = 2,
rgb565 = 3,
rgba4 = 4,
la8 = 5,
hilo8 = 6,
l8 = 7,
a8 = 8,
la4 = 9,
l4 = 0xa,
a4 = 0xb,
etc1 = 0xc,
etc1a4 = 0xd,
dontCare
}
class TextureCodec
{
private static readonly int[] tileOrder = { 0, 1, 8, 9, 2, 3, 10, 11, 16, 17, 24, 25, 18, 19, 26, 27, 4, 5, 12, 13, 6, 7, 14, 15, 20, 21, 28, 29, 22, 23, 30, 31, 32, 33, 40, 41, 34, 35, 42, 43, 48, 49, 56, 57, 50, 51, 58, 59, 36, 37, 44, 45, 38, 39, 46, 47, 52, 53, 60, 61, 54, 55, 62, 63 };
private static readonly int[,] etc1LUT = { { 2, 8, -2, -8 }, { 5, 17, -5, -17 }, { 9, 29, -9, -29 }, { 13, 42, -13, -42 }, { 18, 60, -18, -60 }, { 24, 80, -24, -80 }, { 33, 106, -33, -106 }, { 47, 183, -47, -183 } };
private static class TextureConverter
{
/// <summary>
/// Gets a Bitmap from a RGBA8 Texture buffer.
/// </summary>
/// <param name="array">The Buffer</param>
/// <param name="width">Width of the Texture</param>
/// <param name="height">Height of the Texture</param>
/// <returns></returns>
public static Bitmap ToBitmap(byte[] array, int width, int height)
{
Bitmap img = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(array, 0, imgData.Scan0, array.Length);
img.UnlockBits(imgData);
return img;
}
/// <summary>
/// Gets a RGBA8 Texture Buffer from a Bitmap.
/// </summary>
/// <param name="img">The Bitmap</param>
/// <returns></returns>
public static byte[] ToArray(Bitmap img)
{
BitmapData imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte[] array = new byte[imgData.Stride * img.Height];
Marshal.Copy(imgData.Scan0, array, 0, array.Length);
img.UnlockBits(imgData);
return array;
}
}
/// <summary>
/// Decodes a PICA200 Texture.
/// </summary>
/// <param name="data">Buffer with the Texture</param>
/// <param name="width">Width of the Texture</param>
/// <param name="height">Height of the Texture</param>
/// <param name="format">Pixel Format of the Texture</param>
/// <returns></returns>
public static Bitmap Decode(byte[] data, int width, int height, _3DSTextureFormat format)
{
byte[] output = new byte[width * height * 4];
int dataOffset = 0;
bool toggle = false;
switch (format)
{
case _3DSTextureFormat.argb8:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
int outputOffset = ((tX * 8 + x) + ((tY * 8 + y) * width)) * 4;
Buffer.BlockCopy(data, dataOffset + 1, output, outputOffset, 3);
output[outputOffset + 3] = data[dataOffset];
dataOffset += 4;
}
}
}
break;
case _3DSTextureFormat.rgb8:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + ((tY * 8 + y) * width)) * 4;
Buffer.BlockCopy(data, dataOffset, output, (int)outputOffset, 3);
output[outputOffset + 3] = 0xff;
dataOffset += 3;
}
}
}
break;
case _3DSTextureFormat.rgba5551:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + (((tY * 8 + y)) * width)) * 4;
ushort pixelData = (ushort)(data[dataOffset] | (data[dataOffset + 1] << 8));
byte r = (byte)(((pixelData >> 1) & 0x1f) << 3);
byte g = (byte)(((pixelData >> 6) & 0x1f) << 3);
byte b = (byte)(((pixelData >> 11) & 0x1f) << 3);
byte a = (byte)((pixelData & 1) * 0xff);
output[outputOffset] = (byte)(r | (r >> 5));
output[outputOffset + 1] = (byte)(g | (g >> 5));
output[outputOffset + 2] = (byte)(b | (b >> 5));
output[outputOffset + 3] = a;
dataOffset += 2;
}
}
}
break;
case _3DSTextureFormat.rgb565:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + (((tY * 8 + y)) * width)) * 4;
ushort pixelData = (ushort)(data[dataOffset] | (data[dataOffset + 1] << 8));
byte r = (byte)((pixelData & 0x1f) << 3);
byte g = (byte)(((pixelData >> 5) & 0x3f) << 2);
byte b = (byte)(((pixelData >> 11) & 0x1f) << 3);
output[outputOffset] = (byte)(r | (r >> 5));
output[outputOffset + 1] = (byte)(g | (g >> 6));
output[outputOffset + 2] = (byte)(b | (b >> 5));
output[outputOffset + 3] = 0xff;
dataOffset += 2;
}
}
}
break;
case _3DSTextureFormat.rgba4:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + (((tY * 8 + y)) * width)) * 4;
ushort pixelData = (ushort)(data[dataOffset] | (data[dataOffset + 1] << 8));
byte r = (byte)((pixelData >> 4) & 0xf);
byte g = (byte)((pixelData >> 8) & 0xf);
byte b = (byte)((pixelData >> 12) & 0xf);
byte a = (byte)(pixelData & 0xf);
output[outputOffset] = (byte)(r | (r << 4));
output[outputOffset + 1] = (byte)(g | (g << 4));
output[outputOffset + 2] = (byte)(b | (b << 4));
output[outputOffset + 3] = (byte)(a | (a << 4));
dataOffset += 2;
}
}
}
break;
case _3DSTextureFormat.la8:
case _3DSTextureFormat.hilo8:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + (((tY * 8 + y)) * width)) * 4;
output[outputOffset] = data[dataOffset];
output[outputOffset + 1] = data[dataOffset];
output[outputOffset + 2] = data[dataOffset];
output[outputOffset + 3] = data[dataOffset + 1];
dataOffset += 2;
}
}
}
break;
case _3DSTextureFormat.l8:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + (((tY * 8 + y)) * width)) * 4;
output[outputOffset] = data[dataOffset];
output[outputOffset + 1] = data[dataOffset];
output[outputOffset + 2] = data[dataOffset];
output[outputOffset + 3] = 0xff;
dataOffset++;
}
}
}
break;
case _3DSTextureFormat.a8:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + (((tY * 8 + y)) * width)) * 4;
output[outputOffset] = 0xff;
output[outputOffset + 1] = 0xff;
output[outputOffset + 2] = 0xff;
output[outputOffset + 3] = data[dataOffset];
dataOffset++;
}
}
}
break;
case _3DSTextureFormat.la4:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + (((tY * 8 + y)) * width)) * 4;
output[outputOffset] = (byte)(data[dataOffset] >> 4);
output[outputOffset + 1] = (byte)(data[dataOffset] >> 4);
output[outputOffset + 2] = (byte)(data[dataOffset] >> 4);
output[outputOffset + 3] = (byte)(data[dataOffset] & 0xf);
dataOffset++;
}
}
}
break;
case _3DSTextureFormat.l4:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + (((tY * 8 + y)) * width)) * 4;
byte c = toggle ? (byte)((data[dataOffset++] & 0xf0) >> 4) : (byte)(data[dataOffset] & 0xf);
toggle = !toggle;
c = (byte)((c << 4) | c);
output[outputOffset] = c;
output[outputOffset + 1] = c;
output[outputOffset + 2] = c;
output[outputOffset + 3] = 0xff;
}
}
}
break;
case _3DSTextureFormat.a4:
for (int tY = 0; tY < height / 8; tY++)
{
for (int tX = 0; tX < width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
long outputOffset = ((tX * 8) + x + (((tY * 8 + y)) * width)) * 4;
output[outputOffset] = 0xff;
output[outputOffset + 1] = 0xff;
output[outputOffset + 2] = 0xff;
byte a = toggle ? (byte)((data[dataOffset++] & 0xf0) >> 4) : (byte)(data[dataOffset] & 0xf);
toggle = !toggle;
output[outputOffset + 3] = (byte)((a << 4) | a);
}
}
}
break;
case _3DSTextureFormat.etc1:
case _3DSTextureFormat.etc1a4:
byte[] decodedData = etc1Decode(data, width, height, format == _3DSTextureFormat.etc1a4);
int[] etc1Order = etc1Scramble(width, height);
int i = 0;
for (int tY = 0; tY < height / 4; tY++)
{
for (int tX = 0; tX < width / 4; tX++)
{
int TX = etc1Order[i] % (width / 4);
int TY = (etc1Order[i] - TX) / (width / 4);
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
dataOffset = ((TX * 4) + x + (((TY * 4) + y) * width)) * 4;
long outputOffset = ((tX * 4) + x + (((tY * 4 + y)) * width)) * 4;
Buffer.BlockCopy(decodedData, (int)dataOffset, output, (int)outputOffset, 4);
}
}
i += 1;
}
}
break;
}
return TextureConverter.ToBitmap(output, width, height);
}
/// <summary>
/// Encodes a PICA200 Texture.
/// </summary>
/// <param name="img">Input image to be encoded</param>
/// <param name="format">Pixel Format of the Texture</param>
/// <returns></returns>
public static byte[] Encode(Bitmap img, _3DSTextureFormat format)
{
byte[] data = TextureConverter.ToArray(img);
byte[] output = new byte[data.Length];
int outputOffset = 0;
switch (format)
{
case _3DSTextureFormat.argb8:
for (int tY = 0; tY < img.Height / 8; tY++)
{
for (int tX = 0; tX < img.Width / 8; tX++)
{
for (int pixel = 0; pixel < 64; pixel++)
{
int x = tileOrder[pixel] % 8;
int y = (tileOrder[pixel] - x) / 8;
int dataOffset = ((tX * 8) + x + (tY * 8 + y) * img.Width) * 4;
Buffer.BlockCopy(data, dataOffset, output, outputOffset + 1, 3);
output[outputOffset] = data[dataOffset + 3];
outputOffset += 4;
}
}
}
break;
default: throw new NotImplementedException(nameof(format));
}
return output;
}
#region "ETC1"
private static byte[] etc1Decode(byte[] input, int width, int height, bool alpha)
{
byte[] output = new byte[(width * height * 4)];
long offset = 0;
for (int y = 0; y < height / 4; y++)
{
for (int x = 0; x < width / 4; x++)
{
byte[] colorBlock = new byte[8];
byte[] alphaBlock = new byte[8];
if (alpha)
{
for (int i = 0; i < 8; i++)
{
colorBlock[7 - i] = input[offset + 8 + i];
alphaBlock[i] = input[offset + i];
}
offset += 16;
}
else
{
for (int i = 0; i < 8; i++)
{
colorBlock[7 - i] = input[offset + i];
alphaBlock[i] = 0xff;
}
offset += 8;
}
colorBlock = etc1DecodeBlock(colorBlock);
bool toggle = false;
long alphaOffset = 0;
for (int tX = 0; tX < 4; tX++)
{
for (int tY = 0; tY < 4; tY++)
{
int outputOffset = (x * 4 + tX + ((y * 4 + tY) * width)) * 4;
int blockOffset = (tX + (tY * 4)) * 4;
Buffer.BlockCopy(colorBlock, blockOffset, output, outputOffset, 3);
byte a = toggle ? (byte)((alphaBlock[alphaOffset++] & 0xf0) >> 4) : (byte)(alphaBlock[alphaOffset] & 0xf);
output[outputOffset + 3] = (byte)((a << 4) | a);
toggle = !toggle;
}
}
}
}
return output;
}
private static byte[] etc1DecodeBlock(byte[] data)
{
uint blockTop = BitConverter.ToUInt32(data, 0);
uint blockBottom = BitConverter.ToUInt32(data, 4);
bool flip = (blockTop & 0x1000000) > 0;
bool difference = (blockTop & 0x2000000) > 0;
uint r1, g1, b1;
uint r2, g2, b2;
if (difference)
{
r1 = blockTop & 0xf8;
g1 = (blockTop & 0xf800) >> 8;
b1 = (blockTop & 0xf80000) >> 16;
r2 = (uint)((sbyte)(r1 >> 3) + ((sbyte)((blockTop & 7) << 5) >> 5));
g2 = (uint)((sbyte)(g1 >> 3) + ((sbyte)((blockTop & 0x700) >> 3) >> 5));
b2 = (uint)((sbyte)(b1 >> 3) + ((sbyte)((blockTop & 0x70000) >> 11) >> 5));
r1 |= r1 >> 5;
g1 |= g1 >> 5;
b1 |= b1 >> 5;
r2 = (r2 << 3) | (r2 >> 2);
g2 = (g2 << 3) | (g2 >> 2);
b2 = (b2 << 3) | (b2 >> 2);
}
else
{
r1 = blockTop & 0xf0;
g1 = (blockTop & 0xf000) >> 8;
b1 = (blockTop & 0xf00000) >> 16;
r2 = (blockTop & 0xf) << 4;
g2 = (blockTop & 0xf00) >> 4;
b2 = (blockTop & 0xf0000) >> 12;
r1 |= r1 >> 4;
g1 |= g1 >> 4;
b1 |= b1 >> 4;
r2 |= r2 >> 4;
g2 |= g2 >> 4;
b2 |= b2 >> 4;
}
uint table1 = (blockTop >> 29) & 7;
uint table2 = (blockTop >> 26) & 7;
byte[] output = new byte[(4 * 4 * 4)];
if (!flip)
{
for (int y = 0; y <= 3; y++)
{
for (int x = 0; x <= 1; x++)
{
Color color1 = etc1Pixel(r1, g1, b1, x, y, blockBottom, table1);
Color color2 = etc1Pixel(r2, g2, b2, x + 2, y, blockBottom, table2);
int offset1 = (y * 4 + x) * 4;
output[offset1] = color1.B;
output[offset1 + 1] = color1.G;
output[offset1 + 2] = color1.R;
int offset2 = (y * 4 + x + 2) * 4;
output[offset2] = color2.B;
output[offset2 + 1] = color2.G;
output[offset2 + 2] = color2.R;
}
}
}
else
{
for (int y = 0; y <= 1; y++)
{
for (int x = 0; x <= 3; x++)
{
Color color1 = etc1Pixel(r1, g1, b1, x, y, blockBottom, table1);
Color color2 = etc1Pixel(r2, g2, b2, x, y + 2, blockBottom, table2);
int offset1 = (y * 4 + x) * 4;
output[offset1] = color1.B;
output[offset1 + 1] = color1.G;
output[offset1 + 2] = color1.R;
int offset2 = ((y + 2) * 4 + x) * 4;
output[offset2] = color2.B;
output[offset2 + 1] = color2.G;
output[offset2 + 2] = color2.R;
}
}
}
return output;
}
private static Color etc1Pixel(uint r, uint g, uint b, int x, int y, uint block, uint table)
{
int index = x * 4 + y;
uint MSB = block << 1;
int pixel = index < 8
? etc1LUT[table, ((block >> (index + 24)) & 1) + ((MSB >> (index + 8)) & 2)]
: etc1LUT[table, ((block >> (index + 8)) & 1) + ((MSB >> (index - 8)) & 2)];
r = saturate((int)(r + pixel));
g = saturate((int)(g + pixel));
b = saturate((int)(b + pixel));
return Color.FromArgb((int)r, (int)g, (int)b);
}
private static byte saturate(int value)
{
if (value > 0xff)
return 0xff;
if (value < 0)
return 0;
return (byte)(value & 0xff);
}
private static int[] etc1Scramble(int width, int height)
{
//Maybe theres a better way to do this?
int[] tileScramble = new int[((width / 4) * (height / 4))];
int baseAccumulator = 0;
int rowAccumulator = 0;
int baseNumber = 0;
int rowNumber = 0;
for (int tile = 0; tile < tileScramble.Length; tile++)
{
if ((tile % (width / 4) == 0) && tile > 0)
{
if (rowAccumulator < 1)
{
rowAccumulator += 1;
rowNumber += 2;
baseNumber = rowNumber;
}
else
{
rowAccumulator = 0;
baseNumber -= 2;
rowNumber = baseNumber;
}
}
tileScramble[tile] = baseNumber;
if (baseAccumulator < 1)
{
baseAccumulator++;
baseNumber++;
}
else
{
baseAccumulator = 0;
baseNumber += 3;
}
}
return tileScramble;
}
#endregion
}
}