DynamicTextureModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 string Params;
  220. public UUID PrimID;
  221. public bool SetNewFrontAlpha = false;
  222. public UUID SimUUID;
  223. public UUID UpdaterID;
  224. public int UpdateTimer;
  225. public int Face;
  226. public int Disp;
  227. public string Url;
  228. public DynamicTextureUpdater()
  229. {
  230. UpdateTimer = 0;
  231. BodyData = null;
  232. }
  233. /// <summary>
  234. /// Called once new texture data has been received for this updater.
  235. /// </summary>
  236. public void DataReceived(byte[] data, Scene scene)
  237. {
  238. SceneObjectPart part = scene.GetSceneObjectPart(PrimID);
  239. if (part == null || data == null || data.Length <= 1)
  240. {
  241. string msg =
  242. String.Format("DynamicTextureModule: Error preparing image using URL {0}", Url);
  243. scene.SimChat(Utils.StringToBytes(msg), ChatTypeEnum.Say,
  244. 0, part.ParentGroup.RootPart.AbsolutePosition, part.Name, part.UUID, false);
  245. return;
  246. }
  247. byte[] assetData = null;
  248. AssetBase oldAsset = null;
  249. if (BlendWithOldTexture)
  250. {
  251. Primitive.TextureEntryFace defaultFace = part.Shape.Textures.DefaultTexture;
  252. if (defaultFace != null)
  253. {
  254. oldAsset = scene.AssetService.Get(defaultFace.TextureID.ToString());
  255. if (oldAsset != null)
  256. assetData = BlendTextures(data, oldAsset.Data, SetNewFrontAlpha, FrontAlpha);
  257. }
  258. }
  259. if (assetData == null)
  260. {
  261. assetData = new byte[data.Length];
  262. Array.Copy(data, assetData, data.Length);
  263. }
  264. // Create a new asset for user
  265. AssetBase asset
  266. = new AssetBase(
  267. UUID.Random(), "DynamicImage" + Util.RandomClass.Next(1, 10000), (sbyte)AssetType.Texture,
  268. scene.RegionInfo.RegionID.ToString());
  269. asset.Data = assetData;
  270. asset.Description = String.Format("URL image : {0}", Url);
  271. asset.Local = false;
  272. asset.Temporary = ((Disp & DISP_TEMP) != 0);
  273. scene.AssetService.Store(asset);
  274. IJ2KDecoder cacheLayerDecode = scene.RequestModuleInterface<IJ2KDecoder>();
  275. if (cacheLayerDecode != null)
  276. {
  277. cacheLayerDecode.Decode(asset.FullID, asset.Data);
  278. cacheLayerDecode = null;
  279. }
  280. UUID oldID = UUID.Zero;
  281. lock (part)
  282. {
  283. // mostly keep the values from before
  284. Primitive.TextureEntry tmptex = part.Shape.Textures;
  285. // remove the old asset from the cache
  286. oldID = tmptex.DefaultTexture.TextureID;
  287. if (Face == ALL_SIDES)
  288. {
  289. tmptex.DefaultTexture.TextureID = asset.FullID;
  290. }
  291. else
  292. {
  293. try
  294. {
  295. Primitive.TextureEntryFace texface = tmptex.CreateFace((uint)Face);
  296. texface.TextureID = asset.FullID;
  297. tmptex.FaceTextures[Face] = texface;
  298. }
  299. catch (Exception)
  300. {
  301. tmptex.DefaultTexture.TextureID = asset.FullID;
  302. }
  303. }
  304. // I'm pretty sure we always want to force this to true
  305. // I'm pretty sure noone whats to set fullbright true if it wasn't true before.
  306. // tmptex.DefaultTexture.Fullbright = true;
  307. part.UpdateTextureEntry(tmptex.GetBytes());
  308. }
  309. if (oldID != UUID.Zero && ((Disp & DISP_EXPIRE) != 0))
  310. {
  311. if (oldAsset == null) oldAsset = scene.AssetService.Get(oldID.ToString());
  312. if (oldAsset != null)
  313. {
  314. if (oldAsset.Temporary == true)
  315. {
  316. scene.AssetService.Delete(oldID.ToString());
  317. }
  318. }
  319. }
  320. }
  321. private byte[] BlendTextures(byte[] frontImage, byte[] backImage, bool setNewAlpha, byte newAlpha)
  322. {
  323. ManagedImage managedImage;
  324. Image image;
  325. if (OpenJPEG.DecodeToImage(frontImage, out managedImage, out image))
  326. {
  327. Bitmap image1 = new Bitmap(image);
  328. if (OpenJPEG.DecodeToImage(backImage, out managedImage, out image))
  329. {
  330. Bitmap image2 = new Bitmap(image);
  331. if (setNewAlpha)
  332. SetAlpha(ref image1, newAlpha);
  333. Bitmap joint = MergeBitMaps(image1, image2);
  334. byte[] result = new byte[0];
  335. try
  336. {
  337. result = OpenJPEG.EncodeFromImage(joint, true);
  338. }
  339. catch (Exception e)
  340. {
  341. m_log.ErrorFormat(
  342. "[DYNAMICTEXTUREMODULE]: OpenJpeg Encode Failed. Exception {0}{1}",
  343. e.Message, e.StackTrace);
  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. }