SlideIO 2.10.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
ndpifile.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
7#if defined(_MSC_VER)
8#pragma warning( push )
9#pragma warning(disable: 4251)
10#endif
11#include <memory>
12#include <string>
13
14#include "ndpitifftools.hpp"
15#include "ndpitiffkeeper.hpp"
16#include "slideio/core/exceptions.hpp"
17#include "slideio/core/tools/contextpool.hpp"
18#include "slideio/core/tools/readcontext.hpp"
19
20namespace libtiff
21{
22 struct tiff;
23 typedef tiff TIFF;
24}
25
26namespace slideio
27{
28 // The NDPI counterpart of e.g. SCNReadContext: one owning handle. NDPITIFFKeeper's
29 // path-taking constructor opens the file and this throws if that fails, so no
30 // null-handle guard is needed at any acquireContext() call site.
31 class NDPIReadContext : public ReadContext
32 {
33 public:
34 explicit NDPIReadContext(const std::string& filePath) : keeper(filePath) {
35 if (!keeper.isValid()) {
36 RAISE_RUNTIME_ERROR << "NDPIImageDriver: cannot open file " << filePath;
37 }
38 }
39 NDPITIFFKeeper keeper;
40 };
41
42 class SLIDEIO_NDPI_EXPORTS NDPIFile
43 {
44 public:
45 NDPIFile(){
46 }
47 ~NDPIFile();
48 void init(const std::string& filePath);
49 const std::vector<NDPITiffDirectory>& directories() const {
50 return m_directories;
51 }
52 const std::string getFilePath() const {
53 return m_filePath;
54 }
59 ContextPool::Borrow acquireContext() { return pool().acquire(); }
65 int contextCount() const { return pool().contextCount(); }
66 const NDPITiffDirectory& findZoomDirectory(double zoom, int sceneWidth, int dirBegin, int dirEnd);
67 private:
68 void scanFile();
69 // The pool exists only after init() has supplied a path. No live path reaches
70 // either accessor before that -- scenes are constructed from an NDPIFile whose
71 // init() has already returned -- but the shape invites one, so the null is
72 // reported as an error rather than dereferenced.
73 ContextPool& pool() const {
74 if (!m_contextPool) {
75 RAISE_RUNTIME_ERROR << "NDPIFile: the file is not initialized";
76 }
77 return *m_contextPool;
78 }
79 private:
80 std::string m_filePath;
81 std::vector<NDPITiffDirectory> m_directories;
82 // A plain ContextPool member can't be initialized until filePath is known, and
83 // NDPIFile is default-constructed well before that (init() supplies the path
84 // later) -- so the pool is built in init(), not in the constructor init list.
85 //
86 // Declared last on purpose, and it must stay after m_directories.
87 // ~ContextPool blocks until every outstanding borrow is returned, and
88 // members are destroyed in reverse declaration order, so this ordering
89 // is what keeps m_directories alive for an in-flight NDPIScene::readTile
90 // -- which reads m_pfile->directories() -- until that read has finished.
91 std::unique_ptr<ContextPool> m_contextPool;
92 };
93}
94
95
96#if defined(_MSC_VER)
97#pragma warning( pop )
98#endif
Definition: converter.hpp:18