diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/Scripting/LoadImageURL/LoadImageURLModule.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/Scripting/LoadImageURL/LoadImageURLModule.cs | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Scripting/LoadImageURL/LoadImageURLModule.cs b/OpenSim/Region/CoreModules/Scripting/LoadImageURL/LoadImageURLModule.cs new file mode 100644 index 0000000..afcaff1 --- /dev/null +++ b/OpenSim/Region/CoreModules/Scripting/LoadImageURL/LoadImageURLModule.cs | |||
@@ -0,0 +1,229 @@ | |||
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.Drawing; | ||
30 | using System.IO; | ||
31 | using System.Net; | ||
32 | using OpenMetaverse; | ||
33 | using OpenMetaverse.Imaging; | ||
34 | using Nini.Config; | ||
35 | using OpenSim.Region.Framework.Interfaces; | ||
36 | using OpenSim.Region.Framework.Scenes; | ||
37 | |||
38 | namespace OpenSim.Region.CoreModules.Scripting.LoadImageURL | ||
39 | { | ||
40 | public class LoadImageURLModule : IRegionModule, IDynamicTextureRender | ||
41 | { | ||
42 | private string m_name = "LoadImageURL"; | ||
43 | private Scene m_scene; | ||
44 | private IDynamicTextureManager m_textureManager; | ||
45 | |||
46 | private string m_proxyurl = ""; | ||
47 | private string m_proxyexcepts = ""; | ||
48 | |||
49 | #region IDynamicTextureRender Members | ||
50 | |||
51 | public string GetName() | ||
52 | { | ||
53 | return m_name; | ||
54 | } | ||
55 | |||
56 | public string GetContentType() | ||
57 | { | ||
58 | return ("image"); | ||
59 | } | ||
60 | |||
61 | public bool SupportsAsynchronous() | ||
62 | { | ||
63 | return true; | ||
64 | } | ||
65 | |||
66 | public byte[] ConvertUrl(string url, string extraParams) | ||
67 | { | ||
68 | return null; | ||
69 | } | ||
70 | |||
71 | public byte[] ConvertStream(Stream data, string extraParams) | ||
72 | { | ||
73 | return null; | ||
74 | } | ||
75 | |||
76 | public bool AsyncConvertUrl(UUID id, string url, string extraParams) | ||
77 | { | ||
78 | MakeHttpRequest(url, id); | ||
79 | return true; | ||
80 | } | ||
81 | |||
82 | public bool AsyncConvertData(UUID id, string bodyData, string extraParams) | ||
83 | { | ||
84 | return false; | ||
85 | } | ||
86 | |||
87 | #endregion | ||
88 | |||
89 | #region IRegionModule Members | ||
90 | |||
91 | public void Initialise(Scene scene, IConfigSource config) | ||
92 | { | ||
93 | if (m_scene == null) | ||
94 | { | ||
95 | m_scene = scene; | ||
96 | } | ||
97 | |||
98 | m_proxyurl = config.Configs["Startup"].GetString("HttpProxy"); | ||
99 | m_proxyexcepts = config.Configs["Startup"].GetString("HttpProxyExceptions"); | ||
100 | } | ||
101 | |||
102 | public void PostInitialise() | ||
103 | { | ||
104 | m_textureManager = m_scene.RequestModuleInterface<IDynamicTextureManager>(); | ||
105 | if (m_textureManager != null) | ||
106 | { | ||
107 | m_textureManager.RegisterRender(GetContentType(), this); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | public void Close() | ||
112 | { | ||
113 | } | ||
114 | |||
115 | public string Name | ||
116 | { | ||
117 | get { return m_name; } | ||
118 | } | ||
119 | |||
120 | public bool IsSharedModule | ||
121 | { | ||
122 | get { return true; } | ||
123 | } | ||
124 | |||
125 | #endregion | ||
126 | |||
127 | private void MakeHttpRequest(string url, UUID requestID) | ||
128 | { | ||
129 | WebRequest request = HttpWebRequest.Create(url); | ||
130 | |||
131 | if (m_proxyurl != null && m_proxyurl.Length > 0) | ||
132 | { | ||
133 | if (m_proxyexcepts != null && m_proxyexcepts.Length > 0) | ||
134 | { | ||
135 | string[] elist = m_proxyexcepts.Split(';'); | ||
136 | request.Proxy = new WebProxy(m_proxyurl, true, elist); | ||
137 | } | ||
138 | else | ||
139 | { | ||
140 | request.Proxy = new WebProxy(m_proxyurl, true); | ||
141 | } | ||
142 | } | ||
143 | |||
144 | RequestState state = new RequestState((HttpWebRequest) request, requestID); | ||
145 | // IAsyncResult result = request.BeginGetResponse(new AsyncCallback(HttpRequestReturn), state); | ||
146 | request.BeginGetResponse(new AsyncCallback(HttpRequestReturn), state); | ||
147 | |||
148 | TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1)); | ||
149 | state.TimeOfRequest = (int) t.TotalSeconds; | ||
150 | } | ||
151 | |||
152 | private void HttpRequestReturn(IAsyncResult result) | ||
153 | { | ||
154 | RequestState state = (RequestState) result.AsyncState; | ||
155 | WebRequest request = (WebRequest) state.Request; | ||
156 | try | ||
157 | { | ||
158 | HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); | ||
159 | if (response.StatusCode == HttpStatusCode.OK) | ||
160 | { | ||
161 | Bitmap image = new Bitmap(response.GetResponseStream()); | ||
162 | Size newsize; | ||
163 | |||
164 | // TODO: make this a bit less hard coded | ||
165 | if ((image.Height < 64) && (image.Width < 64)) | ||
166 | { | ||
167 | newsize = new Size(32, 32); | ||
168 | } | ||
169 | else if ((image.Height < 128) && (image.Width < 128)) | ||
170 | { | ||
171 | newsize = new Size(64, 64); | ||
172 | } | ||
173 | else if ((image.Height < 256) && (image.Width < 256)) | ||
174 | { | ||
175 | newsize = new Size(128, 128); | ||
176 | } | ||
177 | else if ((image.Height < 512 && image.Width < 512)) | ||
178 | { | ||
179 | newsize = new Size(256, 256); | ||
180 | } | ||
181 | else if ((image.Height < 1024 && image.Width < 1024)) | ||
182 | { | ||
183 | newsize = new Size(512, 512); | ||
184 | } | ||
185 | else | ||
186 | { | ||
187 | newsize = new Size(1024, 1024); | ||
188 | } | ||
189 | |||
190 | Bitmap resize = new Bitmap(image, newsize); | ||
191 | byte[] imageJ2000 = new byte[0]; | ||
192 | |||
193 | try | ||
194 | { | ||
195 | imageJ2000 = OpenJPEG.EncodeFromImage(resize, true); | ||
196 | } | ||
197 | catch (Exception) | ||
198 | { | ||
199 | Console.WriteLine( | ||
200 | "[LOADIMAGEURLMODULE]: OpenJpeg Encode Failed. Empty byte data returned!"); | ||
201 | } | ||
202 | |||
203 | m_textureManager.ReturnData(state.RequestID, imageJ2000); | ||
204 | } | ||
205 | } | ||
206 | catch (WebException) | ||
207 | { | ||
208 | |||
209 | } | ||
210 | } | ||
211 | |||
212 | #region Nested type: RequestState | ||
213 | |||
214 | public class RequestState | ||
215 | { | ||
216 | public HttpWebRequest Request = null; | ||
217 | public UUID RequestID = UUID.Zero; | ||
218 | public int TimeOfRequest = 0; | ||
219 | |||
220 | public RequestState(HttpWebRequest request, UUID requestID) | ||
221 | { | ||
222 | Request = request; | ||
223 | RequestID = requestID; | ||
224 | } | ||
225 | } | ||
226 | |||
227 | #endregion | ||
228 | } | ||
229 | } | ||