4using System.Collections.Generic;
5using System.ComponentModel;
7using System.Diagnostics;
9using System.IO.Compression;
12using System.Threading.Tasks;
13using System.Windows.Forms;
17 public partial class SlideImager : Form
19 private static Stopwatch Stopwatch =
new Stopwatch();
28 pp.X = (float)((p.X - stage.X) / physX);
29 pp.Y = (float)((p.Y - stage.Y) / physY);
34 public double Focus {
get;
set; }
48 public List<Level>
Levels =
new List<Level>();
51 public List<BioImage>
BioImages {
get;
set; } =
new List<BioImage>();
53 public double Focus {
get;
set; }
58 public AForge.Bitmap
Image {
get;
set; } =
null;
59 public double Width {
get;
set; }
64 public List<Channel>
Channels =
new List<Channel>();
65 public Enums.ForeignTiffCompression Compression {
get;
set; }
70 int wi = (int)(w / r.W);
71 int hi = (int)(h / r.H);
72 double xx = 2 * index;
78 if (
Image.BitsPerPixel > 8)
83 public Level(
double w,
double h,
PixelFormat pf,
int levels, Enums.ForeignTiffCompression comp,
double physX,
double physY)
87 for (
int i = 0; i < levels; i++)
99 string met =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
100 "<OME xmlns=\"http://www.openmicroscopy.org/Schemas/OME/2016-06\" " +
101 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
102 "xsi:schemaLocation=\"http://www.openmicroscopy.org/Schemas/OME/2016-06 http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd\">";
104 string endian =
Parent.
Image.LittleEndian.ToString().ToLower();
107 "<Image ID=\"Image:" + ib +
"\">" +
108 "<Pixels BigEndian=\"" + endian +
"\" DimensionOrder= \"XYCZT\" ID= \"Pixels:0\" Interleaved=\"true\" " +
109 "PhysicalSizeX=\"" +
PhysicalX +
"\" PhysicalSizeXUnit=\"µm\" PhysicalSizeY=\"" +
PhysicalY +
"\" PhysicalSizeYUnit=\"µm\" SignificantBits=\"" + Parent.Image.BitsPerPixel +
"\" " +
110 "SizeC = \"" + c +
"\" SizeT = \"" + 1 +
"\" SizeX =\"" + Parent.Image.Width +
111 "\" SizeY= \"" + Parent.Image.Height +
"\" SizeZ=\"" + 1;
112 if (
Parent.
Image.BitsPerPixel > 8) met +=
"\" Type= \"uint16\">";
113 else met +=
"\" Type= \"uint8\">";
117 met +=
"<Channel ID=\"Channel:" + ib +
":" + i +
"\" SamplesPerPixel=\"1\"></Channel>";
120 met +=
"</Pixels></Image></OME>";
127 mutable.Set(GValue.GStrType,
"image-description", met);
128 mutable.Set(GValue.GIntType,
"page-height",
Parent.
Image.Height);
131 mutated.Tiffsave(file, Compression, 1, Enums.ForeignTiffPredictor.None,
true, (
int)512, (
int)512,
true,
false, 16,
132 Enums.ForeignTiffResunit.Cm, 1000 *
PhysicalX, 1000 *
PhysicalY,
true,
null, Enums.RegionShrink.Nearest,
135 mutated.Tiffsave(file, Compression, 1, Enums.ForeignTiffPredictor.None,
true, (
int)512, (
int)512,
true,
false, 8,
136 Enums.ForeignTiffResunit.Cm, 1000 *
PhysicalX, 1000 *
PhysicalY,
true,
null, Enums.RegionShrink.Nearest,
141 Console.WriteLine(e.ToString());
149 string met =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
150 "<OME xmlns=\"http://www.openmicroscopy.org/Schemas/OME/2016-06\" " +
151 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
152 "xsi:schemaLocation=\"http://www.openmicroscopy.org/Schemas/OME/2016-06 http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd\">";
153 int c =
Image.RGBChannelsCount;
154 string endian =
Image.LittleEndian.ToString().ToLower();
159 "<Image ID=\"Image:" + ib +
"\">" +
160 "<Pixels BigEndian=\"" + endian +
"\" DimensionOrder= \"XYCZT\" ID= \"Pixels:0\" Interleaved=\"true\" " +
161 "PhysicalSizeX=\"" +
PhysicalX +
"\" PhysicalSizeXUnit=\"µm\" PhysicalSizeY=\"" +
PhysicalY +
"\" PhysicalSizeYUnit=\"µm\" SignificantBits=\"" + Image.BitsPerPixel +
"\" " +
162 "SizeC = \"" + c +
"\" SizeT = \"" + 1 +
"\" SizeX =\"" + Image.Width +
163 "\" SizeY= \"" + Image.Height +
"\" SizeZ=\"" + 1;
164 if (
Image.BitsPerPixel > 8) met +=
"\" Type= \"uint16\">";
165 else met +=
"\" Type= \"uint8\">";
169 met +=
"<Channel ID=\"Channel:" + ib +
":" + i +
"\" SamplesPerPixel=\"1\"></Channel>";
172 met +=
"</Pixels></Image>";
179 string fs = file.Replace(
".ome.tif",
"");
180 NetVips.Image im = NetVips.Image.NewFromFile(fs +
"-" + li +
"ome.tif");
181 using var mutated = im.Mutate(
mutable =>
184 mutable.Set(GValue.GStrType,
"image-description", met);
187 if (l.
Image.BitsPerPixel > 8)
188 mutated.Tiffsave(file, Compression, 1, Enums.ForeignTiffPredictor.None,
true, (
int)l.
WidthPx, (
int)l.
HeightPx,
true,
false, 16,
189 Enums.ForeignTiffResunit.Cm, 1000 * l.
PhysicalX, 1000 * l.
PhysicalY,
true,
null, Enums.RegionShrink.Nearest,
192 mutated.Tiffsave(file, Compression, 1, Enums.ForeignTiffPredictor.None,
true, (
int)l.
WidthPx, (
int)l.
HeightPx,
true,
false, 8,
193 Enums.ForeignTiffResunit.Cm, 1000 * l.
PhysicalX, 1000 * l.
PhysicalY,
true,
null, Enums.RegionShrink.Nearest,
206 int w = (int)Math.Ceiling(l.
Width / r.W);
207 int h = (int)Math.Ceiling(l.
Height / r.H);
209 bool leftright =
false;
210 for (
int y = 0; y < h; y++)
212 for (
int x = 0; x < w; x++)
215 l.ImageTile(
new PointD(pd.X, pd.Y),(w * h) - ((y * w) + x));
216 SlideImager.progress = (int)((
double)((y * w) + x) / (
double)(w * h));
223 l.ImageTile(
new PointD(p.X, p.Y), (w * h) - ((y * w) + w));
224 leftright = !leftright;
228 private void ImageTile(
PointD loc,
int tiles)
241 if (b.bitsPerPixel > 8)
242 im = NetVips.Image.NewFromMemory(b.Buffers[0].Data, (ulong)b.Buffers[0].Length, b.Buffers[0].Width, b.Buffers[0].Height, bands, Enums.BandFormat.Ushort);
244 im = NetVips.Image.NewFromMemory(b.Buffers[0].Data, (ulong)b.Buffers[0].Length, b.Buffers[0].Width, b.Buffers[0].Height, bands, Enums.BandFormat.Uchar);
249 status =
"Time Left:" + (Stopwatch.Elapsed * tiles).ToString();
252 private Level SlideLevel {
get;
set; } =
null;
253 private List<BaseLevel> Levels {
get;
set; } =
new List<BaseLevel>();
256 InitializeComponent();
261 foreach (Enums.ForeignTiffCompression o in (Enums.ForeignTiffCompression[])Enum.GetValues(typeof(Enums.ForeignTiffCompression)))
263 compBox.Items.Add(o);
268 private void setToStageBut_Click(
object sender, EventArgs e)
271 xBox.Value = (decimal)p.X;
272 yBox.Value = (decimal)p.Y;
275 private void setSizeBut_Click(
object sender, EventArgs e)
277 Point3D p = Microscope.GetPosition();
278 wBox.Value = (decimal)(p.X - (
double)xBox.Value);
279 hBox.Value = (decimal)(p.Y - (
double)yBox.Value);
282 private void startBut_Click(
object sender, EventArgs e)
284 if (saveFileDialog.ShowDialog() != DialogResult.OK)
286 Microscope.Objectives.SetPosition(Levels[0].Objective);
287 BioImage b = Microscope.TakeImage(
false,
false,
false);
288 SlideLevel =
new Level((
double)wBox.Value, (
double)hBox.Value, b.Buffers[0].PixelFormat, Levels.Count, (Enums.ForeignTiffCompression)compBox.SelectedItem, b.PhysicalSizeX, b.PhysicalSizeY);
289 for (
int i = 0; i < Levels.Count; i++)
291 SlideLevel.
Levels[i].Location = b.Volume.Location;
292 SlideLevel.
Levels[i].Focus = Levels[i].Focus;
293 SlideLevel.
Levels[i].Objective = Levels[i].Objective;
295 SlideLevel.PixelFormat = b.Buffers[0].PixelFormat;
296 SlideLevel.PhysicalX = b.PhysicalSizeX;
297 SlideLevel.PhysicalY = b.PhysicalSizeY;
299 RunImaging(saveFileDialog.FileName, SlideLevel, Levels);
301 static Level level =
null;
302 static string ffile =
null;
303 static List<BaseLevel> levels;
305 static string status =
"";
306 static bool imaging =
false;
307 static int count = 0;
308 private static int progress {
get {
return prog; }
set { prog = value; } }
309 private static void Image()
311 for (
int i = 0; i < levels.Count; i++)
313 string f = ffile.Replace(
".ome.tif",
"");
318 level.Levels[i].SaveLevel(f +
"-" + i +
".ome.tif");
322 Console.WriteLine(e.ToString());
327 level.SaveLevels(ffile);
329 private static void RunImaging(
string file, Level lev, List<BaseLevel> lvs)
335 Thread th =
new Thread(
Image);
339 private void addBut_Click(
object sender, EventArgs e)
341 BaseLevel bl =
new BaseLevel((
double)focBox.Value, objBox.SelectedIndex);
343 listBox.Items.Add(bl);
346 private void SlideImager_FormClosing(
object sender, FormClosingEventArgs e)
352 private void focBut_Click(
object sender, EventArgs e)
354 focBox.Value = (decimal)Microscope.GetFocus();
357 private void listBox_SelectedIndexChanged(
object sender, EventArgs e)
359 if (listBox.SelectedItem ==
null)
361 BaseLevel bl = (BaseLevel)listBox.SelectedItem;
362 focBox.Value = (decimal)bl.Focus;
363 objBox.SelectedIndex = bl.Objective;
366 private void objBox_SelectedIndexChanged(
object sender, EventArgs e)
368 if (listBox.SelectedItems.Count > 0)
370 BaseLevel bl = (BaseLevel)listBox.SelectedItem;
371 bl.Objective = objBox.SelectedIndex;
375 private void focBox_ValueChanged(
object sender, EventArgs e)
377 if (listBox.SelectedItems.Count > 0)
379 BaseLevel bl = (BaseLevel)listBox.SelectedItem;
380 bl.Focus = (double)focBox.Value;
384 private void remBut_Click(
object sender, EventArgs e)
386 if (listBox.SelectedItems.Count > 0)
387 listBox.Items.RemoveAt(listBox.SelectedIndex);
390 private void timer_Tick(
object sender, EventArgs e)
394 toolStripProgressBar.Value = progress * 100;
395 toolStripStatusLabel.Text = status;
AForge.RectangleD RectangleD
System.Drawing.Image Image
AForge.PixelFormat PixelFormat
static Objectives Objectives
static void SetPosition(Point3D p)
static RectangleD GetObjectiveViewRectangle()
static Point3D GetPosition()
static void MoveFieldDown()
Move the field down by one unit.
static BioImage TakeImage()
static void MoveFieldRight()
Move the field right by the width of the view.
static void MoveFieldLeft()
Move the field left by the width of the viewport.
void SetPosition(int index)
override string ToString()
BaseLevel(double focus, int objective)
Level(double w, double h, PixelFormat pf)
void ImageLevel(int level)
Level(double w, double h, PixelFormat pf, int levels, Enums.ForeignTiffCompression comp, double physX, double physY)
void Init(double w, double h, PixelFormat pf, int index)
void SaveLevels(string file)
List< BioImage > BioImages
void SaveLevel(string file)
static PointD ToImageSpace(PointD p, PointD stage, double physX, double physY)