DDSImporter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #region License
  2. // Copyright 2015-2016 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.IO;
  18. using Microsoft.Xna.Framework.Content.Pipeline;
  19. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  20. using Microsoft.Xna.Framework.Graphics;
  21. namespace nkast.Aether.Content.Pipeline
  22. {
  23. [ContentImporter(".dds", DisplayName = "DDS Importer - Aether", DefaultProcessor = "DDSProcessor")]
  24. public class DDSImporter : ContentImporter<TextureContent>
  25. {
  26. public override TextureContent Import(string filename, ContentImporterContext context)
  27. {
  28. TextureContent output;
  29. using(Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  30. {
  31. using(BinaryReader reader = new BinaryReader(stream))
  32. {
  33. DDSHeader header = new DDSHeader(reader);
  34. if (header.PixelFormat.Flags == PF_Flags.FOURCC && header.PixelFormat.FourCC == PF_FourCC.DX10)
  35. {
  36. throw new NotImplementedException("DX10 Header not supported");
  37. }
  38. if (header.PitchOrLinearSize != 0)
  39. throw new NotImplementedException();
  40. if (header.Caps2.HasFlag(DDS_Caps2.CUBEMAP))
  41. {
  42. TextureCubeContent cube = new TextureCubeContent();
  43. SurfaceFormat format = GetFormat(header.PixelFormat);
  44. for (int f = 0; f < 6; f++)
  45. {
  46. int width = header.Width;
  47. int height = header.Height;
  48. BitmapContent bitmap = CreateBitmap(format, width, height);
  49. int size = GetBitmapSize(format, width, height);
  50. byte[] src = reader.ReadBytes(size);
  51. bitmap.SetPixelData(src);
  52. cube.Faces[f].Add(bitmap);
  53. if (header.Caps.HasFlag(DDS_Caps.MIPMAP))
  54. {
  55. for(int m=0; m<header.MipMapCount-1; m++)
  56. {
  57. width = width/2;
  58. height = height/2;
  59. bitmap = CreateBitmap(format, width, height);
  60. size = GetBitmapSize(format, width, height);
  61. src = reader.ReadBytes(size);
  62. bitmap.SetPixelData(src);
  63. cube.Faces[f].Add(bitmap);
  64. }
  65. }
  66. }
  67. output = cube;
  68. }
  69. else
  70. {
  71. throw new NotImplementedException(header.Caps2 + " not supported");
  72. }
  73. reader.Close();
  74. stream.Close();
  75. }
  76. }
  77. return output;
  78. }
  79. internal static int GetBitmapSize(SurfaceFormat format, int width, int height)
  80. {
  81. int pixels = width * height;
  82. switch (format)
  83. {
  84. case SurfaceFormat.Dxt1:
  85. return Math.Max(8, (pixels / 2));
  86. default:
  87. throw new NotImplementedException();
  88. }
  89. }
  90. private SurfaceFormat GetFormat(DDSPixelFormat pixelFormat)
  91. {
  92. switch (pixelFormat.Flags)
  93. {
  94. case PF_Flags.FOURCC:
  95. switch (pixelFormat.FourCC)
  96. {
  97. case PF_FourCC.DXT1:
  98. return SurfaceFormat.Dxt1;
  99. default:
  100. throw new NotImplementedException();
  101. }
  102. default:
  103. throw new NotImplementedException();
  104. }
  105. }
  106. private BitmapContent CreateBitmap(SurfaceFormat targetFormat, int width, int height)
  107. {
  108. switch (targetFormat)
  109. {
  110. case SurfaceFormat.Dxt1:
  111. return new Dxt1BitmapContent(width, height);
  112. default:
  113. throw new NotImplementedException();
  114. }
  115. }
  116. }
  117. }