LoadImageURLModule.cs 6.1 KB

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