SlideIO 2.10.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
cziscene.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 "slideio/drivers/czi/czi_api_def.hpp"
6#include "slideio/core/cvscene.hpp"
7#include "slideio/core/tools/filereader.hpp"
8#include "slideio/core/tools/tilecomposer.hpp"
9#include "slideio/drivers/czi/czisubblock.hpp"
10#include "slideio/drivers/czi/czistructs.hpp"
11#include <map>
12#include <memory>
13
14#if defined(_MSC_VER)
15#pragma warning( push )
16#pragma warning(disable: 4251)
17#endif
18
19namespace slideio
20{
21 class CZISlide;
22 class SLIDEIO_CZI_EXPORTS CZIScene : public CVScene, public Tiler
23 {
24 public:
25 struct SceneParams
26 {
27 int illuminationIndex;
28 int bAcquisitionIndex;
29 int rotationIndex;
30 int sceneIndex;
31 int hPhaseIndex;
32 int viewIndex;
33 };
34 private:
35 struct Tile
36 {
37 Tile() { rect = { 0,0,0,0 }; }
38 std::vector<int> blockIndices;
39 cv::Rect rect;
40 };
41 typedef std::vector<Tile> Tiles;
42 struct ZoomLevel
43 {
44 double zoom{};
45 CZISubBlocks blocks;
46 Tiles tiles;
47 };
48 struct ComponentInfo
49 {
50 std::string name;
51 DataType dataType;
52 };
53 struct SceneChannelInfo
54 {
55 std::string name;
56 int32_t pixelType;
57 int32_t pixelSize;
58 int32_t firstComponent;
59 int32_t numComponents;
60 DataType componentType;
61 };
62 struct TilerData
63 {
64 int zoomLevelIndex;
65 int zSliceIndex;
66 int tFrameIndex;
67 double relativeZoom;
68 };
69 public:
70 CZIScene();
71 std::string getFilePath() const override;
72 int getSceneIndex() const override { return m_sceneIndex; }
73 const std::string& getDriverId() const override {
74 return m_driverId;
75 }
76 bool supportsConcurrentReads() const override { return true; }
77 cv::Rect getRect() const override;
78 int getNumChannels() const override;
79 int getNumZSlices() const override;
80 int getNumTFrames() const override;
81 double getZSliceResolution() const override;
82 double getTFrameResolution() const override;
83 slideio::DataType getChannelDataType(int channel) const override;
84 int getChannelSignificantBits(int channelIndex) const override;
85 bool hasPlaneTimestamps() const override { return !m_planeTimestamps.empty(); }
86 double getPlaneTimestamp(int tFrame, int channel, int zSlice) const override;
87 int64_t getAcquisitionTime() const override { return m_acquisitionTime; }
88 std::string getChannelName(int channel) const override;
89 Resolution getResolution() const override;
90 double getMagnification() const override;
91 std::string getName() const override;
92 void init(uint64_t sceneId, SceneParams& sceneParams, const std::string& filePath, int sceneIndex, const std::string& driverId, const CZISubBlocks& blocks, CZISlide* slide, bool mainScene = true);
93 // interface Tiler implementaton
94 int getTileCount(void* userData) override;
95 bool getTileRect(int tileIndex, cv::Rect& tileRect, void* userData) override;
96 bool readTile(int tileIndex, const std::vector<int>& componentIndices, cv::OutputArray tileRaster,
97 void* userData) override;
98 void initializeBlock(const cv::Size& blockSize, const std::vector<int>& channelIndices, cv::OutputArray output) override;
99 Compression getCompression() const override{
100 return m_compression;
101 }
102 void addAuxImage(const std::string& name, std::shared_ptr<CVScene> image);
103 std::shared_ptr<CVScene> getAuxImage(const std::string& sceneName) const override;
104 bool isMosaic() const { return m_bMosaic; }
105 void readResampledBlockChannelsEx(const cv::Rect& blockRect, const cv::Size& blockSize,
106 const std::vector<int>& componentIndices, int zSliceIndex, int tFrameIndex, cv::OutputArray output) override;
107 void readResampledLevelBlockChannelsEx(int level, const cv::Rect& levelRect,
108 const cv::Size& blockSize, const std::vector<int>& componentIndices,
109 int zSliceIndex, int tFrameIndex, cv::OutputArray output) override;
110 private:
111 void setMosaic(bool mosaic) { m_bMosaic = mosaic; }
112 void setupComponents(const std::map<int, int>& channelPixelType);
113 void setupComponentSignificantBits();
114 void collectPlaneTimestamps(const CZISubBlocks& blocks);
115 void setupComponentsAux(const std::map<int, int>& channelPixelType);
116 void generateSceneName();
117 void computeSceneRect();
118 void computeSceneTiles();
119 void compute4DParameters();
120 void updateTileRects(ZoomLevel& value);
121 void updateTileRects();
122 const ZoomLevel& getBaseZoomLevel() const;
123 void initZoomLevelInfo();
124 int findBlockIndex(const Tile& tile, const CZISubBlocks& blocks, int channelIndex, int zSliceIndex, int tFrameIndex) const ;
125 const Tile& getTile(const TilerData* tilerData, int tileIndex) const;
126 const CZISubBlocks& getBlocks(const TilerData* tilerData) const;
127 bool blockHasData(const CZISubBlock& block, const std::vector<int>& componentIndices, const TilerData* tilerData);
128 std::vector<uint8_t> decodeData(const CZISubBlock& block, const std::vector<unsigned char>& encodedData);
129 void unpackChannels(const CZISubBlock& block, const std::vector<int>& orgComponentIndices, const std::vector<unsigned char>& blockData, const TilerData* tilerData, std::vector<cv::Mat>& componentRasters);
130 void computeSceneMetadata();
131 public:
132 // static members
133 static uint64_t sceneIdFromDims(int s, int i, int v, int h, int r, int b);
134 static uint64_t sceneIdFromDims(const std::vector<Dimension>& dims);
135 static void sceneIdsFromDims(const std::vector<Dimension>& dims, std::vector<uint64_t>& ids);
136 static uint64_t sceneIdFromDims(const SceneParams& params);
137 static void dimsFromSceneId(uint64_t sceneId, int& s, int& i, int& v, int& h, int& r, int& b);
138 static void dimsFromSceneId(uint64_t sceneId, SceneParams& params);
139 static void channelComponentInfo(CZIDataType channelType, DataType& componentType, int& numComponents, int& pixelSize);
140 private:
141 void combineBlockInTiles(ZoomLevel& zoomLevel);
142 // data members
143 private:
144 std::vector<ZoomLevel> m_zoomLevels;
145 std::vector<ComponentInfo> m_componentInfos;
146 // ComponentBitCount of the CZI channel each scene component belongs to,
147 // falling back to the image level count and 0 where the file states
148 // neither. A channel of several components -- an interleaved Bgr24, say --
149 // gives all of them its own count.
150 std::vector<int> m_componentSignificantBits;
151 // Seconds from getAcquisitionTime(), one per plane, indexed
152 // (t * numZ + z) * numChannels + channel. Empty unless every plane states
153 // a time. Channel here is the CZI channel, which an interleaved pixel
154 // format expands into several scene components sharing one plane. A plane
155 // built of many tiles takes the earliest time its tiles state.
156 std::vector<double> m_planeTimestamps;
157 int64_t m_acquisitionTime = 0;
158 // Whether this scene has an acquisition origin of its own. Not the same as
159 // a zero m_acquisitionTime, and not the same as the slide having one: an
160 // attachment scene is excluded even where the main image states one.
161 bool m_hasAcquisitionTime = false;
162 int m_timestampChannels = 0;
163 std::vector<SceneChannelInfo> m_channelInfos;
164 std::string m_filePath;
165 cv::Rect m_sceneRect;
166 std::map<int, std::pair<int, int>> m_componentToChannelIndex;
167 CZISlide* m_slide;
168 std::shared_ptr<const FileReader> m_reader;
169 std::string m_name;
170 uint64_t m_id{};
171 SceneParams m_sceneParams{};
172 int m_numZSlices;
173 int m_numTFrames;
174 int m_firstSliceIndex = 0;
175 int m_firstTFrameIndex = 0;
176 Compression m_compression;
177 std::map<std::string, std::shared_ptr<CVScene>> m_auxImages;
178 bool m_bMosaic;
179 int m_sceneIndex;
180 std::string m_driverId;
181
182 };
183}
184
185
186#if defined(_MSC_VER)
187#pragma warning( pop )
188#endif
Definition: converter.hpp:18
Compression
raster data compression enum
Definition: slideio_enums.hpp:13