Пример #1
0
        public static unsafe CodecError DecodeFile(Stream input, out ImageData output)
        {
            StreamIOCallbacks streamCallbacks = new StreamIOCallbacks(input);
            IOCallbacks       callbacks       = new IOCallbacks()
            {
                Read  = new ReadDelegate(streamCallbacks.Read),
                Write = new WriteDelegate(streamCallbacks.Write),
                Seek  = new SeekDelegate(streamCallbacks.Seek)
            };

            CodecError result;

            if (IntPtr.Size == 8)
            {
                result = IO_x64.DecodeFile(callbacks, out output);
            }
            else
            {
                result = IO_x86.DecodeFile(callbacks, out output);
            }

            GC.KeepAlive(callbacks);
            GC.KeepAlive(streamCallbacks);

            return(result);
        }
Пример #2
0
        public static unsafe void Save(
            DDSSaveInfo info,
            TextureCollection textures,
            Stream output,
            DdsProgressCallback progressCallback)
        {
            StreamIOCallbacks streamIO  = new StreamIOCallbacks(output);
            IOCallbacks       callbacks = new IOCallbacks
            {
                Read    = streamIO.Read,
                Write   = streamIO.Write,
                Seek    = streamIO.Seek,
                GetSize = streamIO.GetSize
            };

            DDSBitmapData[] bitmapData = CreateBitmapDataArray(textures, info.arraySize, info.mipLevels);

            int hr;

            unsafe
            {
                fixed(DDSBitmapData *pBitmapData = bitmapData)
                {
                    if (IntPtr.Size == 8)
                    {
                        hr = DdsIO_x64.Save(info, pBitmapData, (uint)bitmapData.Length, callbacks, progressCallback);
                    }
                    else
                    {
                        hr = DdsIO_x86.Save(info, pBitmapData, (uint)bitmapData.Length, callbacks, progressCallback);
                    }
                }
            }

            GC.KeepAlive(streamIO);
            GC.KeepAlive(callbacks);
            GC.KeepAlive(progressCallback);

            if (FAILED(hr))
            {
                if (streamIO.CallbackExceptionInfo != null)
                {
                    streamIO.CallbackExceptionInfo.Throw();
                }
                else
                {
                    switch (hr)
                    {
                    case HResult.CanceledError:
                        throw new OperationCanceledException();

                    default:
                        Marshal.ThrowExceptionForHR(hr);
                        break;
                    }
                }
            }
        }
Пример #3
0
        private static void SaveDdsFile(
            Surface surface,
            DdsFileFormat format,
            DdsErrorMetric errorMetric,
            BC7CompressionMode compressionMode,
            bool cubeMap,
            bool generateMipmaps,
            MipMapSampling mipMapSampling,
            Stream output,
            DdsProgressCallback progressCallback)
        {
            DDSSaveInfo info = new DDSSaveInfo
            {
                scan0           = surface.Scan0.Pointer,
                width           = surface.Width,
                height          = surface.Height,
                stride          = surface.Stride,
                format          = format,
                errorMetric     = errorMetric,
                compressionMode = compressionMode,
                cubeMap         = cubeMap && IsCrossedCubeMapSize(surface),
                generateMipmaps = generateMipmaps,
                mipmapSampling  = mipMapSampling
            };

            StreamIOCallbacks streamIO  = new StreamIOCallbacks(output);
            IOCallbacks       callbacks = new IOCallbacks
            {
                Read    = streamIO.Read,
                Write   = streamIO.Write,
                Seek    = streamIO.Seek,
                GetSize = streamIO.GetSize
            };

            int hr;

            if (IntPtr.Size == 8)
            {
                hr = DdsIO_x64.Save(ref info, callbacks, progressCallback);
            }
            else
            {
                hr = DdsIO_x86.Save(ref info, callbacks, progressCallback);
            }

            GC.KeepAlive(streamIO);
            GC.KeepAlive(callbacks);
            GC.KeepAlive(progressCallback);

            if (FAILED(hr))
            {
                Marshal.ThrowExceptionForHR(hr);
            }
        }
Пример #4
0
        public static unsafe DdsImage Load(Stream stream)
        {
            StreamIOCallbacks streamIO  = new StreamIOCallbacks(stream);
            IOCallbacks       callbacks = new IOCallbacks
            {
                Read    = streamIO.Read,
                Write   = streamIO.Write,
                Seek    = streamIO.Seek,
                GetSize = streamIO.GetSize
            };

            int         hr;
            DDSLoadInfo info = new DDSLoadInfo();

            if (IntPtr.Size == 8)
            {
                hr = DdsIO_x64.Load(callbacks, ref info);
            }
            else
            {
                hr = DdsIO_x86.Load(callbacks, ref info);
            }

            GC.KeepAlive(streamIO);
            GC.KeepAlive(callbacks);

            if (FAILED(hr))
            {
                if (streamIO.CallbackExceptionInfo != null)
                {
                    streamIO.CallbackExceptionInfo.Throw();
                }
                else
                {
                    switch (hr)
                    {
                    case HResult.InvalidData:
                        throw new FormatException("The DDS file is invalid.");

                    case HResult.NotSupported:
                        throw new FormatException("The file is not a supported DDS format.");

                    default:
                        Marshal.ThrowExceptionForHR(hr);
                        break;
                    }
                }
            }

            return(DdsImageFactory(info));
        }
Пример #5
0
        public static void EncodeFile(IntPtr inData, int width, int height, int stride, int channelCount, EncodeParams parameters, Stream output)
        {
            StreamIOCallbacks streamCallbacks = new StreamIOCallbacks(output);
            IOCallbacks       callbacks       = new IOCallbacks()
            {
                Read  = new ReadDelegate(streamCallbacks.Read),
                Write = new WriteDelegate(streamCallbacks.Write),
                Seek  = new SeekDelegate(streamCallbacks.Seek)
            };

            CodecError result;

            if (IntPtr.Size == 8)
            {
                result = IO_x64.EncodeFile(inData, width, height, stride, channelCount, parameters, callbacks);
            }
            else
            {
                result = IO_x86.EncodeFile(inData, width, height, stride, channelCount, parameters, callbacks);
            }
            GC.KeepAlive(callbacks);
            GC.KeepAlive(streamCallbacks);

            if (result != CodecError.Ok)
            {
                string message = string.Empty;
                switch (result)
                {
                case CodecError.InitFailed:
                    message = Resources.InitFailed;
                    break;

                case CodecError.OutOfMemory:
                    throw new OutOfMemoryException();

                case CodecError.EncodeFailure:
                    message = Resources.EncodeFailure;
                    break;

                case CodecError.ImageBufferWrite:
                    message = Resources.ImageBufferWrite;
                    break;
                }

                throw new FormatException(message);
            }
        }
Пример #6
0
        private static void LoadDdsFile(Stream stream, ref DDSLoadInfo info)
        {
            StreamIOCallbacks streamIO  = new StreamIOCallbacks(stream);
            IOCallbacks       callbacks = new IOCallbacks
            {
                Read    = streamIO.Read,
                Write   = streamIO.Write,
                Seek    = streamIO.Seek,
                GetSize = streamIO.GetSize
            };

            int hr;

            if (IntPtr.Size == 8)
            {
                hr = DdsIO_x64.Load(callbacks, ref info);
            }
            else
            {
                hr = DdsIO_x86.Load(callbacks, ref info);
            }

            GC.KeepAlive(streamIO);
            GC.KeepAlive(callbacks);

            if (FAILED(hr))
            {
                switch (hr)
                {
                case HResult.InvalidData:
                    throw new FormatException("The DDS file is invalid.");

                case HResult.NotSupported:
                    throw new FormatException("The file is not a supported DDS format.");

                default:
                    Marshal.ThrowExceptionForHR(hr);
                    break;
                }
            }
        }
Пример #7
0
        public static unsafe void Save(
            DDSSaveInfo info,
            TextureCollection textures,
            Stream output,
            DdsProgressCallback progressCallback)
        {
            StreamIOCallbacks streamIO  = new StreamIOCallbacks(output);
            IOCallbacks       callbacks = new IOCallbacks
            {
                Read    = streamIO.Read,
                Write   = streamIO.Write,
                Seek    = streamIO.Seek,
                GetSize = streamIO.GetSize
            };

            DDSBitmapData[] bitmapData = CreateBitmapDataArray(textures, info.arraySize, info.mipLevels);

            int hr;

            unsafe
            {
                fixed(DDSBitmapData *pBitmapData = bitmapData)
                {
#if NET47
                    if (IntPtr.Size == 8)
#else
                    if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
#endif
                    {
                        hr = DdsIO_x64.Save(info, pBitmapData, (uint)bitmapData.Length, callbacks, progressCallback);
                    }
#if NET47
                    else if (IntPtr.Size == 4)
#else
                    else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
#endif
                    {
                        hr = DdsIO_x86.Save(info, pBitmapData, (uint)bitmapData.Length, callbacks, progressCallback);
                    }
                    else
                    {
                        throw new PlatformNotSupportedException();
                    }
                }
            }

            GC.KeepAlive(streamIO);
            GC.KeepAlive(callbacks);
            GC.KeepAlive(progressCallback);

            if (FAILED(hr))
            {
                if (streamIO.CallbackExceptionInfo != null)
                {
                    streamIO.CallbackExceptionInfo.Throw();
                }
                else
                {
                    switch (hr)
                    {
                    case HResult.CanceledError:
                        throw new OperationCanceledException();

                    case HResult.UnknownDdsSaveFormat:
                        throw new InvalidOperationException("The DDSFileFormat value does not map to a DXGI format.");

                    default:
                        Marshal.ThrowExceptionForHR(hr);
                        break;
                    }
                }
            }
        }
Пример #8
0
        public static unsafe DdsImage Load(Stream stream)
        {
            StreamIOCallbacks streamIO  = new StreamIOCallbacks(stream);
            IOCallbacks       callbacks = new IOCallbacks
            {
                Read    = streamIO.Read,
                Write   = streamIO.Write,
                Seek    = streamIO.Seek,
                GetSize = streamIO.GetSize
            };

            int         hr;
            DDSLoadInfo info = new DDSLoadInfo();

#if NET47
            if (IntPtr.Size == 8)
#else
            if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
#endif
            {
                hr = DdsIO_x64.Load(callbacks, ref info);
            }
#if NET47
            else if (IntPtr.Size == 4)
#else
            else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
#endif
            {
                hr = DdsIO_x86.Load(callbacks, ref info);
            }
            else
            {
                throw new PlatformNotSupportedException();
            }

            GC.KeepAlive(streamIO);
            GC.KeepAlive(callbacks);

            if (FAILED(hr))
            {
                if (streamIO.CallbackExceptionInfo != null)
                {
                    streamIO.CallbackExceptionInfo.Throw();
                }
                else
                {
                    switch (hr)
                    {
                    case HResult.InvalidData:
                        throw new FormatException("The DDS file is invalid.");

                    case HResult.NotSupported:
                        throw new FormatException("The file is not a supported DDS format.");

                    default:
                        Marshal.ThrowExceptionForHR(hr);
                        break;
                    }
                }
            }

            return(new DdsImage(info));
        }