Commit 7e7e39a8 authored by BORNON Théophile's avatar BORNON Théophile

Better faster stronger

parent 77e62751
...@@ -3,7 +3,7 @@ using System.Threading; ...@@ -3,7 +3,7 @@ using System.Threading;
namespace S04_Projet namespace S04_Projet
{ {
class MultiThreadedTask unsafe class MultiThreadedTask
{ {
#region Attributs #region Attributs
public enum Operation public enum Operation
...@@ -20,6 +20,8 @@ namespace S04_Projet ...@@ -20,6 +20,8 @@ namespace S04_Projet
} }
private byte[] file; private byte[] file;
private byte[] NewRMatrix, NewGMatrix, NewBMatrix;
byte* rPixelPtr, gPixelPtr, bPixelPtr;
private int nbPixel; private int nbPixel;
private int nbProcessors; private int nbProcessors;
private Options opt; private Options opt;
...@@ -49,8 +51,11 @@ namespace S04_Projet ...@@ -49,8 +51,11 @@ namespace S04_Projet
this.opt = opt; this.opt = opt;
this.filter = filter; this.filter = filter;
this.factor = factor; this.factor = factor;
this.rPixelPtr = rPixelPtr;
this.gPixelPtr = gPixelPtr;
this.bPixelPtr = bPixelPtr;
size = filter.GetLength(0); size = filter.GetLength(0);
nbProcessors = Environment.ProcessorCount; nbProcessors = 4;
} }
public void Start() public void Start()
...@@ -96,10 +101,14 @@ namespace S04_Projet ...@@ -96,10 +101,14 @@ namespace S04_Projet
#endregion #endregion
#region Filter #region Filter
public void ApplyFilter() public MyImage ApplyFilter()
{ {
int pixelNumber = opt.width * opt.height; int pixelNumber = opt.width * opt.height;
int pixelPerThread = pixelNumber / nbProcessors; int pixelPerThread = pixelNumber / nbProcessors;
NewRMatrix = new byte[pixelNumber];
NewGMatrix = new byte[pixelNumber];
NewBMatrix = new byte[pixelNumber];
Thread[] threads = new Thread[nbProcessors]; Thread[] threads = new Thread[nbProcessors];
Console.WriteLine("Creating {0} thread(s)", nbProcessors); Console.WriteLine("Creating {0} thread(s)", nbProcessors);
...@@ -114,9 +123,11 @@ namespace S04_Projet ...@@ -114,9 +123,11 @@ namespace S04_Projet
{ {
threads[i].Join(); threads[i].Join();
} }
return new MyImage(opt, NewRMatrix, NewGMatrix, NewBMatrix);
} }
public unsafe void ApplyFilterTask(object i) private unsafe void ApplyFilterTask(object i)
{ {
int x, y; int x, y;
int start = opt.width * opt.height / nbProcessors * (int)i; int start = opt.width * opt.height / nbProcessors * (int)i;
...@@ -141,7 +152,10 @@ namespace S04_Projet ...@@ -141,7 +152,10 @@ namespace S04_Projet
r = ConvolutionalResult(rMatrixPtr, filterPtr, size, factor); r = ConvolutionalResult(rMatrixPtr, filterPtr, size, factor);
g = ConvolutionalResult(gMatrixPtr, filterPtr, size, factor); g = ConvolutionalResult(gMatrixPtr, filterPtr, size, factor);
b = ConvolutionalResult(bMatrixPtr, filterPtr, size, factor); b = ConvolutionalResult(bMatrixPtr, filterPtr, size, factor);
NewRMatrix[j] = r;
NewGMatrix[j] = g;
NewBMatrix[j] = b;
} }
} }
} }
...@@ -167,12 +181,12 @@ namespace S04_Projet ...@@ -167,12 +181,12 @@ namespace S04_Projet
if (y < 0) y = 0; if (y < 0) y = 0;
else if (y >= opt.height) y = opt.height - 1; else if (y >= opt.height) y = opt.height - 1;
if (rgb == RGB.R) ; if (rgb == RGB.R)
// *(matrixPtr + i) = Pixels[x, y].r; *(matrixPtr + i) = *(rPixelPtr + x + y * opt.width);
else if (rgb == RGB.G) ; else if (rgb == RGB.G)
// *(matrixPtr + i) = Pixels[x, y].g; *(matrixPtr + i) = *(gPixelPtr + x + y * opt.width);
else if (rgb == RGB.B) ; else if (rgb == RGB.B)
// *(matrixPtr + i) = Pixels[x, y].b; *(matrixPtr + i) = *(bPixelPtr + x + y * opt.width);
} }
} }
......
This diff is collapsed.
...@@ -60,12 +60,12 @@ namespace S04_Projet ...@@ -60,12 +60,12 @@ namespace S04_Projet
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
MyImage imgg = new MyImage("img/image.bmp");
Console.WriteLine("Loading done");
sw.Start(); sw.Start();
MyImage imgg = new MyImage("img/flocon.bmp"); MyImage newImg = imgg.Filter(sharpenFilter, 1);
sw.Stop(); sw.Stop();
imgg.Save("test.bmp"); newImg.Save("filter.bmp");
Console.WriteLine("Loading done");
//imgg.ApplyConvFilter(edgeDetect1Filter, 1);
Console.WriteLine(sw.ElapsedMilliseconds); Console.WriteLine(sw.ElapsedMilliseconds);
Console.Read(); Console.Read();
......
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