VectorRenderModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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.Drawing.Imaging;
  30. using System.Globalization;
  31. using System.IO;
  32. using System.Net;
  33. using OpenMetaverse;
  34. using OpenMetaverse.Imaging;
  35. using Nini.Config;
  36. using OpenSim.Region.Environment.Interfaces;
  37. using OpenSim.Region.Environment.Scenes;
  38. //using Cairo;
  39. namespace OpenSim.Region.Environment.Modules.Scripting.VectorRender
  40. {
  41. public class VectorRenderModule : IRegionModule, IDynamicTextureRender
  42. {
  43. private string m_name = "VectorRenderModule";
  44. private Scene m_scene;
  45. private IDynamicTextureManager m_textureManager;
  46. public VectorRenderModule()
  47. {
  48. }
  49. #region IDynamicTextureRender Members
  50. public string GetContentType()
  51. {
  52. return ("vector");
  53. }
  54. public string GetName()
  55. {
  56. return m_name;
  57. }
  58. public bool SupportsAsynchronous()
  59. {
  60. return true;
  61. }
  62. public byte[] ConvertUrl(string url, string extraParams)
  63. {
  64. return null;
  65. }
  66. public byte[] ConvertStream(Stream data, string extraParams)
  67. {
  68. return null;
  69. }
  70. public bool AsyncConvertUrl(UUID id, string url, string extraParams)
  71. {
  72. return false;
  73. }
  74. public bool AsyncConvertData(UUID id, string bodyData, string extraParams)
  75. {
  76. Draw(bodyData, id, extraParams);
  77. return true;
  78. }
  79. #endregion
  80. #region IRegionModule Members
  81. public void Initialise(Scene scene, IConfigSource config)
  82. {
  83. if (m_scene == null)
  84. {
  85. m_scene = scene;
  86. }
  87. }
  88. public void PostInitialise()
  89. {
  90. m_textureManager = m_scene.RequestModuleInterface<IDynamicTextureManager>();
  91. if (m_textureManager != null)
  92. {
  93. m_textureManager.RegisterRender(GetContentType(), this);
  94. }
  95. }
  96. public void Close()
  97. {
  98. }
  99. public string Name
  100. {
  101. get { return m_name; }
  102. }
  103. public bool IsSharedModule
  104. {
  105. get { return true; }
  106. }
  107. #endregion
  108. private void Draw(string data, UUID id, string extraParams)
  109. {
  110. // TODO: this is a brutal hack. extraParams should actually be parsed reasonably.
  111. int size = 256;
  112. try
  113. {
  114. size = Convert.ToInt32(extraParams);
  115. }
  116. catch (Exception e)
  117. {
  118. //Ckrinke: Add a WriteLine to remove the warning about 'e' defined but not used
  119. Console.WriteLine("Problem with Draw. Please verify parameters." + e.ToString());
  120. }
  121. if ((size < 128) || (size > 1024))
  122. size = 256;
  123. Bitmap bitmap = new Bitmap(size, size, PixelFormat.Format32bppArgb);
  124. Graphics graph = Graphics.FromImage(bitmap);
  125. extraParams = extraParams.ToLower();
  126. int alpha = 255;
  127. if (extraParams == "setalpha")
  128. {
  129. alpha = 0;
  130. }
  131. else
  132. {
  133. graph.FillRectangle(new SolidBrush(Color.White), 0, 0, size, size);
  134. }
  135. for (int w = 0; w < bitmap.Width; w++)
  136. {
  137. for (int h = 0; h < bitmap.Height; h++)
  138. {
  139. bitmap.SetPixel(w, h, Color.FromArgb(alpha, bitmap.GetPixel(w, h)));
  140. }
  141. }
  142. GDIDraw(data, graph);
  143. byte[] imageJ2000 = OpenJPEG.EncodeFromImage(bitmap, true);
  144. m_textureManager.ReturnData(id, imageJ2000);
  145. }
  146. /*
  147. private void CairoDraw(string data, System.Drawing.Graphics graph)
  148. {
  149. using (Win32Surface draw = new Win32Surface(graph.GetHdc()))
  150. {
  151. Context contex = new Context(draw);
  152. contex.Antialias = Antialias.None; //fastest method but low quality
  153. contex.LineWidth = 7;
  154. char[] lineDelimiter = { ';' };
  155. char[] partsDelimiter = { ',' };
  156. string[] lines = data.Split(lineDelimiter);
  157. foreach (string line in lines)
  158. {
  159. string nextLine = line.Trim();
  160. if (nextLine.StartsWith("MoveTO"))
  161. {
  162. float x = 0;
  163. float y = 0;
  164. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  165. contex.MoveTo(x, y);
  166. }
  167. else if (nextLine.StartsWith("LineTo"))
  168. {
  169. float x = 0;
  170. float y = 0;
  171. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  172. contex.LineTo(x, y);
  173. contex.Stroke();
  174. }
  175. }
  176. }
  177. graph.ReleaseHdc();
  178. }
  179. */
  180. private void GDIDraw(string data, Graphics graph)
  181. {
  182. Point startPoint = new Point(0, 0);
  183. Point endPoint = new Point(0, 0);
  184. Pen drawPen = new Pen(Color.Black, 7);
  185. string fontName = "Arial";
  186. float fontSize = 14;
  187. Font myFont = new Font(fontName, fontSize);
  188. SolidBrush myBrush = new SolidBrush(Color.Black);
  189. char[] lineDelimiter = {';'};
  190. char[] partsDelimiter = {','};
  191. string[] lines = data.Split(lineDelimiter);
  192. foreach (string line in lines)
  193. {
  194. string nextLine = line.Trim();
  195. //replace with switch, or even better, do some proper parsing
  196. if (nextLine.StartsWith("MoveTo"))
  197. {
  198. float x = 0;
  199. float y = 0;
  200. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  201. startPoint.X = (int) x;
  202. startPoint.Y = (int) y;
  203. }
  204. else if (nextLine.StartsWith("LineTo"))
  205. {
  206. float x = 0;
  207. float y = 0;
  208. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  209. endPoint.X = (int) x;
  210. endPoint.Y = (int) y;
  211. graph.DrawLine(drawPen, startPoint, endPoint);
  212. startPoint.X = endPoint.X;
  213. startPoint.Y = endPoint.Y;
  214. }
  215. else if (nextLine.StartsWith("Text"))
  216. {
  217. nextLine = nextLine.Remove(0, 4);
  218. nextLine = nextLine.Trim();
  219. graph.DrawString(nextLine, myFont, myBrush, startPoint);
  220. }
  221. else if (nextLine.StartsWith("Image"))
  222. {
  223. float x = 0;
  224. float y = 0;
  225. GetParams(partsDelimiter, ref nextLine, 5, ref x, ref y);
  226. endPoint.X = (int) x;
  227. endPoint.Y = (int) y;
  228. Image image = ImageHttpRequest(nextLine);
  229. graph.DrawImage(image, (float) startPoint.X, (float) startPoint.Y, x, y);
  230. startPoint.X += endPoint.X;
  231. startPoint.Y += endPoint.Y;
  232. }
  233. else if (nextLine.StartsWith("Rectangle"))
  234. {
  235. float x = 0;
  236. float y = 0;
  237. GetParams(partsDelimiter, ref nextLine, 9, ref x, ref y);
  238. endPoint.X = (int) x;
  239. endPoint.Y = (int) y;
  240. graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  241. startPoint.X += endPoint.X;
  242. startPoint.Y += endPoint.Y;
  243. }
  244. else if (nextLine.StartsWith("FillRectangle"))
  245. {
  246. float x = 0;
  247. float y = 0;
  248. GetParams(partsDelimiter, ref nextLine, 13, ref x, ref y);
  249. endPoint.X = (int) x;
  250. endPoint.Y = (int) y;
  251. graph.FillRectangle(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  252. startPoint.X += endPoint.X;
  253. startPoint.Y += endPoint.Y;
  254. }
  255. else if (nextLine.StartsWith("Ellipse"))
  256. {
  257. float x = 0;
  258. float y = 0;
  259. GetParams(partsDelimiter, ref nextLine, 7, ref x, ref y);
  260. endPoint.X = (int) x;
  261. endPoint.Y = (int) y;
  262. graph.DrawEllipse(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  263. startPoint.X += endPoint.X;
  264. startPoint.Y += endPoint.Y;
  265. }
  266. else if (nextLine.StartsWith("FontSize"))
  267. {
  268. nextLine = nextLine.Remove(0, 8);
  269. nextLine = nextLine.Trim();
  270. fontSize = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  271. myFont = new Font(fontName, fontSize);
  272. }
  273. else if (nextLine.StartsWith("FontName"))
  274. {
  275. nextLine = nextLine.Remove(0, 8);
  276. fontName = nextLine.Trim();
  277. myFont = new Font(fontName, fontSize);
  278. }
  279. else if (nextLine.StartsWith("PenSize"))
  280. {
  281. nextLine = nextLine.Remove(0, 7);
  282. nextLine = nextLine.Trim();
  283. float size = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  284. drawPen.Width = size;
  285. }
  286. else if (nextLine.StartsWith("PenColour"))
  287. {
  288. nextLine = nextLine.Remove(0, 9);
  289. nextLine = nextLine.Trim();
  290. int hex = 0;
  291. Color newColour;
  292. if (Int32.TryParse(nextLine, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex))
  293. {
  294. newColour = Color.FromArgb(hex);
  295. }
  296. else
  297. {
  298. // this doesn't fail, it just returns black if nothing is found
  299. newColour = Color.FromName(nextLine);
  300. }
  301. myBrush.Color = newColour;
  302. drawPen.Color = newColour;
  303. }
  304. }
  305. }
  306. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref float x, ref float y)
  307. {
  308. line = line.Remove(0, startLength);
  309. string[] parts = line.Split(partsDelimiter);
  310. if (parts.Length == 2)
  311. {
  312. string xVal = parts[0].Trim();
  313. string yVal = parts[1].Trim();
  314. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  315. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  316. }
  317. else if (parts.Length > 2)
  318. {
  319. string xVal = parts[0].Trim();
  320. string yVal = parts[1].Trim();
  321. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  322. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  323. line = "";
  324. for (int i = 2; i < parts.Length; i++)
  325. {
  326. line = line + parts[i].Trim();
  327. line = line + " ";
  328. }
  329. }
  330. }
  331. private Bitmap ImageHttpRequest(string url)
  332. {
  333. WebRequest request = HttpWebRequest.Create(url);
  334. //Ckrinke: Comment out for now as 'str' is unused. Bring it back into play later when it is used.
  335. //Ckrinke Stream str = null;
  336. HttpWebResponse response = (HttpWebResponse) (request).GetResponse();
  337. if (response.StatusCode == HttpStatusCode.OK)
  338. {
  339. Bitmap image = new Bitmap(response.GetResponseStream());
  340. return image;
  341. }
  342. return null;
  343. }
  344. }
  345. }