annotate Database/Lua/Jpeg2000Conversion.lua @ 735:be8f174d3c9d find-refactoring tip

tools/find: Limit and Since are now forbidden when filtering on DICOM tags that are not stored in DB
author Alain Mazy <am@orthanc.team>
date Thu, 24 Oct 2024 15:08:59 +0200
parents b6b207b1de0d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
151
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 -- https://groups.google.com/d/msg/orthanc-users/Rc-Beb42xc8/JUgdzrmCAgAJ
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 function OnStoredInstance(instanceId, tags, metadata, origin)
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 -- Do not compress twice the same file
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 if origin['RequestOrigin'] ~= 'Lua' then
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 -- Retrieve the incoming DICOM instance from Orthanc
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 local dicom = RestApiGet('/instances/' .. instanceId .. '/file')
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 -- Write the DICOM content to some temporary file
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 local uncompressed = '/tmp/uncompressed-' .. instanceId .. '.dcm'
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 local target = assert(io.open(uncompressed, 'wb'))
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 target:write(dicom)
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 target:close()
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 -- Compress to JPEG2000 using gdcm
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 local compressed = '/tmp/compressed-' .. instanceId .. '.dcm'
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 os.execute('gdcmconv -U --j2k ' .. uncompressed .. ' ' .. compressed)
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 -- Generate a new SOPInstanceUID for the JPEG2000 file, as
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 -- gdcmconv does not do this by itself
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 os.execute('dcmodify --no-backup -m "SOPInstanceUID=' ..
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 tags['SOPInstanceUID'] .. '" ' .. compressed)
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 local source = assert(io.open(compressed, 'rb'))
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 local jpeg2k = source:read("*all")
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 source:close()
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 -- Upload the JPEG2000 file and remove the uncompressed file
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 RestApiDelete('/instances/' .. instanceId)
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 RestApiPost('/instances', jpeg2k)
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 -- Remove the temporary DICOM files
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 os.remove(uncompressed)
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 os.remove(compressed)
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 end
b6b207b1de0d missing file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 end