1
0

SculptMap.cs 5.8 KB

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