SculptMap.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) Contributors
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // to build without references to System.Drawing, comment this out
  28. #define SYSTEM_DRAWING
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. #if SYSTEM_DRAWING
  33. using System.Drawing;
  34. using System.Drawing.Imaging;
  35. namespace PrimMesher
  36. {
  37. public class SculptMap
  38. {
  39. public int width;
  40. public int height;
  41. public byte[] redBytes;
  42. public byte[] greenBytes;
  43. public byte[] blueBytes;
  44. public SculptMap()
  45. {
  46. }
  47. public SculptMap(Bitmap bm, int lod)
  48. {
  49. int bmW = bm.Width;
  50. int bmH = bm.Height;
  51. if (bmW == 0 || bmH == 0)
  52. throw new Exception("SculptMap: bitmap has no data");
  53. int numLodPixels = lod * lod; // (32 * 2)^2 = 64^2 pixels for default sculpt map image
  54. bool smallMap = bmW * bmH <= numLodPixels;
  55. bool needsScaling = false;
  56. width = bmW;
  57. height = bmH;
  58. while (width * height > numLodPixels * 4)
  59. {
  60. width >>= 1;
  61. height >>= 1;
  62. needsScaling = true;
  63. }
  64. try
  65. {
  66. if (needsScaling)
  67. bm = ScaleImage(bm, width, height);
  68. }
  69. catch (Exception e)
  70. {
  71. throw new Exception("Exception in ScaleImage(): e: " + e.ToString());
  72. }
  73. if (width * height > numLodPixels)
  74. {
  75. width >>= 1;
  76. height >>= 1;
  77. }
  78. int numBytes = (width + 1) * (height + 1);
  79. redBytes = new byte[numBytes];
  80. greenBytes = new byte[numBytes];
  81. blueBytes = new byte[numBytes];
  82. int byteNdx = 0;
  83. try
  84. {
  85. for (int y = 0; y <= height; y++)
  86. {
  87. for (int x = 0; x <= width; x++)
  88. {
  89. Color c;
  90. if (smallMap)
  91. c = bm.GetPixel(x < width ? x : x - 1,
  92. y < height ? y : y - 1);
  93. else
  94. c = bm.GetPixel(x < width ? x * 2 : x * 2 - 1,
  95. y < height ? y * 2 : y * 2 - 1);
  96. redBytes[byteNdx] = c.R;
  97. greenBytes[byteNdx] = c.G;
  98. blueBytes[byteNdx] = c.B;
  99. ++byteNdx;
  100. }
  101. }
  102. }
  103. catch (Exception e)
  104. {
  105. throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString());
  106. }
  107. width++;
  108. height++;
  109. }
  110. public List<List<Coord>> ToRows(bool mirror)
  111. {
  112. int numRows = height;
  113. int numCols = width;
  114. List<List<Coord>> rows = new List<List<Coord>>(numRows);
  115. float pixScale = 1.0f / 255;
  116. int rowNdx, colNdx;
  117. int smNdx = 0;
  118. for (rowNdx = 0; rowNdx < numRows; rowNdx++)
  119. {
  120. List<Coord> row = new List<Coord>(numCols);
  121. for (colNdx = 0; colNdx < numCols; colNdx++)
  122. {
  123. if (mirror)
  124. row.Add(new Coord(-((float)redBytes[smNdx] * pixScale - 0.5f), ((float)greenBytes[smNdx] * pixScale - 0.5f), (float)blueBytes[smNdx] * pixScale - 0.5f));
  125. else
  126. row.Add(new Coord((float)redBytes[smNdx] * pixScale - 0.5f, (float)greenBytes[smNdx] * pixScale - 0.5f, (float)blueBytes[smNdx] * pixScale - 0.5f));
  127. ++smNdx;
  128. }
  129. rows.Add(row);
  130. }
  131. return rows;
  132. }
  133. private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight)
  134. {
  135. Bitmap scaledImage = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
  136. Color c;
  137. float xscale = srcImage.Width / destWidth;
  138. float yscale = srcImage.Height / destHeight;
  139. float sy = 0.5f;
  140. for (int y = 0; y < destHeight; y++)
  141. {
  142. float sx = 0.5f;
  143. for (int x = 0; x < destWidth; x++)
  144. {
  145. try
  146. {
  147. c = srcImage.GetPixel((int)(sx), (int)(sy));
  148. scaledImage.SetPixel(x, y, Color.FromArgb(c.R, c.G, c.B));
  149. }
  150. catch (IndexOutOfRangeException)
  151. {
  152. }
  153. sx += xscale;
  154. }
  155. sy += yscale;
  156. }
  157. srcImage.Dispose();
  158. return scaledImage;
  159. }
  160. }
  161. }
  162. #endif