VectorRenderModule.cs 13 KB

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