-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
921 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using BinderTool.Core.Dds.Enum; | ||
|
||
namespace BinderTool.Core.Dds | ||
{ | ||
public class DdsFile | ||
{ | ||
private const int MagicNumber = 0x20534444; | ||
|
||
public DdsFileHeader Header { get; set; } | ||
|
||
public DdsFileHeaderDx10 HeaderDx10 { get; set; } | ||
|
||
public byte[] Data { get; set; } | ||
|
||
public void Write(Stream outputStream) | ||
{ | ||
BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true); | ||
writer.Write(MagicNumber); | ||
Header.Write(outputStream); | ||
if (Header.IsDx10()) | ||
{ | ||
HeaderDx10.Write(outputStream); | ||
} | ||
|
||
writer.Write(Data); | ||
} | ||
|
||
public static byte[] ConvertData(byte[] sourceBuffer, int height, int width, DxgiFormat dxgiFormat) | ||
{ | ||
if (sourceBuffer == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var inputStream = new MemoryStream(sourceBuffer); | ||
byte[] targetBuffer = new byte[sourceBuffer.Length]; | ||
byte[] blockBuffer = new byte[64]; | ||
int heightBlock = height / 4; | ||
int widthBlock = width / 4; | ||
int bytesPerPixel = DdsPixelFormat.DxgiToBytesPerPixel(dxgiFormat) * 2; | ||
for (int y = 0; y < heightBlock; y++) | ||
{ | ||
for (int x = 0; x < widthBlock; x++) | ||
{ | ||
int mx = x; | ||
int my = y; | ||
if (widthBlock > 1 && heightBlock > 1) | ||
{ | ||
MapBlockPosition(x, y, widthBlock, 2, out mx, out my); | ||
} | ||
|
||
if (widthBlock > 2 && heightBlock > 2) | ||
{ | ||
MapBlockPosition(mx, my, widthBlock, 4, out mx, out my); | ||
} | ||
|
||
if (widthBlock > 4 && heightBlock > 4) | ||
{ | ||
MapBlockPosition(mx, my, widthBlock, 8, out mx, out my); | ||
} | ||
|
||
inputStream.Read(blockBuffer, 0, bytesPerPixel); | ||
int destinationIndex = bytesPerPixel * (my * widthBlock + mx); | ||
Array.Copy(blockBuffer, 0, targetBuffer, destinationIndex, bytesPerPixel); | ||
} | ||
} | ||
|
||
return targetBuffer; | ||
} | ||
|
||
private static void MapBlockPosition(int x, int y, int w, int bx, out int mx, out int my) | ||
{ | ||
int num1 = bx / 2; | ||
int num2 = x / bx; | ||
int num3 = y / num1; | ||
int num4 = x % bx; | ||
int num5 = y % num1; | ||
int num6 = w / bx; | ||
int num7 = 2 * num6; | ||
int num8 = num2 + num3 * num6; | ||
int num9 = num8 % num7; | ||
int num10 = num9 / 2 + num9 % 2 * num6; | ||
int num11 = num8 / num7 * num7 + num10; | ||
int num12 = num11 % num6; | ||
int num13 = num11 / num6; | ||
|
||
mx = num12 * bx + num4; | ||
my = num13 * num1 + num5; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using BinderTool.Core.Dds.Enum; | ||
|
||
namespace BinderTool.Core.Dds | ||
{ | ||
public class DdsFileHeader | ||
{ | ||
public const int DefaultHeaderSize = 124; | ||
|
||
public int Size { get; set; } = DefaultHeaderSize; | ||
|
||
public DdsFileHeaderFlags Flags { get; set; } | ||
|
||
public int Height { get; set; } | ||
|
||
public int Width { get; set; } | ||
|
||
public int PitchOrLinearSize { get; set; } | ||
|
||
public int Depth { get; set; } | ||
|
||
public int MipMapCount { get; set; } | ||
|
||
public DdsPixelFormat PixelFormat { get; set; } | ||
|
||
public DdsSurfaceFlags Caps { get; set; } | ||
|
||
public DdsCaps2Flags Caps2 { get; set; } | ||
|
||
public int Caps3 { get; set; } | ||
|
||
public int Caps4 { get; set; } | ||
|
||
public void Write(Stream outputStream) | ||
{ | ||
BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true); | ||
writer.Write(Size); | ||
writer.Write(Convert.ToInt32(Flags)); | ||
writer.Write(Height); | ||
writer.Write(Width); | ||
writer.Write(PitchOrLinearSize); | ||
writer.Write(Depth); | ||
writer.Write(MipMapCount); | ||
// int Reserved1[11]; | ||
writer.WriteZeros(44); | ||
PixelFormat.Write(outputStream); | ||
writer.Write(Convert.ToInt32(Caps)); | ||
writer.Write(Convert.ToInt32(Caps2)); | ||
writer.Write(Caps3); | ||
writer.Write(Caps4); | ||
// int Reserved2 | ||
writer.WriteZeros(4); | ||
} | ||
|
||
public bool IsDx10() | ||
{ | ||
return PixelFormat != null && | ||
PixelFormat.Flags.HasFlag(DdsPixelFormatFlag.FourCc) && | ||
PixelFormat.FourCc == DdsPixelFormat.Dx10FourCc; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"Size: {Size}, Flags: {Flags}, Height: {Height}, Width: {Width}," + | ||
$" PitchOrLinearSize: {PitchOrLinearSize}, Depth: {Depth}, MipMapCount: {MipMapCount}," + | ||
$" PixelFormat: {PixelFormat}, Caps: {Caps}, Caps2: {Caps2}, Caps3: {Caps3}, " + $"Caps4: {Caps4}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using BinderTool.Core.Dds.Enum; | ||
|
||
namespace BinderTool.Core.Dds | ||
{ | ||
public class DdsFileHeaderDx10 | ||
{ | ||
public DxgiFormat Format { get; set; } | ||
|
||
public D3D10ResourceDimension ResourceDimension { get; set; } | ||
|
||
public uint MiscFlag { get; set; } | ||
|
||
public uint ArraySize { get; set; } | ||
|
||
public uint MiscFlags2 { get; set; } | ||
|
||
public void Write(Stream outputStream) | ||
{ | ||
BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true); | ||
writer.Write(Convert.ToUInt32(Format)); | ||
writer.Write(Convert.ToInt32(ResourceDimension)); | ||
writer.Write(MiscFlag); | ||
writer.Write(ArraySize); | ||
writer.Write(MiscFlags2); | ||
} | ||
} | ||
} |
Oops, something went wrong.