SculptMap.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using System.Drawing;
  31. using System.Drawing.Imaging;
  32. namespace PrimMesher
  33. {
  34. public class SculptMap
  35. {
  36. public int width;
  37. public int height;
  38. public byte[] redBytes;
  39. public byte[] greenBytes;
  40. public byte[] blueBytes;
  41. public SculptMap()
  42. {
  43. }
  44. public SculptMap(Bitmap bm, int lod)
  45. {
  46. int bmW = bm.Width;
  47. int bmH = bm.Height;
  48. if (bmW == 0 || bmH == 0)
  49. throw new Exception("SculptMap: bitmap has no data");
  50. int numLodPixels = lod * lod; // (32 * 2)^2 = 64^2 pixels for default sculpt map image
  51. bool needsScaling = false;
  52. bool smallMap = false;
  53. width = bmW;
  54. height = bmH;
  55. while (width * height > numLodPixels * 4)
  56. {
  57. width >>= 1;
  58. height >>= 1;
  59. needsScaling = true;
  60. }
  61. if (needsScaling)
  62. bm = ScaleImage(bm, width, height);
  63. if (width * height > numLodPixels)
  64. {
  65. smallMap = false;
  66. width >>= 1;
  67. height >>= 1;
  68. }
  69. else
  70. smallMap = true;
  71. int numBytes = (width + 1) * (height + 1);
  72. redBytes = new byte[numBytes];
  73. greenBytes = new byte[numBytes];
  74. blueBytes = new byte[numBytes];
  75. int byteNdx = 0;
  76. Color c;
  77. try
  78. {
  79. for (int y = 0; y <= height; y++)
  80. {
  81. for (int x = 0; x < width; x++)
  82. {
  83. if (smallMap)
  84. c = bm.GetPixel(x, y < height ? y : y - 1);
  85. else
  86. c = bm.GetPixel(x * 2, y < height ? y * 2 : y * 2 - 1);
  87. redBytes[byteNdx] = c.R;
  88. greenBytes[byteNdx] = c.G;
  89. blueBytes[byteNdx] = c.B;
  90. ++byteNdx;
  91. }
  92. if (smallMap)
  93. c = bm.GetPixel(width - 1, y < height ? y : y - 1);
  94. else
  95. c = bm.GetPixel(width * 2 - 1, 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. catch (Exception e)
  103. {
  104. if (needsScaling)
  105. bm.Dispose();
  106. throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString());
  107. }
  108. width++;
  109. height++;
  110. if(needsScaling)
  111. bm.Dispose();
  112. }
  113. public List<List<Coord>> ToRows(bool mirror)
  114. {
  115. int numRows = height;
  116. int numCols = width;
  117. List<List<Coord>> rows = new List<List<Coord>>(numRows);
  118. float pixScale = 1.0f / 255;
  119. int rowNdx, colNdx;
  120. int smNdx = 0;
  121. for (rowNdx = 0; rowNdx < numRows; rowNdx++)
  122. {
  123. List<Coord> row = new List<Coord>(numCols);
  124. for (colNdx = 0; colNdx < numCols; colNdx++)
  125. {
  126. if (mirror)
  127. row.Add(new Coord(-((float)redBytes[smNdx] * pixScale - 0.5f), ((float)greenBytes[smNdx] * pixScale - 0.5f), (float)blueBytes[smNdx] * pixScale - 0.5f));
  128. else
  129. row.Add(new Coord((float)redBytes[smNdx] * pixScale - 0.5f, (float)greenBytes[smNdx] * pixScale - 0.5f, (float)blueBytes[smNdx] * pixScale - 0.5f));
  130. ++smNdx;
  131. }
  132. rows.Add(row);
  133. }
  134. return rows;
  135. }
  136. private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight)
  137. {
  138. Bitmap scaledImage = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
  139. Color c;
  140. // will let last step to be eventually diferent, as seems to be in sl
  141. float xscale = (float)srcImage.Width / (float)destWidth;
  142. float yscale = (float)srcImage.Height / (float)destHeight;
  143. int lastsx = srcImage.Width - 1;
  144. int lastsy = srcImage.Height - 1;
  145. int lastdx = destWidth - 1;
  146. int lastdy = destHeight - 1;
  147. float sy = 0.5f;
  148. float sx;
  149. for (int y = 0; y < lastdy; y++)
  150. {
  151. sx = 0.5f;
  152. for (int x = 0; x < lastdx; x++)
  153. {
  154. try
  155. {
  156. c = srcImage.GetPixel((int)(sx), (int)(sy));
  157. scaledImage.SetPixel(x, y, Color.FromArgb(c.R, c.G, c.B));
  158. }
  159. catch (IndexOutOfRangeException)
  160. {
  161. }
  162. sx += xscale;
  163. }
  164. try
  165. {
  166. c = srcImage.GetPixel(lastsx, (int)(sy));
  167. scaledImage.SetPixel(lastdx, y, Color.FromArgb(c.R, c.G, c.B));
  168. }
  169. catch (IndexOutOfRangeException)
  170. {
  171. }
  172. sy += yscale;
  173. }
  174. sx = 0.5f;
  175. for (int x = 0; x < lastdx; x++)
  176. {
  177. try
  178. {
  179. c = srcImage.GetPixel((int)(sx), lastsy);
  180. scaledImage.SetPixel(x, lastdy, Color.FromArgb(c.R, c.G, c.B));
  181. }
  182. catch (IndexOutOfRangeException)
  183. {
  184. }
  185. sx += xscale;
  186. }
  187. try
  188. {
  189. c = srcImage.GetPixel(lastsx, lastsy);
  190. scaledImage.SetPixel(lastdx, lastdy, Color.FromArgb(c.R, c.G, c.B));
  191. }
  192. catch (IndexOutOfRangeException)
  193. {
  194. }
  195. srcImage.Dispose();
  196. return scaledImage;
  197. }
  198. }
  199. }