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