comparison OrthancServer/Resources/Samples/Lua/AutomatedJpeg2kCompression.lua @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Resources/Samples/Lua/AutomatedJpeg2kCompression.lua@e371519d4ac9
children
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
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 -U --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