Mercurial > hg > orthanc
changeset 1465:905842836ad4
sample Lua script to write DICOM series to disk
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 27 Jul 2015 17:33:47 +0200 |
parents | f401dd90b35e |
children | 0cd0f2ad3599 |
files | Core/Lua/LuaContext.cpp Core/Lua/LuaFunctionCall.cpp OrthancServer/LuaScripting.cpp Resources/Samples/Lua/WriteToDisk.lua |
diffstat | 4 files changed, 42 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/Core/Lua/LuaContext.cpp Fri Jul 03 10:03:40 2015 +0200 +++ b/Core/Lua/LuaContext.cpp Mon Jul 27 17:33:47 2015 +0200 @@ -137,7 +137,7 @@ Json::FastWriter writer; std::string s = writer.write(json); - lua_pushstring(state, s.c_str()); + lua_pushlstring(state, s.c_str(), s.size()); return 1; } @@ -181,7 +181,7 @@ } // Return the result of the HTTP request - lua_pushstring(state, str.c_str()); + lua_pushlstring(state, str.c_str(), str.size()); return true; } @@ -307,7 +307,8 @@ { if (value.isString()) { - lua_pushstring(lua_, value.asCString()); + const std::string s = value.asString(); + lua_pushlstring(lua_, s.c_str(), s.size()); } else if (value.isDouble()) { @@ -356,7 +357,7 @@ it = members.begin(); it != members.end(); ++it) { // Push the index of the cell - lua_pushstring(lua_, it->c_str()); + lua_pushlstring(lua_, it->c_str(), it->size()); // Push the value of the cell PushJson(value[*it]);
--- a/Core/Lua/LuaFunctionCall.cpp Fri Jul 03 10:03:40 2015 +0200 +++ b/Core/Lua/LuaFunctionCall.cpp Mon Jul 27 17:33:47 2015 +0200 @@ -61,7 +61,7 @@ void LuaFunctionCall::PushString(const std::string& value) { CheckAlreadyExecuted(); - lua_pushstring(context_.lua_, value.c_str()); + lua_pushlstring(context_.lua_, value.c_str(), value.size()); } void LuaFunctionCall::PushBoolean(bool value)
--- a/OrthancServer/LuaScripting.cpp Fri Jul 03 10:03:40 2015 +0200 +++ b/OrthancServer/LuaScripting.cpp Mon Jul 27 17:33:47 2015 +0200 @@ -86,7 +86,7 @@ std::string result; if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), uri)) { - lua_pushstring(state, result.c_str()); + lua_pushlstring(state, result.c_str(), result.size()); } else { @@ -133,7 +133,7 @@ HttpToolbox::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), uri, bodyData, bodySize)) { - lua_pushstring(state, result.c_str()); + lua_pushlstring(state, result.c_str(), result.size()); } else {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Resources/Samples/Lua/WriteToDisk.lua Mon Jul 27 17:33:47 2015 +0200 @@ -0,0 +1,34 @@ +TARGET = '/tmp/lua' + +function ToAscii(s) + -- http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub + return s:gsub('[^a-zA-Z0-9-/ ]', '_') +end + +function OnStableSeries(seriesId, tags, metadata) + print('This series is now stable, writing its instances on the disk: ' .. seriesId) + + local instances = ParseJson(RestApiGet('/series/' .. seriesId)) ['Instances'] + local patient = ParseJson(RestApiGet('/series/' .. seriesId .. '/patient')) ['MainDicomTags'] + local study = ParseJson(RestApiGet('/series/' .. seriesId .. '/study')) ['MainDicomTags'] + local series = ParseJson(RestApiGet('/series/' .. seriesId)) ['MainDicomTags'] + + for i, instance in pairs(instances) do + local path = ToAscii(TARGET .. '/' .. + patient['PatientID'] .. ' - ' .. patient['PatientName'] .. '/' .. + study['StudyDate'] .. ' - ' .. study['StudyDescription'] .. '/' .. + series['SeriesDescription']) + + -- Retrieve the DICOM file from Orthanc + local dicom = RestApiGet('/instances/' .. instance .. '/file') + + -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!) + -- http://stackoverflow.com/a/16029744/881731 + os.execute('mkdir -p "' .. path .. '"') + + -- Write to the file + local target = assert(io.open(path .. '/' .. instance .. '.dcm', 'wb')) + target:write(dicom) + target:close() + end +end