Ejemplo n.º 1
0
        public void UpdatePane()
        {
            if (session == null || bmpPane == null || WindowState == FormWindowState.Minimized)
            {
                return;
            }

            if (session.Spectrums.Count < 1)
            {
                return;
            }

            if (settings.ROIList.Count < 1)
            {
                return;
            }

            try
            {
                Graphics g = Graphics.FromImage(bmpPane);
                g.Clear(SystemColors.ButtonFace);

                Pen penSelect = new Pen(Color.Black);
                penSelect.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

                Pen penMarker = new Pen(Color.FromArgb(255, 120, 120, 120));
                penMarker.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;

                FontFamily fontFamily = new FontFamily("Arial");
                Font       font       = new Font(fontFamily, 10, FontStyle.Regular, GraphicsUnit.Pixel);

                double maxRoiCount = -1d;

                foreach (ROIData rd in settings.ROIList)
                {
                    if (!rd.Active)
                    {
                        continue;
                    }

                    if (rd.StartChannel < 0 || rd.StartChannel >= session.NumChannels || rd.EndChannel < 0 || rd.EndChannel >= session.NumChannels)
                    {
                        log.Warn("ROI entry " + rd.Name + " is outside spectrum");
                        continue;
                    }

                    double mc = session.GetMaxCountInROI((int)rd.StartChannel, (int)rd.EndChannel);
                    if (btnSubtractBackground.Checked)
                    {
                        mc -= session.GetBackgroundCountInROI((int)rd.StartChannel, (int)rd.EndChannel);
                        if (mc < 0d)
                        {
                            mc = 0d;
                        }
                    }

                    if (mc > 0d)
                    {
                        mc = Math.Log(mc);
                    }

                    if (maxRoiCount == -1d)
                    {
                        maxRoiCount = mc;
                    }
                    else if (mc > maxRoiCount)
                    {
                        maxRoiCount = mc;
                    }
                }

                double unit = (bmpPane.Height - 40) / maxRoiCount;

                labelScaling.Text = "Scale factor: " + String.Format("{0:0.0#}", unit);

                foreach (ROIData rd in settings.ROIList)
                {
                    if (!rd.Active)
                    {
                        continue;
                    }

                    Pen pen = new Pen(Color.FromName(rd.ColorName), 1);
                    int x = 0;
                    int last_x = 0, last_y = bmpPane.Height - 40;

                    for (int i = firstSpectrum; i < firstSpectrum + bmpPane.Width && i < session.Spectrums.Count; i++)
                    {
                        Spectrum s        = session.Spectrums[i];
                        double   roiCount = s.GetCountInROI((int)rd.StartChannel, (int)rd.EndChannel);
                        if (btnSubtractBackground.Checked)
                        {
                            roiCount -= session.GetBackgroundCountInROI((int)rd.StartChannel, (int)rd.EndChannel);
                            if (roiCount < 0d)
                            {
                                roiCount = 0d;
                            }
                        }

                        double weightedCount = 0d;
                        if (roiCount > 0d)
                        {
                            weightedCount = Math.Log(roiCount);
                        }

                        weightedCount *= unit;

                        if (weightedCount < 0d)
                        {
                            weightedCount = 0d;
                        }

                        int y = bmpPane.Height - 40 - (int)weightedCount;

                        try
                        {
                            if (x >= 0 && x < bmpPane.Width && y >= 0 && y < bmpPane.Height - 40)
                            {
                                g.DrawLine(pen, last_x, last_y, x, y);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("last_x: " + last_x + " last_y: " + last_y + " x: " + x + " y: " + y);
                        }

                        bmpPane.SetPixel(x, bmpPane.Height - 1, Utils.ToColor(s.SessionIndex));

                        last_x = x;
                        last_y = y;
                        x++;
                    }
                }

                for (int j = 0; j < bmpPane.Width; j++)
                {
                    int idx = Utils.ToArgb(bmpPane.GetPixel(j, bmpPane.Height - 1));

                    if (idx % 100 == 0)
                    {
                        g.DrawLine(penMarker, new Point(j, 0), new Point(j, bmpPane.Height - 30));
                        g.DrawString(idx.ToString(), font, new SolidBrush(Color.FromArgb(255, 125, 125, 125)), j, bmpPane.Height - 20);
                    }

                    if (idx == SelectedSessionIndex1)
                    {
                        g.DrawLine(penSelect, new Point(j, 0), new Point(j, bmpPane.Height - 30));
                    }

                    if (idx == SelectedSessionIndex2 && SelectedSessionIndex1 != SelectedSessionIndex2)
                    {
                        g.DrawLine(penSelect, new Point(j, 0), new Point(j, bmpPane.Height - 30));
                    }
                }

                pane.Refresh();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
            }
        }