Get slice.
182 {
183 if (cache == null)
184 cache = new TileCache(this);
185 var curLevel = Image.BioImage.LevelFromResolution(sliceInfo.Resolution);
186 var curUnitsPerPixel = Schema.Resolutions[curLevel].UnitsPerPixel;
187 var tileInfos = Schema.GetTileInfos(sliceInfo.Extent, curLevel);
188 List<Tuple<Extent, byte[]>> tiles = new List<Tuple<Extent, byte[]>>();
189 foreach (BruTile.TileInfo t in tileInfos)
190 {
191 TileInformation tf = new TileInformation();
192 tf.Extent = t.Extent;
193 tf.Coordinate = sliceInfo.Coordinate;
194 tf.Index = t.Index;
195 byte[] c = await cache.GetTile(tf);
196 if(c!=null)
197 tiles.Add(Tuple.Create(t.Extent.WorldToPixelInvertedY(curUnitsPerPixel), c));
198 }
199 var srcPixelExtent = sliceInfo.Extent.WorldToPixelInvertedY(curUnitsPerPixel);
200 var dstPixelExtent = sliceInfo.Extent.WorldToPixelInvertedY(sliceInfo.Resolution);
201 var dstPixelHeight = sliceInfo.Parame.DstPixelHeight > 0 ? sliceInfo.Parame.DstPixelHeight : dstPixelExtent.Height;
202 var dstPixelWidth = sliceInfo.Parame.DstPixelWidth > 0 ? sliceInfo.Parame.DstPixelWidth : dstPixelExtent.Width;
203 destExtent = new Extent(0, 0, dstPixelWidth, dstPixelHeight);
204 sourceExtent = srcPixelExtent;
205 if (UseVips)
206 {
207 try
208 {
209 NetVips.Image im = null;
210 if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format16bppGrayScale)
211 im = ImageUtil.JoinVips16(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
212 else if(this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format24bppRgb)
213 im = ImageUtil.JoinVipsRGB24(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
214 return im.WriteToMemory();
215 }
216 catch (Exception e)
217 {
218 UseVips = false;
219 Console.WriteLine("Failed to use LibVips please install Libvips for your platform.");
220 Console.WriteLine(e.Message);
221 }
222 }
223 try
224 {
225 Image im = null;
226 if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format16bppGrayScale)
227 {
228 im = ImageUtil.Join16(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
229 byte[] bts = Get16Bytes((Image<L16>)im);
230 im.Dispose();
231 return bts;
232 }
233 else if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format24bppRgb)
234 {
235 im = ImageUtil.JoinRGB24(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
236 byte[] bts = GetRgb24Bytes((Image<Rgb24>)im);
237 im.Dispose();
238 return bts;
239 }
240 else if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format8bppIndexed)
241 {
242 im = ImageUtil.Join8Bit(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
243 byte[] bts = Get8BitBytes((Image<L8>)im);
244 im.Dispose();
245 return bts;
246 }
247 }
248 catch (Exception er)
249 {
250 Console.WriteLine(er.Message);
251 return null;
252 }
253 return null;
254 }