本文整理汇总了C#中Octree.AddColor方法的典型用法代码示例。如果您正苦于以下问题:C# Octree.AddColor方法的具体用法?C# Octree.AddColor怎么用?C# Octree.AddColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Octree
的用法示例。
在下文中一共展示了Octree.AddColor方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
//.........这里部分代码省略.........
示例2: ReduceColors
private static unsafe System.Drawing.Image ReduceColors(Bitmap bitmap, int maxColors, int numBits, Color transparentColor)
{
byte* numPtr;
if ((numBits < 3) || (numBits > 8))
{
throw new ArgumentOutOfRangeException("numBits");
}
if (maxColors < 0x10)
{
throw new ArgumentOutOfRangeException("maxColors");
}
int width = bitmap.Width;
int height = bitmap.Height;
Octree octree = new Octree(maxColors, numBits, transparentColor);
for (int i = 0; i < width; i++)
{
for (int k = 0; k < height; k++)
{
octree.AddColor(bitmap.GetPixel(i, k));
}
}
ColorIndexTable colorIndexTable = octree.GetColorIndexTable();
Bitmap bitmap2 = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette palette = bitmap2.Palette;
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapdata = bitmap2.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
IntPtr ptr = bitmapdata.Scan0;
if (bitmapdata.Stride > 0)
{
numPtr = (byte*) ptr.ToPointer();
}
else
{
numPtr = (byte*) (ptr.ToPointer() + (bitmapdata.Stride * (height - 1)));
}
int num5 = Math.Abs(bitmapdata.Stride);
for (int j = 0; j < height; j++)
{
for (int m = 0; m < width; m++)
{
byte* numPtr2 = (numPtr + (j * num5)) + m;
Color pixel = bitmap.GetPixel(m, j);
byte num8 = (byte) colorIndexTable[pixel];
numPtr2[0] = num8;
}
}
colorIndexTable.CopyToColorPalette(palette);
bitmap2.Palette = palette;
bitmap2.UnlockBits(bitmapdata);
return bitmap2;
}