DynamicTextureModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 libsecondlife;
  32. using Nini.Config;
  33. using OpenJPEGNet;
  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<LLUUID, Scene> RegisteredScenes = new Dictionary<LLUUID, Scene>();
  42. private Dictionary<string, IDynamicTextureRender> RenderPlugins =
  43. new Dictionary<string, IDynamicTextureRender>();
  44. private Dictionary<LLUUID, DynamicTextureUpdater> Updaters = new Dictionary<LLUUID, 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. public void ReturnData(LLUUID id, byte[] data)
  54. {
  55. if (Updaters.ContainsKey(id))
  56. {
  57. DynamicTextureUpdater updater = Updaters[id];
  58. if (RegisteredScenes.ContainsKey(updater.SimUUID))
  59. {
  60. Scene scene = RegisteredScenes[updater.SimUUID];
  61. updater.DataReceived(data, scene);
  62. }
  63. }
  64. }
  65. public LLUUID AddDynamicTextureURL(LLUUID simID, LLUUID primID, string contentType, string url,
  66. string extraParams, int updateTimer)
  67. {
  68. return AddDynamicTextureURL(simID, primID, contentType, url, extraParams, updateTimer, false, 255);
  69. }
  70. public LLUUID AddDynamicTextureURL(LLUUID simID, LLUUID primID, string contentType, string url,
  71. string extraParams, int updateTimer, bool SetBlending, byte AlphaValue)
  72. {
  73. if (RenderPlugins.ContainsKey(contentType))
  74. {
  75. //Console.WriteLine("dynamic texture being created: " + url + " of type " + contentType);
  76. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  77. updater.SimUUID = simID;
  78. updater.PrimID = primID;
  79. updater.ContentType = contentType;
  80. updater.Url = url;
  81. updater.UpdateTimer = updateTimer;
  82. updater.UpdaterID = LLUUID.Random();
  83. updater.Params = extraParams;
  84. updater.BlendWithOldTexture = SetBlending;
  85. updater.FrontAlpha = AlphaValue;
  86. if (!Updaters.ContainsKey(updater.UpdaterID))
  87. {
  88. Updaters.Add(updater.UpdaterID, updater);
  89. }
  90. RenderPlugins[contentType].AsyncConvertUrl(updater.UpdaterID, url, extraParams);
  91. return updater.UpdaterID;
  92. }
  93. return LLUUID.Zero;
  94. }
  95. public LLUUID AddDynamicTextureData(LLUUID simID, LLUUID primID, string contentType, string data,
  96. string extraParams, int updateTimer)
  97. {
  98. return AddDynamicTextureData(simID, primID, contentType, data, extraParams, updateTimer, false, 255);
  99. }
  100. public LLUUID AddDynamicTextureData(LLUUID simID, LLUUID primID, string contentType, string data,
  101. string extraParams, int updateTimer, bool SetBlending, byte AlphaValue)
  102. {
  103. if (RenderPlugins.ContainsKey(contentType))
  104. {
  105. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  106. updater.SimUUID = simID;
  107. updater.PrimID = primID;
  108. updater.ContentType = contentType;
  109. updater.BodyData = data;
  110. updater.UpdateTimer = updateTimer;
  111. updater.UpdaterID = LLUUID.Random();
  112. updater.Params = extraParams;
  113. updater.BlendWithOldTexture = SetBlending;
  114. updater.FrontAlpha = AlphaValue;
  115. if (!Updaters.ContainsKey(updater.UpdaterID))
  116. {
  117. Updaters.Add(updater.UpdaterID, updater);
  118. }
  119. RenderPlugins[contentType].AsyncConvertData(updater.UpdaterID, data, extraParams);
  120. return updater.UpdaterID;
  121. }
  122. return LLUUID.Zero;
  123. }
  124. #endregion
  125. #region IRegionModule Members
  126. public void Initialise(Scene scene, IConfigSource config)
  127. {
  128. if (!RegisteredScenes.ContainsKey(scene.RegionInfo.RegionID))
  129. {
  130. RegisteredScenes.Add(scene.RegionInfo.RegionID, scene);
  131. scene.RegisterModuleInterface<IDynamicTextureManager>(this);
  132. }
  133. }
  134. public void PostInitialise()
  135. {
  136. }
  137. public void Close()
  138. {
  139. }
  140. public string Name
  141. {
  142. get { return "DynamicTextureModule"; }
  143. }
  144. public bool IsSharedModule
  145. {
  146. get { return true; }
  147. }
  148. #endregion
  149. #region Nested type: DynamicTextureUpdater
  150. public class DynamicTextureUpdater
  151. {
  152. public bool BlendWithOldTexture = false;
  153. public string BodyData;
  154. public string ContentType;
  155. public byte FrontAlpha = 255;
  156. public LLUUID LastAssetID;
  157. public string Params;
  158. public LLUUID PrimID;
  159. public bool SetNewFrontAlpha = false;
  160. public LLUUID SimUUID;
  161. public LLUUID UpdaterID;
  162. public int UpdateTimer;
  163. public string Url;
  164. public DynamicTextureUpdater()
  165. {
  166. LastAssetID = LLUUID.Zero;
  167. UpdateTimer = 0;
  168. BodyData = null;
  169. }
  170. public void DataReceived(byte[] data, Scene scene)
  171. {
  172. SceneObjectPart part = scene.GetSceneObjectPart(PrimID);
  173. byte[] assetData;
  174. AssetBase oldAsset = null;
  175. if (BlendWithOldTexture)
  176. {
  177. LLUUID lastTextureID = part.Shape.Textures.DefaultTexture.TextureID;
  178. oldAsset = scene.AssetCache.GetAsset(lastTextureID, true);
  179. if (oldAsset != null)
  180. {
  181. assetData = BlendTextures(data, oldAsset.Data, SetNewFrontAlpha, FrontAlpha);
  182. }
  183. else
  184. {
  185. assetData = new byte[data.Length];
  186. Array.Copy(data, assetData, data.Length);
  187. }
  188. }
  189. else
  190. {
  191. assetData = new byte[data.Length];
  192. Array.Copy(data, assetData, data.Length);
  193. }
  194. //TODO delete the last asset(data), if it was a dynamic texture
  195. AssetBase asset = new AssetBase();
  196. asset.FullID = LLUUID.Random();
  197. asset.Data = assetData;
  198. asset.Name = "DynamicImage" + Util.RandomClass.Next(1, 10000);
  199. asset.Type = 0;
  200. asset.Description = "dynamic image";
  201. asset.Local = false;
  202. asset.Temporary = true;
  203. scene.AssetCache.AddAsset(asset);
  204. LastAssetID = asset.FullID;
  205. part.Shape.Textures = new LLObject.TextureEntry(asset.FullID);
  206. part.ScheduleFullUpdate();
  207. }
  208. private byte[] BlendTextures(byte[] frontImage, byte[] backImage, bool setNewAlpha, byte newAlpha)
  209. {
  210. Bitmap image1 = new Bitmap(OpenJPEG.DecodeToImage(frontImage));
  211. Bitmap image2 = new Bitmap(OpenJPEG.DecodeToImage(backImage));
  212. if (setNewAlpha)
  213. {
  214. SetAlpha(ref image1, newAlpha);
  215. }
  216. Bitmap joint = MergeBitMaps(image1, image2);
  217. return OpenJPEG.EncodeFromImage(joint, true);
  218. }
  219. public Bitmap MergeBitMaps(Bitmap front, Bitmap back)
  220. {
  221. Bitmap joint;
  222. Graphics jG;
  223. joint = new Bitmap(back.Width, back.Height, PixelFormat.Format32bppArgb);
  224. jG = Graphics.FromImage(joint);
  225. jG.DrawImage(back, 0, 0, back.Width, back.Height);
  226. jG.DrawImage(front, 0, 0, back.Width, back.Height);
  227. return joint;
  228. }
  229. private void SetAlpha(ref Bitmap b, byte alpha)
  230. {
  231. for (int w = 0; w < b.Width; w++)
  232. {
  233. for (int h = 0; h < b.Height; h++)
  234. {
  235. b.SetPixel(w, h, Color.FromArgb(alpha, b.GetPixel(w, h)));
  236. }
  237. }
  238. }
  239. }
  240. #endregion
  241. }
  242. }