-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
204 lines (162 loc) · 8.63 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.IO;
using BCnEncoder.Encoder;
using BCnEncoder.Decoder;
using BCnEncoder.Shared;
using BCnEncoder.ImageSharp;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using BCnEncoder.Shared.ImageFiles;
using System.Text;
using SixLabors.ImageSharp.Formats.Bmp;
using Microsoft.Toolkit.HighPerformance;
using txtr_converter;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors;
using System.Windows.Media.Imaging;
//using txtr_converter.Extensions;
//using ColorRgba32 = txtr_converter.Extensions.ColorRgba32;
public class Program
{
/*
public static void EncodeImageToTxtr()
{
using Image<Rgba32> image = Image.Load<Rgba32>("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\custom_image.png");
using FileStream fs = File.OpenWrite("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\logo_d.txtr");
BinaryWriterTxtrtRaw(image,fs);
}
public static void DecodeTxtrToImage()
{
using FileStream fs = File.OpenRead("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\atlas_gameicons.txtr");
BinaryReaderTxtr(in fs, out KtxFile ktx);
BcDecoder decoder = new BcDecoder();
using Image<Rgba32> image = decoder.DecodeToImageRgba32(ktx);
using FileStream outFs = File.OpenWrite("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\custom_image.png");
image.SaveAsPng(outFs);
}
public static void ConvertTxtrToKtx()
{
using FileStream fs = File.OpenRead("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\logo_d.txtr");
BinaryReaderTxtr(in fs, out KtxFile ktx);
using FileStream fs2 = File.OpenWrite("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\atlas_gameicons_custom.ktx");
ktx.Write(fs2);
}
static void decode()
{
using FileStream fs = File.OpenRead("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\atlas_gameicons.dds");
BcDecoder decoder = new BcDecoder();
using Image<Rgba32> image = decoder.DecodeToImageRgba32(fs);
using FileStream outFs = File.OpenWrite("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\custom.png");
image.SaveAsPng(outFs);
}
static void encode()
{
using Image<Rgba32> image = Image.Load<Rgba32>("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\custom_image.png");
BcEncoder encoder = new BcEncoder();
encoder.OutputOptions.GenerateMipMaps = false;
encoder.OutputOptions.Quality = CompressionQuality.Balanced;
encoder.OutputOptions.Format = CompressionFormat.Rgba;
encoder.OutputOptions.FileFormat = OutputFileFormat.Dds;
using FileStream fs = File.OpenWrite("C:\\Users\\X605\\source\\repos\\txtrConverter\\txtr converter\\bin\\Debug\\net6.0\\logoSingle_raw.dds");
encoder.EncodeToStream(image, fs);
}
*/
public static void BinaryWriterTxtrtRaw(in Image<Rgba32> image, FileStream fs)
{
var input = BCnEncoderExtensions.ImageToMemory2D(image);
BcEncoder encoder = new BcEncoder();
encoder.OutputOptions.GenerateMipMaps = false;
encoder.OutputOptions.Quality = CompressionQuality.BestQuality;
encoder.OutputOptions.Format = CompressionFormat.Rgba;
encoder.OutputOptions.FileFormat = OutputFileFormat.Ktx;
var ktxFile = encoder.EncodeToKtx(input);
TxtrFile txtrFile = new();
txtrFile = txtrFile.ConvertKtxToTxtrFile(ktxFile, TxtrFormat.Raw_Single_Mip);
txtrFile.Write(fs);
}
public static void BinaryWriterTxtrtCompressed(in Image<Rgba32> image, FileStream fs)
{
var input = BCnEncoderExtensions.ImageToMemory2D(image);
BcEncoder encoder = new BcEncoder();
encoder.OutputOptions.GenerateMipMaps = true;
encoder.OutputOptions.Quality = CompressionQuality.BestQuality;
encoder.OutputOptions.Format = CompressionFormat.Bc3;
encoder.OutputOptions.FileFormat = OutputFileFormat.Ktx;
var ktxFile = encoder.EncodeToKtx(input);
TxtrFile txtrFile = new();
txtrFile = txtrFile.ConvertKtxToTxtrFile(ktxFile, TxtrFormat.Compressed_MipMaped_Dxt5);
txtrFile.Write(fs);
}
public static void BinaryReaderTxtr(in FileStream fs, out KtxFile ktx, out bool isCompressed)
{
isCompressed = false;
ktx = new KtxFile();
using (var br = new BinaryReader(fs, Encoding.UTF8, true))
{
TxtrHeader txtrHeader = new();
txtrHeader.identifier = br.ReadUInt32(); // txtr
if (txtrHeader.identifier != 1381259348)
{
throw new FormatException("File doesn't contain txtr.");
}
txtrHeader.flag = (TxtrFlag)br.ReadUInt32(); // SW uses value 3 and CC2 value 2
if (txtrHeader.flag != TxtrFlag.SW)
{
throw new FormatException("Uknown flag. File is not from SW.");
}
txtrHeader.format = (TxtrFormat)br.ReadUInt32(); // value 2 uncompressed, value 3 compressed bc3/dxt5
if (txtrHeader.format != TxtrFormat.Raw_Single_Mip && txtrHeader.format != TxtrFormat.Compressed_MipMaped_Dxt5)
{
throw new FormatException("Uknown type.");
}
txtrHeader.padding = br.ReadByte(); // padding?
txtrHeader.height = br.ReadUInt16();
txtrHeader.width = br.ReadUInt16();
txtrHeader.mipMapCount = br.ReadUInt16();
Console.WriteLine("identifier {0}", txtrHeader.identifier);
Console.WriteLine("flag {0}", txtrHeader.flag);
Console.WriteLine("type {0}", txtrHeader.format);
Console.WriteLine("padding {0}", txtrHeader.padding);
Console.WriteLine("height {0}", txtrHeader.height);
Console.WriteLine("width {0}", txtrHeader.width);
Console.WriteLine("mipMapCount {0}", txtrHeader.mipMapCount);
switch (txtrHeader.format)
{
case TxtrFormat.Raw_Single_Mip:
var rawHeader = KtxHeader.InitializeCompressed(txtrHeader.width, txtrHeader.height, GlInternalFormat.GlRgba8, GlFormat.GlRgba);
ktx.header = rawHeader;
for (int mip = 0; mip < 1; mip++)
{
uint dataSize = br.ReadUInt32();
byte[] data = br.ReadBytes((int)dataSize);
Console.WriteLine("dataSize {0}", dataSize);
ktx.MipMaps.Add(new KtxMipmap((uint)data.Length, (uint)txtrHeader.width, (uint)txtrHeader.height, 1));
ktx.MipMaps[mip].Faces[0] = new KtxMipFace(data, (uint)txtrHeader.width, (uint)txtrHeader.height);
}
ktx.header.NumberOfFaces = 1;
ktx.header.NumberOfMipmapLevels = txtrHeader.mipMapCount;
break;
case TxtrFormat.Compressed_MipMaped_Dxt5:
var compHeader = KtxHeader.InitializeCompressed(txtrHeader.width, txtrHeader.height, GlInternalFormat.GlCompressedRgbaS3TcDxt5Ext, GlFormat.GlRgba);
ktx.header = compHeader;
isCompressed = true;
//var mipChain = GenerateMipChain(ref width, ref height, ref mipMapCount);
for (int mipmaps = 0; mipmaps < txtrHeader.mipMapCount; mipmaps++)
{
uint dataSize = br.ReadUInt32();
byte[] data = br.ReadBytes((int)dataSize);
Console.WriteLine("dataSize {0}", dataSize);
/*
ktx.MipMaps.Add(new KtxMipmap((uint)data.Length, (uint)mipChain[mipmaps].Width, (uint)mipChain[mipmaps].Height, 1));
ktx.MipMaps[mipmaps].Faces[0] = new KtxMipFace(data, (uint)mipChain[mipmaps].Width, (uint)mipChain[mipmaps].Height);
*/
ktx.MipMaps.Add(new KtxMipmap((uint)data.Length, (uint)txtrHeader.width, (uint)txtrHeader.height, 1));
ktx.MipMaps[mipmaps].Faces[0] = new KtxMipFace(data, (uint)txtrHeader.width, (uint)txtrHeader.height);
}
ktx.header.NumberOfFaces = 1;
ktx.header.NumberOfMipmapLevels = txtrHeader.mipMapCount;
break;
}
}
}
}