Commit 944de5dd authored by BORNON Théophile's avatar BORNON Théophile

Create image from nothing available

parent e34ee352
...@@ -176,6 +176,7 @@ namespace S04_Projet ...@@ -176,6 +176,7 @@ namespace S04_Projet
output = FilterOperation(img); output = FilterOperation(img);
break; break;
case Operation.CreateImage: // TODO case Operation.CreateImage: // TODO
output = CreateImageOperation();
break; break;
case Operation.Histogram: case Operation.Histogram:
output = HistogramOperation(img); output = HistogramOperation(img);
...@@ -336,10 +337,7 @@ namespace S04_Projet ...@@ -336,10 +337,7 @@ namespace S04_Projet
public static MyImage SaveOperation(MyImage img) public static MyImage SaveOperation(MyImage img)
{ {
bool error = false;
string path = ""; string path = "";
error = false;
Console.WriteLine("Veuillez saisir le chemin de sauvegarde du fichier"); Console.WriteLine("Veuillez saisir le chemin de sauvegarde du fichier");
path = Console.ReadLine(); path = Console.ReadLine();
if (!path.EndsWith(".bmp")) path += ".bmp"; if (!path.EndsWith(".bmp")) path += ".bmp";
...@@ -349,118 +347,111 @@ namespace S04_Projet ...@@ -349,118 +347,111 @@ namespace S04_Projet
return null; return null;
} }
public static void AlertMessage(string msg) public unsafe static MyImage CreateImageOperation()
{ {
Console.ForegroundColor = ConsoleColor.Red; Options opt = new Options
Console.WriteLine(msg);
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// Effectue l'opération demandée sur l'image passée en paramètres
/// </summary>
/// <param name="ope">Opération désirée</param>
/// <param name="img">Image sur laquelle effectuer l'opération</param>
/// <returns>Image avec l'opération effectuée dessus</returns>
/*public static MyImage PerformOperation(Operation ope, MyImage img)
{ {
MyImage output = null; offset = 54,
Stopwatch sw = new Stopwatch(); fileInfoHeaderSize = 40,
Console.Clear(); bitsPerPixel = 24,
format = "BM"
};
int x, y, form, width, height;
byte[] pixels;
opt.height = opt.width = width = height = x = y = form = 0;
bool error = false;
sw.Start(); do opt.height = AskForInteger("Longueur de l'image :", error, "Veuillez saisir un entier supérieur à 0."); while (error = opt.height <= 0);
if (ope == Operation.Rotate90) output = img.Rotate90(); error = false;
else if (ope == Operation.Rotate180) output = img.Rotate180(); do opt.width = AskForInteger("Largeur de l'image :", error, "Veuillez saisir un entier supérieur à 0."); while (error = opt.width <= 0);
else if (ope == Operation.Rotate270) output = img.Rotate270(); error = false;
else if (ope == Operation.GrayScaleLinear || ope == Operation.GrayScaleLuminosity) do form = AskForInteger("Type de forme :\n1)Rectangle\n2)Cercle :", error, "Veuillez saisir un entier égal à 1 ou 2."); while (error = (form != 1 && form != 2));
error = false;
do x = AskForInteger("Coordonnée x du centre de la forme :", error, "Veuillez saisir un entier supérieur ou égal à 0 et inférieur à " + opt.width + "."); while (error = (x < 0 || x >= opt.width));
error = false;
do y = AskForInteger("Coordonnée y du centre de la forme :", error, "Veuillez saisir un entier supérieur ou égal à 0 et inférieur à " + opt.width + "."); while (error = (y < 0 || y >= opt.width));
error = false;
if (form == 1)
{ {
bool retry = false; do height = AskForInteger("Longueur du rectangle :", error, "Veuillez saisir un entier supérieur à 0."); while (error = height <= 0);
int input; error = false;
do width = AskForInteger("Largeur du rectangle :", error, "Veuillez saisir un entier supérieur à 0."); while (error = width <= 0);
do error = false;
}
else if (form == 2)
{ {
if (retry) Console.WriteLine("Saisie incorrecte"); do width = AskForInteger("Rayon du cercle :", error, "Veuillez saisir un entier supérieur à 0."); while (error = width <= 0);
Console.WriteLine("Veuillez saisir le nombre de nuances de gris désirées (entre 2 et 255)"); error = false;
}
retry = !int.TryParse(Console.ReadLine(), out input); pixels = new byte[opt.width * opt.height];
} while (retry && input >= 2 && input <= 255); opt.fileSize = opt.offset + opt.width * opt.height * 3;
sw.Restart(); sw.Restart();
if (ope == Operation.GrayScaleLinear) output = img.ToGrayScale((byte)input, MyImage.GrayFilterType.LINEAR);
else if (ope == Operation.GrayScaleLuminosity) output = img.ToGrayScale((byte)input, MyImage.GrayFilterType.LUMINOSITY);
}
else if(ope == Operation.Shrink)
{
bool retry = false;
int input;
do fixed (byte* pixelsPtr = pixels)
{ {
if (retry) Console.WriteLine("Saisie incorrecte"); if (form == 1)
Console.WriteLine("Veuillez saisir le facteur de rétrécissement souhaité"); {
int x0 = x - width / 2;
retry = !int.TryParse(Console.ReadLine(), out input); int y0 = y - height / 2;
} while (retry && input >= 2);
sw.Restart(); for (int i = 0; i < width; i++)
output = img.Shrink(input);
}
else if(ope == Operation.SwitchMultiThreading)
{ {
Program.MULTITHREADING = !Program.MULTITHREADING; if (x0 + i > 0 && x0 + i < opt.width)
if (Program.MULTITHREADING)
{ {
lastOperationMessage = "Multithreading activé avec succès"; if (y0 >= 0)
menuItems[(int)ope] = "Désactiver le multithreading"; *(pixelsPtr + x0 + i + y0 * opt.width) = 255;
if (y0 + height < opt.height)
*(pixelsPtr + x0 + i + (y0 + height) * opt.width) = 255;
} }
else }
for (int i = 0; i < height; i++)
{
if (y0 + i > 0 && y0 + i < opt.height)
{ {
lastOperationMessage = "Multithreading désactivé avec succès"; if (x0 >= 0)
menuItems[(int)ope] = "Activer le multithreading"; *(pixelsPtr + x0 + (y0 + i) * opt.width) = 255;
if (x0 + width < opt.width)
*(pixelsPtr + x0 + width + (y0 + i) * opt.width) = 255;
}
} }
elapsedTime = 0;
return img;
} }
else if (ope == Operation.Save) else
{ {
string path; int x0;
bool anotherTry = false; int y0;
for (double i = 0; i < 360; i+= 0.001)
do
{ {
if (anotherTry) Console.WriteLine("Le format du chemin est incorrect"); x0 = x + (int)(width * Math.Cos(i));
Console.WriteLine("Veuillez saisir le nom du fichier en sortie"); y0 = y + (int)(width * Math.Sin(i));
path = Console.ReadLine();
if (!path.EndsWith(".bmp")) path += ".bmp";
} while (anotherTry = !Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute));
sw.Restart(); *(pixelsPtr + x0 + y0 * opt.width) = 255;
img.Save(path); }
} }
else
{
throw new Exception("Opération non reconnue");
} }
sw.Stop(); return new MyImage(opt, pixels, pixels, pixels);
elapsedTime = sw.ElapsedMilliseconds; }
lastOperationMessage = String.Format("Opération {0}, effectuée avec succès en {1}ms", ope, elapsedTime);
fileInfos = (output != null) ? output.ToString() : "";
return output; public static void AlertMessage(string msg)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(msg);
Console.ForegroundColor = ConsoleColor.White;
} }
/// <summary> public static int AskForInteger(string msg, bool error, string errorMsg)
/// Ecran d'attente
/// </summary>
public static void WaitingScreen()
{ {
Console.Write("Traitement de l'image en cours"); int result = 0;
while(true) do
{ {
Console.Write("."); if (error) AlertMessage(errorMsg);
Thread.Sleep(250); Console.WriteLine(msg);
} while (error = !int.TryParse(Console.ReadLine(), out result));
return result;
} }
}*/
} }
} }
...@@ -148,8 +148,8 @@ namespace S04_Projet ...@@ -148,8 +148,8 @@ namespace S04_Projet
/// <param name="output">Chemin de sortie de l'image</param> /// <param name="output">Chemin de sortie de l'image</param>
private unsafe void FromImageToFile(string output) private unsafe void FromImageToFile(string output)
{ {
int nbPixel = (opt.height * opt.width); long nbPixel = (opt.height * opt.width);
int bytesNumber = nbPixel * 3 + opt.offset + opt.height * opt.padding; // Multiply by 3 because 3 bytes / pixel long 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];
...@@ -494,7 +494,7 @@ namespace S04_Projet ...@@ -494,7 +494,7 @@ namespace S04_Projet
format = "BM" format = "BM"
}; };
options.height = (int)(options.width / 1.6180339887); options.height = (int)(options.width / 1.6180339887);
options.fileSize = options.fileInfoHeaderSize + options.width * options.height * 3; options.fileSize = options.offset + options.width * options.height * 3;
byte[] newRPixel = new byte[options.width * options.height]; byte[] newRPixel = new byte[options.width * options.height];
byte[] newGPixel = new byte[options.width * options.height]; byte[] newGPixel = new byte[options.width * options.height];
......
This image diff could not be displayed because it is too large. You can view the blob instead.
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