LoadImageURLModule.cs 5.8 KB

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