当前位置: 首页>>代码示例>>C#>>正文


C# Octree.GetPaletteIndex方法代码示例

本文整理汇总了C#中Octree.GetPaletteIndex方法的典型用法代码示例。如果您正苦于以下问题:C# Octree.GetPaletteIndex方法的具体用法?C# Octree.GetPaletteIndex怎么用?C# Octree.GetPaletteIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Octree的用法示例。


在下文中一共展示了Octree.GetPaletteIndex方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DoQuantize

        /// <summary>
        /// Does the quantize.
        /// </summary>
        /// <param name="bitmapSource">The bitmap source.</param>
        /// <param name="pixelFormat">The pixel format.</param>
        /// <param name="useDither">if set to <c>true</c> [use dither].</param>
        /// <returns>The quantized image with the recalculated color palette.</returns>
        private static Bitmap DoQuantize(Bitmap bitmapSource, PixelFormat pixelFormat, bool useDither)
        {
            // We use these values a lot
            int width = bitmapSource.Width;
            int height = bitmapSource.Height;
            Rectangle sourceRect = Rectangle.FromLTRB(0, 0, width, height);

            Bitmap bitmapOptimized = null;

            try
            {
                // Create a bitmap with the same dimensions and the desired format
                bitmapOptimized = new Bitmap(width, height, pixelFormat);

                // Lock the bits of the source image for reading.
                // we will need to write if we do the dither.
                BitmapData bitmapDataSource = bitmapSource.LockBits(
                    sourceRect,
                    ImageLockMode.ReadWrite,
                    PixelFormat.Format32bppArgb);

                try
                {
                    // Perform the first pass, which generates the octree data
                    // Create an Octree
                    Octree octree = new Octree(pixelFormat);

                    // Stride might be negative, indicating inverted row order.
                    // Allocate a managed buffer for the pixel data, and copy it from the unmanaged pointer.
                    int strideSource = Math.Abs(bitmapDataSource.Stride);
                    byte[] sourceDataBuffer = new byte[strideSource * height];
                    Marshal.Copy(bitmapDataSource.Scan0, sourceDataBuffer, 0, sourceDataBuffer.Length);

                    // We could skip every other row and/or every other column when sampling the colors
                    // of the source image, rather than hitting every other pixel. It doesn't seem to
                    // degrade the resulting image too much. But it doesn't really help the performance
                    // too much because the majority of the time seems to be spent in other places.

                    // For every row
                    int rowStartSource = 0;
                    for (int ndxRow = 0; ndxRow < height; ndxRow += 1)
                    {
                        // For each column
                        for (int ndxCol = 0; ndxCol < width; ndxCol += 1)
                        {
                            // Add the color (4 bytes per pixel - ARGB)
                            Pixel pixel = GetSourcePixel(sourceDataBuffer, rowStartSource, ndxCol);
                            octree.AddColor(pixel);
                        }

                        rowStartSource += strideSource;
                    }

                    // Get the optimized colors
                    Color[] colors = octree.GetPaletteColors();

                    // Set the palette from the octree
                    ColorPalette palette = bitmapOptimized.Palette;
                    for (var ndx = 0; ndx < palette.Entries.Length; ++ndx)
                    {
                        // Use the colors we calculated
                        // for the rest, just set to transparent
                        palette.Entries[ndx] = (ndx < colors.Length)
                            ? colors[ndx]
                            : Color.Transparent;
                    }

                    bitmapOptimized.Palette = palette;

                    // Lock the bits of the optimized bitmap for writing.
                    // we will also need to read if we are doing 1bpp or 4bpp
                    BitmapData bitmapDataOutput = bitmapOptimized.LockBits(sourceRect, ImageLockMode.ReadWrite, pixelFormat);
                    try
                    {
                        // Create a managed array for the destination bytes given the desired color depth
                        // and marshal the unmanaged data to the managed array
                        int strideOutput = Math.Abs(bitmapDataOutput.Stride);
                        byte[] bitmapOutputBuffer = new byte[strideOutput * height];

                        // For each source pixel, compute the appropriate color index
                        rowStartSource = 0;
                        int rowStartOutput = 0;

                        for (int ndxRow = 0; ndxRow < height; ++ndxRow)
                        {
                            // For each column
                            for (int ndxCol = 0; ndxCol < width; ++ndxCol)
                            {
                                // Get the source color
                                Pixel pixel = GetSourcePixel(sourceDataBuffer, rowStartSource, ndxCol);

                                // Get the closest palette index
                                int paletteIndex = octree.GetPaletteIndex(pixel);
//.........这里部分代码省略.........
开发者ID:jesperordrup,项目名称:ImageProcessor,代码行数:101,代码来源:ColorQuantizer.cs


注:本文中的Octree.GetPaletteIndex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。