Ejemplo n.º 1
0
        private void mnuExportDDS_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
                {
                    RW4Section section = this.DataContext as RW4Section;
                    SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture;

                    SaveFileDialog dlg = new SaveFileDialog();
                    dlg.Filter = "DirectDraw Surface .DDS|*.dds";
                    if (dlg.ShowDialog().GetValueOrDefault(false))
                    {
                        using (Stream stream = File.Create(dlg.FileName))
                        {
                            DXT5Image img = new DXT5Image();
                            img.Height      = textureSection.height;
                            img.Width       = textureSection.width;
                            img.TextureType = textureSection.textureType;
                            img.MipMaps     = (int)textureSection.mipmapInfo;

                            img.SetData(textureSection.texData.blob);

                            img.Write(stream);

                            GraphicsDeviceService.AddRef(new WindowInteropHelper(Application.Current.MainWindow).Handle);
                            Texture2D texture;

                            try
                            {
                                DDSLib.DDSFromStream(stream, GraphicsDeviceService.Instance.GraphicsDevice, 0, true, out texture);
                                texturePreview.Source = DDSLib.Texture2Image(texture);

                                texturePreview.Visibility = System.Windows.Visibility.Visible;
                                textBlockError.Visibility = System.Windows.Visibility.Hidden;
                            }
                            catch (Exception ex)
                            {
                                texturePreview.Visibility = System.Windows.Visibility.Hidden;
                                textBlockError.Visibility = System.Windows.Visibility.Visible;
                                textBlockError.Text       = ex.Message;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An exception occurred: {0}", ex.Message));
            }
        }
Ejemplo n.º 2
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
            {
                RW4Section section = this.DataContext as RW4Section;
                SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture;

                if (textureSection.textureType != 21)
                {
                    if (textureSection != null)
                    {
                        using (var stream = new MemoryStream())
                        {
                            DXT5Image img = new DXT5Image();
                            img.Height      = textureSection.height;
                            img.Width       = textureSection.width;
                            img.TextureType = textureSection.textureType;
                            img.MipMaps     = (int)textureSection.mipmapInfo;

                            img.SetData(textureSection.texData.blob);

                            img.Write(stream);

                            if (textureSection.textureType == 0x21)
                            {
                                mnuExportBMP.IsEnabled = true;
                                mnuExportDDS.IsEnabled = false;
                                mnuImportBMP.IsEnabled = true;
                                mnuImportDDS.IsEnabled = false;
                            }
                            else
                            {
                                mnuExportBMP.IsEnabled = false;
                                mnuExportDDS.IsEnabled = true;
                                mnuImportBMP.IsEnabled = false;
                                mnuImportDDS.IsEnabled = true;
                            }


                            GraphicsDeviceService.AddRef(new WindowInteropHelper(Application.Current.MainWindow).Handle);
                            Texture2D texture;

                            try
                            {
                                DDSLib.DDSFromStream(stream, GraphicsDeviceService.Instance.GraphicsDevice, 0, true, out texture);
                                texturePreview.Source = DDSLib.Texture2Image(texture);

                                texturePreview.Visibility = System.Windows.Visibility.Visible;
                                textBlockError.Visibility = System.Windows.Visibility.Hidden;
                            }
                            catch (Exception ex)
                            {
                                texturePreview.Visibility = System.Windows.Visibility.Hidden;
                                textBlockError.Visibility = System.Windows.Visibility.Visible;
                                textBlockError.Text       = ex.Message;
                            }
                        }
                    }
                }
                else
                {
                    using (MemoryStream byteStream = new MemoryStream(textureSection.texData.blob, 0, textureSection.texData.blob.Length))
                    {
                        for (int i = 0; i < textureSection.mipmapInfo; i++)
                        {
                            //  uint blockSize = byteStream.ReadU32().Swap();
                            WriteableBitmap bitmap = new WriteableBitmap((int)textureSection.width, (int)textureSection.height, 300, 300, PixelFormats.Pbgra32, BitmapPalettes.Halftone64);
                            if (textureSection.textureType == 21)
                            {
                                for (int j = 0; j < (byteStream.Length / 4); j++)
                                {
                                    byte r = byteStream.ReadU8();
                                    byte g = byteStream.ReadU8();
                                    byte b = byteStream.ReadU8();
                                    byte a = byteStream.ReadU8();


                                    try
                                    {
                                        if ((j / textureSection.width) < textureSection.height)
                                        {
                                            bitmap.SetPixel((int)(j % textureSection.width), (int)(j / textureSection.width), a, r, g, b);
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }

                                texturePreview.Source = bitmap;
                                break;
                            }
                        }
                    }
                }

                if (textureSection != null)
                {
                    txtHeight.Text = textureSection.height.ToString();
                    txtWidth.Text  = textureSection.width.ToString();

                    txtTextureType.Text = textureSection.textureType.ToHex();
                    txtMipMapInfo.Text  = textureSection.mipmapInfo.ToHex();

                    txtSection.Text = textureSection.texData.section.Number.ToString();
                    txtUnknown.Text = textureSection.unk1.ToHex();
                }

                //  .AddObject(texture.texData, TextureBlob.type_code);
            }
        }