Last active
August 29, 2015 14:06
-
-
Save JulesGorny/5c5426df1665b917c5bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char *file = "C:/Folder1/Folder2/dicomfile"; | |
//Open the DICOM file and get the dictionary | |
DcmFileFormat fileformat; | |
OFCondition status = fileformat.loadFile(file); | |
DcmDataset *dictionary; | |
if (status.good()) | |
dictionary = fileformat.getDataset(); | |
//Get the transfer syntax to check if we need jpeg codecs | |
E_TransferSyntax xfer = dictionary->getOriginalXfer(); | |
if(xfer >= EXS_JPEGProcess1TransferSyntax && xfer <= EXS_JPEGProcess14SV1TransferSyntax ) | |
DJDecoderRegistration::registerCodecs(); // register JPEG codecs | |
Uint16 samplePerPixel = 1; //Initialisation in case we can't get a proper value | |
dictionary->findAndGetUint16(DCM_SamplesPerPixel,samplePerPixel); | |
//Get the number of rows and columns in the image | |
Uint16 rows, cols; | |
dictionary->findAndGetUint16(DCM_Rows, rows); | |
dictionary->findAndGetUint16(DCM_Columns, cols); | |
//Get the pixels of the image | |
const Uint8* pixelValues8 = new Uint8(); | |
const Uint16* pixelValues16 = new Uint16(); | |
Uint16 nbBitsAllocated; | |
dictionary->findAndGetUint16(DcmTagKey(0x0028, 0x0100), nbBitsAllocated); | |
const OFBool searchIntoSub=OFFalse; | |
OFCondition getDataok; | |
//This has to be before the writeBMP. | |
if( nbBitsAllocated == 8 ) | |
getDataok = dictionary->findAndGetUint8Array(DCM_PixelData, pixelValues8, 0, searchIntoSub); | |
else if( nbBitsAllocated == 16 ) | |
getDataok = dictionary->findAndGetUint16Array(DCM_PixelData, pixelValues16, 0, searchIntoSub); | |
if( getDataok != EC_Normal ) | |
std::cout << "Can't get the pixels from PixelData, fail to anonymize." << std::endl; | |
//Get the pixelData lenght | |
Uint32 pixelDataLenght = rows*cols*samplePerPixel*nbBitsAllocated/2; | |
Uint8* newPixelValues8 = new Uint8[pixelDataLenght]; | |
Uint16* newPixelValues16 = new Uint16[pixelDataLenght]; | |
//We can't modify the original image. | |
//Preparing a copy of the actual image. | |
for(unsigned long y = 0; y < rows; y++) | |
for(unsigned long x = 0; x < cols; x++) | |
{ | |
int index=(x + y + y*(cols-1))*samplePerPixel; | |
if( nbBitsAllocated == 8 ) | |
copyPixel(pixelValues8, newPixelValues8, index, samplePerPixel); | |
else if( nbBitsAllocated == 16 ) | |
copyPixel(pixelValues16, newPixelValues16, index, samplePerPixel); | |
} | |
//Do what you have to do with the pixels here (here in x y) | |
int index = (x + y*cols)*samplePerPixel; | |
if( nbBitsAllocated == 8 ) | |
setDataGrayScale(newPixelValues8, index, samplePerPixel,0); | |
else if( nbBitsAllocated == 16 ) | |
setDataGrayScale(newPixelValues16, index, samplePerPixel,0); | |
//Rewrite the pixels in the dictionary | |
if( nbBitsAllocated == 8 ) | |
cond = dictionary->putAndInsertUint8Array(DCM_PixelData, (const Uint8*)newPixelValues8, pixelDataLenght, true); | |
else if( nbBitsAllocated == 16 ) | |
cond = dictionary->putAndInsertUint16Array(DCM_PixelData, (const Uint16*)newPixelValues16, pixelDataLenght, true); | |
if( cond.bad() ) | |
std::cout << "Cannot modify the pixels" << std::endl; //cond.text() for the message error | |
//Clean the codecs | |
if(xfer >= EXS_JPEGProcess1TransferSyntax && xfer <= EXS_JPEGProcess14SV1TransferSyntax ) | |
DJDecoderRegistration::cleanup(); | |
// Destroy used object and release memory | |
delete [] newPixelValues8; | |
delete [] newPixelValues16; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment