First of all Dicom dataset needs to be in DS_EXPLICIT_VR and DS_LITTLE_ENDIAN transfer syntax to insert compressed image unless file already has compressed transfer syntax and you are adding more frames to it with same compression. Also, you cannot insert a frame in to an existing DICOM with different compression than the one already in the file.
In order to compress the image in the existing DICOM dataset, you need to extract the images out of DICOM dataset and delete the Pixel Data element and save it to a temporary file with DS_EXPLICIT_VR|DS_LITTLE_ENDIAN flags. Than free the DS and reload the temporary DIUCOM file and insert the pixel data element and images to it with any LEADTOOLS supported DICOM compression. Now you can save the DICOM Dataset with SaveDS call and DO NOT pass any flags that will modify the transfer syntax of the image (i.e. DS_IMPLICIT_VR or DS_EXPLICIT_VR or DS_LITTLE_ENDIAN or DS_BIG_ENDIAN or DS_LENGTH_EXPLICIT).
ChangeTransferSyntax method is your best option for converting compression in existing image in DICOM dataset. If you are initializing the dataset and creating a new DICOM file always use DS_EXPLICIT_VR |DS_LITTLE_ENDIAN|DS flags in calls to InitDS method. If a DICOM dataset with image already exist and you want to change the transfer syntax or compression, you can also follow the following steps instead of calling ChangeTransferSyntax:
We added a new flag called "DS_KEEP_PIXEL_DATA_INTACT (0x0400)”, please make sure that you pass this flag whenever you call "LoadDS" or "InitDS”.
Whenever this flag is set, LEADTOOLS will not modify the image data when you retrieve the image. If the flag is not set we may convert the image to unsigned and monochrome 1 to monochrome 2 format.
In order to insert the image back into a dataset and have it display properly by third party applications please follow the steps below:
Start by loading thedataset by callingLoadDS and passing DS_KEEP_PIXEL_DATA_INTACTflag.
Now extract the values for the following elements (if available in the dataset):
- Smallest Image Pixel Value (0028,0106)
- Largest Image Pixel Value (0028,0107)
- Window Center (0028,1050), value multiplicity could be more than 1.
- Window Width (0028,1051), value multiplicity could be more than 1.
- Rescale Slope (0028,1052)
- Rescale Intercept (0028,1053)
- Bit Allocated
- Bit Stored (0028,0101)
- High Bit (0028,0102)
- Pixel Representation (0028,0103)
- Photometric Interpretation
Extract the image(s).
If the dataset has
A compressed transfer syntax such as
1. 1.2.840.10008.1.2.4.50.
2. 1.2.840.10008.1.2.4.51.
3. 1.2.840.10008.1.2.4.70.
4. 1.2.840.10008.1.2.5.
5. 1.2.840.10008.1.2.4.57
Then delete the pixel data element and also update the transfer syntax element with Explicit VR Little Endian.
An"Explicit VR Little Endian" transfer syntax then just delete the pixel data element.
Other transfer syntaxes (Implicit VR little Endian , Explicit VR big endian) then delete the pixel data element and save the dataset by calling SaveDS to a temp file and pass DS_EXPLICIT_VR |DS_LITTLE_ENDIAN|DS_METAHEADER_PRESENT then call ResetDS andcall LoadDSto load the temp file.
6. Insert the pixel data element (VR_OW)
7. Insert the image(s) with desired compression or uncompressed.
Note: you cannot insert 16-bit lossy JPEG. Not a valid option for DICOM.
8. Restore the following elements with saved value from above:
- Smallest Image Pixel Value (0028,0106)
- Largest Image Pixel Value (0028,0107)
- Window Center (0028,1050), value multiplicity could be more than 1.
- Window Width (0028,1051), value multiplicity could be more than 1.
- Rescale Slope (0028,1052)
- Rescale Intercept (0028,1053)
- Bit Allocated
- Bit Stored (0028,0101)
- High Bit (0028,0102)
- Pixel Representation (0028,0103)
- Photometric Interpretation
Note: If the original Dataset didn't have "Smallest Image PixeValue " and "Largest Image Pixel Value " elements then make sure that you delete them from the current dataset.
9. Call SaveDS to save the dataset with only these flags: DS_METAHEADER_PRESENT.
In your sample code, you are setting J2K compression for gLeadDicomDS and inserting the image in newDicomDS. Yes, we update the transfer syntax element to reflect the compression used in the dataset. Our default JPEG Lossless compression transfer syntax is 1.2.840.10008.1.2.4.70 but we save the file in a way so that changing the value for transfer syntax to 1.2.840.10008.1.2.4.57 will also work.
I would use the change transfer syntax function (COM : ChangeTransferSyntax method (ILEADDicomDS), C++: LDicomDS::ChangeTransferSyntax, API: L_DicomChangeTransferSyntax) , it should take care of all the details above and much more, you can use it regardless what your source and destination transfer syntax is.
Edited by moderator Wednesday, April 5, 2017 8:01:40 AM(UTC)
| Reason: Not specified