private static void InsertSlides(SlideInfoDbContext context) { if (context.Slides.Any()) { return; } var dirs = FileFilter.Filter(AppDirectories.SlideStorage, FileFilter.OpenSlideExtensions); foreach (var path in dirs) { var osr = new OpenSlide(path); var slide = new Slide(osr); context.Slides.Add(slide); context.SaveChanges(); var properties = osr.ReadProperties(); foreach (var slideProp in properties) { var property = new Property(slide.Id, slideProp.Key, slideProp.Value); context.Properties.Add(property); } } context.SaveChanges(); }
public IActionResult AssociatedTile(int?id, string imageName, int level, int col, int row) { logger.LogInformation("Getting tile of {slug} | lev: {level}, col: {col}, row: {row}", imageName, level, col, row); try { var slide = context.Slides.FirstOrDefault(m => m.Id == id); if (slide != null) { using (var osr = new OpenSlide(slide.FilePath)) { var associated = osr.AssociatedImages[imageName].ToImageSlide(); var viewModel = new DisplayViewModel(slide.Name, slide.DziUrl, slide.Mpp, associated); var tile = viewModel.DeepZoomGenerator.GetTile(level, new SizeL(col, row)); using (var stream = new MemoryStream()) { tile.Save(stream, ImageFormat.Jpeg); tile.Dispose(); return(File(stream.ToArray(), "image/jpeg")); } } } } catch (OpenSlideException) { logger.LogError("Error while getting tile | lev: {level}, col: {col}, row: {row}", level, col, row); HttpContext.Session.SetString(SessionConstants.ALERT, SessionConstants.CANT_LOAD_DATA); } return(new FileContentResult(new byte[] { }, "")); }
// GET: Slides/Display/5 public async Task <IActionResult> Display(int?id) { logger.LogInformation("Getting slide {ID} to display...", id); if (id == null) { logger.LogWarning("Slide id was null"); return(NotFound()); } var slide = await context.Slides.FindAsync(id.Value); if (slide == null) { logger.LogError("GetById({ID}) NOT FOUND", id); return(NotFound()); } HttpContext.Session.Set(SessionConstants.CURRENT_SLIDE, slide); ViewData[SLIDE_ID] = slide.Id.ToString(); HttpContext.Session.SetString(SLIDE_ID, slide.Id.ToString()); ViewData[SLIDE_NAME] = slide.Name; ViewData[HAS_ASSOCIATED_IMAGES] = slide.HasAssociatedImages; var comments = context.Comments.Where(c => c.SlideId == id); ViewData[HAS_COMMENTS] = comments.Any(); var osr = new OpenSlide(slide.FilePath); var viewModel = new DisplayViewModel(slide.Name, slide.DziUrl, slide.Mpp, osr); return(View(viewModel)); }
public IActionResult Tile(string slug, int level, int col, int row) { try { logger.LogInformation("Getting tile: {level}, col: {col}, row: {row}", level, col, row); var slide = HttpContext.Session.Get <Slide>(SessionConstants.CURRENT_SLIDE) ?? context.Slides.FirstOrDefault(m => m.Url == slug); if (slide != null) { using (var osr = new OpenSlide(slide.FilePath)) { var viewModel = new DisplayViewModel(slide.Name, slide.DziUrl, slide.Mpp, osr); var tile = viewModel.DeepZoomGenerator.GetTile(level, new SizeL(col, row)); using (var stream = new MemoryStream()) { tile.Save(stream, ImageFormat.Jpeg); tile.Dispose(); return(File(stream.ToArray(), "image/jpeg")); } } } } catch (OpenSlideException) { logger.LogError("Error while getting tile lev: {level}, col: {col}, row: {row}", level, col, row); HttpContext.Session.SetString(SessionConstants.ALERT, SessionConstants.CANT_LOAD_DATA); } return(new FileContentResult(new byte[] { }, "")); }
private void GenerateSlideThumbnails(ICollection <Slide> slides) { Directory.CreateDirectory(AppDirectories.SlideThumbs); var existingThumbs = from file in Directory.EnumerateFiles(AppDirectories.SlideThumbs, "*.jpeg") select file; var existingThumbsCount = existingThumbs.Count(); if (slides.Count == existingThumbsCount) { return; } foreach (var slide in slides) { var existingSlideThumb = from file in existingThumbs where file.ToLower().Contains($"{slide.Name.ToUrlSlug()}") select file; if (existingSlideThumb.Any()) { continue; } logger.LogInformation("Generating thumbnail of slide {id}: {name}..", slide.Id, slide.Name); using (var osr = new OpenSlide(slide.FilePath)) { var thumb = osr.GetThumbnail(new Size(400, 400)); thumb.Save($@"{AppDirectories.SlideThumbs}{slide.Name.ToUrlSlug()}.jpeg", ImageFormat.Jpeg); } } }
public static void UpdateSlides(SlideInfoDbContext context) { context.Database.EnsureCreated(); var dirs = FileFilter.Filter(AppDirectories.SlideStorage, FileFilter.OpenSlideExtensions); foreach (var path in dirs) { var osr = new OpenSlide(path); var newSlide = new Slide(osr); var existingSlide = context.Slides.FirstOrDefault(s => s.FilePath == path); if (existingSlide != null) { newSlide.Id = existingSlide.Id; context.Entry(existingSlide).CurrentValues.SetValues(newSlide); } else { context.Add(newSlide); var properties = osr.ReadProperties(); foreach (var slideProp in properties) { var property = new Property(newSlide.Id, slideProp.Key, slideProp.Value); context.Add(property); } } } context.SaveChanges(); }
public void TestDetectFormat() { var osr = new OpenSlide(); Assert.IsTrue(osr.DetectFormat("nonExisting") == null); Assert.IsTrue(osr.DetectFormat("setup.py") == null); Assert.IsTrue(osr.DetectFormat("tests/boxes.tiff") == "generic-tiff"); }
public void TestReadRegionOnClosedHandle() { var osr = new OpenSlide("tests/boxes.tiff"); osr.Close(); osr.ReadRegion(new SizeL(0, 0), 0, new SizeL(100, 100)); }
public AssociatedImagesWriter(string fileName, string outDirectory = "out", string format = "bmp", int quality = 75) { this.fileName = fileName; fileNameWithoutExtension = FileName.GetFileNameWithoutExtension(fileName); this.outDirectory = outDirectory; this.format = format; this.quality = quality; osr = new OpenSlide(fileName); pathBuilder = new StringBuilder(); }
static void GetJpg(OpenSlide openSlide, int level, int row, int col, string filename, string outputname) { using (var stream = openSlide.GetJpg(filename, $"_files/{level}/{row}_{col}.jpeg")) { if (stream.TryGetBuffer(out ArraySegment <byte> buffer)) { Console.WriteLine($"{outputname}.jpeg successfully created!"); File.WriteAllBytes($"{outputname}.jpeg", buffer.ToArray()); } else { Console.WriteLine($"{outputname}.jpeg failed..."); } } }
static void GetDZI(OpenSlide openSlide, long width, long height, string filename, string outputname) { using (var stream = openSlide.GetDZI(filename, out width, out height)) { if (stream.TryGetBuffer(out ArraySegment <byte> buffer)) { Console.WriteLine($"{outputname}.dzi successfully created!"); File.WriteAllBytes($"{outputname}.dzi", buffer.ToArray()); } else { Console.WriteLine($"{outputname}.dzi failed..."); } } }
public DeepZoomTiler(string fileName, string outDirectory = "out", string format = "jpeg", int quality = 75, int tileSize = 254, int overlap = 1, bool limitBounds = true) { this.fileName = fileName; fileNameWithoutExtension = FileName.GetFileNameWithoutExtension(fileName); this.outDirectory = outDirectory; this.format = format; this.quality = quality; this.tileSize = tileSize; this.overlap = overlap; this.limitBounds = limitBounds; osr = new OpenSlide(fileName); dz = new DeepZoomGenerator(osr, tileSize, overlap, limitBounds); pathBuilder = new StringBuilder(); processedTilesCount = 0; }
// GET: Slides/AssociatedImages/5 public async Task <IActionResult> AssociatedImages(int?id, string imageName) { logger.LogInformation("Getting associated images of slide {ID}...", id); if (id == null) { return(NotFound()); } var slide = await context.Slides.FindAsync(id.Value); if (slide == null) { return(NotFound()); } ViewData[SLIDE_NAME] = slide.Name; ViewData[SLIDE_ID] = id.ToString(); HttpContext.Session.SetString(SLIDE_ID, slide.Id.ToString()); ViewData[HAS_ASSOCIATED_IMAGES] = slide.HasAssociatedImages; var comments = context.Comments.Where(c => c.SlideId == id); ViewData[HAS_COMMENTS] = comments.Any(); var osr = new OpenSlide(slide.FilePath); if (!String.IsNullOrEmpty(imageName)) { logger.LogInformation("Getting associated image {name} of slide {ID}...", imageName, id); var associatedSlide = osr.AssociatedImages[imageName].ToImageSlide(); var imageUrl = slide.Id + "/" + imageName + ".dzi"; var displayViewModel = new DisplayViewModel(imageName, imageUrl, 0, associatedSlide); return(View("Display", displayViewModel)); } var associated = osr.ReadAssociatedImages(); GenerateAssociatedImagesThumbnails(id.Value, associated); var viewModel = new AssociatedImagesViewModel(slide.Name, associated); return(View(viewModel)); }
public Slide(OpenSlide osr) { FilePath = osr.FilePath; Name = Path.GetFileName(FilePath); Url = UrlFormatter.UrlFor(Name); Vendor = osr.DetectFormat(FilePath); Width = osr.Dimensions.Width; Height = osr.Dimensions.Height; HasAssociatedImages = osr.ReadAssociatedImages().Any(); try { double.TryParse(osr.Properties[OpenSlide.PROPERTY_NAME_MPP_X], out double mppX); double.TryParse(osr.Properties[OpenSlide.PROPERTY_NAME_MPP_Y], out double mppY); Mpp = (mppX + mppY) / 2; } catch (Exception) { Mpp = 0; } }
public string Dzi(string slug) { try { logger.LogInformation("Getting {slug}.dzi metadata...", slug); var slide = HttpContext.Session.Get <Slide>(SessionConstants.CURRENT_SLIDE) ?? context.Slides.FirstOrDefault(m => m.Url == slug); if (slide != null) { using (var osr = new OpenSlide(slide.FilePath)) { var viewModel = new DisplayViewModel(slide.Name, slide.DziUrl, slide.Mpp, osr); return(viewModel.DeepZoomGenerator.GetDziMetadataString()); } } } catch (Exception) { logger.LogError("Error while getting {slug}.dzi", slug); HttpContext.Session.SetString(SessionConstants.ALERT, SessionConstants.NO_ACCESS); } return(""); }
public string AssociatedDzi(int?id, string imageName) { logger.LogInformation("Getting {slug}.dzi metadata...", imageName); try { var slide = context.Slides.FirstOrDefault(m => m.Id == id); if (slide != null) { using (var osr = new OpenSlide(slide.FilePath)) { var associated = osr.AssociatedImages[imageName].ToImageSlide(); var viewModel = new DisplayViewModel(slide.Name, slide.DziUrl, slide.Mpp, associated); return(viewModel.DeepZoomGenerator.GetDziMetadataString()); } } } catch (Exception) { logger.LogError("Error while getting {slug}.dzi", imageName); HttpContext.Session.SetString(SessionConstants.ALERT, SessionConstants.NO_ACCESS); } return(""); }
static void Main(string[] args) { string filename = "CMU-1-Small-Region"; var openSlide = new OpenSlide(); //var test = openSlide.GetMPP("CMU-1-Small-Region.svs"); //MPP = microns per pixel SizeL[] levels = openSlide.Levels($"{filename}.svs"); List <SizeL> dimensions = openSlide.Dimensions($"{filename}.svs"); SizeL maxDimensions = dimensions[dimensions.Count - 1]; //Loads DZI file GetDZI(openSlide, maxDimensions.Width, maxDimensions.Height, $"{filename}.svs", filename); for (int level = 0; level < levels.Length; level++) { Directory.CreateDirectory($"{filename}_files/{level}"); SizeL levelInfo = levels[level]; for (int row = 0; row < levelInfo.Width; row++) { for (int col = 0; col < levelInfo.Height; col++) { GetJpg(openSlide, level, row, col, $"{filename}.svs", $"{filename}_files/{level}/{row}_{col}"); } } } Console.WriteLine("Done"); Console.ReadLine(); }
public Slide(string pathToSlide) { FilePath = pathToSlide; Name = Path.GetFileName(pathToSlide); Url = UrlFormatter.UrlFor(Name); Vendor = OpenSlide.DetectVendor(pathToSlide); using (var osr = new OpenSlide(pathToSlide)) { try { double.TryParse(osr.Properties[OpenSlide.PROPERTY_NAME_MPP_X], out double mppX); double.TryParse(osr.Properties[OpenSlide.PROPERTY_NAME_MPP_Y], out double mppY); Mpp = (mppX + mppY) / 2; } catch (Exception) { Mpp = 0; } Width = osr.Dimensions.Width; Height = osr.Dimensions.Height; HasAssociatedImages = osr.ReadAssociatedImages().Any(); } }
public void SetUp() { osr = new OpenSlide("tests/boxes.tiff"); dz = new DeepZoomGenerator(osr); }
public void SetUp() { osr = new OpenSlide(fileName); }
public void TestContextManager() { var osr = new OpenSlide("tests/boxes.tiff"); Assert.AreEqual(4, osr.LevelCount); }