SlideIO 2.10.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
volume.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 <algorithm>
6#include <cstdint>
7#include <string>
8#include <vector>
9
10#include "taginfo.hpp"
11#include "vsistruct.hpp"
12#include "slideio/drivers/vsi/vsi_api_def.hpp"
13#include "slideio/imagetools/tifftools.hpp"
14#include "slideio/drivers/vsi/dimensions.hpp"
15
16#if defined(_MSC_VER)
17#pragma warning( push )
18#pragma warning(disable: 4251)
19#endif
20
21
22namespace slideio
23{
24 namespace vsi
25 {
26 class SLIDEIO_VSI_EXPORTS Volume : public IDimensionOrder
27 {
28 public:
29 // The order of a dimension is only known once DIMENSION_DESCRIPTION has
30 // been read, so every dimension starts unset. X and Y are the exception:
31 // the format fixes them at 0 and 1, which is also why UNSET_DIMENSION_ORDER
32 // cannot be 0 -- that is a real order, and readers here test for "> 1".
33 Volume() {
34 std::fill(std::begin(m_dimensionOrder), std::end(m_dimensionOrder),
35 UNSET_DIMENSION_ORDER);
36 m_dimensionOrder[dimensionIndex(Dimensions::X)] = 0;
37 m_dimensionOrder[dimensionIndex(Dimensions::Y)] = 1;
38 }
39 std::string getName() const { return m_name; }
40 void setName(const std::string& name) { m_name = name; }
41
42 double getMagnification() const { return m_magnification; }
43 void setMagnification(double magnification) { m_magnification = magnification; }
44
45 StackType getType() const { return m_type; }
46 void setType(StackType type) { m_type = type; }
47
48 cv::Size getSize() const { return m_size; }
49 void setSize(const cv::Size& size) { m_size = size; }
50
51 int getBitDepth() const { return m_bitDepth; }
52 void setBitDepth(int bitDepth) { m_bitDepth = bitDepth; }
53
54 bool hasExternalFile() const { return m_hasExternalFile; }
55 void setHasExternalFile(bool hasExternalFile) { m_hasExternalFile = hasExternalFile; }
56
57 int getNumAuxVolumes() const { return static_cast<int>(m_auxVolumes.size()); }
58 std::shared_ptr<Volume> getAuxVolume(int index) const { return m_auxVolumes[index]; }
59 void addAuxVolume(std::shared_ptr<Volume>& volume) { m_auxVolumes.push_back(volume); }
60
61 void setIFD(int ifd) { m_ifd = ifd; }
62 int getIFD() const { return m_ifd; }
63
64 void setDefaultColor(int color) { m_defaultColor = color; }
65 int getDefaultColor() const { return m_defaultColor; }
66
67 int getDimensionOrder(Dimensions dim) const override { return m_dimensionOrder[dimensionIndex(dim)]; }
68 void setDimensionOrder(Dimensions dim, int value) { m_dimensionOrder[dimensionIndex(dim)] = value; }
69
70 // Length in a Volume is always in metres, on the same terms as time
71 // below: the raw value arrives with the unit the file stated and is
72 // converted here, and a unit that cannot be read stores nothing rather
73 // than a number waiting to be scaled by a guess.
74 const Resolution& getResolution() const { return m_resolution; }
75 void setResolution(double rawX, double rawY, const std::string& unit);
76 void setZResolution(double raw, const std::string& unit);
77 double getZResolution() const { return m_zResolution; }
78 // Time in a Volume is always in seconds. A raw value arrives with the unit
79 // the file stated and is converted here; if that unit cannot be read the
80 // number means nothing and is not stored, rather than kept to be scaled by
81 // a guess later.
82 void setTResolution(double raw, const std::string& unit);
83 double getTResolution() const { return m_tResolution; }
84 void setChannelName(int channelIndex, const std::string& channelName);
85 std::string getChannelName(int channelIndex) const;
86
87 // Channel display color extracted from VSI metadata (tag STACK_DISPLAY_LUT
88 // gradient endpoint or DISPLAY_COLOR). Stored as 0x00RRGGBB. Sentinel
89 // 0xFFFFFFFF means "no color stored for this channel".
90 void setChannelColor(int channelIndex, uint8_t r, uint8_t g, uint8_t b);
91 bool hasChannelColor(int channelIndex) const;
92 void getChannelColor(int channelIndex, uint8_t& r, uint8_t& g, uint8_t& b) const;
93
94 // Per-channel emission wavelength in nanometres (tag CHANNEL_WAVELENGTH).
95 // 0.0 means "not set".
96 void setChannelEmissionWavelength(int channelIndex, double nm);
97 double getChannelEmissionWavelength(int channelIndex) const;
98
99 // Converted to seconds on the way in, like the T resolution above.
100 // An unreadable unit stores nothing, so a count of 0 means "no usable
101 // timestamps" and every stored value is in seconds.
102 void setPlaneTimestamps(const std::vector<double>& raw, const std::string& unit);
103 int getPlaneTimestampCount() const;
104 double getPlaneTimestampByIndex(int index) const;
105
106 // Acquisition start as a Unix epoch in seconds; 0 when the file records none.
107 void setAcquisitionTime(int64_t epochSeconds) { m_acquisitionTime = epochSeconds; }
108 int64_t getAcquisitionTime() const { return m_acquisitionTime; }
109
110 const bool isValid() const {
111 return m_size.height>0 && m_size.width>0;
112 }
113
114 private:
115 static int dimensionIndex(Dimensions dim) {
116 return static_cast<int>(dim);
117 }
118 private:
119 std::string m_name;
120 double m_magnification = 0.;
121 StackType m_type = StackType::UNKNOWN;
122 cv::Size m_size = {};
123 int m_bitDepth = 0;
124 bool m_hasExternalFile = false;
125 int m_ifd = -1;
126 std::vector<std::shared_ptr<Volume>> m_auxVolumes;
127 int m_defaultColor = 0;
128 // Filled by the constructor: a braced initialiser would set only the
129 // first element and value-initialise the rest to 0.
130 int m_dimensionOrder[MAX_DIMENSIONS];
131 Resolution m_resolution;
132 double m_zResolution = 0.;
133 double m_tResolution = 0.; // seconds
134 std::vector<std::string> m_channelNames;
135 static constexpr uint32_t kNoChannelColor = 0xFFFFFFFFu;
136 std::vector<uint32_t> m_channelColors;
137 std::vector<double> m_channelEmissionWavelengths;
138 std::vector<double> m_planeTimestamps; // seconds
139 int64_t m_acquisitionTime = 0;
140 };
141
142 };
143};
Definition: converter.hpp:18