# HG changeset patch # User Sebastien Jodogne # Date 1448887821 -3600 # Node ID d10a8164da5fbfefec41a4e60401afe46b257741 # Parent 697ae8d0e287eebaadd8a395ae075d19b88760d0 ensure images returned to plugins are writable diff -r 697ae8d0e287 -r d10a8164da5f Plugins/Engine/OrthancPlugins.cpp --- a/Plugins/Engine/OrthancPlugins.cpp Mon Nov 30 13:16:52 2015 +0100 +++ b/Plugins/Engine/OrthancPlugins.cpp Mon Nov 30 13:50:21 2015 +0100 @@ -1263,6 +1263,25 @@ } + static OrthancPluginImage* ReturnImage(std::auto_ptr& image) + { + // Images returned to plugins are assumed to be writeable. If the + // input image is read-only, we return a copy so that it can be modified. + + if (image->IsReadOnly()) + { + std::auto_ptr copy(new Image(image->GetFormat(), image->GetWidth(), image->GetHeight())); + ImageProcessing::Copy(*copy, *image); + image.reset(NULL); + return reinterpret_cast(copy.release()); + } + else + { + return reinterpret_cast(image.release()); + } + } + + void OrthancPlugins::UncompressImage(const void* parameters) { const _OrthancPluginUncompressImage& p = *reinterpret_cast(parameters); @@ -1296,7 +1315,7 @@ throw OrthancException(ErrorCode_ParameterOutOfRange); } - *(p.target) = reinterpret_cast(image.release()); + *(p.target) = ReturnImage(image); } @@ -1386,7 +1405,7 @@ std::auto_ptr target(new Image(Plugins::Convert(p.targetFormat), source.GetWidth(), source.GetHeight())); ImageProcessing::Convert(*target, source); - *(p.target) = reinterpret_cast(target.release()); + *(p.target) = ReturnImage(target); } @@ -1550,7 +1569,7 @@ throw OrthancException(ErrorCode_InternalError); } - *(p.target) = reinterpret_cast(result.release()); + *(p.target) = ReturnImage(result); } @@ -1964,14 +1983,7 @@ case _OrthancPluginService_GetImageBuffer: { const _OrthancPluginGetImageInfo& p = *reinterpret_cast(parameters); - const ImageAccessor& image = reinterpret_cast(p.image); - - if (image.IsReadOnly()) - { - throw OrthancException(ErrorCode_ReadOnly); - } - - *(p.resultBuffer) = image.GetBuffer(); + *(p.resultBuffer) = reinterpret_cast(p.image)->GetBuffer(); return true; } @@ -2293,7 +2305,7 @@ return reinterpret_cast(pluginImage); } - LOG(WARNING) << "The custom image decoder cannot handle an image, trying with the built-in decoder"; + LOG(WARNING) << "The custom image decoder cannot handle an image, fallback to the built-in decoder"; } }