SlideIO 2.0.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#ifndef OPENCV_slideio_tools_HPP
5#define OPENCV_slideio_tools_HPP
6
7#if defined(WIN32)
8#elif __APPLE__
9#else
10#include <stdarg.h>
11#include <stddef.h>
12#include <setjmp.h>
13#endif
14#include "slideio/core/slideio_core_def.hpp"
15#include <vector>
16#include <string>
17#include <cmath>
18#include <list>
19#include <opencv2/core.hpp>
20#include "slideio/base/slideio_enums.hpp"
21#include "slideio/base/slideio_structs.hpp"
22
23namespace slideio
24{
25 class SLIDEIO_CORE_EXPORTS Tools
26 {
27 public:
28 struct FileDeleter {
29 void operator()(std::FILE* file) const {
30 if (file) {
31 std::fclose(file);
32 }
33 }
34 };
35 static bool matchPattern(const std::string& path, const std::string& pattern);
36 static bool isConsecutiveFromZero(const std::vector<int>& vec, int size);
37 static std::vector<int> completeChannelList(const std::vector<int>& orgChannelList, int numChannels)
38 {
39 std::vector<int> channelList(orgChannelList);
40 if(channelList.empty())
41 {
42 channelList.resize(numChannels);
43 for(int channel=0; channel<numChannels; ++channel)
44 {
45 channelList[channel] = channel;
46 }
47 }
48 return channelList;
49 }
50 template <typename Functor>
51 static int findZoomLevel(double zoom, int numLevels, Functor zoomFunction)
52 {
53 if(numLevels <= 0) {
54 return -1;
55 }
56 const double baseZoom = zoomFunction(0);
57 if (zoom >= baseZoom)
58 {
59 return 0;
60 }
61 int goodLevelIndex = -1;
62 double lastZoom = baseZoom;
63 for (int levelIndex = 1; levelIndex < numLevels; levelIndex++)
64 {
65 const double currentZoom = zoomFunction(levelIndex);
66 const double absDif = std::abs(currentZoom - zoom);
67 const double relDif = absDif / currentZoom;
68 if (relDif < 0.01)
69 {
70 goodLevelIndex = levelIndex;
71 break;
72 }
73 if (zoom <= lastZoom && zoom > currentZoom)
74 {
75 goodLevelIndex = levelIndex - 1;
76 break;
77 }
78 lastZoom = currentZoom;
79 }
80 if (goodLevelIndex < 0)
81 {
82 goodLevelIndex = numLevels - 1;
83 }
84 return goodLevelIndex;
85 }
86 static void convert12BitsTo16Bits(const uint8_t* source, uint16_t* target, int targetLen);
87 static void scaleRect(const cv::Rect& srcRect, const cv::Size& newSize, cv::Rect& trgRect);
88 static void scaleRect(const cv::Rect& srcRect, double scaleX, double scaleY, cv::Rect& trgRect);
89 static bool isCompleteChannelList(const std::vector<int>& channelIndices, const int numChannels)
90 {
91 bool allChannels = channelIndices.empty();
92 if(!allChannels) {
93 if (channelIndices.size() == numChannels) {
94 allChannels = true;
95 for (int channel = 0; channel < channelIndices.size(); ++channel) {
96 if (channelIndices[channel] != channel) {
97 allChannels = false;
98 break;
99 }
100 }
101 }
102 }
103 return allChannels;
104 }
105#if defined(WIN32)
106 static std::wstring toWstring(const std::string& string);
107#endif
108
109 static std::string fromUnicode16(const std::u16string& u16string);
110 static void throwIfPathNotExist(const std::string& path, const std::string label);
111 static std::list<std::string> findFilesWithExtension(const std::string& directory, const std::string& extension);
112 static void extractChannels(const cv::Mat& sourceRaster, const std::vector<int>& channels, cv::OutputArray output);
113 static FILE* openFile(const std::string& filePath, const char* mode);
114 static uint64_t getFilePos(FILE* file);
115 static int setFilePos(FILE* file, uint64_t pos, int origin);
116 static uint64_t getFileSize(FILE* file);
117 static int dataTypeSize(slideio::DataType dt);
118 static Size cvSizeToSize(const cv::Size& cvSize) {
119 return {cvSize.width, cvSize.height};
120 }
121 static Rect cvRectToRect(const cv::Rect& cvRect) {
122 return {cvRect.x, cvRect.y, cvRect.width, cvRect.height};
123 }
124 static void replaceAll(std::string& str, const std::string& from, const std::string& to);
125 static std::vector<std::string> split(const std::string& value, char delimiter);
126 static std::string randomUUID();
127
128 };
129}
130#endif
Definition: exceptions.hpp:15