Пример #1
0
        public static byte[] ConvertDDS(ulong guid, DXGI_FORMAT targetFormat, WICCodecs imageFormat, int frame)
        {
            try {
                if (GetDataType(guid) != DataType.Image)
                {
                    return(null);
                }

                teTexture texture = LoadTexture(guid);
                Stream    ms      = texture.SaveToDDS(1);

                return(DDSConverter.ConvertDDS(ms, targetFormat, imageFormat, frame));
            } catch {
                // ignored
            }

            return(null);
        }
Пример #2
0
 public abstract Guid GetWICCodec(WICCodecs codec);
Пример #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);
        }
Пример #4
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);
        }
Пример #5
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);
        }