SculptMap.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 * 2 * lod * 2; // (32 * 2)^2 = 64^2 pixels for default sculpt map image
  54. bool needsScaling = false;
  55. bool smallMap = bmW * bmH <= lod * lod;
  56. width = bmW;
  57. height = bmH;
  58. while (width * height > numLodPixels)
  59. {
  60. width >>= 1;
  61. height >>= 1;
  62. needsScaling = true;
  63. }
  64. try
  65. {
  66. if (needsScaling)
  67. bm = ScaleImage(bm, width, height,
  68. System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor);
  69. }
  70. catch (Exception e)
  71. {
  72. throw new Exception("Exception in ScaleImage(): e: " + e.ToString());
  73. }
  74. if (width * height > lod * lod)
  75. {
  76. width >>= 1;
  77. height >>= 1;
  78. }
  79. int numBytes = (width + 1) * (height + 1);
  80. redBytes = new byte[numBytes];
  81. greenBytes = new byte[numBytes];
  82. blueBytes = new byte[numBytes];
  83. int byteNdx = 0;
  84. try
  85. {
  86. for (int y = 0; y <= height; y++)
  87. {
  88. for (int x = 0; x <= width; x++)
  89. {
  90. Color c;
  91. if (smallMap)
  92. c = bm.GetPixel(x < width ? x : x - 1,
  93. y < height ? y : y - 1);
  94. else
  95. c = bm.GetPixel(x < width ? x * 2 : x * 2 - 1,
  96. y < height ? y * 2 : y * 2 - 1);
  97. redBytes[byteNdx] = c.R;
  98. greenBytes[byteNdx] = c.G;
  99. blueBytes[byteNdx] = c.B;
  100. ++byteNdx;
  101. }
  102. }
  103. }
  104. catch (Exception e)
  105. {
  106. throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString());
  107. }
  108. width++;
  109. height++;
  110. }
  111. public List<List<Coord>> ToRows(bool mirror)
  112. {
  113. int numRows = height;
  114. int numCols = width;
  115. List<List<Coord>> rows = new List<List<Coord>>(numRows);
  116. float pixScale = 1.0f / 255;
  117. int rowNdx, colNdx;
  118. int smNdx = 0;
  119. for (rowNdx = 0; rowNdx < numRows; rowNdx++)
  120. {
  121. List<Coord> row = new List<Coord>(numCols);
  122. for (colNdx = 0; colNdx < numCols; colNdx++)
  123. {
  124. if (mirror)
  125. row.Add(new Coord(-(redBytes[smNdx] * pixScale - 0.5f), (greenBytes[smNdx] * pixScale - 0.5f), blueBytes[smNdx] * pixScale - 0.5f));
  126. else
  127. row.Add(new Coord(redBytes[smNdx] * pixScale - 0.5f, greenBytes[smNdx] * pixScale - 0.5f, blueBytes[smNdx] * pixScale - 0.5f));
  128. ++smNdx;
  129. }
  130. rows.Add(row);
  131. }
  132. return rows;
  133. }
  134. private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight,
  135. System.Drawing.Drawing2D.InterpolationMode interpMode)
  136. {
  137. Bitmap scaledImage = new Bitmap(srcImage, destWidth, destHeight);
  138. scaledImage.SetResolution(96.0f, 96.0f);
  139. Graphics grPhoto = Graphics.FromImage(scaledImage);
  140. grPhoto.InterpolationMode = interpMode;
  141. grPhoto.DrawImage(srcImage,
  142. new Rectangle(0, 0, destWidth, destHeight),
  143. new Rectangle(0, 0, srcImage.Width, srcImage.Height),
  144. GraphicsUnit.Pixel);
  145. grPhoto.Dispose();
  146. return scaledImage;
  147. }
  148. }
  149. }
  150. #endif