SlideIO 2.0.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
ndpitifftools.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_ndpitifftools_HPP
5#define OPENCV_slideio_ndpitifftools_HPP
6
7
8#include "slideio/drivers/ndpi/ndpi_api_def.hpp"
9#include "slideio/core/cvstructs.hpp"
10#include "slideio/base/slideio_enums.hpp"
11#include "slideio/base/base.hpp"
12#include <opencv2/core.hpp>
13#include <string>
14#include <vector>
15
16namespace libtiff
17{
18 struct tiff;
19 typedef tiff TIFF;
20}
21
22#if defined(_MSC_VER)
23#pragma warning( push )
24#pragma warning(disable: 4251)
25#endif
26
27namespace slideio
28{
29 class NDPIFile;
30 class CacheManager;
31
32 struct SLIDEIO_NDPI_EXPORTS NDPITiffDirectory
33 {
34 enum class Type
35 {
36 Tiled = 0,
37 SingleStripe = 1,
38 SingleStripeMCU = 2,
39 Striped = 3
40 };
41 int width;
42 int height;
43 bool tiled;
44 int tileWidth;
45 int tileHeight;
46 int channels;
47 int bitsPerSample;
48 int photometric;
49 int YCbCrSubsampling[2];
50 uint32_t compression;
51 Compression slideioCompression;
52 int dirIndex;
53 int64 offset;
54 std::string description;
55 std::string userLabel;
56 std::string comments;
57 std::vector<NDPITiffDirectory> subdirectories;
58 Resolution res;
59 cv::Point2d position;
60 bool interleaved;
61 int rowsPerStrip;
62 DataType dataType;
63 int stripSize;
64 double magnification;
65 uint32_t blankLines;
66 std::vector<uint32_t> mcuStarts;
67 uint64_t jpegHeaderOffset;
68 uint64_t jpegSOFMarker;
69 uint32_t jpegHeaderSize;
70 uint32_t rawStripSize = 0;
71 bool auxImage = false;
72
73 Type getType() const {
74 if(tiled) {
75 return Type::Tiled;
76 }
77 else if(tileWidth > 0 && tileHeight > 0 && !mcuStarts.empty() && slideioCompression == Compression::Jpeg) {
78 return Type::SingleStripeMCU;
79 }
80 else if(rowsPerStrip == height) {
81 return Type::SingleStripe;
82 }
83 else {
84 return Type::Striped;
85 }
86 }
87 };
88
89 SLIDEIO_NDPI_EXPORTS std::ostream& operator << (std::ostream& os, const NDPITiffDirectory::Type& type);;
90
91 class SLIDEIO_NDPI_EXPORTS NDPITiffTools
92 {
93 public:
94 static libtiff::TIFF* openTiffFile(const std::string& path);
95 static void closeTiffFile(libtiff::TIFF* file);
96 static cv::Size computeMCUTileSize(FILE* file, const cv::Size& dirSize);
97 static std::pair<uint64_t, uint64_t> getJpegHeaderPos(FILE* file);
98 static void readMCUTile(FILE* file, const NDPITiffDirectory& dir, int tile, cv::OutputArray output);
99 static void jpeglibDecodeTile(const uint8_t* jpg_buffer, size_t jpg_size, const cv::Size& tileSize, cv::OutputArray output);
100 static void scanTiffDirTags(libtiff::TIFF* tiff, int dirIndex, int64_t dirOffset, slideio::NDPITiffDirectory& dir);
101 static void updateJpegXRCompressedDirectoryMedatata(libtiff::TIFF* tiff, NDPITiffDirectory& dir);
102 static void scanTiffDir(libtiff::TIFF* tiff, int dirIndex, int64_t dirOffset, slideio::NDPITiffDirectory& dir);
103 static void readNotRGBStripedDir(libtiff::TIFF* tiff, const NDPITiffDirectory& dir, cv::_OutputArray output);
104 static void readRegularStripedDir(libtiff::TIFF* file, const slideio::NDPITiffDirectory& dir, cv::OutputArray output);
105 static void readJpegXRStripedDir(libtiff::TIFF* tiff, const NDPITiffDirectory& dir, cv::_OutputArray output);
106 static void readStripedDir(libtiff::TIFF* file, const slideio::NDPITiffDirectory& dir, cv::OutputArray output);
107 static void readJpegXRTile(libtiff::TIFF* tiff, const slideio::NDPITiffDirectory& dir, int tile, const std::vector<int>& vector, cv::OutputArray output);
108 static void readTile(libtiff::TIFF* hFile, const slideio::NDPITiffDirectory& dir, int tile,
109 const std::vector<int>& channelIndices, cv::OutputArray output);
110 static void readJpegXRStrip(libtiff::TIFF* tiff, const NDPITiffDirectory& dir, int strip, const std::vector<int>& vector, cv::_OutputArray output);
111 static void readNotRGBStrip(libtiff::TIFF* tiff, const NDPITiffDirectory& dir, int strip, const std::vector<int>& vector, cv::_OutputArray output);
112 static void readRegularStrip(libtiff::TIFF* tiff, const NDPITiffDirectory& dir, int strip, const std::vector<int>& vector, cv::_OutputArray output);
113 static void readStripe(libtiff::TIFF* hFile, const slideio::NDPITiffDirectory& dir, int strip,
114 const std::vector<int>& channelIndices, cv::OutputArray output);
115 static void setCurrentDirectory(libtiff::TIFF* hFile, const slideio::NDPITiffDirectory& dir);
116 static void decodeJxrBlock(const uint8_t* data, size_t dataBlockSize, cv::OutputArray output);
117 static void readRegularTile(libtiff::TIFF* hFile, const slideio::NDPITiffDirectory& dir, int tile,
118 const std::vector<int>& channelIndices, cv::OutputArray output);
119 static void readNotRGBTile(libtiff::TIFF* hFile, const slideio::NDPITiffDirectory& dir, int tile,
120 const std::vector<int>& channelIndices, cv::OutputArray output);
121 static int computeStripHeight(int height, int rowsPerStrip, int strip);
122 static cv::Size computeTileSize(const NDPITiffDirectory& dir, int tile);
123 static cv::Size computeTileCounts(const NDPITiffDirectory& dir);
124 static void readJpegScanlines(libtiff::TIFF* tiff, FILE* file, const NDPITiffDirectory& dir, int firstScanline,
125 int numberScanlines, const std::vector<int>& channelIndices, cv::_OutputArray output);
126 static void cacheScanlines(NDPIFile* file, const NDPITiffDirectory& dir,
127 cv::Size tileSize, CacheManager* cacheManager);
128 static void readJpegDirectoryRegion(libtiff::TIFF* tiff, const std::string& filePath, const cv::Rect& region, const NDPITiffDirectory& dir,
129 const std::vector<int>& channelIndices, cv::_OutputArray output);
130 static void readDirectoryJpegHeaders(NDPIFile* ndpi, NDPITiffDirectory& dir);
131 static void readUncompressedScanlines(libtiff::TIFF* tiff, FILE* file, const NDPITiffDirectory& dir, int firstScanline, int numberScanlines, const std::vector<int>& vector,
132 cv::_OutputArray tileRaster);
133 private:
134 static void fixJpegHeader(const NDPITiffDirectory& dir, uint8_t* data);
135 };
136
137 class NDPITIFFKeeper
138 {
139 public:
140 NDPITIFFKeeper(libtiff::TIFF* hfile=nullptr);
141 ~NDPITIFFKeeper();
142 libtiff::TIFF* getHandle() const{
143 return m_hFile;
144 }
145 bool isValid() const{
146 return getHandle() != nullptr;
147 }
148 operator libtiff::TIFF* () const {
149 return getHandle();
150 }
151 NDPITIFFKeeper& operator = (libtiff::TIFF* hFile){
152 m_hFile = hFile;
153 return *this;
154 }
155
156 private:
157 libtiff::TIFF* m_hFile;
158 };
159}
160
161#if defined(_MSC_VER)
162#pragma warning( pop )
163#endif
164
165#endif
Definition: exceptions.hpp:12
Compression
raster data compression enum
Definition: slideio_enums.hpp:12