MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlPagedImage.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_PAGED_IMAGE_H
14#define ML_PAGED_IMAGE_H
15
34
35// ML-includes
36#include "mlInitSystemML.h"
38#include "mlMemoryImage.h"
39
41#include "mlModuleInterfaces.h"
42
43#include <mlMemoryBlockHandle.h>
45
46#include <unordered_map>
47#include <vector>
48
50
53class Module;
54class SubImage;
55class PageRequest;
56class TileRequest;
58
59
60//------------------------------------------------------------------------------
64//------------------------------------------------------------------------------
66{
68 struct PageEntry
69 {
71 PageRequest* pendingRequest{};
72 };
73
74 struct ExtendedPageEntry;
75
76public:
79 NO_THREAD_SUPPORT = 0,
80 MULTITHREADED = 1,
81 IO_THREAD = 4,
82 USE_MODULE_THREAD_SUPPORT = -1
83 };
84
86 friend class Host;
87
88 // needed for testing protected API:
92
93 //------------------------------------------------------
96 //------------------------------------------------------
97
105 MLEXPORT PagedImage(Module* module = nullptr, MLint outputIndex = -1);
106
111
114 {
115 return _calculateOutputImageHandler;
116 }
117
121
124 {
125 return _isExternalCalculateOutputImageHandler;
126 }
127
130
133 MLEXPORT void setCalculateOutputImagePropertiesCB(CalculateOutputImagePropertiesCB* callback, void* userData);
134
137 {
138 return _id;
139 }
140
142 inline void updateID()
143 {
144 _id = getUniqueId();
145 }
146
147 //-------------------------------------------------------------------
150 //-------------------------------------------------------------------
156 inline bool isValid() const
157 {
158 return _isValid;
159 }
160
163 inline void setValid(bool flag = true)
164 {
165 _isValid = flag;
166 }
167
170 inline void setInvalid()
171 {
172 _isValid = false;
173 }
174
178
182 {
183 return _updateOfImagePropertiesNeeded;
184 }
185
187 [[nodiscard]]
189
191
192 //------------------------------------------------------
195 //------------------------------------------------------
196
198 MLEXPORT virtual void setImageExtent(const ImageVector& extent);
199
203 inline void setImageExtent(MLint x, MLint y, MLint z = 1, MLint c = 1, MLint t = 1, MLint u = 1)
204 {
205 setImageExtent(ImageVector(x, y, z, c, t, u));
206 }
207
211 inline void setImageExtent(const SubImageBox& box)
212 {
214 }
215
221
224
235 inline void setClampPagesToImageExtent(bool flag = true)
236 {
237 _clampPagesToImageExtent = flag;
238 }
239
241 inline bool getClampPagesToImageExtent() const
242 {
243 return _clampPagesToImageExtent;
244 }
245
248 inline void setPageExtent(MLint x, MLint y, MLint z = 1, MLint c = 1, MLint t = 1, MLint u = 1)
249 {
250 setPageExtent(ImageVector(x, y, z, c, t, u));
251 }
252
254 MLEXPORT void setDataType(MLDataType dataType) override;
255
258
261
264
268
273
278
285
286
287 //------------------------------------------------------
290 //------------------------------------------------------
297 MLEXPORT virtual void setStateInfo(const std::string& info, MLErrorCode errorCode);
298
306
309 MLEXPORT virtual const std::string& getStateInfoString() const;
310
315
316 //---------------------------------------------------------------------------
319 //---------------------------------------------------------------------------
320
326 inline void setBypassIndex(MLint index)
327 {
328 _bypassIndex = index;
329 }
330
333 inline MLint getBypassIndex() const
334 {
335 return _bypassIndex;
336 }
338
339
340 //------------------------------------------------------
343 //------------------------------------------------------
344
346 inline Module* getModule() const
347 {
348 return _module;
349 }
350
353
355 inline MLint getOutputIndex() const
356 {
357 return _outIndex;
358 }
359
362 {
363 return _pageSetExt;
364 }
365
368 {
369 return _pageStride;
370 }
371
374 {
375 return _pageExt;
376 }
377
379 inline MLint getPageID(const ImageVector& voxelPosition) const
380 {
381 return (voxelPosition / _pageExt).dot(_pageStride);
382 }
383
389 {
390 return _memoryImg;
391 }
392 inline const MemoryImage& getMemoryImage() const
393 {
394 return _memoryImg;
395 }
397
403 {
404 _threadSupport = support;
405 }
406
411
415
424
427
430
434
437 MLEXPORT void getPageIDs(std::vector<MLint>& pageIDs, const SubImageBox& subImageBox) const;
438
442 MLEXPORT static void getPageIDs(std::vector<MLint>& pageIDs, const SubImageBox& subImageBox,
444
447
451 {
452 return _pageSetSize;
453 }
454
456
457
458 //---------------------------------------------------------------------------
461 //---------------------------------------------------------------------------
462
472 {
473 _inPlaceIndex = inputIndex;
474 }
475
483 inline MLint getInPlaceIndex() const
484 {
485 return _inPlaceIndex;
486 }
488
489
490 //------------------------------------------------------
498 //------------------------------------------------------
499
506
515
519
534
539
543
546
549
551
552 //------------------------------------------------------
555 //------------------------------------------------------
556
557 //---------------------------------------------------------------------------------
567 //---------------------------------------------------------------------------------
568 MLEXPORT void copyPages(const std::vector<MLint>& pageIDs, SubImage* subImage,
571
572
573 //-------------------------------------------------------------------
576 //-------------------------------------------------------------------
581 [[nodiscard]]
582 MLEXPORT MLErrorCode getTile(SubImageBox location, MLDataType dataType, void** data,
585 void* progressCallbackUserData = nullptr);
586
591 [[nodiscard]]
594 void* progressCallbackUserData = nullptr);
595
597 MLEXPORT void freeTile(void* data);
598
606
608
611
614 {
615 _pageCountLimitToUsePageEntryMap = limit;
616 }
617
620 {
621 return _pageCountLimitToUsePageEntryMap;
622 }
623
624protected:
628
631
634
635private:
637 static MLuint32 getUniqueId();
638
640 MLEXPORT bool arePropertiesLocked() const
641 {
642 return _propertiesLocked;
643 }
644
646 MLEXPORT bool setPropertiesLocked(bool locked);
647
649 inline bool _checkPropertyWriteAccess(const char* methodName)
650 {
651 if (_propertiesLocked) {
652 _showInvalidPropertyWriteAccessError(methodName);
653 }
654 return !_propertiesLocked;
655 }
656
658 void _showInvalidPropertyWriteAccessError(const char* methodName);
659
660 PagedImage(const PagedImage&) = delete;
661 PagedImage& operator=(const PagedImage&) = delete;
662
663
664 //------------------------------------------------------
667 //------------------------------------------------------
672 void _updateAll();
673
674
676 void _allocatePageList();
678
679
680 void static deleteMemoryBlockCallback(void* data, size_t size, void* userData);
681
682 /*
683 * Returns \c true if the registration was successfully; otherwise, it returns \c false.
684 */
685 static bool registerDeletionCallback(const ExtendedPageEntry* entry);
686
687 /*
688 * This returns \c true if the ownership of the entry stays within this class.
689 */
690 static bool deregisterDeletionCallback(const ExtendedPageEntry* entry);
691
692 static ExtendedPageEntry* deletePageEntryFromMapUnsafe(std::unordered_map<MLint, ExtendedPageEntry*>& pageEntryMap,
693 MLint pageId);
694
695 //------------------------------------------------------
698 //------------------------------------------------------
699
701 Module* _module;
702
704 CalculateOutputImageHandler* _calculateOutputImageHandler;
705
707 CalculateOutputImagePropertiesCB* _calculateOutputImageProperties;
709 void* _calculateOutputImagePropertiesUserData;
710
712 bool _isExternalCalculateOutputImageHandler;
713
716 MLint _outIndex;
717
719 ImageVector _pageExt;
720
722 ImageVector _pageStride;
723
725 ImageVector _pageSetExt;
726
728 MLint _pageSetSize;
729
731 std::vector<PageEntry> _pageList;
732
734 std::unordered_map<MLint, ExtendedPageEntry*> _pageEntryMap;
735
737 mutable Mutex _pageAccessMutex;
738
740 bool _pageHandlesAreStoredInList;
741
747 MLint _inPlaceIndex;
748
750 MLint _bypassIndex;
751
753 bool _updateOfImagePropertiesNeeded;
754
756 bool _isValid;
757
761 bool _propertiesLocked;
762
765 bool _clampPagesToImageExtent;
766
769 MemoryImage _memoryImg;
770
772 std::string _stateInfoString;
773
775 MLErrorCode _stateInfoErrorCode;
776
778 MLuint32 _id;
779
781 std::vector<InputSubImageProperties> _inSubImageProps;
782
784 ThreadSupport _threadSupport;
785
788 mutable PagedImageCurrentlyUsedMemoryTracker* _currentlyUsedPageMemoryTracker;
789
791 mutable MLMetaProfilePtr _metaProfilePtr;
793
795 static MLuint32 _staticUniqueId;
796
798 static MLuint32 _pageCountLimitToUsePageEntryMap;
799
800private:
802 static const char* const _pageAllocationMemoryProfileKey;
803 static const char* const _pageCacheHitsCountProfileKey;
804 static const char* const _pageCacheMissesCountProfileKey;
805};
806
808
809// Stream output for std::ostream
810namespace std
811{
812
814 MLEXPORT std::ostream& operator<<(std::ostream& s, const ML_NAMESPACE::PagedImage& pi);
815
816} // namespace std
817
818#endif // of __mlPagedImage_H
The strong handle of a MLMemoryBlock.
The pointer is automatically reset when the metaprofile is destroyed.
The weak handle of a MLMemoryBlock.
Base class for the calculation of pages of an output image (PagedImage) of a Module.
The Host is the central image processing class in the ML.
Definition mlHost.h:116
MLEXPORT void setImageExtent(const ImageVector &extent)
Sets the extent of the (sub)image.
Defines the class InSubImageProps, a simple container for some properties that can be defined for an ...
This class encapsulates basic medical image properties:
MedicalImageProperties & operator=(const MedicalImageProperties &medicalImageProperties)
Assignment operator from an existing edicalImageProperties object to get a copy.
A memory cache for the complete output image of an output connector.
Base class for an image processing module of the ML.
Definition mlModule.h:150
A PageRequest represents the request for the calculation of a single page of a PagedImage.
The class PagedImage, representing a fragmented image that manages properties and data of an image lo...
MLint getInPlaceIndex() const
Returns optimization flag: returns the index of the input image whose input tile is used also as outp...
MLEXPORT void setInputSubImageDataType(int inputIndex, MLDataType dataType)
Sets the dataType for the given inputIndex.
MLEXPORT PagedImage(Module *module=nullptr, MLint outputIndex=-1)
Constructor.
MLEXPORT void setMaxVoxelValue(MLdouble maxval)
Sets the assumed maximum value to maxval. Overridden to perform access check.
MLEXPORT void clearPendingPageRequest(MLint pageID)
Clears a pending page request for the given page ID.
virtual MLEXPORT void setStateInfo(const std::string &info, MLErrorCode errorCode)
Sets the state information for this PagedImage to the string info and the MLErrorCode errorCode.
Module * getModule() const
Returns the associated Module instance if passed to the constructor, NULL otherwise.
MLEXPORT MLErrorCode getTile(SubImageBox location, MLDataType dataType, void **data, const ScaleShiftData &scaleShiftData=ScaleShiftData(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
See Host::getTile( module, outputIndex, location, dataType, data, scaleShiftData).
MLEXPORT void clearPageList()
Removes all pages of the page image and from the ML cache, and all page data chunks are freed if not ...
bool isValid() const
Returns whether image properties are valid and up-to-date.
MLEXPORT MLdouble getInterpolatedValue(const Vector3 &pos, MLint c=0, MLint t=0, MLint u=0, MLdouble outsideFillValue=0)
Returns the trilinearly interpolated image value at a given voxel position pos (center of a voxel is ...
MLEXPORT SubImageBox getPageBox(MLint pageID) const
Returns the rectangular region in the entire image covered by the page with ID pageID.
ImageVector getPageExtent() const
Returns the extent of a page.
MLEXPORT const InputSubImageProperties & getInputSubImageProperties(int inputIndex) const
Returns a const reference to the InputSubImageProperties of the given input image at inputIndex.
MLEXPORT MLMemoryBlockHandle getPage(MLint pageID) const
Returns the pointer to the CachedBuffer containing the data of the page with the ID pageID.
void setInvalid()
Sets image properties invalid, i.e., isValid() returns false afterwards.
MLEXPORT const PagedImage * getInputImage(int inputIndex) const
Returns the input image at inputIndex of the associated Module.
MLEXPORT void setPageExtent(const ImageVector &pageExtent)
Sets the extents of the pages to pageExtent.
MLEXPORT void setMinVoxelValue(MLdouble minval)
Sets the assumed minimum value to minval. Overridden to perform access check.
MLEXPORT MLDataType getInputSubImageDataType(int inputIndex) const
Returns the input subimage data type for the input image at inputIndex.
MLEXPORT ThreadSupport getThreadSupport() const
Returns the current thread support of the paged image.
virtual MLEXPORT void setStateInfoErrorCode(MLErrorCode errorCode)
Sets the state information as MLErrorCode errorCode for this instance.
MLEXPORT void setCalculateOutputImagePropertiesCB(CalculateOutputImagePropertiesCB *callback, void *userData)
Sets the callback and userdata for the calculation of the properties of this PagedImage if they need ...
void setValid(bool flag=true)
Sets image properties to valid/up-to-date, i.e., isValid() returns true afterwards.
MLEXPORT void setUpdateOfImagePropertiesNeeded(bool flag)
Sets whether the update of the image properties is needed, as the ML graph has changed since the last...
MLEXPORT void setExternalCalculateOutputImageHandler(CalculateOutputImageHandler *handler)
Sets the pointer to the output image calculation interface handler, the ownership is NOT passed to th...
ImageVector getPageStride() const
Returns the stride to get from one page to another.
MLEXPORT void removePage(MLint pageID)
This method removes a page with ID pageID from this instance.
static MLEXPORT void setPageCountLimitToUsePageEntryMap(MLuint32 limit)
Sets the limit of number of pages for using the page entry map instead of the page list.
MLuint32 getID()
Returns the current image ID that changes whenever the image changes and that is unique accross all i...
MLEXPORT void copyPages(const std::vector< MLint > &pageIDs, SubImage *subImage, const ScaleShiftData &scaleShiftData=ScaleShiftData()) const
Fills the subImage with contents of the Pages with the IDs pagesIDs The gray values are transformed l...
MLint getOutputIndex() const
Returns associated output index of Module instance if passed to constructor, -1 otherwise.
const MemoryImage & getMemoryImage() const
MLint getPageID(const ImageVector &voxelPosition) const
Returns the ID of that page that contains the image voxel position voxelPosition.
MLEXPORT void setInputSubImageIsReadOnly(int inputIndex, bool readOnly)
Sets the input subimage for the input image at inputIndex to readOnly.
MLEXPORT void setInputSubImageUseMemoryImage(int inputIndex, bool useMemoryImage)
Sets that the input subimage for the input image at inputIndex should use a memory image (this implic...
MLEXPORT void setInputSubImageScaleShift(int inputIndex, const ScaleShiftData &scaleShift)
Sets that the input subimage for the input image at inputIndex should be rescaled with the given scal...
MLEXPORT void setThreadSupport(ThreadSupport support)
Sets the thread support of the paged image to the given value.
bool getClampPagesToImageExtent() const
See setClampPagesToImageExtent().
ML_FRIEND_TEST(PagedImageDeprecationTest, NewAPI)
MLEXPORT void setPage(MLint pageID, const MLMemoryBlockHandle &page)
Sets the given page handle as pagedata at pageID.
MLEXPORT bool updateOfImagePropertiesNeeded() const
Returns whether the update of the image properties is needed, as the ML graph has changed since the l...
void setBypassIndex(MLint index)
Sets the index of the input image whose pages should be used instead of the pages of this image.
CalculateOutputImageHandler * getCalculateOutputImageHandler() const
Returns the pointer to the output image calculation interface.
virtual MLEXPORT void setImageProperties(const PagedImage &imageProperties)
Function to copy all associated characteristics from imageProperties.
MLint getBypassIndex() const
Returns the index of the input image whose pages should be used instead of the pages of this image or...
MLEXPORT ~PagedImage() override
Destructor.
ImageVector getPageSetExtent() const
Returns the extents of the page set covering the entire image extents.
void setImageExtent(MLint x, MLint y, MLint z=1, MLint c=1, MLint t=1, MLint u=1)
Sets the extents of the image to the extent (x,y,z,c,t,u) and clears the list of valid pages and its ...
virtual MLEXPORT const std::string & getStateInfoString() const
Returns the current state information string, which might explain the state of this instance.
static MLEXPORT void getPageIDs(std::vector< MLint > &pageIDs, const SubImageBox &subImageBox, const ImageVector &imageExtent, const ImageVector &pageExtent)
Clears pageIDs and inserts the IDs of all pages that need to be calculated to cover subImageBox compl...
void updateID()
Sets that the image has changed.
MLEXPORT bool getInputSubImageIsReadOnly(int inputIndex) const
Returns whether the input subimage at inputIndex is set to 'read-only'.
MLEXPORT void deletePageEntryFromDeletionCallback(ExtendedPageEntry *entry)
This method removes a page referenced by the page entry.
MLEXPORT MLMetaProfilePtr & getMetaProfile() const
Returns the MLMetaProfile for this paged image. Creates it if necessary.
MLEXPORT PagedImage & getBypassSourceImage()
Returns the reference to the source image of a bypass chain.
bool hasExternalCalculateOutputImageHandler() const
Returns whether the CalculateOutputImageHandler is an external handler and should not be overwritten.
MLEXPORT void setCalculateOutputImageHandler(CalculateOutputImageHandler *handler)
Sets the pointer to the output image calculation interface handler; the ownership is passed to the Pa...
ThreadSupport
Defines the multithreading support of the paged image.
MLEXPORT MLErrorCode getTile(SubImage &subImage, const ScaleShiftData &scaleShiftData=ScaleShiftData(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
See Host::getTile( module, outputIndex, subImg, scaleShiftData).
static MLEXPORT MLuint32 getPageCountLimitToUsePageEntryMap()
Returns the limit of number of pages for using the page entry map instead of the page list.
ML_FRIEND_TEST(PagedImageDeprecationTest, DeprecatedAPI)
MLEXPORT void getPageIDs(std::vector< MLint > &pageIDs, const SubImageBox &subImageBox) const
Clears pageIDs and inserts the IDs of all pages that need to be calculated to cover subImageBox compl...
MLEXPORT MLMemoryBlockHandle getPageOrCreatePageRequestOrAddToPendingRequest(MLint &pageID, TileRequest *tileRequest, PageRequest **newPageRequest)
Returns the valid page handle if it exists.
void setClampPagesToImageExtent(bool flag=true)
If enabled, the pages are clamped to the image extent, ensuring that pages on the border do not exten...
MLEXPORT MLint getNumPages() const
Returns the total number of pages in this output image, regardless of whether they are allocated.
void setPageExtent(MLint x, MLint y, MLint z=1, MLint c=1, MLint t=1, MLint u=1)
Sets the extents of the pages to the extent (x,y,z,c,t,u).
MLEXPORT void calculateImageProperties()
Updates the image properties via the Module::calculateOutputImageProperties or via CalculateOutputIma...
ML_FRIEND_TEST(TypedHandlersTest, SetupAndVerifyProperties)
virtual MLEXPORT MLErrorCode getStateInfoErrorCode() const
Returns the current state information as MLError code, which might explain the state of this instance...
MLEXPORT MLErrorCode updateImageProperties()
Updates the image properties of the PagedImage using the Host::updateImageProperties method.
void setInPlaceIndex(MLint inputIndex)
Sets optimization flag: when calculating a page in calculateOutputSubImage(), the output image page s...
MLEXPORT void initializeInputSubImageProperties(MLint numInputs)
Initializes the InSubImageProps for a given number of inputs; should only be called by the Host!
MLEXPORT void setDataType(MLDataType dataType) override
Sets the type of data to dataType. Overridden to perform access check.
MLEXPORT void clearPageListAndMemoryImage()
Removes all pages of the page image and from the ML cache, and all page data chunks are freed.
MLEXPORT void freeTile(void *data)
Same as corresponding Host::freeTile(void *data).
MLEXPORT std::size_t getCachedPagesSizeInBytes() const
Returns the currently used cache size for pages in bytes.
MLEXPORT void validateInputSubImagePropertiesAfterCalculateOutputImageProperties()
Validates the properties and sets the data type where it was not set; should only be called by the Ho...
MemoryImage & getMemoryImage()
Allows access to a memory-cached output of the paged image.
void setImageExtent(const SubImageBox &box)
Sets the extents of the image to box.getExtent() and updates all PagedImage settings.
virtual MLEXPORT void setImageExtent(const ImageVector &extent)
Sets the extents of the image to extent. The list of valid pages and its content are cleared.
MLEXPORT void setInputSubImagesAreReadOnly(bool readOnly=true)
Sets the read-only flag for all input images.
This class manages/represents a rectangular 6D image region that is organized linearly in memory.
Definition mlSubImage.h:75
VectorType getExtent() const
Returns the extents of the subimage region.
A TileRequest either represents the input subimage that is needed by a PageRequest or if it is a root...
MLint32 MLDataType
MLDataType.
Definition mlTypeDefs.h:592
MLint32 MLErrorCode
Type of an ML Error code.
Definition mlTypeDefs.h:772
boost::mutex Mutex
Defines a non-recursive mutex.
Definition mlMutex.h:39
#define MLEXPORT
To export symbols from a DLL/shared object, we need to mark them with the MLEXPORT symbol.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
unsigned int MLuint32
Definition mlTypeDefs.h:185
void MLRequestProgressCB(void *usrData, double progress)
double MLdouble
Definition mlTypeDefs.h:217
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition mlTypeDefs.h:489
std::ostream & operator<<(std::ostream &out, const ml::Variant &variant)
Definition mlVariant.h:231
void CalculateOutputImagePropertiesCB(void *userData, PagedImage *outImage)
Callback for the calculation of the output image properties for outputImage.
TScaleShiftData< MLdouble > ScaleShiftData
Double version of TScaleShiftData for maximum reasonable precision.
TImageVector< MLint > ImageVector
Defines the standard ImageVector type that is used by the ML for indexing and coordinates.
STL namespace.