Example #1
0
        protected override Image GetDrawingImage()
        {
            if (_currentDDS != null && !_currentDDS.IsDisposed)
            {
                _currentDDS.Dispose();
            }

            _currentDDS = GetScratchImage();

            try
            {
                Guid codec = TexHelper.Instance.GetWICCodec(WICCodecs.PNG);

                UnmanagedMemoryStream imageStream = _currentDDS.SaveToWICMemory(0, WIC_FLAGS.NONE, codec);

                Image image = Image.FromStream(imageStream);

                imageStream.Close();
                imageStream?.Dispose();

                return(image);
            }
            catch (Exception)
            {
                return(null);
            }
        }
        private async System.Threading.Tasks.Task <bool> CompareImagesAsync(ScratchImage dds, ScratchImage png)
        {
            Guid        codec    = DirectXTexNet.TexHelper.Instance.GetWICCodec(WICCodecs.PNG);
            TexMetadata metadata = dds.GetMetadata();

            UnmanagedMemoryStream ddsStream;

            if (IsCompressed(metadata.Format))
            {
                using (ScratchImage decompressed = dds.Decompress(DXGI_FORMAT.UNKNOWN))
                {
                    ddsStream = decompressed.SaveToWICMemory(0, WIC_FLAGS.NONE, codec);
                }
            }
            else
            {
                ddsStream = dds.SaveToWICMemory(0, WIC_FLAGS.NONE, codec);
            }

            UnmanagedMemoryStream pngStream = png.SaveToWICMemory(0, WIC_FLAGS.NONE, codec);

            var  scompare = new StreamCompare();
            bool result   = await scompare.AreEqualAsync(ddsStream, pngStream);

            ddsStream.Dispose();
            pngStream.Dispose();

            return(result);
        }
Example #3
0
        public static unsafe byte[] ConvertDDS(Stream ddsSteam, DXGI_FORMAT targetFormat, WICCodecs codec, int frame)
        {
            try {
                CoInitializeEx(IntPtr.Zero, CoInit.MultiThreaded | CoInit.SpeedOverMemory);

                byte[] data = new byte[ddsSteam.Length];
                ddsSteam.Read(data, 0, data.Length);
                ScratchImage scratch = null;
                try
                {
                    fixed(byte *dataPin = data)
                    {
                        scratch = TexHelper.Instance.LoadFromDDSMemory((IntPtr)dataPin, data.Length, DDS_FLAGS.NONE);
                        TexMetadata info = scratch.GetMetadata();

                        if (TexHelper.Instance.IsCompressed(info.Format))
                        {
                            ScratchImage temp = scratch.Decompress(DXGI_FORMAT.UNKNOWN);
                            scratch.Dispose();
                            scratch = temp;

                            info = scratch.GetMetadata();
                        }

                        if (info.Format != targetFormat)
                        {
                            ScratchImage temp = scratch.Convert(targetFormat, TEX_FILTER_FLAGS.DEFAULT, 0.5f);
                            scratch.Dispose();
                            scratch = temp;
                        }

                        UnmanagedMemoryStream stream = null;
                        var isMultiFrame             = codec == WICCodecs.GIF || codec == WICCodecs.TIFF;

                        if (info.ArraySize == 1 || !isMultiFrame)
                        {
                            stream = scratch.SaveToWICMemory(frame, WIC_FLAGS.NONE, TexHelper.Instance.GetWICCodec(codec));
                        }
                        else
                        {
                            stream = scratch.SaveToWICMemory(0, info.ArraySize, WIC_FLAGS.ALL_FRAMES, TexHelper.Instance.GetWICCodec(codec));
                        }

                        if (stream == null)
                        {
                            return(null);
                        }

                        byte[] tex = new byte[stream.Length];
                        stream.Read(tex, 0, tex.Length);
                        scratch.Dispose();
                        return(tex);
                    }
                } catch {
                    if (scratch != null && scratch.IsDisposed == false)
                    {
                        scratch.Dispose();
                    }
                }
            } catch {
                // ignored
            }

            return(null);
        }
Example #4
0
        /* Update file preview */
        private void UpdateSelectedFilePreview()
        {
            //First, reset the GUI
            groupBox1.Visible = false;
            filePreviewImage.BackgroundImage = null;
            fileNameInfo.Text  = "";
            fileSizeInfo.Text  = "";
            fileTypeInfo.Text  = "";
            exportFile.Enabled = false;
            importFile.Enabled = false;
            removeFile.Enabled = false;
            addFile.Enabled    = true; //Eventually move this to only be enabled on directory selection

            //Exit early if nothing selected
            if (FileTree.SelectedNode == null)
            {
                return;
            }

            //Handle file selection
            if (((TreeItem)FileTree.SelectedNode.Tag).Item_Type == TreeItemType.EXPORTABLE_FILE)
            {
                string FileName = ((TreeItem)FileTree.SelectedNode.Tag).String_Value;

                //Populate filename/type info
                fileNameInfo.Text = Path.GetFileName(FileName);
                fileTypeInfo.Text = GetFileTypeDescription(Path.GetExtension(FileName));

                //Populate file size info
                int FileSize = -1;
                foreach (PAK thisPAK in AlienPAKs)
                {
                    if (FileSize == -1)
                    {
                        FileSize = thisPAK.GetFileSize(FileName);
                    }
                }
                if (FileSize == -1)
                {
                    return;
                }
                fileSizeInfo.Text = FileSize.ToString() + " bytes";

                //Show file preview if selected an image
                if (Path.GetExtension(FileName).ToUpper() == ".DDS")
                {
                    try
                    {
                        byte[] ImageFile = GetFileAsBytes(FileName);

                        //Using the DDS, try and convert it to Bitmap and display it
                        using (ScratchImage img = TexHelper.Instance.LoadFromDDSMemory(Marshal.UnsafeAddrOfPinnedArrayElement(ImageFile, 0), ImageFile.Length, DDS_FLAGS.NONE))
                        {
                            ScratchImage          imgDecom = img.Decompress(DXGI_FORMAT.UNKNOWN);
                            UnmanagedMemoryStream imgJpg   = imgDecom.SaveToWICMemory(0, WIC_FLAGS.NONE, TexHelper.Instance.GetWICCodec(WICCodecs.PNG));
                            ResizeImagePreview((Bitmap)System.Drawing.Image.FromStream(imgJpg));
                        }
                    }
                    catch (Exception e)
                    {
                        //MessageBox.Show(e.ToString());
                        if (File.Exists("temp.dds"))
                        {
                            File.Delete("temp.dds");
                        }
                    }
                }
                groupBox1.Visible = (filePreviewImage.BackgroundImage != null);

                //Enable buttons
                exportFile.Enabled = true;
                importFile.Enabled = true;
                removeFile.Enabled = true;
            }
        }
Example #5
0
        public static byte[] ConvertDDS(GUIDEntry value, DXGI_FORMAT targetFormat, ImageFormat imageFormat, int frame)
        {
            try
            {
                if (GetDataType(value) != DataType.Image)
                {
                    return(null);
                }
                teTexture texture = new teTexture(IOHelper.OpenFile(value));
                if (texture.PayloadRequired)
                {
                    ulong payload = texture.GetPayloadGUID(value.GUID);
                    if (value.APM.FirstOccurence.ContainsKey(payload))
                    {
                        texture.LoadPayload(IOHelper.OpenFile(value.APM.FirstOccurence[payload]));
                    }
                    else
                    {
                        return(null);
                    }
                }

                Stream ms = texture.SaveToDDS();

                CoInitializeEx(IntPtr.Zero, CoInit.MultiThreaded | CoInit.SpeedOverMemory);

                byte[] data = new byte[ms.Length];
                ms.Read(data, 0, data.Length);
                ScratchImage scratch = null;
                try
                {
                    unsafe
                    {
                        fixed(byte *dataPin = data)
                        {
                            scratch = TexHelper.Instance.LoadFromDDSMemory((IntPtr)dataPin, data.Length, DDS_FLAGS.NONE);
                            TexMetadata info = scratch.GetMetadata();

                            if (TexHelper.Instance.IsCompressed(info.Format))
                            {
                                ScratchImage temp = scratch.Decompress(frame, DXGI_FORMAT.UNKNOWN);
                                scratch.Dispose();
                                scratch = temp;
                            }
                            info = scratch.GetMetadata();

                            if (info.Format != targetFormat)
                            {
                                ScratchImage temp = scratch.Convert(targetFormat, TEX_FILTER_FLAGS.DEFAULT, 0.5f);
                                scratch.Dispose();
                                scratch = temp;
                            }

                            UnmanagedMemoryStream stream = null;

                            if (imageFormat == ImageFormat.TGA)
                            {
                                stream = scratch.SaveToTGAMemory(frame < 0 ? 0 : frame);
                            }
                            else
                            {
                                WICCodecs codec        = WICCodecs.PNG;
                                bool      isMultiframe = false;
                                switch (imageFormat)
                                {
                                case ImageFormat.BMP:
                                    codec = WICCodecs.BMP;
                                    break;

                                case ImageFormat.GIF:
                                    codec        = WICCodecs.GIF;
                                    isMultiframe = true;
                                    break;

                                case ImageFormat.JPEG:
                                    codec = WICCodecs.JPEG;
                                    break;

                                case ImageFormat.PNG:
                                    codec = WICCodecs.PNG;
                                    break;

                                case ImageFormat.TIF:
                                    codec        = WICCodecs.TIFF;
                                    isMultiframe = true;
                                    break;
                                }

                                if (frame < 0)
                                {
                                    if (!isMultiframe)
                                    {
                                        frame = 0;
                                    }
                                    else
                                    {
                                        stream = scratch.SaveToWICMemory(0, info.ArraySize, WIC_FLAGS.ALL_FRAMES, TexHelper.Instance.GetWICCodec(codec));
                                    }
                                }
                                if (frame >= 0)
                                {
                                    stream = scratch.SaveToWICMemory(frame, WIC_FLAGS.NONE, TexHelper.Instance.GetWICCodec(codec));
                                }

                                byte[] tex = new byte[stream.Length];
                                stream.Read(tex, 0, tex.Length);
                                scratch.Dispose();
                                return(tex);
                            }
                        }
                    }
                }
                catch
                {
                    if (scratch != null && scratch.IsDisposed == false)
                    {
                        scratch?.Dispose();
                    }
                }
            }
            catch
            {
            }
            return(null);
        }
Example #6
0
        public static unsafe byte[] ConvertDDS(Stream ddsSteam, DXGI_FORMAT targetFormat, ImageFormat imageFormat, int frame)
        {
            try {
                CoInitializeEx(IntPtr.Zero, CoInit.MultiThreaded | CoInit.SpeedOverMemory);

                byte[] data = new byte[ddsSteam.Length];
                ddsSteam.Read(data, 0, data.Length);
                ScratchImage scratch = null;
                try
                {
                    fixed(byte *dataPin = data)
                    {
                        scratch = TexHelper.Instance.LoadFromDDSMemory((IntPtr)dataPin, data.Length, DDS_FLAGS.NONE);
                        TexMetadata info = scratch.GetMetadata();

                        if (TexHelper.Instance.IsCompressed(info.Format))
                        {
                            ScratchImage temp = scratch.Decompress(frame, DXGI_FORMAT.UNKNOWN);
                            scratch.Dispose();
                            scratch = temp;
                        }

                        info = scratch.GetMetadata();

                        if (info.Format != targetFormat)
                        {
                            ScratchImage temp = scratch.Convert(targetFormat, TEX_FILTER_FLAGS.DEFAULT, 0.5f);
                            scratch.Dispose();
                            scratch = temp;
                        }

                        UnmanagedMemoryStream stream = null;

                        if (imageFormat == ImageFormat.TGA)
                        {
                            stream = scratch.SaveToTGAMemory(frame < 0 ? 0 : frame);
                        }
                        else
                        {
                            WICCodecs codec        = WICCodecs.PNG;
                            bool      isMultiFrame = false;
                            switch (imageFormat)
                            {
                            case ImageFormat.BMP:
                                codec = WICCodecs.BMP;
                                break;

                            case ImageFormat.GIF:
                                codec        = WICCodecs.GIF;
                                isMultiFrame = true;
                                break;

                            case ImageFormat.JPEG:
                                codec = WICCodecs.JPEG;
                                break;

                            case ImageFormat.PNG:
                                codec = WICCodecs.PNG;
                                break;

                            case ImageFormat.TIF:
                                codec        = WICCodecs.TIFF;
                                isMultiFrame = true;
                                break;

                            case ImageFormat.TGA:
                                break;

                            default:
                                throw new ArgumentOutOfRangeException(nameof(imageFormat), imageFormat, null);
                            }

                            if (frame < 0)
                            {
                                if (!isMultiFrame)
                                {
                                    frame = 0;
                                }
                                else
                                {
                                    stream = scratch.SaveToWICMemory(0, info.ArraySize, WIC_FLAGS.ALL_FRAMES, TexHelper.Instance.GetWICCodec(codec));
                                }
                            }

                            if (frame >= 0)
                            {
                                stream = scratch.SaveToWICMemory(frame, WIC_FLAGS.NONE, TexHelper.Instance.GetWICCodec(codec));
                            }
                        }

                        if (stream == null)
                        {
                            return(null);
                        }

                        byte[] tex = new byte[stream.Length];
                        stream.Read(tex, 0, tex.Length);
                        scratch.Dispose();
                        return(tex);
                    }
                } catch {
                    if (scratch != null && scratch.IsDisposed == false)
                    {
                        scratch.Dispose();
                    }
                }
            } catch {
                // ignored
            }

            return(null);
        }