Exemplo n.º 1
0
        /// <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">JPEG2000 data</param>
        /// <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)
        {
            bool decodedSuccessfully = true;
            int  DecodeTime          = Environment.TickCount;

            layers = new OpenJPEG.J2KLayerInfo[0]; // Dummy result for if it fails.  Informs that there's only full quality

            try
            {
                using (MemoryStream ms = new MemoryStream(j2kData))
                {
                    List <int> layerStarts = CSJ2K.J2kImage.GetLayerBoundaries(ms);

                    if (layerStarts != null && layerStarts.Count > 0)
                    {
                        layers = new OpenJPEG.J2KLayerInfo[layerStarts.Count];

                        for (int i = 0; i < layerStarts.Count; i++)
                        {
                            OpenJPEG.J2KLayerInfo layer = new OpenJPEG.J2KLayerInfo();

                            if (i == 0)
                            {
                                layer.Start = 0;
                            }
                            else
                            {
                                layer.Start = layerStarts[i];
                            }

                            if (i == layerStarts.Count - 1)
                            {
                                layer.End = j2kData.Length;
                            }
                            else
                            {
                                layer.End = layerStarts[i + 1] - 1;
                            }

                            layers[i] = layer;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                m_log.Warn("[J2KDecoderModule]: CSJ2K threw an exception decoding texture " + assetID + ": " + ex.Message);
                decodedSuccessfully = false;
            }

            if (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);
                decodedSuccessfully = false;
            }
            else
            {
                int elapsed = Environment.TickCount - DecodeTime;
                if (elapsed >= 50)
                {
                    m_log.InfoFormat("[J2KDecoderModule]: {0} Decode Time: {1}", elapsed, assetID);
                }
                // Cache Decoded layers
                fCache.SaveCacheForAsset(assetID, layers);
            }

            return(decodedSuccessfully);
        }