DynamicTextureModule.cs 11 KB

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