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

TD1

parent b2f32485
...@@ -10,11 +10,20 @@ namespace S04_Projet ...@@ -10,11 +10,20 @@ namespace S04_Projet
{ {
public class MyImage public class MyImage
{ {
private string type; private int fileInfoHeaderSize;
private int fileSize;
private int offset;
private int width; private int width;
private int height; 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; private Pixel[,] Pixels;
public MyImage(string path) public MyImage(string path)
...@@ -31,25 +40,25 @@ namespace S04_Projet ...@@ -31,25 +40,25 @@ namespace S04_Projet
finally finally
{ {
#region Header #region Header
string format = (char)file[0] + "" + (char)file[1]; format = (char)file[0] + "" + (char)file[1];
int endian = EndianToInt(file, 2, 6); fileSize = EndianToInt(file, 2, 6);
int offset = EndianToInt(file, 10, 14); offset = EndianToInt(file, 10, 14);
Console.WriteLine("Format : {0}\nTaille : {1} octets", format, endian); Console.WriteLine("Format : {0}\nTaille : {1} octets", format, fileSize);
Console.WriteLine("Offset : " + offset); Console.WriteLine("Offset : " + offset);
#endregion #endregion
#region File info #region File info
int fileInfoHeaderSize = EndianToInt(file, 14, 18); fileInfoHeaderSize = EndianToInt(file, 14, 18);
int width = EndianToInt(file, 18, 22); width = EndianToInt(file, 18, 22);
int height = EndianToInt(file, 22, 26); height = EndianToInt(file, 22, 26);
int colorPlanesNb = EndianToInt(file, 26, 28); colorPlanesNb = EndianToInt(file, 26, 28);
int bitsPerPixel = EndianToInt(file, 28, 30); bitsPerPixel = EndianToInt(file, 28, 30);
int compressionMethod = EndianToInt(file, 30, 34); compressionMethod = EndianToInt(file, 30, 34);
int imgSize = EndianToInt(file, 34, 38); imgSize = EndianToInt(file, 34, 38);
int horizontalRes = EndianToInt(file, 38, 42); horizontalRes = EndianToInt(file, 38, 42);
int VerticalRes = EndianToInt(file, 42, 46); VerticalRes = EndianToInt(file, 42, 46);
int nbColor = EndianToInt(file, 46, 50); nbColor = EndianToInt(file, 46, 50);
int nbImportantColor = EndianToInt(file, 50, 54); nbImportantColor = EndianToInt(file, 50, 54);
Console.WriteLine("File info header size : {0} octets", fileInfoHeaderSize); Console.WriteLine("File info header size : {0} octets", fileInfoHeaderSize);
Console.WriteLine("Size : {0}x{1}px", width, height); Console.WriteLine("Size : {0}x{1}px", width, height);
...@@ -61,7 +70,7 @@ namespace S04_Projet ...@@ -61,7 +70,7 @@ namespace S04_Projet
Console.WriteLine("Number of colors : " + nbColor); Console.WriteLine("Number of colors : " + nbColor);
Console.WriteLine("Number of important colors : " + nbImportantColor); Console.WriteLine("Number of important colors : " + nbImportantColor);
#endregion #endregion
#region Pixel array #region Pixel array
int pixelNumber = width * height; int pixelNumber = width * height;
Pixels = new Pixel[width, height]; Pixels = new Pixel[width, height];
...@@ -69,7 +78,7 @@ namespace S04_Projet ...@@ -69,7 +78,7 @@ namespace S04_Projet
Stopwatch s = new Stopwatch(); Stopwatch s = new Stopwatch();
#region Mono #region Mono
s.Start(); /*s.Start();
for (int i = 0; i < pixelNumber; i++) for (int i = 0; i < pixelNumber; i++)
{ {
int x = i % width; int x = i % width;
...@@ -77,44 +86,88 @@ namespace S04_Projet ...@@ -77,44 +86,88 @@ namespace S04_Projet
byte r = file[i * 3 + offset]; byte r = file[i * 3 + offset];
byte g = file[i * 3 + offset + 1]; byte g = file[i * 3 + offset + 1];
byte b = file[i * 3 + offset + 2]; 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(); s.Stop();
Console.WriteLine(s.ElapsedMilliseconds); Console.WriteLine(s.ElapsedMilliseconds);*/
#endregion #endregion
#region Parallel #region Parallel
/*s.Start(); s.Start();
Parallel.For(0, pixelNumber, i => Parallel.For(0, pixelNumber, i =>
{ {
int x = i % width; Pixels[i % width, i / width] = new Pixel(file[i * 3 + offset], file[i * 3 + offset + 1], file[i * 3 + offset + 2]);
int y = i / width;
Pixels[x, y] = new Pixel(file[i * 3 + offset], file[i * 3 + offset + 1], file[i * 3 + offset + 2]);
}); });
s.Stop(); s.Stop();
Console.WriteLine(s.ElapsedMilliseconds);*/ Console.WriteLine(s.ElapsedMilliseconds);
#endregion #endregion
#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) public static int EndianToInt(byte[] arr, int from, int to)
{ {
int somme = 0; 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; 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 ...@@ -18,5 +18,10 @@ namespace S04_Projet
this.g = g; this.g = g;
this.b = b; this.b = b;
} }
public byte[] getRGB()
{
return new byte[] { r, g, b };
}
} }
} }
...@@ -13,7 +13,8 @@ namespace S04_Projet ...@@ -13,7 +13,8 @@ namespace S04_Projet
{ {
static void Main(string[] args) 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(); 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