Example #1
0
        /// <summary>
        /// Encodes the captures into a single packet and prepares for it's transmission
        /// </summary>
        /// <param name="id">Client Id</param>
        /// <param name="screenImg">Screenshot</param>
        /// <param name="logStream">Key & Mouse event log stream</param>
        /// <param name="isSingleCapture">Live stream or buffered stream</param>
        public void EncodeCapture(int id, Bitmap screenImg, Stream logStream, bool isSingleCapture)
        {
            Quantizer oq = this.GetQuantizer();//new OctreeQuantizer(this.CaptureQuantizePalette, this.CaptureQuantizeDepth);

            screenImg = oq.Quantize(ScreenSnap.ShrinkBitmap(screenImg, this.CaptureShrinkFactor));
            this.EncodeCapture(id, new CapturePacket(ScreenSnap.SnapshotToStream(screenImg, System.Drawing.Imaging.ImageFormat.Png), logStream), isSingleCapture);
        }
Example #2
0
        private static readonly byte[] gifEnd = new byte[] { 0x3b };                                                                  //结束信息

        public override void AppendFrame(Bitmap image, int delay)
        {
            mStream.SetLength(0);
            mStream.Position = 0;
            using (var tempGif = quantizer.Quantize(image))
            {
                tempGif.Save(mStream, ImageFormat.Gif);
            }

            byte[] tempArray = mStream.GetBuffer();
            // 781开始为Graphic Control Extension块 标志为21 F9 04
            tempArray[784] = (byte)0x09; //图像刷新时屏幕返回初始帧 貌似不打会bug 意味不明 测试用
            delay          = delay / 10;
            tempArray[785] = (byte)(delay & 0xff);
            tempArray[786] = (byte)(delay >> 8 & 0xff); //写入2字节的帧delay
                                                        // 787为透明色索引  788为块结尾0x00
            tempArray[787] = (byte)0xff;
            // 789开始为Image Descriptor块 标志位2C
            // 790~793为帧偏移大小 默认为0
            // 794~797为帧图像大小 默认他
            tempArray[798] = (byte)(tempArray[798] | 0X87); //本地色彩表标志

            //写入到gif文件
            bWriter.Write(tempArray, 781, 18);
            bWriter.Write(tempArray, 13, 768);
            bWriter.Write(tempArray, 799, (int)mStream.Length - 800);
        }
Example #3
0
    public static void Quantize(ref PilotSettings serverVariables)
    {
        Object settingsObject      = serverVariables;
        Object speedSettingsObject = serverVariables.speeds;

        var varibles = typeof(PilotSettings).GetFields();

        for (int i = 0; i < varibles.Length; i++)
        {
            if (varibles[i].FieldType == typeof(float))
            {
                var field = typeof(PilotSettings).GetField(varibles[i].Name);
                field.SetValue(settingsObject, Quantizer.Quantize((float)field.GetValue(settingsObject), 100));
            }
        }
        var speedVaribles = typeof(PilotSettings.Speeds).GetFields();

        for (int i = 0; i < speedVaribles.Length; i++)
        {
            if (speedVaribles[i].FieldType == typeof(float))
            {
                var field = typeof(PilotSettings.Speeds).GetField(speedVaribles[i].Name);
                field.SetValue(speedSettingsObject, Quantizer.Quantize((float)field.GetValue(speedSettingsObject), 100));
            }
        }

        serverVariables        = (PilotSettings)settingsObject;
        serverVariables.speeds = (PilotSettings.Speeds)speedSettingsObject;
    }
Example #4
0
        /// <summary>
        /// Writes to the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="image">The image.</param>
        /// <returns>True if it writes successfully, false otherwise.</returns>
        public override bool Write(BinaryWriter stream, Image image)
        {
            var Quantized = Quantizer.Quantize(image, Quality);

            LoadImage(image, Quantized);
            WriteToFile(stream);
            return(true);
        }
Example #5
0
        /// <summary>
        /// Writes to the specified stream.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="animation">The animation.</param>
        /// <returns>True if it writes successfully, false otherwise.</returns>
        public override bool Write(BinaryWriter writer, Animation animation)
        {
            var Quantized = Quantizer.Quantize(animation[0], Quality);

            LoadAnimation(animation, Quantized);
            WriteToFile(writer);
            return(true);
        }
Example #6
0
        public void TestQuantize()
        {
            Quantizer q = new Quantizer(0.0, 0.1, 0.3);

            Assert.AreEqual(0, q.Quantize(-0.1));
            Assert.AreEqual(1, q.Quantize(0.0));
            Assert.AreEqual(1, q.Quantize(0.03));
            Assert.AreEqual(2, q.Quantize(0.1));
            Assert.AreEqual(2, q.Quantize(0.13));
            Assert.AreEqual(3, q.Quantize(0.3));
            Assert.AreEqual(3, q.Quantize(0.33));
            Assert.AreEqual(3, q.Quantize(1000.0));
        }
Example #7
0
        public void GenerateImagesForDoc()
        {
            //var testData0500 = TestDataFactory.Data.Single(x => x.ImageKey == "0500");
            //var testData0413 = TestDataFactory.Data.Single(x => x.ImageKey == "0413");

            var image     = new Mat(@"C:\Users\Winkler\Desktop\orig.png", LoadImageType.AnyColor);
            var keypoints = new List <Point> {
                new Point(140, 116), new Point(477, 120), new Point(163, 370), new Point(447, 369)
            };
            var testData0500 = new { Keypoints = keypoints, Image = image };

            var configMock = TestHelper.GetFakeConfig();
            //var quantizer = new Quantizer(configMock.Object);

            // simple threshold
            IQuantizer quantizer = new SimpleThresholdQuantizer {
                Threshold = 220
            };

            quantizer.Keypoints = testData0500.Keypoints;
            var quantizedImage = quantizer.Quantize(testData0500.Image);

            //TestHelper.Show(quantizedImage);
            TestHelper.Save(quantizedImage, "threshold_simple.png");

            // adaptive threshold
            quantizer = new Quantizer(configMock.Object)
            {
                ThresholdBlockSize = 17, ThresholdConstant = 6
            };
            quantizer.Keypoints = testData0500.Keypoints;
            quantizedImage      = quantizer.Quantize(testData0500.Image);
            //TestHelper.Show(quantizedImage);
            TestHelper.Save(quantizedImage, "threshold_adaptive.png");

            // warp
            quantizer           = new WarpOnlyQuantizer();
            quantizer.Keypoints = testData0500.Keypoints;
            quantizedImage      = quantizer.Quantize(testData0500.Image);
            //TestHelper.Show(quantizedImage);
            TestHelper.Save(quantizedImage, "warp_result.png");

            // morphological
            quantizer = new MorphologyQuantizer(configMock.Object)
            {
                ThresholdBlockSize = 17, ThresholdConstant = 6
            };
            quantizer.Keypoints = testData0500.Keypoints;
            quantizedImage      = quantizer.Quantize(testData0500.Image);
            //TestHelper.Show(quantizedImage);
            TestHelper.Save(quantizedImage, "opening_result.png");
        }
Example #8
0
        public static (double[] scale, double bias) QuantizeWeights(bool isConv2d, Tensor <float> weights, K210ConvLayerConfig config, int weightsBits)
        {
#if CHANNEL_WISE
            var kernels     = weights.ToDenseTensor().Buffer.Span;
            var channels    = weights.Dimensions[isConv2d ? 0 : 1];
            var channelSize = weights.Dimensions.GetSize() / channels;

            var totalRange = Quantizer.GetRange(kernels);
            var scales     = new double[channels];

            for (int i = 0; i < channels; i++)
            {
                double s;
                var    buffer = kernels.Slice(i * channelSize, channelSize);
                var    range  = Quantizer.GetRange(buffer);

                var s1 = totalRange.Max / range.Max;
                var s2 = totalRange.Min / range.Min;
                s = (s1 < 0 || s2 < 0) ? Math.Max(s1, s2) : Math.Min(s1, s2);

                Debug.Assert(s > 0);
                for (int j = 0; j < buffer.Length; j++)
                {
                    buffer[j] = (float)(buffer[j] * s);
                }
                scales[i] = s;
            }

            (var scale, var bias) = Quantizer.GetRange(kernels).GetScaleBias(weightsBits);

            (var mul, var shift) = Quantizer.ExtractValueAndShift(bias, 24, 15);
            config.Weights       = Quantizer.Quantize(kernels, scale, bias, weightsBits);
            config.ArgX          = (int)Math.Round(mul);
            config.ShiftX        = shift;

            for (int i = 0; i < scales.Length; i++)
            {
                scales[i] *= scale;
            }
            return(scales, bias);
#else
            var buffer = weights.ToDenseTensor().Buffer.Span;
            (var scale, var bias) = GetRange(buffer).GetScaleBias();

            (var mul, var shift) = ExtractValueAndShift(bias, 24, 15);
            config.Weights       = Quantize(buffer, scale, bias);
            config.ArgX          = (int)Math.Round(mul);
            config.ShiftX        = shift;
            return(Enumerable.Repeat(scale, weights.Dimensions[0]).ToArray(), bias);
#endif
        }
Example #9
0
        /// <summary>
        /// Loads the animation.
        /// </summary>
        /// <param name="animation">The animation.</param>
        /// <param name="quantizedImage">The quantized image.</param>
        private void LoadAnimation(Animation animation, QuantizedImage quantizedImage)
        {
            var TempImage         = animation[0];
            var TransparencyIndex = quantizedImage.TransparentIndex;

            Header           = new FileHeader();
            ScreenDescriptor = new LogicalScreenDescriptor(TempImage, TransparencyIndex, BitDepth);
            Frames.Add(new Frame(TempImage, quantizedImage, BitDepth, animation.Delay));
            if (animation.Count > 1)
            {
                AppExtension = new ApplicationExtension(animation.RepeatCount, animation.Count);
                for (int x = 1; x < animation.Count; ++x)
                {
                    quantizedImage = Quantizer.Quantize(animation[x], Quality);
                    TempImage      = animation[x];
                    Frames.Add(new Frame(TempImage, quantizedImage, BitDepth, animation.Delay));
                }
            }
        }
Example #10
0
        public static Image Quantize(Image image, Quality quality = Quality.Bpp8)
        {
            if (quality == Quality.Inherit)
            {
                return(image);
            }

            Quantizer quantizer = quality switch
            {
                Quality.Grayscale => new GrayscaleQuantizer(),
                Quality.Bpp1 => new OctreeQuantizer(1, 1),
                Quality.Bpp2 => new OctreeQuantizer(3, 2),
                Quality.Bpp4 => new OctreeQuantizer(15, 4),
                Quality.Bpp8 => new OctreeQuantizer(255, 8),
                _ => new OctreeQuantizer(255, 8)
            };

            return(quantizer.Quantize(image));
        }
    }
Example #11
0
        public static void CreateGifStream(Image image, Stream stream, Quality quality = Quality.Bpp8)
        {
            if (quality == Quality.Inherit)
            {
                image.Save(stream, ImageFormat.Gif);
            }
            else
            {
                Quantizer quantizer = quality switch
                {
                    Quality.Grayscale => new GrayscaleQuantizer(),
                    Quality.Bpp1 => new OctreeQuantizer(1, 1),
                    Quality.Bpp2 => new OctreeQuantizer(3, 2),
                    Quality.Bpp4 => new OctreeQuantizer(15, 4),
                    Quality.Bpp8 => new OctreeQuantizer(255, 8),
                    _ => new OctreeQuantizer(255, 8)
                };

                using (Bitmap result = quantizer.Quantize(image))
                    result.Save(stream, ImageFormat.Gif);
            }
        }
Example #12
0
		private CompressionStats Compress( IGrid grid,
		                                   ICompressor compressor,
										   double[] errors,
										   string outName,
		                                   ProgressViewModel progressBar )
		{
			double[] leftBorders = new double[grid.ColumnCount];
			double[] rightBorders = new double[grid.ColumnCount];

			var qs = new IQuantization[grid.ColumnCount];
			var distrs = new IDistribution[grid.ColumnCount];

			progressBar.Status = "Quantizing columns...";

			Parallel.For( 0, grid.ColumnCount, column =>
			{
				var distr = new EmpiricalDistribution( grid, column );

				leftBorders[column] = double.MaxValue;
				rightBorders[column] = double.MinValue;

				for ( int row = 0; row < grid.RowCount; ++row )
				{
					double value = grid.GetValue( row, column );
					leftBorders[column] = leftBorders[column] < value ? leftBorders[column] : value;
					rightBorders[column] = rightBorders[column] > value ? rightBorders[column] : value;
				}

				var quantizer = new Quantizer( leftBorders[column], rightBorders[column] );
				var quantization = quantizer.Quantize( errors[column], distr );

				lock ( _lockGuard )
				{
					progressBar.Progress += 1.0 / ( grid.ColumnCount + 1 );
					distrs[column] = distr;
					qs[column] = quantization;
				}
			} );

			var quantizations = new List<IQuantization>( qs );
			var distributions = new List<IDistribution>( distrs );

			progressBar.Status = "Writing archive...";
			progressBar.Progress = ( double )grid.ColumnCount / ( grid.ColumnCount + 1 );

			ICompressionResult result;

			using ( var stream = new FileOutputStream( outName ) )
			{
				result = compressor.Compress( grid, quantizations, stream );
			}

			progressBar.Progress = 1.0;
			progressBar.TryClose( );

			return new CompressionStats
			{
				CompressionResult = result,
				Distributions = distributions,
				LeftBorders = leftBorders,
				RightBorders = rightBorders,
				Quantizations = quantizations
			};
		}
Example #13
0
        private static void QuantizeActivation(K210Conv2d layer, double postMul, QuantizationRange range, QuantizationRange beforeActRange, K210ConvLayerConfig config)
        {
            if (layer.NonTrivialActivation == null)
            {
                switch (layer.FusedActivationFunction)
                {
                case ActivationFunctionType.Linear:
                case ActivationFunctionType.Relu:
                case ActivationFunctionType.Relu6:
                    break;

                default:
                    throw new NotSupportedException($"Activation of {layer.FusedActivationFunction} is not supported.");
                }

                var starts = new ulong[]
                {
                    0x800000000, 0xf7d4cf4b8, 0xf8ed5a20c, 0xfa05e4f60,
                    0xfb2e05baa, 0xfc46908fe, 0xfd5f1b652, 0xfe77a63a6,
                    0xff9fc6ff0, 0xfffd4a9b7, 0, 0x7FFFFFFF0,
                    0x7FFFFFFF1, 0x7FFFFFFF2, 0x7FFFFFFF3, 0x7FFFFFFF4
                };

                for (int i = 0; i < starts.Length; i++)
                {
                    var param = config.ActConfigs[i] = new K210LayerActConfig();
                    param.StartX = starts[i];

                    if (i == 10)
                    {
                        (var mul, var shift) = Quantizer.ExtractValueAndShift(1 / postMul, 16, 20);
                        param.Mul            = (int)Math.Round(mul);
                        param.Shift          = shift;
                    }
                }
            }
            else if (layer.NonTrivialActivation is LeakyRelu leakyRelu)
            {
                (var scale, var bias) = range.GetScaleBias(8);
                var zero   = (long)(Quantizer.Quantize(0, scale, bias) * postMul);
                var yTable = Generator.IntegerStep(0, (int)-bias, 15).Take(14).ToArray();

                for (int i = 0; i < 16; i++)
                {
                    var param = config.ActConfigs[i] = new K210LayerActConfig();
                    if (i == 0)
                    {
                        param.StartX = 0x800000000;
                    }
                    else if (i == 15)
                    {
                        (var mul, var shift) = Quantizer.ExtractValueAndShift(1 / postMul, 16, 20);
                        param.StartX         = (ulong)zero;
                        param.Mul            = (int)Math.Round(mul);
                        param.Shift          = shift;
                        param.Add            = (byte)(-bias);
                    }
                    else
                    {
                        // f(x) = (1 - slope) * zero + x * slope
                        // f(x1) - f(x0) = (x1 - x0) * slope
                        // x0 = zero - (zero - y0) / slope
                        var add = (byte)yTable[i - 1];
                        var y0  = add * postMul;
                        var x0  = zero - (zero - y0) / leakyRelu.Slope;

                        (var mul, var shift) = Quantizer.ExtractValueAndShift(1 / postMul * leakyRelu.Slope, 16, 20);
                        param.StartX         = (ulong)(long)Math.Floor(x0);
                        param.Mul            = (int)Math.Round(mul);
                        param.Shift          = shift;
                        param.Add            = add;
                    }
                }
            }
            else
            {
                throw new NotSupportedException($"Activation of {layer.NonTrivialActivation.GetType().Name} is not supported.");
            }
        }
Example #14
0
        public int Classify(IntegralImage image, int offset)
        {
            double value = m_filter.Apply(image, offset);

            return(m_quantizer.Quantize(value));
        }
Example #15
0
		public async void InitPlot( )
		{
			var reader = new CsvGridReader( 1024, ';' );

			OpenFileDialog openFileDialog = new OpenFileDialog
			{
				Filter = "Text files|*.csv",
				ValidateNames = true
			};

			var column = 0;

			var fileName = openFileDialog.ShowDialog( ) == true ? openFileDialog.FileName : null;

			if ( fileName == null )
			{ return; }

			var grid = await Task<IGrid>.Factory.StartNew( ( ) => reader.Read( fileName, false, false ) );

			double left = double.MaxValue, right = double.MinValue;

			for ( int i = 0; i < grid.RowCount; ++i )
			{
				var value = grid.GetValue( i, column );
				left = left < value ? left : value;
				right = right > value ? right : value;
			}

			var quantizer = new Quantizer( left, right );

			var empirical = new EmpiricalDistribution( grid, column );

			var q = await Task<IQuantization>.Factory.StartNew( ( ) => quantizer.Quantize( 15, 1e-3, empirical ) );

			var zero = new LineSeries
			{
				Color = OxyColor.FromRgb( 0, 0, 0 ),
				StrokeThickness = 1
			};
			zero.Points.Add( new DataPoint( left, 0 ) );
			zero.Points.Add( new DataPoint( right, 0 ) );
			plot.Series.Add( zero );

			var func = new FunctionSeries( x => empirical.Density( x ), left, right, 1e-2 );
			plot.Series.Add( func );

			foreach ( var border in q.Borders )
			{
				var line = new LineSeries
				{
					LineStyle = LineStyle.Dash,
					Color = OxyColor.FromRgb( 0, 0, 0 ),
					StrokeThickness = 1
				};
				line.Points.Add( new DataPoint( border, 3e-1 ) );
				line.Points.Add( new DataPoint( border, -3e-2 ) );
				plot.Series.Add( line );
			}

			foreach ( var code in q.Codes )
			{
				var line = new LineSeries
				{
					LineStyle = LineStyle.Dash,
					Color = OxyColor.FromRgb( 140, 140, 140 ),
					StrokeThickness = 0.5
				};
				line.Points.Add( new DataPoint( code, 3e-1 ) );
				line.Points.Add( new DataPoint( code, -3e-2 ) );
				plot.Series.Add( line );
			}

			var codes = from code in q.Codes
			            select new ScatterPoint( code, empirical.Density( code ) );

			var points = new ScatterSeries
			{
				MarkerType = MarkerType.Circle,
				MarkerStroke = OxyColor.FromRgb( 2, 133, 230 )/*( 255, 0, 0 )*/,
				MarkerFill = OxyColor.FromRgb( 2, 133, 230 )/*( 255, 115, 41 )*/
			};
			points.Points.AddRange( codes );

			plot.Series.Add( points );

			PlotView.Model = plot;
		}