TextureUtil.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //using System;
  2. //using System.Drawing;
  3. //using ImageMagick;
  4. //namespace Util
  5. //{
  6. // public static class TextureUtil
  7. // {
  8. // public struct Tuple
  9. // {
  10. // public bool Item1;
  11. // public int Item2;
  12. // public float Item3;
  13. // public Tuple(bool item1, int item2, float item3)
  14. // {
  15. // this.Item1 = item1;
  16. // this.Item2 = item2;
  17. // this.Item3 = item3;
  18. // }
  19. // }
  20. // public static void EXR2HDRPNG(string filePath)
  21. // {
  22. // int width = 0;
  23. // int height = 0;
  24. // MagickReadSettings settings = new MagickReadSettings();
  25. // settings.Format = MagickFormat.Exr;
  26. // // Read from file.
  27. // MagickImage image = new MagickImage(filePath, settings);
  28. // width = image.Width;
  29. // height = image.Height;
  30. // float[] pixelBuffer = new float[width * height * 4];
  31. // float[] fixedPixelBuffer = new float[width * height * 4];
  32. // byte[] rgbeBuffer = new byte[width * height * 4];
  33. // int count = 0;
  34. // IPixelCollection pixelCollection = image.GetPixels();
  35. // pixelBuffer = pixelCollection.GetValues();
  36. // // 修正參數是2^16,自己總結出來的,目前測試的是正確的
  37. // int fixConst = 65535;
  38. // for (int y = 0; y < height; y++)
  39. // {
  40. // for (int x = 0; x < width; x++)
  41. // {
  42. // fixedPixelBuffer[count++] = pixelBuffer[((y) * width + x) * 4] / fixConst;
  43. // fixedPixelBuffer[count++] = pixelBuffer[((y) * width + x) * 4 + 1] / fixConst;
  44. // fixedPixelBuffer[count++] = pixelBuffer[((y) * width + x) * 4 + 2] / fixConst;
  45. // fixedPixelBuffer[count++] = pixelBuffer[((y) * width + x) * 4 + 3] / fixConst;
  46. // }
  47. // }
  48. // // 把HDR的Float類型轉成RGBE類型的像素
  49. // FloatRGB2RGBE(width * height, fixedPixelBuffer, rgbeBuffer);
  50. // // save as png
  51. // Bitmap bmp = new Bitmap(width, height);
  52. // for (int y = 0; y < height; ++y)
  53. // for (int x = 0; x < width; ++x)
  54. // {
  55. // int offset = (y * width + x) * 4;
  56. // bmp.SetPixel(x, y, Color.FromArgb
  57. // (rgbeBuffer[offset + 3], rgbeBuffer[offset + 0], rgbeBuffer[offset + 1], rgbeBuffer[offset + 2]));
  58. // }
  59. // int lastDotIndex = filePath.LastIndexOf('.');
  60. // string outputPath = filePath.Substring(0, lastDotIndex) + ".png";
  61. // bmp.Save(outputPath);
  62. // }
  63. // // 把HDR的Float類型轉成RGBE類型的像素
  64. // private static void FloatRGB2RGBE(int InImageSize, float[] InPixelBuffer, byte[] OutRGBEBuffer)
  65. // {
  66. // for (int i = 0; i < InImageSize; i++)
  67. // {
  68. // float red = InPixelBuffer[i * 4 + 0];
  69. // float green = InPixelBuffer[i * 4 + 1];
  70. // float blue = InPixelBuffer[i * 4 + 2];
  71. // float maxValue = red;
  72. // if (green > maxValue) maxValue = green;
  73. // if (blue > maxValue) maxValue = blue;
  74. // if (maxValue < Single.Epsilon)
  75. // {
  76. // OutRGBEBuffer[i * 4 + 0] = OutRGBEBuffer[i * 4 + 1]
  77. // = OutRGBEBuffer[i * 4 + 2] = OutRGBEBuffer[i * 4 + 3] = 0;
  78. // }
  79. // else
  80. // {
  81. // int exponent = 0;
  82. // float mantissa = 0;
  83. // Tuple tuple = SHARP_frexp(maxValue);
  84. // if (tuple.Item1)
  85. // {
  86. // OutRGBEBuffer[i * 4 + 0] = OutRGBEBuffer[i * 4 + 1]
  87. // = OutRGBEBuffer[i * 4 + 2] = OutRGBEBuffer[i * 4 + 3] = 0;
  88. // return;
  89. // }
  90. // else
  91. // {
  92. // exponent = tuple.Item2;
  93. // mantissa = tuple.Item3;
  94. // // 当某个值为v的时候,其尾数就是e[0]。 这里*256了,所以反向的时候有个/256即-(128+8)里的8
  95. // // e[0]永远不会为1所以结果<256
  96. // float scaleRatio = (float)(mantissa * 256.0 / maxValue);
  97. // OutRGBEBuffer[i * 4 + 0] = (byte)(red * scaleRatio);
  98. // OutRGBEBuffer[i * 4 + 1] = (byte)(green * scaleRatio);
  99. // OutRGBEBuffer[i * 4 + 2] = (byte)(blue * scaleRatio);
  100. // OutRGBEBuffer[i * 4 + 3] = (byte)(exponent + 128);
  101. // }
  102. // }
  103. // }
  104. // }
  105. // // 把一個浮點數轉換成X*2^N的形式
  106. // // 比如8.000000 = 0.500000 * 2^4,123.45 = 0.964453 * 2^7
  107. // private static Tuple SHARP_frexp(double d)
  108. // {
  109. // // Translate the double into sign, exponent and mantissa.
  110. // long bits = BitConverter.DoubleToInt64Bits(d);
  111. // // Note that the shift is sign-extended, hence the test against -1 not 1
  112. // bool negative = (bits < 0);
  113. // int exponent = (int)((bits >> 52) & 0x7ffL);
  114. // long mantissa = bits & 0xfffffffffffffL;
  115. // // Subnormal numbers; exponent is effectively one higher,
  116. // // but there's no extra normalisation bit in the mantissa
  117. // if (exponent == 0)
  118. // {
  119. // exponent++;
  120. // }
  121. // // Normal numbers; leave exponent as it is but add extra
  122. // // bit to the front of the mantissa
  123. // else
  124. // {
  125. // mantissa = mantissa | (1L << 52);
  126. // }
  127. // // Bias the exponent. It's actually biased by 1023, but we're
  128. // // treating the mantissa as m.0 rather than 0.m, so we need
  129. // // to subtract another 52 from it.
  130. // exponent -= 1075;
  131. // if (mantissa == 0)
  132. // {
  133. // return new Tuple(false, 0, 0);
  134. // }
  135. // /* Normalize */
  136. // while ((mantissa & 1) == 0)
  137. // { /* i.e., Mantissa is even */
  138. // mantissa >>= 1;
  139. // exponent++;
  140. // }
  141. // // 原來的算法是m.0*2^n 要換成0.m*2^n
  142. // float floatMantissa = mantissa;
  143. // while (floatMantissa > 1 - Single.Epsilon)
  144. // {
  145. // floatMantissa /= 2;
  146. // exponent++;
  147. // }
  148. // return new Tuple(negative, exponent, floatMantissa);
  149. // }
  150. // }
  151. //}