DynamicTextureModule.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 OpenSim 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 Nini.Config;
  32. using OpenMetaverse;
  33. using OpenMetaverse.Imaging;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using log4net;
  38. using System.Reflection;
  39. namespace OpenSim.Region.CoreModules.Scripting.DynamicTexture
  40. {
  41. public class DynamicTextureModule : IRegionModule, IDynamicTextureManager
  42. {
  43. private Dictionary<UUID, Scene> RegisteredScenes = new Dictionary<UUID, Scene>();
  44. private Dictionary<string, IDynamicTextureRender> RenderPlugins =
  45. new Dictionary<string, IDynamicTextureRender>();
  46. private Dictionary<UUID, DynamicTextureUpdater> Updaters = new Dictionary<UUID, DynamicTextureUpdater>();
  47. #region IDynamicTextureManager Members
  48. public void RegisterRender(string handleType, IDynamicTextureRender render)
  49. {
  50. if (!RenderPlugins.ContainsKey(handleType))
  51. {
  52. RenderPlugins.Add(handleType, render);
  53. }
  54. }
  55. /// <summary>
  56. /// Called by code which actually renders the dynamic texture to supply texture data.
  57. /// </summary>
  58. /// <param name="id"></param>
  59. /// <param name="data"></param>
  60. public void ReturnData(UUID id, byte[] data)
  61. {
  62. if (Updaters.ContainsKey(id))
  63. {
  64. DynamicTextureUpdater updater = Updaters[id];
  65. if (RegisteredScenes.ContainsKey(updater.SimUUID))
  66. {
  67. Scene scene = RegisteredScenes[updater.SimUUID];
  68. updater.DataReceived(data, scene);
  69. }
  70. }
  71. }
  72. public UUID AddDynamicTextureURL(UUID simID, UUID primID, string contentType, string url,
  73. string extraParams, int updateTimer)
  74. {
  75. return AddDynamicTextureURL(simID, primID, contentType, url, extraParams, updateTimer, false, 255);
  76. }
  77. public UUID AddDynamicTextureURL(UUID simID, UUID primID, string contentType, string url,
  78. string extraParams, int updateTimer, bool SetBlending, byte AlphaValue)
  79. {
  80. if (RenderPlugins.ContainsKey(contentType))
  81. {
  82. //m_log.Debug("dynamic texture being created: " + url + " of type " + contentType);
  83. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  84. updater.SimUUID = simID;
  85. updater.PrimID = primID;
  86. updater.ContentType = contentType;
  87. updater.Url = url;
  88. updater.UpdateTimer = updateTimer;
  89. updater.UpdaterID = UUID.Random();
  90. updater.Params = extraParams;
  91. updater.BlendWithOldTexture = SetBlending;
  92. updater.FrontAlpha = AlphaValue;
  93. if (!Updaters.ContainsKey(updater.UpdaterID))
  94. {
  95. Updaters.Add(updater.UpdaterID, updater);
  96. }
  97. RenderPlugins[contentType].AsyncConvertUrl(updater.UpdaterID, url, extraParams);
  98. return updater.UpdaterID;
  99. }
  100. return UUID.Zero;
  101. }
  102. public UUID AddDynamicTextureData(UUID simID, UUID primID, string contentType, string data,
  103. string extraParams, int updateTimer)
  104. {
  105. return AddDynamicTextureData(simID, primID, contentType, data, extraParams, updateTimer, false, 255);
  106. }
  107. public UUID AddDynamicTextureData(UUID simID, UUID primID, string contentType, string data,
  108. string extraParams, int updateTimer, bool SetBlending, byte AlphaValue)
  109. {
  110. if (RenderPlugins.ContainsKey(contentType))
  111. {
  112. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  113. updater.SimUUID = simID;
  114. updater.PrimID = primID;
  115. updater.ContentType = contentType;
  116. updater.BodyData = data;
  117. updater.UpdateTimer = updateTimer;
  118. updater.UpdaterID = UUID.Random();
  119. updater.Params = extraParams;
  120. updater.BlendWithOldTexture = SetBlending;
  121. updater.FrontAlpha = AlphaValue;
  122. if (!Updaters.ContainsKey(updater.UpdaterID))
  123. {
  124. Updaters.Add(updater.UpdaterID, updater);
  125. }
  126. RenderPlugins[contentType].AsyncConvertData(updater.UpdaterID, data, extraParams);
  127. return updater.UpdaterID;
  128. }
  129. return UUID.Zero;
  130. }
  131. public void GetDrawStringSize(string contentType, string text, string fontName, int fontSize,
  132. out double xSize, out double ySize)
  133. {
  134. xSize = 0;
  135. ySize = 0;
  136. if (RenderPlugins.ContainsKey(contentType))
  137. {
  138. RenderPlugins[contentType].GetDrawStringSize(text, fontName, fontSize, out xSize, out ySize);
  139. }
  140. }
  141. #endregion
  142. #region IRegionModule Members
  143. public void Initialise(Scene scene, IConfigSource config)
  144. {
  145. if (!RegisteredScenes.ContainsKey(scene.RegionInfo.RegionID))
  146. {
  147. RegisteredScenes.Add(scene.RegionInfo.RegionID, scene);
  148. scene.RegisterModuleInterface<IDynamicTextureManager>(this);
  149. }
  150. }
  151. public void PostInitialise()
  152. {
  153. }
  154. public void Close()
  155. {
  156. }
  157. public string Name
  158. {
  159. get { return "DynamicTextureModule"; }
  160. }
  161. public bool IsSharedModule
  162. {
  163. get { return true; }
  164. }
  165. #endregion
  166. #region Nested type: DynamicTextureUpdater
  167. public class DynamicTextureUpdater
  168. {
  169. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  170. public bool BlendWithOldTexture = false;
  171. public string BodyData;
  172. public string ContentType;
  173. public byte FrontAlpha = 255;
  174. public UUID LastAssetID;
  175. public string Params;
  176. public UUID PrimID;
  177. public bool SetNewFrontAlpha = false;
  178. public UUID SimUUID;
  179. public UUID UpdaterID;
  180. public int UpdateTimer;
  181. public string Url;
  182. public DynamicTextureUpdater()
  183. {
  184. LastAssetID = UUID.Zero;
  185. UpdateTimer = 0;
  186. BodyData = null;
  187. }
  188. /// <summary>
  189. /// Called once new texture data has been received for this updater.
  190. /// </summary>
  191. public void DataReceived(byte[] data, Scene scene)
  192. {
  193. SceneObjectPart part = scene.GetSceneObjectPart(PrimID);
  194. byte[] assetData;
  195. AssetBase oldAsset = null;
  196. if (BlendWithOldTexture)
  197. {
  198. UUID lastTextureID = part.Shape.Textures.DefaultTexture.TextureID;
  199. oldAsset = scene.CommsManager.AssetCache.GetAsset(lastTextureID, true);
  200. if (oldAsset != null)
  201. {
  202. assetData = BlendTextures(data, oldAsset.Data, SetNewFrontAlpha, FrontAlpha);
  203. }
  204. else
  205. {
  206. assetData = new byte[data.Length];
  207. Array.Copy(data, assetData, data.Length);
  208. }
  209. }
  210. else
  211. {
  212. assetData = new byte[data.Length];
  213. Array.Copy(data, assetData, data.Length);
  214. }
  215. // Create a new asset for user
  216. AssetBase asset = new AssetBase();
  217. asset.FullID = UUID.Random();
  218. asset.Data = assetData;
  219. asset.Name = "DynamicImage" + Util.RandomClass.Next(1, 10000);
  220. asset.Type = 0;
  221. asset.Description = "dynamic image";
  222. asset.Local = false;
  223. asset.Temporary = true;
  224. scene.CommsManager.AssetCache.AddAsset(asset);
  225. LastAssetID = asset.FullID;
  226. IJ2KDecoder cacheLayerDecode = scene.RequestModuleInterface<IJ2KDecoder>();
  227. if (cacheLayerDecode != null)
  228. {
  229. cacheLayerDecode.syncdecode(asset.FullID, asset.Data);
  230. }
  231. cacheLayerDecode = null;
  232. // mostly keep the values from before
  233. Primitive.TextureEntry tmptex = part.Shape.Textures;
  234. // remove the old asset from the cache
  235. UUID oldID = tmptex.DefaultTexture.TextureID;
  236. scene.CommsManager.AssetCache.ExpireAsset(oldID);
  237. tmptex.DefaultTexture.TextureID = asset.FullID;
  238. // I'm pretty sure we always want to force this to true
  239. tmptex.DefaultTexture.Fullbright = true;
  240. part.Shape.Textures = tmptex;
  241. part.ScheduleFullUpdate();
  242. }
  243. private byte[] BlendTextures(byte[] frontImage, byte[] backImage, bool setNewAlpha, byte newAlpha)
  244. {
  245. ManagedImage managedImage;
  246. Image image;
  247. if (OpenJPEG.DecodeToImage(frontImage, out managedImage, out image))
  248. {
  249. Bitmap image1 = new Bitmap(image);
  250. if (OpenJPEG.DecodeToImage(backImage, out managedImage, out image))
  251. {
  252. Bitmap image2 = new Bitmap(image);
  253. if (setNewAlpha)
  254. SetAlpha(ref image1, newAlpha);
  255. Bitmap joint = MergeBitMaps(image1, image2);
  256. byte[] result = new byte[0];
  257. try
  258. {
  259. result = OpenJPEG.EncodeFromImage(joint, true);
  260. }
  261. catch (Exception)
  262. {
  263. m_log.Error("[DYNAMICTEXTUREMODULE]: OpenJpeg Encode Failed. Empty byte data returned!");
  264. }
  265. return result;
  266. }
  267. }
  268. return null;
  269. }
  270. public Bitmap MergeBitMaps(Bitmap front, Bitmap back)
  271. {
  272. Bitmap joint;
  273. Graphics jG;
  274. joint = new Bitmap(back.Width, back.Height, PixelFormat.Format32bppArgb);
  275. jG = Graphics.FromImage(joint);
  276. jG.DrawImage(back, 0, 0, back.Width, back.Height);
  277. jG.DrawImage(front, 0, 0, back.Width, back.Height);
  278. return joint;
  279. }
  280. private void SetAlpha(ref Bitmap b, byte alpha)
  281. {
  282. for (int w = 0; w < b.Width; w++)
  283. {
  284. for (int h = 0; h < b.Height; h++)
  285. {
  286. b.SetPixel(w, h, Color.FromArgb(alpha, b.GetPixel(w, h)));
  287. }
  288. }
  289. }
  290. }
  291. #endregion
  292. }
  293. }