DynamicTextureModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 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 static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private const int ALL_SIDES = -1;
  45. public const int DISP_EXPIRE = 1;
  46. public const int DISP_TEMP = 2;
  47. private Dictionary<UUID, Scene> RegisteredScenes = new Dictionary<UUID, Scene>();
  48. private Dictionary<string, IDynamicTextureRender> RenderPlugins =
  49. new Dictionary<string, IDynamicTextureRender>();
  50. private Dictionary<UUID, DynamicTextureUpdater> Updaters = new Dictionary<UUID, DynamicTextureUpdater>();
  51. #region IDynamicTextureManager Members
  52. public void RegisterRender(string handleType, IDynamicTextureRender render)
  53. {
  54. if (!RenderPlugins.ContainsKey(handleType))
  55. {
  56. RenderPlugins.Add(handleType, render);
  57. }
  58. }
  59. /// <summary>
  60. /// Called by code which actually renders the dynamic texture to supply texture data.
  61. /// </summary>
  62. /// <param name="id"></param>
  63. /// <param name="data"></param>
  64. public void ReturnData(UUID id, byte[] data)
  65. {
  66. DynamicTextureUpdater updater = null;
  67. lock (Updaters)
  68. {
  69. if (Updaters.ContainsKey(id))
  70. {
  71. updater = Updaters[id];
  72. }
  73. }
  74. if (updater != null)
  75. {
  76. if (RegisteredScenes.ContainsKey(updater.SimUUID))
  77. {
  78. Scene scene = RegisteredScenes[updater.SimUUID];
  79. updater.DataReceived(data, scene);
  80. }
  81. }
  82. if (updater.UpdateTimer == 0)
  83. {
  84. lock (Updaters)
  85. {
  86. if (!Updaters.ContainsKey(updater.UpdaterID))
  87. {
  88. Updaters.Remove(updater.UpdaterID);
  89. }
  90. }
  91. }
  92. }
  93. public UUID AddDynamicTextureURL(UUID simID, UUID primID, string contentType, string url,
  94. string extraParams, int updateTimer)
  95. {
  96. return AddDynamicTextureURL(simID, primID, contentType, url, extraParams, updateTimer, false, 255);
  97. }
  98. public UUID AddDynamicTextureURL(UUID simID, UUID primID, string contentType, string url,
  99. string extraParams, int updateTimer, bool SetBlending, byte AlphaValue)
  100. {
  101. return AddDynamicTextureURL(simID, primID, contentType, url,
  102. extraParams, updateTimer, SetBlending,
  103. (int)(DISP_TEMP|DISP_EXPIRE), AlphaValue, ALL_SIDES);
  104. }
  105. public UUID AddDynamicTextureURL(UUID simID, UUID primID, string contentType, string url,
  106. string extraParams, int updateTimer, bool SetBlending,
  107. int disp, byte AlphaValue, int face)
  108. {
  109. if (RenderPlugins.ContainsKey(contentType))
  110. {
  111. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  112. updater.SimUUID = simID;
  113. updater.PrimID = primID;
  114. updater.ContentType = contentType;
  115. updater.Url = url;
  116. updater.UpdateTimer = updateTimer;
  117. updater.UpdaterID = UUID.Random();
  118. updater.Params = extraParams;
  119. updater.BlendWithOldTexture = SetBlending;
  120. updater.FrontAlpha = AlphaValue;
  121. updater.Face = face;
  122. updater.Disp = disp;
  123. lock (Updaters)
  124. {
  125. if (!Updaters.ContainsKey(updater.UpdaterID))
  126. {
  127. Updaters.Add(updater.UpdaterID, updater);
  128. }
  129. }
  130. RenderPlugins[contentType].AsyncConvertUrl(updater.UpdaterID, url, extraParams);
  131. return updater.UpdaterID;
  132. }
  133. return UUID.Zero;
  134. }
  135. public UUID AddDynamicTextureData(UUID simID, UUID primID, string contentType, string data,
  136. string extraParams, int updateTimer)
  137. {
  138. return AddDynamicTextureData(simID, primID, contentType, data, extraParams, updateTimer, false, 255);
  139. }
  140. public UUID AddDynamicTextureData(UUID simID, UUID primID, string contentType, string data,
  141. string extraParams, int updateTimer, bool SetBlending, byte AlphaValue)
  142. {
  143. return AddDynamicTextureData(simID, primID, contentType, data, extraParams, updateTimer, SetBlending,
  144. (int) (DISP_TEMP|DISP_EXPIRE), AlphaValue, ALL_SIDES);
  145. }
  146. public UUID AddDynamicTextureData(UUID simID, UUID primID, string contentType, string data,
  147. string extraParams, int updateTimer, bool SetBlending, int disp, byte AlphaValue, int face)
  148. {
  149. if (RenderPlugins.ContainsKey(contentType))
  150. {
  151. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  152. updater.SimUUID = simID;
  153. updater.PrimID = primID;
  154. updater.ContentType = contentType;
  155. updater.BodyData = data;
  156. updater.UpdateTimer = updateTimer;
  157. updater.UpdaterID = UUID.Random();
  158. updater.Params = extraParams;
  159. updater.BlendWithOldTexture = SetBlending;
  160. updater.FrontAlpha = AlphaValue;
  161. updater.Face = face;
  162. updater.Url = "Local image";
  163. updater.Disp = disp;
  164. lock (Updaters)
  165. {
  166. if (!Updaters.ContainsKey(updater.UpdaterID))
  167. {
  168. Updaters.Add(updater.UpdaterID, updater);
  169. }
  170. }
  171. RenderPlugins[contentType].AsyncConvertData(updater.UpdaterID, data, extraParams);
  172. return updater.UpdaterID;
  173. }
  174. return UUID.Zero;
  175. }
  176. public void GetDrawStringSize(string contentType, string text, string fontName, int fontSize,
  177. out double xSize, out double ySize)
  178. {
  179. xSize = 0;
  180. ySize = 0;
  181. if (RenderPlugins.ContainsKey(contentType))
  182. {
  183. RenderPlugins[contentType].GetDrawStringSize(text, fontName, fontSize, out xSize, out ySize);
  184. }
  185. }
  186. #endregion
  187. #region IRegionModule Members
  188. public void Initialise(Scene scene, IConfigSource config)
  189. {
  190. if (!RegisteredScenes.ContainsKey(scene.RegionInfo.RegionID))
  191. {
  192. RegisteredScenes.Add(scene.RegionInfo.RegionID, scene);
  193. scene.RegisterModuleInterface<IDynamicTextureManager>(this);
  194. }
  195. }
  196. public void PostInitialise()
  197. {
  198. }
  199. public void Close()
  200. {
  201. }
  202. public string Name
  203. {
  204. get { return "DynamicTextureModule"; }
  205. }
  206. public bool IsSharedModule
  207. {
  208. get { return true; }
  209. }
  210. #endregion
  211. #region Nested type: DynamicTextureUpdater
  212. public class DynamicTextureUpdater
  213. {
  214. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  215. public bool BlendWithOldTexture = false;
  216. public string BodyData;
  217. public string ContentType;
  218. public byte FrontAlpha = 255;
  219. public UUID LastAssetID;
  220. public string Params;
  221. public UUID PrimID;
  222. public bool SetNewFrontAlpha = false;
  223. public UUID SimUUID;
  224. public UUID UpdaterID;
  225. public int UpdateTimer;
  226. public int Face;
  227. public int Disp;
  228. public string Url;
  229. public DynamicTextureUpdater()
  230. {
  231. LastAssetID = UUID.Zero;
  232. UpdateTimer = 0;
  233. BodyData = null;
  234. }
  235. /// <summary>
  236. /// Called once new texture data has been received for this updater.
  237. /// </summary>
  238. public void DataReceived(byte[] data, Scene scene)
  239. {
  240. SceneObjectPart part = scene.GetSceneObjectPart(PrimID);
  241. if (part == null || data == null || data.Length <= 1)
  242. {
  243. string msg =
  244. String.Format("DynamicTextureModule: Error preparing image using URL {0}", Url);
  245. scene.SimChat(Utils.StringToBytes(msg), ChatTypeEnum.Say,
  246. 0, part.ParentGroup.RootPart.AbsolutePosition, part.Name, part.UUID, false);
  247. return;
  248. }
  249. byte[] assetData;
  250. AssetBase oldAsset = null;
  251. if (BlendWithOldTexture)
  252. {
  253. UUID lastTextureID = part.Shape.Textures.DefaultTexture.TextureID;
  254. oldAsset = scene.AssetService.Get(lastTextureID.ToString());
  255. if (oldAsset != null)
  256. {
  257. assetData = BlendTextures(data, oldAsset.Data, SetNewFrontAlpha, FrontAlpha);
  258. }
  259. else
  260. {
  261. assetData = new byte[data.Length];
  262. Array.Copy(data, assetData, data.Length);
  263. }
  264. }
  265. else
  266. {
  267. assetData = new byte[data.Length];
  268. Array.Copy(data, assetData, data.Length);
  269. }
  270. // Create a new asset for user
  271. AssetBase asset = new AssetBase();
  272. asset.FullID = UUID.Random();
  273. asset.Data = assetData;
  274. asset.Name = "DynamicImage" + Util.RandomClass.Next(1, 10000);
  275. asset.Type = 0;
  276. asset.Description = String.Format("URL image : {0}", Url);
  277. asset.Local = false;
  278. asset.Temporary = ((Disp & DISP_TEMP) != 0);
  279. scene.AssetService.Store(asset);
  280. // scene.CommsManager.AssetCache.AddAsset(asset);
  281. IJ2KDecoder cacheLayerDecode = scene.RequestModuleInterface<IJ2KDecoder>();
  282. if (cacheLayerDecode != null)
  283. {
  284. cacheLayerDecode.syncdecode(asset.FullID, asset.Data);
  285. cacheLayerDecode = null;
  286. LastAssetID = asset.FullID;
  287. }
  288. UUID oldID = UUID.Zero;
  289. lock (part)
  290. {
  291. // mostly keep the values from before
  292. Primitive.TextureEntry tmptex = part.Shape.Textures;
  293. // remove the old asset from the cache
  294. oldID = tmptex.DefaultTexture.TextureID;
  295. if (Face == ALL_SIDES)
  296. {
  297. tmptex.DefaultTexture.TextureID = asset.FullID;
  298. }
  299. else
  300. {
  301. try
  302. {
  303. Primitive.TextureEntryFace texface = tmptex.CreateFace((uint)Face);
  304. texface.TextureID = asset.FullID;
  305. tmptex.FaceTextures[Face] = texface;
  306. }
  307. catch (Exception)
  308. {
  309. tmptex.DefaultTexture.TextureID = asset.FullID;
  310. }
  311. }
  312. // I'm pretty sure we always want to force this to true
  313. // I'm pretty sure noone whats to set fullbright true if it wasn't true before.
  314. // tmptex.DefaultTexture.Fullbright = true;
  315. part.UpdateTexture(tmptex);
  316. }
  317. if (oldID != UUID.Zero && ((Disp & DISP_EXPIRE) != 0))
  318. {
  319. // scene.CommsManager.AssetCache.ExpireAsset(oldID);
  320. scene.AssetService.Delete(oldID.ToString());
  321. }
  322. }
  323. private byte[] BlendTextures(byte[] frontImage, byte[] backImage, bool setNewAlpha, byte newAlpha)
  324. {
  325. ManagedImage managedImage;
  326. Image image;
  327. if (OpenJPEG.DecodeToImage(frontImage, out managedImage, out image))
  328. {
  329. Bitmap image1 = new Bitmap(image);
  330. if (OpenJPEG.DecodeToImage(backImage, out managedImage, out image))
  331. {
  332. Bitmap image2 = new Bitmap(image);
  333. if (setNewAlpha)
  334. SetAlpha(ref image1, newAlpha);
  335. Bitmap joint = MergeBitMaps(image1, image2);
  336. byte[] result = new byte[0];
  337. try
  338. {
  339. result = OpenJPEG.EncodeFromImage(joint, true);
  340. }
  341. catch (Exception)
  342. {
  343. m_log.Error("[DYNAMICTEXTUREMODULE]: OpenJpeg Encode Failed. Empty byte data returned!");
  344. }
  345. return result;
  346. }
  347. }
  348. return null;
  349. }
  350. public Bitmap MergeBitMaps(Bitmap front, Bitmap back)
  351. {
  352. Bitmap joint;
  353. Graphics jG;
  354. joint = new Bitmap(back.Width, back.Height, PixelFormat.Format32bppArgb);
  355. jG = Graphics.FromImage(joint);
  356. jG.DrawImage(back, 0, 0, back.Width, back.Height);
  357. jG.DrawImage(front, 0, 0, back.Width, back.Height);
  358. return joint;
  359. }
  360. private void SetAlpha(ref Bitmap b, byte alpha)
  361. {
  362. for (int w = 0; w < b.Width; w++)
  363. {
  364. for (int h = 0; h < b.Height; h++)
  365. {
  366. b.SetPixel(w, h, Color.FromArgb(alpha, b.GetPixel(w, h)));
  367. }
  368. }
  369. }
  370. }
  371. #endregion
  372. }
  373. }