# Custom ImageIOBase class for loading existing buffer through image file reader

**URL:** <https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419>\
**Category:** Uncategorized\
**Created:** [November 14, 2017, 8:35pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419 "2017-11-14T20:35:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyoll](https://discourse.itk.org/user_avatar/discourse.itk.org/dyoll/32/2311_2.png) [@dyoll](https://discourse.itk.org/u/dyoll)\
**Post date:** [November 14, 2017, 8:35pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/1 "2017-11-14T20:35:27Z")

</div>

Hi

I would like to create an ImageIO class, which permits to load images from an image “cache” through the image file reader, and storing it back in the cache via the image file writer.

Currently I am stumbling over the problem that the ImageFileReader is allocating the memory used by the ImageIOBase derived class to read into. Is there a mechanism, by which the ImageIOBase class could provide the buffer?

The reason I am investigating this problem, is because it might allow to integrate (command-) tools from the ANTs library in our application.

Thanks for any suggestions,  
Bryn

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [November 14, 2017, 11:18pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/2 "2017-11-14T23:18:12Z")

</div>

Hi,

This is an interesting idea that you explaining here. Would you plan to delegate to loading to a “real” IO if your cache does not know your image? Is your cache suppose to be an interface between the Reader and the current IOs, or is it suppose to be independent?

Concerning your question, I don’t think there is a mechanism now in ITK to not allocate the memory in the reader (more specifically in the ImageSource which the Reader derives from). Maybe it is something we can consider if there is a good reason to do so.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 14, 2017, 11:21pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/3 "2017-11-14T23:21:04Z")

</div>

Have you looked at the ImportImageFilter?  
[https://itk.org/Doxygen/html/classitk\_1\_1ImportImageFilter.html](https://itk.org/Doxygen/html/classitk_1_1ImportImageFilter.html)

That is typically used in this situation. Any reason it does not meet your needs?

---

<div class="post-metadata">

**Author:** ![dyoll](https://discourse.itk.org/user_avatar/discourse.itk.org/dyoll/32/2311_2.png) [@dyoll](https://discourse.itk.org/u/dyoll)\
**Post date:** [November 15, 2017, 6:08am UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/4 "2017-11-15T06:08:45Z")

</div>

The reason why I don’t want to use the ImportImageFilter or similar approach, is that I want to integrate command line-like tools from ANTs (Advanced Normalization Tools). These are written with as functions with an api like int Foo(int argc, char\*\*argv). Typically, they load data using the ImageFileReader.

I would like to easily integrate the tools without modification, so if there is a new release in 10 months, I don’t need to redo the same work again.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 15, 2017, 2:55pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/5 "2017-11-15T14:55:30Z")

</div>

The fact that buffer is allocated for the image to be read does not prevent you from writing a custom IO type. The only effect that might arise is increased memory usage.

Once the buffer is allocated, your custom IO could simply copy the memory from your cache into that buffer.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 15, 2017, 3:08pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/6 "2017-11-15T15:08:11Z")

</div>

3D Slicer has an itk ImageIO which the command line modules utilize to get an image from either in memory (MRML scene) or from a file. It sounds like you want to do a similar thing. Here is the class:

> <https://github.com/Slicer/Slicer/blob/master/Libs/MRML/IDImageIO/itkMRMLIDImageIO.h>

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [November 15, 2017, 3:21pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/7 "2017-11-15T15:21:43Z")

</div>

The link @blowekamp shared is great. I was talking with some Slicer developers to know where this mechanism was implemented in Slicer. In their case, they copy the memory (see their `Read()` function), which might be better anyway to avoid overwriting your input image, depending on how the function you are calling is implemented.

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [November 15, 2017, 3:50pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/8 "2017-11-15T15:50:25Z")

</div>

To allow the IO to manage the memory, one would need to modify `itkImageFileReader`. A way of doing so would be to add a method to `ImageIOBase` to interrogate the IO if it manages the memory or not. If it does (default/current), then `itkImageFileReader` would manage and allocate the memory. Otherwise, the IO would be responsible. I haven’t thought about all the implications this may have and if that would integrate nicely with the current mechanism. As I said earlier, it may be better to always copy the buffer anyway, to avoid problems.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 15, 2017, 3:58pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/9 "2017-11-15T15:58:27Z")

</div>

Historically, Slicer had an option for the ImageIO to reuse the buffer, but that was dropped some where between 3.20 and 4.0. There were some problems with the feature because an ITK filters could always run “InPlace” and “steal” the buffer and then free it, unbeknownst to the owner of the buffer.

See about:  
2ffb32de2f890e2052eee57ee6f36d10471d8c0c

The performance of a coping memory on modern system is very high. As long as the system has the memory available then its not work the problems that sharing memory can create.

---

<div class="post-metadata">

**Author:** ![dyoll](https://discourse.itk.org/user_avatar/discourse.itk.org/dyoll/32/2311_2.png) [@dyoll](https://discourse.itk.org/u/dyoll)\
**Post date:** [November 15, 2017, 4:23pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/10 "2017-11-15T16:23:20Z")

</div>

thanks for all the good answers. its true that for my application the only worry I might have is increased memory consumption.

---

<div class="post-metadata">

**Author:** ![bill.lorensen](https://discourse.itk.org/user_avatar/discourse.itk.org/bill.lorensen/32/66_2.png) [@bill.lorensen](https://discourse.itk.org/u/bill.lorensen)\
**Post date:** [November 15, 2017, 4:55pm UTC](https://discourse.itk.org/t/custom-imageiobase-class-for-loading-existing-buffer-through-image-file-reader/419/11 "2017-11-15T16:55:10Z")

</div>

Look at the FileFreeImageIO test in IO/ImageBase.
