Extract the image in the DICOM file to the function of the BMP file

xiaoxiao2021-04-08  387

First create a dialog box cdicombmpdlg, then write functions

Void cdicombmpdlg :: convertdOMTOBMP () {

short int nCols = 0, nRows = 0; short int nBitsAllocated, nSamplesPerPixel = 1; short int nHighBit = 0; float fWindowWidth = 0, fWindowCenter = 0, fRescaleSlope = 1, fRescaleIntercept = 0; BOOL bIsSigned = FALSE;

Bool bGroup2done = false, bgroup28done = false, bpixeldatadone = false;

INT NBYTESP = 0; INT NFRAMESIZE = 0; long int NLENGTH; const char * pszFileName = m_strfilename.getbuffer (3); char szphotometric [32] = "", sztemp [32] = "", sztransfersyntaxuid [80] = "" ;

Bool BIMPLICITVR = true; compression_mode ncompressionMode = compress_none; data_endian nDataEndian = little_endian; int i; int nbytes;

File * fp; char * pdata = 0; Short Int gtag, etc; int nnumframes = 1;

FP = FOPEN (PSZFileName, "RB"); if (! fp) {AFXMessageBox ("Failed to Open File for Read."); Return;}

While (Fread (& gtag, sizeof (short), 1, fp) == 1) {if (ndataendian == BIG_ENDIAN) SWAPWORD ((char *) & gtag, 1);

Switch (gtag) {case 0x0002: // meta header.

IF (bgroup2done) Break;

fread (& eTag, sizeof (short), 1, fp); // Meta header is always in Little Endian Explicit VR syntax switch (eTag) {case 0x0010:. // Transfer syntax UID if (readString (fp, szTransferSyntaxUID, FALSE, Little_endian)! = 0) BREAK;

// Check data endian. If (! Strcmp (SZTransfersyntaxuid, "1.2.840.10008.1.2.2")) // Explicit VR BIG Endian NDataEndian = BIG_ENDIAN DDATAENDIAN = BIG_ENDIAN; / / BIG Endian Else NDataEndian = little_endian; // little endian

// Check if it is implicit VR or Explicit VR if (strcmp (szTransferSyntaxUID, "1.2.840.10008.1.2")!) // Implicit VR Little Endian bImplicitVR = TRUE; // Implicit VR else bImplicitVR = FALSE; // Explicit VR // Parse the encapsulation / compression if (! strcmp (szTransferSyntaxUID, "1.2.840.10008.1.2.4.50")) // JPEG lossy nCompressionMode = COMPRESS_JPEGLOSSY; else if (strcmp (szTransferSyntaxUID, "1.2.840.10008.1.2.4.51"! )) // JPEG lossy 12bit nCompressionMode = COMPRESS_JPEGLOSSY12BIT;! else if (strcmp (szTransferSyntaxUID, "1.2.840.10008.1.2.4.70")) // JPEG lossless first order prediction nCompressionMode = COMPRESS_JPEGLOSSLESS;! else if (strcmp (szTransferSyntaxUID, " 1.2.840.10008.1.2.4.57 ")) // JPEG lossless 14 nCompressionMode = COMPRESS_JPEGLOSSLESS2 process;! else if (strcmp (szTransferSyntaxUID," 1.2.840.10008.1.2.5 ")) // RLE nCompressionMode = COMPRESS_RLE;

BGROUP2DONE = True;

Break;

} Break;

Case 0x0008: // First Non-Meta Group Fread (& ETAG, SIZEOF (Short), 1, FP); IF (nDataEndian == BIG_ENDIAN) SWAPWORD ((char *) & etc, 1);

IF ((ETAG == 0x0005) || (ETAG == 0x0008)) BGROUP2DONE = True; Break;

Case 0x0028: // Image Pixel Data Info Group Fread (& ETAG, SIZEOF (SHORT), 1, FP);

IF (bgroup28done) Break;

IF (nDataEndian == BIG_ENDIAN) SWAPWORD ((char *) & etc, 1);

Switch (ETAG) {casse 0x0002: // Samples Per Pixel Nsamplesperpixel = Readus (FP, NDataEndian); Break;

Case 0x0004: // Photometric Interpolation ReadString (FP, SzPhotometric, BIMPLICITVR, NDATAENDIAN); Break; Case 0x0008: // Number of Frames nnumframes = Readis (FP, BIMPLICITVR, NDATAENDIAN); Break;

Case 0x0010: // rows nrows = readus (fp, ndatandian); Break;

Case 0x0011: // columns ncols = readus (fp, ndataendian); Break;

Case 0x0100: // bits allocated nbitsallocated = readus (fp, ndataendian); Break;

Case 0x0102: // high bit nhighbit = readus (fp, ndatandian); Break;

Case 0x0103: // is sign (fp, ndataendian); Break;

Case 0x1050: // window center fwindowcenter = readds (fp, bimplicitvr, ndatandian); Break;

Case 0x1051: // WINDOW WIDTH FWINDOWIDTH = ReadDS (FP, BIMPLICITVR, NDATAENDIAN); BREAK;

Case 0x1052: // rescale Intercept FrescaleIntercept = readds (FP, BIMPLICITVR, NDATAENDIAN); BREAK

Case 0x1053: // rescale slope frescaleslope = readds (fp, bimplicitvr, ndatandian); bgroup28done = true; Break;

DEFAULT: // do Nothing Break;} Break; Case 0x7fe0: Fread (& etag, sizeof (short), 1, fp); if (bpixeldataDone) Break;

IF (nDataEndian == BIG_ENDIAN) SWAPWORD ((char *) & etc, 1);

IF (ETAG == 0x0010) {nbytesp = nsamplesperpixel * nbitallocated / 8; nframesize = ncols * nROWS * NBYTESP; NLENGTH = nnumframes * nframesize;

// don't try to parse grup 2 and group 28 Any more bgroup2done = true; bgroup28done = true;

// PARSE PIXEL DATA SWITCH (NCompRESSIONMODE) {CASE compress_none: pdata = new char [nlength 16]; fseek (fp, 4, seek_cur); // Skip 4 bytes NBYTES = FREAD (PData, 1, NLENGTH , fp); if (nbytes! = nlength) {AFXMessageBox ("Failed to read all pixel data);} bpixeldatadone = true;

Case Compress_Rle: AFXMessageBox ("Rle Compression Not Supported At this moment"); // to do: // 1. read the offset table. // 2. Read and uncompress Rle Image frames Into Either Rgb or monochrome format. // 3 .. Put frames in the pData buffer, one frame after another // Public domain RLE decompression source code is in Papyrus and dcmtk break; case COMPRESS_JPEGLOSSY:. AfxMessageBox ( "JPEG lossy compression not supported at this moment"); // To do :. // 1. Read the offset table // 2. Read and uncompress JPEG image frames into either RGB or monochrome format // 3. Put frames in the pData buffer, one frame after another // Public domain JPEG decompression source code. is in Papyrus and dcmtk break; case COMPRESS_JPEGLOSSY12BIT:. AfxMessageBox ( "JPEG lossy 12-bit compression not supported at this moment"); // To do: // 1. Read the offset table // 2. Read and uncompress JPEG image Frames Into .. Either RGB or monochrome format // 3. Put frames in the pData buffer, one frame after another // Public domain JPEG decompression source code is in Papyrus and dcmtk break; case COMPRESS_JPEGLOSSLESS:. Case COMPRESS_JPEGLOSSLESS2: AfxMessageBox ( "JPEG lossless compression NOT Supported at this moment "); // to do: // 1. read and uncompress jpeg image frames Into Either rgb or monochrome format. // 3. Put frames in the PData Buffer, One Frame after another. // public domain jpeg defompression source code code is in adap;}} Break;

IF (PDATA) BREAK; // We are done.

Fclose (fp);

IF (PDATA) // Have We Got The Pixel Data? {// NEED TO DO BYTE SWAP? IF (nDataEndian == BIG_ENDIAN) {if (nbitallocated> 8) SwapWord (pdata, nlength / 2);} if (nbitsallocated> 8) {// we need to convert it to 8-bit. Char * pnewdata;

PNewData = ConvertTo8bit (PDATA, NLENGTH / 2, BISSIGNED, NHIGHBIT, FRESCALESLOPE, FRESCALEINTERCEPT, FWINDOWCENTER, FWINDOWIDTH);

// use the new 8-bit data. If (pnewdata) {delete [] pdata; pdata = pnewdata; nbytesp = 1; nframesize / = 2;

NLENGTH / = 2;}}

// Write Bmp Files for (i = 0; i

// free the memory. Delete [] pdata;

AfxMessageBox

}

}

转载请注明原文地址:https://www.9cbs.com/read-132692.html

New Post(0)