Ejemplo n.º 1
0
        public void LoadFrom(ImportedTexture tex)
        {
            m_Name = Path.GetFileNameWithoutExtension(tex.Name);

            Device           dev = new Device(new Direct3D(), 0, DeviceType.Hardware, new IntPtr(), CreateFlags.SoftwareVertexProcessing, new PresentParameters());
            ImageInformation imageInfo;

            using (Texture renderTexture = Texture.FromMemory(dev, tex.Data, 0, 0, -1, Usage.None, Format.Unknown, Pool.Managed, Filter.Default, Filter.Default, 0, out imageInfo))
            {
                m_Width  = imageInfo.Width;
                m_Height = imageInfo.Height;

                TextureFormat tf;
                if (TextureFormat.TryParse(imageInfo.Format.ToString(), true, out tf))
                {
                    m_TextureFormat = tf;
                }
                else if (imageInfo.Format == Format.R8G8B8)
                {
                    m_TextureFormat = TextureFormat.RGB24;
                }
                else if (imageInfo.Format == Format.A8R8G8B8)
                {
                    m_TextureFormat = TextureFormat.ARGB32;
                }
                else
                {
                    throw new Exception("Unknown format " + imageInfo.Format);
                }

                m_MipMap           = imageInfo.MipLevels > 1;
                m_ReadAllowed      = true;
                m_ImageCount       = 1;
                m_TextureDimension = 2;
                m_TextureSettings  = new GLTextureSettings();
                m_ColorSpace       = 1;

                int bytesPerPixel = 0, originY = 0;
                using (BinaryReader reader = new BinaryReader(new MemoryStream(tex.Data)))
                {
                    switch (m_TextureFormat)
                    {
                    case TextureFormat.DXT1:
                    case TextureFormat.DXT5:
                        reader.BaseStream.Position = 0x80;
                        m_CompleteImageSize        = (int)(reader.BaseStream.Length - reader.BaseStream.Position);
                        break;

                    case TextureFormat.RGB24:
                    case TextureFormat.ARGB32:
                        reader.BaseStream.Position = 0x0A;
                        originY = reader.ReadInt16();
                        reader.BaseStream.Position = 0x12;
                        bytesPerPixel       = m_TextureFormat == TextureFormat.RGB24 ? 3 : 4;
                        m_CompleteImageSize = m_Width * m_Height * bytesPerPixel;
                        break;
                    }
                    image_data = reader.ReadBytes(m_CompleteImageSize);
                }
                switch (m_TextureFormat)
                {
                case TextureFormat.RGB24:
                    for (int i = 0, j = 2; j < m_CompleteImageSize; i += 3, j += 3)
                    {
                        byte b = image_data[j];
                        image_data[j] = image_data[i];
                        image_data[i] = b;
                    }
                    break;

                case TextureFormat.ARGB32:
                    for (int i = 0, j = 3, k = 1, l = 2; j < m_CompleteImageSize; i += 4, j += 4, k += 4, l += 4)
                    {
                        byte b = image_data[j];
                        image_data[j] = image_data[i];
                        image_data[i] = b;
                        b             = image_data[l];
                        image_data[l] = image_data[k];
                        image_data[k] = b;
                    }
                    break;
                }
                if (bytesPerPixel > 0 && originY > 0)
                {
                    for (int srcIdx = 0, dstIdx = (originY - 1) * m_Width * bytesPerPixel; srcIdx < dstIdx; srcIdx += m_Width * bytesPerPixel, dstIdx -= m_Width * bytesPerPixel)
                    {
                        for (int i = 0; i < m_Width * bytesPerPixel; i++)
                        {
                            byte b = image_data[srcIdx + i];
                            image_data[srcIdx + i] = image_data[dstIdx + i];
                            image_data[dstIdx + i] = b;
                        }
                    }
                }
            }
            dev.Direct3D.Dispose();
            dev.Dispose();
        }
Ejemplo n.º 2
0
        public void LoadFrom(ImportedTexture tex)
        {
            TextureFormat destFormat     = (TextureFormat)0;
            Match         nameWithFormat = Regex.Match(tex.Name, @"(.+)-([^-]+)(\..+)", RegexOptions.CultureInvariant);

            if (nameWithFormat.Success)
            {
                m_Name = nameWithFormat.Groups[1].Value;
                TextureFormat.TryParse(nameWithFormat.Groups[2].Value, true, out destFormat);
            }
            else
            {
                m_Name = Path.GetFileNameWithoutExtension(tex.Name);
            }

            Device dev = null;

            try
            {
                if (Gui.Renderer != null)
                {
                    dev = Gui.Renderer.Device;
                }
                else
                {
                    IntPtr handle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
                    if (handle.ToInt64() == 0)
                    {
                        handle = FindWindowByCaption(IntPtr.Zero, Console.Title);
                    }
                    dev = new Device(new Direct3D(), 0, DeviceType.Hardware, handle, CreateFlags.SoftwareVertexProcessing, new PresentParameters());
                }
                ImageInformation imageInfo;
                using (Texture renderTexture = Texture.FromMemory(dev, tex.Data, 0, 0, -1, Usage.None, Format.Unknown, Pool.Managed, Filter.Default, Filter.Default, 0, out imageInfo))
                {
                    m_Width  = imageInfo.Width;
                    m_Height = imageInfo.Height;

                    TextureFormat tf;
                    if (TextureFormat.TryParse(imageInfo.Format.ToString(), true, out tf))
                    {
                        m_TextureFormat = tf;
                    }
                    else if (imageInfo.Format == Format.R8G8B8)
                    {
                        m_TextureFormat = TextureFormat.RGB24;
                    }
                    else if (imageInfo.Format == Format.A8R8G8B8)
                    {
                        m_TextureFormat = destFormat != (TextureFormat)0 ? destFormat : TextureFormat.ARGB32;
                    }
                    else
                    {
                        throw new Exception("Unknown format " + imageInfo.Format);
                    }

                    m_MipMap           = imageInfo.MipLevels > 1;
                    m_ReadAllowed      = true;
                    m_ImageCount       = 1;
                    m_TextureDimension = 2;
                    m_TextureSettings  = new GLTextureSettings();
                    m_ColorSpace       = 1;

                    int bytesPerPixel = 0, originY = 0;
                    using (BinaryReader reader = new BinaryReader(new MemoryStream(tex.Data)))
                    {
                        switch (m_TextureFormat)
                        {
                        case TextureFormat.DXT1:
                        case TextureFormat.DXT5:
                            reader.BaseStream.Position = 0x80;
                            m_CompleteImageSize        = (int)(reader.BaseStream.Length - reader.BaseStream.Position);
                            break;

                        case TextureFormat.RGB24:
                        case TextureFormat.ARGB32:
                        case TextureFormat.RGBA32:
                            reader.BaseStream.Position = 0x0A;
                            originY = reader.ReadInt16();
                            reader.BaseStream.Position = 0x12;
                            bytesPerPixel       = m_TextureFormat == TextureFormat.RGB24 ? 3 : 4;
                            m_CompleteImageSize = m_Width * m_Height * bytesPerPixel;
                            break;
                        }
                        image_data = reader.ReadBytes(m_CompleteImageSize);
                    }
                    switch (m_TextureFormat)
                    {
                    case TextureFormat.RGB24:
                        for (int i = 0, j = 2; j < m_CompleteImageSize; i += 3, j += 3)
                        {
                            byte b = image_data[j];
                            image_data[j] = image_data[i];
                            image_data[i] = b;
                        }
                        break;

                    case TextureFormat.ARGB32:
                        for (int i = 0, j = 3, k = 1, l = 2; j < m_CompleteImageSize; i += 4, j += 4, k += 4, l += 4)
                        {
                            byte b = image_data[j];
                            image_data[j] = image_data[i];
                            image_data[i] = b;
                            b             = image_data[l];
                            image_data[l] = image_data[k];
                            image_data[k] = b;
                        }
                        break;
                    }
                    if (bytesPerPixel > 0 && originY > 0)
                    {
                        for (int srcIdx = 0, dstIdx = (originY - 1) * m_Width * bytesPerPixel; srcIdx < dstIdx; srcIdx += m_Width * bytesPerPixel, dstIdx -= m_Width * bytesPerPixel)
                        {
                            for (int i = 0; i < m_Width * bytesPerPixel; i++)
                            {
                                byte b = image_data[srcIdx + i];
                                image_data[srcIdx + i] = image_data[dstIdx + i];
                                image_data[dstIdx + i] = b;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Utility.ReportException(e);
            }
            finally
            {
                if (Gui.Renderer == null && dev != null)
                {
                    dev.Direct3D.Dispose();
                    dev.Dispose();
                }
            }
        }