Lazy compression

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
wijnandpost
Posts: 2
Joined: Mon, 2017-02-20, 09:04

Lazy compression

#1 Post by wijnandpost »

(This is my first post here; please let me know if I posted this question in the wrong section on the forum or did something out of the ordinary)

When I change the representation of a dataset, a lot of memory is consumed. I'm searching for a way to reduce the memory usage.

The related forum posts I could find were:
viewtopic.php?f=1&t=4149
viewtopic.php?f=1&t=3214
Unfortunately it didn't gave the information I sought.

Currently I'm testing with an uncompressed multiframe dataset. Using the jpeg compression example, I've created the following test:

Code: Select all

	DJEncoderRegistration::registerCodecs();
	DcmFileFormat fileformat;
	if (fileformat.loadFile(argv[1]).good())
	{
		DcmDataset *dataset = fileformat.getDataset();
		DcmItem *metaInfo = fileformat.getMetaInfo();
		DJ_RPLossless params;

		dataset->chooseRepresentation(EXS_JPEGProcess14SV1TransferSyntax, &params);
		if (dataset->canWriteXfer(EXS_JPEGProcess14SV1TransferSyntax))
		{
			delete metaInfo->remove(DCM_MediaStorageSOPClassUID);
			delete metaInfo->remove(DCM_MediaStorageSOPInstanceUID);

			fileformat.saveFile(argv[2], EXS_JPEGProcess14SV1TransferSyntax);
		}
	}
	DJEncoderRegistration::cleanup();

When the representation is changed, the DcmPixelData::encode(...) method will load the entire multiframe pixelData blob into memory (via DcmPolymorphOBOW::getUint16Array(pixelData)). This array is fed into DcmCodecList::encode(...), which creates the compressed jpeg frames. Now both the original pixelData and the compressed version reside in memory.

When saving the jpeg representation to file, or sending it over the network, there is no need to keep the entire dataset (in both compressed and uncompressed form) in memory. Is there a way to avoid the compression until needed, and release the memory afterwards?
In other words:
- load dataset (without pixel data)
- change representation (but don't load & convert pixel data yet)
- save / send dataset => when the frames are written, use DcmPixelData::getUncompressedFrame(...), compress single frame, store compressed frame, and release frame memory.

Thanks,
Wijnand

J. Riesmeier
DCMTK Developer
Posts: 2501
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Lazy compression

#2 Post by J. Riesmeier »

Actually, there is a way of doing a frame-wise compression of large multi-frame images using the DCMTK. However, this is a little more complex than just calling chooseRepresentation(). The basic approach would be to feed the uncompressed pixel data of the individual frames into the JPEG encoder (from "dcmjpeg" module), store the resulting blob as a file and put a reference to this file as the value of the corresponding pixel item (using createValueFromTempFile()). I personally already did that in the past for one of my customers. It works quite well.

However, I agree that it would be nice if DCMTK would support the "lazy compression" directly but this is currently not the case. Of course, it is possible to add this feature to the toolkit but it requires lots of work. In case you want to contribute it to the community, we would be happy to add it to the DCMTK. Also see this blog posting: https://blog.jriesmeier.com/2013/03/how ... the-dcmtk/

wijnandpost
Posts: 2
Joined: Mon, 2017-02-20, 09:04

Re: Lazy compression

#3 Post by wijnandpost »

Thank you for your reply.
I'm currently investigating what the best option would be. I'll post an update if I have something more to share.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest