Commit 7984ce1e authored by Théophile BORNON's avatar Théophile BORNON

Shrinking okay, convolution WIP

parent 6c5baa1f
...@@ -18,6 +18,7 @@ namespace S04_Projet ...@@ -18,6 +18,7 @@ namespace S04_Projet
Rotate270, Rotate270,
GrayScaleLinear, GrayScaleLinear,
GrayScaleLuminosity, GrayScaleLuminosity,
Shrink,
Save Save
} }
...@@ -93,6 +94,7 @@ namespace S04_Projet ...@@ -93,6 +94,7 @@ namespace S04_Projet
"Rotation à 270°", "Rotation à 270°",
"Passage à une image en nuances de gris (linéairement)", "Passage à une image en nuances de gris (linéairement)",
"Passage à une image en nuances de gris (luminosité)", "Passage à une image en nuances de gris (luminosité)",
"Rétrécissement de l'image",
"Sauvegarder" "Sauvegarder"
}; };
...@@ -161,6 +163,22 @@ namespace S04_Projet ...@@ -161,6 +163,22 @@ namespace S04_Projet
if (ope == Operation.GrayScaleLinear) output = img.ToGrayScale((byte)input, MyImage.grayFilterType.LINEAR); 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.GrayScaleLuminosity) output = img.ToGrayScale((byte)input, MyImage.grayFilterType.LUMINOSITY);
} }
else if(ope == Operation.Shrink)
{
bool retry = false;
int input;
do
{
if (retry) Console.WriteLine("Saisie incorrecte");
Console.WriteLine("Veuillez saisir le facteur de rétrécissement souhaité");
retry = !int.TryParse(Console.ReadLine(), out input);
} while (retry && input >= 2);
sw.Restart();
output = img.Shrink(input);
}
else if (ope == Operation.Save) else if (ope == Operation.Save)
{ {
string path; string path;
......
...@@ -69,7 +69,7 @@ namespace S04_Projet ...@@ -69,7 +69,7 @@ namespace S04_Projet
#region File info #region File info
opt.fileInfoHeaderSize = EndianToInt(file, 14, 18); opt.fileInfoHeaderSize = EndianToInt(file, 14, 18);
opt.width = EndianToInt(file, 18, 22); opt.width = EndianToInt(file, 18, 22);
opt.stride = opt.width + (opt.width % 4 != 0 ? 4 - (opt.width % 4) : 0); // A CHANGER opt.padding = opt.width % 4;
opt.height = EndianToInt(file, 22, 26); opt.height = EndianToInt(file, 22, 26);
opt.colorPlanesNb = EndianToInt(file, 26, 28); opt.colorPlanesNb = EndianToInt(file, 26, 28);
opt.bitsPerPixel = EndianToInt(file, 28, 30); opt.bitsPerPixel = EndianToInt(file, 28, 30);
...@@ -125,7 +125,7 @@ namespace S04_Projet ...@@ -125,7 +125,7 @@ namespace S04_Projet
private void FromImageToFile(string output) private void FromImageToFile(string output)
{ {
int nbPixel = (opt.height * opt.width); int nbPixel = (opt.height * opt.width);
int bytesNumber = nbPixel * 3 + opt.offset; // 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
...@@ -153,9 +153,13 @@ namespace S04_Projet ...@@ -153,9 +153,13 @@ namespace S04_Projet
#endregion #endregion
#region Pixel array #region Pixel array
int x;
int y;
for (int i = 0; i < nbPixel; i++) for (int i = 0; i < nbPixel; i++)
{ {
MixArrays(file, Pixels[i % opt.width, i / opt.width].getBGR(), 54 + (i * 3)); x = i % opt.width;
y = i / opt.width;
MixArrays(file, Pixels[x, y].getBGR(), opt.offset + (i * 3) + opt.padding * y);
} }
#endregion #endregion
...@@ -212,10 +216,69 @@ namespace S04_Projet ...@@ -212,10 +216,69 @@ namespace S04_Projet
} }
#endregion #endregion
public MyImage Enlarge(int coeff) /*public MyImage Enlarge(int coeff)
{ {
Options options = opt.Copy(); Options options = opt.Copy();
Pixel[,] PixelArr = new Pixel[opt.width*2, opt.height*2]; options.width *= coeff;
options.height *= coeff;
Pixel[,] PixelArr = new Pixel[opt.width * 2, opt.height * 2];
for (int i = 0; i < options.width * options.height; i++)
{
int x = i % options.width;
int y = i % options.height;
byte[] fromX = Pixels[x, y].getRGB();
byte[] toX;
byte[] toY;
byte[] toXY;
if (x + 1 < options.width) toX = Pixels[x + 1, y].getRGB();
if (y + 1 < options.height) toY = Pixels[x, y + 1].getRGB();
if (y + 1 < options.height && x + 1 < options.width) toXY = Pixels[x + 1, y + 1].getRGB();
}
}*/
public MyImage Shrink(int coeff)
{
Options options = opt.Copy();
options.width /= coeff;
options.height /= coeff;
options.padding = options.width % 4;
options.imgSize = options.width * options.height * 3 + options.padding * options.height;
options.fileSize = options.imgSize + options.fileInfoHeaderSize;
Pixel[,] PixelArr = new Pixel[options.width, options.height];
int x0, y0, x, y;
int[] rgb;
byte[] _rgb;
for (int i = 0; i < options.width * options.height; i++)
{
x0 = i % options.width;
y0 = i / options.width;
rgb = new int[] { 0, 0, 0 };
for (int j = 0; j < coeff * coeff; j++)
{
x = x0 * coeff + j % coeff;
y = y0 * coeff + j / coeff;
_rgb = Pixels[x, y].getRGB();
for (int k = 0; k < 3; k++)
rgb[k] += _rgb[k];
}
for (int j = 0; j < 3; j++)
rgb[j] = (byte)(rgb[j] / (coeff * coeff));
PixelArr[x0, y0] = new Pixel(rgb);
}
return new MyImage(options, PixelArr);
} }
public MyImage ToGrayScale(byte scale, grayFilterType type) public MyImage ToGrayScale(byte scale, grayFilterType type)
...@@ -242,7 +305,7 @@ namespace S04_Projet ...@@ -242,7 +305,7 @@ namespace S04_Projet
int size = filter.GetLength(0); int size = filter.GetLength(0);
Options options = opt.Copy(); Options options = opt.Copy();
int nbPixel = options.height * options.width; int nbPixel = options.height * options.width;
Pixel[,] PixelArr = new Pixel[opt.width, opt.height]; Pixel[,] PixelArr = new Pixel[options.width, options.height];
int x, y; int x, y;
int[,] rMatrix, gMatrix, bMatrix; int[,] rMatrix, gMatrix, bMatrix;
...@@ -280,7 +343,7 @@ namespace S04_Projet ...@@ -280,7 +343,7 @@ namespace S04_Projet
{ {
int[,] matrix = new int[size, size]; int[,] matrix = new int[size, size];
for (int i = 0; i < Math.Pow(size, 2); 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);
int y = y0 + (i / size) - ((size - 1) / 2); int y = y0 + (i / size) - ((size - 1) / 2);
...@@ -299,10 +362,10 @@ namespace S04_Projet ...@@ -299,10 +362,10 @@ namespace S04_Projet
return matrix; return matrix;
} }
private byte ConvolutionalResult(int[,] matrix, int[,] conv, int size, double factor) public static byte ConvolutionalResult(int[,] matrix, int[,] conv, int size, double factor)
{ {
int r = 0; int r = 0;
for (int i = 0; i < Math.Pow(size, 2); i++) for (int i = 0; i < size * size; i++)
{ {
int x = i % size; int x = i % size;
int y = i / size; int y = i / size;
......
...@@ -11,7 +11,6 @@ namespace S04_Projet ...@@ -11,7 +11,6 @@ namespace S04_Projet
public int fileInfoHeaderSize; public int fileInfoHeaderSize;
public int width; public int width;
public int height; public int height;
public int stride;
public int colorPlanesNb; public int colorPlanesNb;
public int bitsPerPixel; public int bitsPerPixel;
public int compressionMethod; public int compressionMethod;
...@@ -33,7 +32,6 @@ namespace S04_Projet ...@@ -33,7 +32,6 @@ namespace S04_Projet
options.fileInfoHeaderSize = fileInfoHeaderSize; options.fileInfoHeaderSize = fileInfoHeaderSize;
options.width = width; options.width = width;
options.height = height; options.height = height;
options.stride = stride;
options.padding = padding; options.padding = padding;
options.colorPlanesNb = colorPlanesNb; options.colorPlanesNb = colorPlanesNb;
options.bitsPerPixel = bitsPerPixel; options.bitsPerPixel = bitsPerPixel;
......
...@@ -12,6 +12,10 @@ namespace S04_Projet ...@@ -12,6 +12,10 @@ namespace S04_Projet
public byte g { get; private set; } public byte g { get; private set; }
public byte b { get; private set; } public byte b { get; private set; }
public double H { get; private set; }
public double S { get; private set; }
public double L { get; private set; }
/// <summary> /// <summary>
/// Prend les trois couleurs en paramètres /// Prend les trois couleurs en paramètres
/// </summary> /// </summary>
...@@ -47,6 +51,13 @@ namespace S04_Projet ...@@ -47,6 +51,13 @@ namespace S04_Projet
b = rgb[2]; b = rgb[2];
} }
public Pixel(int[] rgb)
{
r = (byte)rgb[0];
g = (byte)rgb[1];
b = (byte)rgb[2];
}
/// <summary> /// <summary>
/// Retourne un tableau contenant les valeurs RGB du pixel /// Retourne un tableau contenant les valeurs RGB du pixel
/// </summary> /// </summary>
...@@ -89,5 +100,27 @@ namespace S04_Projet ...@@ -89,5 +100,27 @@ namespace S04_Projet
public void SetR(byte r) { this.r = r; } public void SetR(byte r) { this.r = r; }
public void SetG(byte g) { this.g = g; } public void SetG(byte g) { this.g = g; }
public void SetB(byte b) { this.b = b; } public void SetB(byte b) { this.b = b; }
private void computeHSl()
{
double rH = r / 255.0;
double gH = g / 255.0;
double bH = b / 255.0;
double Cmax = Math.Max(Math.Max(rH, gH), bH);
double Cmin = Math.Min(Math.Min(rH, gH), bH);
double delta = Cmax - Cmin;
L = (Cmin + Cmax) / 2.0;
if (delta == 0) S = 0;
else S = delta / (1.0 - Math.Abs(2.0 * L - 1));
if (delta == 0) H = 0;
else if (Cmax == rH) H = 60 * (((gH - bH) / delta) % 6);
else if (Cmax == gH) H = 60 * (((bH - rH) / delta) + 2);
else H = 60 * (((rH - gH) / delta) + 4);
}
} }
} }
...@@ -13,6 +13,55 @@ namespace S04_Projet ...@@ -13,6 +13,55 @@ namespace S04_Projet
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
int[,] boxBlurFilter = new int[,] {
{1,1,1 },
{1,1,1 },
{1,1,1 }
};
int[,] identityFilter = new int[,]
{
{ 0, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 0 }
};
int[,] edgeDetect1Filter = new int[,]
{
{ 1, 0, -1 },
{ 0, 0, 0 },
{ -1, 0, 1 }
};
int[,] edgeDetect2Filter = new int[,]
{
{ 0, 1, 0 },
{ 1, -4, 1 },
{ 0, 1, 0 }
};
int[,] edgeDetect3Filter = new int[,]
{
{ -1, -1, -1 },
{ -1, 8, -1 },
{ -1, -1, -1 }
};
int[,] sharpenFilter = new int[,]
{
{ 0, -1, 0 },
{ -1, 5, -1 },
{ 0, -1, 0 }
};
MyImage imgg = new MyImage("img/ex.bmp");
//imgg.ApplyConvFilter(identityFilter, 1).Save("id.bmp");
imgg.ApplyConvFilter(edgeDetect1Filter, 1).Save("edge1.bmp");
//imgg.ApplyConvFilter(edgeDetect2Filter, 1).Save("edge2.bmp");
imgg.ApplyConvFilter(edgeDetect3Filter, 1).Save("edge3.bmp");
//imgg.ApplyConvFilter(sharpenFilter, 1).Save("sharpen.bmp");
//imgg.ApplyConvFilter(boxBlurFilter, (double)1/9).Save("blur.bmp");
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("#############################################################"); Console.WriteLine("#############################################################");
Console.WriteLine("### Bienvenue ###"); Console.WriteLine("### Bienvenue ###");
...@@ -35,10 +84,6 @@ namespace S04_Projet ...@@ -35,10 +84,6 @@ namespace S04_Projet
//Console.Clear(); //Console.Clear();
Console.Read(); Console.Read();
#region Test #region Test
/*MyImage imgRotate90 = img.Rotate90(); /*MyImage imgRotate90 = img.Rotate90();
imgRotate90.Save("90.bmp"); imgRotate90.Save("90.bmp");
......
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