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
22namespace slideio
23{
24 class SLIDEIO_CORE_EXPORTS Tools
25 {
26 public:
27 struct FileDeleter {
28 void operator()(std::FILE* file) const {
29 if (file) {
30 std::fclose(file);
31 }
32 }
33 };
34 static bool matchPattern(const std::string& path, const std::string& pattern);
35 static std::vector<int> completeChannelList(const std::vector<int>& orgChannelList, int numChannels)
36 {
37 std::vector<int> channelList(orgChannelList);
38 if(channelList.empty())
39 {
40 channelList.resize(numChannels);
41 for(int channel=0; channel<numChannels; ++channel)
42 {
43 channelList[channel] = channel;
44 }
45 }
46 return channelList;
47 }
48 template <typename Functor>
49 static int findZoomLevel(double zoom, int numLevels, Functor zoomFunction)
50 {
51 const double baseZoom = zoomFunction(0);
52 if (zoom >= baseZoom)
53 {
54 return 0;
55 }
56 int goodLevelIndex = -1;
57 double lastZoom = baseZoom;
58 for (int levelIndex = 1; levelIndex < numLevels; levelIndex++)
59 {
60 const double currentZoom = zoomFunction(levelIndex);
61 const double absDif = std::abs(currentZoom - zoom);
62 const double relDif = absDif / currentZoom;
63 if (relDif < 0.01)
64 {
65 goodLevelIndex = levelIndex;
66 break;
67 }
68 if (zoom <= lastZoom && zoom > currentZoom)
69 {
70 goodLevelIndex = levelIndex - 1;
71 break;
72 }
73 lastZoom = currentZoom;
74 }
75 if (goodLevelIndex < 0)
76 {
77 goodLevelIndex = numLevels - 1;
78 }
79 return goodLevelIndex;
80 }
81 static void convert12BitsTo16Bits(uint8_t* source, uint16_t* target, int targetLen);
82 static void scaleRect(const cv::Rect& srcRect, const cv::Size& newSize, cv::Rect& trgRect);
83 static void scaleRect(const cv::Rect& srcRect, double scaleX, double scaleY, cv::Rect& trgRect);
84 static bool isCompleteChannelList(const std::vector<int>& channelIndices, const int numChannels)
85 {
86 bool allChannels = channelIndices.empty();
87 if(!allChannels) {
88 if (channelIndices.size() == numChannels) {
89 allChannels = true;
90 for (int channel = 0; channel < channelIndices.size(); ++channel) {
91 if (channelIndices[channel] != channel) {
92 allChannels = false;
93 break;
94 }
95 }
96 }
97 }
98 return allChannels;
99 }
100 static std::wstring toWstring(const std::string& string);
101 static std::string fromWstring(const std::wstring& wstring);
102 static std::string fromUnicode16(const std::u16string& u16string);
103 static void throwIfPathNotExist(const std::string& path, const std::string label);
104 static std::list<std::string> findFilesWithExtension(const std::string& directory, const std::string& extension);
105 static void extractChannels(const cv::Mat& sourceRaster, const std::vector<int>& channels, cv::OutputArray output);
106 static FILE* openFile(const std::string& filePath, const char* mode);
107 // Function to detect if the system is little endian
108 static bool isLittleEndian() {
109 uint16_t number = 0x1;
110 char* numPtr = (char*)&number;
111 return (numPtr[0] == 1);
112 }
113 // Function to convert from big endian to little endian for 16 bit short
114 static uint16_t bigToLittleEndian16(uint16_t bigEndianValue) {
115 return ((bigEndianValue >> 8) & 0xff) |
116 ((bigEndianValue << 8) & 0xff00);
117 }
118 static uint64_t getFilePos(FILE* file);
119 static int setFilePos(FILE* file, uint64_t pos, int origin);
120 static uint64_t getFileSize(FILE* file);
121
122 };
123}
124#endif
Definition: exceptions.hpp:12