diff options
Diffstat (limited to 'OpenSim/Region/Environment/Modules/VectorRenderModule.cs')
-rw-r--r-- | OpenSim/Region/Environment/Modules/VectorRenderModule.cs | 308 |
1 files changed, 308 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/VectorRenderModule.cs b/OpenSim/Region/Environment/Modules/VectorRenderModule.cs new file mode 100644 index 0000000..d033170 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/VectorRenderModule.cs | |||
@@ -0,0 +1,308 @@ | |||
1 | | ||
2 | using System; | ||
3 | using System.Drawing; | ||
4 | using System.IO; | ||
5 | using System.Net; | ||
6 | using System.Globalization; | ||
7 | using libsecondlife; | ||
8 | using Nini.Config; | ||
9 | using OpenJPEGNet; | ||
10 | using OpenSim.Framework; | ||
11 | using OpenSim.Region.Environment.Interfaces; | ||
12 | using OpenSim.Region.Environment.Scenes; | ||
13 | //using Cairo; | ||
14 | |||
15 | namespace OpenSim.Region.Environment.Modules | ||
16 | { | ||
17 | public class VectorRenderModule : IRegionModule, IDynamicTextureRender | ||
18 | { | ||
19 | private Scene m_scene; | ||
20 | private string m_name = "VectorRenderModule"; | ||
21 | private IDynamicTextureManager m_textureManager; | ||
22 | |||
23 | public VectorRenderModule() | ||
24 | { | ||
25 | } | ||
26 | |||
27 | public void Initialise(Scene scene, IConfigSource config) | ||
28 | { | ||
29 | if (m_scene == null) | ||
30 | { | ||
31 | m_scene = scene; | ||
32 | } | ||
33 | } | ||
34 | |||
35 | public void PostInitialise() | ||
36 | { | ||
37 | m_textureManager = m_scene.RequestModuleInterface<IDynamicTextureManager>(); | ||
38 | if (m_textureManager != null) | ||
39 | { | ||
40 | m_textureManager.RegisterRender(GetContentType(), this); | ||
41 | } | ||
42 | } | ||
43 | |||
44 | public void Close() | ||
45 | { | ||
46 | } | ||
47 | |||
48 | public string Name | ||
49 | { | ||
50 | get { return m_name; } | ||
51 | } | ||
52 | |||
53 | public bool IsSharedModule | ||
54 | { | ||
55 | get { return true; } | ||
56 | } | ||
57 | |||
58 | private void Draw(string data, LLUUID id, string extraParams) | ||
59 | { | ||
60 | Bitmap bitmap = new Bitmap(256, 256, System.Drawing.Imaging.PixelFormat.Format32bppArgb); | ||
61 | |||
62 | System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bitmap); | ||
63 | |||
64 | extraParams = extraParams.ToLower(); | ||
65 | int alpha = 255; | ||
66 | if (extraParams == "setalpha") | ||
67 | { | ||
68 | alpha = 0; | ||
69 | } | ||
70 | else | ||
71 | { | ||
72 | graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 256, 256); | ||
73 | } | ||
74 | |||
75 | for (int w = 0; w < bitmap.Width; w++) | ||
76 | { | ||
77 | for (int h = 0; h < bitmap.Height; h++) | ||
78 | { | ||
79 | bitmap.SetPixel(w, h, Color.FromArgb(alpha, bitmap.GetPixel(w, h))); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | |||
84 | |||
85 | GDIDraw(data, graph); | ||
86 | |||
87 | byte[] imageJ2000 = OpenJPEG.EncodeFromImage(bitmap, true); | ||
88 | m_textureManager.ReturnData(id, imageJ2000); | ||
89 | |||
90 | } | ||
91 | |||
92 | /* private void CairoDraw(string data, System.Drawing.Graphics graph) | ||
93 | { | ||
94 | using (Win32Surface draw = new Win32Surface(graph.GetHdc())) | ||
95 | { | ||
96 | Context contex = new Context(draw); | ||
97 | |||
98 | contex.Antialias = Antialias.None; //fastest method but low quality | ||
99 | contex.LineWidth = 7; | ||
100 | char[] lineDelimiter = { ';' }; | ||
101 | char[] partsDelimiter = { ',' }; | ||
102 | string[] lines = data.Split(lineDelimiter); | ||
103 | |||
104 | foreach (string line in lines) | ||
105 | { | ||
106 | string nextLine = line.Trim(); | ||
107 | |||
108 | if (nextLine.StartsWith("MoveTO")) | ||
109 | { | ||
110 | float x = 0; | ||
111 | float y = 0; | ||
112 | GetParams(partsDelimiter, ref nextLine, ref x, ref y); | ||
113 | contex.MoveTo(x, y); | ||
114 | } | ||
115 | else if (nextLine.StartsWith("LineTo")) | ||
116 | { | ||
117 | float x = 0; | ||
118 | float y = 0; | ||
119 | GetParams(partsDelimiter, ref nextLine, ref x, ref y); | ||
120 | contex.LineTo(x, y); | ||
121 | contex.Stroke(); | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | graph.ReleaseHdc(); | ||
126 | }*/ | ||
127 | |||
128 | private void GDIDraw(string data, System.Drawing.Graphics graph) | ||
129 | { | ||
130 | System.Drawing.Point startPoint = new System.Drawing.Point(0, 0); | ||
131 | System.Drawing.Point endPoint = new System.Drawing.Point(0, 0); | ||
132 | System.Drawing.Pen drawPen = new Pen(System.Drawing.Color.Black, 7); | ||
133 | Font myFont = new Font("Times New Roman", 14); | ||
134 | SolidBrush myBrush = new SolidBrush(Color.Black); | ||
135 | char[] lineDelimiter = { ';' }; | ||
136 | char[] partsDelimiter = { ',' }; | ||
137 | string[] lines = data.Split(lineDelimiter); | ||
138 | |||
139 | |||
140 | foreach (string line in lines) | ||
141 | { | ||
142 | string nextLine = line.Trim(); | ||
143 | |||
144 | if (nextLine.StartsWith("MoveTo")) | ||
145 | { | ||
146 | float x = 0; | ||
147 | float y = 0; | ||
148 | GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y); | ||
149 | startPoint.X = (int)x; | ||
150 | startPoint.Y = (int)y; | ||
151 | } | ||
152 | else if (nextLine.StartsWith("LineTo")) | ||
153 | { | ||
154 | float x = 0; | ||
155 | float y = 0; | ||
156 | GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y); | ||
157 | endPoint.X = (int)x; | ||
158 | endPoint.Y = (int)y; | ||
159 | graph.DrawLine(drawPen, startPoint, endPoint); | ||
160 | startPoint.X = endPoint.X; | ||
161 | startPoint.Y = endPoint.Y; | ||
162 | } | ||
163 | else if (nextLine.StartsWith("Text")) | ||
164 | { | ||
165 | nextLine = nextLine.Remove(0, 4); | ||
166 | nextLine = nextLine.Trim(); | ||
167 | graph.DrawString(nextLine, myFont, myBrush, startPoint); | ||
168 | } | ||
169 | else if (nextLine.StartsWith("Image")) | ||
170 | { | ||
171 | float x = 0; | ||
172 | float y = 0; | ||
173 | GetParams(partsDelimiter, ref nextLine, 5, ref x, ref y); | ||
174 | endPoint.X = (int)x; | ||
175 | endPoint.Y = (int)y; | ||
176 | System.Drawing.Image image = ImageHttpRequest(nextLine); | ||
177 | graph.DrawImage(image, (float)startPoint.X, (float)startPoint.Y, x, y); | ||
178 | startPoint.X += endPoint.X; | ||
179 | startPoint.Y += endPoint.Y; | ||
180 | } | ||
181 | else if (nextLine.StartsWith("Rectangle")) | ||
182 | { | ||
183 | float x = 0; | ||
184 | float y = 0; | ||
185 | GetParams(partsDelimiter, ref nextLine, 9, ref x, ref y); | ||
186 | endPoint.X = (int)x; | ||
187 | endPoint.Y = (int)y; | ||
188 | graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y); | ||
189 | startPoint.X += endPoint.X; | ||
190 | startPoint.Y += endPoint.Y; | ||
191 | } | ||
192 | else if (nextLine.StartsWith("FillRectangle")) | ||
193 | { | ||
194 | float x = 0; | ||
195 | float y = 0; | ||
196 | GetParams(partsDelimiter, ref nextLine, 13, ref x, ref y); | ||
197 | endPoint.X = (int)x; | ||
198 | endPoint.Y = (int)y; | ||
199 | graph.FillRectangle(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y); | ||
200 | startPoint.X += endPoint.X; | ||
201 | startPoint.Y += endPoint.Y; | ||
202 | } | ||
203 | else if (nextLine.StartsWith("Ellipse")) | ||
204 | { | ||
205 | float x = 0; | ||
206 | float y = 0; | ||
207 | GetParams(partsDelimiter, ref nextLine, 7, ref x, ref y); | ||
208 | endPoint.X = (int)x; | ||
209 | endPoint.Y = (int)y; | ||
210 | graph.DrawEllipse(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y); | ||
211 | startPoint.X += endPoint.X; | ||
212 | startPoint.Y += endPoint.Y; | ||
213 | } | ||
214 | else if (nextLine.StartsWith("FontSize")) | ||
215 | { | ||
216 | nextLine = nextLine.Remove(0, 8); | ||
217 | nextLine = nextLine.Trim(); | ||
218 | float size = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture); | ||
219 | myFont = new Font("Times New Roman", size); | ||
220 | } | ||
221 | else if (nextLine.StartsWith("PenSize")) | ||
222 | { | ||
223 | nextLine = nextLine.Remove(0, 8); | ||
224 | nextLine = nextLine.Trim(); | ||
225 | float size = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture); | ||
226 | drawPen.Width = size; | ||
227 | } | ||
228 | } | ||
229 | } | ||
230 | |||
231 | private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref float x, ref float y) | ||
232 | { | ||
233 | line = line.Remove(0, startLength); | ||
234 | string[] parts = line.Split(partsDelimiter); | ||
235 | if (parts.Length == 2) | ||
236 | { | ||
237 | string xVal = parts[0].Trim(); | ||
238 | string yVal = parts[1].Trim(); | ||
239 | x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture); | ||
240 | y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture); | ||
241 | } | ||
242 | else if (parts.Length > 2) | ||
243 | { | ||
244 | string xVal = parts[0].Trim(); | ||
245 | string yVal = parts[1].Trim(); | ||
246 | x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture); | ||
247 | y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture); | ||
248 | |||
249 | line = ""; | ||
250 | for (int i = 2; i < parts.Length; i++) | ||
251 | { | ||
252 | line = line + parts[i].Trim(); | ||
253 | line = line + " "; | ||
254 | } | ||
255 | } | ||
256 | } | ||
257 | |||
258 | private Bitmap ImageHttpRequest(string url) | ||
259 | { | ||
260 | WebRequest request = HttpWebRequest.Create(url); | ||
261 | Stream str = null; | ||
262 | HttpWebResponse response = (HttpWebResponse)(request).GetResponse(); | ||
263 | if (response.StatusCode == HttpStatusCode.OK) | ||
264 | { | ||
265 | Bitmap image = new Bitmap(response.GetResponseStream()); | ||
266 | return image; | ||
267 | } | ||
268 | |||
269 | return null; | ||
270 | } | ||
271 | |||
272 | public string GetContentType() | ||
273 | { | ||
274 | return ("vector"); | ||
275 | } | ||
276 | |||
277 | public string GetName() | ||
278 | { | ||
279 | return m_name; | ||
280 | } | ||
281 | |||
282 | public bool SupportsAsynchronous() | ||
283 | { | ||
284 | return true; | ||
285 | } | ||
286 | |||
287 | public byte[] ConvertUrl(string url, string extraParams) | ||
288 | { | ||
289 | return null; | ||
290 | } | ||
291 | |||
292 | public byte[] ConvertStream(Stream data, string extraParams) | ||
293 | { | ||
294 | return null; | ||
295 | } | ||
296 | |||
297 | public bool AsyncConvertUrl(LLUUID id, string url, string extraParams) | ||
298 | { | ||
299 | return false; | ||
300 | } | ||
301 | |||
302 | public bool AsyncConvertData(LLUUID id, string bodyData, string extraParams) | ||
303 | { | ||
304 | Draw(bodyData, id, extraParams); | ||
305 | return true; | ||
306 | } | ||
307 | } | ||
308 | } \ No newline at end of file | ||