Commit 45249fb9 authored by BORNON Théophile's avatar BORNON Théophile

Pointers everywhere

parent 19eeb88e
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<DocumentationFile> <DocumentationFile>
</DocumentationFile> </DocumentationFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget> <PlatformTarget>x64</PlatformTarget>
......
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; using System.Threading;
namespace S04_Projet namespace S04_Projet
...@@ -111,9 +108,6 @@ namespace S04_Projet ...@@ -111,9 +108,6 @@ namespace S04_Projet
nbProcessors = Environment.ProcessorCount; nbProcessors = Environment.ProcessorCount;
int pixelPerThread = pixelNumber / nbProcessors; int pixelPerThread = pixelNumber / nbProcessors;
Thread[] threads = new Thread[nbProcessors]; Thread[] threads = new Thread[nbProcessors];
Console.WriteLine("Génération de {0} threads", nbProcessors);
for (int i = 0; i < nbProcessors; i++) for (int i = 0; i < nbProcessors; i++)
{ {
threads[i] = new Thread(new ParameterizedThreadStart(ApplyFilterTask)); threads[i] = new Thread(new ParameterizedThreadStart(ApplyFilterTask));
...@@ -123,34 +117,40 @@ namespace S04_Projet ...@@ -123,34 +117,40 @@ namespace S04_Projet
for (int i = 0; i < nbProcessors; i++) for (int i = 0; i < nbProcessors; i++)
{ {
threads[i].Join(); threads[i].Join();
Console.WriteLine("Thread {0} done", i);
} }
} }
public void ApplyFilterTask(object i) public 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;
int end = opt.width * opt.height / nbProcessors * ((int)i + 1); int end = opt.width * opt.height / nbProcessors * ((int)i + 1);
byte[,] rMatrix, gMatrix, bMatrix; byte[,] rMatrix = new byte[size, size];
byte[,] gMatrix = new byte[size, size];
byte[,] bMatrix = new byte[size, size];
byte r, g, b; byte r, g, b;
fixed (short* filterPtr = filter)
{
fixed (byte* rMatrixPtr = rMatrix, gMatrixPtr = gMatrix, bMatrixPtr = bMatrix)
{
for (int j = start; j < end; j++) for (int j = start; j < end; j++)
{ {
x = j % opt.width; x = j % opt.width;
y = j / opt.width; y = j / opt.width;
rMatrix = GetMatrix(x, y, size, RGB.R); GetMatrix(x, y, size, RGB.R, rMatrixPtr);
gMatrix = GetMatrix(x, y, size, RGB.G); GetMatrix(x, y, size, RGB.G, gMatrixPtr);
bMatrix = GetMatrix(x, y, size, RGB.B); GetMatrix(x, y, size, RGB.B, bMatrixPtr);
r = ConvolutionalResult(rMatrixPtr, filterPtr, size, factor);
r = ConvolutionalResult(rMatrix, filter, size, factor); g = ConvolutionalResult(gMatrixPtr, filterPtr, size, factor);
g = ConvolutionalResult(gMatrix, filter, size, factor); b = ConvolutionalResult(bMatrixPtr, filterPtr, size, factor);
b = ConvolutionalResult(bMatrix, filter, size, factor);
NewPixels[x, y] = new Pixel(r, g, b); NewPixels[x, y] = new Pixel(r, g, b);
} }
} }
}
}
/// <summary> /// <summary>
/// Retourne la matrice de pixels de taille size ,de milieu (x0,y0) et de couleur RGB /// Retourne la matrice de pixels de taille size ,de milieu (x0,y0) et de couleur RGB
...@@ -160,10 +160,8 @@ namespace S04_Projet ...@@ -160,10 +160,8 @@ namespace S04_Projet
/// <param name="size">Taille de la matrice de pixel désirée</param> /// <param name="size">Taille de la matrice de pixel désirée</param>
/// <param name="rgb">Couleur souhaitée</param> /// <param name="rgb">Couleur souhaitée</param>
/// <returns>Matrice de pixels de taille size ,de milieu (x0,y0) et de couleur RGB</returns> /// <returns>Matrice de pixels de taille size ,de milieu (x0,y0) et de couleur RGB</returns>
private byte[,] GetMatrix(int x0, int y0, int size, RGB rgb) private unsafe void GetMatrix(int x0, int y0, int size, RGB rgb, byte* matrixPtr)
{ {
byte[,] matrix = new byte[size, size];
for (int i = 0; i < size * size; i++) for (int i = 0; i < size * size; i++)
{ {
int x = x0 + (i % size) - ((size - 1) / 2); int x = x0 + (i % size) - ((size - 1) / 2);
...@@ -175,14 +173,12 @@ namespace S04_Projet ...@@ -175,14 +173,12 @@ namespace S04_Projet
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)
matrix[(i % size), (i / size)] = Pixels[x, y].r; *(matrixPtr + i) = Pixels[x, y].r;
else if (rgb == RGB.G) else if (rgb == RGB.G)
matrix[(i % size), (i / size)] = Pixels[x, y].g; *(matrixPtr + i) = Pixels[x, y].g;
else if (rgb == RGB.B) else if (rgb == RGB.B)
matrix[(i % size), (i / size)] = Pixels[x, y].b; *(matrixPtr + i) = Pixels[x, y].b;
} }
return matrix;
} }
/// <summary> /// <summary>
...@@ -193,12 +189,13 @@ namespace S04_Projet ...@@ -193,12 +189,13 @@ namespace S04_Projet
/// <param name="size">Taille du filtre</param> /// <param name="size">Taille du filtre</param>
/// <param name="factor">Factor de normalisation</param> /// <param name="factor">Factor de normalisation</param>
/// <returns>Résultat du calcul convolutinnel</returns> /// <returns>Résultat du calcul convolutinnel</returns>
public static byte ConvolutionalResult(byte[,] matrix, short[,] conv, int size, double factor) public unsafe byte ConvolutionalResult(byte* matrix, short* conv, int size, double factor)
{ {
short r = 0; ; short r = 0; ;
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++) for (int i = 0; i < size * size; i++)
r += (short)(matrix[x, y] * conv[x, y]); r += (short)(*(matrix + i) * *(conv + i));
r = (short)Math.Round(r * factor); r = (short)Math.Round(r * factor);
if (r > 255) return 255; if (r > 255) return 255;
else if (r < 0) return 0; else if (r < 0) return 0;
......
using System; using System;
using System.IO; using System.IO;
using System.Diagnostics;
using System.Text; using System.Text;
using System.Threading;
namespace S04_Projet namespace S04_Projet
{ {
...@@ -80,15 +78,15 @@ namespace S04_Projet ...@@ -80,15 +78,15 @@ namespace S04_Projet
/// <summary> /// <summary>
/// Charge une image. Multithreading WIP. Dépendante de la fonction LoadImage pour le multithreading /// Charge une image. Multithreading WIP. Dépendante de la fonction LoadImage pour le multithreading
/// </summary> /// </summary>
private void FromFileToImage() private unsafe void FromFileToImage()
{ {
opt = new Options(); opt = new Options();
#region Header #region Header
opt.format = (char)file[0] + "" + (char)file[1]; opt.format = (char)file[0] + "" + (char)file[1];
opt.fileSize = EndianToInt(file, 2); opt.fileSize = EndianToInt(file, 2);
opt.offset = EndianToInt(file, 10); opt.offset = EndianToInt(file, 10);
#endregion #endregion
#region File info #region File info
opt.fileInfoHeaderSize = EndianToInt(file, 14); opt.fileInfoHeaderSize = EndianToInt(file, 14);
opt.width = EndianToInt(file, 18); opt.width = EndianToInt(file, 18);
...@@ -103,7 +101,6 @@ namespace S04_Projet ...@@ -103,7 +101,6 @@ namespace S04_Projet
opt.nbColor = EndianToInt(file, 46); opt.nbColor = EndianToInt(file, 46);
opt.nbImportantColor = EndianToInt(file, 50); opt.nbImportantColor = EndianToInt(file, 50);
#endregion #endregion
#region Pixel array #region Pixel array
int pixelNumber = opt.width * opt.height; int pixelNumber = opt.width * opt.height;
Pixels = new Pixel[opt.width, opt.height]; Pixels = new Pixel[opt.width, opt.height];
...@@ -114,17 +111,22 @@ namespace S04_Projet ...@@ -114,17 +111,22 @@ namespace S04_Projet
{ {
int x, y; int x, y;
byte r, g, b; byte r, g, b;
fixed (byte* filePtr = file)
{
for (int i = 0; i < pixelNumber; i++) for (int i = 0; i < pixelNumber; i++)
{ {
x = i % opt.width; x = i % opt.width;
y = i / opt.width; y = i / opt.width;
b = file[i * 3 + opt.padding * y + opt.offset]; /*b = file[i * 3 + opt.padding * y + opt.offset];
g = file[i * 3 + opt.padding * y + opt.offset + 1]; g = file[i * 3 + opt.padding * y + opt.offset + 1];
r = file[i * 3 + opt.padding * y + opt.offset + 2]; r = file[i * 3 + opt.padding * y + opt.offset + 2];*/
b = *(filePtr + i * 3 + opt.padding * y + opt.offset);
g = *(filePtr + i * 3 + opt.padding * y + opt.offset + 1);
r = *(filePtr + i * 3 + opt.padding * y + opt.offset + 2);
Pixels[x, y] = new Pixel(r, g, b); Pixels[x, y] = new Pixel(r, g, b);
} }
} }
}
#endregion #endregion
#region Multithreading #region Multithreading
...@@ -151,6 +153,7 @@ namespace S04_Projet ...@@ -151,6 +153,7 @@ namespace S04_Projet
int bytesNumber = nbPixel * 3 + opt.offset + opt.height * opt.padding; // Multiply by 3 because 3 bytes / pixel int bytesNumber = nbPixel * 3 + opt.offset + opt.height * opt.padding; // Multiply by 3 because 3 bytes / pixel
byte[] file = new byte[bytesNumber]; byte[] file = new byte[bytesNumber];
#region HEADER #region HEADER
// FORMAT // FORMAT
ASCIIEncoding encoding = new ASCIIEncoding(); ASCIIEncoding encoding = new ASCIIEncoding();
......
...@@ -33,6 +33,13 @@ namespace S04_Projet ...@@ -33,6 +33,13 @@ namespace S04_Projet
this.b = b; this.b = b;
} }
public unsafe Pixel(byte* r, byte* g, byte* b)
{
this.r = *r;
this.g = *g;
this.b = *b;
}
/// <summary> /// <summary>
/// Assigne une même valeur à toutes les couleurs /// Assigne une même valeur à toutes les couleurs
/// </summary> /// </summary>
...@@ -75,6 +82,15 @@ namespace S04_Projet ...@@ -75,6 +82,15 @@ namespace S04_Projet
return new byte[] { r, g, b }; return new byte[] { r, g, b };
} }
public unsafe byte* getRGBPtr()
{
unsafe
{
fixed (byte* ptr = new byte[] { r, g, b })
return ptr;
}
}
public byte[] getBGR() public byte[] getBGR()
{ {
return new byte[] { b, g, r }; return new byte[] { b, g, r };
......
...@@ -58,8 +58,9 @@ namespace S04_Projet ...@@ -58,8 +58,9 @@ namespace S04_Projet
}; };
#endregion #endregion
MyImage imgg = new MyImage("flocon.bmp");
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
MyImage imgg = new MyImage("img/flocon.bmp");
Console.WriteLine("Loading done"); Console.WriteLine("Loading done");
sw.Start(); sw.Start();
imgg.ApplyConvFilter(edgeDetect1Filter, 1); imgg.ApplyConvFilter(edgeDetect1Filter, 1);
......
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