MapImageService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 OpenSimulator 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. * The design of this map service is based on SimianGrid's PHP-based
  28. * map service. See this URL for the original PHP version:
  29. * https://github.com/openmetaversefoundation/simiangrid/
  30. */
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Drawing;
  34. using System.Drawing.Imaging;
  35. using System.IO;
  36. using System.Net;
  37. using System.Reflection;
  38. using System.Threading;
  39. using Nini.Config;
  40. using log4net;
  41. using OpenMetaverse;
  42. using OpenSim.Framework;
  43. using OpenSim.Framework.Console;
  44. using OpenSim.Services.Interfaces;
  45. namespace OpenSim.Services.MapImageService
  46. {
  47. public class MapImageService : IMapImageService
  48. {
  49. private static readonly ILog m_log =
  50. LogManager.GetLogger(
  51. MethodBase.GetCurrentMethod().DeclaringType);
  52. #pragma warning disable 414
  53. private string LogHeader = "[MAP IMAGE SERVICE]";
  54. #pragma warning restore 414
  55. private const int ZOOM_LEVELS = 8;
  56. private const int IMAGE_WIDTH = 256;
  57. private const int HALF_WIDTH = 128;
  58. private const int JPEG_QUALITY = 80;
  59. private static string m_TilesStoragePath = "maptiles";
  60. private static object m_Sync = new object();
  61. private static bool m_Initialized = false;
  62. private static string m_WaterTileFile = string.Empty;
  63. private static Color m_Watercolor = Color.FromArgb(29, 71, 95);
  64. private static Bitmap m_WaterBitmap = null;
  65. private static byte[] m_WaterBytes = null;
  66. public MapImageService(IConfigSource config)
  67. {
  68. if (!m_Initialized)
  69. {
  70. m_Initialized = true;
  71. m_log.Debug("[MAP IMAGE SERVICE]: Starting MapImage service");
  72. IConfig serviceConfig = config.Configs["MapImageService"];
  73. if (serviceConfig != null)
  74. {
  75. m_TilesStoragePath = serviceConfig.GetString("TilesStoragePath", m_TilesStoragePath);
  76. if (!Directory.Exists(m_TilesStoragePath))
  77. Directory.CreateDirectory(m_TilesStoragePath);
  78. m_WaterTileFile = Path.Combine(m_TilesStoragePath, "water.jpg");
  79. if (!File.Exists(m_WaterTileFile))
  80. {
  81. Bitmap waterTile = new Bitmap(IMAGE_WIDTH, IMAGE_WIDTH);
  82. FillImage(waterTile, m_Watercolor);
  83. waterTile.Save(m_WaterTileFile, ImageFormat.Jpeg);
  84. m_WaterBitmap = waterTile;
  85. }
  86. if (File.Exists(m_WaterTileFile))
  87. {
  88. m_WaterBitmap = new Bitmap(m_WaterTileFile);
  89. using (MemoryStream ms = new MemoryStream())
  90. {
  91. m_WaterBitmap.Save(ms,ImageFormat.Jpeg);
  92. ms.Seek(0, SeekOrigin.Begin);
  93. m_WaterBytes = ms.ToArray();
  94. }
  95. }
  96. }
  97. }
  98. }
  99. #region IMapImageService
  100. public bool AddMapTile(int x, int y, byte[] imageData, UUID scopeID, out string reason)
  101. {
  102. reason = string.Empty;
  103. string fileName = GetFileName(1, x, y, scopeID);
  104. lock (m_Sync)
  105. {
  106. try
  107. {
  108. using (FileStream f = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write))
  109. f.Write(imageData, 0, imageData.Length);
  110. }
  111. catch (Exception e)
  112. {
  113. m_log.WarnFormat("[MAP IMAGE SERVICE]: Unable to save image file {0}: {1}", fileName, e);
  114. reason = e.Message;
  115. return false;
  116. }
  117. }
  118. return UpdateMultiResolutionFiles(x, y, scopeID, out reason);
  119. }
  120. public bool RemoveMapTile(int x, int y, UUID scopeID, out string reason)
  121. {
  122. reason = String.Empty;
  123. string fileName = GetFileName(1, x, y, scopeID);
  124. lock (m_Sync)
  125. {
  126. try
  127. {
  128. File.Delete(fileName);
  129. }
  130. catch (Exception e)
  131. {
  132. m_log.WarnFormat("[MAP IMAGE SERVICE]: Unable to save delete file {0}: {1}", fileName, e);
  133. reason = e.Message;
  134. return false;
  135. }
  136. }
  137. return UpdateMultiResolutionFiles(x, y, scopeID, out reason);
  138. }
  139. // When large varregions start up, they can send piles of new map tiles. This causes
  140. // this multi-resolution routine to be called a zillion times an causes much CPU
  141. // time to be spent creating multi-resolution tiles that will be replaced when
  142. // the next maptile arrives.
  143. private class mapToMultiRez
  144. {
  145. public int xx;
  146. public int yy;
  147. public UUID scopeID;
  148. public mapToMultiRez(int pX, int pY, UUID pscopeID)
  149. {
  150. xx = pX;
  151. yy = pY;
  152. scopeID = pscopeID;
  153. }
  154. };
  155. private Queue<mapToMultiRez> multiRezToBuild = new Queue<mapToMultiRez>();
  156. private bool UpdateMultiResolutionFiles(int x, int y, UUID scopeID, out string reason)
  157. {
  158. reason = String.Empty;
  159. lock (multiRezToBuild)
  160. {
  161. // m_log.DebugFormat("{0} UpdateMultiResolutionFilesAsync: scheduling update for <{1},{2}>", LogHeader, x, y);
  162. multiRezToBuild.Enqueue(new mapToMultiRez(x, y, scopeID));
  163. if (multiRezToBuild.Count == 1)
  164. Util.FireAndForget(
  165. DoUpdateMultiResolutionFilesAsync);
  166. }
  167. return true;
  168. }
  169. private void DoUpdateMultiResolutionFilesAsync(object o)
  170. {
  171. // let acumulate large region tiles
  172. Thread.Sleep(60 * 1000); // large regions take time to upload tiles
  173. while (multiRezToBuild.Count > 0)
  174. {
  175. mapToMultiRez toMultiRez = null;
  176. lock (multiRezToBuild)
  177. {
  178. if (multiRezToBuild.Count > 0)
  179. toMultiRez = multiRezToBuild.Dequeue();
  180. }
  181. if (toMultiRez != null)
  182. {
  183. int x = toMultiRez.xx;
  184. int y = toMultiRez.yy;
  185. UUID scopeID = toMultiRez.scopeID;
  186. // m_log.DebugFormat("{0} DoUpdateMultiResolutionFilesAsync: doing build for <{1},{2}>", LogHeader, x, y);
  187. int width = 1;
  188. // Stitch seven more aggregate tiles together
  189. for (uint zoomLevel = 2; zoomLevel <= ZOOM_LEVELS; zoomLevel++)
  190. {
  191. // Calculate the width (in full resolution tiles) and bottom-left
  192. // corner of the current zoom level
  193. width *= 2;
  194. int x1 = x - (x % width);
  195. int y1 = y - (y % width);
  196. lock (m_Sync) // must lock the reading and writing of the maptile files
  197. {
  198. if (!CreateTile(zoomLevel, x1, y1, scopeID))
  199. {
  200. m_log.WarnFormat("[MAP IMAGE SERVICE]: Unable to create tile for {0},{1} at zoom level {1}", x, y, zoomLevel);
  201. return;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. return;
  208. }
  209. public byte[] GetMapTile(string fileName, UUID scopeID, out string format)
  210. {
  211. //m_log.DebugFormat("[MAP IMAGE SERVICE]: Getting map tile {0}", fileName);
  212. string fullName = Path.Combine(m_TilesStoragePath, scopeID.ToString());
  213. fullName = Path.Combine(fullName, fileName);
  214. try
  215. {
  216. format = Path.GetExtension(fileName).ToLower();
  217. //m_log.DebugFormat("[MAP IMAGE SERVICE]: Found file {0}, extension {1}", fileName, format);
  218. return File.ReadAllBytes(fullName);
  219. }
  220. catch
  221. {
  222. format = ".jpg";
  223. if (m_WaterBytes != null)
  224. return (byte[])m_WaterBytes.Clone();
  225. else
  226. {
  227. //m_log.DebugFormat("[MAP IMAGE SERVICE]: unable to get file {0}", fileName);
  228. return new byte[0];
  229. }
  230. }
  231. }
  232. #endregion
  233. private string GetFileName(uint zoomLevel, int x, int y, UUID scopeID)
  234. {
  235. string extension = "jpg";
  236. string path = Path.Combine(m_TilesStoragePath, scopeID.ToString());
  237. Directory.CreateDirectory(path);
  238. return Path.Combine(path, string.Format("map-{0}-{1}-{2}-objects.{3}", zoomLevel, x, y, extension));
  239. }
  240. private Bitmap GetInputTileImage(string fileName)
  241. {
  242. try
  243. {
  244. if (File.Exists(fileName))
  245. return new Bitmap(fileName);
  246. }
  247. catch (Exception e)
  248. {
  249. m_log.WarnFormat("[MAP IMAGE SERVICE]: Unable to read image data from {0}: {1}", fileName, e);
  250. }
  251. return null;
  252. }
  253. private Bitmap GetOutputTileImage(string fileName)
  254. {
  255. try
  256. {
  257. if (File.Exists(fileName))
  258. return new Bitmap(fileName);
  259. else
  260. {
  261. // Create a new output tile with a transparent background
  262. Bitmap bm = new Bitmap(IMAGE_WIDTH, IMAGE_WIDTH, PixelFormat.Format24bppRgb);
  263. //bm.MakeTransparent(); // 24bpp does not have transparency, this would make it 32bpp
  264. return bm;
  265. }
  266. }
  267. catch (Exception e)
  268. {
  269. m_log.WarnFormat("[MAP IMAGE SERVICE]: Unable to read image data from {0}: {1}", fileName, e);
  270. }
  271. return null;
  272. }
  273. private bool CreateTile(uint zoomLevel, int x, int y, UUID scopeID)
  274. {
  275. // m_log.DebugFormat("[MAP IMAGE SERVICE]: Create tile for {0} {1}, zoom {2}", x, y, zoomLevel);
  276. int prevWidth = (int)Math.Pow(2, (double)zoomLevel - 2);
  277. int thisWidth = (int)Math.Pow(2, (double)zoomLevel - 1);
  278. // Convert x and y to the bottom left tile for this zoom level
  279. int xIn = x - (x % prevWidth);
  280. int yIn = y - (y % prevWidth);
  281. // Convert x and y to the bottom left tile for the next zoom level
  282. int xOut = x - (x % thisWidth);
  283. int yOut = y - (y % thisWidth);
  284. // Try to open the four input tiles from the previous zoom level
  285. Bitmap inputBL = GetInputTileImage(GetFileName(zoomLevel - 1, xIn, yIn, scopeID));
  286. Bitmap inputBR = GetInputTileImage(GetFileName(zoomLevel - 1, xIn + prevWidth, yIn, scopeID));
  287. Bitmap inputTL = GetInputTileImage(GetFileName(zoomLevel - 1, xIn, yIn + prevWidth, scopeID));
  288. Bitmap inputTR = GetInputTileImage(GetFileName(zoomLevel - 1, xIn + prevWidth, yIn + prevWidth, scopeID));
  289. // Open the output tile (current zoom level)
  290. string outputFile = GetFileName(zoomLevel, xOut, yOut, scopeID);
  291. int ntiles = 0;
  292. Bitmap output = (Bitmap)m_WaterBitmap.Clone();
  293. if (inputBL != null)
  294. {
  295. ImageCopyResampled(output, inputBL, 0, HALF_WIDTH, 0, 0);
  296. inputBL.Dispose();
  297. ntiles++;
  298. }
  299. if (inputBR != null)
  300. {
  301. ImageCopyResampled(output, inputBR, HALF_WIDTH, HALF_WIDTH, 0, 0);
  302. inputBR.Dispose();
  303. ntiles++;
  304. }
  305. if (inputTL != null)
  306. {
  307. ImageCopyResampled(output, inputTL, 0, 0, 0, 0);
  308. inputTL.Dispose();
  309. ntiles++;
  310. }
  311. if (inputTR != null)
  312. {
  313. ImageCopyResampled(output, inputTR, HALF_WIDTH, 0, 0, 0);
  314. inputTR.Dispose();
  315. ntiles++;
  316. }
  317. // Write the modified output
  318. if (ntiles == 0)
  319. File.Delete(outputFile);
  320. else
  321. {
  322. try
  323. {
  324. output.Save(outputFile, ImageFormat.Jpeg);
  325. }
  326. catch (Exception e)
  327. {
  328. m_log.WarnFormat("[MAP IMAGE SERVICE]: Oops on saving {0} {1}", outputFile, e);
  329. }
  330. } // Save also as png?
  331. output.Dispose();
  332. return true;
  333. }
  334. #region Image utilities
  335. private void FillImage(Bitmap bm, Color c)
  336. {
  337. for (int x = 0; x < bm.Width; x++)
  338. for (int y = 0; y < bm.Height; y++)
  339. bm.SetPixel(x, y, c);
  340. }
  341. private void ImageCopyResampled(Bitmap output, Bitmap input, int destX, int destY, int srcX, int srcY)
  342. {
  343. int resamplingRateX = 2; // (input.Width - srcX) / (output.Width - destX);
  344. int resamplingRateY = 2; // (input.Height - srcY) / (output.Height - destY);
  345. for (int x = destX; x < destX + HALF_WIDTH; x++)
  346. for (int y = destY; y < destY + HALF_WIDTH; y++)
  347. {
  348. Color p = input.GetPixel(srcX + (x - destX) * resamplingRateX, srcY + (y - destY) * resamplingRateY);
  349. output.SetPixel(x, y, p);
  350. }
  351. }
  352. #endregion
  353. }
  354. }