SlideIO 2.0.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
cachemanager.hpp
1// This file is part of slideio project.
2// It is subject to the license terms in the LICENSE file found in the top-level directory
3// of this distribution and at http://slideio.com/license.html.
4#pragma once
5#include <map>
6
7#include "slideio/core/slideio_core_def.hpp"
8#include <unordered_map>
9#include <boost/container_hash/hash.hpp>
10#include <opencv2/core.hpp>
11
12#include "tilecomposer.hpp"
13#include "tools.hpp"
14
15#if defined(_MSC_VER)
16#pragma warning( push )
17#pragma warning(disable: 4251)
18#endif
19
20namespace slideio
21{
22 class SLIDEIO_CORE_EXPORTS CacheManager
23 {
24 public:
25 class Level
26 {
27 public:
28 Level(int id) : m_id(id)
29 {
30 }
31
32 int getId() const { return m_id; }
33
34 void addTile(const cv::Rect& rect, int cacheIndex)
35 {
36 tiles.emplace_back(rect, cacheIndex);
37 }
38
39 int getTileCount() const
40 {
41 return static_cast<int>(tiles.size());
42 }
43
44 const cv::Rect& getTileRect(int index) const
45 {
46 return tiles[index].first;
47 }
48
49 int getTileCacheIndex(int index) const
50 {
51 return tiles[index].second;
52 }
53
54 private:
55 int m_id;
56 std::vector<std::pair<cv::Rect, int>> tiles;
57 };
58
59 CacheManager();
60 virtual ~CacheManager();
61 void addTile(int level, cv::Point2i pos, const cv::Mat& tile);
62 int getTileCount(int level) const;
63 cv::Mat getTile(int levelId, int index) const;
64 const cv::Rect getTileRect(int levelId, int tileIndex) const;
65
66 private:
67 std::vector<cv::Mat> m_cache;
68 std::map<int, std::shared_ptr<Level>> m_levels;
69 };
70
71 class SLIDEIO_CORE_EXPORTS CacheManagerTiler : public Tiler
72 {
73 public:
74 CacheManagerTiler(std::shared_ptr<CacheManager>& cacheManager, const cv::Size& tileSize, int levelId) :
75 m_cacheManager(cacheManager), m_tileSize(tileSize), m_levelId(levelId) {
76 }
77 int getTileCount(void* userData) override {
78 return m_cacheManager->getTileCount(m_levelId);
79 }
80 bool getTileRect(int tileIndex, cv::Rect& tileRect, void* userData) override {
81 tileRect = m_cacheManager->getTileRect(m_levelId, tileIndex);
82 return true;
83 }
84 bool readTile(int tileIndex, const std::vector<int>& channelIndices, cv::OutputArray tileRaster,
85 void* userData) override {
86 cv::Mat tile = m_cacheManager->getTile(m_levelId, tileIndex);
87 tileRaster.create(tile.size(), tile.type());
88 Tools::extractChannels(tile, channelIndices, tileRaster);
89 return true;
90 }
91 void initializeBlock(const cv::Size& blockSize, const std::vector<int>& channelIndices,
92 cv::OutputArray output) override {
93 cv::Mat tile = m_cacheManager->getTile(m_levelId, 0);
94 int channelCount = static_cast<int>(channelIndices.size());
95 if (channelCount == 0) {
96 channelCount = tile.channels();
97 }
98 output.create(blockSize, CV_MAKETYPE(tile.depth(), channelCount));
99 output.setTo(0);
100 }
101
102 private:
103 std::shared_ptr<CacheManager> m_cacheManager;
104 cv::Size m_tileSize;
105 int m_levelId;
106 };
107}
Definition: exceptions.hpp:12