DDSProcessor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Microsoft.Xna.Framework;
  17. using Microsoft.Xna.Framework.Content.Pipeline;
  18. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  19. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  20. using Microsoft.Xna.Framework.Graphics;
  21. namespace nkast.Aether.Content.Pipeline
  22. {
  23. [ContentProcessor(DisplayName = "DDSProcessor - Aether")]
  24. public class DDSProcessor : ContentProcessor<TextureContent, TextureContent>
  25. {
  26. public virtual bool GenerateMipmaps { get; set; }
  27. public virtual TextureProcessorOutputFormat TextureFormat { get; set; }
  28. public DDSProcessor()
  29. {
  30. GenerateMipmaps = true;
  31. TextureFormat = TextureProcessorOutputFormat.NoChange;
  32. }
  33. public override TextureContent Process(TextureContent input, ContentProcessorContext context)
  34. {
  35. TextureContent output = input;
  36. switch (TextureFormat)
  37. {
  38. case TextureProcessorOutputFormat.Color:
  39. SurfaceFormat format;
  40. output.Faces[0][0].TryGetFormat(out format);
  41. if (format != SurfaceFormat.Color)
  42. ConvertToColor(output);
  43. break;
  44. }
  45. //output.ConvertBitmapType();
  46. if (GenerateMipmaps)
  47. output.GenerateMipmaps(false);
  48. return output;
  49. }
  50. private void ConvertToColor(TextureContent textureContent)
  51. {
  52. foreach (MipmapChain face in textureContent.Faces)
  53. {
  54. for (int m = 0; m < face.Count; m++)
  55. {
  56. BitmapContent input = face[m];
  57. BitmapContent output = ConvertToColor(input);
  58. face[m] = output;
  59. }
  60. }
  61. return;
  62. }
  63. unsafe private static BitmapContent ConvertToColor(BitmapContent input)
  64. {
  65. int width = input.Width;
  66. int height = input.Height;
  67. SurfaceFormat format;
  68. input.TryGetFormat(out format);
  69. int formatSize = DDSImporter.GetBitmapSize(format, width, height);
  70. int blocks = formatSize;
  71. byte[] inData = input.GetPixelData();
  72. var output = new PixelBitmapContent<Color>(width, height);
  73. fixed (byte* p = &inData[0])
  74. {
  75. DXT1Block* block = (DXT1Block*)p;
  76. //convert DXT1 to color
  77. for (int y = 0; y < height; ++y)
  78. {
  79. for (int x = 0; x < width; ++x)
  80. {
  81. int blockIdx = (x / 4) + (y / 4) * (width / 4);
  82. int colorIndex = x % 4 + (y % 4) * 4;
  83. Color color = block[blockIdx].GetColor(colorIndex);
  84. output.SetPixel(x, y, color);
  85. }
  86. }
  87. }
  88. return output;
  89. }
  90. }
  91. }