Example #1
0
        private void Assets_OnImageReceived(TextureRequestState state, AssetTexture assetTexture)
        {
            lock (OutfitAssets)
            {
                if (OutfitAssets.Contains(assetTexture.AssetID))
                {
                    if (state == TextureRequestState.Finished)
                    {
                        try
                        {
                            File.WriteAllBytes(assetTexture.AssetID + ".jp2", assetTexture.AssetData);
                            Console.WriteLine("Wrote JPEG2000 image " + assetTexture.AssetID + ".jp2");

                            ManagedImage imgData;
                            OpenJPEG.DecodeToImage(assetTexture.AssetData, out imgData);
                            byte[] tgaFile = imgData.ExportTGA();
                            File.WriteAllBytes(assetTexture.AssetID + ".tga", tgaFile);
                            Console.WriteLine("Wrote TGA image " + assetTexture.AssetID + ".tga");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                    }
                    else
                    {
                        Console.WriteLine("Failed to download image " + assetTexture.AssetID);
                    }

                    OutfitAssets.Remove(assetTexture.AssetID);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Decodes the JPEG2000 data in <code>AssetData</code> to the
        /// <seealso cref="ManagedImage"/> object <seealso cref="Image"/>
        /// </summary>
        /// <returns>True if the decoding was successful, otherwise false</returns>
        public override bool Decode()
        {
            if (AssetData != null && AssetData.Length > 0)
            {
                this.Components = 0;

                if (OpenJPEG.DecodeToImage(AssetData, out Image))
                {
                    if ((Image.Channels & ManagedImage.ImageChannels.Color) != 0)
                    {
                        Components += 3;
                    }
                    if ((Image.Channels & ManagedImage.ImageChannels.Gray) != 0)
                    {
                        ++Components;
                    }
                    if ((Image.Channels & ManagedImage.ImageChannels.Bump) != 0)
                    {
                        ++Components;
                    }
                    if ((Image.Channels & ManagedImage.ImageChannels.Alpha) != 0)
                    {
                        ++Components;
                    }

                    return(true);
                }
            }

            return(false);
        }
Example #3
0
        private void GroupProfileHandler(object sender, GroupProfileEventArgs e)
        {
            Profile = e.Group;

            if (Group.InsigniaID != UUID.Zero)
            {
                Client.Assets.RequestImage(Group.InsigniaID, ImageType.Normal,
                                           delegate(TextureRequestState state, AssetTexture assetTexture)
                {
                    ManagedImage imgData;
                    Image bitmap;

                    if (state != TextureRequestState.Timeout || state != TextureRequestState.NotFound)
                    {
                        OpenJPEG.DecodeToImage(assetTexture.AssetData, out imgData, out bitmap);
                        picInsignia.Image = bitmap;
                        UpdateInsigniaProgressText("Progress...");
                    }
                    if (state == TextureRequestState.Finished)
                    {
                        UpdateInsigniaProgressText("");
                    }
                }, true);
            }

            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(UpdateProfile));
            }
        }
Example #4
0
        private Bitmap fetchTexture(UUID id)
        {
            AssetBase asset = m_scene.AssetService.Get(id.ToString());

            //m_log.DebugFormat("Fetched texture {0}, found: {1}", id, asset != null);
            if (asset == null)
            {
                return(null);
            }

            ManagedImage managedImage;
            Image        image;

            try
            {
                if (OpenJPEG.DecodeToImage(asset.Data, out managedImage, out image))
                {
                    return(new Bitmap(image));
                }
            }
            catch (DllNotFoundException)
            {
                m_log.ErrorFormat("[TexturedMapTileRenderer]: OpenJpeg is not installed correctly on this system.   Asset Data is emtpy for {0}", id);
            }
            catch (IndexOutOfRangeException)
            {
                m_log.ErrorFormat("[TexturedMapTileRenderer]: OpenJpeg was unable to encode this.   Asset Data is emtpy for {0}", id);
            }
            catch (Exception)
            {
                m_log.ErrorFormat("[TexturedMapTileRenderer]: OpenJpeg was unable to encode this.   Asset Data is emtpy for {0}", id);
            }
            return(null);
        }
Example #5
0
        void Network_OnCurrentSimChanged(object sender, SimChangedEventArgs e)
        {
            if (_Client.Network.Connected)
            {
                return;
            }

            GridRegion region;

            if (Client.Grid.GetGridRegion(Client.Network.CurrentSim.Name, GridLayerType.Objects, out region))
            {
                SetMapLayer(null);

                _MapImageID = region.MapImageID;
                ManagedImage nullImage;

                Client.Assets.RequestImage(_MapImageID, ImageType.Baked,
                                           delegate(TextureRequestState state, AssetTexture asset)
                {
                    if (state == TextureRequestState.Finished)
                    {
                        OpenJPEG.DecodeToImage(asset.AssetData, out nullImage, out _MapLayer);
                    }
                });
            }
        }
Example #6
0
        //comes in on a separate thread
        private void Assets_OnImageReceived(TextureRequestState image, AssetTexture texture)
        {
            ManagedImage mImg;
            Image        sImage = null;

            if (texture.AssetID != SLImageID && texture.AssetID != FLImageID)
            {
                if (texture.AssetID != PickImageID)
                {
                    return;
                }

                OpenJPEG.DecodeToImage(texture.AssetData, out mImg, out sImage);
                System.Drawing.Image decodedImage1 = sImage;

                if (decodedImage1 != null)
                {
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        pictureBox1.Image = decodedImage1;
                        loadwait2.Visible = false;
                    }));

                    instance.ImageCache.AddImage(texture.AssetID, decodedImage1);
                }
            }
            else
            {
                //System.Drawing.Image decodedImage = ImageHelper.Decode(image.AssetData);
                //System.Drawing.Image decodedImage = OpenJPEGNet.OpenJPEG.DecodeToImage(image.AssetData);
                OpenJPEG.DecodeToImage(texture.AssetData, out mImg, out sImage);
                System.Drawing.Image decodedImage = sImage;

                if (decodedImage == null)
                {
                    if (texture.AssetID == SLImageID)
                    {
                        BeginInvoke(new MethodInvoker(SetBlankSLImage));
                    }
                    else if (texture.AssetID == FLImageID)
                    {
                        BeginInvoke(new MethodInvoker(SetBlankFLImage));
                    }

                    return;
                }

                instance.ImageCache.AddImage(texture.AssetID, decodedImage);

                try
                {
                    BeginInvoke(new OnSetProfileImage(SetProfileImage), new object[] { texture.AssetID, decodedImage });
                }
                catch {; }

                //if (image.Success)
                //    picInsignia.Image = OpenJPEGNet.OpenJPEG.DecodeToImage(image.AssetData);
            }
        }
Example #7
0
        /// <summary>
        /// Decodes the JPEG2000 data in <code>AssetData</code> to the
        /// <code>ManagedImage</code> object <code>Image</code>
        /// </summary>
        /// <returns>True if the decoding was successful, otherwise false</returns>
        public override bool Decode()
        {
#if PocketPC
            throw new Exception("OpenJPEG decoding is not supported on the PocketPC");
#else
            return(OpenJPEG.DecodeToImage(AssetData, out Image));
#endif
        }
Example #8
0
 void Assets_OnImageReceived(ImageDownload image, AssetTexture asset)
 {
     if (asset.AssetID == _MapImageID)
     {
         ManagedImage nullImage;
         OpenJPEG.DecodeToImage(asset.AssetData, out nullImage, out _MapLayer);
     }
 }
Example #9
0
        /// <summary>
        /// Fetch a texture and return an OMVA.AssetTexture. The only information initialized
        /// in the AssetTexture is the UUID and the binary data.
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        public override IPromise <Image> FetchTextureAsImage(EntityHandle handle)
        {
            var prom = new Promise <Image>();

            // Don't bother with async -- this call will hang until the asset is fetched
            AssetBase asset = _assetService.Get(handle.ToString());

            if (asset != null)
            {
                Image imageDecoded = null;
                if (asset.IsBinaryAsset && asset.Type == (sbyte)OMV.AssetType.Texture)
                {
                    try {
                        /* Code for using NuGet CSJ2K. Thought it might be better but noticed no difference.
                         * CSJ2K.Util.BitmapImageCreator.Register();
                         * imageDecoded = CSJ2K.J2kImage.FromBytes(asset.Data).As<Bitmap>();
                         */
                        ManagedImage mimage;
                        if (OpenJPEG.DecodeToImage(asset.Data, out mimage, out imageDecoded))
                        {
                            mimage = null;  // 'mimage' is unused so release the reference
                        }
                        else
                        {
                            // Could not decode the image. Odd.
                            imageDecoded = null;
                        }
                        prom.Resolve(imageDecoded);
                    }
                    catch (Exception e) {
                        prom.Reject(new Exception("FetchTextureAsImage: exception decoding JPEG2000 texture. ID=" + handle.ToString()
                                                  + ", e=" + e.ToString()));
                    }
                }
                // THis application overloads the definition of TextureTGA to be a PNG format bytes
                else if (asset.IsBinaryAsset && asset.Type == (sbyte)OMV.AssetType.TextureTGA)
                {
                    using (Stream byteStream = new MemoryStream(asset.Data)) {
                        Bitmap readBitmap = new Bitmap(byteStream);
                        // Doing this clone because of the comment about keeping the stream open for
                        //     the life if the Bitmap in the MS documentation. Odd but making a copy.
                        imageDecoded = (Image)readBitmap.Clone();
                        readBitmap.Dispose();
                    }
                    prom.Resolve(imageDecoded);
                }
                else
                {
                    prom.Reject(new Exception("FetchTextureAsImage: asset was not of type texture. ID=" + handle.ToString()));
                }
            }
            else
            {
                prom.Reject(new Exception("FetchTextureAsImage: could not fetch texture asset. ID=" + handle.ToString()));
            }

            return(prom);
        }
Example #10
0
        public Bitmap LoadBitmap(UUID assetID)
        {
            AssetBase    bmp = m_scene.CommsManager.AssetCache.GetAsset(assetID, true);
            ManagedImage outimg;
            Image        img;

            OpenJPEG.DecodeToImage(bmp.Data, out outimg, out img);

            return(new Bitmap(img));
        }
Example #11
0
        public Bitmap LoadBitmap(UUID assetID)
        {
            AssetBase    bmp = m_scene.AssetService.Get(assetID.ToString());
            ManagedImage outimg;
            Image        img;

            OpenJPEG.DecodeToImage(bmp.Data, out outimg, out img);

            return(new Bitmap(img));
        }
Example #12
0
        public void RegenerateMaptile(string ID, byte[] data)
        {
            MemoryStream imgstream  = new MemoryStream();
            Bitmap       mapTexture = new Bitmap(1, 1);
            ManagedImage managedImage;
            Image        image = (Image)mapTexture;

            try
            {
                // Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular jpeg data

                imgstream = new MemoryStream();

                // Decode image to System.Drawing.Image
                if (OpenJPEG.DecodeToImage(data, out managedImage, out image))
                {
                    // Save to bitmap
                    mapTexture = new Bitmap(image);

                    EncoderParameters myEncoderParameters = new EncoderParameters();
                    myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 95L);

                    // Save bitmap to stream
                    mapTexture.Save(imgstream, GetEncoderInfo("image/jpeg"), myEncoderParameters);

                    // Write the stream to a byte array for output
                    myMapImageJPEG = imgstream.ToArray();
                }
            }
            catch (Exception)
            {
                // Dummy!
                m_log.Warn("[WORLD MAP]: Unable to generate Map image");
            }
            finally
            {
                // Reclaim memory, these are unmanaged resources
                // If we encountered an exception, one or more of these will be null
                if (mapTexture != null)
                {
                    mapTexture.Dispose();
                }

                if (image != null)
                {
                    image.Dispose();
                }

                if (imgstream != null)
                {
                    imgstream.Close();
                    imgstream.Dispose();
                }
            }
        }
Example #13
0
        private void Assets_OnImageReceived(TextureRequestState image, AssetTexture texture)
        {
            if (texture.AssetID == _MapImageID)
            {
                ManagedImage nullImage;
                OpenJPEG.DecodeToImage(texture.AssetData, out nullImage, out _MapLayer);

                //UpdateMiniMap(sim);
                BeginInvoke((MethodInvoker) delegate { UpdateMiniMap(sim); });
            }
        }
Example #14
0
        void Assets_OnImageReceived(ImageDownload image, AssetTexture assetTexture)
        {
            ManagedImage imgData;
            Image        bitmap;

            if (image.Success)
            {
                OpenJPEG.DecodeToImage(image.AssetData, out imgData, out bitmap);
                picInsignia.Image = bitmap;
            }
        }
Example #15
0
        private bool LoadTexture(UUID textureID, ref Image texture, bool removeAlpha)
        {
            if (textureID == UUID.Zero)
            {
                return(false);
            }

            ManualResetEvent gotImage = new ManualResetEvent(false);
            Image            img      = null;

            try
            {
                gotImage.Reset();
                instance.Client.Assets.RequestImage(textureID, (state, assetTexture) =>
                {
                    try
                    {
                        if (state == TextureRequestState.Finished)
                        {
                            ManagedImage mi;
                            OpenJPEG.DecodeToImage(assetTexture.AssetData, out mi);

                            if (removeAlpha)
                            {
                                if ((mi.Channels & ManagedImage.ImageChannels.Alpha) != 0)
                                {
                                    mi.ConvertChannels(mi.Channels & ~ManagedImage.ImageChannels.Alpha);
                                }
                            }

                            img = LoadTGAClass.LoadTGA(new MemoryStream(mi.ExportTGA()));
                        }
                    }
                    finally
                    {
                        gotImage.Set();
                    }
                }
                                                    );
                gotImage.WaitOne(30 * 1000, false);
                if (img != null)
                {
                    texture = img;
                    return(true);
                }
                return(false);
            }
            catch (Exception e)
            {
                Logger.Log(e.Message, Helpers.LogLevel.Error, instance.Client, e);
                return(false);
            }
        }
Example #16
0
 private static TextureDownloadCallback TextureDownloadCallback(Bitmap[] detailTexture, int i, AutoResetEvent textureDone)
 {
     return((state, assetTexture) =>
     {
         if (state == TextureRequestState.Finished && assetTexture != null && assetTexture.AssetData != null)
         {
             Image img;
             ManagedImage mi;
             OpenJPEG.DecodeToImage(assetTexture.AssetData, out mi, out img);
             detailTexture[i] = (Bitmap)img;
         }
         textureDone.Set();
     });
 }
Example #17
0
            private byte[] BlendTextures(byte[] frontImage, byte[] backImage, byte newAlpha)
            {
                ManagedImage managedImage;
                Image        image;

                if (!OpenJPEG.DecodeToImage(frontImage, out managedImage, out image) || image == null)
                {
                    return(null);
                }

                Bitmap image1 = new Bitmap(image);

                image.Dispose();

                if (!OpenJPEG.DecodeToImage(backImage, out managedImage, out image) || image == null)
                {
                    image1.Dispose();
                    return(null);
                }

                Bitmap image2 = new Bitmap(image);

                image.Dispose();

                if (newAlpha < 255)
                {
                    SetAlpha(ref image1, newAlpha);
                }

                using (Bitmap joint = MergeBitMaps(image1, image2))
                {
                    image1.Dispose();
                    image2.Dispose();

                    byte[] result = new byte[0];

                    try
                    {
                        result = OpenJPEG.EncodeFromImage(joint, true);
                    }
                    catch (Exception e)
                    {
                        m_log.ErrorFormat(
                            "[DYNAMICTEXTUREMODULE]: OpenJpeg Encode Failed.  Exception {0}{1}",
                            e.Message, e.StackTrace);
                    }

                    return(result);
                }
            }
Example #18
0
    Bitmap Jpgbytes2Bitmap(byte[] jpg)
    {
        ManagedImage mi;

        if (!OpenJPEG.DecodeToImage(jpg, out mi))
        {
            return(null);
        }
        byte[] imageBytes = mi.ExportTGA();

        using (MemoryStream byteData = new MemoryStream(imageBytes))
        {
            return(LoadTGAClass.LoadTGA(byteData));
        }
    }
        public Hashtable OnHTTPGetTextureImage(Hashtable keysvals)
        {
            Hashtable reply = new Hashtable();

            int statuscode = 200;

            byte[]        jpeg           = new byte[0];
            IAssetService m_AssetService = _registry.RequestModuleInterface <IAssetService>();

            using (MemoryStream imgstream = new MemoryStream())
            {
                // Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular jpeg data

                // non-async because we know we have the asset immediately.
                AssetBase mapasset = m_AssetService.Get(keysvals["uuid"].ToString());

                if (mapasset != null)
                {
                    // Decode image to System.Drawing.Image
                    Image        image = null;
                    ManagedImage managedImage;
                    if (OpenJPEG.DecodeToImage(mapasset.Data, out managedImage, out image))
                    {
                        // Save to bitmap
                        using (Bitmap texture = ResizeBitmap(image, 256, 256))
                        {
                            EncoderParameters myEncoderParameters = new EncoderParameters();
                            myEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);

                            // Save bitmap to stream
                            texture.Save(imgstream, GetEncoderInfo("image/jpeg"), myEncoderParameters);

                            // Write the stream to a byte array for output
                            jpeg = imgstream.ToArray();
                        }
                        image.Dispose();
                    }
                    mapasset.Dispose();
                }
            }

            reply["str_response_string"] = Convert.ToBase64String(jpeg);
            reply["int_response_code"]   = statuscode;
            reply["content_type"]        = "image/jpeg";

            return(reply);
        }
Example #20
0
        //comes in on separate thread
        private void Assets_OnImageReceived(TextureRequestState image, AssetTexture texture)
        {
            if (texture.AssetID != item.AssetUUID)
            {
                return;
            }

            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(() => Assets_OnImageReceived(image, texture)));
                return;
            }

            BeginInvoke(new OnSetStatusText(SetStatusText), new object[] { "Image downloaded. Decoding..." });

            //System.Drawing.Image decodedImage = ImageHelper.Decode(image.AssetData);

            //if (decodedImage == null)
            //{
            //    BeginInvoke(new OnSetStatusText(SetStatusText), new object[] { "D'oh! Error decoding image." });
            //    BeginInvoke(new MethodInvoker(DoErrorState));
            //    return;
            //}

            //instance.ImageCache.AddImage(image.ID, decodedImage);
            //BeginInvoke(new OnSetFinalImage(SetFinalImage), new object[] { decodedImage });

            ManagedImage mImg;
            Image        sImage = null;

            //System.Drawing.Image decodedImage = ImageHelper.Decode(image.AssetData);
            //System.Drawing.Image decodedImage = OpenJPEGNet.OpenJPEG.DecodeToImage(image.AssetData);
            OpenJPEG.DecodeToImage(texture.AssetData, out mImg, out sImage);
            System.Drawing.Image decodedImage = sImage;

            if (decodedImage == null)
            {
                BeginInvoke(new OnSetStatusText(SetStatusText), new object[] { "D'oh! Error decoding image." });
                BeginInvoke(new MethodInvoker(DoErrorState));
                return;
            }

            instance.ImageCache.AddImage(texture.AssetID, decodedImage);
            BeginInvoke(new OnSetFinalImage(SetFinalImage), new object[] { decodedImage });
        }
Example #21
0
        public static Image Decode(byte[] j2cdata)
        {
            ManagedImage mImg;
            Image        image = null;

            //Boolean bRet;

            try
            {
                //bRet = OpenJPEG.DecodeToImage(j2cdata, out mImg,out image);
                OpenJPEG.DecodeToImage(j2cdata, out mImg, out image);
            }
            catch
            {
            }

            return(image);
        }
Example #22
0
        void ExportArchiveImage(UUID imageUUID, string archiveName, string filePath)
        {
            byte [] jpeg;

            using (MemoryStream imgstream = new MemoryStream()) {
                // Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular jpeg data

                // non-async because we know we have the asset immediately.
                byte [] imageAsset = assetService.GetData(imageUUID.ToString());

                if (imageAsset != null)
                {
                    // Decode image to System.Drawing.Image
                    Image        image;
                    ManagedImage managedImage;
                    if (OpenJPEG.DecodeToImage(imageAsset, out managedImage, out image))
                    {
                        // Save to bitmap
                        using (Bitmap texture = ResizeBitmap(image, 256, 256, archiveName)) {
                            EncoderParameters myEncoderParameters = new EncoderParameters();
                            myEncoderParameters.Param [0] = new EncoderParameter(Encoder.Quality, 75L);

                            // Save bitmap to stream
                            var encInfo = GetEncoderInfo("image/jpeg");
                            if (encInfo != null)
                            {
                                texture.Save(imgstream, encInfo, myEncoderParameters);
                            }
                            myEncoderParameters.Dispose();

                            // Write the stream to a byte array for output
                            jpeg = imgstream.ToArray();

                            // save image
                            string fileName = archiveName + ".jpg";
                            string fullPath = Path.Combine(filePath, fileName);
                            File.WriteAllBytes(fullPath, jpeg);
                        }
                        image.Dispose();
                    }
                }
            }
        }
Example #23
0
        public Image DecodeToImage(byte [] j2kData)
        {
            if (m_useCSJ2K)
            {
                return(J2kImage.FromBytes(j2kData));
            }

            // decode using OpenJpeg
            ManagedImage mimage;
            Image        image;

            if (OpenJPEG.DecodeToImage(j2kData, out mimage, out image))
            {
                mimage = null;
                return(image);
            }

            return(null);
        }
// TODO: unused:
//         private void ShadeBuildings(Bitmap map)
//         {
//             lock (map)
//             {
//                 lock (m_scene.Entities)
//                 {
//                     foreach (EntityBase entity in m_scene.Entities.Values)
//                     {
//                         if (entity is SceneObjectGroup)
//                         {
//                             SceneObjectGroup sog = (SceneObjectGroup) entity;
//
//                             foreach (SceneObjectPart primitive in sog.Children.Values)
//                             {
//                                 int x = (int) (primitive.AbsolutePosition.X - (primitive.Scale.X / 2));
//                                 int y = (int) (primitive.AbsolutePosition.Y - (primitive.Scale.Y / 2));
//                                 int w = (int) primitive.Scale.X;
//                                 int h = (int) primitive.Scale.Y;
//
//                                 int dx;
//                                 for (dx = x; dx < x + w; dx++)
//                                 {
//                                     int dy;
//                                     for (dy = y; dy < y + h; dy++)
//                                     {
//                                         if (x < 0 || y < 0)
//                                             continue;
//                                         if (x >= map.Width || y >= map.Height)
//                                             continue;
//
//                                         map.SetPixel(dx, dy, Color.DarkGray);
//                                     }
//                                 }
//                             }
//                         }
//                     }
//                 }
//             }
//         }

        private Bitmap FetchTexture(UUID id)
        {
            AssetBase asset = m_scene.AssetService.Get(id.ToString());

            if (asset != null)
            {
                m_log.DebugFormat("[MAPTILE]: Static map image texture {0} found for {1}", id, m_scene.Name);
            }
            else
            {
                m_log.WarnFormat("[MAPTILE]: Static map image texture {0} not found for {1}", id, m_scene.Name);
                return(null);
            }

            ManagedImage managedImage;
            Image        image;

            try
            {
                if (OpenJPEG.DecodeToImage(asset.Data, out managedImage, out image))
                {
                    return(new Bitmap(image));
                }
                else
                {
                    return(null);
                }
            }
            catch (DllNotFoundException)
            {
                m_log.ErrorFormat("[MAPTILE]: OpenJpeg is not installed correctly on this system.   Asset Data is empty for {0}", id);
            }
            catch (IndexOutOfRangeException)
            {
                m_log.ErrorFormat("[MAPTILE]: OpenJpeg was unable to decode this.   Asset Data is empty for {0}", id);
            }
            catch (Exception)
            {
                m_log.ErrorFormat("[MAPTILE]: OpenJpeg was unable to decode this.   Asset Data is empty for {0}", id);
            }
            return(null);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog
            {
                AddExtension     = true,
                RestoreDirectory = true,
                Title            = "Save image as...",
                Filter           =
                    "PNG (*.png)|*.png|Targa (*.tga)|*.tga|Jpeg2000 (*.j2c)|*.j2c|Jpeg (*.jpg)|*.jpg|Bitmap (*.bmp)|*.bmp"
            };



            if (dlg.ShowDialog() == DialogResult.OK)
            {
                int type = dlg.FilterIndex;
                if (type == 3)
                { // jpeg2000
                    File.WriteAllBytes(dlg.FileName, UploadData);
                }
                else if (type == 2)
                { // targa
                    ManagedImage imgManaged;
                    OpenJPEG.DecodeToImage(UploadData, out imgManaged);
                    File.WriteAllBytes(dlg.FileName, imgManaged.ExportTGA());
                }
                else if (type == 1)
                { // png
                    pbPreview.Image.Save(dlg.FileName, ImageFormat.Png);
                }
                else if (type == 4)
                { // jpg
                    pbPreview.Image.Save(dlg.FileName, ImageFormat.Jpeg);
                }
                else
                { // BMP
                    pbPreview.Image.Save(dlg.FileName, ImageFormat.Bmp);
                }
            }

            dlg.Dispose();
        }
Example #26
0
        private void Assets_OnImageReceived(AssetTexture assetTexture)
        {
            if (assetTexture.AssetID != this.imageID)
            {
                return;
            }

            if (InvokeRequired)
            {
                if (IsHandleCreated || !instance.MonoRuntime)
                {
                    BeginInvoke(new MethodInvoker(() => Assets_OnImageReceived(assetTexture)));
                }
                return;
            }

            try
            {
                progressBar1.Hide();
                lblProgress.Hide();

                if (!OpenJPEG.DecodeToImage(assetTexture.AssetData, out imgManaged, out image))
                {
                    throw new Exception("decoding failure");
                }

                Text = Text; // yeah, really ;)

                pictureBox1.Image   = image;
                pictureBox1.Enabled = true;
                jpegdata            = assetTexture.AssetData;
                if (Detached)
                {
                    ClientSize = pictureBox1.Size = new Size(image.Width, image.Height);
                }
            }
            catch (Exception excp)
            {
                this.Hide();
                System.Console.WriteLine("Error decoding image: " + excp.Message);
            }
        }
        public byte[] OnHTTPGetTextureImage(string path, Stream request, OSHttpRequest httpRequest,
                                            OSHttpResponse httpResponse)
        {
            byte[]        jpeg           = new byte[0];
            IAssetService m_AssetService = _registry.RequestModuleInterface <IAssetService>();

            using (MemoryStream imgstream = new MemoryStream())
            {
                // Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular jpeg data

                // non-async because we know we have the asset immediately.
                byte[] mapasset = m_AssetService.GetData(httpRequest.QueryString["uuid"]);

                if (mapasset != null)
                {
                    // Decode image to System.Drawing.Image
                    Image        image = null;
                    ManagedImage managedImage;
                    if (OpenJPEG.DecodeToImage(mapasset, out managedImage, out image))
                    {
                        // Save to bitmap
                        using (Bitmap texture = ResizeBitmap(image, 256, 256))
                        {
                            EncoderParameters myEncoderParameters = new EncoderParameters();
                            myEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
                                                                                75L);

                            // Save bitmap to stream
                            texture.Save(imgstream, GetEncoderInfo("image/jpeg"), myEncoderParameters);

                            // Write the stream to a byte array for output
                            jpeg = imgstream.ToArray();
                        }
                        image.Dispose();
                    }
                }
            }

            httpResponse.ContentType = "image/jpeg";

            return(jpeg);
        }
Example #28
0
        public Image GetTexture(UUID TextureID)
        {
            if (TextureID == UUID.Zero) //Send white instead of null
            {
                TextureID = Util.BLANK_TEXTURE_UUID;
            }

            AssetBase    asset = m_assetService.Get(TextureID.ToString());
            ManagedImage managedImage;
            Image        image;

            if (asset != null && OpenJPEG.DecodeToImage(asset.Data, out managedImage, out image))
            {
                return(image);
            }
            else
            {
                return(new Bitmap(1, 1));
            }
        }
Example #29
0
        bool LoadTexture(UUID textureID, ref Image texture, bool removeAlpha)
        {
            var   gotImage = new System.Threading.ManualResetEvent(false);
            Image img      = null;

            try
            {
                gotImage.Reset();
                byte[] tgaData;
                Client.Assets.RequestImage(textureID, (state, assetTexture) =>
                {
                    ManagedImage mi;
                    if (state == TextureRequestState.Finished && OpenJPEG.DecodeToImage(assetTexture.AssetData, out mi))
                    {
                        if (removeAlpha)
                        {
                            if ((mi.Channels & ManagedImage.ImageChannels.Alpha) != 0)
                            {
                                mi.ConvertChannels(mi.Channels & ~ManagedImage.ImageChannels.Alpha);
                            }
                        }
                        tgaData = mi.ExportTGA();
                        img     = LoadTGAClass.LoadTGA(new MemoryStream(tgaData));
                    }
                    gotImage.Set();
                });
                gotImage.WaitOne(30 * 1000, false);

                if (img != null)
                {
                    texture = img;
                    return(true);
                }
                return(false);
            }
            catch (Exception e)
            {
                Logger.Log(e.Message, Helpers.LogLevel.Error, Client, e);
                return(false);
            }
        }
Example #30
0
        private void imageReceivedCallback(TextureRequestState state, AssetTexture asset)
        {
            if (state == TextureRequestState.Timeout)
            {
                // need a re-request if a texture times out but doing it here borks libomv
                //m_user.Assets.RequestImage(asset.AssetID, ImageType.Normal, imageReceivedCallback);
                return;
            }

            if (state != TextureRequestState.Finished)
            {
                return;
            }

            if (OnTextureDownloaded != null)
            {
                VTexture texture = new VTexture();
                texture.TextureId = asset.AssetID;

                ManagedImage managedImage;
                Image        tempImage;

                try
                {
                    if (OpenJPEG.DecodeToImage(asset.AssetData, out managedImage, out tempImage))
                    {
                        Bitmap   textureBitmap = new Bitmap(tempImage.Width, tempImage.Height, PixelFormat.Format32bppArgb);
                        Graphics graphics      = Graphics.FromImage(textureBitmap);
                        graphics.DrawImage(tempImage, 0, 0);
                        graphics.Flush();
                        graphics.Dispose();
                        texture.Image = textureBitmap;
                        OnTextureDownloaded(texture);
                    }
                }
                catch (Exception e)
                {
                    m_log.Error(":( :( :( :( got exception decoding image ): ): ): ):\nException: " + e.ToString());
                }
            }
        }