changeset 151:b6b207b1de0d Orthanc-1.4.1

missing file
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 17 Jul 2018 11:24:21 +0200
parents 4a6b2e036300
children ed9fec2876ae
files Database/Lua/Jpeg2000Conversion.lua
diffstat 1 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Database/Lua/Jpeg2000Conversion.lua	Tue Jul 17 11:24:21 2018 +0200
@@ -0,0 +1,37 @@
+-- https://groups.google.com/d/msg/orthanc-users/Rc-Beb42xc8/JUgdzrmCAgAJ
+
+function OnStoredInstance(instanceId, tags, metadata, origin)
+   -- Do not compress twice the same file
+   if origin['RequestOrigin'] ~= 'Lua' then
+
+      -- Retrieve the incoming DICOM instance from Orthanc
+      local dicom = RestApiGet('/instances/' .. instanceId .. '/file')
+
+      -- Write the DICOM content to some temporary file
+      local uncompressed = '/tmp/uncompressed-' .. instanceId .. '.dcm'
+      local target = assert(io.open(uncompressed, 'wb'))
+      target:write(dicom)
+      target:close()
+
+      -- Compress to JPEG2000 using gdcm
+      local compressed = '/tmp/compressed-' .. instanceId .. '.dcm'
+      os.execute('gdcmconv -U --j2k ' .. uncompressed .. ' ' .. compressed)
+
+      -- Generate a new SOPInstanceUID for the JPEG2000 file, as
+      -- gdcmconv does not do this by itself
+      os.execute('dcmodify --no-backup -m "SOPInstanceUID=' ..
+                    tags['SOPInstanceUID'] .. '" ' .. compressed)
+
+      local source = assert(io.open(compressed, 'rb'))
+      local jpeg2k = source:read("*all")
+      source:close()
+
+      -- Upload the JPEG2000 file and remove the uncompressed file
+      RestApiDelete('/instances/' .. instanceId)
+      RestApiPost('/instances', jpeg2k)
+      
+      -- Remove the temporary DICOM files
+      os.remove(uncompressed)
+      os.remove(compressed)
+   end
+end