https://github.com/mcraiha/csharp-dithering
CSharp (C#) versions of certain dithering algorithms
https://github.com/mcraiha/csharp-dithering
csharp dithering-methods dotnet net8 netstandard20
Last synced: 6 months ago
JSON representation
CSharp (C#) versions of certain dithering algorithms
- Host: GitHub
- URL: https://github.com/mcraiha/csharp-dithering
- Owner: mcraiha
- Created: 2015-08-13T18:31:27.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2025-02-10T18:21:40.000Z (8 months ago)
- Last Synced: 2025-03-26T23:09:12.880Z (6 months ago)
- Topics: csharp, dithering-methods, dotnet, net8, netstandard20
- Language: C#
- Homepage:
- Size: 887 KB
- Stars: 24
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGE-LOG.md
Awesome Lists containing this project
README
# CSharp-Dithering
CSharp (C#) versions of certain dithering algorithms. This project is .NET 8 compatible, managed and available as Nuget!## Build status
## Nuget
[](https://www.nuget.org/packages/LibDithering/)## Projects using this library
[Dithery-cli](https://github.com/mcraiha/Dithery-cli)
[Dithery](https://github.com/mcraiha/Dithery)## Introduction to this project
This project contains implementations of different dithering algorithms (C#). They can be used with any graphics/image API.## Introduction to dithering
As [Wikipedia](https://en.wikipedia.org/wiki/Dither) says *"Dither is an intentionally applied form of noise used to randomize quantization error, preventing large-scale patterns such as color banding in images."*In this case dithering is used help in color reduction (less banding). This reduction of colors + dithering combo can be used e.g. to reduce file sizes, make artsy images and avoid issues when displaying images on displays that have limited color range.
## Implementation
Inspiration for this project came from [blog post](http://www.tannerhelland.com/4660/dithering-eleven-algorithms-source-code/) made by **Tanner Helland**.[DitheringBase.cs](https://github.com/mcraiha/CSharp-Dithering/blob/master/src/DitheringBase.cs) contains the abstract base class that every error pushing dithering implmentation should use.
[FakeDithering.cs](https://github.com/mcraiha/CSharp-Dithering/blob/master/src/FakeDithering.cs) is "fake" dithering since it doesn't do any dithering. It is used to get image with reduced colors.
Other .cs files are used for different dithering algorithms, and the files are named as **SomeAlgorithm**Dithering.cs
[Samples folder](https://github.com/mcraiha/CSharp-Dithering/blob/master/samples) contains images that are shown in the end of this Readme file
## Examples
Use Atkinson dithering with web safe color reduction for 24 bit PNG input with System.Drawing (this example is Windows only)
```cs
public void DoAtkinsonDithering()
{
AtkinsonDitheringRGB atkinson = new AtkinsonDitheringRGB(TrueColorBytesToWebSafeColorBytes);using(FileStream pngStream = new FileStream("half.png", FileMode.Open, FileAccess.Read))
using(var image = new Bitmap(pngStream))
{
byte[,,] bytes = ReadBitmapToColorBytes(image);TempByteImageFormat temp = new TempByteImageFormat(bytes);
atkinson.DoDithering(temp);WriteToBitmap(image, temp.GetPixelChannels);
image.Save("test.png");
}
}private static void TrueColorBytesToWebSafeColorBytes(in byte[] input, ref byte[] output)
{
for (int i = 0; i < input.Length; i++)
{
output[i] = (byte)(Math.Round(input[i] / 51.0) * 51);
}
}private static byte[,,] ReadBitmapToColorBytes(Bitmap bitmap)
{
byte[,,] returnValue = new byte[bitmap.Width, bitmap.Height, 3];
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
Color color = bitmap.GetPixel(x, y);
returnValue[x, y, 0] = color.R;
returnValue[x, y, 1] = color.G;
returnValue[x, y, 2] = color.B;
}
}
return returnValue;
}private static void WriteToBitmap(Bitmap bitmap, Func reader)
{
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
byte[] read = reader(x, y);
Color color = Color.FromArgb(read[0], read[1], read[2]);
bitmap.SetPixel(x, y, color);
}
}
}
```
## UsageYou have to always give color reduction method as parameter for dither constructor. You can dither multiple images with one instance by calling DoDithering again with different input.
## Wasn't this .NET Framework project?
Yes, but time moves on...
## License
Text in this document and source code files are released into the public domain. See [PUBLICDOMAIN](https://github.com/mcraiha/CSharp-Dithering/blob/master/PUBLICDOMAIN) file.
Parrot image (half.png) is made from image that comes from [Kodak Lossless True Color Image Suite](http://r0k.us/graphics/kodak/) and it doesn't have any specific license.
## Samples
I took the famous [parrot image](http://r0k.us/graphics/kodak/kodim23.html) and reduced its size. Then I ran the image (which has 64655 different colors) with all dithering methods and using [Web safe colors](https://en.wikipedia.org/wiki/Web_colors#Web-safe_colors) as palette (216 colors).

Original
Floyd-Steinberg
Jarvis-Judice-Ninke
Stucki
Atkinson
Burkes
Sierra
Sierra Two Row
Sierra Lite
No dithering, just color reduction