Click here to Skip to main content
Click here to Skip to main content

Converting a DICOM Image to a Common Graphic Format and Vice Versa with DCMTK and CxImage

By , 10 Nov 2005
Rate this:
Please Sign up or sign in to vote.

Introduction

This article presents a minimum runnable toy application as a starting point to show how to convert a DICOM image to common graphic formats (i.e. BMP, JPG, TIF, etc.) and vice versa. Our sample application is based on two open source libraries, they are DCMTK and CxImage.

Background

The DICOM standard (Digital Imaging and Communications in Medicine) is a standard created by the National Electrical Manufacturers Association (NEMA) to ease the distribution and exchange of medical images, such as CT scans, MRIs and ultrasound. In this article, we will focus on file format conversions. The file format is described in Part 10 of the DICOM standard, which you can download from here[^]. There is also a brief introduction to the file format available here.

DCMTK is a widely used open source implementation of the DICOM standard; it is a collection of C/C++ libraries and applications with complete source code. To compile the sample in this article, you need to download DCMTK first. If you have trouble building the downloaded DCMTK package, please refer to DCMTK for Dummies.

Another library used in this article is CxImage, it is a C++ class that can load, save, display and transform images in a very simple and fast way. It supports almost all the common graphic types, such as BMP, JPG, TIF, PNG, etc. In this article, we will expand this library to support displaying and transforming DICOM images by using the DICOM format encoding/decoding features provided by DCMTK. Download CxImage and follow its usage guidance to make sure it can be compiled successfully on your machine.

Using the Code

We simply derive our CxImageDCM class from the base class CxImage. Doing so enables CxImageDCM class to load and decode common graphics using the methods inherited from the base class. There are three extra methods in the derived class, LoadDCM(…), SaveAsDCM(…), SaveAsJPG(…), they are used to decode, encode and convert a DICOM image, respectively.

//
class CxImageDCM : public CxImage  
{
public:
    CxImageDCM();
    virtual ~CxImageDCM();
    
    bool LoadDCM(const TCHAR* filename);
    bool SaveAsDCM(const TCHAR* filename);
    bool SaveAsJPG(const TCHAR* fileName);

};//

Load DCM

In the sample application, a DICOM image is loaded and decoded with the classes provided by DCMTK, then it is converted to a temporary bitmap file for later manipulations:

//
bool CxImageDCM::LoadDCM(const TCHAR* filename)
{  
    DcmFileFormat *dfile = new DcmFileFormat();
    OFCondition cond = dfile->loadFile(filename, EXS_Unknown,
                      EGL_withoutGL,DCM_MaxReadLength,OFFalse);
    
    if (cond.bad()) {
        AfxMessageBox(cond.text());
    }
    
    E_TransferSyntax xfer = 
            dfile->getDataset()->getOriginalXfer();
    DicomImage *di = new DicomImage(dfile, xfer, 
                         CIF_AcrNemaCompatibility, 0, 1);
    
    if (di->getStatus() != EIS_Normal)
        AfxMessageBox(DicomImage::getString(di->getStatus()));
    
    di->writeBMP("c:\\from_dicom.bmp",24);
    
    return CxImage::Load("c:\\from_dicom.bmp",CXIMAGE_FORMAT_BMP);
    
}//

Converting from DCM

After loading a DCM file, you can save it as a common graphic file using the encoding features provided by CxImage, or you may also use DCMTK’s encoding plugins to do the conversion (however, CxImage supports more formats):

//
bool CxImageDCM::SaveAsJPG(const TCHAR* fileName)
{//you may also use DCMTK's JPG encoding plug-in
    return CxImage::Save(fileName,CXIMAGE_FORMAT_JPG);

}//

Converting to DCM

To convert a common graphic file to a DCM file, you need to load the common graphic first, then set the necessary tag and copy the pixel data to the destination DCM file:

//
bool CxImageDCM::SaveAsDCM(const TCHAR* filename)
{
    CxImageDCM::IncreaseBpp(24);
    char uid[100]; 
    DcmFileFormat fileformat; 
    DcmDataset *dataset = fileformat.getDataset(); 
    dataset->putAndInsertString(DCM_SOPClassUID, 
               UID_SecondaryCaptureImageStorage); 
    /* ... */
    //dataset->putAndInsertUint32(DCM_MetaElementGroupLength,128);
    dataset->putAndInsertUint16(DCM_FileMetaInformationVersion,
                                                          0x0001);
    /* ... */    
    dataset->putAndInsertString(DCM_UID,
        UID_MultiframeTrueColorSecondaryCaptureImageStorage);
    dataset->putAndInsertString(DCM_PhotometricInterpretation,
                                                        "RGB"); 
    //add more tags here
    /* ... */ 
    BYTE* pData=new BYTE[GetHeight()*info.dwEffWidth];
    BYTE* pSrc=GetBits(head.biHeight-1);
    BYTE* pDst=pData;
    for(long y=0; y < head.biHeight; y++){
        memcpy(pDst,pSrc,info.dwEffWidth);
        pSrc-=info.dwEffWidth;
        pDst+=info.dwEffWidth;
    }
    dataset->putAndInsertUint8Array(DCM_PixelData, 
                 pData, GetHeight()*info.dwEffWidth); 
    delete[] pData;
    
    OFCondition status = fileformat.saveFile(filename, 
                           EXS_LittleEndianImplicit,
                           EET_UndefinedLength,EGL_withoutGL); 
    if (status.bad()) 
        AfxMessageBox("Error: cannot write DICOM file ");
    
    return true;     
}//

Points of Interest

In this article, the encoding feature provided by CxImage is used to convert a DICOM image to a JPG file (or other formats that CxImage supports). Actually, DCMTK already has a full-fledged utility called dcmj2pnm to convert a DICOM image to a BMP, PNG, TIF or JPG image. For other formats, that dcmj2pnm doesn’t support, such as GIF, TGA, PCX, WBMP, etc., you may use CxImage’s encoding features to write your own converting functions. One thing that I need to clarify here is that our sample application is only a toy utility to give you a starting point. To write a decent DICOM image converter, you need to consider many more DICOM related options. For more information, you can refer to the implementation of dcmj2pnm. (It is included in the DCMTK source code package.)

From my experience, CxImage is easy to use; it "can load, save, display and transform images in a simple and fast way". However, I find it annoying when you have to derive a new image encoder/decoder from the base class, CxImage. The base class must know all the derived classes to give a polymorphic behavior. Fortunately, in our sample, the derived class CxImageDCM needs only the encoding/decoding functions it inherits from the base class, so I didn’t bother to touch the source code of CxImage.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

lxwde
Software Developer
China China
No Biography provided

Comments and Discussions

 
QuestionThanks! PinmemberMember 1043165327-Nov-13 22:50 
Questionnew at dicom Pinmemberaguseee11-Jun-09 17:14 
QuestionHow to use this code in VC++ 2008 Pinmemberin_si4-Mar-09 9:34 
GeneralI cant able to view the DCM image created(I converted jpg to DCM) in any DCM viewer PinmemberMember 28586934-Dec-08 1:34 
GeneralRe: I cant able to view the DCM image created(I converted jpg to DCM) in any DCM viewer PinmemberMember 885942720-Apr-12 12:54 
Generalfatal error C1083: Cannot open include file: 'dcmtk/config/osconfig.h': No such file or directory Pinmembersithara manoj3-Oct-08 2:55 
QuestionRe: fatal error C1083: Cannot open include file: 'dcmtk/config/osconfig.h': No such file or directory Pinmembersithara manoj3-Oct-08 2:57 
GeneralRe: fatal error C1083: Cannot open include file: 'dcmtk/config/osconfig.h': No such file or directory PinmemberMember 28586934-Dec-08 1:41 
GeneralTypo error in given source code? Unable to compile PinmemberAimevous26-May-08 22:33 
QuestionIt seems to have some bug in the funtion(SaveAsDCM) Pinmemberdaodaowang22-May-08 3:47 
General"cximaged.lib" not found under visual studio 6.0 Pinmembersureshtech4-Apr-08 20:52 
QuestionUnable to compile under Visual Studio 8 2005 Pinmembersureshtech2-Apr-08 21:08 
GeneralAdditional .... Pinmembersureshtech2-Apr-08 22:12 
Generalcovert btr image to jpeg image PinmemberMember #38884986-Mar-07 19:13 
Generaldicom to bmp coversion Pinmemberooo_my_dream15-Feb-07 8:05 
GeneralRe: dicom to bmp coversion Pinmemberooo_my_dream15-Feb-07 8:17 
GeneralRe: dicom to bmp coversion PinstaffChristian Graus15-Feb-07 8:41 
Generalon converting Waterlilies.jpg image Pinmembersixiang7-Feb-07 13:31 
Questionhow can we solve these types of error PinmemberMadhawi23-Jan-07 19:52 
CxImage.lib(ximabmp.obj) : error LNK2001: unresolved external symbol __imp__floor
CxImage.lib(ximainfo.obj) : error LNK2001: unresolved external symbol __imp__floor
CxImage.lib(ximatran.obj) : error LNK2001: unresolved external symbol __imp__floor
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol __imp__floor
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol __imp__floor
CxImage.lib(ximatran.obj) : error LNK2001: unresolved external symbol __imp__div
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_finish_decompress
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_read_scanlines
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol __imp__longjmp
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol __imp__longjmp
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_start_decompress
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_calc_output_dimensions
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_read_header
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_resync_to_restart
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_CreateDecompress
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_destroy_decompress
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_std_error
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_finish_compress
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_write_scanlines
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_start_compress
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_simple_progression
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_set_quality
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_set_colorspace
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_set_defaults
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_CreateCompress
CxImage.lib(ximajpg.obj) : error LNK2001: unresolved external symbol _jpeg_destroy_compress
CxImage.lib(ximaexif.obj) : error LNK2001: unresolved external symbol __imp__printf
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFClose
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFReadEncodedStrip
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFComputeStrip
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFReadTile
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFTileRowSize
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFScanlineSize
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFTileSize
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFIsTiled
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFStripSize
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFReadRGBAImage
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFSetField
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFGetFieldDefaulted
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFGetField
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFSetDirectory
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFNumberOfDirectories
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFWriteDirectory
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFWriteScanline
CxImage.lib(ximatif.obj) : error LNK2001: unresolved external symbol _TIFFDefaultStripSize
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_stream_close
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_cleanup
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_matrix_destroy
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_image_readcmpt
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_matrix_create
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_cmprof_destroy
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_image_destroy
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_image_chclrspc
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_cmprof_createfromclrspc
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_image_decode
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_free
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_stream_fdopen
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_init
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_stream_flush
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_image_encode
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_image_strtofmt
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_image_writecmpt
CxImage.lib(ximajas.obj) : error LNK2001: unresolved external symbol _jas_image_create
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_read_end
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_read_row
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_interlace_handling
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_bgr
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_filler
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_expand
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_gray_to_rgb
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_strip_16
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_background
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_get_bKGD
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_read_info
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_error_fn
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_read_fn
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_destroy_read_struct
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_create_info_struct
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_create_read_struct
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_error
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_write_end
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_write_row
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_write_info
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_IHDR
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_pHYs
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_bKGD
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_set_write_fn
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_destroy_write_struct
CxImage.lib(ximapng.obj) : error LNK2001: unresolved external symbol _png_create_write_struct
CxImage.lib(tif_xfile.obj) : error LNK2001: unresolved external symbol _TIFFClientOpen
Release/xImageDCM.exe : fatal error LNK1120: 85 unresolved externals
Error executing link.exe.

AnswerRe: how can we solve these types of error Pinmemberlxwde7-Feb-07 14:13 
GeneralUnable to compile the code. Help!!!! PinmemberMadhawi23-Jan-07 18:51 
GeneralUnable to compile the code. Help!!!! PinmemberNam pashankar5-Dec-06 1:28 
QuestionIs it possible a simple console application? Pinmemberazman_356-Nov-06 22:39 
Generalhelp convert DICOM file to bmp, png... Pinmemberloitls19-Oct-06 5:38 
GeneralRe: help convert DICOM file to bmp, png... PinstaffChristian Graus19-Oct-06 5:57 
GeneralRe: help convert DICOM file to bmp, png... Pinmemberloitls19-Oct-06 9:30 
QuestionConersion to Dicom PinmemberHarpreet Kaur18-Sep-06 0:08 
AnswerRe: Conersion to Dicom Pinmemberlxwde18-Sep-06 2:09 
GeneralRe: Conersion to Dicom PinmemberHarpreet Kaur18-Sep-06 17:57 
GeneralRe: Conersion to Dicom PinstaffChristian Graus19-Oct-06 6:10 
GeneralI hardly see my dicom files PinmemberDabe4-Sep-06 7:47 
GeneralRe: I hardly see my dicom files PinstaffChristian Graus19-Oct-06 6:10 
Generalhelp! Pinmemberloitls11-Aug-06 11:46 
GeneralRe: help! PinstaffChristian Graus19-Oct-06 6:11 
Questionhow to set pixels of image Pinmemberkhushi_muskan18-Jun-06 23:01 
AnswerRe: how to set pixels of image PinstaffChristian Graus19-Oct-06 6:12 
AnswerRe: how to set pixels of image [modified] Pinmemberblackangelxp18-Sep-07 8:54 
GeneralLINK : warning LNK4049: locally defined symbol "_malloc" imported Pinmemberkhushi_muskan12-Jun-06 19:14 
GeneralRe: LINK : fatal error LNK1104: cannot open file "cximaged.lib" Pinmemberlxwde10-May-06 3:53 
GeneralRe: LINK : fatal error LNK1104: cannot open file "cximaged.lib" Pinmemberkhushi_muskan10-May-06 21:10 
GeneralRe: LINK : fatal error LNK1104: cannot open file &quot;cximaged.lib&quot; Pinmemberlxwde10-May-06 21:47 
GeneralRe: LINK : fatal error LNK1104: cannot open file &quot;cximaged.lib&quot; Pinmemberkhushi_muskan10-May-06 23:21 
GeneralRe: LINK : fatal error LNK1104: cannot open file &quot;cximaged.lib&quot; Pinmemberlxwde11-May-06 23:05 
GeneralRe: LINK : fatal error LNK1104: cannot open file &quot;cximaged.lib&quot; Pinmemberkhushi_muskan12-May-06 0:56 
GeneralRe: LINK : fatal error LNK1104: cannot open file &quot;cximaged.lib&quot; Pinmemberlxwde12-May-06 1:20 
GeneralUnable to run the application Pinmemberalwittta13-Mar-06 2:18 
GeneralRe: Unable to run the application Pinmemberlxwde13-Mar-06 22:01 
GeneralSome points about conversion to DICOM Pinmembermarvinbbb15-Nov-05 6:07 
GeneralRe: Some points about conversion to DICOM Pinmemberlxwde15-Nov-05 14:25 
GeneralThank you very much! Pinmemberkhler14-Nov-05 15:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 10 Nov 2005
Article Copyright 2005 by lxwde
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid