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 std::vector<int> completeChannelList(const std::vector<int>& orgChannelList, int numChannels)
37 {
38 std::vector<int> channelList(orgChannelList);
39 if(channelList.empty())
40 {
41 channelList.resize(numChannels);
42 for(int channel=0; channel<numChannels; ++channel)
43 {
44 channelList[channel] = channel;
45 }
46 }
47 return channelList;
48 }
49 template <typename Functor>
50 static int findZoomLevel(double zoom, int numLevels, Functor zoomFunction)
51 {
52 if(numLevels <= 0) {
53 return -1;
54 }
55 const double baseZoom = zoomFunction(0);
56 if (zoom >= baseZoom)
57 {
58 return 0;
59 }
60 int goodLevelIndex = -1;
61 double lastZoom = baseZoom;
62 for (int levelIndex = 1; levelIndex < numLevels; levelIndex++)
63 {
64 const double currentZoom = zoomFunction(levelIndex);
65 const double absDif = std::abs(currentZoom - zoom);
66 const double relDif = absDif / currentZoom;
67 if (relDif < 0.01)
68 {
69 goodLevelIndex = levelIndex;
70 break;
71 }
72 if (zoom <= lastZoom && zoom > currentZoom)
73 {
74 goodLevelIndex = levelIndex - 1;
75 break;
76 }
77 lastZoom = currentZoom;
78 }
79 if (goodLevelIndex < 0)
80 {
81 goodLevelIndex = numLevels - 1;
82 }
83 return goodLevelIndex;
84 }
85 static void convert12BitsTo16Bits(const uint8_t* source, uint16_t* target, int targetLen);
86 static void scaleRect(const cv::Rect& srcRect, const cv::Size& newSize, cv::Rect& trgRect);
87 static void scaleRect(const cv::Rect& srcRect, double scaleX, double scaleY, cv::Rect& trgRect);
88 static bool isCompleteChannelList(const std::vector<int>& channelIndices, const int numChannels)
89 {
90 bool allChannels = channelIndices.empty();
91 if(!allChannels) {
92 if (channelIndices.size() == numChannels) {
93 allChannels = true;
94 for (int channel = 0; channel < channelIndices.size(); ++channel) {
95 if (channelIndices[channel] != channel) {
96 allChannels = false;
97 break;
98 }
99 }
100 }
101 }
102 return allChannels;
103 }
104#if defined(WIN32)
105 static std::wstring toWstring(const std::string& string);
106#endif
107
108 static std::string fromUnicode16(const std::u16string& u16string);
109 static void throwIfPathNotExist(const std::string& path, const std::string label);
110 static std::list<std::string> findFilesWithExtension(const std::string& directory, const std::string& extension);
111 static void extractChannels(const cv::Mat& sourceRaster, const std::vector<int>& channels, cv::OutputArray output);
112 static FILE* openFile(const std::string& filePath, const char* mode);
113 static uint64_t getFilePos(FILE* file);
114 static int setFilePos(FILE* file, uint64_t pos, int origin);
115 static uint64_t getFileSize(FILE* file);
116 static int dataTypeSize(slideio::DataType dt);
117 static Size cvSizeToSize(const cv::Size& cvSize) {
118 return {cvSize.width, cvSize.height};
119 }
120 static Rect cvRectToRect(const cv::Rect& cvRect) {
121 return {cvRect.x, cvRect.y, cvRect.width, cvRect.height};
122 }
123 static void replaceAll(std::string& str, const std::string& from, const std::string& to);
124 static std::vector<std::string> split(const std::string& value, char delimiter);
125
126 };
127}
128#endif
Definition: exceptions.hpp:15