diff options
Diffstat (limited to 'OpenSim/Region/Environment/Modules/Scripting/LoadImageURL/LoadImageURLModule.cs')
-rw-r--r-- | OpenSim/Region/Environment/Modules/Scripting/LoadImageURL/LoadImageURLModule.cs | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/Scripting/LoadImageURL/LoadImageURLModule.cs b/OpenSim/Region/Environment/Modules/Scripting/LoadImageURL/LoadImageURLModule.cs new file mode 100644 index 0000000..eaf9d36 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/Scripting/LoadImageURL/LoadImageURLModule.cs | |||
@@ -0,0 +1,179 @@ | |||
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 libsecondlife; | ||
33 | using Nini.Config; | ||
34 | using OpenJPEGNet; | ||
35 | using OpenSim.Region.Environment.Interfaces; | ||
36 | using OpenSim.Region.Environment.Scenes; | ||
37 | |||
38 | namespace OpenSim.Region.Environment.Modules.Scripting.LoadImageURL | ||
39 | { | ||
40 | public class LoadImageURLModule : IRegionModule, IDynamicTextureRender | ||
41 | { | ||
42 | private string m_name = "LoadImageURL"; | ||
43 | private IDynamicTextureManager m_textureManager; | ||
44 | private Scene m_scene; | ||
45 | |||
46 | public void Initialise(Scene scene, IConfigSource config) | ||
47 | { | ||
48 | if (m_scene == null) | ||
49 | { | ||
50 | m_scene = scene; | ||
51 | } | ||
52 | } | ||
53 | |||
54 | public void PostInitialise() | ||
55 | { | ||
56 | m_textureManager = m_scene.RequestModuleInterface<IDynamicTextureManager>(); | ||
57 | if (m_textureManager != null) | ||
58 | { | ||
59 | m_textureManager.RegisterRender(GetContentType(), this); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | public void Close() | ||
64 | { | ||
65 | } | ||
66 | |||
67 | public string Name | ||
68 | { | ||
69 | get { return m_name; } | ||
70 | } | ||
71 | |||
72 | public bool IsSharedModule | ||
73 | { | ||
74 | get { return true; } | ||
75 | } | ||
76 | |||
77 | public string GetName() | ||
78 | { | ||
79 | return m_name; | ||
80 | } | ||
81 | |||
82 | public string GetContentType() | ||
83 | { | ||
84 | return ("image"); | ||
85 | } | ||
86 | |||
87 | public bool SupportsAsynchronous() | ||
88 | { | ||
89 | return true; | ||
90 | } | ||
91 | |||
92 | public byte[] ConvertUrl(string url, string extraParams) | ||
93 | { | ||
94 | return null; | ||
95 | } | ||
96 | |||
97 | public byte[] ConvertStream(Stream data, string extraParams) | ||
98 | { | ||
99 | return null; | ||
100 | } | ||
101 | |||
102 | public bool AsyncConvertUrl(LLUUID id, string url, string extraParams) | ||
103 | { | ||
104 | MakeHttpRequest(url, id); | ||
105 | return true; | ||
106 | } | ||
107 | |||
108 | public bool AsyncConvertData(LLUUID id, string bodyData, string extraParams) | ||
109 | { | ||
110 | return false; | ||
111 | } | ||
112 | |||
113 | private void MakeHttpRequest(string url, LLUUID requestID) | ||
114 | { | ||
115 | WebRequest request = HttpWebRequest.Create(url); | ||
116 | RequestState state = new RequestState((HttpWebRequest) request, requestID); | ||
117 | IAsyncResult result = request.BeginGetResponse(new AsyncCallback(HttpRequestReturn), state); | ||
118 | |||
119 | TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1)); | ||
120 | state.TimeOfRequest = (int) t.TotalSeconds; | ||
121 | } | ||
122 | |||
123 | private void HttpRequestReturn(IAsyncResult result) | ||
124 | { | ||
125 | RequestState state = (RequestState) result.AsyncState; | ||
126 | WebRequest request = (WebRequest) state.Request; | ||
127 | HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(result); | ||
128 | if (response.StatusCode == HttpStatusCode.OK) | ||
129 | { | ||
130 | Bitmap image = new Bitmap(response.GetResponseStream()); | ||
131 | Size newsize; | ||
132 | |||
133 | // TODO: make this a bit less hard coded | ||
134 | if ((image.Height < 64) && (image.Width < 64)) | ||
135 | { | ||
136 | newsize = new Size(32, 32); | ||
137 | } | ||
138 | else if ((image.Height < 128) && (image.Width < 128)) | ||
139 | { | ||
140 | newsize = new Size(64, 64); | ||
141 | } | ||
142 | else if ((image.Height <256) && (image.Width < 256)) | ||
143 | { | ||
144 | newsize = new Size(128, 128); | ||
145 | } | ||
146 | else if ((image.Height < 512 && image.Width < 512)) | ||
147 | { | ||
148 | newsize = new Size(256, 256); | ||
149 | } | ||
150 | else if ((image.Height < 1024 && image.Width < 1024)) | ||
151 | { | ||
152 | newsize = new Size(512, 512); | ||
153 | } | ||
154 | else | ||
155 | { | ||
156 | newsize = new Size(1024,1024); | ||
157 | } | ||
158 | |||
159 | Bitmap resize = new Bitmap(image, newsize); | ||
160 | byte[] imageJ2000 = OpenJPEG.EncodeFromImage(resize, true); | ||
161 | |||
162 | m_textureManager.ReturnData(state.RequestID, imageJ2000); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | public class RequestState | ||
167 | { | ||
168 | public HttpWebRequest Request = null; | ||
169 | public LLUUID RequestID = LLUUID.Zero; | ||
170 | public int TimeOfRequest = 0; | ||
171 | |||
172 | public RequestState(HttpWebRequest request, LLUUID requestID) | ||
173 | { | ||
174 | Request = request; | ||
175 | RequestID = requestID; | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | } \ No newline at end of file | ||