Advertisement
Guest User

Sorted trigrad

a guest
Jul 4th, 2015
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Linq;
  6. using System.Security.Cryptography.X509Certificates;
  7. using TriangleNet;
  8.  
  9. namespace Trigrad.DataTypes
  10. {
  11.     /// <summary> The TrigradCompressed form of a bitmap. </summary>
  12.     public class TrigradCompressed
  13.     {
  14.         /// <summary> Constructs an empty TrigradCompressed. </summary>
  15.         public TrigradCompressed()
  16.         {
  17.  
  18.         }
  19.  
  20.         /// <summary> A dictionary of sampled points to their corresponding colors. </summary>
  21.         public Dictionary<Point, Color> SampleTable = new Dictionary<Point, Color>();
  22.         /// <summary> The width of the bitmap. </summary>
  23.         public int Width;
  24.         /// <summary> The height of the bitmap. </summary>
  25.         public int Height;
  26.         /// <summary> Provides a visualisation of the SampleTable. </summary>
  27.         public Bitmap DebugVisualisation()
  28.         {
  29.             Bitmap bitmap = new Bitmap(Width, Height);
  30.             foreach (var value in SampleTable)
  31.             {
  32.                 Point p = value.Key;
  33.  
  34.                 bitmap.SetPixel(p.X, p.Y, value.Value);
  35.             }
  36.             return bitmap;
  37.         }
  38.  
  39.         /// <summary> Loads a TrigradCompressed image from a stream. </summary>
  40.         public TrigradCompressed(Stream s)
  41.         {
  42.             using (GZipStream dezipper = new GZipStream(s, CompressionMode.Decompress))
  43.             using (BinaryReader reader = new BinaryReader(new MemoryStream()))
  44.             {
  45.                 dezipper.CopyTo(reader.BaseStream);
  46.                 reader.BaseStream.Position = 0;
  47.  
  48.  
  49.                 Width = reader.ReadUInt16();
  50.                 Height = reader.ReadUInt16();
  51.  
  52.                 List<Color> colorIndex = new List<Color>();
  53.                 Dictionary<Point, ushort> pointIndex = new Dictionary<Point, ushort>();
  54.  
  55.                 ushort colors = reader.ReadUInt16();
  56.                 List<byte> r = new List<byte>();
  57.                 List<byte> g = new List<byte>();
  58.                 List<byte> b = new List<byte>();
  59.                 for (int i = 0; i < colors; i++) r.Add(reader.ReadByte());
  60.                 for (int i = 0; i < colors; i++) g.Add(reader.ReadByte());
  61.                 for (int i = 0; i < colors; i++) b.Add(reader.ReadByte());
  62.                 for (int i = 0; i < colors; i++)
  63.                 {
  64.                     colorIndex.Add(Color.FromArgb(r[i], g[i], b[i]));
  65.                 }
  66.                 uint points = reader.ReadUInt32();
  67.                 List<ushort> x = new List<ushort>();
  68.                 List<ushort> y = new List<ushort>();
  69.                 List<ushort> c = new List<ushort>();
  70.                 for (int i = 0; i < points; i++) x.Add(reader.ReadUInt16());
  71.                 for (int i = 0; i < points; i++) y.Add(reader.ReadUInt16());
  72.                 for (int i = 0; i < points; i++) c.Add(reader.ReadUInt16());
  73.                 for (int i = 0; i < points; i++)
  74.                 {
  75.                     Point p = new Point(x[i], y[i]);
  76.                     ushort index = c[i];
  77.                     pointIndex.Add(p, index);
  78.                 }
  79.  
  80.                 foreach (var pair in pointIndex)
  81.                 {
  82.                     SampleTable.Add(pair.Key, colorIndex[pair.Value]);
  83.                 }
  84.  
  85.             }
  86.         }
  87.  
  88.         /// <summary> Saves a TrigradCompressed image to a stream. </summary>
  89.         public void Save(Stream s)
  90.         {
  91.             using(GZipStream zipper = new GZipStream(s,CompressionLevel.Optimal))
  92.             using (BinaryWriter writer = new BinaryWriter(zipper))
  93.             {
  94.                 writer.Write((ushort)Width);
  95.                 writer.Write((ushort)Height);
  96.  
  97.                 List<Color> colorIndex = new List<Color>();
  98.                 Dictionary<Point,ushort> pointIndex = new Dictionary<Point, ushort>();
  99.  
  100.                 foreach (var pair in SampleTable.OrderBy(kvp => kvp.Value.ToArgb()))
  101.                 {
  102.                     if (colorIndex.Contains(pair.Value))
  103.                     {
  104.                         pointIndex.Add(pair.Key, (ushort)colorIndex.IndexOf(pair.Value));
  105.                     }
  106.                     else
  107.                     {
  108.                         colorIndex.Add(pair.Value);
  109.                         pointIndex.Add(pair.Key,(ushort)(colorIndex.Count -1));
  110.                     }
  111.                 }
  112.  
  113.                 System.Console.WriteLine("{0} {1}", colorIndex.Count, pointIndex.Count);
  114.                 System.Console.WriteLine("{0} {1}", colorIndex.Count * 3, pointIndex.Count * 3 * 2);
  115.  
  116.                 writer.Write((ushort)colorIndex.Count);
  117.  
  118.                 foreach (var color in colorIndex)
  119.                 {
  120.                     writer.Write(color.R);
  121.                 }
  122.                 foreach (var color in colorIndex)
  123.                 {
  124.                     writer.Write(color.G);
  125.                 }
  126.                 foreach (var color in colorIndex)
  127.                 {
  128.                     writer.Write(color.B);
  129.                 }
  130.                 var colorEnd = s.Position;
  131.                
  132.  
  133.                 writer.Write((uint)pointIndex.Count);
  134.  
  135.                 //var sorted = pointIndex;
  136.                 //var sorted = pointIndex.OrderBy(kvp => kvp.Value).ToArray();
  137.                 var sorted = pointIndex.OrderBy(kvp => kvp.Key.X*ushort.MaxValue+kvp.Key.Y).ToArray();
  138.                 foreach (var pair in sorted)
  139.                 {
  140.                     writer.Write((ushort)pair.Key.X);
  141.                 }
  142.                 foreach (var pair in sorted)
  143.                 {
  144.                     writer.Write((ushort)pair.Key.Y);
  145.                 }
  146.                 var xyEnd = s.Position;
  147.                
  148.  
  149.                 foreach (var pair in sorted)
  150.                 {
  151.                     writer.Write(pair.Value);
  152.                 }
  153.                 var indexEnd = s.Position;
  154.                 System.Console.WriteLine("Colors: {0}", colorEnd);
  155.                 System.Console.WriteLine("XY: {0}", xyEnd - colorEnd);
  156.                 System.Console.WriteLine("Index: {0}", indexEnd - xyEnd);
  157.  
  158.                 writer.Flush();
  159.             }
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement