Commit 720079de authored by Théophile BORNON's avatar Théophile BORNON

Post menu

parent 21409355
......@@ -24,7 +24,7 @@ namespace S04_Projet
Filter,
CreateImage,
Histogram,
Innovation,
Dithering,
Debug,
Save
}
......@@ -42,7 +42,7 @@ namespace S04_Projet
"Ajouter un filtre de convolution",
"Créer une image",
"Générer les histogrammes de l'image",
"",
"Floyd-Steinberg dithering",
"Activer le mode debug",
"Sauvegarder"
};
......@@ -181,7 +181,9 @@ namespace S04_Projet
case Operation.Histogram:
output = HistogramOperation(img);
break;
// TODO INNOVATION
case Operation.Dithering:
output = img.Dithering(1);
break;
case Operation.Debug:
output = DebugOperation(img);
break;
......
......@@ -578,6 +578,72 @@ namespace S04_Projet
return new MyImage(opt, newRPixel, newGPixel, newBPixel);
}
public unsafe MyImage Dithering(byte factor)
{
// cf https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering
int nbPixel = opt.width * opt.height;
int r, g, b;
int rErr, gErr, bErr;
int newR, newG, newB;
byte[] newRPixel = new byte[nbPixel];
byte[] newGPixel = new byte[nbPixel];
byte[] newBPixel = new byte[nbPixel];
for (int i = 0; i < nbPixel; i++)
{
newRPixel[i] = rPixels[i];
newGPixel[i] = gPixels[i];
newBPixel[i] = bPixels[i];
}
for (int y = opt.height - 1; y >= 1; y--)
{
for (int x = 1; x < opt.width - 1; x++)
{
byte oldR = newRPixel[x + y * opt.width];
byte oldG = newGPixel[x + y * opt.width];
byte oldB = newBPixel[x + y * opt.width];
newR = (int)Math.Round((double)factor * oldR / 255) * (255 / factor);
newG = (int)Math.Round((double)factor * oldG / 255) * (255 / factor);
newB = (int)Math.Round((double)factor * oldB / 255) * (255 / factor);
newRPixel[x + y * opt.width] = (byte)newR;
newGPixel[x + y * opt.width] = (byte)newG;
newBPixel[x + y * opt.width] = (byte)newB;
rErr = oldR - newR;
gErr = oldG - newG;
bErr = oldB - newB;
newRPixel[x + 1 + y * opt.width] = ToByte(newRPixel[x + 1 + y * opt.width] + ((rErr * 7) >> 4));
newGPixel[x + 1 + y * opt.width] = ToByte(newGPixel[x + 1 + y * opt.width] + ((gErr * 7) >> 4));
newBPixel[x + 1 + y * opt.width] = ToByte(newBPixel[x + 1 + y * opt.width] + ((bErr * 7) >> 4));
newRPixel[x - 1 + (y - 1) * opt.width] = ToByte(newRPixel[x - 1 + (y - 1) * opt.width] + ((rErr * 3) >> 4));
newGPixel[x - 1 + (y - 1) * opt.width] = ToByte(newGPixel[x - 1 + (y - 1) * opt.width] + ((gErr * 3) >> 4));
newBPixel[x - 1 + (y - 1) * opt.width] = ToByte(newBPixel[x - 1 + (y - 1) * opt.width] + ((bErr * 3) >> 4));
newRPixel[x + (y - 1) * opt.width] = ToByte(newRPixel[x + (y - 1) * opt.width] + ((rErr * 5) >> 4));
newGPixel[x + (y - 1) * opt.width] = ToByte(newGPixel[x + (y - 1) * opt.width] + ((gErr * 5) >> 4));
newBPixel[x + (y - 1) * opt.width] = ToByte(newBPixel[x + (y - 1) * opt.width] + ((bErr * 5) >> 4));
newRPixel[x + 1 + (y - 1) * opt.width] = ToByte(newRPixel[x + 1 + (y - 1) * opt.width] + (rErr >> 4));
newGPixel[x + 1 + (y - 1) * opt.width] = ToByte(newGPixel[x + 1 + (y - 1) * opt.width] + (gErr >> 4));
newBPixel[x + 1 + (y - 1) * opt.width] = ToByte(newBPixel[x + 1 + (y - 1) * opt.width] + (bErr >> 4));
}
}
return new MyImage(opt, newRPixel, newGPixel, newBPixel);
}
private byte ToByte(int i)
{
if (i > 255) return 255;
else if (i < 0) return 0;
else return (byte)i;
}
#endregion
#region Helpers
......
......@@ -18,19 +18,19 @@ namespace S04_Projet
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
/*MyImage img = new MyImage("img/coco.bmp");
/*
MyImage img = new MyImage("img/kitten.bmp");
Console.WriteLine("Loading done");
sw.Start();
img = img.Filter(Filter.motionBlueFilter, 1.0 / 9.0);
img = img.ToGrayScale(4, MyImage.GrayFilterType.LUMINOSITY).Dithering(1);
sw.Stop();
img.Save("filter.bmp");
img.Save("dither.bmp");
Console.WriteLine(sw.ElapsedMilliseconds);
Console.Read();*/
Console.Read();
*/
/*#region Arguments
if (args.Length > 0)
#region Arguments
/*if (args.Length > 0)
{
MULTITHREADING = false;
bool filter = false;
......@@ -68,8 +68,8 @@ namespace S04_Projet
{
Console.WriteLine("--input argument required");
}
}
#endregion*/
}*/
#endregion
#region Display
......
This image diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment