Exemplo n.º 1
0
        public void Open(Stream stream)
        {
            currentFile = TexFile.ReadFile(stream);

            using (MagickImage image = new MagickImage(currentFile.Data))
                ReloadUI(image, currentFile.Format);
        }
Exemplo n.º 2
0
        private void importToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog
            {
                Filter      = "All supported image formats (*.png;*.jpg;*.jpeg;*.dds)|*.png;*.jpg;*.jpeg;*.dds|PNG image (*.png)|*.png|JPEG image (*.jpeg;*.jpg)|*.jpeg;*.jpg|DDS texture (*.dds)|*.dds|All Files (*.*)|*.*",
                Multiselect = true
            };

            if (open.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            foreach (string filename in open.FileNames)
            {
                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    byte[] data = fs.ToBytes();

                    using (MagickImage image = new MagickImage(data))
                    {
                        TextureFormat format;
                        string        internalPath = Path.GetFileName(filename);

                        switch (Path.GetExtension(filename))
                        {
                        case ".png":
                            format = TextureFormat.ARGB32;
                            break;

                        case ".jpg":
                        case ".jpeg":
                            format = TextureFormat.RGB24;
                            break;

                        default:
                        case ".dds":
                            format = TextureFormat.DXT5;
                            break;
                        }

                        TexFile file = new TexFile(internalPath, format, new Size(image.Width, image.Height));
                        file.Data = data;

                        string outputFilename = Path.ChangeExtension(filename, ".tex");

                        using (var output = new FileStream(outputFilename, FileMode.Create))
                            file.WriteFile(output);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void Import()
        {
            OpenFileDialog open = new OpenFileDialog
            {
                Filter = "All supported image formats (*.png;*.jpg;*.jpeg;*.dds)|*.png;*.jpg;*.jpeg;*.dds|PNG image (*.png)|*.png|JPEG image (*.jpeg;*.jpg)|*.jpeg;*.jpg|DDS texture (*.dds)|*.dds|All Files (*.*)|*.*"
            };

            if (open.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            byte[] data;

            using (var stream = open.OpenFile())
                data = stream.ToBytes();

            using (MagickImage image = new MagickImage(data))
            {
                TextureFormat format;

                switch (image.Format)
                {
                default:
                case MagickFormat.Png:
                case MagickFormat.Png32:
                case MagickFormat.Png24:
                    format = TextureFormat.ARGB32;
                    break;

                case MagickFormat.Jpeg:
                case MagickFormat.Jpg:
                    format = TextureFormat.RGB24;
                    break;

                case MagickFormat.Dxt1:
                    format = TextureFormat.DXT1;
                    break;

                case MagickFormat.Dds:
                case MagickFormat.Dxt5:
                    format = TextureFormat.DXT5;
                    break;
                }

                currentFile      = new TexFile(Path.GetFileName(open.FileName), format, new Size(image.Width, image.Height));
                currentFile.Data = data;

                ReloadUI(image, format);
            }
        }
Exemplo n.º 4
0
        private void exportToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog
            {
                Filter      = "Texture file (*.tex)|*.tex|All Files (*.*)|*.*",
                Multiselect = true
            };

            if (open.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            foreach (string filename in open.FileNames)
            {
                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    TexFile file = TexFile.ReadFile(fs);

                    string outputFilename;

                    switch (file.Format)
                    {
                    case TextureFormat.ARGB32:
                        outputFilename = Path.ChangeExtension(filename, ".png");
                        break;

                    case TextureFormat.RGB24:
                        outputFilename = Path.ChangeExtension(filename, ".jpeg");
                        break;

                    default:
                    case TextureFormat.DXT1:
                    case TextureFormat.DXT5:
                        outputFilename = Path.ChangeExtension(filename, ".dds");
                        break;
                    }

                    File.WriteAllBytes(outputFilename, file.Data);
                }
            }
        }
Exemplo n.º 5
0
        public static TexFile ReadFile(Stream stream)
        {
            BinaryReader binaryReader = new BinaryReader(stream, Encoding.UTF8);
            string       text         = binaryReader.ReadString();

            if (text != MAGIC)
            {
                throw new InvalidDataException("This is not a TEX file.");
            }

            int           version       = binaryReader.ReadInt32();
            string        path          = binaryReader.ReadString();
            int           width         = 0;
            int           height        = 0;
            TextureFormat textureFormat = TextureFormat.ARGB32;

            //Rect[] array = null;
            if (1010 <= version)
            {
                if (1011 <= version)
                {
                    throw new NotSupportedException("1011 format .TEX files are not supported.");

                    //int num2 = binaryReader.ReadInt32();
                    //if (0 < num2)
                    //{
                    //	array = new Rect[num2];
                    //	for (int i = 0; i < num2; i++)
                    //	{
                    //		float x = binaryReader.ReadSingle();
                    //		float y = binaryReader.ReadSingle();
                    //		float width2 = binaryReader.ReadSingle();
                    //		float height2 = binaryReader.ReadSingle();
                    //		array[i] = new Rect(x, y, width2, height2);
                    //	}
                    //}
                }

                width         = binaryReader.ReadInt32();
                height        = binaryReader.ReadInt32();
                textureFormat = (TextureFormat)binaryReader.ReadInt32();
            }

            int dataLength = binaryReader.ReadInt32();

            var data = new byte[dataLength];

            binaryReader.Read(data, 0, dataLength);

            if (version == 1000)
            {
                width  = data[16] << 24 | data[17] << 16 | data[18] << 8 | data[19];
                height = data[20] << 24 | data[21] << 16 | data[22] << 8 | data[23];
            }

            TexFile file = new TexFile(path, textureFormat, new Size(width, height))
            {
                Data = data
            };

            return(file);
        }