From b551dd47ec332746b31a63001d377fb9e29788bb Mon Sep 17 00:00:00 2001 From: Saalvage <29021710+Saalvage@users.noreply.github.com> Date: Wed, 12 Aug 2020 04:17:00 +0200 Subject: [PATCH] Removed unused providers --- CBRE.Editor/Editor.cs | 2 - CBRE.Editor/Settings/FileTypeRegistration.cs | 6 - CBRE.Providers/CBRE.Providers.csproj | 5 - CBRE.Providers/Map/MapFormatProvider.cs | 287 ---------- CBRE.Providers/Map/RmfProvider.cs | 401 ------------- CBRE.Providers/Texture/Vtf/VtfImageFlag.cs | 47 -- CBRE.Providers/Texture/Vtf/VtfImageFormat.cs | 45 -- CBRE.Providers/Texture/Vtf/VtfProvider.cs | 574 ------------------- 8 files changed, 1367 deletions(-) delete mode 100644 CBRE.Providers/Map/MapFormatProvider.cs delete mode 100644 CBRE.Providers/Map/RmfProvider.cs delete mode 100644 CBRE.Providers/Texture/Vtf/VtfImageFlag.cs delete mode 100644 CBRE.Providers/Texture/Vtf/VtfImageFormat.cs delete mode 100644 CBRE.Providers/Texture/Vtf/VtfProvider.cs diff --git a/CBRE.Editor/Editor.cs b/CBRE.Editor/Editor.cs index 2ce3e021d..d5bd89b1a 100644 --- a/CBRE.Editor/Editor.cs +++ b/CBRE.Editor/Editor.cs @@ -115,8 +115,6 @@ private void EditorLoad(object sender, EventArgs e) { } TextureProvider.SetCachePath(SettingsManager.GetTextureCachePath()); - MapProvider.Register(new RmfProvider()); - MapProvider.Register(new MapFormatProvider()); MapProvider.Register(new VmfProvider()); MapProvider.Register(new L3DWProvider()); GameDataProvider.Register(new FgdProvider()); diff --git a/CBRE.Editor/Settings/FileTypeRegistration.cs b/CBRE.Editor/Settings/FileTypeRegistration.cs index 187e8fd1c..1cf355335 100644 --- a/CBRE.Editor/Settings/FileTypeRegistration.cs +++ b/CBRE.Editor/Settings/FileTypeRegistration.cs @@ -15,13 +15,7 @@ public static FileType[] GetSupportedExtensions() return new[] { new FileType(".vmf", "Valve Map File", true, true), - new FileType(".rmf", "Worldcraft RMF", true, true), - new FileType(".map", "Quake MAP Format", true, true), new FileType(".3dw", "Leadwerks 3D World Studio File", false, true), - - new FileType(".rmx", "Worldcraft RMF (Hammer Backup)", false, true), - new FileType(".max", "Quake MAP Format (Hammer Backup)", false, true), - new FileType(".vmx", "Valve Map File (Hammer Backup)", false, true) }; } diff --git a/CBRE.Providers/CBRE.Providers.csproj b/CBRE.Providers/CBRE.Providers.csproj index 0a7196ba6..46b357ec7 100644 --- a/CBRE.Providers/CBRE.Providers.csproj +++ b/CBRE.Providers/CBRE.Providers.csproj @@ -56,7 +56,6 @@ - @@ -65,7 +64,6 @@ - @@ -77,9 +75,6 @@ - - - diff --git a/CBRE.Providers/Map/MapFormatProvider.cs b/CBRE.Providers/Map/MapFormatProvider.cs deleted file mode 100644 index 7ec82dd77..000000000 --- a/CBRE.Providers/Map/MapFormatProvider.cs +++ /dev/null @@ -1,287 +0,0 @@ -using CBRE.Common; -using CBRE.DataStructures.Geometric; -using CBRE.DataStructures.MapObjects; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Globalization; -using System.IO; -using System.Linq; - -namespace CBRE.Providers.Map { - public class MapFormatProvider : MapProvider { - protected override IEnumerable GetFormatFeatures() { - return new[] - { - MapFeature.Worldspawn, - MapFeature.Solids, - MapFeature.Entities - }; - } - - protected override bool IsValidForFileName(string filename) { - return filename.EndsWith(".map", StringComparison.OrdinalIgnoreCase) - || filename.EndsWith(".max", StringComparison.OrdinalIgnoreCase); - } - - private string CleanLine(string line) { - if (line == null) return null; - var ret = line; - if (ret.Contains("//")) ret = ret.Substring(0, ret.IndexOf("//", StringComparison.Ordinal)); // Comments - return ret.Trim(); - } - - private void Assert(bool b, string message = "Malformed file.") { - if (!b) throw new Exception(message); - } - - private string FormatCoordinate(Coordinate c) { - return c.X.ToString("0.000") - + " " + c.Y.ToString("0.000") - + " " + c.Z.ToString("0.000"); - } - - private void CollectSolids(List solids, MapObject parent) { - solids.AddRange(parent.GetChildren().OfType()); - parent.GetChildren().OfType().ToList().ForEach(x => CollectSolids(solids, x)); - } - - private void CollectEntities(List entities, MapObject parent) { - entities.AddRange(parent.GetChildren().OfType()); - parent.GetChildren().OfType().ToList().ForEach(x => CollectEntities(entities, x)); - } - - private Face ReadFace(string line, IDGenerator generator) { - const NumberStyles ns = NumberStyles.Float; - - var parts = line.Split(' ').Where(x => !String.IsNullOrWhiteSpace(x)).ToList(); - - - Assert(parts[0] == "("); - Assert(parts[4] == ")"); - Assert(parts[5] == "("); - Assert(parts[9] == ")"); - Assert(parts[10] == "("); - Assert(parts[14] == ")"); - - var face = new Face(generator.GetNextFaceID()) { - Plane = new Plane(Coordinate.Parse(parts[1], parts[2], parts[3]), - Coordinate.Parse(parts[6], parts[7], parts[8]), - Coordinate.Parse(parts[11], parts[12], parts[13])), - Texture = { Name = parts[15] } - }; - - // Cater for older-style map formats - if (parts.Count == 21) { - face.AlignTextureToFace(); - face.Texture.XShift = decimal.Parse(parts[16], ns); - face.Texture.YShift = decimal.Parse(parts[17], ns); - face.Texture.Rotation = decimal.Parse(parts[18], ns); - face.Texture.XScale = decimal.Parse(parts[19], ns); - face.Texture.YScale = decimal.Parse(parts[20], ns); - } else { - Assert(parts[16] == "["); - Assert(parts[21] == "]"); - Assert(parts[22] == "["); - Assert(parts[27] == "]"); - - face.Texture.UAxis = Coordinate.Parse(parts[17], parts[18], parts[19]); - face.Texture.XShift = decimal.Parse(parts[20], ns); - face.Texture.VAxis = Coordinate.Parse(parts[23], parts[24], parts[25]); - face.Texture.YShift = decimal.Parse(parts[26], ns); - face.Texture.Rotation = decimal.Parse(parts[28], ns); - face.Texture.XScale = decimal.Parse(parts[29], ns); - face.Texture.YScale = decimal.Parse(parts[30], ns); - } - - return face; - } - - private void WriteFace(StreamWriter sw, Face face) { - // ( -128 64 64 ) ( -64 64 64 ) ( -64 0 64 ) AAATRIGGER [ 1 0 0 0 ] [ 0 -1 0 0 ] 0 1 1 - var strings = face.Vertices.Take(3).Select(x => "( " + FormatCoordinate(x.Location) + " )").ToList(); - strings.Add(String.IsNullOrWhiteSpace(face.Texture.Name) ? "AAATRIGGER" : face.Texture.Name); - strings.Add("["); - strings.Add(FormatCoordinate(face.Texture.UAxis)); - strings.Add(face.Texture.XShift.ToString("0.000")); - strings.Add("]"); - strings.Add("["); - strings.Add(FormatCoordinate(face.Texture.VAxis)); - strings.Add(face.Texture.YShift.ToString("0.000")); - strings.Add("]"); - strings.Add(face.Texture.Rotation.ToString("0.000")); - strings.Add(face.Texture.XScale.ToString("0.000")); - strings.Add(face.Texture.YScale.ToString("0.000")); - sw.WriteLine(String.Join(" ", strings)); - } - - private Solid ReadSolid(StreamReader rdr, IDGenerator generator) { - var faces = new List(); - string line; - while ((line = CleanLine(rdr.ReadLine())) != null) { - if (String.IsNullOrWhiteSpace(line)) continue; - if (line == "}") { - var ret = Solid.CreateFromIntersectingPlanes(faces.Select(x => x.Plane), generator); - ret.Colour = Colour.GetRandomBrushColour(); - foreach (var face in ret.Faces) { - var f = faces.FirstOrDefault(x => x.Plane.Normal.EquivalentTo(face.Plane.Normal)); - if (f == null) { - // TODO: Report invalid solids - Debug.WriteLine("Invalid solid!"); - return null; - } - face.Texture = f.Texture; - face.Parent = ret; - face.Colour = ret.Colour; - } - ret.UpdateBoundingBox(false); - return ret; - } - faces.Add(ReadFace(line, generator)); - } - return null; - } - - private void WriteSolid(StreamWriter sw, Solid solid) { - sw.WriteLine("{"); - solid.Faces.ForEach(x => WriteFace(sw, x)); - sw.WriteLine("}"); - } - - private static readonly string[] ExcludedKeys = new[] { "spawnflags", "classname", "origin", "wad", "mapversion" }; - - private static void ReadProperty(Entity ent, string line) { - var split = line.Split(' '); - var key = split[0].Trim('"'); - - var val = String.Join(" ", split.Skip(1)).Trim('"'); - - if (key == "classname") { - ent.EntityData.Name = val; - } else if (key == "spawnflags") { - ent.EntityData.Flags = int.Parse(val); - } else if (key == "origin") { - var osp = val.Split(' '); - ent.Origin = Coordinate.Parse(osp[0], osp[1], osp[2]); - } else if (!ExcludedKeys.Contains(key.ToLower())) { - ent.EntityData.SetPropertyValue(key, val); - } - } - - private void WriteProperty(StreamWriter sw, string key, string value) { - sw.WriteLine('"' + key + "\" \"" + value + '"'); - } - - private Entity ReadEntity(StreamReader rdr, IDGenerator generator) { - var ent = new Entity(generator.GetNextObjectID()) { EntityData = new EntityData(), Colour = Colour.GetRandomBrushColour() }; - string line; - while ((line = CleanLine(rdr.ReadLine())) != null) { - if (String.IsNullOrWhiteSpace(line)) continue; - if (line[0] == '"') ReadProperty(ent, line); - else if (line[0] == '{') { - var s = ReadSolid(rdr, generator); - if (s != null) s.SetParent(ent, false); - } else if (line[0] == '}') break; - } - ent.UpdateBoundingBox(false); - return ent; - } - - private void WriteEntity(StreamWriter sw, Entity ent) { - var solids = new List(); - CollectSolids(solids, ent); - - sw.WriteLine("{"); - WriteProperty(sw, "classname", ent.EntityData.Name); - - if (ent.EntityData.Flags > 0) { - // VHE doesn't write the spawnflags when they are zero - WriteProperty(sw, "spawnflags", ent.EntityData.Flags.ToString()); - } - foreach (var prop in ent.EntityData.Properties) { - if (prop.Key == "classname" || prop.Key == "spawnflags" || prop.Key == "origin") continue; - - // VHE doesn't write empty or zero values to the .map file - var gameDataProp = ent.GameData != null ? ent.GameData.Properties.FirstOrDefault(x => String.Equals(x.Name, prop.Key, StringComparison.OrdinalIgnoreCase)) : null; - if (gameDataProp != null) { - var emptyGd = String.IsNullOrWhiteSpace(gameDataProp.DefaultValue) || gameDataProp.DefaultValue == "0"; - var emptyProp = String.IsNullOrWhiteSpace(prop.Value) || prop.Value == "0"; - - // The value hasn't changed from the default, don't write if it's an empty value - if (emptyGd && emptyProp) continue; - } - WriteProperty(sw, prop.Key, prop.Value); - } - - if (solids.Any()) solids.ForEach(x => WriteSolid(sw, x)); // Brush entity - else WriteProperty(sw, "origin", FormatCoordinate(ent.Origin)); // Point entity - - sw.WriteLine("}"); - } - - private void WriteWorld(StreamWriter sw, World world) { - var solids = new List(); - var entities = new List(); - CollectSolids(solids, world); - CollectEntities(entities, world); - - sw.WriteLine("{"); - - WriteProperty(sw, "classname", world.EntityData.Name); - WriteProperty(sw, "spawnflags", world.EntityData.Flags.ToString()); - WriteProperty(sw, "mapversion", "220"); - foreach (var prop in world.EntityData.Properties) { - if (prop.Key == "classname" || prop.Key == "spawnflags" || prop.Key == "mapversion") continue; - WriteProperty(sw, prop.Key, prop.Value); - } - solids.ForEach(x => WriteSolid(sw, x)); - - sw.WriteLine("}"); - - entities.ForEach(x => WriteEntity(sw, x)); - } - - private List ReadAllEntities(StreamReader rdr, IDGenerator generator) { - var list = new List(); - string line; - while ((line = CleanLine(rdr.ReadLine())) != null) { - if (String.IsNullOrWhiteSpace(line)) continue; - if (line == "{") list.Add(ReadEntity(rdr, generator)); - } - return list; - } - - /// - /// Reads a map from a stream in MAP format. - /// - /// The stream to read from - /// The parsed map - protected override DataStructures.MapObjects.Map GetFromStream(Stream stream) { - using (var reader = new StreamReader(stream)) { - var map = new DataStructures.MapObjects.Map(); - var allentities = ReadAllEntities(reader, map.IDGenerator); - var worldspawn = allentities.FirstOrDefault(x => x.EntityData.Name == "worldspawn") - ?? new Entity(0) { EntityData = { Name = "worldspawn" } }; - allentities.Remove(worldspawn); - map.WorldSpawn.EntityData = worldspawn.EntityData; - allentities.ForEach(x => x.SetParent(map.WorldSpawn, false)); - foreach (var obj in worldspawn.GetChildren().ToArray()) { - obj.SetParent(map.WorldSpawn, false); - } - map.WorldSpawn.UpdateBoundingBox(false); - return map; - } - } - - /// - /// Writes a map to a stream in MAP format. - /// - /// The stream to write to - /// The map to save - protected override void SaveToStream(Stream stream, DataStructures.MapObjects.Map map) { - using (var writer = new StreamWriter(stream)) { - WriteWorld(writer, map.WorldSpawn); - } - } - } -} diff --git a/CBRE.Providers/Map/RmfProvider.cs b/CBRE.Providers/Map/RmfProvider.cs deleted file mode 100644 index 4a94d3c28..000000000 --- a/CBRE.Providers/Map/RmfProvider.cs +++ /dev/null @@ -1,401 +0,0 @@ -using CBRE.DataStructures.MapObjects; -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Text; -using Path = CBRE.DataStructures.MapObjects.Path; - -namespace CBRE.Providers.Map { - public class RmfProvider : MapProvider { - protected override IEnumerable GetFormatFeatures() { - return new[] - { - MapFeature.Worldspawn, - MapFeature.Solids, - MapFeature.Entities, - MapFeature.Groups, - - MapFeature.Colours, - MapFeature.SingleVisgroups, - MapFeature.Cameras - }; - } - - protected override bool IsValidForFileName(string filename) { - return filename.EndsWith(".rmf", StringComparison.OrdinalIgnoreCase) - || filename.EndsWith(".rmx", StringComparison.OrdinalIgnoreCase); - } - - protected override DataStructures.MapObjects.Map GetFromStream(Stream stream) { - var map = new DataStructures.MapObjects.Map(); - var br = new BinaryReader(stream); - - // Only RMF version 2.2 is supported for the moment. - var version = Math.Round(br.ReadSingle(), 1); - if (Math.Abs(version - 2.2) > 0.01) { - throw new ProviderException("Incorrect RMF version number. Expected 2.2, got " + version + "."); - } - map.Version = (decimal)version; - - // RMF header test - var header = br.ReadFixedLengthString(Encoding.UTF8, 3); - if (header != "RMF") { - throw new ProviderException("Incorrect RMF header. Expected 'RMF', got '" + header + "'."); - } - - // Visgroups - var visgroups = ReadVisgroups(br); - map.Visgroups.AddRange(visgroups); - - // Map Objects - map.IDGenerator.Reset(); // So the world will be ID = 1 - var worldspawn = ReadWorldSpawn(br, map.Visgroups, map.IDGenerator); - map.WorldSpawn = worldspawn; - - // DOCINFO string check - var docinfo = br.ReadFixedLengthString(Encoding.UTF8, 8); - if (docinfo != "DOCINFO") { - throw new ProviderException("Incorrect RMF format. Expected 'DOCINFO', got '" + docinfo + "'."); - } - - // Cameras - br.ReadSingle(); // Appears to be a version number for camera data. Unused. - var activeCamera = br.ReadInt32(); - var cameras = ReadCameras(br); - map.Cameras.AddRange(cameras); - if (activeCamera >= 0 && activeCamera < map.Cameras.Count) { - map.ActiveCamera = map.Cameras[activeCamera]; - } - - return map; - } - - protected override void SaveToStream(Stream stream, DataStructures.MapObjects.Map map) { - var bw = new BinaryWriter(stream, Encoding.UTF8); - bw.Write(2.2f); // RMF version number - bw.WriteFixedLengthString(Encoding.UTF8, 3, "RMF"); - WriteVisgroups(bw, map.Visgroups); - WriteMapObject(bw, map.WorldSpawn); - bw.WriteFixedLengthString(Encoding.UTF8, 8, "DOCINFO"); - bw.Write(0.2f); // Unused - bw.Write(map.Cameras.IndexOf(map.ActiveCamera)); - WriteCameras(bw, map.Cameras); - } - - private static Property ReadProperty(BinaryReader br) { - return new Property { - Key = br.ReadCString(), - Value = br.ReadCString() - }; - } - - public static void WriteProperty(BinaryWriter bw, Property p) { - bw.WriteCString(p.Key); - bw.WriteCString(p.Value); - } - - private static IEnumerable ReadCameras(BinaryReader br) { - var num = br.ReadInt32(); - for (var i = 0; i < num; i++) { - yield return new Camera { - EyePosition = br.ReadCoordinate(), - LookPosition = br.ReadCoordinate() - }; - } - } - - public static void WriteCameras(BinaryWriter bw, List cameras) { - bw.Write(cameras.Count); - foreach (var camera in cameras) { - bw.WriteCoordinate(camera.EyePosition); - bw.WriteCoordinate(camera.LookPosition); - } - } - - private static readonly string[] ExcludedKeys = new[] { "spawnflags", "classname", "origin", "wad", "mapversion" }; - - private static EntityData ReadEntityData(BinaryReader br) { - var data = new EntityData { - Name = br.ReadCString() - }; - - br.ReadBytes(4); // Unused bytes - - data.Flags = br.ReadInt32(); - - var numProperties = br.ReadInt32(); - for (var i = 0; i < numProperties; i++) { - var prop = ReadProperty(br); - if (ExcludedKeys.Contains(prop.Key.ToLower())) continue; - data.SetPropertyValue(prop.Key, prop.Value); - } - - br.ReadBytes(12); // More unused bytes - - return data; - } - - private static void WriteEntityData(BinaryWriter bw, EntityData data) { - bw.WriteCString(data.Name); - bw.Write(new byte[4]); // Unused - bw.Write(data.Flags); - bw.Write(data.Properties.Count); - foreach (var property in data.Properties) { - WriteProperty(bw, property); - } - bw.Write(new byte[12]); // Unused - } - - private static World ReadWorldSpawn(BinaryReader br, List visgroups, IDGenerator generator) { - return (World)ReadMapObject(br, visgroups, generator); - } - - private static MapObject ReadMapObject(BinaryReader br, List visgroups, IDGenerator generator) { - var type = br.ReadCString(); - switch (type) { - case "CMapWorld": - return ReadMapWorld(br, visgroups, generator); - case "CMapGroup": - return ReadMapGroup(br, visgroups, generator); - case "CMapSolid": - return ReadMapSolid(br, visgroups, generator); - case "CMapEntity": - return ReadMapEntity(br, visgroups, generator); - default: - throw new ProviderException("Unknown RMF map object: " + type); - } - } - - private static void WriteMapObject(BinaryWriter bw, MapObject mo) { - if (mo is World) WriteMapWorld(bw, (World)mo); - else if (mo is Group) WriteMapGroup(bw, (Group)mo); - else if (mo is Solid) WriteMapSolid(bw, (Solid)mo); - else if (mo is Entity) WriteMapEntity(bw, (Entity)mo); - } - - private static void ReadMapBase(BinaryReader br, MapObject obj, List visgroups, IDGenerator generator) { - var visgroupId = br.ReadInt32(); - if (visgroupId > 0 && visgroups.Any(x => x.ID == visgroupId)) { - obj.Visgroups.Add(visgroupId); - obj.IsVisgroupHidden = !visgroups.First(x => x.ID == visgroupId).Visible; - } - - obj.Colour = br.ReadRGBColour(); - - var numChildren = br.ReadInt32(); - for (var i = 0; i < numChildren; i++) { - var child = ReadMapObject(br, visgroups, generator); - child.SetParent(obj); - } - } - - private static void WriteMapBase(BinaryWriter bw, MapObject obj) { - bw.Write(obj.Visgroups.Except(obj.AutoVisgroups).FirstOrDefault()); - bw.WriteRGBColour(obj.Colour); - bw.Write(obj.ChildCount); - foreach (var mo in obj.GetChildren()) { - WriteMapObject(bw, mo); - } - } - - private static Entity ReadMapEntity(BinaryReader br, List visgroups, IDGenerator generator) { - var ent = new Entity(generator.GetNextObjectID()); - ReadMapBase(br, ent, visgroups, generator); - ent.EntityData = ReadEntityData(br); - br.ReadBytes(2); // Unused - ent.Origin = br.ReadCoordinate(); - br.ReadBytes(4); // Unused - ent.UpdateBoundingBox(false); - return ent; - } - - private static void WriteMapEntity(BinaryWriter bw, Entity ent) { - bw.WriteCString("CMapEntity"); - WriteMapBase(bw, ent); - WriteEntityData(bw, ent.EntityData); - bw.Write(new byte[2]); // Unused - bw.WriteCoordinate(ent.Origin); - bw.Write(new byte[4]); // Unused - } - - private static Face ReadFace(BinaryReader br, IDGenerator generator) { - var face = new Face(generator.GetNextFaceID()); - var textureName = br.ReadFixedLengthString(Encoding.UTF8, 256); - br.ReadBytes(4); // Unused - face.Texture.Name = textureName; - face.Texture.UAxis = br.ReadCoordinate(); - face.Texture.XShift = br.ReadSingleAsDecimal(); - face.Texture.VAxis = br.ReadCoordinate(); - face.Texture.YShift = br.ReadSingleAsDecimal(); - face.Texture.Rotation = br.ReadSingleAsDecimal(); - face.Texture.XScale = br.ReadSingleAsDecimal(); - face.Texture.YScale = br.ReadSingleAsDecimal(); - br.ReadBytes(16); // Unused - var numVerts = br.ReadInt32(); - for (var i = 0; i < numVerts; i++) { - face.Vertices.Add(new Vertex(br.ReadCoordinate(), face)); - } - face.Plane = br.ReadPlane(); - face.UpdateBoundingBox(); - return face; - } - - private static void WriteFace(BinaryWriter bw, Face face) { - bw.WriteFixedLengthString(Encoding.UTF8, 256, face.Texture.Name); - bw.Write(new byte[4]); - bw.WriteCoordinate(face.Texture.UAxis); - bw.WriteDecimalAsSingle(face.Texture.XShift); - bw.WriteCoordinate(face.Texture.VAxis); - bw.WriteDecimalAsSingle(face.Texture.YShift); - bw.WriteDecimalAsSingle(face.Texture.Rotation); - bw.WriteDecimalAsSingle(face.Texture.XScale); - bw.WriteDecimalAsSingle(face.Texture.YScale); - bw.Write(new byte[16]); - bw.Write(face.Vertices.Count); - foreach (var vertex in face.Vertices) { - bw.WriteCoordinate(vertex.Location); - } - bw.WritePlane(face.Vertices.Select(v => v.Location).ToArray()); - } - - private static Solid ReadMapSolid(BinaryReader br, List visgroups, IDGenerator generator) { - var sol = new Solid(generator.GetNextObjectID()); - ReadMapBase(br, sol, visgroups, generator); - var numFaces = br.ReadInt32(); - for (var i = 0; i < numFaces; i++) { - var face = ReadFace(br, generator); - face.Parent = sol; - face.Colour = sol.Colour; - sol.Faces.Add(face); - } - sol.UpdateBoundingBox(false); - return sol; - } - - private static void WriteMapSolid(BinaryWriter bw, Solid s) { - bw.WriteCString("CMapSolid"); - WriteMapBase(bw, s); - bw.Write(s.Faces.Count); - foreach (var face in s.Faces) { - WriteFace(bw, face); - } - } - - private static Group ReadMapGroup(BinaryReader br, List visgroups, IDGenerator generator) { - var grp = new Group(generator.GetNextObjectID()); - ReadMapBase(br, grp, visgroups, generator); - grp.UpdateBoundingBox(false); - return grp; - } - - private static void WriteMapGroup(BinaryWriter bw, Group g) { - bw.WriteCString("CMapGroup"); - WriteMapBase(bw, g); - } - - private static PathNode ReadPathNode(BinaryReader br) { - var node = new PathNode { - Position = br.ReadCoordinate(), - ID = br.ReadInt32(), - Name = br.ReadFixedLengthString(Encoding.UTF8, 128) - }; - var numProps = br.ReadInt32(); - for (var i = 0; i < numProps; i++) { - node.Properties.Add(ReadProperty(br)); - } - return node; - } - - private static void WritePathNode(BinaryWriter bw, PathNode node) { - bw.WriteCoordinate(node.Position); - bw.Write(node.ID); - bw.WriteFixedLengthString(Encoding.UTF8, 128, node.Name); - bw.Write(node.Properties.Count); - foreach (var property in node.Properties) { - WriteProperty(bw, property); - } - } - - private static Path ReadPath(BinaryReader br) { - var path = new Path { - Name = br.ReadFixedLengthString(Encoding.UTF8, 128), - Type = br.ReadFixedLengthString(Encoding.UTF8, 128), - Direction = (PathDirection)br.ReadInt32() - }; - var numNodes = br.ReadInt32(); - for (var i = 0; i < numNodes; i++) { - var node = ReadPathNode(br); - node.Parent = path; - path.Nodes.Add(node); - } - return path; - } - - private static void WritePath(BinaryWriter bw, Path path) { - bw.WriteFixedLengthString(Encoding.UTF8, 128, path.Name); - bw.WriteFixedLengthString(Encoding.UTF8, 128, path.Type); - bw.Write((int)path.Direction); - bw.Write(path.Nodes.Count); - foreach (var node in path.Nodes) { - WritePathNode(bw, node); - } - } - - private static MapObject ReadMapWorld(BinaryReader br, List visgroups, IDGenerator generator) { - var wld = new World(generator.GetNextObjectID()); - ReadMapBase(br, wld, visgroups, generator); - wld.EntityData = ReadEntityData(br); - var numPaths = br.ReadInt32(); - for (var i = 0; i < numPaths; i++) { - wld.Paths.Add(ReadPath(br)); - } - return wld; - } - - private static void WriteMapWorld(BinaryWriter bw, World w) { - bw.WriteCString("CMapWorld"); - WriteMapBase(bw, w); - WriteEntityData(bw, w.EntityData); - bw.Write(w.Paths.Count); - foreach (var path in w.Paths) { - WritePath(bw, path); - } - } - - private static IEnumerable ReadVisgroups(BinaryReader br) { - var list = new List(); - var numVisgroups = br.ReadInt32(); - for (var i = 0; i < numVisgroups; i++) { - var vis = new Visgroup { - Name = br.ReadFixedLengthString(Encoding.UTF8, 128), - Colour = br.ReadRGBAColour(), - ID = br.ReadInt32(), - Visible = br.ReadBoolean() - }; - vis.Colour = Color.FromArgb(255, vis.Colour); - br.ReadBytes(3); - list.Add(vis); - } - // Get rid of zero groups - foreach (var vg in list.Where(x => x.ID == 0)) { - vg.ID = list.Max(x => x.ID) + 1; - } - return list; - } - - private static void WriteVisgroups(BinaryWriter bw, IEnumerable visgroups) { - var vis = visgroups.Where(x => !x.IsAutomatic).ToList(); - bw.Write(vis.Count); - foreach (var visgroup in vis) { - bw.WriteFixedLengthString(Encoding.UTF8, 128, visgroup.Name); - bw.WriteRGBAColour(visgroup.Colour); - bw.Write(visgroup.ID); - bw.Write(visgroup.Visible); - bw.Write(new byte[3]); // Unused - } - } - } -} diff --git a/CBRE.Providers/Texture/Vtf/VtfImageFlag.cs b/CBRE.Providers/Texture/Vtf/VtfImageFlag.cs deleted file mode 100644 index a08e69fb7..000000000 --- a/CBRE.Providers/Texture/Vtf/VtfImageFlag.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; - -namespace CBRE.Providers.Texture.Vtf { - [Flags] - public enum VtfImageFlag : uint { - Pointsample = 0x00000001, - Trilinear = 0x00000002, - Clamps = 0x00000004, - Clampt = 0x00000008, - Anisotropic = 0x00000010, - HintDxt5 = 0x00000020, - Srgb = 0x00000040, - DeprecatedNocompress = 0x00000040, - Normal = 0x00000080, - Nomip = 0x00000100, - Nolod = 0x00000200, - Minmip = 0x00000400, - Procedural = 0x00000800, - Onebitalpha = 0x00001000, - Eightbitalpha = 0x00002000, - Envmap = 0x00004000, - Rendertarget = 0x00008000, - Depthrendertarget = 0x00010000, - Nodebugoverride = 0x00020000, - Singlecopy = 0x00040000, - Unused0 = 0x00080000, - DeprecatedOneovermiplevelinalpha = 0x00080000, - Unused1 = 0x00100000, - DeprecatedPremultcolorbyoneovermiplevel = 0x00100000, - Unused2 = 0x00200000, - DeprecatedNormaltodudv = 0x00200000, - Unused3 = 0x00400000, - DeprecatedAlphatestmipgeneration = 0x00400000, - Nodepthbuffer = 0x00800000, - Unused4 = 0x01000000, - DeprecatedNicefiltered = 0x01000000, - Clampu = 0x02000000, - Vertextexture = 0x04000000, - Ssbump = 0x08000000, - Unused5 = 0x10000000, - DeprecatedUnfilterableOk = 0x10000000, - Border = 0x20000000, - DeprecatedSpecvarRed = 0x40000000, - DeprecatedSpecvarAlpha = 0x80000000, - Last = 0x20000000 - } -} \ No newline at end of file diff --git a/CBRE.Providers/Texture/Vtf/VtfImageFormat.cs b/CBRE.Providers/Texture/Vtf/VtfImageFormat.cs deleted file mode 100644 index 6171df3fc..000000000 --- a/CBRE.Providers/Texture/Vtf/VtfImageFormat.cs +++ /dev/null @@ -1,45 +0,0 @@ -namespace CBRE.Providers.Texture.Vtf { - public enum VtfImageFormat : int { - None = -1, - Rgba8888 = 0, - Abgr8888, - Rgb888, - Bgr888, - Rgb565, - I8, - Ia88, - P8, - A8, - Rgb888Bluescreen, - Bgr888Bluescreen, - Argb8888, - Bgra8888, - Dxt1, - Dxt3, - Dxt5, - Bgrx8888, - Bgr565, - Bgrx5551, - Bgra4444, - Dxt1Onebitalpha, - Bgra5551, - Uv88, - Uvwq8888, - Rgba16161616F, - Rgba16161616, - Uvlx8888, - R32F, - Rgb323232F, - Rgba32323232F, - NvDst16, - NvDst24, - NvIntz, - NvRawz, - AtiDst16, - AtiDst24, - NvNull, - Ati2N, - Ati1N, - } - -} diff --git a/CBRE.Providers/Texture/Vtf/VtfProvider.cs b/CBRE.Providers/Texture/Vtf/VtfProvider.cs deleted file mode 100644 index 6beb3274d..000000000 --- a/CBRE.Providers/Texture/Vtf/VtfProvider.cs +++ /dev/null @@ -1,574 +0,0 @@ -using CBRE.FileSystem; -using OpenTK; -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Imaging; -using System.IO; -using System.Runtime.InteropServices; -using System.Text; - -namespace CBRE.Providers.Texture.Vtf { - // Uses a lot of code from the excellent VtfLib - // Re-created natively for flexibility and portability - public static class VtfProvider { - private const string VtfHeader = "VTF"; - - public static Size GetSize(IFile file) { - return GetSize(file.Open()); - } - - public static Size GetSize(Stream stream) { - using (var br = new BinaryReader(stream)) { - var header = br.ReadFixedLengthString(Encoding.ASCII, 4); - if (header != VtfHeader) throw new ProviderException("Invalid VTF header. Expected '" + VtfHeader + "', got '" + header + "'."); - - var v1 = br.ReadUInt32(); - var v2 = br.ReadUInt32(); - var version = v1 + (v2 / 10); // e.g. 7.3 - - var headerSize = br.ReadUInt32(); - var width = br.ReadUInt16(); - var height = br.ReadUInt16(); - - return new Size(width, height); - } - } - - public static BitmapRef GetImage(IFile file, int maxWidth = 0, int maxHeight = 0) { - return GetImage(file.Open(), maxWidth, maxHeight); - } - - public static BitmapRef GetImage(Stream stream, int maxWidth = 0, int maxHeight = 0) { - using (var br = new BinaryReader(stream)) { - var header = br.ReadFixedLengthString(Encoding.ASCII, 4); - if (header != VtfHeader) throw new ProviderException("Invalid VTF header. Expected '" + VtfHeader + "', got '" + header + "'."); - - var v1 = br.ReadUInt32(); - var v2 = br.ReadUInt32(); - var version = v1 + (v2 / 10m); // e.g. 7.3 - - var headerSize = br.ReadUInt32(); - var width = br.ReadUInt16(); - var height = br.ReadUInt16(); - - var flags = (VtfImageFlag)br.ReadUInt32(); - - var numFrames = br.ReadUInt16(); - var firstFrame = br.ReadUInt16(); - - br.ReadBytes(4); // padding - - var reflectivity = br.ReadCoordinate(); - - br.ReadBytes(4); // padding - - var bumpmapScale = br.ReadSingle(); - - var highResImageFormat = (VtfImageFormat)br.ReadUInt32(); - var mipmapCount = br.ReadByte(); - var lowResImageFormat = (VtfImageFormat)br.ReadUInt32(); - var lowResWidth = br.ReadByte(); - var lowResHeight = br.ReadByte(); - - ushort depth = 1; - uint numResources = 0; - - if (version >= 7.2m) { - depth = br.ReadUInt16(); - } - if (version >= 7.3m) { - br.ReadBytes(3); - numResources = br.ReadUInt32(); - } - - var faces = 1; - if (flags.HasFlag(VtfImageFlag.Envmap)) { - faces = version < 7.5m && firstFrame != 0xFFFF ? 7 : 6; - } - - var imageSize = ComputeImageSize(width, height, depth, mipmapCount, highResImageFormat) * faces * numFrames; - var thumbnailSize = lowResImageFormat == VtfImageFormat.None - ? 0 - : ComputeImageSize(lowResWidth, lowResHeight, 1, lowResImageFormat); - - var thumbnailPos = headerSize; - var dataPos = headerSize + thumbnailSize; - - for (var i = 0; i < numResources; i++) { - var type = br.ReadUInt32(); - var data = br.ReadUInt32(); - switch (type) { - case 0x01: - // Low res image - thumbnailPos = data; - break; - case 0x30: - // Regular image - dataPos = data; - break; - } - } - - if (lowResImageFormat != VtfImageFormat.None) { - br.BaseStream.Position = thumbnailPos; - var thumbnail = br.ReadBytes((int)thumbnailSize); - //var thumbImage = LoadImage(br, lowResWidth, lowResHeight, lowResImageFormat); - //return thumbImage; - } - - var mipNum = 0; - if (maxWidth > 0 || maxHeight > 0) mipNum = GetMipToLoad(width, height, maxWidth > 0 ? maxWidth : width, maxHeight > 0 ? maxHeight : height, mipmapCount); - - var wid = MipmapResize(width, mipNum); - var hei = MipmapResize(height, mipNum); - var offset = GetImageDataLocation(0, 0, 0, mipNum, highResImageFormat, width, height, numFrames, faces, depth, mipmapCount); - br.BaseStream.Position = offset + dataPos; - - var img = LoadImage(br, (uint)wid, (uint)hei, highResImageFormat); - //img.Save(String.Format(@"D:\Github\sledge\_Resources\VTF\_test_fr{0}_fa{1}_sl{2}_m{3}.png", frame, face, slice, mip)); - return new BitmapRef(img); //TODO: mark as disposable? - } - } - - private static int GetMipToLoad(uint width, uint height, int maxWidth, int maxHeight, int mipCount) { - var mip = 0; - while (mip < mipCount - 1 && (width > maxWidth || height > maxHeight)) { - mip++; - width >>= 1; - height >>= 1; - } - return mip; - } - - private static long GetImageDataLocation(int frame, int face, int slice, int mip, VtfImageFormat format, int width, int height, int numFrames, int numFaces, int depth, int numMips) { - long offset = 0; - - // Transverse past all frames and faces of each mipmap (up to the requested one). - for (var i = numMips - 1; i > mip; i--) { - offset += ComputeMipmapSize(width, height, depth, i, format) * numFrames * numFaces; - } - - var uiTemp1 = ComputeMipmapSize(width, height, depth, mip, format); - var uiTemp2 = ComputeMipmapSize(width, height, 1, mip, format); - - // Transverse past requested frames and faces of requested mipmap. - offset += uiTemp1 * frame * numFaces * depth; - offset += uiTemp1 * face * depth; - offset += uiTemp2 * slice; - - return offset; - } - - private static int MipmapResize(int input, int level) { - var res = input >> level; - if (res < 1) res = 1; - return res; - } - - private static void TransformBytes(byte[] buffer, BinaryReader br, uint width, uint height, int bpp, int a, int r, int g, int b, bool bluescreen) { - var bytes = br.ReadBytes((int)(width * height * bpp)); - for (int i = 0, j = 0; i < bytes.Length; i += bpp, j += 4) { - buffer[j + 0] = (b >= 0) ? bytes[i + b] : (byte)0; // b - buffer[j + 1] = (g >= 0) ? bytes[i + g] : (byte)0; // g - buffer[j + 2] = (r >= 0) ? bytes[i + r] : (byte)0; // r - buffer[j + 3] = (a >= 0) ? bytes[i + a] : (byte)255; // a - if (bluescreen && buffer[j + 0] == 255 && buffer[j + 1] == 0 && buffer[j + 2] == 0) buffer[j + 3] = 0; - } - } - - private static void TransformShorts(byte[] buffer, BinaryReader br, uint width, uint height, int bpp, int a, int r, int g, int b) { - a *= 2; - r *= 2; - g *= 2; - b *= 2; - var bytes = br.ReadBytes((int)(width * height * bpp)); - for (int i = 0, j = 0; i < bytes.Length; i += bpp, j += 4) { - buffer[j + 0] = (b >= 0) ? (byte)((bytes[i + b] | bytes[i + b + 1] << 8) / 256) : (byte)0; // b - buffer[j + 1] = (g >= 0) ? (byte)((bytes[i + g] | bytes[i + g + 1] << 8) / 256) : (byte)0; // g - buffer[j + 2] = (r >= 0) ? (byte)((bytes[i + r] | bytes[i + r + 1] << 8) / 256) : (byte)0; // r - buffer[j + 3] = (a >= 0) ? (byte)((bytes[i + a] | bytes[i + a + 1] << 8) / 256) : (byte)255; // a - } - } - - private static void TransformRgba16161616F(byte[] buffer, BinaryReader br, uint width, uint height) { - // I have no idea how this works. It's just converted straight from VTFLib. - // I think the half format is slightly different to what it should be, which causes the result to be different to VTFLib. - // Fortunately CBRE does not need to care about cubemaps, which is what this format seems to be used for... - const int a = 6; - const int r = 0; - const int g = 2; - const int b = 4; - var bytes = br.ReadBytes((int)(width * height * 8)); - - var log = 0d; - for (int i = 0, j = 0; i < bytes.Length; i += 8, j += 4) { - var hb = Half.FromBytes(bytes, i + b).ToSingle(); - var hg = Half.FromBytes(bytes, i + g).ToSingle(); - var hr = Half.FromBytes(bytes, i + r).ToSingle(); - var lum = hr * 0.299f + hg * 0.587f + hb * 0.114f; - log += Math.Log(0.0000000001d + lum); - } - log = Math.Exp(log / (width * height)); - - for (int i = 0, j = 0; i < bytes.Length; i += 8, j += 4) { - var hb = Half.FromBytes(bytes, i + b).ToSingle(); - var hg = Half.FromBytes(bytes, i + g).ToSingle(); - var hr = Half.FromBytes(bytes, i + r).ToSingle(); - var ha = Half.FromBytes(bytes, i + a).ToSingle(); - - var y = hr * 0.299f + hg * 0.587f + hb * 0.114f; - var u = (hb - y) * 0.565f; - var v = (hr - y) * 0.713f; - - var mul = 4 * y / log; - mul = mul / (1 + mul); - mul /= y; - - hr = (float)Math.Pow((y + 1.403f * v) * mul, 2.25f); - hg = (float)Math.Pow((y - 0.344f * u - 0.714f * v) * mul, 2.25f); - hb = (float)Math.Pow((y + 1.770f * u) * mul, 2.25f); - - if (hr < 0) hr = 0; - if (hr > 1) hr = 1; - if (hg < 0) hg = 0; - if (hg > 1) hg = 1; - if (hb < 0) hb = 0; - if (hb > 1) hb = 1; - - buffer[j + 0] = (byte)(hb * 255); // b - buffer[j + 1] = (byte)(hg * 255); // g - buffer[j + 2] = (byte)(hr * 255); // r - buffer[j + 3] = (byte)(ha * 255); // a - } - } - - private static Bitmap LoadImage(BinaryReader br, uint width, uint height, VtfImageFormat format) { - var buffer = new byte[width * height * 4]; - switch (format) { - case VtfImageFormat.Rgba8888: - TransformBytes(buffer, br, width, height, 4, 3, 0, 1, 2, false); - break; - case VtfImageFormat.Abgr8888: - TransformBytes(buffer, br, width, height, 4, 0, 3, 2, 1, false); - break; - case VtfImageFormat.Rgb888: - TransformBytes(buffer, br, width, height, 3, -1, 0, 1, 2, false); - break; - case VtfImageFormat.Bgr888: - TransformBytes(buffer, br, width, height, 3, -1, 2, 1, 0, false); - break; - case VtfImageFormat.Rgb888Bluescreen: - TransformBytes(buffer, br, width, height, 3, -1, 0, 1, 2, true); - break; - case VtfImageFormat.Bgr888Bluescreen: - TransformBytes(buffer, br, width, height, 3, -1, 2, 1, 0, true); - break; - case VtfImageFormat.Argb8888: - TransformBytes(buffer, br, width, height, 4, 0, 1, 2, 3, false); - break; - case VtfImageFormat.Bgra8888: - br.Read(buffer, 0, buffer.Length); - break; - case VtfImageFormat.Dxt1: - case VtfImageFormat.Dxt1Onebitalpha: - DecompressDxt1(buffer, br, width, height); - break; - case VtfImageFormat.Dxt5: - DecompressDxt5(buffer, br, width, height); - break; - case VtfImageFormat.Bgrx8888: - TransformBytes(buffer, br, width, height, 4, -1, 2, 1, 0, false); - break; - case VtfImageFormat.Uv88: - TransformBytes(buffer, br, width, height, 2, -1, 0, 1, -1, false); - break; - case VtfImageFormat.Rgba16161616F: - TransformRgba16161616F(buffer, br, width, height); - break; - case VtfImageFormat.Rgba16161616: - TransformShorts(buffer, br, width, height, 8, 3, 0, 1, 2); - break; - default: - throw new NotImplementedException(); - } - var bmp = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb); - var bits = bmp.LockBits(new Rectangle(0, 0, (int)width, (int)height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); - Marshal.Copy(buffer, 0, bits.Scan0, buffer.Length); - bmp.UnlockBits(bits); - return bmp; - } - - private static void DecompressDxt1(byte[] buffer, BinaryReader br, uint width, uint height) { - var num = ((width + 3) / 4) * ((height + 3) / 4) * 8; - var all = br.ReadBytes((int)num); - var pos = 0; - var c = new byte[16]; - for (var y = 0; y < height; y += 4) { - for (var x = 0; x < width; x += 4) { - int c0 = all[pos++]; - c0 |= all[pos++] << 8; - - int c1 = all[pos++]; - c1 |= all[pos++] << 8; - - c[0] = (byte)((c0 & 0xF800) >> 8); - c[1] = (byte)((c0 & 0x07E0) >> 3); - c[2] = (byte)((c0 & 0x001F) << 3); - c[3] = 255; - - c[4] = (byte)((c1 & 0xF800) >> 8); - c[5] = (byte)((c1 & 0x07E0) >> 3); - c[6] = (byte)((c1 & 0x001F) << 3); - c[7] = 255; - - if (c0 > c1) { - // No alpha channel - - c[8] = (byte)((2 * c[0] + c[4]) / 3); - c[9] = (byte)((2 * c[1] + c[5]) / 3); - c[10] = (byte)((2 * c[2] + c[6]) / 3); - c[11] = 255; - - c[12] = (byte)((c[0] + 2 * c[4]) / 3); - c[13] = (byte)((c[1] + 2 * c[5]) / 3); - c[14] = (byte)((c[2] + 2 * c[6]) / 3); - c[15] = 255; - } else { - // 1-bit alpha channel - - c[8] = (byte)((c[0] + c[4]) / 2); - c[9] = (byte)((c[1] + c[5]) / 2); - c[10] = (byte)((c[2] + c[6]) / 2); - c[11] = 255; - c[12] = 0; - c[13] = 0; - c[14] = 0; - c[15] = 0; - } - - int bytes = all[pos++]; - bytes |= all[pos++] << 8; - bytes |= all[pos++] << 16; - bytes |= all[pos++] << 24; - - for (var yy = 0; yy < 4; yy++) { - for (var xx = 0; xx < 4; xx++) { - var xpos = x + xx; - var ypos = y + yy; - if (xpos < width && ypos < height) { - var index = bytes & 0x0003; - index *= 4; - var pointer = ypos * width * 4 + xpos * 4; - buffer[pointer + 0] = c[index + 2]; // b - buffer[pointer + 1] = c[index + 1]; // g - buffer[pointer + 2] = c[index + 0]; // r - buffer[pointer + 3] = c[index + 3]; // a - } - bytes >>= 2; - } - } - } - } - } - - private static void DecompressDxt5(byte[] buffer, BinaryReader br, uint width, uint height) { - var num = ((width + 3) / 4) * ((height + 3) / 4) * 16; - var all = br.ReadBytes((int)num); - var pos = 0; - var c = new byte[16]; - var a = new int[8]; - for (var y = 0; y < height; y += 4) { - for (var x = 0; x < width; x += 4) { - var a0 = all[pos++]; - var a1 = all[pos++]; - - a[0] = a0; - a[1] = a1; - - if (a0 > a1) { - a[2] = (6 * a[0] + 1 * a[1] + 3) / 7; - a[3] = (5 * a[0] + 2 * a[1] + 3) / 7; - a[4] = (4 * a[0] + 3 * a[1] + 3) / 7; - a[5] = (3 * a[0] + 4 * a[1] + 3) / 7; - a[6] = (2 * a[0] + 5 * a[1] + 3) / 7; - a[7] = (1 * a[0] + 6 * a[1] + 3) / 7; - } else { - a[2] = (4 * a[0] + 1 * a[1] + 2) / 5; - a[3] = (3 * a[0] + 2 * a[1] + 2) / 5; - a[4] = (2 * a[0] + 3 * a[1] + 2) / 5; - a[5] = (1 * a[0] + 4 * a[1] + 2) / 5; - a[6] = 0x00; - a[7] = 0xFF; - } - - var aindex = 0L; - for (var i = 0; i < 6; i++) aindex |= ((long)all[pos++]) << (8 * i); - - int c0 = all[pos++]; - c0 |= all[pos++] << 8; - - int c1 = all[pos++]; - c1 |= all[pos++] << 8; - - c[0] = (byte)((c0 & 0xF800) >> 8); - c[1] = (byte)((c0 & 0x07E0) >> 3); - c[2] = (byte)((c0 & 0x001F) << 3); - c[3] = 255; - - c[4] = (byte)((c1 & 0xF800) >> 8); - c[5] = (byte)((c1 & 0x07E0) >> 3); - c[6] = (byte)((c1 & 0x001F) << 3); - c[7] = 255; - - c[8] = (byte)((2 * c[0] + c[4]) / 3); - c[9] = (byte)((2 * c[1] + c[5]) / 3); - c[10] = (byte)((2 * c[2] + c[6]) / 3); - c[11] = 255; - - c[12] = (byte)((c[0] + 2 * c[4]) / 3); - c[13] = (byte)((c[1] + 2 * c[5]) / 3); - c[14] = (byte)((c[2] + 2 * c[6]) / 3); - c[15] = 255; - - int bytes = all[pos++]; - bytes |= all[pos++] << 8; - bytes |= all[pos++] << 16; - bytes |= all[pos++] << 24; - - for (var yy = 0; yy < 4; yy++) { - for (var xx = 0; xx < 4; xx++) { - var xpos = x + xx; - var ypos = y + yy; - if (xpos < width && ypos < height) { - var index = bytes & 0x0003; - index *= 4; - var alpha = (byte)a[aindex & 0x07]; - var pointer = ypos * width * 4 + xpos * 4; - buffer[pointer + 0] = c[index + 2]; // b - buffer[pointer + 1] = c[index + 1]; // g - buffer[pointer + 2] = c[index + 0]; // r - buffer[pointer + 3] = alpha; // a - } - bytes >>= 2; - aindex >>= 3; - } - } - } - } - } - - private static VtfImageFormatInfo GetImageFormatInfo(VtfImageFormat imageFormat) { - return ImageFormats[imageFormat]; - } - - private static uint ComputeMipmapSize(int width, int height, int depth, int mipLevel, VtfImageFormat format) { - var w = MipmapResize(width, mipLevel); - var h = MipmapResize(height, mipLevel); - var d = MipmapResize(depth, mipLevel); - return ComputeImageSize((uint)w, (uint)h, (uint)d, format); - } - - private static uint ComputeImageSize(uint width, uint height, uint depth, VtfImageFormat imageFormat) { - switch (imageFormat) { - case VtfImageFormat.Dxt1: - case VtfImageFormat.Dxt1Onebitalpha: - if (width < 4 && width > 0) width = 4; - if (height < 4 && height > 0) height = 4; - return ((width + 3) / 4) * ((height + 3) / 4) * 8 * depth; - case VtfImageFormat.Dxt3: - case VtfImageFormat.Dxt5: - if (width < 4 && width > 0) width = 4; - if (height < 4 && height > 0) height = 4; - return ((width + 3) / 4) * ((height + 3) / 4) * 16 * depth; - default: - return width * height * depth * GetImageFormatInfo(imageFormat).BytesPerPixel; - } - } - - private static uint ComputeImageSize(uint width, uint height, uint depth, uint uiMipmaps, VtfImageFormat imageFormat) { - uint uiImageSize = 0; - - for (var i = 0; i < uiMipmaps; i++) { - uiImageSize += ComputeImageSize(width, height, depth, imageFormat); - - width >>= 1; - height >>= 1; - depth >>= 1; - - if (width < 1) width = 1; - if (height < 1) height = 1; - if (depth < 1) depth = 1; - } - - return uiImageSize; - } - - private class VtfImageFormatInfo { - public VtfImageFormat Format { get; set; } - public uint BitsPerPixel { get; set; } - public uint BytesPerPixel { get; set; } - public uint RedBitsPerPixel { get; set; } - public uint GreenBitsPerPixel { get; set; } - public uint BlueBitsPerPixel { get; set; } - public uint AlphaBitsPerPixel { get; set; } - public bool IsCompressed { get; set; } - public bool IsSupported { get; set; } - - public VtfImageFormatInfo(VtfImageFormat format, uint bitsPerPixel, uint bytesPerPixel, uint redBitsPerPixel, uint greenBitsPerPixel, uint blueBitsPerPixel, uint alphaBitsPerPixel, bool isCompressed, bool isSupported) { - Format = format; - BitsPerPixel = bitsPerPixel; - BytesPerPixel = bytesPerPixel; - RedBitsPerPixel = redBitsPerPixel; - GreenBitsPerPixel = greenBitsPerPixel; - BlueBitsPerPixel = blueBitsPerPixel; - AlphaBitsPerPixel = alphaBitsPerPixel; - IsCompressed = isCompressed; - IsSupported = isSupported; - } - } - - private static readonly Dictionary ImageFormats = new Dictionary - { - {VtfImageFormat.Rgba8888, new VtfImageFormatInfo(VtfImageFormat.Rgba8888, 32, 4, 8, 8, 8, 8, false, true)}, - {VtfImageFormat.Abgr8888, new VtfImageFormatInfo(VtfImageFormat.Abgr8888, 32, 4, 8, 8, 8, 8, false, true)}, - {VtfImageFormat.Rgb888, new VtfImageFormatInfo(VtfImageFormat.Rgb888, 24, 3, 8, 8, 8, 0, false, true)}, - {VtfImageFormat.Bgr888, new VtfImageFormatInfo(VtfImageFormat.Bgr888, 24, 3, 8, 8, 8, 0, false, true)}, - {VtfImageFormat.Rgb565, new VtfImageFormatInfo(VtfImageFormat.Rgb565, 16, 2, 5, 6, 5, 0, false, true)}, - {VtfImageFormat.I8, new VtfImageFormatInfo(VtfImageFormat.I8, 8, 1, 0, 0, 0, 0, false, true)}, - {VtfImageFormat.Ia88, new VtfImageFormatInfo(VtfImageFormat.Ia88, 16, 2, 0, 0, 0, 8, false, true)}, - {VtfImageFormat.P8, new VtfImageFormatInfo(VtfImageFormat.P8, 8, 1, 0, 0, 0, 0, false, false)}, - {VtfImageFormat.A8, new VtfImageFormatInfo(VtfImageFormat.A8, 8, 1, 0, 0, 0, 8, false, true)}, - {VtfImageFormat.Rgb888Bluescreen, new VtfImageFormatInfo(VtfImageFormat.Rgb888Bluescreen, 24, 3, 8, 8, 8, 0, false, true)}, - {VtfImageFormat.Bgr888Bluescreen, new VtfImageFormatInfo(VtfImageFormat.Bgr888Bluescreen, 24, 3, 8, 8, 8, 0, false, true)}, - {VtfImageFormat.Argb8888, new VtfImageFormatInfo(VtfImageFormat.Argb8888, 32, 4, 8, 8, 8, 8, false, true)}, - {VtfImageFormat.Bgra8888, new VtfImageFormatInfo(VtfImageFormat.Bgra8888, 32, 4, 8, 8, 8, 8, false, true)}, - {VtfImageFormat.Dxt1, new VtfImageFormatInfo(VtfImageFormat.Dxt1, 4, 0, 0, 0, 0, 0, true, true)}, - {VtfImageFormat.Dxt3, new VtfImageFormatInfo(VtfImageFormat.Dxt3, 8, 0, 0, 0, 0, 8, true, true)}, - {VtfImageFormat.Dxt5, new VtfImageFormatInfo(VtfImageFormat.Dxt5, 8, 0, 0, 0, 0, 8, true, true)}, - {VtfImageFormat.Bgrx8888, new VtfImageFormatInfo(VtfImageFormat.Bgrx8888, 32, 4, 8, 8, 8, 0, false, true)}, - {VtfImageFormat.Bgr565, new VtfImageFormatInfo(VtfImageFormat.Bgr565, 16, 2, 5, 6, 5, 0, false, true)}, - {VtfImageFormat.Bgrx5551, new VtfImageFormatInfo(VtfImageFormat.Bgrx5551, 16, 2, 5, 5, 5, 0, false, true)}, - {VtfImageFormat.Bgra4444, new VtfImageFormatInfo(VtfImageFormat.Bgra4444, 16, 2, 4, 4, 4, 4, false, true)}, - {VtfImageFormat.Dxt1Onebitalpha, new VtfImageFormatInfo(VtfImageFormat.Dxt1Onebitalpha, 4, 0, 0, 0, 0, 1, true, true)}, - {VtfImageFormat.Bgra5551, new VtfImageFormatInfo(VtfImageFormat.Bgra5551, 16, 2, 5, 5, 5, 1, false, true)}, - {VtfImageFormat.Uv88, new VtfImageFormatInfo(VtfImageFormat.Uv88, 16, 2, 8, 8, 0, 0, false, true)}, - {VtfImageFormat.Uvwq8888, new VtfImageFormatInfo(VtfImageFormat.Uvwq8888, 32, 4, 8, 8, 8, 8, false, true)}, - {VtfImageFormat.Rgba16161616F, new VtfImageFormatInfo(VtfImageFormat.Rgba16161616F, 64, 8, 16, 16, 16, 16, false, true)}, - {VtfImageFormat.Rgba16161616, new VtfImageFormatInfo(VtfImageFormat.Rgba16161616, 64, 8, 16, 16, 16, 16, false, true)}, - {VtfImageFormat.Uvlx8888, new VtfImageFormatInfo(VtfImageFormat.Uvlx8888, 32, 4, 8, 8, 8, 8, false, true)}, - {VtfImageFormat.R32F, new VtfImageFormatInfo(VtfImageFormat.R32F, 32, 4, 32, 0, 0, 0, false, true)}, - {VtfImageFormat.Rgb323232F, new VtfImageFormatInfo(VtfImageFormat.Rgb323232F, 96, 12, 32, 32, 32, 0, false, true)}, - {VtfImageFormat.Rgba32323232F, new VtfImageFormatInfo(VtfImageFormat.Rgba32323232F, 128, 16, 32, 32, 32, 32, false, true)}, - {VtfImageFormat.NvDst16, new VtfImageFormatInfo(VtfImageFormat.NvDst16, 16, 2, 0, 0, 0, 0, false, true)}, - {VtfImageFormat.NvDst24, new VtfImageFormatInfo(VtfImageFormat.NvDst24, 24, 3, 0, 0, 0, 0, false, true)}, - {VtfImageFormat.NvIntz, new VtfImageFormatInfo(VtfImageFormat.NvIntz, 32, 4, 0, 0, 0, 0, false, true)}, - {VtfImageFormat.NvRawz, new VtfImageFormatInfo(VtfImageFormat.NvRawz, 32, 4, 0, 0, 0, 0, false, true)}, - {VtfImageFormat.AtiDst16, new VtfImageFormatInfo(VtfImageFormat.AtiDst16, 16, 2, 0, 0, 0, 0, false, true)}, - {VtfImageFormat.AtiDst24, new VtfImageFormatInfo(VtfImageFormat.AtiDst24, 24, 3, 0, 0, 0, 0, false, true)}, - {VtfImageFormat.NvNull, new VtfImageFormatInfo(VtfImageFormat.NvNull, 32, 4, 0, 0, 0, 0, false, true)}, - {VtfImageFormat.Ati1N, new VtfImageFormatInfo(VtfImageFormat.Ati1N, 4, 0, 0, 0, 0, 0, true, true)}, - {VtfImageFormat.Ati2N, new VtfImageFormatInfo(VtfImageFormat.Ati2N, 8, 0, 0, 0, 0, 0, true, true)}, - }; - } -} \ No newline at end of file