Exemple #1
0
 public static void Set(ushort[] data, int width)
 {
     if (_fetch == null)
     {
         _fetch = new DisplayWindow(width, data.Length / width)
         {
             ClientSize = new System.Drawing.Size(width, data.Length / width)
         };
         new Thread(() => _fetch.ShowDialog())
         {
             IsBackground = true
         }.Start();
     }
     int[] dataConvert;
     if (AutoStretchAmount > 0)
     {
         var dataSort = new ushort[data.Length];
         Array.Copy(data, dataSort, data.Length);
         Array.Sort(dataSort);
         dataConvert = Array.ConvertAll(data, value =>
         {
             var index = (double)Array.BinarySearch(dataSort, value) / dataSort.Length;
             var val   = (double)value / dataSort[dataSort.Length - 1];
             val       = val * (1 - AutoStretchAmount) + index * AutoStretchAmount;
             var final = (byte)(Math.Max(Math.Min(val, 1), 0) * byte.MaxValue);
             return(final << 16 | final << 8 | final);
         });
     }
     else
     {
         dataConvert = Array.ConvertAll(data, u =>
         {
             var value = (byte)(u * byte.MaxValue / ushort.MaxValue);
             return(value << 16 | value << 8 | value);
         });
     }
     lock (_fetch._lock)
     {
         var locked = _fetch._bitmap.LockBits(new Rectangle(0, 0, _fetch._bitmap.Width, _fetch._bitmap.Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
         Marshal.Copy(dataConvert, 0, locked.Scan0, data.Length);
         _fetch._bitmap.UnlockBits(locked);
         _fetch.BeginInvoke((Action)_fetch.Invalidate);
     }
 }
Exemple #2
0
 public static void Set(ushort[] data, int width)
 {
     if (_fetch == null)
     {
         _fetch = new DisplayWindow(width, data.Length / width)
         {
             ClientSize = new System.Drawing.Size(width, data.Length / width)
         };
         new Thread(() => _fetch.ShowDialog()) { IsBackground = true }.Start();
     }
     int[] dataConvert;
     if (AutoStretchAmount > 0)
     {
         var dataSort = new ushort[data.Length];
         Array.Copy(data, dataSort, data.Length);
         Array.Sort(dataSort);
         dataConvert = Array.ConvertAll(data, value =>
         {
             var index = (double)Array.BinarySearch(dataSort, value) / dataSort.Length;
             var val = (double)value / dataSort[dataSort.Length - 1];
             val = val * (1 - AutoStretchAmount) + index * AutoStretchAmount;
             var final = (byte)(Math.Max(Math.Min(val, 1), 0) * byte.MaxValue);
             return final << 16 | final << 8 | final;
         });
     }
     else
     {
         dataConvert = Array.ConvertAll(data, u =>
         {
             var value = (byte)(u * byte.MaxValue / ushort.MaxValue);
             return value << 16 | value << 8 | value;
         });
     }
     lock (_fetch._lock)
     {
         var locked = _fetch._bitmap.LockBits(new Rectangle(0, 0, _fetch._bitmap.Width, _fetch._bitmap.Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
         Marshal.Copy(dataConvert, 0, locked.Scan0, data.Length);
         _fetch._bitmap.UnlockBits(locked);
         _fetch.BeginInvoke((Action)_fetch.Invalidate);
     }
 }
Exemple #3
0
        static void Main()
        {
            new Thread(() =>
            {
                var saving = false;
                while (true)
                {
                    if (_camera != null)
                    {
                        if (_save != 0 && saving == false)
                        {
                            _camera.Exposure = _saveExposure;
                        }
                        else if (_save == 0 && saving)
                        {
                            _camera.Exposure = _liveExposure;
                        }
                        saving = _save != 0;
                        try
                        {
                            _camera.Snap();
                        }
                        catch (Exception e)
                        {
                            _camera.Dispose();
                            _camera = null;
                            Console.WriteLine("Disconnected from camera");
                            Console.WriteLine(e.GetType().Name + ": " + e.Message);
                            continue;
                        }
                        if (saving)
                        {
                            _save--;
                            _save = Math.Max(_save, 0);
                            ImageSaver.Save(_savedir, _camera.Data, _camera.Width);
                            Console.WriteLine("Saved image, {0} to go", _save);
                        }
                        DisplayWindow.Set(_camera.Data, _camera.Width);
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
            })
            {
                IsBackground = true
            }.Start();
            while (true)
            {
                Console.WriteLine("Commands: connect, cross, zoom, gain, stretch, exposure, save, saveexp, savedir");
                Console.Write("> ");
                var readLine = Console.ReadLine();
                if (readLine == null)
                {
                    break;
                }
                var line = readLine.Split(null);
                switch (line[0])
                {
                case "connect":
                    Console.WriteLine("Connecting");
                    var newCamera = LumeneraCamera.Create(1);
                    if (newCamera == null)
                    {
                        Console.WriteLine("Couldn't connect to camera number 1");
                    }
                    else
                    {
                        _camera = newCamera;
                    }
                    break;

                case "cross":
                    DisplayWindow.Cross = !DisplayWindow.Cross;
                    break;

                case "zoom":
                    int newZoom;
                    if (line.Length == 2 && int.TryParse(line[1], out newZoom) && newZoom > 0)
                    {
                        DisplayWindow.Zoom = newZoom;
                    }
                    else if (DisplayWindow.Zoom == 0)
                    {
                        DisplayWindow.Zoom = 80;
                    }
                    else
                    {
                        DisplayWindow.Zoom = 0;
                    }
                    break;

                case "gain":
                    float resultGain;
                    if (_camera != null && line.Length == 2 && float.TryParse(line[1], out resultGain))
                    {
                        _camera.Gain = resultGain;
                    }
                    else
                    {
                        Console.WriteLine("Bad gain command, syntax: gain [number]");
                    }
                    break;

                case "stretch":
                    double newStretch;
                    if (line.Length == 2 && double.TryParse(line[1], out newStretch) && newStretch > 0)
                    {
                        DisplayWindow.AutoStretchAmount = newStretch;
                    }
                    else if (DisplayWindow.AutoStretchAmount <= 0)
                    {
                        DisplayWindow.AutoStretchAmount = 0.5;
                    }
                    else
                    {
                        DisplayWindow.AutoStretchAmount = 0;
                    }
                    break;

                case "exposure":
                    float resultExposure;
                    if (_camera != null && line.Length == 2 && float.TryParse(line[1], out resultExposure))
                    {
                        _liveExposure = resultExposure;
                        if (_save == 0)
                        {
                            _camera.Exposure = _liveExposure;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Bad exposure command, syntax: exposure [seconds]");
                    }
                    break;

                case "save":
                    int resultSave;
                    if (_camera != null && line.Length == 2 && int.TryParse(line[1], out resultSave) &&
                        resultSave > 0)
                    {
                        _save = resultSave;
                    }
                    else
                    {
                        Console.WriteLine("Bad save command, syntax: save [number]");
                    }
                    break;

                case "saveexp":
                    float saveExp;
                    if (_camera != null && line.Length == 2 && float.TryParse(line[1], out saveExp))
                    {
                        _saveExposure = saveExp;
                    }
                    else
                    {
                        Console.WriteLine("Bad saveexp command, syntax: saveexp [exposure in seconds]");
                    }
                    break;

                case "savedir":
                    if (line.Length == 1)
                    {
                        _savedir = null;
                    }
                    else if (line.Length == 2)
                    {
                        _savedir = line[1];
                    }
                    else
                    {
                        Console.WriteLine("Bad savedir command");
                    }
                    break;

                default:
                    Console.WriteLine("Unknown command " + line[0]);
                    break;
                }
            }
        }