aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.RemoteDesktop/Encoders/GifEncoder.cs
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2020-03-02 23:30:34 +0200
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2020-03-02 23:30:34 +0200
commit0dcd742a3c35527386a93e1b1ef761c2aeff8308 (patch)
treed5adb3fee35e73af95fa5d68b5316d25522471de /Software/Visual_Studio/Tango.RemoteDesktop/Encoders/GifEncoder.cs
parent1a7fb274158f8a0e279aef26206a65fefac8c4c3 (diff)
downloadTango-0dcd742a3c35527386a93e1b1ef761c2aeff8308.tar.gz
Tango-0dcd742a3c35527386a93e1b1ef761c2aeff8308.zip
Implemented Tango.RemoteDesktop.
Implemented png 8 bit quantization. Implemented RasterFrame bounds clipping. Refactored VectorFrame to use indexed colors.
Diffstat (limited to 'Software/Visual_Studio/Tango.RemoteDesktop/Encoders/GifEncoder.cs')
-rw-r--r--Software/Visual_Studio/Tango.RemoteDesktop/Encoders/GifEncoder.cs101
1 files changed, 101 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.RemoteDesktop/Encoders/GifEncoder.cs b/Software/Visual_Studio/Tango.RemoteDesktop/Encoders/GifEncoder.cs
new file mode 100644
index 000000000..abf69d9a8
--- /dev/null
+++ b/Software/Visual_Studio/Tango.RemoteDesktop/Encoders/GifEncoder.cs
@@ -0,0 +1,101 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.RemoteDesktop.Encoders
+{
+ public class GifEncoder : JpegEncoder
+ {
+ public GifEncoder(IFrame frame) : base(frame)
+ {
+
+ }
+
+ public override MemoryStream ToStream()
+ {
+ return ToStream(100);
+ }
+
+ public override MemoryStream ToStream(long quality)
+ {
+ MemoryStream ms = new MemoryStream();
+ var gif = MakeTransparentGif(Frame.ToBitmap(), Color.Transparent);
+ gif.Save(ms, ImageFormat.Gif);
+ gif.Dispose();
+ ms.Position = 0;
+ return ms;
+ }
+
+ public static Bitmap MakeTransparentGif(Bitmap bitmap, Color color)
+ {
+ byte R = color.R;
+ byte G = color.G;
+ byte B = color.B;
+ MemoryStream fin = new MemoryStream();
+ bitmap.Save(fin, System.Drawing.Imaging.ImageFormat.Gif);
+ MemoryStream fout = new MemoryStream((int)fin.Length);
+ int count = 0;
+ byte[] buf = new byte[256];
+ byte transparentIdx = 0;
+ fin.Seek(0, SeekOrigin.Begin);
+ //header
+ count = fin.Read(buf, 0, 13);
+ if ((buf[0] != 71) || (buf[1] != 73) || (buf[2] != 70)) return null; //GIF
+ fout.Write(buf, 0, 13);
+ int i = 0;
+ if ((buf[10] & 0x80) > 0)
+ {
+ i = 1 << ((buf[10] & 7) + 1) == 256 ? 256 : 0;
+ }
+ for (; i != 0; i--)
+ {
+ fin.Read(buf, 0, 3);
+ if ((buf[0] == R) && (buf[1] == G) && (buf[2] == B))
+ {
+ transparentIdx = (byte)(256 - i);
+ }
+ fout.Write(buf, 0, 3);
+ }
+ bool gcePresent = false;
+ while (true)
+ {
+ fin.Read(buf, 0, 1);
+ fout.Write(buf, 0, 1);
+ if (buf[0] != 0x21) break;
+ fin.Read(buf, 0, 1);
+ fout.Write(buf, 0, 1);
+ gcePresent = (buf[0] == 0xf9);
+ while (true)
+ {
+ fin.Read(buf, 0, 1);
+ fout.Write(buf, 0, 1);
+ if (buf[0] == 0) break;
+ count = buf[0];
+ if (fin.Read(buf, 0, count) != count) return null;
+ if (gcePresent)
+ {
+ if (count == 4)
+ {
+ buf[0] |= 0x01;
+ buf[3] = transparentIdx;
+ }
+ }
+ fout.Write(buf, 0, count);
+ }
+ }
+ while (count > 0)
+ {
+ count = fin.Read(buf, 0, 1);
+ fout.Write(buf, 0, 1);
+ }
+ fin.Close();
+ fout.Flush();
+ return new Bitmap(fout);
+ }
+ }
+}