comparison Resources/Samples/Lua/AutomatedJpeg2kCompression.lua @ 1614:1c9e99d2bfd2

AutomatedJpeg2kCompression.lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 11 Sep 2015 18:46:13 +0200
parents
children e371519d4ac9
comparison
equal deleted inserted replaced
1613:1ec254a7c645 1614:1c9e99d2bfd2
1 -- This sample shows how to use Orthanc to compress on-the-fly any
2 -- incoming DICOM file, as a JPEG2k file.
3
4 function OnStoredInstance(instanceId, tags, metadata, origin)
5 -- Do not compress twice the same file
6 if origin['RequestOrigin'] ~= 'Lua' then
7
8 -- Retrieve the incoming DICOM instance from Orthanc
9 local dicom = RestApiGet('/instances/' .. instanceId .. '/file')
10
11 -- Write the DICOM content to some temporary file
12 local uncompressed = instanceId .. '-uncompressed.dcm'
13 local target = assert(io.open(uncompressed, 'wb'))
14 target:write(dicom)
15 target:close()
16
17 -- Compress to JPEG2000 using gdcm
18 local compressed = instanceId .. '-compressed.dcm'
19 os.execute('gdcmconv --j2k ' .. uncompressed .. ' ' .. compressed)
20
21 -- Generate a new SOPInstanceUID for the JPEG2000 file, as
22 -- gdcmconv does not do this by itself
23 os.execute('dcmodify --no-backup -gin ' .. compressed)
24
25 -- Read the JPEG2000 file
26 local source = assert(io.open(compressed, 'rb'))
27 local jpeg2k = source:read("*all")
28 source:close()
29
30 -- Upload the JPEG2000 file and remove the uncompressed file
31 RestApiPost('/instances', jpeg2k)
32 RestApiDelete('/instances/' .. instanceId)
33
34 -- Remove the temporary DICOM files
35 os.remove(uncompressed)
36 os.remove(compressed)
37 end
38 end