4using System.Collections.Generic;
5using System.Diagnostics;
8using System.Threading.Tasks;
9using RectangleD = AForge.RectangleD;
12using System.Runtime.InteropServices;
13using org.micromanager.@
internal;
18 public static class MicroManager
20 public static mmcorej.CMMCore core;
21 public static MMStudio studio;
22 public static PointD location;
23 public static double focus;
24 public static Dictionary<string, List<Conf>> Config =
new Dictionary<string, List<Conf>>();
27 public string Class {
get;
set; }
28 public string Type {
get;
set; }
29 public string[] Values {
get;
set; }
30 public Conf(
string Class,
string Type,
string[] Values)
36 public override string ToString()
38 string s = Class +
"," + Type +
",";
39 foreach (
string item
in Values)
46 public static Conf[] GetConfigs(
string Class,
string Type)
48 List<Conf> configs =
new List<Conf>();
49 foreach (var conf
in Config[Class])
51 if(conf.Type == Type) configs.Add(conf);
53 return configs.ToArray();
55 public static bool initialized =
false;
56 public static string TurretName =
"";
57 public static bool Initialize(
string config)
61 Directory.SetCurrentDirectory(
"C:/Program Files/Micro-Manager-2.0/");
62 java.lang.System.setProperty(
"force.annotation.index",
"true");
64 java.lang.System.setProperty(
"org.micromanager.corej.path",
"C:/Program Files/Micro-Manager-2.0");
65 java.lang.System.setProperty(
"user.dir",
"C:/Program Files/Micro-Manager-2.0/");
66 MMStudio.main(
new string[] { });
67 studio = MMStudio.getInstance();
68 core = (CMMCore)studio.core();
69 Objectives.Initialize();
70 Directory.SetCurrentDirectory(Path.GetDirectoryName(Environment.ProcessPath));
76 Console.WriteLine(
"Error during Micro-Manager initialization: " + e.Message);
81 public static BioImage TakeImage(
string name)
83 int width, height, depth, bits, channels = 0;
85 TakeImage(name,out width,out height,out depth,out bits,out channels, out bm);
86 BioImage im =
new BioImage(name);
87 foreach (AForge.Bitmap item in bm)
89 item.Stats = Statistics.FromBytes(item);
91 im.Buffers.AddRange(bm);
93 RectangleD rec = Microscope.GetViewRectangle(
false);
94 Point3D p = Microscope.GetPosition();
95 Resolution res =
new Resolution(width, height, im.Buffers[0].PixelFormat, rec.W / im.Buffers[0].SizeX, rec.H / im.Buffers[0].SizeY, 1, p.X, p.Y, p.Z);
96 im.Volume.Location = p;
97 im.Volume.Width = rec.W;
98 im.Volume.Height = rec.H;
99 im.bitsPerPixel = im.Buffers[0].BitsPerPixel;
100 im.UpdateCoords(1, 1, 1);
101 im.Resolutions.Add(res);
102 for (
int c = 0; c < bm.Length; c++)
104 im.Channels.Add(
new Channel(c, bm[c].BitsPerPixel, bm[c].RGBChannelsCount));
106 BioImage.AutoThreshold(im,
false);
109 private static bool TakeImage(
string file, out
int width, out
int height, out
int depth, out
int bits, out
int channels, out AForge.Bitmap[] bm)
111 string fil = Path.GetFileNameWithoutExtension(file);
112 width = (int)core.getImageWidth();
113 height = (int)core.getImageHeight();
114 depth = (int)core.getImageBitDepth();
115 bits = (int)core.getBytesPerPixel();
116 channels = (int)core.getNumberOfCameraChannels();
117 PixelFormat format = PixelFormat.Format8bppIndexed;
118 if (depth > 8 && bits == 2)
119 format = PixelFormat.Format16bppGrayScale;
120 else if(depth > 8 && bits == 6)
121 format = PixelFormat.Format48bppRgb;
122 else if (depth == 8 && bits == 3)
123 format = PixelFormat.Format24bppRgb;
124 else if (depth == 8 && bits == 4)
125 format = PixelFormat.Format32bppArgb;
126 bm =
new AForge.Bitmap[channels];
128 for (
int i = 0; i < channels; i++)
132 byte[] bytes = (
byte[])core.getImage(i);
133 bm[i] =
new AForge.Bitmap(width, height, format, bytes ,
new ZCT(),
"");
137 ushort[] buf = (ushort[])core.getImage(i);
138 byte[] bts =
new byte[buf.Length * 2];
139 for (
int b = 0; b < buf.Length; b++)
141 byte[] pl = BitConverter.GetBytes(buf[b]);
145 bm[i] =
new AForge.Bitmap(width, height, format, bts,
new ZCT(),
"");
150 public static void SetExposure(
float f)
154 public static void SetPosition(Point3D p)
156 core.setXYPosition(p.X, p.Y);
157 core.setPosition(p.Z);
159 public static void SetPosition(PointD p)
161 core.setXYPosition(p.X, p.Y);
163 public static void GetPosition3D(out Point3D p,
bool update)
165 var pos = core.getXYStagePosition();
166 var f = core.getPosition();
167 p =
new Point3D(pos.x, pos.y, f);
169 public static void GetPosition(out PointD p,
bool update)
171 var pos = core.getXYStagePosition();
172 p =
new PointD(pos.x, pos.y);
174 public static class Objectives
179 public static int InstalledCount
183 return GetConfigs(
"ConfigGroup",
"Objective").Length;
186 public static List<Objective> List =
new List<Objective>();
189 public string Name {
get;
set; }
190 public int Index {
get;
set; }
191 public int Magnification {
get;
set; }
192 public Objective(
string name,
int index,
int mag)
199 internal static void Initialize()
201 foreach (
Conf obj
in GetConfigs(
"ConfigGroup",
"Objective"))
203 List.Add(
new Objective(obj.Values[0],
int.Parse(obj.Values[3]),
int.Parse(obj.Values[0].Replace(
"X",
""))));
207 public static Conf GetObjective(
int index)
209 Conf[] cfs = GetConfigs(
"ConfigGroup",
"Objective");
212 public static void SetPosition(
int i)
214 core.setConfig(TurretName, List[i].Name);
216 public static int GetPosition()
218 string s = core.getCurrentConfig(TurretName);
220 foreach(Objective obj
in Objectives.List)
228 public static class Shutters
232 public string Name {
get;
set; }
238 public static List<Shutter> List =
new List<Shutter>();
239 public static void Initialize()
241 if (Config.ContainsKey(
"Device"))
244 foreach (
Conf item
in Config[
"Device"])
246 if (item.Values.Last().Contains(
"Shutter"))
248 List.Add(
new Shutter(item.Type));
253 public static int GetPosition(
string shutterName)
255 if (core.getShutterOpen())
260 public static void SetPosition(
string shutterName,
int state)
263 core.setShutterOpen(shutterName,
true);
265 core.setShutterOpen(shutterName,
false);