public override void ImportImage(string filename)
        {
            // Import
            // Se le va a pasar un png y hay que convertirlo al formato de la DDS

            using (ScratchImage originalDds = DirectXTexNet.TexHelper.Instance.LoadFromDDSFile(Path, DDS_FLAGS.NONE))
            {
                TexMetadata originalMetadata = originalDds.GetMetadata();

                ScratchImage png = DirectXTexNet.TexHelper.Instance.LoadFromWICFile(filename, WIC_FLAGS.NONE);

                var compareTask = CompareImagesAsync(originalDds, png);
                var areEqual    = compareTask.WaitAndUnwrapException();

                if (areEqual)
                {
                    png.Dispose();
                    return;
                }

                if (originalMetadata.MipLevels > 1)
                {
                    ScratchImage aux = png.GenerateMipMaps(TEX_FILTER_FLAGS.DEFAULT, originalMetadata.MipLevels);
                    png.Dispose();
                    png = aux;
                }

                if (IsCompressed(originalMetadata.Format))
                {
                    DXGI_FORMAT format = originalMetadata.Format;
                    if (format == DXGI_FORMAT.BC7_UNORM_SRGB)
                    {
                        format = DXGI_FORMAT.BC7_UNORM;
                    }

                    using (ScratchImage newDds = png.Compress(format, TEX_COMPRESS_FLAGS.PARALLEL, 0.5f))
                    {
                        newDds.SaveToDDSFile(DDS_FLAGS.NONE, ChangesFile);
                    }
                }
                else
                {
                    png.SaveToDDSFile(DDS_FLAGS.NONE, ChangesFile);
                }

                png?.Dispose();
            }
        }
예제 #2
0
        public override void ExportImage(string filename)
        {
            if (_currentDDS != null && !_currentDDS.IsDisposed)
            {
                _currentDDS.Dispose();
                _currentDDS = null;
            }

            _currentDDS = GetScratchImage();

            string directory = System.IO.Path.GetDirectoryName(filename);

            Directory.CreateDirectory(directory);

            _currentDDS.SaveToDDSFile(DDS_FLAGS.NONE, filename);

            _currentDDS.Dispose();
            _currentDDS = null;
        }
예제 #3
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string choice = (string)((ComboBoxItem)ResolutionChoice.SelectedItem).Content;

            string[] parts = choice.Split('x');
            int      width, height;

            int.TryParse(parts[0], out width);
            int.TryParse(parts[1], out height);
            if (image.GetMetadata().Width != width || image.GetMetadata().Height != height)
            {
                image = image.Resize(width, height, TEX_FILTER_FLAGS.DEFAULT);
            }
            image = image.TransformImage(changeAlpha);
            if (BC3.IsChecked == true)
            {
                image = image.Compress(DXGI_FORMAT.BC3_UNORM, TEX_COMPRESS_FLAGS.DEFAULT, 0.5f);
            }
            else if (BC7.IsChecked == true)
            {
                image = convertBC7(image);
            }
            string textureDir = getFullPath("work/textures/sky");

            System.IO.Directory.CreateDirectory(textureDir);

            image.SaveToDDSFile(DDS_FLAGS.NONE, textureDir + "/skyrimgalaxy.dds");
            image.Dispose();

            string folder = getFullPath("work");
            string target = getFullPath("UltraHiResNightsky.zip");

            ZipFile.CreateFromDirectory(folder, target, CompressionLevel.Fastest, false);

            Close();
        }
예제 #4
0
        /// <summary>
        /// Matches a given DDS file on the disk to a D3DTX object.
        /// </summary>
        /// <param name="ddsPath"></param>
        /// <param name="d3dtx"></param>
        /// <param name="options"></param>
        public void Match_DDS_With_D3DTX(string ddsPath, D3DTX_Master d3dtx, DDS_Matching_Options options)
        {
            //load in the DDS image using DirectXTexNet
            ScratchImage scratchImage = TexHelper.Instance.LoadFromDDSFile(ddsPath, DDS_FLAGS.NONE);

            //create our main variables that will be used when doing conversion operations
            int             d3dtx_width     = 0;
            int             d3dtx_height    = 0;
            int             d3dtx_mipAmount = 0;
            T3SurfaceFormat d3dtx_format    = T3SurfaceFormat.eSurface_DXT1;
            T3SurfaceGamma  d3dtx_gamma     = T3SurfaceGamma.eSurfaceGamma_sRGB;

            //assign the values depending on which version is active
            if (d3dtx.d3dtx9 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx9.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx9.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx9.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx9.mSurfaceFormat;
                d3dtx_gamma     = d3dtx.d3dtx9.mSurfaceGamma;
            }
            else if (d3dtx.d3dtx8 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx8.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx8.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx8.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx8.mSurfaceFormat;
                d3dtx_gamma     = d3dtx.d3dtx8.mSurfaceGamma;
            }
            else if (d3dtx.d3dtx7 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx7.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx7.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx7.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx7.mSurfaceFormat;
                d3dtx_gamma     = d3dtx.d3dtx7.mSurfaceGamma;
            }
            else if (d3dtx.d3dtx6 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx6.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx6.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx6.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx6.mSurfaceFormat;
                d3dtx_gamma     = T3SurfaceGamma.eSurfaceGamma_sRGB; //this version doesn't have a surface gamma field, so give it an SRGB by default
            }
            else if (d3dtx.d3dtx5 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx5.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx5.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx5.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx5.mSurfaceFormat;
                d3dtx_gamma     = T3SurfaceGamma.eSurfaceGamma_sRGB; //this version doesn't have a surface gamma field, so give it an SRGB by default
            }
            else if (d3dtx.d3dtx4 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx4.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx4.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx4.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx4.mSurfaceFormat;
                d3dtx_gamma     = T3SurfaceGamma.eSurfaceGamma_sRGB; //this version doesn't have a surface gamma field, so give it an SRGB by default
            }

            //-------------------------------------- CONVERSION START --------------------------------------
            //change the compression if needed
            if (options.MatchCompression)
            {
                DXGI_FORMAT dxgi_format = DDS_Functions.GetSurfaceFormatAsDXGI(d3dtx_format, d3dtx_gamma);
                scratchImage.Convert(dxgi_format, TEX_FILTER_FLAGS.DITHER, 0.0f);
            }

            //rescale the image to match if needed
            if (options.MatchResolution)
            {
                scratchImage.Resize(d3dtx_width, d3dtx_height, TEX_FILTER_FLAGS.CUBIC);
            }

            //generate mip maps if needed
            if (options.GenerateMipMaps)
            {
                if (options.MatchMipMapCount)
                {
                    scratchImage.GenerateMipMaps(0, TEX_FILTER_FLAGS.CUBIC, d3dtx_mipAmount, false);
                }
                else
                {
                    scratchImage.GenerateMipMaps(0, TEX_FILTER_FLAGS.CUBIC, 0, false);
                }
            }

            //resave the newly modified DDS
            scratchImage.SaveToDDSFile(DDS_FLAGS.NONE, ddsPath);
        }
예제 #5
0
 protected override void FormOnSaveImage(string filename)
 {
     _currentDDS.SaveToDDSFile(DDS_FLAGS.NONE, filename);
 }
예제 #6
0
        /* Import a file to replace the selected PAK entry */
        private void ImportSelectedFile()
        {
            if (FileTree.SelectedNode == null || ((TreeItem)FileTree.SelectedNode.Tag).Item_Type != TreeItemType.EXPORTABLE_FILE)
            {
                MessageBox.Show("Please select a file from the list.", "No file selected.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //If import file is DDS, check first to see if it can be imported as WIC format
            string      filter     = "Import File|*" + Path.GetExtension(FileTree.SelectedNode.Text);
            DXGI_FORMAT baseFormat = DXGI_FORMAT.UNKNOWN;

            if (Path.GetExtension(FileTree.SelectedNode.Text).ToUpper() == ".DDS")
            {
                byte[] ImageFile = GetFileAsBytes(((TreeItem)FileTree.SelectedNode.Tag).String_Value);
                try
                {
                    ScratchImage img = TexHelper.Instance.LoadFromDDSMemory(Marshal.UnsafeAddrOfPinnedArrayElement(ImageFile, 0), ImageFile.Length, DDS_FLAGS.NONE);
                    baseFormat = img.GetMetadata().Format;
                    if (baseFormat != DXGI_FORMAT.UNKNOWN)
                    {
                        filter = "PNG Image|*.png|DDS Image|*.dds";                                    //Can import as WIC
                    }
                }
                catch { }
            }

            //Allow selection of a file (force extension), then drop it in
            OpenFileDialog FilePicker = new OpenFileDialog();

            FilePicker.Filter = filter;
            if (FilePicker.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;

                //Special import for DDS conversion
                bool ImportOK           = true;
                bool ImportingConverted = false;
                if (baseFormat != DXGI_FORMAT.UNKNOWN && Path.GetExtension(FilePicker.FileName).ToUpper() == ".PNG")
                {
                    try
                    {
                        ScratchImage img      = TexHelper.Instance.LoadFromWICFile(FilePicker.FileName, WIC_FLAGS.FORCE_RGB).GenerateMipMaps(TEX_FILTER_FLAGS.DEFAULT, 10); /* Was using 11, but gives remainders - going for 10 */
                        ScratchImage imgDecom = img.Compress(DXGI_FORMAT.BC7_UNORM, TEX_COMPRESS_FLAGS.BC7_QUICK, 0.5f);                                                    //TODO use baseFormat
                        imgDecom.SaveToDDSFile(DDS_FLAGS.FORCE_DX10_EXT, FilePicker.FileName + ".DDS");
                        FilePicker.FileName += ".DDS";
                        ImportingConverted   = true;
                    }
                    catch (Exception e)
                    {
                        //MessageBox.Show(e.ToString());
                        ImportOK = false;
                        MessageBox.Show("Failed to import as PNG!\nPlease try again as DDS.", AlienErrors.ErrorMessageTitle(PAKReturnType.FAIL_UNKNOWN), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                //Regular import
                if (ImportOK)
                {
                    foreach (PAK thisPAK in AlienPAKs)
                    {
                        thisPAK.ImportFile(((TreeItem)FileTree.SelectedNode.Tag).String_Value, FilePicker.FileName);
                    }
                    if (ImportingConverted)
                    {
                        File.Delete(FilePicker.FileName);                     //We temp dump out a converted file, which this cleans up
                    }
                    MessageBox.Show(AlienErrors.ErrorMessageBody(PAKReturnType.SUCCESS), AlienErrors.ErrorMessageTitle(PAKReturnType.SUCCESS), MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                Cursor.Current = Cursors.Default;
            }
            UpdateSelectedFilePreview();
        }