aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.UnitTesting/TCC/TCC_TST.cs
blob: ae5c66f6ea8620b85d150ba61d822cfc89e5837a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Google.Protobuf;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.IO;
using Tango.CSV;
using Tango.PMR.TCC;
using Tango.TCC.BL;

namespace Tango.UnitTesting.TCC
{
    [TestClass]
    [TestCategory("TCC")]
    public class TCC_TST
    {
        [TestMethod]
        public void Detect()
        {
            var original_bitmap = Directory.GetCurrentDirectory() + "\\TCC\\TCC Resources\\bitmap.bmp";

            using (ColorDetector detector = new ColorDetector())
            {
                DetectionInput input = new DetectionInput()
                {
                    Number = 5,
                    Columns = 10,
                    Rows = 11,
                    TargetIndex = 99,
                    Bitmap = ByteString.CopyFrom(File.ReadAllBytes(original_bitmap)),
                    RequestColorMatrix = true,
                    RequestDebugImage = true,
                };

                var output = detector.Detect(input);

                if (input.RequestDebugImage)
                {
                    using (MemoryStream ms = new MemoryStream(output.DebugBitmap.ToArray()))
                    {
                        var outputBitmap = TemporaryManager.Default.CreateFile(".bmp");
                        outputBitmap.Persist = true;
                        ms.Position = 0;
                        Bitmap bmp = new Bitmap(ms);
                        bmp.Save(outputBitmap);
                        Process.Start(outputBitmap);
                    }
                }

                if (input.RequestColorMatrix)
                {
                    using (Bitmap colorMatrixBmp = ColorDetector.DetectionOutputToImage(input, output, 300, 320))
                    {
                        var outputBitmap = TemporaryManager.Default.CreateFile(".bmp");
                        outputBitmap.Persist = true;
                        colorMatrixBmp.Save(outputBitmap);
                        Process.Start(outputBitmap);
                    }

                    var benchmarksCsvFile = TemporaryManager.Default.CreateFile(".csv");
                    benchmarksCsvFile.Persist = true;

                    using (CsvFile<DetectionColor> benchmarksRGB = new CsvFile<DetectionColor>(new CsvDestination(benchmarksCsvFile)))
                    {
                        foreach (var color in output.ColorMatrix)
                        {
                            benchmarksRGB.Append(color);
                        }
                    }

                    Helper.ShowInExplorer(benchmarksCsvFile);

                    Assert.IsTrue(output.Number == 15);
                }
            }
        }

        private byte[] GetBitmapData(byte[] bitmap)
        {
            using (MemoryStream ms = new MemoryStream(bitmap))
            {
                ms.Position = 0;
                Bitmap image = new Bitmap(ms);
                Rectangle area = new Rectangle(0, 0, image.Width, image.Height);
                BitmapData bitmapData = image.LockBits(area, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                int stride = bitmapData.Stride;
                IntPtr ptr = bitmapData.Scan0;
                int numBytes = bitmapData.Stride * image.Height;
                byte[] rgbValues = new byte[numBytes];
                Marshal.Copy(ptr, rgbValues, 0, numBytes);
                return rgbValues;
            }
        }
    }
}