public void StoreAsset(Asset asset) { if (asset is AssetTexture) { AssetTexture texture = (AssetTexture)asset; if (texture.DecodeLayerBoundaries()) { lock (AssetStore) AssetStore[asset.AssetID] = texture; if (!asset.Temporary) { SaveAsset(texture); } } else { Logger.Log(String.Format("Failed to decoded layer boundaries on texture {0}", texture.AssetID), Helpers.LogLevel.Warning); } } else { if (asset.Decode()) { lock (AssetStore) AssetStore[asset.AssetID] = asset; if (!asset.Temporary) { SaveAsset(asset); } } else { Logger.Log(String.Format("Failed to decode {0} asset {1}", asset.AssetType, asset.AssetID), Helpers.LogLevel.Warning); } } }
/// <summary> /// Decode Jpeg2000 Asset Data /// </summary> /// <param name="AssetId">UUID of Asset</param> /// <param name="j2kdata">Byte Array Asset Data </param> private bool DoJ2KDecode(UUID assetID, byte[] j2kdata, out OpenJPEG.J2KLayerInfo[] layers) { int DecodeTime = 0; DecodeTime = Environment.TickCount; bool decodedSuccessfully = true; layers = new OpenJPEG.J2KLayerInfo[0]; // Dummy result for if it fails. Informs that there's only full quality try { AssetTexture texture = new AssetTexture(assetID, j2kdata); bool sane = false; if (texture.DecodeLayerBoundaries()) { sane = true; // Sanity check all of the layers for (int i = 0; i < texture.LayerInfo.Length; i++) { if (texture.LayerInfo[i].End > texture.AssetData.Length) { sane = false; break; } } } if (sane) { m_log.InfoFormat("[J2KDecoderModule]: {0} Decode Time: {1}", Environment.TickCount - DecodeTime, assetID); layers = texture.LayerInfo; } else { m_log.Warn("[J2KDecoderModule]: Failed to decode layer data for texture " + assetID + ", guessing sane defaults"); decodedSuccessfully = false; // Layer decoding completely failed. Guess at sane defaults for the layer boundaries layers = CreateDefaultLayers(j2kdata.Length); } fCache.SaveCacheForAsset(assetID, layers); texture = null; // dereference and dispose of ManagedImage } catch (DllNotFoundException) { m_log.Error( "[J2KDecoderModule]: OpenJpeg is not installed properly. Decoding disabled! This will slow down texture performance! Often times this is because of an old version of GLIBC. You must have version 2.4 or above!"); OpenJpegFail = true; decodedSuccessfully = false; } catch (Exception ex) { m_log.WarnFormat( "[J2KDecoderModule]: JPEG2000 texture decoding threw an exception for {0}, {1}", assetID, ex); decodedSuccessfully = false; } return(decodedSuccessfully); }
/// <summary> /// Decode Jpeg2000 Asset Data /// </summary> /// <param name="AssetId">UUID of Asset</param> /// <param name="j2kdata">Byte Array Asset Data </param> private void doJ2kDecode(UUID AssetId, byte[] j2kdata) { int DecodeTime = 0; DecodeTime = Environment.TickCount; OpenJPEG.J2KLayerInfo[] layers = new OpenJPEG.J2KLayerInfo[0]; // Dummy result for if it fails. Informs that there's only full quality if (!OpenJpegFail) { if (!fCache.TryLoadCacheForAsset(AssetId, out layers)) { try { AssetTexture texture = new AssetTexture(AssetId, j2kdata); if (texture.DecodeLayerBoundaries()) { bool sane = true; // Sanity check all of the layers for (int i = 0; i < texture.LayerInfo.Length; i++) { if (texture.LayerInfo[i].End > texture.AssetData.Length) { sane = false; break; } } if (sane) { layers = texture.LayerInfo; fCache.SaveFileCacheForAsset(AssetId, layers); // Write out decode time m_log.InfoFormat("[J2KDecoderModule]: {0} Decode Time: {1}", Environment.TickCount - DecodeTime, AssetId); } else { m_log.WarnFormat( "[J2KDecoderModule]: JPEG2000 texture decoding succeeded, but sanity check failed for {0}", AssetId); } } else { /* Random rnd = new Random(); // scramble ends for test for (int i = 0; i < texture.LayerInfo.Length; i++) { texture.LayerInfo[i].End = rnd.Next(999999); } */ // Try to do some heuristics error correction! Yeah. bool sane2Heuristics = true; if (texture.Image == null) sane2Heuristics = false; if (texture.LayerInfo == null) sane2Heuristics = false; if (sane2Heuristics) { if (texture.LayerInfo.Length == 0) sane2Heuristics = false; } if (sane2Heuristics) { // Last layer start is less then the end of the file and last layer start is greater then 0 if (texture.LayerInfo[texture.LayerInfo.Length - 1].Start < texture.AssetData.Length && texture.LayerInfo[texture.LayerInfo.Length - 1].Start > 0) { } else { sane2Heuristics = false; } } if (sane2Heuristics) { int start = 0; // try to fix it by using consistant data in the start field for (int i = 0; i < texture.LayerInfo.Length; i++) { if (i == 0) start = 0; if (i == texture.LayerInfo.Length - 1) texture.LayerInfo[i].End = texture.AssetData.Length; else texture.LayerInfo[i].End = texture.LayerInfo[i + 1].Start - 1; // in this case, the end of the next packet is less then the start of the last packet // after we've attempted to fix it which means the start of the last packet is borked // there's no recovery from this if (texture.LayerInfo[i].End < start) { sane2Heuristics = false; break; } if (texture.LayerInfo[i].End < 0 || texture.LayerInfo[i].End > texture.AssetData.Length) { sane2Heuristics = false; break; } if (texture.LayerInfo[i].Start < 0 || texture.LayerInfo[i].Start > texture.AssetData.Length) { sane2Heuristics = false; break; } start = texture.LayerInfo[i].Start; } } if (sane2Heuristics) { layers = texture.LayerInfo; fCache.SaveFileCacheForAsset(AssetId, layers); // Write out decode time m_log.InfoFormat("[J2KDecoderModule]: HEURISTICS SUCCEEDED {0} Decode Time: {1}", Environment.TickCount - DecodeTime, AssetId); } else { m_log.WarnFormat("[J2KDecoderModule]: JPEG2000 texture decoding failed for {0}. Is this a texture? is it J2K?", AssetId); } } texture = null; // dereference and dispose of ManagedImage } catch (DllNotFoundException) { m_log.Error( "[J2KDecoderModule]: OpenJpeg is not installed properly. Decoding disabled! This will slow down texture performance! Often times this is because of an old version of GLIBC. You must have version 2.4 or above!"); OpenJpegFail = true; } catch (Exception ex) { m_log.WarnFormat( "[J2KDecoderModule]: JPEG2000 texture decoding threw an exception for {0}, {1}", AssetId, ex); } } } // Cache Decoded layers lock (m_cacheddecode) { if (m_cacheddecode.ContainsKey(AssetId)) m_cacheddecode.Remove(AssetId); m_cacheddecode.Add(AssetId, layers); } // Notify Interested Parties lock (m_notifyList) { if (m_notifyList.ContainsKey(AssetId)) { foreach (DecodedCallback d in m_notifyList[AssetId]) { if (d != null) d.DynamicInvoke(AssetId, layers); } m_notifyList.Remove(AssetId); } } }
/// <summary> /// Decode Jpeg2000 Asset Data /// </summary> /// <param name="AssetId">UUID of Asset</param> /// <param name="j2kdata">Byte Array Asset Data </param> private bool DoJ2KDecode(UUID assetID, byte[] j2kdata, out OpenJPEG.J2KLayerInfo[] layers) { int DecodeTime = 0; DecodeTime = Environment.TickCount; bool decodedSuccessfully = true; layers = new OpenJPEG.J2KLayerInfo[0]; // Dummy result for if it fails. Informs that there's only full quality try { AssetTexture texture = new AssetTexture(assetID, j2kdata); bool sane = false; if (texture.DecodeLayerBoundaries()) { sane = true; // Sanity check all of the layers for (int i = 0; i < texture.LayerInfo.Length; i++) { if (texture.LayerInfo[i].End > texture.AssetData.Length) { sane = false; break; } } } if (sane) { m_log.InfoFormat("[J2KDecoderModule]: {0} Decode Time: {1}", Environment.TickCount - DecodeTime, assetID); layers = texture.LayerInfo; } else { m_log.Warn("[J2KDecoderModule]: Failed to decode layer data for texture " + assetID + ", guessing sane defaults"); decodedSuccessfully = false; // Layer decoding completely failed. Guess at sane defaults for the layer boundaries layers = CreateDefaultLayers(j2kdata.Length); } fCache.SaveCacheForAsset(assetID, layers); texture = null; // dereference and dispose of ManagedImage } catch (DllNotFoundException) { m_log.Error( "[J2KDecoderModule]: OpenJpeg is not installed properly. Decoding disabled! This will slow down texture performance! Often times this is because of an old version of GLIBC. You must have version 2.4 or above!"); OpenJpegFail = true; decodedSuccessfully = false; } catch (Exception ex) { m_log.WarnFormat( "[J2KDecoderModule]: JPEG2000 texture decoding threw an exception for {0}, {1}", assetID, ex); decodedSuccessfully = false; } return (decodedSuccessfully); }
/// <summary> /// Decode Jpeg2000 Asset Data /// </summary> /// <param name="AssetId">UUID of Asset</param> /// <param name="j2kdata">Byte Array Asset Data </param> private void doJ2kDecode(UUID AssetId, byte[] j2kdata) { int DecodeTime = 0; DecodeTime = Environment.TickCount; OpenJPEG.J2KLayerInfo[] layers = new OpenJPEG.J2KLayerInfo[0]; // Dummy result for if it fails. Informs that there's only full quality if (!OpenJpegFail) { if (!fCache.TryLoadCacheForAsset(AssetId, out layers)) { try { AssetTexture texture = new AssetTexture(AssetId, j2kdata); if (texture.DecodeLayerBoundaries()) { bool sane = true; // Sanity check all of the layers for (int i = 0; i < texture.LayerInfo.Length; i++) { if (texture.LayerInfo[i].End > texture.AssetData.Length) { sane = false; break; } } if (sane) { layers = texture.LayerInfo; fCache.SaveFileCacheForAsset(AssetId, layers); // Write out decode time m_log.InfoFormat("[J2KDecoderModule]: {0} Decode Time: {1}", Environment.TickCount - DecodeTime, AssetId); } else { m_log.WarnFormat( "[J2KDecoderModule]: JPEG2000 texture decoding succeeded, but sanity check failed for {0}", AssetId); } } else { /* * Random rnd = new Random(); * // scramble ends for test * for (int i = 0; i < texture.LayerInfo.Length; i++) * { * texture.LayerInfo[i].End = rnd.Next(999999); * } */ // Try to do some heuristics error correction! Yeah. bool sane2Heuristics = true; if (texture.Image == null) { sane2Heuristics = false; } if (texture.LayerInfo == null) { sane2Heuristics = false; } if (sane2Heuristics) { if (texture.LayerInfo.Length == 0) { sane2Heuristics = false; } } if (sane2Heuristics) { // Last layer start is less then the end of the file and last layer start is greater then 0 if (texture.LayerInfo[texture.LayerInfo.Length - 1].Start < texture.AssetData.Length && texture.LayerInfo[texture.LayerInfo.Length - 1].Start > 0) { } else { sane2Heuristics = false; } } if (sane2Heuristics) { int start = 0; // try to fix it by using consistant data in the start field for (int i = 0; i < texture.LayerInfo.Length; i++) { if (i == 0) { start = 0; } if (i == texture.LayerInfo.Length - 1) { texture.LayerInfo[i].End = texture.AssetData.Length; } else { texture.LayerInfo[i].End = texture.LayerInfo[i + 1].Start - 1; } // in this case, the end of the next packet is less then the start of the last packet // after we've attempted to fix it which means the start of the last packet is borked // there's no recovery from this if (texture.LayerInfo[i].End < start) { sane2Heuristics = false; break; } if (texture.LayerInfo[i].End <0 || texture.LayerInfo[i].End> texture.AssetData.Length) { sane2Heuristics = false; break; } if (texture.LayerInfo[i].Start <0 || texture.LayerInfo[i].Start> texture.AssetData.Length) { sane2Heuristics = false; break; } start = texture.LayerInfo[i].Start; } } if (sane2Heuristics) { layers = texture.LayerInfo; fCache.SaveFileCacheForAsset(AssetId, layers); // Write out decode time m_log.InfoFormat("[J2KDecoderModule]: HEURISTICS SUCCEEDED {0} Decode Time: {1}", Environment.TickCount - DecodeTime, AssetId); } else { m_log.WarnFormat("[J2KDecoderModule]: JPEG2000 texture decoding failed for {0}. Is this a texture? is it J2K?", AssetId); } } texture = null; // dereference and dispose of ManagedImage } catch (DllNotFoundException) { m_log.Error( "[J2KDecoderModule]: OpenJpeg is not installed properly. Decoding disabled! This will slow down texture performance! Often times this is because of an old version of GLIBC. You must have version 2.4 or above!"); OpenJpegFail = true; } catch (Exception ex) { m_log.WarnFormat( "[J2KDecoderModule]: JPEG2000 texture decoding threw an exception for {0}, {1}", AssetId, ex); } } } // Cache Decoded layers lock (m_cacheddecode) { if (!m_cacheddecode.ContainsKey(AssetId)) { m_cacheddecode.Add(AssetId, layers); } } // Notify Interested Parties lock (m_notifyList) { if (m_notifyList.ContainsKey(AssetId)) { foreach (DecodedCallback d in m_notifyList[AssetId]) { if (d != null) { d.DynamicInvoke(AssetId, layers); } } m_notifyList.Remove(AssetId); } } }