LoadImageURLModule.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 OpenMetaverse;
  32. using OpenMetaverse.Imaging;
  33. using Nini.Config;
  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(UUID id, string url, string extraParams)
  65. {
  66. MakeHttpRequest(url, id);
  67. return true;
  68. }
  69. public bool AsyncConvertData(UUID 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, UUID 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. request.BeginGetResponse(new AsyncCallback(HttpRequestReturn), state);
  108. TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
  109. state.TimeOfRequest = (int) t.TotalSeconds;
  110. }
  111. private void HttpRequestReturn(IAsyncResult result)
  112. {
  113. RequestState state = (RequestState) result.AsyncState;
  114. WebRequest request = (WebRequest) state.Request;
  115. try
  116. {
  117. HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
  118. if (response.StatusCode == HttpStatusCode.OK)
  119. {
  120. Bitmap image = new Bitmap(response.GetResponseStream());
  121. Size newsize;
  122. // TODO: make this a bit less hard coded
  123. if ((image.Height < 64) && (image.Width < 64))
  124. {
  125. newsize = new Size(32, 32);
  126. }
  127. else if ((image.Height < 128) && (image.Width < 128))
  128. {
  129. newsize = new Size(64, 64);
  130. }
  131. else if ((image.Height < 256) && (image.Width < 256))
  132. {
  133. newsize = new Size(128, 128);
  134. }
  135. else if ((image.Height < 512 && image.Width < 512))
  136. {
  137. newsize = new Size(256, 256);
  138. }
  139. else if ((image.Height < 1024 && image.Width < 1024))
  140. {
  141. newsize = new Size(512, 512);
  142. }
  143. else
  144. {
  145. newsize = new Size(1024, 1024);
  146. }
  147. Bitmap resize = new Bitmap(image, newsize);
  148. byte[] imageJ2000 = OpenJPEG.EncodeFromImage(resize, true);
  149. m_textureManager.ReturnData(state.RequestID, imageJ2000);
  150. }
  151. }
  152. catch (WebException)
  153. {
  154. }
  155. }
  156. #region Nested type: RequestState
  157. public class RequestState
  158. {
  159. public HttpWebRequest Request = null;
  160. public UUID RequestID = UUID.Zero;
  161. public int TimeOfRequest = 0;
  162. public RequestState(HttpWebRequest request, UUID requestID)
  163. {
  164. Request = request;
  165. RequestID = requestID;
  166. }
  167. }
  168. #endregion
  169. }
  170. }