DICOM文件编程 下载本文

/* ... */

DicomImage *image = new DicomImage(\if (image != NULL) {

if (image->getStatus() == EIS_Normal) {

Uint8 *pixelData = (Uint8 *)(image->getOutputData(8 /* bits per sample */)); if (pixelData != NULL) {

/* do something useful with the pixel data */ } } else cerr << \cannot load DICOM image (\<< DicomImage::getString(image->getStatus()) << \

}

delete image;

六、dcmjpeg程序包

dcmjpeg提供了一个压缩/解压缩库以及可用工具。该模块包含一些类,可将DICOM图像对象在非压缩和JPEG压缩表示(传输协议)之间转换。无失真和有失真JPEG处理都被支持。这个模块实现了一族codec(编码解码器,由DcmCodec类派生而来),可以将这些codec在codec list中注册,codec list是由dcmdata模块保存的。

主要接口类:

--DJEncoderRegistration: 一个singleton(孤立)类,为所有支持的JPEG处理注册编码器。在djencode.h中定义。

--DJDecoderRegistration: 一个singleton(孤立)类,为所有支持的JPEG处理注册解码器。在djdecode.h中定义。

--DJCodecEncoder: JPEG编码器的一个抽象codec类。This abstract class contains most of the application logic needed for a dcmdata codec object that implements a JPEG encoder using the DJEncoder interface to the underlying JPEG implementation. This class only supports compression, it neither implements decoding nor transcoding. 在djcodece.h中定义。

--DJCodecDecoder: JPEG解码器的一个抽象codec类。This abstract class contains most of the application logic needed for a dcmdata codec object that implements a JPEG decoder using the DJDecoder interface to the underlying JPEG implementation. This class only supports decompression, it neither implements encoding nor transcoding.

工具:

dcmcjpeg: Encode DICOM file to JPEG transfer syntax dcmdjpeg: Decode JPEG-compressed DICOM file

dcmj2pnm: Convert DICOM images to PGM, PPM, BMP, TIFF or JPEG dcmmkdir: Create a DICOMDIR file 举例:

--用无失真JPEG压缩一幅DICOM图像文件。

DJEncoderRegistration::registerCodecs(); // register JPEG codecs DcmFileFormat fileformat;

if (fileformat.loadFile(\

{

DcmDataset *dataset = fileformat.getDataset(); DcmItem *metaInfo = fileformat.getMetaInfo();

DJ_RPLossless params; // codec parameters, we use the defaults // this causes the lossless JPEG version of the dataset to be created

dataset->chooseRepresentation(EXS_JPEGProcess14SV1TransferSyntax, ¶ms); // check if everything went well

if (dataset->canWriteXfer(EXS_JPEGProcess14SV1TransferSyntax))

{ // force the meta-header UIDs to be re-generated when storing the file // since the UIDs in the data set may have changed

delete metaInfo->remove(DCM_MediaStorageSOPClassUID); delete metaInfo->remove(DCM_MediaStorageSOPInstanceUID); // store in lossless JPEG format

fileformat.saveFile(\}

} DJEncoderRegistration::cleanup(); // deregister JPEG codecs --解压缩一幅JPEG压缩的DICOM图像文件。

DJDecoderRegistration::registerCodecs(); // register JPEG codecs DcmFileFormat fileformat;

if (fileformat.loadFile(\{

DcmDataset *dataset = fileformat.getDataset(); // decompress data set if compressed

dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL); // check if everything went well

if (dataset->canWriteXfer(EXS_LittleEndianExplicit)) {

fileformat.saveFile(\}

} DJDecoderRegistration::cleanup(); // deregister JPEG codecs 七、dcmnet程序包

dcmnet是一个网络库及可用工具。该模块包含了实现DICOM网络通信的所有函数集,即:DICOM上层有限状态机(DICOM Upper Layer Finite State Machine),关联控制服务元素(Association Control Service Element, ACSE)以及DICOM消息服务元素(DICOM Message Service Element, DIMSE)。

主要接口:该模块的主要接口包括在文件assoc.h和dimse.h中定义的大量结构和函数。 --assoc.h: 这个文件包含程序,为DICOM应用提供关联管理。它维护描述活动关联的结构,提供对关联特定信息的访问。也提供程序帮助关联协议association negotiation(presentation contexts, abstract syntaxes, transfer syntaxes, maximum PDU length, and other extended negotiation)。该包使用了DICOM上层机制接收/发送关联请求/响应。每一个活动的关联由T_ASC_Association结构表示,包含了所有相关的信息。模块前缀ASC_。

--dimse.h: 这个文件包含程序,为DICOM应用提供dimse层的服务。 工具:

--echoscu: DICOM verification (C-ECHO) SCU

--findscu: DICOM query (C-FIND) SCU

--movescu: DICOM retrieve (C-MOVE) SCU --storescp: DICOM storage (C-STORE) SCP --storescu: DICOM storage (C-STORE) SCU --termscu: DICOM termination SCU 举例:

--一个简单的Echo SCU(Verification Service Class SCU)。大多数错误处理代码省去了,在Win32上的特定代码如WinSock初始化也省去了。

T_ASC_Network *net; // network struct, contains DICOM upper layer FSM etc. ASC_initializeNetwork(NET_REQUESTOR, 0, 1000 /* timeout */, &net); T_ASC_Parameters *params; // parameters of association request

ASC_createAssociationParameters(¶ms, ASC_DEFAULTMAXPDU); // set calling and called AE titles

ASC_setAPTitles(params, \

// the DICOM server accepts connections at server.nowhere.com port 104

ASC_setPresentationAddresses(params, \// list of transfer syntaxes, only a single entry here

const char* ts[] = { UID_LittleEndianImplicitTransferSyntax }; // add presentation context to association request

ASC_addPresentationContext(params, 1, UID_VerificationSOPClass, ts, 1); // request DICOM association T_ASC_Association *assoc;

if (ASC_requestAssociation(net, params, &assoc).good()) {

if (ASC_countAcceptedPresentationContexts(params) == 1)

{ // the remote SCP has accepted the Verification Service Class DIC_US // generate next message ID

DIC_US status; // DIMSE status of C-ECHO-RSP will be stored here DcmDataset *sd = NULL; // status detail will be stored here // send C-ECHO-RQ and handle response

DIMSE_echoUser(assoc, id, DIMSE_BLOCKING, 0, &status, &sd); delete sd; // we don't care about status detail } }

ASC_releaseAssociation(assoc); // release association

ASC_destroyAssociation(&assoc); // delete assoc structure ASC_dropNetwork(&net); // delete net structure 八、dcmpstat程序包

dcmpstat: 一个描述状态(presentation state)库和可用工具。This module contains classes that implement a high-level API for the DICOM Softcopy Grayscale Presentation State Storage SOP Class. It also contains various support classes that are used by DICOMscope, a free DICOM viewer that has been developed as a demonstrator for presentation states. See http://dicom.offis.de/dscope。

主要接口:

--DVPresentationState: 一个灰度软拷贝描述状态。这个类管理着一个描述状态对象的数据结构。描述状态可以创建、读、写和更改。在dvpstat.h中定义。

--DVInterface: 这个接口类是用来帮助软拷贝描述状态浏览器工作的。这个类管理着数据库机制,允许开始和停止网络交互,并访问图像和描述状态。在dviface.h中定义。

--DVPSStoredPrint: the representation of a Stored Print object。在文件dvpssp.h中定义。 工具:

dcmmkcrv: Add 2D curve data to image dcmmklut: Create DICOM look-up tables

dcmp2pgm: Read DICOM image and presentation state and render bitmap dcmprscp: DICOM basic grayscale print management SCP dcmprscu: Print spooler for presentation state viewer dcmpschk: Checking tool for presentation states

dcmpsmk: Create DICOM grayscale softcopy presentation state

dcmpsprt: Read DICOM images and presentation states and render print job dcmpsrcv: Network receive for presentation state viewer dcmpssnd: Network send for presentation state viewer 举例:

--给一幅DICOM图像创建一个缺省的描述状态 DcmFileFormat infile; DcmFileFormat outfile;

if (infile.loadFile(\{

DVPresentationState pstate; // presentation state handler if (pstate.createFromImage(*infile.getDataset()).good()) {

// serialize presentation state into DICOM data set structure if (pstate.write(*outfile.getDataset(), OFFalse).good()) {

// and write to file

outfile.saveFile(\ } } }

--应用一个描述状态中的灰度变换管道给一幅DICOM图像 DcmFileFormat imagefile; DcmFileFormat gspsfile;

if (imagefile.loadFile(\ gspsfile.loadFile(\{

DVPresentationState pstate; // presentation state handler

if (pstate.read(*gspsfile.getDataset()).good()) // parse gsps object {

// attach presentation state to image data

if (pstate.attachImage(&imagefile, OFFalse).good())