# HG changeset patch # User Sebastien Jodogne # Date 1441989973 -7200 # Node ID 1c9e99d2bfd2af1a4d9c1b1dcfdd067b1b46eab4 # Parent 1ec254a7c6450031af95b4c3728f4af74c81e5ef AutomatedJpeg2kCompression.lua diff -r 1ec254a7c645 -r 1c9e99d2bfd2 Resources/Samples/Lua/AutomatedJpeg2kCompression.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Resources/Samples/Lua/AutomatedJpeg2kCompression.lua Fri Sep 11 18:46:13 2015 +0200 @@ -0,0 +1,38 @@ +-- This sample shows how to use Orthanc to compress on-the-fly any +-- incoming DICOM file, as a JPEG2k file. + +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 = instanceId .. '-uncompressed.dcm' + local target = assert(io.open(uncompressed, 'wb')) + target:write(dicom) + target:close() + + -- Compress to JPEG2000 using gdcm + local compressed = instanceId .. '-compressed.dcm' + os.execute('gdcmconv --j2k ' .. uncompressed .. ' ' .. compressed) + + -- Generate a new SOPInstanceUID for the JPEG2000 file, as + -- gdcmconv does not do this by itself + os.execute('dcmodify --no-backup -gin ' .. compressed) + + -- Read the JPEG2000 file + local source = assert(io.open(compressed, 'rb')) + local jpeg2k = source:read("*all") + source:close() + + -- Upload the JPEG2000 file and remove the uncompressed file + RestApiPost('/instances', jpeg2k) + RestApiDelete('/instances/' .. instanceId) + + -- Remove the temporary DICOM files + os.remove(uncompressed) + os.remove(compressed) + end +end