Exemple #1
0
        public static void Save(
            Document input,
            Stream output,
            DdsFileFormat format,
            DdsErrorMetric errorMetric,
            BC7CompressionMode compressionMode,
            bool cubeMap,
            bool generateMipmaps,
            MipMapSampling sampling,
            Surface scratchSurface,
            ProgressEventHandler progressCallback)
        {
            using (RenderArgs args = new RenderArgs(scratchSurface))
            {
                input.Render(args, true);
            }

            DdsProgressCallback ddsProgress = null;

            if (progressCallback != null)
            {
                ddsProgress = (UIntPtr done, UIntPtr total) =>
                {
                    double progress = (double)done.ToUInt64() / (double)total.ToUInt64();
                    progressCallback(null, new ProgressEventArgs(progress * 100.0, true));
                };
            }

            SaveDdsFile(scratchSurface, format, errorMetric, compressionMode, cubeMap, generateMipmaps, sampling, output, ddsProgress);
        }
Exemple #2
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            DdsFileFormat      fileFormat      = (DdsFileFormat)token.GetProperty(PropertyNames.FileFormat).Value;
            BC7CompressionMode compressionMode = (BC7CompressionMode)token.GetProperty(PropertyNames.BC7CompressionMode).Value;
            DdsErrorMetric     errorMetric     = (DdsErrorMetric)token.GetProperty(PropertyNames.ErrorMetric).Value;
            bool           generateMipmaps     = token.GetProperty <BooleanProperty>(PropertyNames.GenerateMipMaps).Value;
            MipMapSampling mipSampling         = (MipMapSampling)token.GetProperty(PropertyNames.MipMapResamplingAlgorithm).Value;

            DdsFile.Save(input, output, fileFormat, errorMetric, compressionMode, generateMipmaps, mipSampling, scratchSurface, progressCallback);
        }
Exemple #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);
            }
        }
        private static unsafe void SaveDdsFile(
            Surface surface,
            DdsFileFormat format,
            DdsErrorMetric errorMetric,
            BC7CompressionMode compressionMode,
            bool generateMipmaps,
            MipMapSampling mipMapSampling,
            DdsWriteImageCallback writeImageCallback,
            DdsProgressCallback progressCallback)
        {
            DDSSaveInfo info = new DDSSaveInfo
            {
                width           = surface.Width,
                height          = surface.Height,
                stride          = surface.Stride,
                format          = format,
                errorMetric     = errorMetric,
                compressionMode = compressionMode,
                generateMipmaps = generateMipmaps,
                mipmapSampling  = mipMapSampling,
                scan0           = surface.Scan0.Pointer
            };

            int hr;

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

            if (FAILED(hr))
            {
                Marshal.ThrowExceptionForHR(hr);
            }
        }
        public static unsafe void Save(
            Document input,
            Stream output,
            DdsFileFormat format,
            DdsErrorMetric errorMetric,
            BC7CompressionMode compressionMode,
            bool generateMipmaps,
            MipMapSampling sampling,
            Surface scratchSurface,
            ProgressEventHandler progressCallback)
        {
            using (RenderArgs args = new RenderArgs(scratchSurface))
            {
                input.Render(args, true);
            }

            DdsWriteImageCallback ddsWriteImage = delegate(IntPtr image, UIntPtr imageSize)
            {
                ulong size = imageSize.ToUInt64();

                if (image != IntPtr.Zero && size > 0)
                {
                    const int MaxBufferSize = 65536;

                    ulong  streamBufferSize = Math.Min(MaxBufferSize, size);
                    byte[] streamBuffer     = new byte[streamBufferSize];

                    output.SetLength(checked ((long)size));

                    ulong offset    = 0;
                    ulong remaining = size;

                    fixed(byte *destPtr = streamBuffer)
                    {
                        byte *srcPtr = (byte *)image.ToPointer();

                        while (remaining > 0)
                        {
                            ulong copySize = Math.Min(MaxBufferSize, remaining);

                            Buffer.MemoryCopy(srcPtr + offset, destPtr, streamBufferSize, copySize);

                            output.Write(streamBuffer, 0, (int)copySize);

                            offset    += copySize;
                            remaining -= copySize;
                        }
                    }
                }
            };

            DdsProgressCallback ddsProgress = delegate(UIntPtr done, UIntPtr total)
            {
                double progress = (double)done.ToUInt64() / (double)total.ToUInt64();
                progressCallback(null, new ProgressEventArgs(progress * 100.0, true));
            };

            SaveDdsFile(scratchSurface, format, errorMetric, compressionMode, generateMipmaps, sampling, ddsWriteImage, ddsProgress);

            GC.KeepAlive(ddsWriteImage);
        }