Exemplo n.º 1
0
        public FieldTexturePS3(Bitmap bitmap)
        {
            Field00 = 0x020200FF;
            Field08 = 0x00000001;
            Field0C = 0x00000000;
            Flags   = FieldTextureFlags.Flag2 | FieldTextureFlags.Flag4 | FieldTextureFlags.Flag80;

            var ddsFormat = DDSCodec.DetermineBestCompressedFormat(bitmap);
            var data      = DDSCodec.CompressPixelData(bitmap, ddsFormat);

            if (ddsFormat == DDSPixelFormatFourCC.DXT3)
            {
                Flags |= FieldTextureFlags.DXT3;
            }
            else if (ddsFormat == DDSPixelFormatFourCC.DXT5)
            {
                Flags |= FieldTextureFlags.DXT5;
            }

            MipMapCount = ( byte )1;
            Field1A     = 2;
            Field1B     = 0;
            Field1C     = 0xAAE4;
            Width       = ( short )bitmap.Width;
            Height      = ( short )bitmap.Height;
            Field24     = 1;
            Data        = data;
        }
Exemplo n.º 2
0
 /// <summary>
 /// Gets a stream containing DDS data. If the file is already in DDS format, it won't be re-encoded.
 /// </summary>
 /// <param name="filepath"></param>
 /// <returns></returns>
 public static Stream GetDDSStream(string filepath)
 {
     if (Path.GetExtension(filepath) == ".dds")
     {
         return(File.OpenRead(filepath));
     }
     else
     {
         return(new MemoryStream(DDSCodec.CompressImage(new Bitmap(filepath))));
     }
 }
Exemplo n.º 3
0
 public SpdTexture(Bitmap bitmap)
 {
     Id          = 1;
     Field04     = 0;
     Width       = bitmap.Width;
     Height      = bitmap.Height;
     Field18     = 0;
     Field1C     = 0;
     Description = "Created with <3 by Amicitia";
     Data        = DDSCodec.CompressImage(bitmap);
 }
Exemplo n.º 4
0
        public static Texture Encode(string sourceFileName)
        {
            if (sourceFileName.EndsWith(".dds", StringComparison.OrdinalIgnoreCase))
            {
                using (var source = File.OpenRead(sourceFileName))
                    return(Encode(source));
            }

            using (var bitmap = new Bitmap(sourceFileName))
                return(Encode(bitmap, DDSCodec.HasTransparency(bitmap) ? TextureFormat.DXT5 : TextureFormat.DXT1, true));
        }
Exemplo n.º 5
0
        public static unsafe Bitmap Decode(SubTexture subTexture)
        {
            var bitmap = new Bitmap(subTexture.Width, subTexture.Height);
            var rect   = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            if (subTexture.Format == TextureFormat.RGB)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

                fixed(byte *ptr = subTexture.Data)
                {
                    RGBtoBGR(ptr, ( byte * )bitmapData.Scan0, subTexture.Data.Length);
                }

                bitmap.UnlockBits(bitmapData);
            }
            else if (subTexture.Format == TextureFormat.RGBA)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                fixed(byte *ptr = subTexture.Data)
                {
                    ByteRGBAToInt32(ptr, ( int * )bitmapData.Scan0, subTexture.Data.Length);
                }

                bitmap.UnlockBits(bitmapData);
            }
            else if (subTexture.Format == TextureFormat.RGBA4)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                fixed(byte *ptr = subTexture.Data)
                {
                    RGBA4toRGBA(ptr, ( int * )bitmapData.Scan0, subTexture.Data.Length);
                }

                bitmap.UnlockBits(bitmapData);
            }
            else
            {
                var buffer = DDSCodec.DecompressPixelDataToRGBA(subTexture.Data, subTexture.Width, subTexture.Height,
                                                                TextureUtilities.GetDDSPixelFormat(subTexture.Format));
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                Marshal.Copy(buffer, 0, bitmapData.Scan0, buffer.Length);
                bitmap.UnlockBits(bitmapData);
            }

            return(bitmap);
        }
Exemplo n.º 6
0
        public static unsafe Bitmap Decode(Texture texture)
        {
            if (texture.IsYCbCr)
            {
                var bitmap = new Bitmap(texture[0].Width, texture[0].Height);
                var rect   = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

                var lumBuffer = DDSCodec.DecompressPixelDataToRGBA(
                    texture[0].Data, texture[0].Width, texture[0].Height,
                    TextureUtilities.GetDDSPixelFormat(texture.Format));

                var cbrBuffer = DDSCodec.DecompressPixelDataToRGBA(
                    texture[1].Data, texture[1].Width, texture[1].Height,
                    TextureUtilities.GetDDSPixelFormat(texture.Format));

                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                fixed(byte *lumPtr = lumBuffer)
                fixed(byte *cbrPtr = cbrBuffer)
                {
                    var lumIntPtr = ( int * )lumPtr;
                    var cbrIntPtr = ( int * )cbrPtr;

                    ConvertYCbCrToRGBA(lumIntPtr, cbrIntPtr, ( int * )bitmapData.Scan0, bitmap.Width, bitmap.Height);
                }

                bitmap.UnlockBits(bitmapData);
                return(bitmap);
            }

            if (texture.UsesDepth)
            {
                var bitmap = new Bitmap(texture.Width * texture.Depth, texture.Height);
                using (var gfx = Graphics.FromImage(bitmap))
                {
                    int currentIndex = 0;
                    foreach (int i in CubeMapToDDSCubeMap())
                    {
                        gfx.DrawImageUnscaled(Decode(texture[i, 0]), currentIndex++ *texture.Width, 0);
                    }
                }

                return(bitmap);
            }

            return(Decode(texture[0]));
        }
Exemplo n.º 7
0
        private static unsafe void Encode(SubTexture subTexture, Bitmap bitmap)
        {
            bool ownsBitmap = false;

            if (subTexture.Width != bitmap.Width || subTexture.Height != bitmap.Height)
            {
                ownsBitmap = true;
                bitmap     = new Bitmap(bitmap, subTexture.Width, subTexture.Height);
            }

            var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            if (subTexture.Format == TextureFormat.RGB)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                fixed(byte *ptr = subTexture.Data)
                {
                    BGRtoRGB(( byte * )bitmapData.Scan0, ptr, subTexture.Data.Length);
                }

                bitmap.UnlockBits(bitmapData);
            }
            else if (subTexture.Format == TextureFormat.RGBA)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                fixed(byte *ptr = subTexture.Data)
                {
                    Int32RGBAToByte(( int * )bitmapData.Scan0, ptr, subTexture.Data.Length);
                }

                bitmap.UnlockBits(bitmapData);
            }
            else
            {
                var compressedPixels =
                    DDSCodec.CompressPixelData(bitmap, TextureUtilities.GetDDSPixelFormat(subTexture.Format));
                Array.Copy(compressedPixels, subTexture.Data, subTexture.Data.Length);
            }

            if (ownsBitmap)
            {
                bitmap.Dispose();
            }
        }
Exemplo n.º 8
0
        public static Bitmap ImportBitmap(string path)
        {
            try
            {
                return(new Bitmap(path));
            }
            catch (Exception)
            {
                var ext = Path.GetExtension(path).ToLowerInvariant();
                switch (ext)
                {
                case ".dds":
                    return(DDSCodec.DecompressImage(path));

                default:
                    return(new Bitmap(32, 32));
                }
            }
        }
Exemplo n.º 9
0
        protected override void InitializeCore()
        {
            RegisterExportHandler <Bitmap>((path) => TextureDecoder.Decode(Data).Save(path));
            RegisterExportHandler <Texture>((path) => TextureDecoder.DecodeToDDS(Data, path));

            RegisterReplaceHandler <Bitmap>((path) =>
            {
                using (var bitmap = new Bitmap(path))
                {
                    if (Data.IsYCbCr)
                    {
                        var format = DDSCodec.HasTransparency(bitmap) ? TextureFormat.RGBA : TextureFormat.RGB;

                        return(TextureEncoder.Encode(bitmap, format, false));
                    }

                    return(TextureEncoder.Encode(bitmap, Data.Format, Data.MipMapCount != 0));
                }
            });
            RegisterReplaceHandler <Texture>(TextureEncoder.Encode);
        }
Exemplo n.º 10
0
        private static Bitmap DecodeDDS(byte[] data)
        {
            try
            {
                // Prefer DDSCodec -- handles alpha properly but doesn't handle non pow 2 textures & DX10+ formats
                return(DDSCodec.DecompressImage(data));
            }
            catch (Exception)
            {
            }

            try
            {
                // Image engine SUCKS at alpha handling, but its better than nothing
                return(DecodeDDSWithImageEngine(data));
            }
            catch (Exception)
            {
            }

            // RIP
            Trace.WriteLine("Failed to decode DDS texture");
            return(new Bitmap(32, 32, PixelFormat.Format32bppArgb));
        }
Exemplo n.º 11
0
 protected override Bitmap GetBitmapCore(DDSStream obj)
 {
     return(DDSCodec.DecompressImage(obj));
 }
Exemplo n.º 12
0
        private static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine(Resources.HelpText);
                Console.ReadLine();
                return;
            }

            string sourceFileName      = null;
            string destinationFileName = null;

            foreach (string arg in args)
            {
                if (sourceFileName == null)
                {
                    sourceFileName = arg;
                }

                else if (destinationFileName == null)
                {
                    destinationFileName = arg;
                }
            }

            if (destinationFileName == null)
            {
                destinationFileName = sourceFileName;
            }

            if (File.GetAttributes(sourceFileName).HasFlag(FileAttributes.Directory))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, "bin");

                var textureSet = new TextureSet();
                var textures   = new SortedList <int, Texture>();
                foreach (string textureFileName in Directory.EnumerateFiles(sourceFileName))
                {
                    if (textureFileName.EndsWith(".dds", StringComparison.OrdinalIgnoreCase) ||
                        textureFileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
                    {
                        string cleanFileName = Path.GetFileNameWithoutExtension(textureFileName);
                        if (int.TryParse(cleanFileName, out int index))
                        {
                            Texture texture;

                            if (textureFileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
                            {
                                var bitmap = new Bitmap(textureFileName);
                                var format = TextureFormat.RGB;

                                if (DDSCodec.HasTransparency(bitmap))
                                {
                                    format = TextureFormat.RGBA;
                                }

                                texture = TextureEncoder.Encode(new Bitmap(textureFileName), format, false);
                            }

                            else
                            {
                                texture = TextureEncoder.Encode(textureFileName);
                            }

                            textures.Add(index, texture);
                        }

                        else
                        {
                            Console.WriteLine(
                                "WARNING: Skipped '{0}' because it didn't match the expected name format",
                                Path.GetFileName(textureFileName));
                        }
                    }
                }

                textureSet.Textures.Capacity = textures.Count;
                foreach (var texture in textures.Values)
                {
                    textureSet.Textures.Add(texture);
                }

                textureSet.Save(destinationFileName);
            }

            else if (sourceFileName.EndsWith(".bin", StringComparison.OrdinalIgnoreCase) ||
                     sourceFileName.EndsWith(".txd", StringComparison.OrdinalIgnoreCase))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, null);

                var textureSet = BinaryFile.Load <TextureSet>(sourceFileName);

                Directory.CreateDirectory(destinationFileName);
                for (int i = 0; i < textureSet.Textures.Count; i++)
                {
                    var    texture = textureSet.Textures[i];
                    string name    = string.IsNullOrEmpty(texture.Name) ? $"{i}" : texture.Name;

                    if (TextureFormatUtilities.IsCompressed(texture.Format))
                    {
                        TextureDecoder.DecodeToDDS(texture, Path.Combine(destinationFileName, $"{name}.dds"));
                    }

                    else
                    {
                        TextureDecoder.DecodeToPNG(texture, Path.Combine(destinationFileName, $"{name}.png"));
                    }
                }
            }
        }
Exemplo n.º 13
0
 public Bitmap GetBitmap()
 {
     return(mBitmap ?? (mBitmap = DDSCodec.DecompressImage(Data)));
 }
Exemplo n.º 14
0
        public static TextureInfo GetTextureInfo(Texture texture)
        {
            // Load DDS header so we can extract some info from it
            var ddsHeader = new DDSHeader(texture.Data);

            TexturePixelFormat format;

            switch (ddsHeader.PixelFormat.FourCC)
            {
            case DDSPixelFormatFourCC.DXT1:
                format = TexturePixelFormat.BC1;
                break;

            case DDSPixelFormatFourCC.DXT3:
                format = TexturePixelFormat.BC2;
                break;

            case DDSPixelFormatFourCC.DXT5:
                format = TexturePixelFormat.BC3;
                break;

            case DDSPixelFormatFourCC.A8R8G8B8:
                format = TexturePixelFormat.ARGB;
                break;

            case DDSPixelFormatFourCC.Unknown:
                // Maybe from a screen ripping tool, or something else
                Debug.WriteLine($"{nameof(GetTextureInfo)}({texture}): DDS FourCC not set");

                if (ddsHeader.PixelFormat.Flags.HasFlag(DDSPixelFormatFlags.RGB) &&
                    ddsHeader.PixelFormat.Flags.HasFlag(DDSPixelFormatFlags.AlphaPixels) &&
                    ddsHeader.PixelFormat.RGBBitCount == 32)
                {
                    Debug.WriteLine("Converting RGBA pixels to DDS");

                    // Read pixel colors
                    var pixelData = new Graphics.Color[(texture.Data.Length - DDSHeader.SIZE) / 4];
                    for (int i = 0; i < pixelData.Length; i++)
                    {
                        pixelData[i] = new Graphics.Color
                                       (
                            texture.Data[DDSHeader.SIZE + (i * 4) + 2],          // r
                            texture.Data[DDSHeader.SIZE + (i * 4) + 1],          // g
                            texture.Data[DDSHeader.SIZE + (i * 4) + 0],          // b
                            texture.Data[DDSHeader.SIZE + (i * 4) + 3]           // a
                                       );
                    }

                    // Create RGBA bitmap
                    var bitmap = BitmapHelper.Create(pixelData, ddsHeader.Width, ddsHeader.Height);

                    // Convert bitmap to DDS
                    texture.Data = DDSCodec.CompressImage(bitmap);
                    return(GetTextureInfo(texture));
                }
                else
                {
                    goto default;
                }

            default:
                throw new NotSupportedException($"Unsupported DDS pixel format");
            }

            return(new TextureInfo(texture, ddsHeader.Width, ddsHeader.Height, ddsHeader.MipMapCount, format));
        }