Fanning Software Consulting

Writing DICOM files in IDL

QUESTION: Is it possible to write a DICOM file from within IDL?

ANSWER: There is no way to write a DICOM file from within IDL with the standard file input and output commands that come with the normal IDL 5.5 distribution. However, Bhautik Joshi ( offered a preliminary version of a DICOM writer on the IDL newsgroup in January 2002. Bhautik wrote the IDL code from C code Mark O'Brian ( provided to the newsgroup.

[Editor Note: As of IDL 6.2, RSI is offering, at some additional cost, IDL DICOM Network Services. This is a DICOM library that allows you to query DICOM databases, as well as read and write DICOM files. I have not used it myself, but I have heard from people who have used it, and they say it works well. Contact your IDL salesperson for details of cost. The IDL DICOM Network Services is explained in the IDL 6.2 on-line help.]

The main features of the software in its present condition are these:

Here is the header of the DICOM_Writer program:

;  0.2
;  Generate a dicom file from within RSI IDL
;  Bhautik Joshi
; USE:
;  DICOM_WRITER, filename, image, VOXELSIZE = voxelsize, SSAI = ssai, $
;     PATIENT = patient, PHYSICIAN = physician
;  filename - string containing name of dicom file to be written to
;  image - Integer (BYTE, FIX, UINT, LONG or ULONG) image - type and bpp
;  is now automagically set
;  voxelsize - Array of 3 floating point values representing voxel size
;  with the format [x,y,z], otherwise set to default of [1.0,1.0,1.0]
;  ssai - Array of 4 integer values representing studyID, seriesnum,
;       acqnum,imagenum, with the format [studyID,seriesnum,acqnum,imagenum],
;  otherwise set to default of [0,0,0,0]
;  patient - patient name, if not defined, set to dummy name
;  physician - physician name, if not defined, set to dummy name
;  * At the moment the program only writes to a single slice
;  * Extra dicom tags can be easily added (see body of program, especially
;    generate_VRtag function)
;  * There is little to no error-checking at the moment, so be 
;    careful!
;  * Analyse seems to need a minimum image size of somewhere around
;    100x100
;  * IMPORTANT: The DICOM writer tries to write 'Implicit VR' type
;    DICOM files - see DICOM standard PS 3.5-1999, part 7
;  * Can write most VR (Value Represenation) tags via new function,
;    generate_VRtag. Currently supported:
;    AE, AS, AT, CS, DA, DS, DT, FL, FD, FD, IS, LO, LT, OB, OW,
;    SH, SL, SS, ST, TM, UI, UL, UN, US, UT
;    and SQ, PN unsupported (I got away with using UI in place of PN)
;  * See comments near generate_VRtag function for notes on usage
;    of the function for adding your own additional tags
;  Create a horrendously boring byte image and store it in a 
;  dicom file, test.dcm, with voxel dimensions of [2.0,3.0,4.0],
;  and studyid=1,series number=2,acquisiton number=3 and image
;  number=4:
;  > rows = 200
;  > cols = 200
;  > image = indgen(rows,cols)
;  > dicom_writer, 'test.dcm', image, voxelsize=[2.0,3.0,4.0], ssai=[1,2,3,4]
;  Based on Marc O'Briens ( TIFF_to_DICOM.c
;  version 0.1    08-01-2002 - first working version produced
;  version 0.11   09-01-2002 - fixed endian-ness issue & added get_lun
;                functionality
;  version 0.2 14-01-2002 - many fixes and additions, including:
;     * replaced most generate_* functions with generate_VRtag
;     * support for many VR (Value representation) types
;     * Autodetection of little/big endian architecture and
;       automagic byte ordering as necessary (for tags/image)
;     * automagically detect image type and set bpp as necessary
;     * more data in the header can be set manually
;  * Allow for more robust dicom writing
;  * Part 10 compliance (!!!!!!!!!!!)
;  * Decent error checking
; Permission to use, copy, modify, and distribute this software and its
; documentation for any purpose and without fee is hereby granted,
; provided that the above copyright notice appear in all copies and that
; both that copyright notice and this permission notice appear in
; supporting documentation.
; This file is provided AS IS with no warranties of any kind.  The author
; shall have no liability with respect to the infringement of copyrights,
; trade secrets or any patents by this file or any part thereof.  In no
; event will the author be liable for any lost revenue or profits or
; other special, indirect and consequential damages.
; The author accepts no responsibility for any action arising from use of 
; this package. The software is not guaranteed to write compliant DICOM
; files. If it causes damage to you or your system, you have been warned -
; this is a work in progress. If it bites your dog, its not my fault. If
; it causes you to curl up on the floor in the foetal position muttering
; about pixies and mushrooms, its not my fault. If it causes you or someone
; else to spontaneously burst into song and dance, its not my fault but
; I'd like to hear about it. You have been warned.

Web Coyote's Guide to IDL Programming