lua script example header and footer in dicom print

  • I am afraid not. The printer server is coded in Delphi, and has few options.


    Every image can go through printerdisplay.lua though DICOM in, bitmap out.


    Headers and footers are bitmaps:


    var headerbmpname: string = 'print_header.bmp';

    footerbmpname: string = 'print_footer.bmp';

    backgroundbmpname: string = 'print_background.bmp';


    The printer client in 1.5.0 is in lua, embedded in the GUI.


    Marcel

  • Hi Marcel,

    Is there any way to use printing on linux without the graphical interface?


    thanks for help.


  • Hi,


    there are lua plugins for printing but never used.


    Something like:


    [lua]

    printserver = dofile('lua/conquestprint.lua')


    The file recieves the DICOM Data and a variable "command_line" that can be, amongst others:


    "gettingprinter"

    "neventprinter"

    "creatingbasicfilmsession"

    "deletebasicfilmsession"

    "printbasicfilmsession"

    "setbasicfilmsession"


    The above are all ignored on the Windows printer.


    "createbasicfilmbox" - here you should read the film design from the Data["2010,0010"] and create a page

    "printbasicfilmbox" - here you should print the page


    or the filename of a DICOM file where the image was just stored - here you should add the image to the page


    This should give enough information to write a printer but you need lua libraries to support printing. I think I looked at CD and such. These should be able to create e.g. a PDF, but not sure they can print on Linux.


    But not a trivial task. Would be fun to just log what happens in conquestprint.lua (which you have to write!). Check the source code of the delphi GUI for how to do it.


    Marcel


    Some basic printer code:


  • This is what you get (tested under Windows):


    dicom.ini:

    [lua]

    printserver = dofile('lua/print.lua')


    lua/print.lua:

    print(command_line)


    [CONQUESTSRV2] creatingbasicfilmsession

    [CONQUESTSRV2] createbasicfilmbox

    [CONQUESTSRV2] c:\dicom150beta\data\printer_files\1.2.826.0.1.3680043.2.135.737700.78920011.6.1629390.1.4.6.0.dcm

    [CONQUESTSRV2] printbasicfilmbox


    On each of these you get the full dicom information sent. The "4.6.0" in the UID is image 0 of 6x4.


    If you print a bunch:


    CONQUESTSRV2] creatingbasicfilmsession

    [CONQUESTSRV2] createbasicfilmbox

    [CONQUESTSRV2] c:\dicom150beta\data\printer_files\1.2.826.0.1.3680043.2.135.737700.78920011.6.1629390.2.4.6.0.dcm

    [CONQUESTSRV2] c:\dicom150beta\data\printer_files\1.2.826.0.1.3680043.2.135.737700.78920011.6.1629390.2.4.6.1.dcm

    [CONQUESTSRV2] c:\dicom150beta\data\printer_files\1.2.826.0.1.3680043.2.135.737700.78920011.6.1629390.2.4.6.2.dcm

    [CONQUESTSRV2] c:\dicom150beta\data\printer_files\1.2.826.0.1.3680043.2.135.737700.78920011.6.1629390.2.4.6.3.dcm

    [CONQUESTSRV2] c:\dicom150beta\data\printer_files\1.2.826.0.1.3680043.2.135.737700.78920011.6.1629390.2.4.6.4.dcm

    [CONQUESTSRV2] c:\dicom150beta\data\printer_files\1.2.826.0.1.3680043.2.135.737700.78920011.6.1629390.2.4.6.5.dcm

    [CONQUESTSRV2] c:\dicom150beta\data\printer_files\1.2.826.0.1.3680043.2.135.737700.78920011.6.1629390.2.4.6.6.dcm

    [CONQUESTSRV2] printbasicfilmbox


    Thus the lua interface is functional. Next find lua stuff to print!


    Edit: the files are not written, but the conquest API has enough methods to grab the image from the DICOM Data, convert it to e.g. jpeg and write it to disk. The lua example printer reads a jpeg image from disk. In other words, it is feasible to create a printer this way.


    Marcel

  • Couldn't help myself: a very basic printer. Requires libraries cd, cdlua51, cdluaim51, freetype6, im, imlua51, zlib1. I find these extremely hard to find for Linux, though. Look for canvasdraw and IM on sourceforge, precompiled libraries are something like (unverified):


    wget -O iuplibs.tar.gz https://sourceforge.net/projec…9.1_Linux44_64_lib.tar.gz

    wget -O iuplualibs.tar.gz https://sourceforge.net/projec…a51_Linux44_64_lib.tar.gz


    I tested this code on windows (with all these dlls) and it prints pages.


    dicom.ini (defines globals here per association to print more images per page)

    Code
    [lua]
    association = local canvas, canvas_w, canvas_h
    printserver = dofile('lua/print.lua')

    print.lua (the actual print server in the server/lua folder)

  • Hi, Marcel.


    As I could understand, with this we can "print to pdf" a given layout when conquest receives images, is that it?


    I have a exportconverters to create pdfs with images when Conquest receives a study, using third party software with some scripting..


    This would be interesting if simplifies the whole process.

  • Hi,


    actually the CD (canvasdraw) package can both print and create PDFs. I have worked some more in the printer code, and it is now prints similar to the GUI. As before it depends on the CD and IM libraries that are public domain.


    This is specific code for the DICOM printer, but similar calls can be made for incoming images.



    These I think are the right libraries for Linux:


    https://sourceforge.net/projec…44_64_lib.tar.gz/download

    https://sourceforge.net/projec…44_64_lib.tar.gz/download

    https://sourceforge.net/projec…44_64_lib.tar.gz/download

    https://sourceforge.net/projec…44_64_lib.tar.gz/download

    Marcel van Herk is developer of the Conquest DICOM server together with Lambert Zijp.

    Edited once, last by marcelvanherk: Added library paths ().

  • Hi Marcel,


    Thanks for the help, you've already given a way :)


    I can't find some lua files in the repository, could you tell me where to get them?


    these 3 files I didn't find...

    cdlua

    imlua

    cdluaim


    log error



    Server Command := 0150

    Message ID := 0021

    deletebasicfilmsession

    *** lua run error lua/print.lua:2: module 'cdlua' not found:

    no field package.preload['cdlua']

    no file './cdlua.lua'

    no file '/usr/local/share/lua/5.1/cdlua.lua'

    no file '/usr/local/share/lua/5.1/cdlua/init.lua'

    no file '/usr/local/lib/lua/5.1/cdlua.lua'

    no file '/usr/local/lib/lua/5.1/cdlua/init.lua'

    no file '/usr/share/lua/5.1/cdlua.lua'

    no file '/usr/share/lua/5.1/cdlua/init.lua'

    no file './lua/cdlua.lua'

    no file './cdlua.so'

    no file '/usr/local/lib/lua/5.1/cdlua.so'

    no file '/usr/lib/x86_64-linux-gnu/lua/5.1/cdlua.so'

    no file '/usr/lib/lua/5.1/cdlua.so'

    no file '/usr/local/lib/lua/5.1/loadall.so' in 'dofile('lua/print.lua')'

    UPACS THREAD 0: ENDED AT: Mon Aug 23 17:54:26 2021

    UPACS THREAD 0: TOTAL RUNNING TIME: 7 SECONDS

  • Unfortunately on Centos 7.x I had several problems with the lua libs, I tried new installation on Ubuntu 16.04 server, just to test, I also had several problems, as a proof of concept I took a windows7 that I use for delphi and put in it, the lua libs operate perfect on windows, i'll keep trying to solve this and as soon as i get it i put here a step by step with the right libs for download :)


    Now I try to put a header and a footer so that the presentation looks nice, I also believe that generating PDF is important, because for testing I don't have a printer at home :)

  • Hi marcelvanherk,


    There are several libs problems with updated versions of Centos and Ubuntu, so far I haven't been able to make it work, while I haven't solved it, I put your last script in lua on windows 7 by downloading the libs for win32 and putting it in the clibs folder, Okay, printing to the default windows printer works perfectly, without using the GUI.


    I have 2 questions:


    1 - how can I save in pdf with the Patient id number "I used exportconverter for this before"


    2 - I liked the idea of header and footer, I tried to insert it and it didn't work, any tips?


    I only save 2 files, print_header.bmp and print_footer.bmp in conquest directory :)


    thanks for help,

    about linux I will inform you as soon as I can solve this libs/compatibility problem.

  • Hi,


    You can create a pdf as canvas, instead of a printer. Similar you can load images with IM and draw them into you canvas as header and footer with a bit of Lua coding. You can best search for canvasdraw and IM documentation.


    Hope you can sort out the linux issue.


    Marcel

  • Hi Marcel,


    Which of these versions are you using?

    https://sourceforge.net/projects/imtoolkit/files/


    I tried several on windows64 bits and I couldn't understand if the error is in the lua version or not.

    in this moment I install these file -> im-3.7_Win64_dll8_lib.zip


    I look in your file readme.txt, but im file name not found :)


    "

    Place lua external libraries here such as IUP and CD for use within the DICOM server.

    Only work correctly with a dgate.exe that uses an external lua5.1.dll (64 bits for now)

    iuplua51.dll and iup.dll are required to run console.wlua and notepad.wlua


    For 64 bits windows use:


    cd-5.4.1_Win64_dll8_lib.zip

    iup-3.5_Win64_dll8_lib.zip

    lua-5.1.4_Win64_dll10_lib.zip

    iup, iuplua51, socket/core as provided


    For 32 bits windows use:


    cd-5.4.1_Win32_dll8_lib.zip

    iup-3.5_Win32_dll8_lib.zip

    lua-5.1.4_Win32_dll10_lib.zip

    socket/core.dll not required (built-in)

    "


    I talked to one of the maintainers of the package, Antonio Sucuri , he gave me some tips, but the errors in the libs are not very clear where I am going wrong.

    print(command_line)

    require"cdlua"

    require"imlua"

    require"cdluaim"

    require"cdpdf" <-- even being in the correct directory causes an error :(




    [CONQUESTPRINT01] UPACS THREAD 3: STARTED AT: Fri Sep 17 11:34:36 2021

    [CONQUESTPRINT01] A-ASSOCIATE-RQ Packet Dump

    [CONQUESTPRINT01] Calling Application Title : "TestPrint"

    [CONQUESTPRINT01] Called Application Title : "CONQUESTPRINT01"

    [CONQUESTPRINT01] Application Context : "1.2.840.10008.3.1.1.1", PDU length: 16384

    [CONQUESTPRINT01] Number of Proposed Presentation Contexts: 1

    [CONQUESTPRINT01] Presentation Context 0 "1.2.840.10008.5.1.1.9" 1

    [CONQUESTPRINT01] Server Command := 0110

    [CONQUESTPRINT01] Message ID := 0003

    [CONQUESTPRINT01] gettingprinter

    [CONQUESTPRINT01] *** lua run error error loading module 'cdpdf' from file 'D:\PACS\Prototipo_Lua\clibs\cdpdf.dll': Não foi possível encontrar o procedimento especificado. in 'dofile('lua/print.lua')'

    [CONQUESTPRINT01] Server Command := 0140

    [CONQUESTPRINT01] Message ID := 0005

    [CONQUESTPRINT01] creatingbasicfilmsession

    [CONQUESTPRINT01] *** lua run error error loading module 'cdpdf' from file 'D:\PACS\Prototipo_Lua\clibs\cdpdf.dll': Não foi possível encontrar o procedimento especificado. in 'dofile('lua/print.lua')'

    [CONQUESTPRINT01] Server Command := 0140

    [CONQUESTPRINT01] Message ID := 0007

    [CONQUESTPRINT01] createbasicfilmbox

    [CONQUESTPRINT01] *** lua run error error loading module 'cdpdf' from file 'D:\PACS\Prototipo_Lua\clibs\cdpdf.dll': Não foi possível encontrar o procedimento especificado. in 'dofile('lua/print.lua')'

    [CONQUESTPRINT01] Server Command := 0120

    [CONQUESTPRINT01] Message ID := 0009

    [CONQUESTPRINT01] D:\PACS\Prototipo_Lua\Data\printer_files\1.2.826.0.1.3680043.2.135.738028.60948977.6.1631889.0.1.1.0.dcm

    [CONQUESTPRINT01] *** lua run error error loading module 'cdpdf' from file 'D:\PACS\Prototipo_Lua\clibs\cdpdf.dll': Não foi possível encontrar o procedimento especificado. in 'dofile('lua/print.lua')'

    [CONQUESTPRINT01] Server Command := 0130

    [CONQUESTPRINT01] Message ID := 000b

    [CONQUESTPRINT01] printbasicfilmbox

    [CONQUESTPRINT01] *** lua run error error loading module 'cdpdf' from file 'D:\PACS\Prototipo_Lua\clibs\cdpdf.dll': Não foi possível encontrar o procedimento especificado. in 'dofile('lua/print.lua')'

    [CONQUESTPRINT01] Server Command := 0150

    [CONQUESTPRINT01] Message ID := 000d

    [CONQUESTPRINT01] deletebasicfilmbox

    [CONQUESTPRINT01] *** lua run error error loading module 'cdpdf' from file 'D:\PACS\Prototipo_Lua\clibs\cdpdf.dll': Não foi possível encontrar o procedimento especificado. in 'dofile('lua/print.lua')'

    [CONQUESTPRINT01] UPACS THREAD 3: ENDED AT: Fri Sep 17 11:34:36 2021

    [CONQUESTPRINT01] UPACS THREAD 3: TOTAL RUNNING TIME: 0 SECONDS




    Não foi possível encontrar o procedimento especificado -> Could not find the specified procedure


    Do you have any idea what it might be?



    On linux it's even worse, the libs are conflicting, i downloaded the source based on the versions you recommended for windows and i'm compiling now on Centos7 i'll inform you as soon as i have some good news :)

  • On linux, a first step is to get console.sh to work. That uses just a few lua libraries.


    I assume you tried this:


    mkdir iup

    wget -O iuplibs.tar.gz https://sourceforge.net/projec…9.1_Linux44_64_lib.tar.gz

    wget -O iuplualibs.tar.gz https://sourceforge.net/projec…a51_Linux44_64_lib.tar.gz


    cd iup

    tar xvf ../iuplibs.tar.gz

    tar xvf ../iuplualibs.tar.gz

    sudo ./install

    ((export LUA_CPATH=./\?.so\;./lib\?.so\;./lib\?51.so\;))


    Marcel

  • Hi,


    cdpdf is likely dependant on other modules, in particular pdflib.dll, cd.dll, cdpdf.dll, and msvcr80.dll. Are these all there?


    Marcel

    Hi again :)


    Yes, all files is here... I download from


    https://sourceforge.net/projects/imtoolkit/files/

    https://sourceforge.net/projects/iup/files/

    https://sourceforge.net/projects/canvasdraw/files/

    https://sourceforge.net/projects/luabinaries/files/


    I try to use "regsvr32 /i msvcr80.dll" but no resolv the fail too :(


    my test is windows10 pro version.

  • Hi,


    you can just put msvcr80.dll ~msvcr80.zip next to the lua DLLs, if that is the DLL you are mssing. Of course you need the 64 bit version.


    I use (very old ones):


    cd-5.4.1_Win64_dll8_lib.zip

    iup-3.5_Win64_dll8_lib.zip

    im-3.8_Win64_dll8_lib.zip


    or:


    iup-3.17-Lua51_Win64_dll10_lib.zip

    iup-3.17_Win64_dll10_lib.zip

    cd-5.9_Win64_dll10_lib.zip

    cd-5.9-Lua51_Win64_dll10_lib.zip

    im-3.10_Win64_dll10_lib.zip

    im-3.10-Lua51_Win64_dll10_lib.zip


    You cannot mix versions!


    Marcel

  • Hi Marcel,


    now using


    iup-3.17-Lua51_Win64_dll10_lib.zip

    iup-3.17_Win64_dll10_lib.zip

    cd-5.9_Win64_dll10_lib.zip

    cd-5.9-Lua51_Win64_dll10_lib.zip

    im-3.10_Win64_dll10_lib.zip

    im-3.10-Lua51_Win64_dll10_lib.zip


    On windows 10 works "print to default printer and print to pdf, without GUI :)", however the image comes out cropped right, I tried to adjust but I couldn't, I'm looking at the GUI code to see why it looks perfect "without Lua printing".

    would it be possible to put the PatientID in the name of the PDF file? I try to use export... did not work, I don't know how to pass the patient's information through the print...


    print(command_line)

    require"cdlua"

    require"cdluapdf"

    require"imlua"

    require"cdluaim"


    if command_line=='createbasicfilmbox' then


    --canvas = cd.CreateCanvas(cd.PRINTER, "test5")

    --canvas = cd.CreateCanvas(cd.PDF, "cd_pdf.pdf -w210 -h297 -s120")

    -- PatientID

    canvas = cd.CreateCanvas(cd.PDF, "saida\\cd_pdf.pdf -w210 -h297 -s120")


    canvas_w, canvas_h = canvas:GetSize()

    print(canvas_w, canvas_h)

    end


    if string.find(command_line, '%.dcm') then

    local c = (Data.WindowCenter or 0) if c=='' then c=0 end

    local w = (Data.WindowWidth or 0) if w=='' then w=0 end

    local s = (Data.RescaleSlope or 1) if s=='' then s=1 end

    local i = (Data.RescaleIntercept or 0) if i=='' then i=0 end


    if string.find(c, '%\\') then

    c = string.match(c, '(.-)\\')

    end


    if string.find(w, '%\\') then

    w = string.match(w, '(.-)\\')

    end


    w = tonumber(w)/tonumber(s)

    c = tonumber(c)/tonumber(s)


    local vr = Data:GetVR(0x2020, 0x0110)

    vr[0]:Script('save jpg level '..math.floor(c)..' window '..(math.ceil(w/2)) .. ' to x.jpg')

    local image = im.FileImageLoad('x.jpg')

    local iw = image:Width()

    local ih = image:Height()


    local rows, cols, num = command_line:match('.*%.(.-)%.(.-)%.(.-)%.(.-)$')

    rows = tonumber(rows)

    cols = tonumber(cols)

    num = tonumber(num)

    local margin=100


    local row = num % rows

    local col = math.floor(num/rows)

    local sizex = (canvas_w-2*margin)/rows

    local sizey = (canvas_h-2*margin)/cols

    local posx = margin + row*sizex

    local posy = (canvas_h-sizey) - (margin + col*sizey)


    if iw>ih then sizey = sizex*ih/iw

    else sizex = sizey*iw/ih end


    image:cdCanvasPutImageRect(canvas, posx, posy, sizex, sizey, 0,0,0,0)

    canvas:Text(posx, posy-40, num)


    end


    if command_line=='printbasicfilmbox' then

    --print("OEF -> final... ")

    canvas:Kill()

    end



    I've already managed to solve part of the problem in Centos7, when I solve the libs post here the solution to install.


    thanks for help :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!