48 public List<Level> Levels =
new List<Level>();
49 public Level Parent {
get;
set; } =
null;
50 public Point3D Location {
get;
set; }
51 public List<BioImage> BioImages {
get;
set; } =
new List<BioImage>();
52 public int Objective {
get;
set; }
53 public double Focus {
get;
set; }
54 public NetVips.Image VipsImage {
get;
set; }
55 public PixelFormat PixelFormat {
get;
set; }
56 public double PhysicalX {
get;
set; }
57 public double PhysicalY {
get;
set; }
58 public AForge.Bitmap Image {
get;
set; } =
null;
59 public double Width {
get;
set; }
60 public double Height {
get;
set; }
61 public int Index {
get;
set; }
62 public double WidthPx {
get {
return Width / PhysicalX; } }
63 public double HeightPx {
get {
return Height / PhysicalY; } }
64 public List<Channel> Channels =
new List<Channel>();
65 public Enums.ForeignTiffCompression Compression {
get;
set; }
66 public int CompressionLevel {
get;
set; }
67 public void Init(
double w,
double h, PixelFormat pf,
int index)
69 RectangleD r = Microscope.GetObjectiveViewRectangle();
70 int wi = (int)(w / r.W);
71 int hi = (int)(h / r.H);
72 double xx = 2 * index;
74 Image =
new AForge.Bitmap((
int)(w / PhysicalX), (
int)(h / PhysicalY), pf);
76 Image =
new AForge.Bitmap((
int)(w / PhysicalX / xx), (
int)(h / PhysicalY / xx), pf);
77 int bands = BioImage.GetBands(pf);
78 if (Image.BitsPerPixel > 8)
79 VipsImage = NetVips.Image.NewFromMemory(Image.Data, (ulong)Image.Length, Image.Width, Image.Height, bands, Enums.BandFormat.Ushort);
81 VipsImage = NetVips.Image.NewFromMemory(Image.Data, (ulong)Image.Length, Image.Width, Image.Height, bands, Enums.BandFormat.Uchar);
83 public Level(
double w,
double h, PixelFormat pf,
int levels, Enums.ForeignTiffCompression comp,
double physX,
double physY)
85 PhysicalX = physX; PhysicalY = physY;
87 for (
int i = 0; i < levels; i++)
89 Levels.Add(
new Level(w, h, pf));
90 Levels[i].Parent =
this;
93 public Level(
double w,
double h, PixelFormat pf)
95 Width = w; Height = h; PixelFormat = pf;
97 public void SaveLevel(
string file)
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\">";
103 int c = Parent.Image.RGBChannelsCount;
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\">";
115 foreach (Channel ch
in Channels)
117 met +=
"<Channel ID=\"Channel:" + ib +
":" + i +
"\" SamplesPerPixel=\"1\"></Channel>";
120 met +=
"</Pixels></Image></OME>";
124 using var mutated = Parent.VipsImage.Mutate(
mutable =>
127 mutable.Set(GValue.GStrType,
"image-description", met);
128 mutable.Set(GValue.GIntType,
"page-height", Parent.Image.Height);
130 if (Parent.Image.BitsPerPixel > 8)
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,
133 CompressionLevel,
true, Enums.ForeignDzDepth.Onetile,
true,
false,
null,
null, (
int)Parent.Image.Height);
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,
137 CompressionLevel,
true, Enums.ForeignDzDepth.Onetile,
true,
false,
null,
null, (
int)Parent.Image.Height);
141 Console.WriteLine(e.ToString());
147 public void SaveLevels(
string file)
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();
156 foreach (
Level l
in Levels)
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\">";
167 foreach (Channel ch
in l.Channels)
169 met +=
"<Channel ID=\"Channel:" + ib +
":" + i +
"\" SamplesPerPixel=\"1\"></Channel>";
172 met +=
"</Pixels></Image>";
177 foreach (
Level l
in Levels)
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);
185 mutable.Set(GValue.GIntType,
"page-height", l.Height * l.PhysicalY);
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,
190 CompressionLevel,
true, Enums.ForeignDzDepth.One,
true,
false,
null,
null, (
int)l.HeightPx);
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,
194 CompressionLevel,
true, Enums.ForeignDzDepth.One,
true,
false,
null,
null, (
int)l.HeightPx);
198 public void ImageLevel(
int level)
200 Level l = Levels[level];
202 Init(l.Width, l.Height, l.PixelFormat, l.Index);
203 Microscope.SetPosition(
new Point3D(l.Location.X, l.Location.Y, l.Focus));
204 Microscope.Objectives.SetPosition(l.Objective);
205 RectangleD r = Microscope.GetObjectiveViewRectangle();
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++)
214 Point3D pd = Microscope.GetPosition();
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));
218 Microscope.MoveFieldLeft();
220 Microscope.MoveFieldRight();
222 Point3D p = Microscope.GetPosition();
223 l.ImageTile(
new PointD(p.X, p.Y), (w * h) - ((y * w) + w));
224 leftright = !leftright;
225 Microscope.MoveFieldDown();
228 private void ImageTile(PointD loc,
int tiles)
232 BioImage b = Microscope.TakeImage(
false,
false,
false);
235 PhysicalX = b.PhysicalSizeX;
236 PhysicalY = b.PhysicalSizeY;
237 Channels = b.Channels;
238 PointD pd =
ToImageSpace(loc,
new PointD(Location.X, Location.Y), b.PhysicalSizeX, b.PhysicalSizeY);
239 int bands = BioImage.GetBands(PixelFormat);
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);
245 Parent.VipsImage = Parent.VipsImage.Insert(im, (
int)pd.X, (
int)pd.Y);
249 status =
"Time Left:" + (Stopwatch.Elapsed * tiles).ToString();