analyze2dcm/analyze2dcm.cpp
// save single-frame RGB DICOM
save single-frame RGB DICOM
save single - frame RGB DICOM
bool SaveDCM_rgb(char* fname, DcmFileFormat* fformat, char* serInstUID, Volume* v, int frame)
{
	DcmDataset *dataset=fformat->getDataset();
	unsigned short par1=0,par2=0,dcmw=0,dcmh=0;
	dataset->findAndGetUint16(DCM_Rows,dcmh);
	dataset->findAndGetUint16(DCM_Columns,dcmw);
	long nFramesPerFile=1;
	int start_frame=g_Origin[2],
		end_frame=start_frame+v[0].m_dims[2]-1;
	if(frame>=start_frame && frame<=end_frame)
	{
		unsigned long count;
		unsigned short w=v[0].SX(),h=v[0].SY();
		unsigned char *buf=new unsigned char[3*v[0].SX()*v[0].SY()];
		int za=frame-g_Origin[2];
		int ind=0;
		for(int y=g_Origin[1],ya=0,yy=g_Origin[1]*3; y<g_Origin[1]+v[0].m_dims[1]; y++,ya++,yy+=3)
		{
			ind=w*yy;
			for(int x=g_Origin[0],xa=0,xx=g_Origin[0]*3; x<g_Origin[0]+v[0].m_dims[0]; x++,xa++,xx+=3)
			{
				buf[ind+xx]=(unsigned char)(v[0](xa,ya,za)+.5);
				buf[ind+xx+1]=(unsigned char)(v[1](xa,ya,za)+.5);
				buf[ind+xx+2]=(unsigned char)(v[2](xa,ya,za)+.5);
			}
		}
		dataset->putAndInsertUint8Array(DCM_PixelData, (Uint8*)buf,v[0].SX()*v[0].SY()*3);
		dataset->putAndInsertUint16(DCM_Rows,v[0].SY());
		dataset->putAndInsertUint16(DCM_Columns,v[0].SX());
		char s[20];
		sprintf(s,"%d",frame+1);
		dataset->putAndInsertOFStringArray(DCM_InstanceNumber,s);
		dataset->putAndInsertOFStringArray(DCM_AcquisitionNumber,s);
		delete[] buf;
	}
	if (g_bSC) DcmCodec::convertToSecondaryCapture(dataset);
	dataset->putAndInsertString(DCM_SeriesInstanceUID, serInstUID);
	dataset->putAndInsertUint16(DCM_NumberOfFrames,1);
	dataset->putAndInsertUint16(DCM_SamplesPerPixel,3);
	dataset->putAndInsertUint16(DCM_PlanarConfiguration,0);
	dataset->putAndInsertString(DCM_PhotometricInterpretation,"RGB");
	dataset->putAndInsertUint16(DCM_BitsAllocated,8);
	dataset->putAndInsertUint16(DCM_BitsStored,8);
	dataset->putAndInsertUint16(DCM_HighBit,7);
	dataset->putAndInsertUint16(DCM_PixelRepresentation,0);
	dataset->putAndInsertString(DCM_NumberOfFrames,"1");
	if(strlen(g_SerDescr)>0)
		dataset->putAndInsertString(DCM_SeriesDescription, g_SerDescr);
	if(strlen(g_PN)>0)
		dataset->putAndInsertString(DcmTagKey(0x0010, 0x0010),g_PN);
	if(strlen(g_SerNum)>0)
		dataset->putAndInsertString(DCM_SeriesNumber,g_SerNum);
	return SaveNewDcmFile(fname, fformat, serInstUID, frame, false);
}
bool SaveDCM_rgb ( char * fname , DcmFileFormat * fformat , char * serInstUID , Volume * v , int frame ) { DcmDataset * dataset = fformat -> getDataset ( ) ; unsigned short par1 = 0 , par2 = 0 , dcmw = 0 , dcmh = 0 ; dataset -> findAndGetUint16 ( DCM_Rows , dcmh ) ; dataset -> findAndGetUint16 ( DCM_Columns , dcmw ) ; long nFramesPerFile = 1 ; int start_frame = g_Origin [ 2 ] , end_frame = start_frame + v [ 0 ] . m_dims [ 2 ] - 1 ; if ( frame >= start_frame && frame <= end_frame ) { unsigned long count ; unsigned short w = v [ 0 ] . SX ( ) , h = v [ 0 ] . SY ( ) ; unsigned char * buf = new unsigned char [ 3 * v [ 0 ] . SX ( ) * v [ 0 ] . SY ( ) ] ; int za = frame - g_Origin [ 2 ] ; int ind = 0 ; for ( int y = g_Origin [ 1 ] , ya = 0 , yy = g_Origin [ 1 ] * 3 ; y < g_Origin [ 1 ] + v [ 0 ] . m_dims [ 1 ] ; y ++ , ya ++ , yy += 3 ) { ind = w * yy ; for ( int x = g_Origin [ 0 ] , xa = 0 , xx = g_Origin [ 0 ] * 3 ; x < g_Origin [ 0 ] + v [ 0 ] . m_dims [ 0 ] ; x ++ , xa ++ , xx += 3 ) { buf [ ind + xx ] = ( unsigned char ) ( v [ 0 ] ( xa , ya , za ) + .5 ) ; buf [ ind + xx + 1 ] = ( unsigned char ) ( v [ 1 ] ( xa , ya , za ) + .5 ) ; buf [ ind + xx + 2 ] = ( unsigned char ) ( v [ 2 ] ( xa , ya , za ) + .5 ) ; } } dataset -> putAndInsertUint8Array ( DCM_PixelData , ( Uint8 * ) buf , v [ 0 ] . SX ( ) * v [ 0 ] . SY ( ) * 3 ) ; dataset -> putAndInsertUint16 ( DCM_Rows , v [ 0 ] . SY ( ) ) ; dataset -> putAndInsertUint16 ( DCM_Columns , v [ 0 ] . SX ( ) ) ; char s [ 20 ] ; sprintf ( s , " " , frame + 1 ) ; dataset -> putAndInsertOFStringArray ( DCM_InstanceNumber , s ) ; dataset -> putAndInsertOFStringArray ( DCM_AcquisitionNumber , s ) ; delete [ ] buf ; } if ( g_bSC ) DcmCodec :: convertToSecondaryCapture ( dataset ) ; dataset -> putAndInsertString ( DCM_SeriesInstanceUID , serInstUID ) ; dataset -> putAndInsertUint16 ( DCM_NumberOfFrames , 1 ) ; dataset -> putAndInsertUint16 ( DCM_SamplesPerPixel , 3 ) ; dataset -> putAndInsertUint16 ( DCM_PlanarConfiguration , 0 ) ; dataset -> putAndInsertString ( DCM_PhotometricInterpretation , " " ) ; dataset -> putAndInsertUint16 ( DCM_BitsAllocated , 8 ) ; dataset -> putAndInsertUint16 ( DCM_BitsStored , 8 ) ; dataset -> putAndInsertUint16 ( DCM_HighBit , 7 ) ; dataset -> putAndInsertUint16 ( DCM_PixelRepresentation , 0 ) ; dataset -> putAndInsertString ( DCM_NumberOfFrames , " " ) ; if ( strlen ( g_SerDescr ) > 0 ) dataset -> putAndInsertString ( DCM_SeriesDescription , g_SerDescr ) ; if ( strlen ( g_PN ) > 0 ) dataset -> putAndInsertString ( DcmTagKey ( 0x0010 , 0x0010 ) , g_PN ) ; if ( strlen ( g_SerNum ) > 0 ) dataset -> putAndInsertString ( DCM_SeriesNumber , g_SerNum ) ; return SaveNewDcmFile ( fname , fformat , serInstUID , frame , false ) ; }
save single-frame RGB DICOM
save single - frame RGB DICOM
//verify that byte format is supported //	dataset->findAndGetUint16(DCM_BitsAllocated,par1); //	dataset->findAndGetUint16(DCM_SamplesPerPixel,par2); //	dataset->findAndGetLongInt(DCM_NumberOfFrames,nFramesPerFile); //modify voxels if this frame has non-empty intersection with subvolume //		unsigned char*& rbuf=buf; //	update DICOM tags