annotate Resources/Samples/Lua/AutomatedJpeg2kCompression.lua @ 3200:1f4a2c58e7fa

set encoding to UTF-8, IBinaryFormatter
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 06 Feb 2019 13:51:20 +0100
parents e371519d4ac9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1614
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 -- This sample shows how to use Orthanc to compress on-the-fly any
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 -- incoming DICOM file, as a JPEG2k file.
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 function OnStoredInstance(instanceId, tags, metadata, origin)
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 -- Do not compress twice the same file
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 if origin['RequestOrigin'] ~= 'Lua' then
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 -- Retrieve the incoming DICOM instance from Orthanc
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 local dicom = RestApiGet('/instances/' .. instanceId .. '/file')
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 -- Write the DICOM content to some temporary file
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 local uncompressed = instanceId .. '-uncompressed.dcm'
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 local target = assert(io.open(uncompressed, 'wb'))
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 target:write(dicom)
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 target:close()
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 -- Compress to JPEG2000 using gdcm
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 local compressed = instanceId .. '-compressed.dcm'
2294
e371519d4ac9 added -U option to gdcmconv to prevent gdcmconv to mess up the files. Without that option, we have seen the SpecificCharSet tag becoming invalid !!! (seen on the VIX samples)
Alain Mazy <am@osimis.io>
parents: 1614
diff changeset
19 os.execute('gdcmconv -U --j2k ' .. uncompressed .. ' ' .. compressed)
1614
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 -- Generate a new SOPInstanceUID for the JPEG2000 file, as
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 -- gdcmconv does not do this by itself
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 os.execute('dcmodify --no-backup -gin ' .. compressed)
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 -- Read the JPEG2000 file
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 local source = assert(io.open(compressed, 'rb'))
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 local jpeg2k = source:read("*all")
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 source:close()
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 -- Upload the JPEG2000 file and remove the uncompressed file
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 RestApiPost('/instances', jpeg2k)
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 RestApiDelete('/instances/' .. instanceId)
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 -- Remove the temporary DICOM files
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 os.remove(uncompressed)
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 os.remove(compressed)
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 end
1c9e99d2bfd2 AutomatedJpeg2kCompression.lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 end