(DdsFormat): thorw exceptions for non-supported formats.

This commit is contained in:
morkt 2016-03-12 12:47:40 +04:00
parent 0600805f06
commit ff7669f745

View File

@ -2,7 +2,7 @@
//! \date Thu Jul 23 18:12:05 2015 //! \date Thu Jul 23 18:12:05 2015
//! \brief Direct Draw Surface image format. //! \brief Direct Draw Surface image format.
// //
// Copyright (C) 2015 by morkt // Copyright (C) 2015-2016 by morkt
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
@ -23,8 +23,10 @@
// IN THE SOFTWARE. // IN THE SOFTWARE.
// //
using System;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Text;
using System.Windows.Media; using System.Windows.Media;
using GameRes.Utility; using GameRes.Utility;
@ -32,8 +34,20 @@ namespace GameRes.Formats.Microsoft
{ {
internal class DdsMetaData : ImageMetaData internal class DdsMetaData : ImageMetaData
{ {
public int DataOffset; public int DataOffset;
public uint PixelFlags; public DdsPF PixelFlags;
public string FourCC;
}
[Flags]
internal enum DdsPF : uint
{
AlphaPixels = 1,
Alpha = 2,
FourCC = 4,
Rgb = 0x40,
Yuv = 0x200,
Luminance = 0x20000,
} }
[Export(typeof(ImageFormat))] [Export(typeof(ImageFormat))]
@ -51,30 +65,38 @@ namespace GameRes.Formats.Microsoft
int dwSize = LittleEndian.ToInt32 (header, 4); int dwSize = LittleEndian.ToInt32 (header, 4);
if (dwSize < 0x7C) if (dwSize < 0x7C)
return null; return null;
uint bitflag = LittleEndian.ToUInt32 (header, 0x50); var bitflags = (DdsPF)LittleEndian.ToUInt32 (header, 0x50);
string four_cc = null;
if (bitflags.HasFlag (DdsPF.FourCC))
four_cc = Binary.GetCString (header, 0x54, 4, Encoding.ASCII);
return new DdsMetaData return new DdsMetaData
{ {
Width = LittleEndian.ToUInt32 (header, 0x10), Width = LittleEndian.ToUInt32 (header, 0x10),
Height = LittleEndian.ToUInt32 (header, 0xC), Height = LittleEndian.ToUInt32 (header, 0xC),
BPP = LittleEndian.ToInt32 (header, 0x58), BPP = LittleEndian.ToInt32 (header, 0x58),
PixelFlags = LittleEndian.ToUInt32 (header, 0x50), PixelFlags = bitflags,
FourCC = four_cc,
DataOffset = 4 + dwSize, DataOffset = 4 + dwSize,
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (Stream stream, ImageMetaData info)
{ {
var meta = info as DdsMetaData; var meta = (DdsMetaData)info;
if (null == meta) if (meta.PixelFlags.HasFlag (DdsPF.Yuv | DdsPF.Luminance))
throw new System.ArgumentException ("DdsFormat.Read should be supplied with DdsMetaData", "info"); throw new NotSupportedException ("Not supported DDS texture color format");
stream.Position = meta.DataOffset; if (!string.IsNullOrEmpty (meta.FourCC))
throw new NotImplementedException ("Compressed DDS textures not implemented");
PixelFormat format; PixelFormat format;
if (24 == info.BPP) if (24 == info.BPP)
format = PixelFormats.Bgr24; format = PixelFormats.Bgr24;
else if (1 == (meta.PixelFlags & 1)) else if (16 == info.BPP)
throw new NotImplementedException ("16bpp DDS textures not implemented");
else if (meta.PixelFlags.HasFlag (DdsPF.AlphaPixels))
format = PixelFormats.Bgra32; format = PixelFormats.Bgra32;
else else
format = PixelFormats.Bgr32; format = PixelFormats.Bgr32;
stream.Position = meta.DataOffset;
var pixels = new byte[info.Width*info.Height*((format.BitsPerPixel+7)/8)]; var pixels = new byte[info.Width*info.Height*((format.BitsPerPixel+7)/8)];
if (pixels.Length != stream.Read (pixels, 0, pixels.Length)) if (pixels.Length != stream.Read (pixels, 0, pixels.Length))
throw new InvalidFormatException ("Unexpected end of file"); throw new InvalidFormatException ("Unexpected end of file");