Warp3DImageModule.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  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.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.IO;
  32. using System.Reflection;
  33. using CSJ2K;
  34. using Nini.Config;
  35. using log4net;
  36. using Rednettle.Warp3D;
  37. using Mono.Addins;
  38. using OpenMetaverse;
  39. using OpenMetaverse.Imaging;
  40. using OpenMetaverse.Rendering;
  41. using OpenMetaverse.StructuredData;
  42. using OpenSim.Framework;
  43. using OpenSim.Region.Framework.Interfaces;
  44. using OpenSim.Region.Framework.Scenes;
  45. using OpenSim.Region.Physics.Manager;
  46. using OpenSim.Services.Interfaces;
  47. using WarpRenderer = global::Warp3D.Warp3D;
  48. namespace OpenSim.Region.CoreModules.World.Warp3DMap
  49. {
  50. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "Warp3DImageModule")]
  51. public class Warp3DImageModule : IMapImageGenerator, INonSharedRegionModule
  52. {
  53. private static readonly UUID TEXTURE_METADATA_MAGIC = new UUID("802dc0e0-f080-4931-8b57-d1be8611c4f3");
  54. private static readonly Color4 WATER_COLOR = new Color4(29, 72, 96, 216);
  55. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  56. private Scene m_scene;
  57. private IRendering m_primMesher;
  58. private IConfigSource m_config;
  59. private Dictionary<UUID, Color4> m_colors = new Dictionary<UUID, Color4>();
  60. private bool m_useAntiAliasing = false; // TODO: Make this a config option
  61. private bool m_Enabled = false;
  62. #region Region Module interface
  63. public void Initialise(IConfigSource source)
  64. {
  65. m_config = source;
  66. IConfig startupConfig = m_config.Configs["Startup"];
  67. if (startupConfig.GetString("MapImageModule", "MapImageModule") != "Warp3DImageModule")
  68. return;
  69. m_Enabled = true;
  70. }
  71. public void AddRegion(Scene scene)
  72. {
  73. if (!m_Enabled)
  74. return;
  75. m_scene = scene;
  76. List<string> renderers = RenderingLoader.ListRenderers(Util.ExecutingDirectory());
  77. if (renderers.Count > 0)
  78. {
  79. m_primMesher = RenderingLoader.LoadRenderer(renderers[0]);
  80. m_log.DebugFormat("[WARP 3D IMAGE MODULE]: Loaded prim mesher {0}", m_primMesher);
  81. }
  82. else
  83. {
  84. m_log.Debug("[WARP 3D IMAGE MODULE]: No prim mesher loaded, prim rendering will be disabled");
  85. }
  86. m_scene.RegisterModuleInterface<IMapImageGenerator>(this);
  87. }
  88. public void RegionLoaded(Scene scene)
  89. {
  90. }
  91. public void RemoveRegion(Scene scene)
  92. {
  93. }
  94. public void Close()
  95. {
  96. }
  97. public string Name
  98. {
  99. get { return "Warp3DImageModule"; }
  100. }
  101. public Type ReplaceableInterface
  102. {
  103. get { return null; }
  104. }
  105. #endregion
  106. #region IMapImageGenerator Members
  107. public Bitmap CreateMapTile()
  108. {
  109. Vector3 camPos = new Vector3(127.5f, 127.5f, 221.7025033688163f);
  110. Viewport viewport = new Viewport(camPos, -Vector3.UnitZ, 1024f, 0.1f, (int)Constants.RegionSize, (int)Constants.RegionSize, (float)Constants.RegionSize, (float)Constants.RegionSize);
  111. return CreateMapTile(viewport, false);
  112. }
  113. public Bitmap CreateViewImage(Vector3 camPos, Vector3 camDir, float fov, int width, int height, bool useTextures)
  114. {
  115. Viewport viewport = new Viewport(camPos, camDir, fov, (float)Constants.RegionSize, 0.1f, width, height);
  116. return CreateMapTile(viewport, useTextures);
  117. }
  118. public Bitmap CreateMapTile(Viewport viewport, bool useTextures)
  119. {
  120. bool drawPrimVolume = true;
  121. bool textureTerrain = true;
  122. try
  123. {
  124. IConfig startupConfig = m_config.Configs["Startup"];
  125. drawPrimVolume = startupConfig.GetBoolean("DrawPrimOnMapTile", drawPrimVolume);
  126. textureTerrain = startupConfig.GetBoolean("TextureOnMapTile", textureTerrain);
  127. }
  128. catch
  129. {
  130. m_log.Warn("[WARP 3D IMAGE MODULE]: Failed to load StartupConfig");
  131. }
  132. m_colors.Clear();
  133. int width = viewport.Width;
  134. int height = viewport.Height;
  135. if (m_useAntiAliasing)
  136. {
  137. width *= 2;
  138. height *= 2;
  139. }
  140. WarpRenderer renderer = new WarpRenderer();
  141. renderer.CreateScene(width, height);
  142. renderer.Scene.autoCalcNormals = false;
  143. #region Camera
  144. warp_Vector pos = ConvertVector(viewport.Position);
  145. pos.z -= 0.001f; // Works around an issue with the Warp3D camera
  146. warp_Vector lookat = warp_Vector.add(ConvertVector(viewport.Position), ConvertVector(viewport.LookDirection));
  147. renderer.Scene.defaultCamera.setPos(pos);
  148. renderer.Scene.defaultCamera.lookAt(lookat);
  149. if (viewport.Orthographic)
  150. {
  151. renderer.Scene.defaultCamera.isOrthographic = true;
  152. renderer.Scene.defaultCamera.orthoViewWidth = viewport.OrthoWindowWidth;
  153. renderer.Scene.defaultCamera.orthoViewHeight = viewport.OrthoWindowHeight;
  154. }
  155. else
  156. {
  157. float fov = viewport.FieldOfView;
  158. fov *= 1.75f; // FIXME: ???
  159. renderer.Scene.defaultCamera.setFov(fov);
  160. }
  161. #endregion Camera
  162. renderer.Scene.addLight("Light1", new warp_Light(new warp_Vector(1.0f, 0.5f, 1f), 0xffffff, 0, 320, 40));
  163. renderer.Scene.addLight("Light2", new warp_Light(new warp_Vector(-1f, -1f, 1f), 0xffffff, 0, 100, 40));
  164. CreateWater(renderer);
  165. CreateTerrain(renderer, textureTerrain);
  166. if (drawPrimVolume)
  167. CreateAllPrims(renderer, useTextures);
  168. renderer.Render();
  169. Bitmap bitmap = renderer.Scene.getImage();
  170. if (m_useAntiAliasing)
  171. {
  172. using (Bitmap origBitmap = bitmap)
  173. bitmap = ImageUtils.ResizeImage(origBitmap, viewport.Width, viewport.Height);
  174. }
  175. // XXX: It shouldn't really be necesary to force a GC here as one should occur anyway pretty shortly
  176. // afterwards. It's generally regarded as a bad idea to manually GC. If Warp3D is using lots of memory
  177. // then this may be some issue with the Warp3D code itself, though it's also quite possible that generating
  178. // this map tile simply takes a lot of memory.
  179. GC.Collect();
  180. m_log.Debug("[WARP 3D IMAGE MODULE]: GC.Collect()");
  181. return bitmap;
  182. }
  183. public byte[] WriteJpeg2000Image()
  184. {
  185. try
  186. {
  187. using (Bitmap mapbmp = CreateMapTile())
  188. return OpenJPEG.EncodeFromImage(mapbmp, true);
  189. }
  190. catch (Exception e)
  191. {
  192. // JPEG2000 encoder failed
  193. m_log.Error("[WARP 3D IMAGE MODULE]: Failed generating terrain map: ", e);
  194. }
  195. return null;
  196. }
  197. #endregion
  198. #region Rendering Methods
  199. private void CreateWater(WarpRenderer renderer)
  200. {
  201. float waterHeight = (float)m_scene.RegionInfo.RegionSettings.WaterHeight;
  202. renderer.AddPlane("Water", 256f * 0.5f);
  203. renderer.Scene.sceneobject("Water").setPos(127.5f, waterHeight, 127.5f);
  204. renderer.AddMaterial("WaterColor", ConvertColor(WATER_COLOR));
  205. renderer.Scene.material("WaterColor").setReflectivity(0); // match water color with standard map module thanks lkalif
  206. renderer.Scene.material("WaterColor").setTransparency((byte)((1f - WATER_COLOR.A) * 255f));
  207. renderer.SetObjectMaterial("Water", "WaterColor");
  208. }
  209. private void CreateTerrain(WarpRenderer renderer, bool textureTerrain)
  210. {
  211. ITerrainChannel terrain = m_scene.Heightmap;
  212. float[] heightmap = terrain.GetFloatsSerialised();
  213. warp_Object obj = new warp_Object(256 * 256, 255 * 255 * 2);
  214. for (int y = 0; y < 256; y++)
  215. {
  216. for (int x = 0; x < 256; x++)
  217. {
  218. int v = y * 256 + x;
  219. float height = heightmap[v];
  220. warp_Vector pos = ConvertVector(new Vector3(x, y, height));
  221. obj.addVertex(new warp_Vertex(pos, (float)x / 255f, (float)(255 - y) / 255f));
  222. }
  223. }
  224. for (int y = 0; y < 256; y++)
  225. {
  226. for (int x = 0; x < 256; x++)
  227. {
  228. if (x < 255 && y < 255)
  229. {
  230. int v = y * 256 + x;
  231. // Normal
  232. Vector3 v1 = new Vector3(x, y, heightmap[y * 256 + x]);
  233. Vector3 v2 = new Vector3(x + 1, y, heightmap[y * 256 + x + 1]);
  234. Vector3 v3 = new Vector3(x, y + 1, heightmap[(y + 1) * 256 + x]);
  235. warp_Vector norm = ConvertVector(SurfaceNormal(v1, v2, v3));
  236. norm = norm.reverse();
  237. obj.vertex(v).n = norm;
  238. // Triangle 1
  239. obj.addTriangle(
  240. v,
  241. v + 1,
  242. v + 256);
  243. // Triangle 2
  244. obj.addTriangle(
  245. v + 256 + 1,
  246. v + 256,
  247. v + 1);
  248. }
  249. }
  250. }
  251. renderer.Scene.addObject("Terrain", obj);
  252. UUID[] textureIDs = new UUID[4];
  253. float[] startHeights = new float[4];
  254. float[] heightRanges = new float[4];
  255. RegionSettings regionInfo = m_scene.RegionInfo.RegionSettings;
  256. textureIDs[0] = regionInfo.TerrainTexture1;
  257. textureIDs[1] = regionInfo.TerrainTexture2;
  258. textureIDs[2] = regionInfo.TerrainTexture3;
  259. textureIDs[3] = regionInfo.TerrainTexture4;
  260. startHeights[0] = (float)regionInfo.Elevation1SW;
  261. startHeights[1] = (float)regionInfo.Elevation1NW;
  262. startHeights[2] = (float)regionInfo.Elevation1SE;
  263. startHeights[3] = (float)regionInfo.Elevation1NE;
  264. heightRanges[0] = (float)regionInfo.Elevation2SW;
  265. heightRanges[1] = (float)regionInfo.Elevation2NW;
  266. heightRanges[2] = (float)regionInfo.Elevation2SE;
  267. heightRanges[3] = (float)regionInfo.Elevation2NE;
  268. uint globalX, globalY;
  269. Utils.LongToUInts(m_scene.RegionInfo.RegionHandle, out globalX, out globalY);
  270. warp_Texture texture;
  271. using (
  272. Bitmap image
  273. = TerrainSplat.Splat(
  274. heightmap, textureIDs, startHeights, heightRanges,
  275. new Vector3d(globalX, globalY, 0.0), m_scene.AssetService, textureTerrain))
  276. {
  277. texture = new warp_Texture(image);
  278. }
  279. warp_Material material = new warp_Material(texture);
  280. material.setReflectivity(50);
  281. renderer.Scene.addMaterial("TerrainColor", material);
  282. renderer.Scene.material("TerrainColor").setReflectivity(0); // reduces tile seams a bit thanks lkalif
  283. renderer.SetObjectMaterial("Terrain", "TerrainColor");
  284. }
  285. private void CreateAllPrims(WarpRenderer renderer, bool useTextures)
  286. {
  287. if (m_primMesher == null)
  288. return;
  289. m_scene.ForEachSOG(
  290. delegate(SceneObjectGroup group)
  291. {
  292. CreatePrim(renderer, group.RootPart, useTextures);
  293. foreach (SceneObjectPart child in group.Parts)
  294. CreatePrim(renderer, child, useTextures);
  295. }
  296. );
  297. }
  298. private void CreatePrim(WarpRenderer renderer, SceneObjectPart prim,
  299. bool useTextures)
  300. {
  301. const float MIN_SIZE = 2f;
  302. if ((PCode)prim.Shape.PCode != PCode.Prim)
  303. return;
  304. if (prim.Scale.LengthSquared() < MIN_SIZE * MIN_SIZE)
  305. return;
  306. Primitive omvPrim = prim.Shape.ToOmvPrimitive(prim.OffsetPosition, prim.RotationOffset);
  307. FacetedMesh renderMesh = m_primMesher.GenerateFacetedMesh(omvPrim, DetailLevel.Medium);
  308. if (renderMesh == null)
  309. return;
  310. warp_Vector primPos = ConvertVector(prim.GetWorldPosition());
  311. warp_Quaternion primRot = ConvertQuaternion(prim.RotationOffset);
  312. warp_Matrix m = warp_Matrix.quaternionMatrix(primRot);
  313. if (prim.ParentID != 0)
  314. {
  315. SceneObjectGroup group = m_scene.SceneGraph.GetGroupByPrim(prim.LocalId);
  316. if (group != null)
  317. m.transform(warp_Matrix.quaternionMatrix(ConvertQuaternion(group.RootPart.RotationOffset)));
  318. }
  319. warp_Vector primScale = ConvertVector(prim.Scale);
  320. string primID = prim.UUID.ToString();
  321. // Create the prim faces
  322. // TODO: Implement the useTextures flag behavior
  323. for (int i = 0; i < renderMesh.Faces.Count; i++)
  324. {
  325. Face face = renderMesh.Faces[i];
  326. string meshName = primID + "-Face-" + i.ToString();
  327. // Avoid adding duplicate meshes to the scene
  328. if (renderer.Scene.objectData.ContainsKey(meshName))
  329. {
  330. continue;
  331. }
  332. warp_Object faceObj = new warp_Object(face.Vertices.Count, face.Indices.Count / 3);
  333. for (int j = 0; j < face.Vertices.Count; j++)
  334. {
  335. Vertex v = face.Vertices[j];
  336. warp_Vector pos = ConvertVector(v.Position);
  337. warp_Vector norm = ConvertVector(v.Normal);
  338. if (prim.Shape.SculptTexture == UUID.Zero)
  339. norm = norm.reverse();
  340. warp_Vertex vert = new warp_Vertex(pos, norm, v.TexCoord.X, v.TexCoord.Y);
  341. faceObj.addVertex(vert);
  342. }
  343. for (int j = 0; j < face.Indices.Count; j += 3)
  344. {
  345. faceObj.addTriangle(
  346. face.Indices[j + 0],
  347. face.Indices[j + 1],
  348. face.Indices[j + 2]);
  349. }
  350. Primitive.TextureEntryFace teFace = prim.Shape.Textures.GetFace((uint)i);
  351. Color4 faceColor = GetFaceColor(teFace);
  352. string materialName = GetOrCreateMaterial(renderer, faceColor);
  353. faceObj.transform(m);
  354. faceObj.setPos(primPos);
  355. faceObj.scaleSelf(primScale.x, primScale.y, primScale.z);
  356. renderer.Scene.addObject(meshName, faceObj);
  357. renderer.SetObjectMaterial(meshName, materialName);
  358. }
  359. }
  360. private Color4 GetFaceColor(Primitive.TextureEntryFace face)
  361. {
  362. Color4 color;
  363. if (face.TextureID == UUID.Zero)
  364. return face.RGBA;
  365. if (!m_colors.TryGetValue(face.TextureID, out color))
  366. {
  367. bool fetched = false;
  368. // Attempt to fetch the texture metadata
  369. UUID metadataID = UUID.Combine(face.TextureID, TEXTURE_METADATA_MAGIC);
  370. AssetBase metadata = m_scene.AssetService.GetCached(metadataID.ToString());
  371. if (metadata != null)
  372. {
  373. OSDMap map = null;
  374. try { map = OSDParser.Deserialize(metadata.Data) as OSDMap; } catch { }
  375. if (map != null)
  376. {
  377. color = map["X-JPEG2000-RGBA"].AsColor4();
  378. fetched = true;
  379. }
  380. }
  381. if (!fetched)
  382. {
  383. // Fetch the texture, decode and get the average color,
  384. // then save it to a temporary metadata asset
  385. AssetBase textureAsset = m_scene.AssetService.Get(face.TextureID.ToString());
  386. if (textureAsset != null)
  387. {
  388. int width, height;
  389. color = GetAverageColor(textureAsset.FullID, textureAsset.Data, out width, out height);
  390. OSDMap data = new OSDMap { { "X-JPEG2000-RGBA", OSD.FromColor4(color) } };
  391. metadata = new AssetBase
  392. {
  393. Data = System.Text.Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(data)),
  394. Description = "Metadata for JPEG2000 texture " + face.TextureID.ToString(),
  395. Flags = AssetFlags.Collectable,
  396. FullID = metadataID,
  397. ID = metadataID.ToString(),
  398. Local = true,
  399. Temporary = true,
  400. Name = String.Empty,
  401. Type = (sbyte)AssetType.Unknown
  402. };
  403. m_scene.AssetService.Store(metadata);
  404. }
  405. else
  406. {
  407. color = new Color4(0.5f, 0.5f, 0.5f, 1.0f);
  408. }
  409. }
  410. m_colors[face.TextureID] = color;
  411. }
  412. return color * face.RGBA;
  413. }
  414. private string GetOrCreateMaterial(WarpRenderer renderer, Color4 color)
  415. {
  416. string name = color.ToString();
  417. warp_Material material = renderer.Scene.material(name);
  418. if (material != null)
  419. return name;
  420. renderer.AddMaterial(name, ConvertColor(color));
  421. if (color.A < 1f)
  422. renderer.Scene.material(name).setTransparency((byte)((1f - color.A) * 255f));
  423. return name;
  424. }
  425. #endregion Rendering Methods
  426. #region Static Helpers
  427. private static warp_Vector ConvertVector(Vector3 vector)
  428. {
  429. return new warp_Vector(vector.X, vector.Z, vector.Y);
  430. }
  431. private static warp_Quaternion ConvertQuaternion(Quaternion quat)
  432. {
  433. return new warp_Quaternion(quat.X, quat.Z, quat.Y, -quat.W);
  434. }
  435. private static int ConvertColor(Color4 color)
  436. {
  437. int c = warp_Color.getColor((byte)(color.R * 255f), (byte)(color.G * 255f), (byte)(color.B * 255f));
  438. if (color.A < 1f)
  439. c |= (byte)(color.A * 255f) << 24;
  440. return c;
  441. }
  442. private static Vector3 SurfaceNormal(Vector3 c1, Vector3 c2, Vector3 c3)
  443. {
  444. Vector3 edge1 = new Vector3(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
  445. Vector3 edge2 = new Vector3(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
  446. Vector3 normal = Vector3.Cross(edge1, edge2);
  447. normal.Normalize();
  448. return normal;
  449. }
  450. public static Color4 GetAverageColor(UUID textureID, byte[] j2kData, out int width, out int height)
  451. {
  452. ulong r = 0;
  453. ulong g = 0;
  454. ulong b = 0;
  455. ulong a = 0;
  456. using (MemoryStream stream = new MemoryStream(j2kData))
  457. {
  458. try
  459. {
  460. int pixelBytes;
  461. using (Bitmap bitmap = (Bitmap)J2kImage.FromStream(stream))
  462. {
  463. width = bitmap.Width;
  464. height = bitmap.Height;
  465. BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  466. pixelBytes = (bitmap.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
  467. // Sum up the individual channels
  468. unsafe
  469. {
  470. if (pixelBytes == 4)
  471. {
  472. for (int y = 0; y < height; y++)
  473. {
  474. byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  475. for (int x = 0; x < width; x++)
  476. {
  477. b += row[x * pixelBytes + 0];
  478. g += row[x * pixelBytes + 1];
  479. r += row[x * pixelBytes + 2];
  480. a += row[x * pixelBytes + 3];
  481. }
  482. }
  483. }
  484. else
  485. {
  486. for (int y = 0; y < height; y++)
  487. {
  488. byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  489. for (int x = 0; x < width; x++)
  490. {
  491. b += row[x * pixelBytes + 0];
  492. g += row[x * pixelBytes + 1];
  493. r += row[x * pixelBytes + 2];
  494. }
  495. }
  496. }
  497. }
  498. }
  499. // Get the averages for each channel
  500. const decimal OO_255 = 1m / 255m;
  501. decimal totalPixels = (decimal)(width * height);
  502. decimal rm = ((decimal)r / totalPixels) * OO_255;
  503. decimal gm = ((decimal)g / totalPixels) * OO_255;
  504. decimal bm = ((decimal)b / totalPixels) * OO_255;
  505. decimal am = ((decimal)a / totalPixels) * OO_255;
  506. if (pixelBytes == 3)
  507. am = 1m;
  508. return new Color4((float)rm, (float)gm, (float)bm, (float)am);
  509. }
  510. catch (Exception ex)
  511. {
  512. m_log.WarnFormat(
  513. "[WARP 3D IMAGE MODULE]: Error decoding JPEG2000 texture {0} ({1} bytes): {2}",
  514. textureID, j2kData.Length, ex.Message);
  515. width = 0;
  516. height = 0;
  517. return new Color4(0.5f, 0.5f, 0.5f, 1.0f);
  518. }
  519. }
  520. }
  521. #endregion Static Helpers
  522. }
  523. public static class ImageUtils
  524. {
  525. /// <summary>
  526. /// Performs bilinear interpolation between four values
  527. /// </summary>
  528. /// <param name="v00">First, or top left value</param>
  529. /// <param name="v01">Second, or top right value</param>
  530. /// <param name="v10">Third, or bottom left value</param>
  531. /// <param name="v11">Fourth, or bottom right value</param>
  532. /// <param name="xPercent">Interpolation value on the X axis, between 0.0 and 1.0</param>
  533. /// <param name="yPercent">Interpolation value on fht Y axis, between 0.0 and 1.0</param>
  534. /// <returns>The bilinearly interpolated result</returns>
  535. public static float Bilinear(float v00, float v01, float v10, float v11, float xPercent, float yPercent)
  536. {
  537. return Utils.Lerp(Utils.Lerp(v00, v01, xPercent), Utils.Lerp(v10, v11, xPercent), yPercent);
  538. }
  539. /// <summary>
  540. /// Performs a high quality image resize
  541. /// </summary>
  542. /// <param name="image">Image to resize</param>
  543. /// <param name="width">New width</param>
  544. /// <param name="height">New height</param>
  545. /// <returns>Resized image</returns>
  546. public static Bitmap ResizeImage(Image image, int width, int height)
  547. {
  548. Bitmap result = new Bitmap(width, height);
  549. using (Graphics graphics = Graphics.FromImage(result))
  550. {
  551. graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  552. graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  553. graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  554. graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
  555. graphics.DrawImage(image, 0, 0, result.Width, result.Height);
  556. }
  557. return result;
  558. }
  559. }
  560. }