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
output = FilterOperation(img);
break;
case Operation.CreateImage: // TODO
output = CreateImageOperation();
break;
case Operation.Histogram:
output = HistogramOperation(img);
......@@ -336,10 +337,7 @@ namespace S04_Projet
public static MyImage SaveOperation(MyImage img)
{
bool error = false;
string path = "";
error = false;
Console.WriteLine("Veuillez saisir le chemin de sauvegarde du fichier");
path = Console.ReadLine();
if (!path.EndsWith(".bmp")) path += ".bmp";
......@@ -349,118 +347,111 @@ namespace S04_Projet
return null;
}
public static void AlertMessage(string msg)
public unsafe static MyImage CreateImageOperation()
{
Console.ForegroundColor = ConsoleColor.Red;
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;
Stopwatch sw = new Stopwatch();
Console.Clear();
sw.Start();
if (ope == Operation.Rotate90) output = img.Rotate90();
else if (ope == Operation.Rotate180) output = img.Rotate180();
else if (ope == Operation.Rotate270) output = img.Rotate270();
else if (ope == Operation.GrayScaleLinear || ope == Operation.GrayScaleLuminosity)
Options opt = new Options
{
bool retry = false;
int input;
do
{
if (retry) Console.WriteLine("Saisie incorrecte");
Console.WriteLine("Veuillez saisir le nombre de nuances de gris désirées (entre 2 et 255)");
retry = !int.TryParse(Console.ReadLine(), out input);
} while (retry && input >= 2 && input <= 255);
offset = 54,
fileInfoHeaderSize = 40,
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.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);
do opt.height = AskForInteger("Longueur de l'image :", error, "Veuillez saisir un entier supérieur à 0."); while (error = opt.height <= 0);
error = false;
do opt.width = AskForInteger("Largeur de l'image :", error, "Veuillez saisir un entier supérieur à 0."); while (error = opt.width <= 0);
error = false;
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)
{
do height = AskForInteger("Longueur du rectangle :", error, "Veuillez saisir un entier supérieur à 0."); while (error = height <= 0);
error = false;
do width = AskForInteger("Largeur du rectangle :", error, "Veuillez saisir un entier supérieur à 0."); while (error = width <= 0);
error = false;
}
else if(ope == Operation.Shrink)
else if (form == 2)
{
bool retry = false;
int input;
do width = AskForInteger("Rayon du cercle :", error, "Veuillez saisir un entier supérieur à 0."); while (error = width <= 0);
error = false;
}
do
{
if (retry) Console.WriteLine("Saisie incorrecte");
Console.WriteLine("Veuillez saisir le facteur de rétrécissement souhaité");
pixels = new byte[opt.width * opt.height];
opt.fileSize = opt.offset + opt.width * opt.height * 3;
retry = !int.TryParse(Console.ReadLine(), out input);
} while (retry && input >= 2);
sw.Restart();
sw.Restart();
output = img.Shrink(input);
}
else if(ope == Operation.SwitchMultiThreading)
fixed (byte* pixelsPtr = pixels)
{
Program.MULTITHREADING = !Program.MULTITHREADING;
if (Program.MULTITHREADING)
if (form == 1)
{
lastOperationMessage = "Multithreading activé avec succès";
menuItems[(int)ope] = "Désactiver le multithreading";
int x0 = x - width / 2;
int y0 = y - height / 2;
for (int i = 0; i < width; i++)
{
if (x0 + i > 0 && x0 + i < opt.width)
{
if (y0 >= 0)
*(pixelsPtr + x0 + i + y0 * opt.width) = 255;
if (y0 + height < opt.height)
*(pixelsPtr + x0 + i + (y0 + height) * opt.width) = 255;
}
}
for (int i = 0; i < height; i++)
{
if (y0 + i > 0 && y0 + i < opt.height)
{
if (x0 >= 0)
*(pixelsPtr + x0 + (y0 + i) * opt.width) = 255;
if (x0 + width < opt.width)
*(pixelsPtr + x0 + width + (y0 + i) * opt.width) = 255;
}
}
}
else
{
lastOperationMessage = "Multithreading désactivé avec succès";
menuItems[(int)ope] = "Activer le multithreading";
int x0;
int y0;
for (double i = 0; i < 360; i+= 0.001)
{
x0 = x + (int)(width * Math.Cos(i));
y0 = y + (int)(width * Math.Sin(i));
*(pixelsPtr + x0 + y0 * opt.width) = 255;
}
}
elapsedTime = 0;
return img;
}
else if (ope == Operation.Save)
{
string path;
bool anotherTry = false;
do
{
if (anotherTry) Console.WriteLine("Le format du chemin est incorrect");
Console.WriteLine("Veuillez saisir le nom du fichier en sortie");
path = Console.ReadLine();
if (!path.EndsWith(".bmp")) path += ".bmp";
} while (anotherTry = !Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute));
sw.Restart();
img.Save(path);
}
else
{
throw new Exception("Opération non reconnue");
}
sw.Stop();
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 new MyImage(opt, pixels, pixels, pixels);
}
return output;
public static void AlertMessage(string msg)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(msg);
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// Ecran d'attente
/// </summary>
public static void WaitingScreen()
public static int AskForInteger(string msg, bool error, string errorMsg)
{
Console.Write("Traitement de l'image en cours");
while(true)
int result = 0;
do
{
Console.Write(".");
Thread.Sleep(250);
}
}*/
if (error) AlertMessage(errorMsg);
Console.WriteLine(msg);
} while (error = !int.TryParse(Console.ReadLine(), out result));
return result;
}
}
}
......@@ -148,8 +148,8 @@ namespace S04_Projet
/// <param name="output">Chemin de sortie de l'image</param>
private unsafe void FromImageToFile(string output)
{
int nbPixel = (opt.height * opt.width);
int bytesNumber = nbPixel * 3 + opt.offset + opt.height * opt.padding; // Multiply by 3 because 3 bytes / pixel
long nbPixel = (opt.height * opt.width);
long bytesNumber = nbPixel * 3 + opt.offset + opt.height * opt.padding; // Multiply by 3 because 3 bytes / pixel
byte[] file = new byte[bytesNumber];
......@@ -494,7 +494,7 @@ namespace S04_Projet
format = "BM"
};
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[] 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