|
| 1 | +namespace ScreenGrid.Models.AppsInterop |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Drawing; |
| 6 | + using System.Linq; |
| 7 | + |
| 8 | + /// <summary> |
| 9 | + /// Native window wrapper of Octane Render |
| 10 | + /// </summary> |
| 11 | + public class OctaneRenderWindow : NativeWindow |
| 12 | + { |
| 13 | + public OctaneRenderWindow(IntPtr hWnd) |
| 14 | + : base(hWnd) |
| 15 | + { |
| 16 | + |
| 17 | + } |
| 18 | + |
| 19 | + private const string ProcessName = "octane"; |
| 20 | + |
| 21 | + public static OctaneRenderWindow GetFromFirstProcess() |
| 22 | + { |
| 23 | + var proc = System.Diagnostics.Process.GetProcesses().Where(p => String.Compare(p.ProcessName, ProcessName, StringComparison.OrdinalIgnoreCase) == 0).FirstOrDefault(); |
| 24 | + |
| 25 | + if (proc != null) |
| 26 | + { |
| 27 | + return new OctaneRenderWindow(proc.MainWindowHandle); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + return null; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static IList<OctaneRenderWindow> GetFromAllProcesses() |
| 36 | + { |
| 37 | + var processes = System.Diagnostics.Process.GetProcesses().Where(p => String.Compare(p.ProcessName, ProcessName, StringComparison.OrdinalIgnoreCase) == 0).ToList(); |
| 38 | + |
| 39 | + return (processes.Select(w => new OctaneRenderWindow(w.MainWindowHandle)).ToList()); |
| 40 | + } |
| 41 | + |
| 42 | + private static Bitmap UpperLeftCorner |
| 43 | + { |
| 44 | + get |
| 45 | + { |
| 46 | + var imageData = ReadResourceFile("ScreenGrid.Models.Resources.UpperLeftCorner.png"); |
| 47 | + using (var ms = new System.IO.MemoryStream(imageData)) |
| 48 | + { |
| 49 | + return new Bitmap(ms); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static Bitmap LowerRightCorner |
| 55 | + { |
| 56 | + get |
| 57 | + { |
| 58 | + var imageData = ReadResourceFile("ScreenGrid.Models.Resources.LowerRightCorner.png"); |
| 59 | + using (var ms = new System.IO.MemoryStream(imageData)) |
| 60 | + { |
| 61 | + return new Bitmap(ms); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static Point TopLeftCorner(FlatImage image) |
| 67 | + { |
| 68 | + var topLeftCornerFragment = new FlatImage(OctaneRenderWindow.UpperLeftCorner); |
| 69 | + |
| 70 | + var topLeftCornerX = 0; |
| 71 | + var topLeftCornerY = 0; |
| 72 | + for (var x = 0; x < image.Width - topLeftCornerFragment.Width; x++) |
| 73 | + { |
| 74 | + for (var y = 0; y < image.Height - topLeftCornerFragment.Height; y++) |
| 75 | + { |
| 76 | + if (image.CompareWithFragmentWithTolerance(topLeftCornerFragment, x, y)) |
| 77 | + { |
| 78 | + topLeftCornerX = x + topLeftCornerFragment.Width - 1; |
| 79 | + topLeftCornerY = y + topLeftCornerFragment.Height - 1; |
| 80 | + return new Point(topLeftCornerX, topLeftCornerY); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return new Point(0, 0); |
| 86 | + } |
| 87 | + |
| 88 | + private static Point BottomRightCorner(FlatImage image) |
| 89 | + { |
| 90 | + var bottomRightCornerFragment = new FlatImage(OctaneRenderWindow.LowerRightCorner); |
| 91 | + |
| 92 | + var bottomRightCornerX = 0; |
| 93 | + var bottomRightCornerY = 0; |
| 94 | + for (var x = 0; x < image.Width - bottomRightCornerFragment.Width; x++) |
| 95 | + { |
| 96 | + for (var y = 0; y < image.Height - bottomRightCornerFragment.Height; y++) |
| 97 | + { |
| 98 | + if (image.CompareWithFragmentWithTolerance(bottomRightCornerFragment, x, y)) |
| 99 | + { |
| 100 | + bottomRightCornerX = x; |
| 101 | + bottomRightCornerY = y; |
| 102 | + return new Point(bottomRightCornerX, bottomRightCornerY); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return new Point(0, 0); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Search image top left point |
| 112 | + /// </summary> |
| 113 | + /// <param name="image"></param> |
| 114 | + /// <param name="topLeftCorner"></param> |
| 115 | + /// <param name="bottomRightCorner"></param> |
| 116 | + /// <returns></returns> |
| 117 | + private static Point TopLeftImage(FlatImage image, Point topLeftCorner, Point bottomRightCorner) |
| 118 | + { |
| 119 | + var topLeftCornerFragment = new FlatImage(OctaneRenderWindow.UpperLeftCorner); |
| 120 | + |
| 121 | + var topLeftImageX = 0; |
| 122 | + var topLeftImageY = 0; |
| 123 | + |
| 124 | + var canvasColor = image.pixels[(int)topLeftCorner.X, (int)topLeftCorner.Y]; |
| 125 | + |
| 126 | + for (var y = (int)topLeftCorner.Y; y < (int)bottomRightCorner.Y; y++) |
| 127 | + { |
| 128 | + for (var x = (int)topLeftCorner.X; x < (int)bottomRightCorner.X; x++) |
| 129 | + { |
| 130 | + //if (PointUtils.MidDiff(image.pixels[x, y], areaColor1) > 2) |
| 131 | + if (image.pixels[x, y] != canvasColor) |
| 132 | + { |
| 133 | + if (y > (int)topLeftCorner.Y) |
| 134 | + { |
| 135 | + topLeftImageX = x; |
| 136 | + topLeftImageY = y; |
| 137 | + return new Point(topLeftImageX, topLeftImageY); |
| 138 | + } |
| 139 | + |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return new Point(0, 0); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Search image bottom right point |
| 150 | + /// </summary> |
| 151 | + /// <param name="image"></param> |
| 152 | + /// <param name="topLeftCorner"></param> |
| 153 | + /// <param name="bottomRightCorner"></param> |
| 154 | + /// <returns></returns> |
| 155 | + private static Point BottomRightImage(FlatImage image, Point topLeftCorner, Point bottomRightCorner) |
| 156 | + { |
| 157 | + var bottomRightCornerFragment = new FlatImage(OctaneRenderWindow.LowerRightCorner); |
| 158 | + |
| 159 | + var areaColor2 = image.pixels[(int)bottomRightCorner.X, (int)bottomRightCorner.Y]; |
| 160 | + |
| 161 | + for (var y = (int)bottomRightCorner.Y - 1; y > (int)topLeftCorner.Y; y--) |
| 162 | + { |
| 163 | + for (var x = (int)bottomRightCorner.X - 1; x > (int)topLeftCorner.X; x--) |
| 164 | + { |
| 165 | + if (image.pixels[x, y] != areaColor2) |
| 166 | + { |
| 167 | + if (y < bottomRightCorner.Y) |
| 168 | + { |
| 169 | + return new Point(x, y); |
| 170 | + } |
| 171 | + |
| 172 | + break; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return new Point(0, 0); |
| 178 | + } |
| 179 | + |
| 180 | + public static Geometry.Rectangle FindRenderedImageBorders(FlatImage flatImage) |
| 181 | + { |
| 182 | + var topLeftCorner = OctaneRenderWindow.TopLeftCorner(flatImage); |
| 183 | + var bottomRightCorner = OctaneRenderWindow.BottomRightCorner(flatImage); |
| 184 | + var topLeftImage = OctaneRenderWindow.TopLeftImage(flatImage, topLeftCorner, bottomRightCorner); |
| 185 | + var bottomRightImage = OctaneRenderWindow.BottomRightImage(flatImage, topLeftCorner, bottomRightCorner); |
| 186 | + |
| 187 | + // TODO: && topLeftImageFound && bottomRightImageFound) |
| 188 | + if ((topLeftCorner.X > 0) && (topLeftCorner.Y > 0) && (bottomRightCorner.X > 0) && (bottomRightCorner.Y > 0) && (topLeftImage.X > 0)) |
| 189 | + { |
| 190 | + return new Geometry.Rectangle |
| 191 | + { |
| 192 | + X = topLeftImage.X, |
| 193 | + Y = topLeftImage.Y, |
| 194 | + Width = bottomRightImage.X - topLeftImage.X + 1, |
| 195 | + Height = bottomRightImage.Y - topLeftImage.Y + 1, |
| 196 | + }; |
| 197 | + } |
| 198 | + else |
| 199 | + { |
| 200 | + return Geometry.Rectangle.Empty; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + private static byte[] ReadResourceFile(string id) |
| 205 | + { |
| 206 | + var assembly = System.Reflection.Assembly.GetExecutingAssembly(); |
| 207 | + |
| 208 | + byte[] data = null; |
| 209 | + using (var stream = assembly.GetManifestResourceStream(id)) |
| 210 | + { |
| 211 | + data = new byte[stream.Length]; |
| 212 | + stream.Read(data, 0, (int)stream.Length); |
| 213 | + } |
| 214 | + |
| 215 | + return data; |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments