Example #1
0
 public static void Initcalculations(List<Formula> formulasin, ref Information iIN)
 {
     Formulas = formulasin;
     I = iIN;
 }
Example #2
0
        private void ApplyFormula()
        {
            if (Calculations.Threadfincount > 0)
            {
                MessageBox.Show("already performing an operation");
                return;
            }

            if (Formulas.Count == 0)
            {
                MessageBox.Show("no formulas are loaded");
                return;
            }

            //trim formulas
            TrimFormulas();

            //sanity checks
            var error = Calculations.FormulaChecks(Formulas, ImagePanels);
            if (string.IsNullOrEmpty(error) == false)
            {
                MessageBox.Show(error, "Formula Error!");
                return;
            }

            var imlist = new Dictionary<int, CustomImage>();

            int maxw = 0, maxh = 0;
            var az = 0;
            var pf = PixelFormat.DontCare;
            foreach (var ic in ImagePanels)
            {
                var newim = new CustomImage(ic.Value);
                if (newim.Width > maxw)
                    maxw = newim.Width;
                if (newim.Height > maxh)
                    maxh = newim.Height;
                pf = newim.Format;
                imlist.Add(az, newim);
                az++;
            }

            if (maxw == 0 || maxh == 0)
            {
                MessageBox.Show("The current formula requires more images to be loaded");
                return;
            }

            var outputBitmap = new Bitmap(maxw, maxh, pf);
            var outputBitmapData = outputBitmap.LockBits(new Rectangle(0, 0, maxw, maxh), ImageLockMode.ReadOnly, outputBitmap.PixelFormat);

            var bys = Math.Abs(outputBitmapData.Stride) * outputBitmap.Height;
            var outputvalues = new byte[bys];

            //var time = DateTime.Now;
            var pixelsto = new CustomImage
            {
                Array = outputvalues,
                Format = outputBitmapData.PixelFormat,
                Height = outputBitmap.Height,
                Width = outputBitmap.Width,
                Scan0 = outputBitmapData.Scan0,
                Stride = outputBitmapData.Stride
            };

            var i = new Information { imlist = imlist, pixelsTo = pixelsto, maxw = maxw, maxh = maxh };

            Calculations.Initcalculations(Formulas, ref i);

            //get the custom matricies
            if (Calculations.GetCustomMatricies(Formulas) == false)
                return;

            //link the variables to their array
            Calculations.SetImageLocations();

            //get max pass count
            var passcount = GetLargestNumber(Calculations.Passoperation, "", 0);
            //if there is no pass, then we just want 1
            if (passcount == -1)
                passcount = 1;

            //set this form
            Calculations.Baseform = this;

            //create threads
            int threads;
            int.TryParse(threadCB.Text, out threads);
            //make sure there arent more threads than width
            if (threads > maxw)
                threads = maxw;
            var pixeldistance = maxw / threads;

            //for each pass
            progressbar.Value = 0;
            progressbar.Maximum = threads * passcount;

            for (var pass = 1; pass <= passcount; pass++)
            {
                //set the pass number
                Calculations.Thispassnumber = pass;
                var dist = 0;
                for (var a = 0; a < threads; a++)
                {
                    var ti = new ThreadInfo(dist, dist + pixeldistance, 0, maxh);

                    var t = new Thread(Calculations.ApplyMain);
                    dist += pixeldistance;
                    t.Start(ti);
                }

                var events = 0;
                while (Calculations.Threadfincount < threads)
                {
                    Thread.Sleep(100);
                    events++;
                    if (events >= 10)
                    {
                        events = 0;
                        Application.DoEvents();
                    }
                }
                Calculations.Threadfincount = 0;
            }

            Marshal.Copy(outputvalues, 0, outputBitmapData.Scan0, bys);
            outputBitmap.UnlockBits(outputBitmapData);

            Image ni = outputBitmap;

            LoadImageIntoTabPage("", ni);

            progressbar.Value = 0;

            //DateTime time2 = DateTime.Now;
            //var ts = time2 - time;
            //MessageBox.Show("seconds:" + TS.TotalSeconds);
            if (showPopupWhenAlgorithmsCompleteToolStripMenuItem.Checked)
            {
                ShowBalloon();
            }
        }