Skip to content

Commit 353ea1c

Browse files
committed
Add more format checks
1 parent c471403 commit 353ea1c

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

BCnEnc.Net/Decoder/BcDecoder.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,10 @@ private ColorRgba32[][] DecodeInternal(DdsFile file, bool allMipMaps, Cancellati
12611261
/// <returns>The decoded Rgba32 image.</returns>
12621262
private ColorRgba32[] DecodeRawInternal(ReadOnlyMemory<byte> input, int pixelWidth, int pixelHeight, CompressionFormat format, CancellationToken token)
12631263
{
1264+
if (format == CompressionFormat.Unknown)
1265+
{
1266+
throw new ArgumentException($"Unsupported compression format: {format}");
1267+
}
12641268
if (input.Length % GetBlockSize(format) != 0)
12651269
{
12661270
throw new ArgumentException("The size of the input buffer does not align with the compression format.");
@@ -1387,6 +1391,11 @@ private Memory2D<ColorRgbFloat>[] DecodeFromStreamInternalHdr2D(Stream stream, b
13871391
/// <returns>An array of decoded Rgba32 images.</returns>
13881392
private ColorRgbFloat[][] DecodeInternalHdr(KtxFile file, bool allMipMaps, CancellationToken token)
13891393
{
1394+
if (GetCompressionFormat(file.header.GlInternalFormat) == CompressionFormat.Unknown)
1395+
{
1396+
throw new ArgumentException($"Unsupported compression format: {file.header.GlInternalFormat}");
1397+
}
1398+
13901399
var mipMaps = allMipMaps ? file.MipMaps.Count : 1;
13911400
var colors = new ColorRgbFloat[mipMaps][];
13921401

@@ -1439,7 +1448,25 @@ private ColorRgbFloat[][] DecodeInternalHdr(KtxFile file, bool allMipMaps, Cance
14391448
/// <returns>An array of decoded Rgba32 images.</returns>
14401449
private ColorRgbFloat[][] DecodeInternalHdr(DdsFile file, bool allMipMaps, CancellationToken token)
14411450
{
1451+
if (file == null)
1452+
{
1453+
throw new ArgumentNullException(nameof(file));
1454+
}
1455+
1456+
var dxtFormat = file.header.ddsPixelFormat.IsDxt10Format
1457+
? file.dx10Header.dxgiFormat
1458+
: file.header.ddsPixelFormat.DxgiFormat;
1459+
1460+
if (GetCompressionFormat(file) == CompressionFormat.Unknown)
1461+
{
1462+
throw new ArgumentException($"Unsupported compression format: {dxtFormat}");
1463+
}
1464+
14421465
var mipMaps = allMipMaps ? file.header.dwMipMapCount : 1;
1466+
1467+
// Assume at least 1 mip map
1468+
mipMaps = Math.Max(1, mipMaps);
1469+
14431470
var colors = new ColorRgbFloat[mipMaps][];
14441471

14451472
var context = new OperationContext
@@ -1455,9 +1482,7 @@ private ColorRgbFloat[][] DecodeInternalHdr(DdsFile file, bool allMipMaps, Cance
14551482

14561483
context.Progress = new OperationProgress(Options.Progress, totalBlocks);
14571484

1458-
var dxtFormat = file.header.ddsPixelFormat.IsDxt10Format
1459-
? file.dx10Header.dxgiFormat
1460-
: file.header.ddsPixelFormat.DxgiFormat;
1485+
14611486
var format = GetCompressionFormat(file);
14621487
var decoder = GetRgbFloatDecoder(format);
14631488

@@ -1499,6 +1524,10 @@ private ColorRgbFloat[][] DecodeInternalHdr(DdsFile file, bool allMipMaps, Cance
14991524
/// <returns>The decoded Rgba32 image.</returns>
15001525
private ColorRgbFloat[] DecodeRawInternalHdr(ReadOnlyMemory<byte> input, int pixelWidth, int pixelHeight, CompressionFormat format, CancellationToken token)
15011526
{
1527+
if (format == CompressionFormat.Unknown)
1528+
{
1529+
throw new ArgumentException($"Unsupported compression format: {format}");
1530+
}
15021531
if (input.Length % GetBlockSize(format) != 0)
15031532
{
15041533
throw new ArgumentException("The size of the input buffer does not align with the compression format.");

0 commit comments

Comments
 (0)