Example #1
0
            public static Texture2D ToImage(ImportedTexture tex, ImageLoadInformation loadInfo, out byte pixelDepth)
            {
                ushort width, height;

                byte[] data;
                using (Stream stream = new MemoryStream(tex.Data))
                {
                    byte  idLen, descriptor;
                    bool  compressed;
                    short originY;
                    GetImageInfo(stream, out idLen, out compressed, out originY, out width, out height, out pixelDepth, out descriptor);
                    if (compressed)
                    {
                        throw new Exception("Warning! Compressed TGAs are not supported");
                    }
                    stream.Position = idLen + 0x12;
                    BinaryReader reader = new BinaryReader(stream);
                    data = reader.ReadToEnd();
                    if (originY == 0)
                    {
                        Flip((short)height, width, height, (byte)(pixelDepth >> 3), data);
                    }
                }
                byte[] header = DDS.CreateHeader(width, height, pixelDepth, 0, false, pixelDepth == 32 ? DDS.UnityCompatibleFormat.ARGB32 : DDS.UnityCompatibleFormat.RGB24);
                using (BinaryWriter writer = new BinaryWriter(new MemoryStream()))
                {
                    writer.Write(header);
                    writer.Write(data);

                    try
                    {
                        writer.BaseStream.Position = 0;
                        return(Texture2D.FromStream(Gui.Renderer.Device, writer.BaseStream, (int)writer.BaseStream.Length, loadInfo));
                    }
                    catch (Exception e)
                    {
                        Direct3D11Exception d3de = e as Direct3D11Exception;
                        if (d3de != null)
                        {
                            Report.ReportLog("Direct3D11 Exception name=\"" + d3de.ResultCode.Name + "\" desc=\"" + d3de.ResultCode.Description + "\" code=0x" + ((uint)d3de.ResultCode.Code).ToString("X"));
                        }
                        else
                        {
                            Utility.ReportException(e);
                        }
                        return(null);
                    }
                }
            }
Example #2
0
        void LoadImage(ImportedTexture tex)
        {
            try
            {
                if (tex == null || Gui.Docking.DockRenderer == null || Gui.Docking.DockRenderer.IsHidden)
                {
                    pictureBox1.Image = null;
                    textBoxName.Text  = String.Empty;
                    textBoxSize.Text  = String.Empty;
                    if (Gui.Docking.DockRenderer != null && !Gui.Docking.DockRenderer.IsHidden)
                    {
                        Gui.Docking.DockRenderer.Enabled = false;
                        Gui.Docking.DockRenderer.Activate();
                        Gui.Docking.DockRenderer.Enabled = true;
                    }
                }
                else
                {
                    textBoxName.Text = tex.Name;

                    if (tex.Data.Length > 0x12)
                    {
                        if (Path.GetExtension(tex.Name).ToUpper() == ".TGA")
                        {
                            byte      pixelDepth;
                            Texture2D renderTexture = Utility.TGA.ToImage(tex, out pixelDepth);
                            if (renderTexture != null)
                            {
                                ToPictureBox(renderTexture, pixelDepth);
                                renderTexture.Dispose();
                            }
                            else
                            {
                                pictureBox1.Image = pictureBox1.ErrorImage;
                            }
                        }
                        else
                        {
                            try
                            {
                                Image img = System.Drawing.Image.FromStream(new MemoryStream(tex.Data));
                                pictureBox1.Image = img;
                                int bpp = 0;
                                if (img.PixelFormat.ToString().IndexOf("Format") >= 0)
                                {
                                    bpp = img.PixelFormat.ToString().IndexOf("bpp");
                                    if (!int.TryParse(img.PixelFormat.ToString().Substring(6, bpp - 6), out bpp))
                                    {
                                        bpp = 0;
                                    }
                                }
                                textBoxSize.Text = img.Width + "x" + img.Height + (bpp > 0 ? "x" + bpp : String.Empty);
                            }
                            catch
                            {
                                try
                                {
                                    int  width, height, bpp;
                                    bool cubemap;
                                    using (Texture2D renderTexture = Utility.DDS.ScaleWhenNeeded(tex.Data, out width, out height, out bpp, out cubemap))
                                    {
                                        ToPictureBox(renderTexture, bpp, width, height, cubemap);
                                    }
                                }
                                catch (Exception e)
                                {
                                    pictureBox1.Image = pictureBox1.ErrorImage;
                                    Direct3D11Exception d3de = e as Direct3D11Exception;
                                    if (d3de != null)
                                    {
                                        Report.ReportLog("Direct3D11 Exception name=\"" + d3de.ResultCode.Name + "\" desc=\"" + d3de.ResultCode.Description + "\" code=0x" + ((uint)d3de.ResultCode.Code).ToString("X"));
                                    }
                                    else
                                    {
                                        Utility.ReportException(e);
                                    }
                                }
                            }
                        }

                        ResizeImage();
                        if (!this.IsHidden)
                        {
                            Enabled = false;
                            Activate();
                            Enabled = true;
                        }
                    }
                    else
                    {
                        pictureBox1.Image = null;
                        textBoxSize.Text  = "0x0";
                    }
                }

                image = tex;
            }
            catch (Exception ex)
            {
                Utility.ReportException(ex);
            }
        }