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); }
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); }
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 void Save( IServiceProvider services, Document input, Stream output, DdsFileFormat format, DdsErrorMetric errorMetric, BC7CompressionMode compressionMode, bool cubeMap, bool generateMipmaps, ResamplingAlgorithm sampling, Surface scratchSurface, ProgressEventHandler progressCallback) { using (RenderArgs args = new RenderArgs(scratchSurface)) { input.Render(args, true); } DdsNative.DdsProgressCallback ddsProgress = null; if (progressCallback != null) { ddsProgress = (UIntPtr done, UIntPtr total) => { double progress = (double)done.ToUInt64() / (double)total.ToUInt64(); try { progressCallback(null, new ProgressEventArgs(progress * 100.0, true)); return(true); } catch (OperationCanceledException) { return(false); } }; } int width = scratchSurface.Width; int height = scratchSurface.Height; int arraySize = 1; Size?cubeMapFaceSize = null; if (cubeMap && IsCrossedCubeMapSize(scratchSurface)) { if (width > height) { width /= 4; height /= 3; } else { width /= 3; height /= 4; } arraySize = 6; cubeMapFaceSize = new Size(width, height); } int mipLevels = generateMipmaps ? GetMipCount(width, height) : 1; bool enableHardwareAcceleration = (bool)services.GetService <ISettingsService>().GetSetting(AppSettingPaths.UI.EnableHardwareAcceleration).Value; DdsNative.DDSSaveInfo info = new DdsNative.DDSSaveInfo { width = width, height = height, arraySize = arraySize, mipLevels = mipLevels, format = format, errorMetric = errorMetric, compressionMode = compressionMode, cubeMap = cubeMapFaceSize.HasValue, enableHardwareAcceleration = enableHardwareAcceleration }; using (TextureCollection textures = GetTextures(scratchSurface, cubeMapFaceSize, mipLevels, sampling)) { DdsNative.Save(info, textures, output, ddsProgress); } }
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); }