VectorRenderModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 = new byte[0];
  144. try
  145. {
  146. imageJ2000 = OpenJPEG.EncodeFromImage(bitmap, true);
  147. }
  148. catch (Exception)
  149. {
  150. Console.WriteLine(
  151. "[VECTORRENDERMODULE]: OpenJpeg Encode Failed. Empty byte data returned!");
  152. }
  153. m_textureManager.ReturnData(id, imageJ2000);
  154. }
  155. /*
  156. private void CairoDraw(string data, System.Drawing.Graphics graph)
  157. {
  158. using (Win32Surface draw = new Win32Surface(graph.GetHdc()))
  159. {
  160. Context contex = new Context(draw);
  161. contex.Antialias = Antialias.None; //fastest method but low quality
  162. contex.LineWidth = 7;
  163. char[] lineDelimiter = { ';' };
  164. char[] partsDelimiter = { ',' };
  165. string[] lines = data.Split(lineDelimiter);
  166. foreach (string line in lines)
  167. {
  168. string nextLine = line.Trim();
  169. if (nextLine.StartsWith("MoveTO"))
  170. {
  171. float x = 0;
  172. float y = 0;
  173. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  174. contex.MoveTo(x, y);
  175. }
  176. else if (nextLine.StartsWith("LineTo"))
  177. {
  178. float x = 0;
  179. float y = 0;
  180. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  181. contex.LineTo(x, y);
  182. contex.Stroke();
  183. }
  184. }
  185. }
  186. graph.ReleaseHdc();
  187. }
  188. */
  189. private void GDIDraw(string data, Graphics graph)
  190. {
  191. Point startPoint = new Point(0, 0);
  192. Point endPoint = new Point(0, 0);
  193. Pen drawPen = new Pen(Color.Black, 7);
  194. string fontName = "Arial";
  195. float fontSize = 14;
  196. Font myFont = new Font(fontName, fontSize);
  197. SolidBrush myBrush = new SolidBrush(Color.Black);
  198. char[] lineDelimiter = {';'};
  199. char[] partsDelimiter = {','};
  200. string[] lines = data.Split(lineDelimiter);
  201. foreach (string line in lines)
  202. {
  203. string nextLine = line.Trim();
  204. //replace with switch, or even better, do some proper parsing
  205. if (nextLine.StartsWith("MoveTo"))
  206. {
  207. float x = 0;
  208. float y = 0;
  209. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  210. startPoint.X = (int) x;
  211. startPoint.Y = (int) y;
  212. }
  213. else if (nextLine.StartsWith("LineTo"))
  214. {
  215. float x = 0;
  216. float y = 0;
  217. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  218. endPoint.X = (int) x;
  219. endPoint.Y = (int) y;
  220. graph.DrawLine(drawPen, startPoint, endPoint);
  221. startPoint.X = endPoint.X;
  222. startPoint.Y = endPoint.Y;
  223. }
  224. else if (nextLine.StartsWith("Text"))
  225. {
  226. nextLine = nextLine.Remove(0, 4);
  227. nextLine = nextLine.Trim();
  228. graph.DrawString(nextLine, myFont, myBrush, startPoint);
  229. }
  230. else if (nextLine.StartsWith("Image"))
  231. {
  232. float x = 0;
  233. float y = 0;
  234. GetParams(partsDelimiter, ref nextLine, 5, ref x, ref y);
  235. endPoint.X = (int) x;
  236. endPoint.Y = (int) y;
  237. Image image = ImageHttpRequest(nextLine);
  238. graph.DrawImage(image, (float) startPoint.X, (float) startPoint.Y, x, y);
  239. startPoint.X += endPoint.X;
  240. startPoint.Y += endPoint.Y;
  241. }
  242. else if (nextLine.StartsWith("Rectangle"))
  243. {
  244. float x = 0;
  245. float y = 0;
  246. GetParams(partsDelimiter, ref nextLine, 9, ref x, ref y);
  247. endPoint.X = (int) x;
  248. endPoint.Y = (int) y;
  249. graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  250. startPoint.X += endPoint.X;
  251. startPoint.Y += endPoint.Y;
  252. }
  253. else if (nextLine.StartsWith("FillRectangle"))
  254. {
  255. float x = 0;
  256. float y = 0;
  257. GetParams(partsDelimiter, ref nextLine, 13, ref x, ref y);
  258. endPoint.X = (int) x;
  259. endPoint.Y = (int) y;
  260. graph.FillRectangle(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  261. startPoint.X += endPoint.X;
  262. startPoint.Y += endPoint.Y;
  263. }
  264. else if (nextLine.StartsWith("Ellipse"))
  265. {
  266. float x = 0;
  267. float y = 0;
  268. GetParams(partsDelimiter, ref nextLine, 7, ref x, ref y);
  269. endPoint.X = (int) x;
  270. endPoint.Y = (int) y;
  271. graph.DrawEllipse(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  272. startPoint.X += endPoint.X;
  273. startPoint.Y += endPoint.Y;
  274. }
  275. else if (nextLine.StartsWith("FontSize"))
  276. {
  277. nextLine = nextLine.Remove(0, 8);
  278. nextLine = nextLine.Trim();
  279. fontSize = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  280. myFont = new Font(fontName, fontSize);
  281. }
  282. else if (nextLine.StartsWith("FontName"))
  283. {
  284. nextLine = nextLine.Remove(0, 8);
  285. fontName = nextLine.Trim();
  286. myFont = new Font(fontName, fontSize);
  287. }
  288. else if (nextLine.StartsWith("PenSize"))
  289. {
  290. nextLine = nextLine.Remove(0, 7);
  291. nextLine = nextLine.Trim();
  292. float size = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  293. drawPen.Width = size;
  294. }
  295. else if (nextLine.StartsWith("PenColour"))
  296. {
  297. nextLine = nextLine.Remove(0, 9);
  298. nextLine = nextLine.Trim();
  299. int hex = 0;
  300. Color newColour;
  301. if (Int32.TryParse(nextLine, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex))
  302. {
  303. newColour = Color.FromArgb(hex);
  304. }
  305. else
  306. {
  307. // this doesn't fail, it just returns black if nothing is found
  308. newColour = Color.FromName(nextLine);
  309. }
  310. myBrush.Color = newColour;
  311. drawPen.Color = newColour;
  312. }
  313. }
  314. }
  315. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref float x, ref float y)
  316. {
  317. line = line.Remove(0, startLength);
  318. string[] parts = line.Split(partsDelimiter);
  319. if (parts.Length == 2)
  320. {
  321. string xVal = parts[0].Trim();
  322. string yVal = parts[1].Trim();
  323. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  324. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  325. }
  326. else if (parts.Length > 2)
  327. {
  328. string xVal = parts[0].Trim();
  329. string yVal = parts[1].Trim();
  330. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  331. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  332. line = "";
  333. for (int i = 2; i < parts.Length; i++)
  334. {
  335. line = line + parts[i].Trim();
  336. line = line + " ";
  337. }
  338. }
  339. }
  340. private Bitmap ImageHttpRequest(string url)
  341. {
  342. WebRequest request = HttpWebRequest.Create(url);
  343. //Ckrinke: Comment out for now as 'str' is unused. Bring it back into play later when it is used.
  344. //Ckrinke Stream str = null;
  345. HttpWebResponse response = (HttpWebResponse) (request).GetResponse();
  346. if (response.StatusCode == HttpStatusCode.OK)
  347. {
  348. Bitmap image = new Bitmap(response.GetResponseStream());
  349. return image;
  350. }
  351. return null;
  352. }
  353. }
  354. }