示例#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);
        }
示例#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);
        }
示例#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
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback)
        {
            scratchSurface.Clear(ColorBgra.Zero);
            using (RenderArgs args = new RenderArgs(scratchSurface))
            {
                input.Render(args, true);
            }
            DdsFileFormat     fileFormat     = (DdsFileFormat)token.GetProperty <StaticListChoiceProperty>(PropertyNames.FileFormat).Value;
            DdsCompressorType compressorType = (DdsCompressorType)token.GetProperty <StaticListChoiceProperty>(PropertyNames.CompressorType).Value;
            DdsErrorMetric    errorMetric    = (DdsErrorMetric)token.GetProperty <StaticListChoiceProperty>(PropertyNames.ErrorMetric).Value;
            bool weightColorByAlpha          = token.GetProperty <BooleanProperty>(PropertyNames.WeightColorByAlpha).Value;
            bool generateMipMaps             = token.GetProperty <BooleanProperty>(PropertyNames.GenerateMipMaps).Value;
            ResamplingAlgorithm mipMapResamplingAlgorithm = (ResamplingAlgorithm)token.GetProperty <StaticListChoiceProperty>(PropertyNames.MipMapResamplingAlgorithm).Value;

            new DdsFile().Save(output, scratchSurface, fileFormat, compressorType, errorMetric, generateMipMaps, mipMapResamplingAlgorithm, weightColorByAlpha, callback);
        }
示例#5
0
        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);
            }
        }
示例#6
0
        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);
            }
        }
示例#7
0
        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);
        }
示例#8
0
        public void Save(Stream output, Surface surface, DdsFileFormat fileFormat, DdsCompressorType compressorType, DdsErrorMetric errorMetric, bool generateMipMaps, ResamplingAlgorithm mipMapResamplingAlgorithm, bool weightColorByAlpha, ProgressEventHandler progressCallback)
        {
            int  num21;
            int  num      = 0;
            bool flag     = ((fileFormat == DdsFileFormat.DDS_FORMAT_DXT1) || (fileFormat == DdsFileFormat.DDS_FORMAT_DXT3)) || (fileFormat == DdsFileFormat.DDS_FORMAT_DXT5);
            int  num2     = 1;
            int  mipWidth = surface.Width;
            int  height   = surface.Height;

            if (generateMipMaps)
            {
                while ((mipWidth > 1) || (height > 1))
                {
                    num2++;
                    mipWidth /= 2;
                    height   /= 2;
                }
            }
            this.m_header.m_size        = this.m_header.Size();
            this.m_header.m_headerFlags = 0x1007;
            if (flag)
            {
                this.m_header.m_headerFlags |= 0x80000;
            }
            else
            {
                this.m_header.m_headerFlags |= 8;
            }
            if (num2 > 1)
            {
                this.m_header.m_headerFlags |= 0x20000;
            }
            this.m_header.m_height = (uint)surface.Height;
            this.m_header.m_width  = (uint)surface.Width;
            if (flag)
            {
                int num5 = ((surface.Width + 3) / 4) * ((surface.Height + 3) / 4);
                int num6 = (fileFormat == DdsFileFormat.DDS_FORMAT_DXT1) ? 8 : 0x10;
                this.m_header.m_pitchOrLinearSize = (uint)(num5 * num6);
            }
            else
            {
                switch (fileFormat)
                {
                case DdsFileFormat.DDS_FORMAT_A8R8G8B8:
                case DdsFileFormat.DDS_FORMAT_X8R8G8B8:
                case DdsFileFormat.DDS_FORMAT_A8B8G8R8:
                case DdsFileFormat.DDS_FORMAT_X8B8G8R8:
                    num = 4;
                    break;

                case DdsFileFormat.DDS_FORMAT_A1R5G5B5:
                case DdsFileFormat.DDS_FORMAT_A4R4G4B4:
                case DdsFileFormat.DDS_FORMAT_R5G6B5:
                    num = 2;
                    break;

                case DdsFileFormat.DDS_FORMAT_R8G8B8:
                    num = 3;
                    break;
                }
                this.m_header.m_pitchOrLinearSize = (uint)(this.m_header.m_width * num);
            }
            this.m_header.m_depth        = 0;
            this.m_header.m_mipMapCount  = (num2 == 1) ? 0 : ((uint)num2);
            this.m_header.m_reserved1_0  = 0;
            this.m_header.m_reserved1_1  = 0;
            this.m_header.m_reserved1_2  = 0;
            this.m_header.m_reserved1_3  = 0;
            this.m_header.m_reserved1_4  = 0;
            this.m_header.m_reserved1_5  = 0;
            this.m_header.m_reserved1_6  = 0;
            this.m_header.m_reserved1_7  = 0;
            this.m_header.m_reserved1_8  = 0;
            this.m_header.m_reserved1_9  = 0;
            this.m_header.m_reserved1_10 = 0;
            this.m_header.m_pixelFormat.Initialise(fileFormat);
            this.m_header.m_surfaceFlags = 0x1000;
            if (num2 > 1)
            {
                this.m_header.m_surfaceFlags |= 0x400008;
            }
            this.m_header.m_cubemapFlags = 0;
            this.m_header.m_reserved2_0  = 0;
            this.m_header.m_reserved2_1  = 0;
            this.m_header.m_reserved2_2  = 0;
            output.WriteUInt32(0x20534444);
            this.m_header.Write(output);
            int squishFlags = this.GetSquishFlags(fileFormat, compressorType, errorMetric, weightColorByAlpha);

            mipWidth = surface.Width;
            height   = surface.Height;
            SizeInt32[] numArray        = new SizeInt32[num2];
            int[]       numArray2       = new int[num2];
            int[]       pixelsCompleted = new int[num2];
            long        totalPixels     = 0L;

            for (int i = 0; i < num2; i++)
            {
                SizeInt32 num8 = new SizeInt32((mipWidth > 0) ? mipWidth : 1, (height > 0) ? height : 1);
                numArray[i] = num8;
                int num9 = num8.Width * num8.Height;
                numArray2[i] = num9;
                if (i == 0)
                {
                    pixelsCompleted[i] = 0;
                }
                else
                {
                    pixelsCompleted[i] = pixelsCompleted[i - 1] + numArray2[i - 1];
                }
                totalPixels += num9;
                mipWidth    /= 2;
                height      /= 2;
            }
            mipWidth = surface.Width;
            height   = surface.Height;
            for (int mipLoop = 0; mipLoop < num2; mipLoop = num21 + 1)
            {
                byte[]    buffer;
                SizeInt32 size     = numArray[mipLoop];
                Surface   surface2 = new Surface(size);
                if (mipLoop == 0)
                {
                    surface2 = surface;
                }
                else
                {
                    IRenderer <ColorBgra> renderer;
                    SizeInt32             newSize = surface2.Size <ColorBgra>();
                    switch (mipMapResamplingAlgorithm)
                    {
                    case ResamplingAlgorithm.NearestNeighbor:
                        renderer = surface.ResizeNearestNeighbor(newSize);
                        break;

                    case ResamplingAlgorithm.Bilinear:
                        renderer = surface.ResizeBilinear(newSize);
                        break;

                    case ResamplingAlgorithm.Bicubic:
                        renderer = surface.ResizeBicubic(newSize);
                        break;

                    case ResamplingAlgorithm.SuperSampling:
                        renderer = surface.ResizeSuperSampling(newSize);
                        break;

                    case ResamplingAlgorithm.Fant:
                        renderer = surface.ResizeFant(newSize);
                        break;

                    default:
                        throw ExceptionUtil.InvalidEnumArgumentException <ResamplingAlgorithm>(mipMapResamplingAlgorithm, "mipMapResamplingAlgorithm");
                    }
                    renderer.Render <ColorBgra>(surface2);
                }
                DdsSquish.ProgressFn fn = delegate(int workDone, int workTotal) {
                    long   num  = workDone * mipWidth;
                    long   num2 = pixelsCompleted[mipLoop];
                    double num3 = (num + num2) / ((double)totalPixels);
                    progressCallback(this, new ProgressEventArgs(DoubleUtil.Clamp(100.0 * num3, 0.0, 100.0)));
                };
                if ((fileFormat >= DdsFileFormat.DDS_FORMAT_DXT1) && (fileFormat <= DdsFileFormat.DDS_FORMAT_DXT5))
                {
                    buffer = DdsSquish.CompressImage(surface2, squishFlags, (progressCallback == null) ? null : fn);
                }
                else
                {
                    int num12 = num * surface2.Width;
                    buffer = new byte[num12 * surface2.Height];
                    buffer.Initialize();
                    for (int j = 0; j < surface2.Height; j++)
                    {
                        for (int k = 0; k < surface2.Width; k++)
                        {
                            ColorBgra point = surface2.GetPoint(k, j);
                            uint      num15 = 0;
                            switch (fileFormat)
                            {
                            case DdsFileFormat.DDS_FORMAT_A8R8G8B8:
                                num15 = (uint)((((point.A << 0x18) | (point.R << 0x10)) | (point.G << 8)) | point.B);
                                break;

                            case DdsFileFormat.DDS_FORMAT_X8R8G8B8:
                                num15 = (uint)(((point.R << 0x10) | (point.G << 8)) | point.B);
                                break;

                            case DdsFileFormat.DDS_FORMAT_A8B8G8R8:
                                num15 = (uint)((((point.A << 0x18) | (point.B << 0x10)) | (point.G << 8)) | point.R);
                                break;

                            case DdsFileFormat.DDS_FORMAT_X8B8G8R8:
                                num15 = (uint)(((point.B << 0x10) | (point.G << 8)) | point.R);
                                break;

                            case DdsFileFormat.DDS_FORMAT_A1R5G5B5:
                                num15 = (uint)((((((point.A != null) ? 1 : 0) << 15) | ((point.R >> 3) << 10)) | ((point.G >> 3) << 5)) | (point.B >> 3));
                                break;

                            case DdsFileFormat.DDS_FORMAT_A4R4G4B4:
                                num15 = (uint)(((((point.A >> 4) << 12) | ((point.R >> 4) << 8)) | ((point.G >> 4) << 4)) | (point.B >> 4));
                                break;

                            case DdsFileFormat.DDS_FORMAT_R8G8B8:
                                num15 = (uint)(((point.R << 0x10) | (point.G << 8)) | point.B);
                                break;

                            case DdsFileFormat.DDS_FORMAT_R5G6B5:
                                num15 = (uint)((((point.R >> 3) << 11) | ((point.G >> 2) << 5)) | (point.B >> 3));
                                break;
                            }
                            int num16 = (j * num12) + (k * num);
                            for (int m = 0; m < num; m++)
                            {
                                buffer[num16 + m] = (byte)((num15 >> (8 * m)) & 0xff);
                            }
                        }
                        if (progressCallback != null)
                        {
                            long   num18 = (j + 1) * mipWidth;
                            long   num19 = pixelsCompleted[mipLoop];
                            double num20 = (num18 + num19) / ((double)totalPixels);
                            progressCallback(this, new ProgressEventArgs(100.0 * num20));
                        }
                    }
                }
                output.Write(buffer, 0, buffer.GetLength(0));
                mipWidth /= 2;
                height   /= 2;
                num21     = mipLoop;
            }
        }
示例#9
0
        private int GetSquishFlags(DdsFileFormat fileFormat, DdsCompressorType compressorType, DdsErrorMetric errorMetric, bool weightColorByAlpha)
        {
            int num = 0;

            if (fileFormat == DdsFileFormat.DDS_FORMAT_DXT1)
            {
                num |= 1;
            }
            else if (fileFormat == DdsFileFormat.DDS_FORMAT_DXT3)
            {
                num |= 2;
            }
            else if (fileFormat == DdsFileFormat.DDS_FORMAT_DXT5)
            {
                num |= 4;
            }
            if (num != 0)
            {
                if (compressorType == DdsCompressorType.ClusterFit)
                {
                    num |= 8;
                }
                else if (compressorType == DdsCompressorType.RangeFit)
                {
                    num |= 0x10;
                }
                else
                {
                    num |= 0x100;
                }
                if (errorMetric == DdsErrorMetric.Perceptual)
                {
                    num |= 0x20;
                }
                else
                {
                    num |= 0x40;
                }
                if ((compressorType == DdsCompressorType.ClusterFit) & weightColorByAlpha)
                {
                    num |= 0x80;
                }
            }
            return(num);
        }