LoadImageURLModule.cs 5.9 KB

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