Skip to content

Commit

Permalink
Add __enter__ and __exit__ for CuImage
Browse files Browse the repository at this point in the history
  • Loading branch information
gigony committed Sep 28, 2021
1 parent 9a4170d commit 1c5ed8f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cpp/include/cucim/cuimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class EXPORT_VISIBLE CuImage : public std::enable_shared_from_this<CuImage>

void save(std::string file_path) const;

void close();

private:
using Mutex = std::mutex;
using ScopedLock = std::scoped_lock<Mutex>;
Expand Down
25 changes: 21 additions & 4 deletions cpp/src/cuimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,7 @@ CuImage::CuImage() : std::enable_shared_from_this<CuImage>()
CuImage::~CuImage()
{
// printf("[cuCIM] CuImage::~CuImage()\n");
if (file_handle_.client_data)
{
image_formats_->formats[0].image_parser.close(&file_handle_);
}
close();
image_formats_ = nullptr; // memory release is handled by the framework
if (image_metadata_)
{
Expand Down Expand Up @@ -615,6 +612,10 @@ CuImage CuImage::read_region(std::vector<int64_t>&& location,
// Read region from internal file if image_data_ is nullptr
if (image_data_ == nullptr)
{
if (file_handle_.fd < 0) // file_handle_ is not opened
{
throw std::runtime_error("[Error] The image file is closed!");
}
if (!image_formats_->formats[0].image_reader.read(
&file_handle_, image_metadata_, &request, image_data, nullptr /*out_metadata*/))
{
Expand Down Expand Up @@ -778,6 +779,10 @@ std::set<std::string> CuImage::associated_images() const

CuImage CuImage::associated_image(const std::string& name, const io::Device& device) const
{
if (file_handle_.fd < 0) // file_handle_ is not opened
{
throw std::runtime_error("[Error] The image file is closed!");
}
auto it = associated_images_.find(name);
if (it != associated_images_.end())
{
Expand Down Expand Up @@ -855,6 +860,18 @@ void CuImage::save(std::string file_path) const
fs.close();
}
}

void CuImage::close()
{
if (file_handle_.client_data)
{
image_formats_->formats[0].image_parser.close(&file_handle_);
}
file_handle_.cufile = nullptr;
file_handle_.path = nullptr;
file_handle_.fd = -1;
}

void CuImage::ensure_init()
{
ScopedLock g(mutex_);
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/filesystem/file_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "cucim/codec/hash_function.h"

CuCIMFileHandle::CuCIMFileHandle()
: fd(0),
: fd(-1),
cufile(nullptr),
type(FileHandleType::kUnknown),
path(nullptr),
Expand Down
14 changes: 14 additions & 0 deletions python/pybind11/cucim_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,21 @@ PYBIND11_MODULE(_cucim, m)
py::arg("name") = "", //
py::arg("device") = io::Device()) //
.def("save", &CuImage::save, doc::CuImage::doc_save, py::call_guard<py::gil_scoped_release>()) //
.def("close", &CuImage::close, doc::CuImage::doc_close, py::call_guard<py::gil_scoped_release>()) //
.def("__bool__", &CuImage::operator bool, py::call_guard<py::gil_scoped_release>()) //
.def(
"__enter__",
[](const std::shared_ptr<CuImage>& cuimg) { //
return cuimg; //
}, //
py::call_guard<py::gil_scoped_release>())
.def(
"__exit__",
[](const std::shared_ptr<CuImage>& cuimg, const py::object& type, const py::object& value,
const py::object& traceback) { //
cuimg->close(); //
}, //
py::call_guard<py::gil_scoped_release>())
.def(
"__repr__", //
[](const CuImage& cuimg) { //
Expand Down
9 changes: 9 additions & 0 deletions python/pybind11/cucim_pydoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ Saves image data to the file path.
Currently it supports only .ppm file format that can be viewed by `eog` command in Ubuntu.
)doc")

// void close();
PYDOC(close, R"doc(
Closes the file handle.
Once the file handle is closed, the image object (if loaded before) still exists but cannot read additional images
from the file.
)doc")


// void _set_array_interface(const CuImage& cuimg);
PYDOC(_set_array_interface, R"doc(
Add `__array_interface__` or `__cuda_array_interface__` depending on the memory type.
Expand Down

0 comments on commit 1c5ed8f

Please sign in to comment.