SlideIO 2.0.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
levelinfo.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/core/slideio_core_def.hpp"
6#include "slideio/base/slideio_structs.hpp"
7#include <cmath>
8
9#if defined(_MSC_VER)
10#pragma warning( push )
11#pragma warning(disable: 4251)
12#endif
13
14namespace slideio
15{
16 class SLIDEIO_CORE_EXPORTS LevelInfo
17 {
18 public:
19 friend std::ostream& operator<<(std::ostream& os, const slideio::LevelInfo& levelInfo)
20 {
21 os << "Level: " << levelInfo.getLevel() << std::endl;
22 os << "Size: " << levelInfo.getSize().width << "x" << levelInfo.getSize().height << std::endl;
23 os << "Scale: " << levelInfo.getScale() << std::endl;
24 os << "Magnification: " << levelInfo.getMagnification() << std::endl;
25 os << "Tile Size: " << levelInfo.getTileSize().width << "x" << levelInfo.getTileSize().height << std::endl;
26 return os;
27 }
28 public:
29 LevelInfo() = default;
30
31 LevelInfo(int level, const Size& size, double scale, double magnification, const Size& tileSize)
32 : m_level(level), m_size(size), m_scale(scale), m_magnification(magnification), m_tileSize(tileSize) {}
33
34 LevelInfo(const LevelInfo& other) {
35 m_level = other.m_level;
36 m_size = other.m_size;
37 m_scale = other.m_scale;
38 m_magnification = other.m_magnification;
39 m_tileSize = other.m_tileSize;
40 }
41
42 LevelInfo& operator=(const LevelInfo& other) {
43 if (this != &other) {
44 m_level = other.m_level;
45 m_size = other.m_size;
46 m_scale = other.m_scale;
47 m_magnification = other.m_magnification;
48 m_tileSize = other.m_tileSize;
49 }
50 return *this;
51 }
52
53 bool operator==(const LevelInfo& other) const {
54 return m_level == other.m_level &&
55 m_size.width == other.m_size.width &&
56 m_size.height == other.m_size.height &&
57 std::fabs(m_scale - other.m_scale) < 1.e-2 &&
58 std::fabs(m_magnification - other.m_magnification) < 1.e-2 &&
59 m_tileSize.width == other.m_tileSize.width &&
60 m_tileSize.height == other.m_tileSize.height;
61 }
62
63 void updateTileCount() const {
64 if (getTileSize().width > 0 && getTileSize().height > 0) {
65 const int tilesX = (getSize().width - 1) / getTileSize().width + 1;
66 const int tilesY = (getSize().height - 1) / getTileSize().height + 1;
67 m_tileCount = tilesX * tilesY;
68 }
69 else {
70 m_tileCount = 1;
71 }
72 }
73
74 int getLevel() const { return m_level; }
75 void setLevel(int level) { m_level = level; }
76
77 Size getSize() const { return m_size; }
78 void setSize(const Size& size) { m_size = size; }
79
80 double getScale() const { return m_scale; }
81 void setScale(double scale) { m_scale = scale; }
82
83 double getMagnification() const { return m_magnification; }
84 void setMagnification(double magnification) { m_magnification = magnification; }
85
86 Size getTileSize() const { return m_tileSize; }
87 void setTileSize(const Size& tileSize) { m_tileSize = tileSize; }
88
89 int getTileCount() const {
90 if (m_tileCount < 1)
91 updateTileCount();
92 return m_tileCount;
93 }
94
95 std::string toString() const;
96
97 Rect getTileRect(int tileIndex) const {
98 Rect tileRect;
99 const int tileCount = getTileCount();
100 if (tileCount > 1) {
101 const int tilesX = (m_size.width - 1) / m_tileSize.width + 1;
102 const int tilesY = (m_size.height - 1) / m_tileSize.height + 1;
103 const int tileY = tileIndex / tilesX;
104 const int tileX = tileIndex % tilesX;
105 tileRect.x = tileX * m_tileSize.width;
106 tileRect.y = tileY * m_tileSize.height;
107 tileRect.width = m_tileSize.width;
108 tileRect.height = m_tileSize.height;
109 }
110 else {
111 tileRect.x = 0;
112 tileRect.y = 0;
113 tileRect.width = m_size.width;
114 tileRect.height = m_size.height;
115 }
116 return tileRect;
117 }
118
119 private:
120 int m_level = 0;
121 Size m_size;
122 double m_scale = 0.0;
123 double m_magnification = 0.0;
124 Size m_tileSize;
125 mutable int m_tileCount = 0;
126 };
127}
128
129#if defined(_MSC_VER)
130#pragma warning( pop )
131#endif
Definition: exceptions.hpp:15