[chronojump] Created a compression utility to send signals



commit 7254f90d4bfbe0a814c22e3043484522d83add0c
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Jan 28 19:18:39 2015 +0100

    Created a compression utility to send signals

 src/gui/encoder.cs |    6 ++++++
 src/utilEncoder.cs |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)
---
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 6f126d9..b90753a 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -1294,6 +1294,12 @@ public partial class ChronoJumpWindow
                        }
                }
 
+               //test: try to compress signal in order to send if.
+               //obviously this is not going to be done here
+
+               LogB.Information("Trying compress function");
+               LogB.Information(UtilEncoder.CompressSignal(UtilEncoder.GetEncoderDataTempFileName()));
+
                if(success) {   
                        //force a recalculate but not save the curve (we are loading)
                        encoderCalculeCurves(encoderActions.LOAD);
diff --git a/src/utilEncoder.cs b/src/utilEncoder.cs
index e29987e..d4055e9 100644
--- a/src/utilEncoder.cs
+++ b/src/utilEncoder.cs
@@ -691,6 +691,56 @@ public class UtilEncoder
        }
 
 
+       /* convert this
+        * 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 2, 1
+        * to
+        * 0*6 1 0*2 1*2 2 1
+        * in order to be shorter if has to be sended by network
+        * this compression reduces size six times aproximately
+        */
+       public static string CompressSignal(string fileNameSignal)
+       {
+               string contents = Util.ReadFile(fileNameSignal, false);
+               string compressed = "";
+               
+               bool start = true;
+               int digit = -10000;
+               int digitPre = -10000; //just an impossible mark
+               int rep = 0;
+               for(int i=0; i < contents.Length; i++) 
+               {
+                       if(! Char.IsDigit(contents[i]))
+                               continue;
+
+                       digit = contents[i] -'0'; 
+                       if(start) {
+                               rep ++;
+                               start = false;
+                       } else if(digit == digitPre)
+                               rep ++;
+                       else {
+                               if(rep == 1)
+                                       compressed += digitPre.ToString() + " ";
+                               else {
+                                       compressed += digitPre.ToString() + "*" + rep.ToString() + " ";
+                                       rep = 1;
+                               }
+                       }
+
+                       digitPre = digit;
+               }
+
+               if(rep == 0)
+                       compressed += "";
+               else if(rep == 1)
+                       compressed += digit.ToString();
+               else
+                       compressed += digit.ToString() + "*" + rep.ToString();
+
+               return compressed;
+       }
+
+
        private static string [] encoderFindPos(string contents, int start, int duration) {
                int startPos = 0;
                int durationPos = 0;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]