2009-02-12 09:53:12 +00:00
|
|
|
/*
|
2009-01-19 18:33:25 +00:00
|
|
|
* Copyright (c) Contributors, http://opensimulator.org/
|
|
|
|
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2009-06-01 06:37:14 +00:00
|
|
|
* * Neither the name of the OpenSimulator Project nor the
|
2009-01-19 18:33:25 +00:00
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
2009-02-12 09:53:12 +00:00
|
|
|
using System.Collections.Generic;
|
2014-01-19 15:32:41 +00:00
|
|
|
using System.Drawing;
|
2009-01-22 01:33:46 +00:00
|
|
|
using System.IO;
|
2009-01-19 18:33:25 +00:00
|
|
|
using System.Reflection;
|
2009-01-22 01:33:46 +00:00
|
|
|
using System.Text;
|
2009-08-29 04:54:38 +00:00
|
|
|
using System.Threading;
|
2009-01-19 18:33:25 +00:00
|
|
|
using log4net;
|
2012-11-13 16:27:51 +00:00
|
|
|
using Mono.Addins;
|
2009-01-19 18:33:25 +00:00
|
|
|
using Nini.Config;
|
|
|
|
using OpenMetaverse;
|
|
|
|
using OpenMetaverse.Imaging;
|
2009-09-30 19:18:22 +00:00
|
|
|
using CSJ2K;
|
2009-01-22 01:33:46 +00:00
|
|
|
using OpenSim.Framework;
|
2009-02-06 16:55:34 +00:00
|
|
|
using OpenSim.Region.Framework.Interfaces;
|
|
|
|
using OpenSim.Region.Framework.Scenes;
|
2009-08-29 04:54:38 +00:00
|
|
|
using OpenSim.Services.Interfaces;
|
2009-01-19 18:33:25 +00:00
|
|
|
|
2009-02-09 09:16:15 +00:00
|
|
|
namespace OpenSim.Region.CoreModules.Agent.TextureSender
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
public delegate void J2KDecodeDelegate(UUID assetID);
|
2009-08-29 04:29:54 +00:00
|
|
|
|
2012-11-13 16:27:51 +00:00
|
|
|
[Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "J2KDecoderModule")]
|
2012-11-10 16:10:43 +00:00
|
|
|
public class J2KDecoderModule : ISharedRegionModule, IJ2KDecoder
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
2009-01-19 18:33:25 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
/// <summary>Temporarily holds deserialized layer data information in memory</summary>
|
|
|
|
private readonly ExpiringCache<UUID, OpenJPEG.J2KLayerInfo[]> m_decodedCache = new ExpiringCache<UUID,OpenJPEG.J2KLayerInfo[]>();
|
|
|
|
/// <summary>List of client methods to notify of results of decode</summary>
|
|
|
|
private readonly Dictionary<UUID, List<DecodedCallback>> m_notifyList = new Dictionary<UUID, List<DecodedCallback>>();
|
|
|
|
/// <summary>Cache that will store decoded JPEG2000 layer boundary data</summary>
|
2016-12-29 12:34:09 +00:00
|
|
|
private IAssetCache m_cache;
|
|
|
|
private IAssetCache Cache
|
2012-11-11 21:49:07 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (m_cache == null)
|
2016-12-29 12:34:09 +00:00
|
|
|
m_cache = m_scene.RequestModuleInterface<IAssetCache>();
|
2012-11-11 21:49:07 +00:00
|
|
|
|
|
|
|
return m_cache;
|
|
|
|
}
|
|
|
|
}
|
2009-09-30 19:18:22 +00:00
|
|
|
/// <summary>Reference to a scene (doesn't matter which one as long as it can load the cache module)</summary>
|
2012-11-10 16:10:43 +00:00
|
|
|
private UUID m_CreatorID = UUID.Zero;
|
2009-09-30 19:18:22 +00:00
|
|
|
private Scene m_scene;
|
2009-01-19 18:33:25 +00:00
|
|
|
|
2012-11-10 16:10:43 +00:00
|
|
|
#region ISharedRegionModule
|
2009-01-19 18:33:25 +00:00
|
|
|
|
2010-03-08 20:29:26 +00:00
|
|
|
private bool m_useCSJ2K = true;
|
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
public string Name { get { return "J2KDecoderModule"; } }
|
2009-01-19 18:33:25 +00:00
|
|
|
|
2009-01-22 01:33:46 +00:00
|
|
|
public J2KDecoderModule()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-10 16:10:43 +00:00
|
|
|
public void Initialise(IConfigSource source)
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2010-03-08 20:29:26 +00:00
|
|
|
IConfig startupConfig = source.Configs["Startup"];
|
|
|
|
if (startupConfig != null)
|
|
|
|
{
|
|
|
|
m_useCSJ2K = startupConfig.GetBoolean("UseCSJ2K", m_useCSJ2K);
|
|
|
|
}
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 16:10:43 +00:00
|
|
|
public void AddRegion(Scene scene)
|
|
|
|
{
|
|
|
|
if (m_scene == null)
|
|
|
|
{
|
|
|
|
m_scene = scene;
|
|
|
|
m_CreatorID = scene.RegionInfo.RegionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
scene.RegisterModuleInterface<IJ2KDecoder>(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveRegion(Scene scene)
|
|
|
|
{
|
|
|
|
if (m_scene == scene)
|
|
|
|
m_scene = null;
|
|
|
|
}
|
|
|
|
|
2009-01-19 18:33:25 +00:00
|
|
|
public void PostInitialise()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-10 16:10:43 +00:00
|
|
|
public void RegionLoaded(Scene scene)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public Type ReplaceableInterface
|
|
|
|
{
|
|
|
|
get { return null; }
|
|
|
|
}
|
|
|
|
|
2012-11-12 19:18:20 +00:00
|
|
|
#endregion Region Module interface
|
2009-01-19 18:33:25 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
#region IJ2KDecoder
|
2009-01-19 18:33:25 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
public void BeginDecode(UUID assetID, byte[] j2kData, DecodedCallback callback)
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
OpenJPEG.J2KLayerInfo[] result;
|
2009-01-19 18:33:25 +00:00
|
|
|
|
|
|
|
// If it's cached, return the cached results
|
2009-09-30 19:18:22 +00:00
|
|
|
if (m_decodedCache.TryGetValue(assetID, out result))
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2012-01-04 21:40:15 +00:00
|
|
|
// m_log.DebugFormat(
|
|
|
|
// "[J2KDecoderModule]: Returning existing cached {0} layers j2k decode for {1}",
|
|
|
|
// result.Length, assetID);
|
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
callback(assetID, result);
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
// Not cached, we need to decode it.
|
2009-01-19 18:33:25 +00:00
|
|
|
// Add to notify list and start decoding.
|
|
|
|
// Next request for this asset while it's decoding will only be added to the notify list
|
|
|
|
// once this is decoded, requests will be served from the cache and all clients in the notifylist will be updated
|
|
|
|
bool decode = false;
|
|
|
|
lock (m_notifyList)
|
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
if (m_notifyList.ContainsKey(assetID))
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
m_notifyList[assetID].Add(callback);
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
List<DecodedCallback> notifylist = new List<DecodedCallback>();
|
2009-09-30 19:18:22 +00:00
|
|
|
notifylist.Add(callback);
|
|
|
|
m_notifyList.Add(assetID, notifylist);
|
2009-01-19 18:33:25 +00:00
|
|
|
decode = true;
|
|
|
|
}
|
|
|
|
}
|
2009-09-30 19:18:22 +00:00
|
|
|
|
2009-01-19 18:33:25 +00:00
|
|
|
// Do Decode!
|
|
|
|
if (decode)
|
2014-11-04 00:55:48 +00:00
|
|
|
Util.FireAndForget(delegate { Decode(assetID, j2kData); }, null, "J2KDecoderModule.BeginDecode");
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 22:40:49 +00:00
|
|
|
public bool Decode(UUID assetID, byte[] j2kData)
|
2009-01-23 11:00:36 +00:00
|
|
|
{
|
2012-01-05 22:54:33 +00:00
|
|
|
OpenJPEG.J2KLayerInfo[] layers;
|
|
|
|
int components;
|
|
|
|
return Decode(assetID, j2kData, out layers, out components);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Decode(UUID assetID, byte[] j2kData, out OpenJPEG.J2KLayerInfo[] layers, out int components)
|
|
|
|
{
|
|
|
|
return DoJ2KDecode(assetID, j2kData, out layers, out components);
|
2009-01-23 11:00:36 +00:00
|
|
|
}
|
|
|
|
|
2014-01-19 15:32:41 +00:00
|
|
|
public Image DecodeToImage(byte[] j2kData)
|
|
|
|
{
|
|
|
|
if (m_useCSJ2K)
|
|
|
|
return J2kImage.FromBytes(j2kData);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ManagedImage mimage;
|
|
|
|
Image image;
|
|
|
|
if (OpenJPEG.DecodeToImage(j2kData, out mimage, out image))
|
|
|
|
{
|
|
|
|
mimage = null;
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
#endregion IJ2KDecoder
|
2009-01-19 18:33:25 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Decode Jpeg2000 Asset Data
|
|
|
|
/// </summary>
|
2009-09-30 19:18:22 +00:00
|
|
|
/// <param name="assetID">UUID of Asset</param>
|
|
|
|
/// <param name="j2kData">JPEG2000 data</param>
|
2012-01-05 22:54:33 +00:00
|
|
|
/// <param name="layers">layer data</param>
|
|
|
|
/// <param name="components">number of components</param>
|
|
|
|
/// <returns>true if decode was successful. false otherwise.</returns>
|
|
|
|
private bool DoJ2KDecode(UUID assetID, byte[] j2kData, out OpenJPEG.J2KLayerInfo[] layers, out int components)
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2012-01-02 19:46:30 +00:00
|
|
|
// m_log.DebugFormat(
|
|
|
|
// "[J2KDecoderModule]: Doing J2K decoding of {0} bytes for asset {1}", j2kData.Length, assetID);
|
|
|
|
|
2012-01-05 22:40:49 +00:00
|
|
|
bool decodedSuccessfully = true;
|
|
|
|
|
2010-03-08 01:03:56 +00:00
|
|
|
//int DecodeTime = 0;
|
|
|
|
//DecodeTime = Environment.TickCount;
|
2012-01-05 22:54:33 +00:00
|
|
|
|
|
|
|
// We don't get this from CSJ2K. Is it relevant?
|
|
|
|
components = 0;
|
2009-01-19 18:33:25 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
if (!TryLoadCacheForAsset(assetID, out layers))
|
2009-01-21 11:16:33 +00:00
|
|
|
{
|
2010-03-08 20:29:26 +00:00
|
|
|
if (m_useCSJ2K)
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2010-03-08 01:03:56 +00:00
|
|
|
try
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2014-06-01 14:39:11 +00:00
|
|
|
List<int> layerStarts;
|
|
|
|
using (MemoryStream ms = new MemoryStream(j2kData))
|
|
|
|
{
|
|
|
|
layerStarts = CSJ2K.J2kImage.GetLayerBoundaries(ms);
|
|
|
|
}
|
2009-01-21 11:16:33 +00:00
|
|
|
|
2010-03-08 01:03:56 +00:00
|
|
|
if (layerStarts != null && layerStarts.Count > 0)
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2010-03-08 01:03:56 +00:00
|
|
|
layers = new OpenJPEG.J2KLayerInfo[layerStarts.Count];
|
2009-09-30 18:26:26 +00:00
|
|
|
|
2010-03-08 01:03:56 +00:00
|
|
|
for (int i = 0; i < layerStarts.Count; i++)
|
|
|
|
{
|
|
|
|
OpenJPEG.J2KLayerInfo layer = new OpenJPEG.J2KLayerInfo();
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2010-03-08 01:03:56 +00:00
|
|
|
if (i == 0)
|
|
|
|
layer.Start = 0;
|
|
|
|
else
|
|
|
|
layer.Start = layerStarts[i];
|
2009-09-30 19:18:22 +00:00
|
|
|
|
2010-03-08 01:03:56 +00:00
|
|
|
if (i == layerStarts.Count - 1)
|
|
|
|
layer.End = j2kData.Length;
|
|
|
|
else
|
|
|
|
layer.End = layerStarts[i + 1] - 1;
|
|
|
|
|
|
|
|
layers[i] = layer;
|
|
|
|
}
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-08 01:03:56 +00:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
m_log.Warn("[J2KDecoderModule]: CSJ2K threw an exception decoding texture " + assetID + ": " + ex.Message);
|
2012-01-05 22:40:49 +00:00
|
|
|
decodedSuccessfully = false;
|
2010-03-08 01:03:56 +00:00
|
|
|
}
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|
2010-03-08 01:03:56 +00:00
|
|
|
else
|
2009-09-30 19:18:22 +00:00
|
|
|
{
|
2010-03-08 01:03:56 +00:00
|
|
|
if (!OpenJPEG.DecodeLayerBoundaries(j2kData, out layers, out components))
|
|
|
|
{
|
|
|
|
m_log.Warn("[J2KDecoderModule]: OpenJPEG failed to decode texture " + assetID);
|
2012-01-05 22:40:49 +00:00
|
|
|
decodedSuccessfully = false;
|
2010-03-08 01:03:56 +00:00
|
|
|
}
|
2009-09-30 19:18:22 +00:00
|
|
|
}
|
2009-09-30 18:26:26 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
if (layers == null || layers.Length == 0)
|
|
|
|
{
|
|
|
|
m_log.Warn("[J2KDecoderModule]: Failed to decode layer data for texture " + assetID + ", guessing sane defaults");
|
|
|
|
// Layer decoding completely failed. Guess at sane defaults for the layer boundaries
|
|
|
|
layers = CreateDefaultLayers(j2kData.Length);
|
2012-01-05 22:40:49 +00:00
|
|
|
decodedSuccessfully = false;
|
2009-09-30 19:18:22 +00:00
|
|
|
}
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
// Cache Decoded layers
|
|
|
|
SaveFileCacheForAsset(assetID, layers);
|
|
|
|
}
|
2017-01-05 19:07:37 +00:00
|
|
|
|
2009-01-19 18:33:25 +00:00
|
|
|
// Notify Interested Parties
|
|
|
|
lock (m_notifyList)
|
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
if (m_notifyList.ContainsKey(assetID))
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
foreach (DecodedCallback d in m_notifyList[assetID])
|
2009-01-19 18:33:25 +00:00
|
|
|
{
|
|
|
|
if (d != null)
|
2009-09-30 19:18:22 +00:00
|
|
|
d.DynamicInvoke(assetID, layers);
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|
2009-09-30 19:18:22 +00:00
|
|
|
m_notifyList.Remove(assetID);
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-05 22:40:49 +00:00
|
|
|
|
|
|
|
return decodedSuccessfully;
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|
2009-08-29 04:54:38 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
private OpenJPEG.J2KLayerInfo[] CreateDefaultLayers(int j2kLength)
|
2009-08-29 04:54:38 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
OpenJPEG.J2KLayerInfo[] layers = new OpenJPEG.J2KLayerInfo[5];
|
|
|
|
|
|
|
|
for (int i = 0; i < layers.Length; i++)
|
|
|
|
layers[i] = new OpenJPEG.J2KLayerInfo();
|
|
|
|
|
|
|
|
// These default layer sizes are based on a small sampling of real-world texture data
|
|
|
|
// with extra padding thrown in for good measure. This is a worst case fallback plan
|
|
|
|
// and may not gracefully handle all real world data
|
|
|
|
layers[0].Start = 0;
|
|
|
|
layers[1].Start = (int)((float)j2kLength * 0.02f);
|
|
|
|
layers[2].Start = (int)((float)j2kLength * 0.05f);
|
|
|
|
layers[3].Start = (int)((float)j2kLength * 0.20f);
|
|
|
|
layers[4].Start = (int)((float)j2kLength * 0.50f);
|
|
|
|
|
|
|
|
layers[0].End = layers[1].Start - 1;
|
|
|
|
layers[1].End = layers[2].Start - 1;
|
|
|
|
layers[2].End = layers[3].Start - 1;
|
|
|
|
layers[3].End = layers[4].Start - 1;
|
|
|
|
layers[4].End = j2kLength;
|
|
|
|
|
|
|
|
return layers;
|
2009-08-29 04:54:38 +00:00
|
|
|
}
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
private void SaveFileCacheForAsset(UUID AssetId, OpenJPEG.J2KLayerInfo[] Layers)
|
2009-01-22 01:33:46 +00:00
|
|
|
{
|
2019-11-18 03:18:23 +00:00
|
|
|
m_decodedCache.AddOrUpdate(AssetId, Layers, TimeSpan.FromMinutes(1));
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2012-11-11 21:49:07 +00:00
|
|
|
if (Cache != null)
|
2009-01-22 01:33:46 +00:00
|
|
|
{
|
2009-11-05 21:10:58 +00:00
|
|
|
string assetID = "j2kCache_" + AssetId.ToString();
|
|
|
|
|
2012-11-10 16:10:43 +00:00
|
|
|
AssetBase layerDecodeAsset = new AssetBase(assetID, assetID, (sbyte)AssetType.Notecard, m_CreatorID.ToString());
|
2009-09-30 19:18:22 +00:00
|
|
|
layerDecodeAsset.Local = true;
|
|
|
|
layerDecodeAsset.Temporary = true;
|
|
|
|
|
|
|
|
#region Serialize Layer Data
|
|
|
|
|
2009-01-22 01:33:46 +00:00
|
|
|
StringBuilder stringResult = new StringBuilder();
|
|
|
|
string strEnd = "\n";
|
|
|
|
for (int i = 0; i < Layers.Length; i++)
|
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
if (i == Layers.Length - 1)
|
|
|
|
strEnd = String.Empty;
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2009-07-25 15:49:10 +00:00
|
|
|
stringResult.AppendFormat("{0}|{1}|{2}{3}", Layers[i].Start, Layers[i].End, Layers[i].End - Layers[i].Start, strEnd);
|
2009-01-22 01:33:46 +00:00
|
|
|
}
|
2009-09-30 18:26:26 +00:00
|
|
|
|
2009-10-03 01:31:08 +00:00
|
|
|
layerDecodeAsset.Data = Util.UTF8.GetBytes(stringResult.ToString());
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
#endregion Serialize Layer Data
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2012-11-11 21:49:07 +00:00
|
|
|
Cache.Cache(layerDecodeAsset);
|
2009-09-30 18:26:26 +00:00
|
|
|
}
|
2009-09-30 19:18:22 +00:00
|
|
|
}
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
bool TryLoadCacheForAsset(UUID AssetId, out OpenJPEG.J2KLayerInfo[] Layers)
|
|
|
|
{
|
|
|
|
if (m_decodedCache.TryGetValue(AssetId, out Layers))
|
2009-01-22 01:33:46 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
return true;
|
2009-01-22 01:33:46 +00:00
|
|
|
}
|
2012-11-11 21:49:07 +00:00
|
|
|
else if (Cache != null)
|
2009-01-22 01:33:46 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
string assetName = "j2kCache_" + AssetId.ToString();
|
2017-02-01 16:31:01 +00:00
|
|
|
AssetBase layerDecodeAsset;
|
|
|
|
Cache.Get(assetName, out layerDecodeAsset);
|
2009-09-30 18:26:26 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
if (layerDecodeAsset != null)
|
2009-01-22 01:33:46 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
#region Deserialize Layer Data
|
2009-09-30 18:26:26 +00:00
|
|
|
|
2009-10-03 01:31:08 +00:00
|
|
|
string readResult = Util.UTF8.GetString(layerDecodeAsset.Data);
|
2009-09-30 19:18:22 +00:00
|
|
|
string[] lines = readResult.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
if (lines.Length == 0)
|
2009-01-22 01:33:46 +00:00
|
|
|
{
|
2009-09-30 19:18:22 +00:00
|
|
|
m_log.Warn("[J2KDecodeCache]: Expiring corrupted layer data (empty) " + assetName);
|
2012-11-11 21:49:07 +00:00
|
|
|
Cache.Expire(assetName);
|
2009-01-22 01:33:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
Layers = new OpenJPEG.J2KLayerInfo[lines.Length];
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
for (int i = 0; i < lines.Length; i++)
|
|
|
|
{
|
|
|
|
string[] elements = lines[i].Split('|');
|
|
|
|
if (elements.Length == 3)
|
|
|
|
{
|
|
|
|
int element1, element2;
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
element1 = Convert.ToInt32(elements[0]);
|
|
|
|
element2 = Convert.ToInt32(elements[1]);
|
|
|
|
}
|
|
|
|
catch (FormatException)
|
|
|
|
{
|
|
|
|
m_log.Warn("[J2KDecodeCache]: Expiring corrupted layer data (format) " + assetName);
|
2012-11-11 21:49:07 +00:00
|
|
|
Cache.Expire(assetName);
|
2009-09-30 19:18:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-29 04:29:54 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
Layers[i] = new OpenJPEG.J2KLayerInfo();
|
|
|
|
Layers[i].Start = element1;
|
|
|
|
Layers[i].End = element2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_log.Warn("[J2KDecodeCache]: Expiring corrupted layer data (layout) " + assetName);
|
2012-11-11 21:49:07 +00:00
|
|
|
Cache.Expire(assetName);
|
2009-09-30 19:18:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-08-29 04:29:54 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
#endregion Deserialize Layer Data
|
2009-01-22 01:33:46 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
return true;
|
2009-01-22 01:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-29 04:29:54 +00:00
|
|
|
|
2009-09-30 19:18:22 +00:00
|
|
|
return false;
|
2009-08-29 04:29:54 +00:00
|
|
|
}
|
2009-01-22 01:33:46 +00:00
|
|
|
}
|
2009-01-19 18:33:25 +00:00
|
|
|
}
|