DynamicTextureModule.cs 12 KB

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