2
0

DXT1Block.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.Runtime.InteropServices;
  18. using Microsoft.Xna.Framework;
  19. using Microsoft.Xna.Framework.Graphics.PackedVector;
  20. namespace nkast.Aether.Content.Pipeline
  21. {
  22. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  23. internal struct DXT1Block
  24. {
  25. Bgr565 color0;
  26. Bgr565 color1;
  27. byte idx0123;
  28. byte idx4567;
  29. byte idx89AB;
  30. byte idxCDEF;
  31. internal Color GetColor(int colorIndex)
  32. {
  33. int H = colorIndex / 4;
  34. int L = colorIndex % 4;
  35. int idx = 0;
  36. switch (H)
  37. {
  38. case 0:
  39. idx = idx0123;
  40. break;
  41. case 1:
  42. idx = idx4567;
  43. break;
  44. case 2:
  45. idx = idx89AB;
  46. break;
  47. case 3:
  48. idx = idxCDEF;
  49. break;
  50. }
  51. idx = (idx >> (L * 2));
  52. idx = idx & 0x03;
  53. switch (idx)
  54. {
  55. case 0: return new Color(color0.ToVector3());
  56. case 1: return new Color(color1.ToVector3());
  57. //case 2: return new Color((2 * color0.ToVector3() + color1.ToVector3()) / 3);
  58. //case 3: return new Color((color0.ToVector3() + 2 * color1.ToVector3()) / 3);
  59. case 2: return (color0.PackedValue > color1.PackedValue) ?
  60. new Color((2 * color0.ToVector3() + color1.ToVector3()) / 3) :
  61. new Color((color0.ToVector3() + color1.ToVector3()) / 2f);
  62. case 3: return (color0.PackedValue > color1.PackedValue) ?
  63. new Color((2 * color1.ToVector3() + color0.ToVector3()) / 3) :
  64. Color.Transparent;
  65. default: throw new NotSupportedException();
  66. }
  67. }
  68. }
  69. }