SlideIO 2.9.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
czisubblock.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
6#include "slideio/drivers/czi/czi_api_def.hpp"
7#include "slideio/drivers/czi/czistructs.hpp"
8#include "slideio/base/slideio_enums.hpp"
9#include <opencv2/core.hpp>
10#include <cstdint>
11#include <vector>
12
13#if defined(_MSC_VER)
14#pragma warning( push )
15#pragma warning(disable: 4251)
16#endif
17
18namespace slideio
19{
20 class SLIDEIO_CZI_EXPORTS CZISubBlock
21 {
22 public:
23 enum Compression
24 {
25 Uncompressed = 0,
26 Jpeg = 1,
27 LZW = 2,
28 JpegXR = 4
29 };
30 CZISubBlock();
31 int firstChannel() const { return firstDimensionIndex(m_channelIndex);}
32 int lastChannel() const { return lastDimensionIndex(m_channelIndex); }
33 int firstZSlice() const { return firstDimensionIndex(m_zSliceIndex); }
34 int lastZSlice() const { return lastDimensionIndex(m_zSliceIndex); }
35 int firstTFrame() const { return firstDimensionIndex(m_tFrameIndex); }
36 int lastTFrame() const { return lastDimensionIndex(m_tFrameIndex); }
37 int firstScene() const { return firstDimensionIndex(m_sceneIndex); }
38 int lastScene() const { return lastDimensionIndex(m_sceneIndex); }
39 int firstIllumination() const { return firstDimensionIndex(m_illuminationIndex); }
40 int lastIllumination() const { return lastDimensionIndex(m_illuminationIndex); }
41 int firstRotation() const { return firstDimensionIndex(m_rotationIndex); }
42 int lastRotation() const { return lastDimensionIndex(m_rotationIndex); }
43 int firstBAcquisition() const { return firstDimensionIndex(m_bAcquisitionIndex); }
44 int lastBAcquisition() const { return lastDimensionIndex(m_bAcquisitionIndex); }
45 int firstHPhase() const { return firstDimensionIndex(m_hPhaseIndex); }
46 int lastHPhase() const { return lastDimensionIndex(m_hPhaseIndex); }
47 int firstView() const { return firstDimensionIndex(m_viewIndex); }
48 int lastView() const { return lastDimensionIndex(m_viewIndex); }
49 int firstMosaic() const { return firstDimensionIndex(m_mosaicIndex); }
50 int lastMosaic() const { return lastDimensionIndex(m_mosaicIndex); }
51 bool isMosaic() const { return m_mosaicIndex > 0; }
52 // Zoom of the pyramid level the sub-block belongs to, derived from the X dimension.
53 double levelZoom() const { return m_levelZoom; }
54 const cv::Rect& rect() const { return m_rect; }
55 int cziPixelType() const { return m_cziPixelType; }
56 int64_t computeDataOffset(int channel, int z, int t, int r, int s, int i, int b, int h, int v) const;
57 void setupBlock(const SubBlockHeader& subblockHeader, std::vector<DimensionEntryDV>& dimensions);
58 bool isInBlock(int channel, int z, int t, int r, int s, int i, int b, int h, int v) const;
59 int pixelSize() const { return m_pixelSize; }
60 slideio::DataType dataType() const {return m_dataType;};
61 int planeSize() const {return m_planeSize;}
62 uint64_t dataPosition() const {return m_dataPosition;}
63 uint64_t dataSize() const {return m_dataSize;}
64 Compression compression() const {return static_cast<Compression>(m_compression);}
65 const std::vector<Dimension>& dimensions() const {return m_dimensions;}
66 // A sub-block does not name its pyramid level: the level is implied by the ratio of the
67 // stored extent to the logical extent. The writer computes storedSize by rounding
68 // size/downsample (ZEN rounds up, some writers round down), so sub-blocks of one level
69 // share the same raw ratio only if their logical extents are all divisible by the
70 // downsample factor. Snap the ratio to the nominal downsample factor of the level - within
71 // one pixel of storedSize - to make the level independent of that rounding. A ratio that no
72 // integer factor explains - an arbitrarily scaled auxiliary image, for instance - is kept
73 // as it is.
74 static double computeLevelZoom(int size, int storedSize);
75 static std::string blockHeaderString()
76 {
77 std::string header= "Scene0\tSceneN\tZoom\tX\tY\tWidth\tHeight\t"
78 "Ch0\tChN\tZ0\tZN\tT0\tTN"
79 "\tAcq0\tAcqN\tPhase0\tPhaseN\tIll0\tIllN\tRot0\tRotN\tView0\tViewN";
80 return header;
81 }
82 friend std::ostream &operator<<(std::ostream &output, const CZISubBlock &subBlock) {
83 output << subBlock.firstScene() << "\t";
84 output << subBlock.lastScene() << "\t";
85 output << subBlock.m_levelZoom << "\t";
86 output << subBlock.m_rect.x << "\t";
87 output << subBlock.m_rect.y << "\t";
88 output << subBlock.m_rect.width << "\t";
89 output << subBlock.m_rect.height << "\t";
90 output << subBlock.firstChannel() << "\t";
91 output << subBlock.lastChannel() << "\t";
92 output << subBlock.firstZSlice() << "\t";
93 output << subBlock.lastZSlice() << "\t";
94 output << subBlock.firstTFrame() << "\t";
95 output << subBlock.lastTFrame() << "\t";
96 output << subBlock.firstBAcquisition() << "\t";
97 output << subBlock.lastBAcquisition() << "\t";
98 output << subBlock.firstHPhase() << "\t";
99 output << subBlock.lastHPhase() << "\t";
100 output << subBlock.firstIllumination() << "\t";
101 output << subBlock.lastIllumination() << "\t";
102 output << subBlock.firstRotation() << "\t";
103 output << subBlock.lastRotation() << "\t";
104 output << subBlock.firstView() << "\t";
105 output << subBlock.lastView() << "\t";
106 output << std::endl;
107 return output;
108 }
109 private:
110 int firstDimensionIndex(int dimension) const
111 {
112 if(dimension>=0 && dimension<static_cast<int>(m_dimensions.size()))
113 return m_dimensions[dimension].start;
114 return 0;
115 }
116 int lastDimensionIndex(int dimension) const
117 {
118 if (dimension >= 0 && dimension < static_cast<int>(m_dimensions.size()))
119 return (m_dimensions[dimension].start + m_dimensions[dimension].size - 1);
120 return 0;
121 }
122 private:
123 slideio::DataType m_dataType;
124 cv::Rect m_rect;
125 int32_t m_cziPixelType;
126 int32_t m_pixelSize;
127 int32_t m_planeSize;
128 int64_t m_filePosition;
129 int64_t m_dataPosition;
130 int64_t m_dataSize;
131 int32_t m_filePart;
132 int32_t m_compression;
133 int m_channelIndex;
134 int m_zSliceIndex;
135 int m_tFrameIndex;
136 int m_illuminationIndex;
137 int m_bAcquisitionIndex;
138 int m_rotationIndex;
139 int m_sceneIndex;
140 int m_hPhaseIndex;
141 int m_viewIndex;
142 int m_mosaicIndex;
143 double m_levelZoom;
144 std::vector<Dimension> m_dimensions;
145 };
146 typedef std::vector<CZISubBlock> CZISubBlocks;
147}
148
149#if defined(_MSC_VER)
150#pragma warning( pop )
151#endif
Definition: exceptions.hpp:15
Compression
raster data compression enum
Definition: slideio_enums.hpp:12
@ Jpeg
JPEG compression.
@ Uncompressed
Uncompressed data.
@ LZW
Lempel - Ziv - Welch universal lossless data compression algorithm.
@ JpegXR
JPEG XR compression.