Ejemplo n.º 1
0
        public static void WriteToTarga(this DdsImage dds, string fileName, DdsOutputArgs args)
        {
            var source = dds.ToBitmapSource(args);
            var format = args.Options.HasFlag(DecompressOptions.Bgr24) ? Imaging.PixelFormat.Format24bppRgb : Imaging.PixelFormat.Format32bppArgb;

            WriteToTarga(source, fileName, format);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves DdsImage into Data.
        /// </summary>
        public void SaveDds(bool onlySaveIfEdited = true)
        {
            if (DdsImage == null || (!wasEdited && onlySaveIfEdited))
            {
                return;
            }

            try
            {
                _loadDdsLock = true;
                ImageEngineImage newImage = null;

                using (MemoryStream png = new MemoryStream())
                {
                    DdsImage.Save(png);
                    newImage = new ImageEngineImage(png);
                }
                Data = newImage.Save(ImageFormat, MipHandling.Default).ToList();
            }
            finally
            {
                wasReloaded  = true;
                _loadDdsLock = false;
            }
        }
Ejemplo n.º 3
0
        public static void WriteToTarga(this DdsImage dds, string fileName, DecompressOptions options, CubemapLayout layout)
        {
            var source = dds.ToBitmapSource(options, layout);
            var format = options.HasFlag(DecompressOptions.Bgr24) ? Imaging.PixelFormat.Format24bppRgb : Imaging.PixelFormat.Format32bppArgb;

            WriteToTarga(source, fileName, format);
        }
Ejemplo n.º 4
0
        private static DdsImage CreateDdsPreview(Stream stream)
        {
            Contract.Requires(stream != null);
            stream.Seek(0, SeekOrigin.Begin);

            DdsImage image = null;

            try
            {
                image = new DdsImage(stream);
            }
            catch (Exception)
            {
                return(null);
            }

            if (!image.IsValid)
            {
                image.Dispose();
                return(null);
            }

            stream.Dispose();
            return(image);
        }
Ejemplo n.º 5
0
 public (bool TextureExists, IntPtr TextureHandle) GetFfxTextureIntPtr(int textureId)
 {
     if (!_loadedTexturesDictionary.ContainsKey(textureId))
     {
         var a = _ffxTexturesIEnumerable.Where(item => item.Name.Contains($"s{textureId.ToString("00000")}.tpf"));
         if (a.Any())
         {
             var tpfBytes        = a.First().Bytes;
             var tpfTexturesList = TPF.Read(tpfBytes).Textures;
             if (tpfTexturesList.Any())
             {
                 var            ddsBytes       = tpfTexturesList.First().Bytes;
                 DdsImage       ddsImage       = new DdsImage(ddsBytes);
                 Image <Rgba32> image          = Image.LoadPixelData <Rgba32>(ddsImage.Data, ddsImage.Width, ddsImage.Height);
                 var            img            = new ImageSharpTexture(image);
                 var            veldridTexture = img.CreateDeviceTexture(MainUserInterface.Gd, MainUserInterface.Gd.ResourceFactory);
                 var            textureHandle  = MainUserInterface.Controller.GetOrCreateImGuiBinding(MainUserInterface.Gd.ResourceFactory, veldridTexture);
                 veldridTexture.Dispose();
                 _loadedTexturesDictionary.Add(textureId, textureHandle);
                 return(true, textureHandle);
             }
         }
         return(false, IntPtr.Zero);
     }
     else
     {
         return(true, _loadedTexturesDictionary[textureId]);
     }
 }
Ejemplo n.º 6
0
        public static unsafe Document Load(Stream input)
        {
            Document doc = null;

            using (DdsImage image = DdsNative.Load(input))
            {
                doc = new Document(image.Width, image.Height);

                BitmapLayer layer = Layer.CreateBackgroundLayer(image.Width, image.Height);

                Surface surface = layer.Surface;

                for (int y = 0; y < surface.Height; ++y)
                {
                    ColorRgba *src = image.GetRowAddressUnchecked(y);
                    ColorBgra *dst = surface.GetRowAddressUnchecked(y);

                    for (int x = 0; x < surface.Width; ++x)
                    {
                        dst->R = src->R;
                        dst->G = src->G;
                        dst->B = src->B;
                        dst->A = src->A;

                        ++src;
                        ++dst;
                    }
                }

                doc.Layers.Add(layer);
            }

            return(doc);
        }
Ejemplo n.º 7
0
        public void LoadImage(DdsImage ddsImg)
        {
            imageBox1.Image = ddsImg.BitmapImage;

            origImg = imageBox1.Image;

            Resize += FrmTextureFile_Resize;
        }
Ejemplo n.º 8
0
 public static void WriteToDxgi(this DdsImage dds, string fileName)
 {
     if (dds.FormatCode == (int)FourCC.XBOX)
     {
         dds = dds.AsUncompressed();
     }
     dds.WriteToDisk(fileName);
 }
Ejemplo n.º 9
0
        public void LoadImage(Stream stream)
        {
            DdsImage ddsImg = new DdsImage(stream);

            imageBox1.Image = ddsImg.BitmapImage;

            origImg = imageBox1.Image;

            Resize += FrmTextureFile_Resize;
        }
Ejemplo n.º 10
0
        public void LoadImage(string imgPath)
        {
            DdsImage ddsImg = new DdsImage(System.IO.File.ReadAllBytes(imgPath));

            imageBox1.Image = ddsImg.BitmapImage;

            origImg = imageBox1.Image;

            Resize += FrmTextureFile_Resize;

            imageBox1.Image = ddsImg.BitmapImage;
        }
Ejemplo n.º 11
0
        public void LoadImage(IBitmap image, string fileName)
        {
            bitmap = image;

            TabModel.ToolTip = fileName;
            TabModel.Header  = Utils.GetFileName(fileName);
            sourceName       = Utils.GetFileNameWithoutExtension(fileName);

            Indexes = Enumerable.Range(0, bitmap.SubmapCount);
            dds     = bitmap.ToDds(0);
            Render();
            CoerceValue(HasMultipleProperty);
        }
Ejemplo n.º 12
0
        public void LoadImage(string imgPath)
        {
            var ddsImg = new DdsImage(File.ReadAllBytes(imgPath));

            pictureBox1.Image = ddsImg.BitmapImage;

            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
            pictureBox1.Anchor   = AnchorStyles.None;

            origImg = pictureBox1.Image;

            ResizeImage();

            Resize += FrmTextureFile_Resize;
        }
Ejemplo n.º 13
0
        public RawBitmap Decode(DdsImage image)
        {
            switch (image.PixelFormat.FourCc)
            {
            case 0x00000000:     // uncompressed
                return(new RawBitmap
                {
                    Width = image.Width,
                    Height = image.Height,
                    Data = _rawBitmapDecoder.Decode32Bit(image.Data, image.Width, image.Height)
                });

            case 0x31545844:     // DXT1
                return(new RawBitmap
                {
                    Width = image.Width,
                    Height = image.Height,
                    Data = _dxtDecoder.DecodeDxt1(image.Data, image.Width, image.Height,
                                                  image.PixelFormat.Flags.HasFlag(DdsPixelFormatFlags.DDPF_ALPHAPIXELS))
                });

            case 0x33545844:     // DXT3
                return(new RawBitmap
                {
                    Width = image.Width,
                    Height = image.Height,
                    Data = _dxtDecoder.DecodeDxt3(image.Data, image.Width, image.Height)
                });

            case 0x35545844:     // DXT5
                return(new RawBitmap
                {
                    Width = image.Width,
                    Height = image.Height,
                    Data = _dxtDecoder.DecodeDxt5(image.Data, image.Width, image.Height)
                });

            default:
                throw new RhythmCodexException($"Unsupported FourCC: 0x{image.PixelFormat.FourCc:X8}");
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Set's preview to DDS image.
        /// </summary>
        /// <param name="stream">DDS image stream.</param>
        private void SetDdsPreview(DdsImage image)
        {
            Contract.Requires(image != null);

            IntPtr       hBitmap = image.BitmapImage.GetHbitmap();
            BitmapSource source  = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            this.PreviewImageBox.Source = source;

            NativeMethods.DeleteObject(hBitmap);
            image.Dispose();

            ChangeControlsVisibilityForFileType(PreviewFileType.Dds);

            SetTextWithTip(this.PreviewText, "Preview",
                           source.PixelWidth + "x" + source.PixelHeight);
        }
Ejemplo n.º 15
0
        public RgbColor GetDdsColor()
        {
            if (DdsImage == null)
            {
                throw new InvalidOperationException("GetDdsColor: DdsImage was null.");
            }
            List <RgbColor> colors = new List <RgbColor>();

            //Lazy code. Checking every single pixel would be WAY too slow, so we just skim through them instead.
            for (int i = 0; i < DdsImage.Width; i += 15)
            {
                if (i > DdsImage.Width)
                {
                    break;
                }

                for (int a = 0; a < DdsImage.Height; a += 15)
                {
                    if (a > DdsImage.Height)
                    {
                        break;
                    }

                    var      pixel    = DdsImage.GetPixel(i, a);
                    RgbColor rgbColor = new RgbColor(pixel.R, pixel.G, pixel.B);

                    if (!rgbColor.IsWhiteOrBlack)
                    {
                        colors.Add(rgbColor);
                    }
                }
            }

            if (colors.Count == 0)
            {
                return(new RgbColor(255, 255, 255));
            }

            return(ColorEx.GetAverageColor(colors));
        }
Ejemplo n.º 16
0
        public DdsImage Read(Stream source, int length)
        {
            var reader = new BinaryReader(source);

            var ddsId = reader.ReadInt32();

            if (ddsId != MagicId)
            {
                throw new RhythmCodexException($"Invalid DDS identifier. Found: 0x{ddsId:X8}");
            }

            var ddsSize = reader.ReadInt32();

            if (ddsSize != 124)
            {
                throw new RhythmCodexException($"Invalid DDS size. Found: 0x{ddsSize:X8}");
            }

            var result = new DdsImage
            {
                Flags             = (DdsFlags)reader.ReadInt32(),
                Height            = reader.ReadInt32(),
                Width             = reader.ReadInt32(),
                PitchOrLinearSize = reader.ReadInt32(),
                Depth             = reader.ReadInt32(),
                MipMapCount       = reader.ReadInt32(),
                Reserved1         = Enumerable.Range(0, 11).Select(i => reader.ReadInt32()).ToArray(),
                PixelFormat       = ReadPixelFormat(reader),
                Caps1             = (DdsCaps1)reader.ReadInt32(),
                Caps2             = reader.ReadInt32(),
                Caps3             = reader.ReadInt32(),
                Caps4             = reader.ReadInt32(),
                Reserved2         = reader.ReadInt32(),
                Data = reader.ReadBytes(length - 128)
            };

            return(result);
        }
Ejemplo n.º 17
0
        public static DdsImage GetDds(int height, int width, object format, bool isCubemap, byte[] data, bool isPC = false)
        {
            var knownFormat = format.AsKnown();

            if (knownFormat == KnownTextureFormat.Unknown)
            {
                throw new ArgumentException("Could not translate to a known texture format.", nameof(format));
            }

            DdsImage dds;

            if (isPC && knownFormat == KnownTextureFormat.DXN)
            {
                dds = new DdsImage(height, width, XboxFormat.DXN_SNorm, DxgiTextureType.Texture2D, data);
            }
            else if (dxgiLookup.ContainsKey(knownFormat))
            {
                dds = new DdsImage(height, width, dxgiLookup[knownFormat], DxgiTextureType.Texture2D, data);
            }
            else if (xboxLookup.ContainsKey(knownFormat))
            {
                dds = new DdsImage(height, width, xboxLookup[knownFormat], DxgiTextureType.Texture2D, data);
            }
            else
            {
                throw Exceptions.BitmapFormatNotSupported(format.ToString());
            }

            if (isCubemap)
            {
                dds.TextureFlags      = TextureFlags.DdsSurfaceFlagsCubemap;
                dds.CubemapFlags      = CubemapFlags.DdsCubemapAllFaces;
                dds.DX10ResourceFlags = D3D10ResourceMiscFlags.TextureCube;
            }

            return(dds);
        }
Ejemplo n.º 18
0
 public void LoadAllFfxTexturesInMemory(IEnumerable <BinderFile> ffxTexturesIEnumerable)
 {
     foreach (var a in ffxTexturesIEnumerable)
     {
         var tpfBytes        = a.Bytes;
         var tpfTexturesList = TPF.Read(tpfBytes).Textures;
         if (tpfTexturesList.Any())
         {
             var tpf = tpfTexturesList.First();
             if (int.TryParse(tpf.Name.TrimStart('s'), out int textureId) && !_loadedTexturesDictionary.ContainsKey(textureId))
             {
                 var            ddsBytes = tpf.Bytes;
                 DdsImage       ddsImage = new DdsImage(ddsBytes);
                 Image <Rgba32> image    = Image.LoadPixelData <Rgba32>(ddsImage.Data, ddsImage.Width, ddsImage.Height);
                 for (int y = 0; y < image.Height; y++)
                 {
                     for (int x = 0; x < image.Width; x++)
                     {
                         var rgba32 = image[x, y];
                         var r      = rgba32.R;
                         var g      = rgba32.G;
                         var b      = rgba32.B;
                         rgba32.R    = b;
                         rgba32.G    = g;
                         rgba32.B    = r;
                         image[x, y] = rgba32; //Set Inverted Pixels
                     }
                 }
                 var img            = new ImageSharpTexture(image);
                 var veldridTexture = img.CreateDeviceTexture(MainUserInterface.Gd, MainUserInterface.Gd.ResourceFactory);
                 var textureHandle  = MainUserInterface.Controller.GetOrCreateImGuiBinding(MainUserInterface.Gd.ResourceFactory, veldridTexture);
                 veldridTexture.Dispose();
                 _loadedTexturesDictionary.Add(textureId, textureHandle);
             }
         }
     }
 }
Ejemplo n.º 19
0
 static DdsNative()
 {
     DdsImage.Initalize();
 }
Ejemplo n.º 20
0
        private bool SaveImage(IBitmap bitmap, string baseDir)
        {
            var extracted = 0;

            for (int i = 0; i < bitmap.SubmapCount; i++)
            {
                var fileName = MakePath(bitmap.Class, bitmap.Name, baseDir);
                var ext      = "." + Settings.BitmapFormat.ToString().ToLower();

                if (bitmap.SubmapCount > 1)
                {
                    fileName += $"[{i}]";
                }

                if (Settings.BitmapFormat == BitmapFormat.DDS)
                {
                    if (Settings.OverwriteExisting || !File.Exists(fileName + ext))
                    {
                        var rawDds = bitmap.ToDds(i);
                        rawDds.WriteToDxgi(fileName + ext);
                        extracted++;
                    }
                    continue;
                }

                var outputs = new List <Tuple <string, DecompressOptions> >();

                ImageFormat format;
                if (Settings.BitmapFormat == BitmapFormat.PNG)
                {
                    format = ImageFormat.Png;
                }
                else if (Settings.BitmapFormat == BitmapFormat.TIF)
                {
                    format = ImageFormat.Tiff;
                }
                else if (Settings.BitmapFormat == BitmapFormat.JPEG)
                {
                    format = ImageFormat.Jpeg;
                }
                else //if (Settings.BitmapFormat == BitmapFormat.TGA)
                {
                    format = null;
                }

                if (Settings.BitmapMode == BitmapMode.Default)
                {
                    outputs.Add(Tuple.Create(fileName + ext, DecompressOptions.Default));
                }
                else if (Settings.BitmapMode == BitmapMode.Bgr24)
                {
                    outputs.Add(Tuple.Create(fileName + ext, DecompressOptions.Bgr24));
                }
                else if (Settings.BitmapMode == BitmapMode.IsolateAlpha)
                {
                    outputs.AddRange(GetParamsIsolateAlpha(fileName, ext));
                }
                else if (Settings.BitmapMode == BitmapMode.IsolateAll)
                {
                    outputs.AddRange(GetParamsIsolateAll(fileName, ext));
                }
                else if (Settings.BitmapMode == BitmapMode.MixedIsolate)
                {
                    outputs.AddRange(GetParamsMixedIsolate(fileName, ext));
                }

                DdsImage dds = null;
                foreach (var param in outputs)
                {
                    if (!Settings.OverwriteExisting && File.Exists(param.Item1))
                    {
                        continue;
                    }

                    if (dds == null)
                    {
                        dds = bitmap.ToDds(i);
                    }

                    var args = new DdsOutputArgs(param.Item2, bitmap.CubeLayout);
                    if (format != null)
                    {
                        dds.WriteToDisk(param.Item1, format, args);
                    }
                    else //if (Settings.BitmapFormat == BitmapFormat.TGA)
                    {
                        dds.WriteToTarga(param.Item1, args);
                    }

                    extracted++;
                }
            }

            return(extracted > 0);
        }