SlideIO 2.10.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
tools.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#if defined(WIN32)
7#elif __APPLE__
8#else
9#include <stdarg.h>
10#include <stddef.h>
11#include <setjmp.h>
12#endif
13#include "slideio/core/slideio_core_def.hpp"
14#include <vector>
15#include <string>
16#include <optional>
17#include <cmath>
18#include <list>
19#include <opencv2/core.hpp>
20#include <opencv2/imgproc.hpp>
21#include "slideio/core/slideio_enums.hpp"
22#include "slideio/core/slideio_structs.hpp"
23
24namespace slideio
25{
26 class SLIDEIO_CORE_EXPORTS Tools
27 {
28 public:
29 struct FileDeleter {
30 void operator()(std::FILE* file) const {
31 if (file) {
32 std::fclose(file);
33 }
34 }
35 };
36 static bool matchPattern(const std::string& path, const std::string& pattern);
37 static bool isConsecutiveFromZero(const std::vector<int>& vec, int size);
38 static std::vector<int> completeChannelList(const std::vector<int>& orgChannelList, int numChannels)
39 {
40 std::vector<int> channelList(orgChannelList);
41 if(channelList.empty())
42 {
43 channelList.resize(numChannels);
44 for(int channel=0; channel<numChannels; ++channel)
45 {
46 channelList[channel] = channel;
47 }
48 }
49 return channelList;
50 }
51 template <typename Functor>
52 static int findZoomLevel(double zoom, int numLevels, Functor zoomFunction)
53 {
54 if(numLevels <= 0) {
55 return -1;
56 }
57 const double baseZoom = zoomFunction(0);
58 if (zoom >= baseZoom)
59 {
60 return 0;
61 }
62 int goodLevelIndex = -1;
63 double lastZoom = baseZoom;
64 for (int levelIndex = 1; levelIndex < numLevels; levelIndex++)
65 {
66 const double currentZoom = zoomFunction(levelIndex);
67 const double absDif = std::abs(currentZoom - zoom);
68 const double relDif = absDif / currentZoom;
69 if (relDif < 0.01)
70 {
71 goodLevelIndex = levelIndex;
72 break;
73 }
74 if (zoom <= lastZoom && zoom > currentZoom)
75 {
76 goodLevelIndex = levelIndex - 1;
77 break;
78 }
79 lastZoom = currentZoom;
80 }
81 if (goodLevelIndex < 0)
82 {
83 goodLevelIndex = numLevels - 1;
84 }
85 return goodLevelIndex;
86 }
87 static void convert12BitsTo16Bits(const uint8_t* source, uint16_t* target, int targetLen);
88 static void scaleRect(const cv::Rect& srcRect, const cv::Size& newSize, cv::Rect& trgRect);
89 static void scaleRect(const cv::Rect& srcRect, double scaleX, double scaleY, cv::Rect& trgRect);
90 static bool isCompleteChannelList(const std::vector<int>& channelIndices, const int numChannels)
91 {
92 bool allChannels = channelIndices.empty();
93 if(!allChannels) {
94 if (channelIndices.size() == numChannels) {
95 allChannels = true;
96 for (int channel = 0; channel < channelIndices.size(); ++channel) {
97 if (channelIndices[channel] != channel) {
98 allChannels = false;
99 break;
100 }
101 }
102 }
103 }
104 return allChannels;
105 }
106#if defined(WIN32)
107 static std::wstring toWstring(const std::string& string);
108#endif
109
110 static std::string fromUnicode16(const std::u16string& u16string);
111 static void throwIfPathNotExist(const std::string& path, const std::string label);
120 static std::optional<double> parseIso8601(const std::string& text);
130 static std::optional<int64_t> parseTiffDateTime(const std::string& text);
139 static std::optional<double> parseDicomDateTime(const std::string& date,
140 const std::string& time,
141 const std::string& zoneOffset = std::string());
142 static std::list<std::string> findFilesWithExtension(const std::string& directory, const std::string& extension);
143 static void extractChannels(const cv::Mat& sourceRaster, const std::vector<int>& channels, cv::OutputArray output);
144 static FILE* openFile(const std::string& filePath, const char* mode);
145 static uint64_t getFilePos(FILE* file);
146 static int setFilePos(FILE* file, uint64_t pos, int origin);
147 static uint64_t getFileSize(FILE* file);
148 static int dataTypeSize(slideio::DataType dt);
149 static Size cvSizeToSize(const cv::Size& cvSize) {
150 return {cvSize.width, cvSize.height};
151 }
152 static Rect cvRectToRect(const cv::Rect& cvRect) {
153 return {cvRect.x, cvRect.y, cvRect.width, cvRect.height};
154 }
155 static void replaceAll(std::string& str, const std::string& from, const std::string& to);
156 static std::vector<std::string> split(const std::string& value, char delimiter);
157 static std::string randomUUID();
158 static void resize( cv::InputArray src, cv::OutputArray dst, cv::Size dsize,
159 int interpolation = cv::INTER_LINEAR);
160 };
161}
Definition: converter.hpp:18