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. format = ".jpg";
  213. string fullName = Path.Combine(m_TilesStoragePath, scopeID.ToString());
  214. fullName = Path.Combine(fullName, fileName);
  215. if (File.Exists(fullName))
  216. {
  217. format = Path.GetExtension(fileName).ToLower();
  218. //m_log.DebugFormat("[MAP IMAGE SERVICE]: Found file {0}, extension {1}", fileName, format);
  219. return File.ReadAllBytes(fullName);
  220. }
  221. else if (m_WaterBytes != null)
  222. {
  223. return (byte[])m_WaterBytes.Clone();
  224. }
  225. else
  226. {
  227. m_log.DebugFormat("[MAP IMAGE SERVICE]: unable to get file {0}", fileName);
  228. return new byte[0];
  229. }
  230. }
  231. #endregion
  232. private string GetFileName(uint zoomLevel, int x, int y, UUID scopeID)
  233. {
  234. string extension = "jpg";
  235. string path = Path.Combine(m_TilesStoragePath, scopeID.ToString());
  236. Directory.CreateDirectory(path);
  237. return Path.Combine(path, string.Format("map-{0}-{1}-{2}-objects.{3}", zoomLevel, x, y, extension));
  238. }
  239. private Bitmap GetInputTileImage(string fileName)
  240. {
  241. try
  242. {
  243. if (File.Exists(fileName))
  244. return new Bitmap(fileName);
  245. }
  246. catch (Exception e)
  247. {
  248. m_log.WarnFormat("[MAP IMAGE SERVICE]: Unable to read image data from {0}: {1}", fileName, e);
  249. }
  250. return null;
  251. }
  252. private Bitmap GetOutputTileImage(string fileName)
  253. {
  254. try
  255. {
  256. if (File.Exists(fileName))
  257. return new Bitmap(fileName);
  258. else
  259. {
  260. // Create a new output tile with a transparent background
  261. Bitmap bm = new Bitmap(IMAGE_WIDTH, IMAGE_WIDTH, PixelFormat.Format24bppRgb);
  262. //bm.MakeTransparent(); // 24bpp does not have transparency, this whould make it 32bpp
  263. return bm;
  264. }
  265. }
  266. catch (Exception e)
  267. {
  268. m_log.WarnFormat("[MAP IMAGE SERVICE]: Unable to read image data from {0}: {1}", fileName, e);
  269. }
  270. return null;
  271. }
  272. private bool CreateTile(uint zoomLevel, int x, int y, UUID scopeID)
  273. {
  274. // m_log.DebugFormat("[MAP IMAGE SERVICE]: Create tile for {0} {1}, zoom {2}", x, y, zoomLevel);
  275. int prevWidth = (int)Math.Pow(2, (double)zoomLevel - 2);
  276. int thisWidth = (int)Math.Pow(2, (double)zoomLevel - 1);
  277. // Convert x and y to the bottom left tile for this zoom level
  278. int xIn = x - (x % prevWidth);
  279. int yIn = y - (y % prevWidth);
  280. // Convert x and y to the bottom left tile for the next zoom level
  281. int xOut = x - (x % thisWidth);
  282. int yOut = y - (y % thisWidth);
  283. // Try to open the four input tiles from the previous zoom level
  284. Bitmap inputBL = GetInputTileImage(GetFileName(zoomLevel - 1, xIn, yIn, scopeID));
  285. Bitmap inputBR = GetInputTileImage(GetFileName(zoomLevel - 1, xIn + prevWidth, yIn, scopeID));
  286. Bitmap inputTL = GetInputTileImage(GetFileName(zoomLevel - 1, xIn, yIn + prevWidth, scopeID));
  287. Bitmap inputTR = GetInputTileImage(GetFileName(zoomLevel - 1, xIn + prevWidth, yIn + prevWidth, scopeID));
  288. // Open the output tile (current zoom level)
  289. string outputFile = GetFileName(zoomLevel, xOut, yOut, scopeID);
  290. int ntiles = 0;
  291. Bitmap output = (Bitmap)m_WaterBitmap.Clone();
  292. if (inputBL != null)
  293. {
  294. ImageCopyResampled(output, inputBL, 0, HALF_WIDTH, 0, 0);
  295. inputBL.Dispose();
  296. ntiles++;
  297. }
  298. if (inputBR != null)
  299. {
  300. ImageCopyResampled(output, inputBR, HALF_WIDTH, HALF_WIDTH, 0, 0);
  301. inputBR.Dispose();
  302. ntiles++;
  303. }
  304. if (inputTL != null)
  305. {
  306. ImageCopyResampled(output, inputTL, 0, 0, 0, 0);
  307. inputTL.Dispose();
  308. ntiles++;
  309. }
  310. if (inputTR != null)
  311. {
  312. ImageCopyResampled(output, inputTR, HALF_WIDTH, 0, 0, 0);
  313. inputTR.Dispose();
  314. ntiles++;
  315. }
  316. // Write the modified output
  317. if (ntiles == 0)
  318. File.Delete(outputFile);
  319. else
  320. {
  321. try
  322. {
  323. output.Save(outputFile, ImageFormat.Jpeg);
  324. }
  325. catch (Exception e)
  326. {
  327. m_log.WarnFormat("[MAP IMAGE SERVICE]: Oops on saving {0} {1}", outputFile, e);
  328. }
  329. } // Save also as png?
  330. output.Dispose();
  331. return true;
  332. }
  333. #region Image utilities
  334. private void FillImage(Bitmap bm, Color c)
  335. {
  336. for (int x = 0; x < bm.Width; x++)
  337. for (int y = 0; y < bm.Height; y++)
  338. bm.SetPixel(x, y, c);
  339. }
  340. private void ImageCopyResampled(Bitmap output, Bitmap input, int destX, int destY, int srcX, int srcY)
  341. {
  342. int resamplingRateX = 2; // (input.Width - srcX) / (output.Width - destX);
  343. int resamplingRateY = 2; // (input.Height - srcY) / (output.Height - destY);
  344. for (int x = destX; x < destX + HALF_WIDTH; x++)
  345. for (int y = destY; y < destY + HALF_WIDTH; y++)
  346. {
  347. Color p = input.GetPixel(srcX + (x - destX) * resamplingRateX, srcY + (y - destY) * resamplingRateY);
  348. output.SetPixel(x, y, p);
  349. }
  350. }
  351. #endregion
  352. }
  353. }