Study Notes for the DCMTK open-source database 1: Save the DCM file as a BMP file or data stream (that is, an array)

Source: Internet
Author: User
Tags bmp image

DCMTK is the most fully available open source library for dicom3.0. By combining the DCMTK open source Library and the cximage open source library, you can easily develop your own DCM file editing and browsing software.

The following describes the learning and exploration process of "converting a DCM file to a BMP file format.

The format of the BMP file is not detailed here (you can refer to the relevant msdn materials ). BMP is a device-independent graph (DIB). In the memory, as long as it contains the BMP File Header (bitmapfileheader), BMP Information header (bitmapinfoheader), palette (palette), and real pixel information, you can display it on all display devices. Therefore, the main task of converting a DCM file to a BMP format is: select the information (such as width, Image Height, space occupied by each pixel, and pixel data) required by the BMP file header and information header from the many data elements of DCM ).

The dcmimgle development kit in DCMTK is used to convert the format using the createwindowsdib interface and writebmp provided by the dicomimage class. The Code is as follows:

DcmFileFormat *mImage=new DcmFileFormat();DJDecoderRegistration::registerCodecs(); // register JPEG codecsDcmDataset *dataset = mImage->getDataset();E_TransferSyntax xfer=dataset->getOriginalXfer();dataset->chooseRepresentation(EXS_LittleEndianExplicit,NULL);DicomImage *pdcmImage=new DicomImage((DcmFileFormat*)mImage,EXS_LittleEndianExplicit,CIF_AcrNemaCompatibility,0,1);if(pdcmImage->getStatus()!=EIS_Normal)exit(0);void* pDIB=NULL;int size=0;pdcmImage->createWindowsDIB(pDIB,size,0,8,1,1);

At this point, the pdib memory obtains the BMP format of the "C: \ test. DCM" file pixel data. If you want to save pdib as a BMP file, you also need to add the BMP File Header (bitmapfileheader), BMP Information header (bitmapinfoheader), and palette ). Refer to the structure of bitmapfileheader and bitmapinfoheader in msdn and assign values to members one by one. Assume that the generated file header variable is mbmpfileheader and the information header variable is mbmpinfoheader. Because the number of BMP pixels in the createwindowsdib function is set to 8, the BMP image is an indexed image. The palette is required in the file. The palette is generated as follows (the simplest method ):

RGBQUAD* mPalette=new RGBQUAD[256];memset(mPalette,0,sizeof(RGBQUAD)*256);for(int i=0;i<256;++i){mPalette[i].rgbBlue=i;mPalette[i].rgbGreen=i;mPalette[i].rgbRed=i;}

Then add mbmpfileheader, mbmpinfoheader, and mpalette to the front of pdib to form a BMP image, which can be directly saved as a BMP file. The procedure is as follows:

int imgsize=width*height;int headsize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256;int size=headsize+imgsize;BYTE* mDibImage=new BYTE[size];memset(mDibImage,0,size);memcpy(mDibImage,mBmpFileHeader,sizeof(BITMAPFILEHEADER));memcpy(&mDibImage[sizeof(BITMAPFILEHEADER)],mBmpInfoHeader,sizeof(BITMAPINFOHEADER));memcpy(&mDibImage[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)],mPalette,sizeof(RGBQUAD)*256);memcpy(&mDibImage[headsize],pDIB,imgsize);

 

So far, the data stored in the mdibimage memory is a complete BMP file. The saved result is shown in (left:

Compared to the results (right) seen in the DCM browsing software, the self-generated image is much blurred. The reason may be the relationship between window width and bit. Dicomimage also provides the setwindow interface for adjusting the window width. Call the SETW function before calling the createwindowsdib function. The following figure shows the generated BMP image:

The results are consistent with those in the commonly used DCM browsing software.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.