Commit 1f714dec authored by BORNON Théophile's avatar BORNON Théophile

TD1

parent b2f32485
......@@ -10,11 +10,20 @@ namespace S04_Projet
{
public class MyImage
{
private string type;
private int fileSize;
private int offset;
private int fileInfoHeaderSize;
private int width;
private int height;
private int colorPlanesNb;
private int bitsPerPixel;
private int compressionMethod;
private int imgSize;
private int horizontalRes;
private int VerticalRes;
private int nbColor;
private int nbImportantColor;
private string format;
private int fileSize;
private int offset;
private Pixel[,] Pixels;
public MyImage(string path)
......@@ -31,25 +40,25 @@ namespace S04_Projet
finally
{
#region Header
string format = (char)file[0] + "" + (char)file[1];
int endian = EndianToInt(file, 2, 6);
int offset = EndianToInt(file, 10, 14);
Console.WriteLine("Format : {0}\nTaille : {1} octets", format, endian);
format = (char)file[0] + "" + (char)file[1];
fileSize = EndianToInt(file, 2, 6);
offset = EndianToInt(file, 10, 14);
Console.WriteLine("Format : {0}\nTaille : {1} octets", format, fileSize);
Console.WriteLine("Offset : " + offset);
#endregion
#region File info
int fileInfoHeaderSize = EndianToInt(file, 14, 18);
int width = EndianToInt(file, 18, 22);
int height = EndianToInt(file, 22, 26);
int colorPlanesNb = EndianToInt(file, 26, 28);
int bitsPerPixel = EndianToInt(file, 28, 30);
int compressionMethod = EndianToInt(file, 30, 34);
int imgSize = EndianToInt(file, 34, 38);
int horizontalRes = EndianToInt(file, 38, 42);
int VerticalRes = EndianToInt(file, 42, 46);
int nbColor = EndianToInt(file, 46, 50);
int nbImportantColor = EndianToInt(file, 50, 54);
fileInfoHeaderSize = EndianToInt(file, 14, 18);
width = EndianToInt(file, 18, 22);
height = EndianToInt(file, 22, 26);
colorPlanesNb = EndianToInt(file, 26, 28);
bitsPerPixel = EndianToInt(file, 28, 30);
compressionMethod = EndianToInt(file, 30, 34);
imgSize = EndianToInt(file, 34, 38);
horizontalRes = EndianToInt(file, 38, 42);
VerticalRes = EndianToInt(file, 42, 46);
nbColor = EndianToInt(file, 46, 50);
nbImportantColor = EndianToInt(file, 50, 54);
Console.WriteLine("File info header size : {0} octets", fileInfoHeaderSize);
Console.WriteLine("Size : {0}x{1}px", width, height);
......@@ -61,7 +70,7 @@ namespace S04_Projet
Console.WriteLine("Number of colors : " + nbColor);
Console.WriteLine("Number of important colors : " + nbImportantColor);
#endregion
#region Pixel array
int pixelNumber = width * height;
Pixels = new Pixel[width, height];
......@@ -69,7 +78,7 @@ namespace S04_Projet
Stopwatch s = new Stopwatch();
#region Mono
s.Start();
/*s.Start();
for (int i = 0; i < pixelNumber; i++)
{
int x = i % width;
......@@ -77,44 +86,88 @@ namespace S04_Projet
byte r = file[i * 3 + offset];
byte g = file[i * 3 + offset + 1];
byte b = file[i * 3 + offset + 2];
Pixels[x, y] = new Pixel(r,g,b);
Pixels[x, y] = new Pixel(r, g, b);
}
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds);
Console.WriteLine(s.ElapsedMilliseconds);*/
#endregion
#region Parallel
/*s.Start();
s.Start();
Parallel.For(0, pixelNumber, i =>
{
int x = i % width;
int y = i / width;
Pixels[x, y] = new Pixel(file[i * 3 + offset], file[i * 3 + offset + 1], file[i * 3 + offset + 2]);
Pixels[i % width, i / width] = new Pixel(file[i * 3 + offset], file[i * 3 + offset + 1], file[i * 3 + offset + 2]);
});
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds);*/
Console.WriteLine(s.ElapsedMilliseconds);
#endregion
#endregion
}
}
public void FromImageToFile(string path)
public void FromImageToFile(string output)
{
int nbPixel = (height * width);
int bytesNumber = nbPixel * 3 + offset; // Multiply by 3 because 3 bytes / pixel
byte[] file = new byte[bytesNumber];
#region HEADER
// FORMAT
ASCIIEncoding encoding = new ASCIIEncoding();
mixArrays(file, encoding.GetBytes(format), 0);
// FILE SIZE
mixArrays(file, IntToEndian(fileSize), 2);
// OFFSET
mixArrays(file, IntToEndian(offset), 10);
#endregion
#region File Info Header
mixArrays(file, IntToEndian(fileInfoHeaderSize), 14);
mixArrays(file, IntToEndian(width), 18);
mixArrays(file, IntToEndian(height), 22);
mixArrays(file, IntToEndian(1), 26); // Number of colors planes
mixArrays(file, IntToEndian(bitsPerPixel), 28);
mixArrays(file, IntToEndian(compressionMethod), 30);
mixArrays(file, IntToEndian(fileSize - offset), 34); // Image size TO CHANGE IF NO MORE BI_RGB
mixArrays(file, IntToEndian(horizontalRes), 38);
mixArrays(file, IntToEndian(VerticalRes), 42);
mixArrays(file, IntToEndian(nbColor), 46);
mixArrays(file, IntToEndian(0), 50); // Number of important colors
#endregion
#region Pixel array
for (int i = 0; i < nbPixel; i++)
{
mixArrays(file, Pixels[i % width, i / width].getRGB(), 54 + (i * 3));
}
File.WriteAllBytes(output, file);
#endregion
}
public static int EndianToInt(byte[] arr, int from, int to)
{
int somme = 0;
for (int i = from; i < to; i++) somme += (arr[i] << ((i-from) * 8));
for (int i = from; i < to; i++) somme += (arr[i] << ((i - from) * 8));
return somme;
}
/*
public byte[] IntToEndian(int val)
public static byte[] IntToEndian(int val)
{
byte[] endian = new byte[4];
for (int i = 0; i < 4; i++)
endian[i] = (byte)(val >> (i * 8));
return endian;
}
}*/
public void mixArrays(byte[] arr, byte[] toAdd, int start)
{
for (int i = start; i < start + toAdd.Length; i++)
{
arr[i] = toAdd[i - start];
}
}
}
}
......@@ -18,5 +18,10 @@ namespace S04_Projet
this.g = g;
this.b = b;
}
public byte[] getRGB()
{
return new byte[] { r, g, b };
}
}
}
......@@ -13,7 +13,8 @@ namespace S04_Projet
{
static void Main(string[] args)
{
MyImage img = new MyImage("img/lac_en_montagne.bmp");
MyImage img = new MyImage("img/flocon.bmp");
//img.FromImageToFile("test.bmp");
Console.Read();
}
}
......
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