Commit a79faff5 authored by Théophile BORNON's avatar Théophile BORNON

Multithreaded loading and test scripts

parent 158e1f69
...@@ -8,6 +8,7 @@ namespace S04_Projet ...@@ -8,6 +8,7 @@ namespace S04_Projet
#region Attributs #region Attributs
public enum Operation public enum Operation
{ {
Load,
Filter Filter
} }
...@@ -21,6 +22,7 @@ namespace S04_Projet ...@@ -21,6 +22,7 @@ namespace S04_Projet
private byte[] file; private byte[] file;
private byte[] NewRMatrix, NewGMatrix, NewBMatrix; private byte[] NewRMatrix, NewGMatrix, NewBMatrix;
byte* rPixelPtr, gPixelPtr, bPixelPtr; byte* rPixelPtr, gPixelPtr, bPixelPtr;
byte* filePtr;
private int nbPixel; private int nbPixel;
private int nbProcessors; private int nbProcessors;
private Options opt; private Options opt;
...@@ -35,8 +37,19 @@ namespace S04_Projet ...@@ -35,8 +37,19 @@ namespace S04_Projet
public void SetFile(byte[] file) => this.file = file; public void SetFile(byte[] file) => this.file = file;
/// <summary>
/// Constructeur pour l'opération Filter
/// </summary>
/// <param name="ToDo"></param>
/// <param name="rPixelPtr"></param>
/// <param name="gPixelPtr"></param>
/// <param name="bPixelPtr"></param>
/// <param name="opt"></param>
/// <param name="filter"></param>
/// <param name="factor"></param>
public unsafe MultiThreadedTask(Operation ToDo, byte* rPixelPtr, byte* gPixelPtr, byte* bPixelPtr, Options opt, short[,] filter, double factor) public unsafe MultiThreadedTask(Operation ToDo, byte* rPixelPtr, byte* gPixelPtr, byte* bPixelPtr, Options opt, short[,] filter, double factor)
{
if (ToDo == Operation.Filter)
{ {
this.ToDo = ToDo; this.ToDo = ToDo;
this.opt = opt; this.opt = opt;
...@@ -49,15 +62,72 @@ namespace S04_Projet ...@@ -49,15 +62,72 @@ namespace S04_Projet
size = filter.GetLength(0); size = filter.GetLength(0);
nbProcessors = 4; nbProcessors = 4;
} }
else throw new Exception("Impossible d'utiliser ce constructeur pour ce type d'opération");
}
public unsafe MultiThreadedTask(Operation ToDo, Options opt, byte* rPixelPtr, byte* gPixelPtr, byte* bPixelPtr) /// <summary>
/// Constructeur pour l'opération Load
/// </summary>
/// <param name="ToDo"></param>
/// <param name="opt"></param>
/// <param name="rPixelPtr"></param>
/// <param name="gPixelPtr"></param>
/// <param name="bPixelPtr"></param>
/// <param name="filePtr"></param>
public unsafe MultiThreadedTask(Operation ToDo, Options opt, byte* rPixelPtr, byte* gPixelPtr, byte* bPixelPtr, byte* filePtr)
{
if (ToDo == Operation.Load)
{ {
this.ToDo = ToDo;
this.rPixelPtr = rPixelPtr; this.rPixelPtr = rPixelPtr;
this.gPixelPtr = gPixelPtr; this.gPixelPtr = gPixelPtr;
this.bPixelPtr = bPixelPtr; this.bPixelPtr = bPixelPtr;
this.ToDo = ToDo;
this.opt = opt;
this.filePtr = filePtr;
nbPixel = opt.width * opt.height; nbPixel = opt.width * opt.height;
nbProcessors = Environment.ProcessorCount;
} }
else throw new Exception("Impossible d'utiliser ce constructeur pour ce type d'opération");
}
#region LoadImage
public unsafe void LoadImage()
{
Console.WriteLine("Load Image");
Thread[] threads = new Thread[nbProcessors];
for (int i = 0; i < nbProcessors; i++)
{
Console.WriteLine("Creating and starting thread {0}", i);
threads[i] = new Thread(new ParameterizedThreadStart(LoadImageTask));
threads[i].Start(i);
}
for (int i = 0; i < nbProcessors; i++)
threads[i].Join();
}
private unsafe void LoadImageTask(object i)
{
if (filePtr != null)
{
int start = nbPixel / nbProcessors * (int)i;
int end = nbPixel / nbProcessors * ((int)i + 1);
int x, y;
for (int j = start; j < end; j++)
{
x = j % opt.width;
y = j / opt.width;
*(bPixelPtr + j) = *(filePtr + opt.offset + opt.padding * y + j * 3);
*(gPixelPtr + j) = *(filePtr + opt.offset + opt.padding * y + j * 3 + 1);
*(rPixelPtr + j) = *(filePtr + opt.offset + opt.padding * y + j * 3 + 2);
}
}
}
#endregion
#region Filter #region Filter
public MyImage ApplyFilter() public MyImage ApplyFilter()
......
...@@ -12,6 +12,7 @@ namespace S04_Projet ...@@ -12,6 +12,7 @@ namespace S04_Projet
byte[] file; byte[] file;
private int nbProcessors; private int nbProcessors;
public enum GrayFilterType public enum GrayFilterType
{ {
LINEAR, LINEAR,
...@@ -129,12 +130,14 @@ namespace S04_Projet ...@@ -129,12 +130,14 @@ namespace S04_Projet
#endregion #endregion
#region Multithreading #region Multithreading
/*else else
{ {
MultiThreadedTask LoadImageTask = new MultiThreadedTask(MultiThreadedTask.Operation.Load, file, opt); fixed (byte* filePtr = file, rPtr = rPixels, gPtr = gPixels, bPtr = bPixels)
LoadImageTask.Start(); {
Pixels = LoadImageTask.getPixelMatrix(); MultiThreadedTask LoadImageTask = new MultiThreadedTask(MultiThreadedTask.Operation.Load, opt, rPtr, gPtr, bPtr, filePtr);
}*/ LoadImageTask.LoadImage();
}
}
#endregion #endregion
file = null; file = null;
......
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Drawing;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
...@@ -12,71 +7,27 @@ namespace S04_Projet ...@@ -12,71 +7,27 @@ namespace S04_Projet
class Program class Program
{ {
public static bool MULTITHREADING_LOAD = false; public static bool MULTITHREADING_LOAD = false;
public static bool MULTITHREADING = true; public static bool MULTITHREADING = false;
public static bool DEBUG = false; public static bool DEBUG = false;
private static string[] menuItems = new string[] { "Charger une image", "Créer une image" }; private static string[] menuItems = new string[] { "Charger une image", "Créer une image" };
private static Menu welcomeMenu = new Menu(menuItems); private static Menu welcomeMenu = new Menu(menuItems);
private static MyImage img;
private static Stopwatch sw = new Stopwatch();
static void Main(string[] args) static void Main(string[] args)
{ {
Stopwatch sw = new Stopwatch();
/*
MyImage img = new MyImage("img/kitten.bmp");
Console.WriteLine("Loading done");
sw.Start();
img = img.ToGrayScale(4, MyImage.GrayFilterType.LUMINOSITY).Dithering(1);
sw.Stop();
img.Save("dither.bmp");
Console.WriteLine(sw.ElapsedMilliseconds);
Console.Read();
*/
#region Arguments #region Arguments
/*if (args.Length > 0) if (args.Length > 0)
{
MULTITHREADING = false;
bool filter = false;
string inputPath = "";
string outputPath = "";
for (int i = 0; i < args.Length; i++)
{
if (args[i].Equals("--input"))
{
inputPath = args[i + 1];
i++;
}
else if (args[i].Equals("--output"))
{ {
outputPath = args[i + 1]; Arguments(args, 0);
i++;
} }
else if (args[i].Equals("--multithreading")) MULTITHREADING = true;
else if (args[i].Equals("--filter")) filter = true;
}
if(inputPath != "")
{
MyImage img = new MyImage(inputPath);
if (filter) img = img.ApplyConvFilter(edgeDetect1Filter, 1);
if (outputPath != "")
{
img.Save(outputPath);
} else
{
Console.WriteLine("No output specified, program will only load image in memory and stop");
}
} else
{
Console.WriteLine("--input argument required");
}
}*/
#endregion #endregion
#region Display #region Display
MyImage img; else
{ {
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("#############################################################"); Console.WriteLine("#############################################################");
...@@ -111,7 +62,37 @@ namespace S04_Projet ...@@ -111,7 +62,37 @@ namespace S04_Projet
Console.Read(); Console.Read();
} }
#endregion #endregion
}
private static void Arguments(string[] args, int i)
{
int increment = 1;
if (DEBUG) sw.Restart();
if (args[i] == "--input" || args[i] == "-i")
{
img = new MyImage(args[i + 1]);
increment = 2;
}
else if (args[i] == "--output" || args[i] == "-o")
{
if (img != null) img.Save(args[i + 1]);
increment = 2;
}
else if (args[i] == "--debug" || args[i] == "-d")
DEBUG = true;
else if (args[i] == "--multithreading" || args[i] == "-MT")
MULTITHREADING = true;
else if (args[i] == "--multithreadingLoad" || args[i] == "-MTL")
MULTITHREADING_LOAD = true;
else if (args[i] == "--") ;
if (DEBUG)
{
sw.Stop();
File.AppendAllText("debug.csv", args[i].Replace("-","") + ";" + sw.ElapsedMilliseconds + ";\n");
}
if (i + increment < args.Length) Arguments(args, i + increment);
} }
} }
} }
@echo off
FOR /L %%i IN (1,1,100) DO (
ECHO %%i
ImageProcessing.exe -d -MTL -i img\flocon.bmp
)
\ No newline at end of file
@echo off
FOR /L %%i IN (1,1,100) DO (
ECHO %%i
ImageProcessing.exe -d -i img\flocon.bmp -o output.bmp
)
\ No newline at end of file
@echo off
FOR /L %%i IN (1,1,100) DO (
ECHO %%i
ImageProcessing.exe -d -i img\flocon.bmp
)
\ No newline at end of file
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