예제 #1
0
        private void SetColors(Color[] colors, string id)
        {
            if (_d0 == null || _d1 == null)
            {
                return;
            }

            var data   = id == "0" ? _d0 : _d1;
            var toSend = ColorUtil.TruncateColors(colors, data.Offset, data.LedCount, data.LedMultiplier);

            if (data.ReverseStrip)
            {
                toSend = toSend.Reverse().ToArray();
            }

            if (data.StripType == 1)
            {
                for (var i = 0; i < toSend.Length; i++)
                {
                    var tCol = toSend[i];
                    tCol      = ColorUtil.ClampAlpha(tCol);
                    toSend[i] = tCol;
                }
            }

            if (id == "0")
            {
                _colors1 = toSend;
            }
            else
            {
                _colors2 = toSend;
            }
        }
예제 #2
0
        public void Update(Mat inputMat)
        {
            _input = inputMat ?? throw new ArgumentException("Invalid input material.");
            // Don't do anything if there's no frame.
            //LogUtil.Write("Updating splitter...");
            if (_input.IsEmpty)
            {
                LogUtil.Write("SPLITTER: NO INPUT.");
                return;
            }
            var outColorsStrip    = new List <Color>();
            var outColorsSector   = new List <Color>();
            var outColorsSectorV2 = new List <Color>();
            var outColorsWled     = new Dictionary <string, List <Color> >();
            var coords            = _fullCoords;
            var sectors           = _fullSectors;
            var sectorsV2         = _fullSectorsV2;
            var wlSectors         = WLSectors;

            if (_frameCount >= 10)
            {
                CheckSectors();
                _frameCount = 0;
            }
            _frameCount++;

            // Only calculate new sectors if the value has changed
            if ((_vCrop || _hCrop) && _sectorChanged)
            {
                _sectorChanged = false;
                var vp = _vCrop ? _vCropPixels : 0;
                var hp = _hCrop ? _hCropPixels : 0;
                coords    = DrawGrid(hp, vp);
                sectors   = DrawSectors(hp, vp);
                sectorsV2 = DrawSectors(hp, vp, true);
                foreach (var id in WLModules.Keys)
                {
                    wlSectors[id] = DrawWledGrid(id, hp, vp);
                }
            }

            // Save a preview image if desired
            if (DoSave)
            {
                DoSave = false;
                var path = Directory.GetCurrentDirectory();
                var gMat = new Mat();
                inputMat.CopyTo(gMat);
                var colInt       = 0;
                var textColor    = new Bgr(Color.White).MCvScalar;
                var previewCheck = 3;
                var sectorTarget = coords;
                var colorTarget  = _colorsLed;
                if (previewCheck == 2)
                {
                    sectorTarget = sectors;
                    colorTarget  = _colorsSectors;
                }
                else if (previewCheck == 3)
                {
                    sectorTarget = sectorsV2;
                    colorTarget  = _colorsSectorsV2;
                }
                foreach (var s in sectorTarget)
                {
                    var scCol = colorTarget[colInt];
                    var stCol = ColorUtil.ClampAlpha(scCol);
                    var col   = new Bgr(stCol).MCvScalar;
                    CvInvoke.Rectangle(gMat, s, col, -1, LineType.AntiAlias);
                    if (previewCheck != 1)
                    {
                        var cInt   = colInt + 1;
                        var tPoint = new Point(s.X, s.Y + 30);
                        CvInvoke.PutText(gMat, cInt.ToString(), tPoint, FontFace.HersheySimplex, 1.0, textColor);
                    }

                    colInt++;
                }
                gMat.Save(path + "/wwwroot/img/_preview_output.jpg");
                gMat.Dispose();
            }

            foreach (var sub in coords.Select(r => new Mat(_input, r)))
            {
                outColorsStrip.Add(GetAverage(sub));
                sub.Dispose();
            }

            foreach (var sub in sectors.Select(r => new Mat(_input, r)))
            {
                outColorsSector.Add(GetAverage(sub));
                sub.Dispose();
            }

            foreach (var sub in sectorsV2.Select(r => new Mat(_input, r)))
            {
                outColorsSectorV2.Add(GetAverage(sub));
                sub.Dispose();
            }

            foreach (var wlId in wlSectors.Keys)
            {
                var colorList = new List <Color>();
                foreach (var r in wlSectors[wlId])
                {
                    if (0 <= r.X &&
                        0 <= r.Width &&
                        r.X + r.Width <= _input.Cols &&
                        0 <= r.Y &&
                        0 <= _input.Height &&
                        r.Y + r.Height <= _input.Rows)
                    {
                        using var sub = new Mat(_input, r);
                        // box within the image plane
                        colorList.Add(GetAverage(sub));
                    }
                }

                outColorsWled[wlId] = colorList;
            }
            _colorsLed       = outColorsStrip;
            _colorsSectors   = outColorsSector;
            _colorsSectorsV2 = outColorsSectorV2;
            _colorsWled      = outColorsWled;
            //LogUtil.Write("Splitter updated...");
        }