Re: [Gdcm2] Buffer problem in Qt environment
Cross-platform DICOM implementation
Brought to you by:
malat
|
From: Mathieu M. <mat...@gm...> - 2009-06-17 13:06:09
|
Commited. Thanks
$ svn ci -m"Adding contribution from Sylvain ADAM. Thank you."
Sending Cxx/CMakeLists.txt
Adding Cxx/ConvertToQImage.cxx
Transmitting file data ..
Committed revision 5837.
On Wed, Jun 17, 2009 at 2:30 PM, Sylvain ADAM<syl...@ho...> wrote:
> Okay, here's the whole source code used for the conversion:
>
> ============================================================================
> gdcm::ImageReader ir;
>
> ir.SetFileName("wedges.ima");
>
> if(!ir.Read())
>
> {
>
> //Read failed
>
> }
>
> std::cout<<"Getting image from ImageReader..."<<std::endl;
>
> gdcm::Image &gimage = ir.GetImage();
>
> const unsigned int* dimension = gimage.GetDimensions();
>
> int dimX = int(dimension[0]);
>
> int dimY = int(dimension[1]);
>
> //This buffer has been declared elsewhere
>
> buffer = new char[gimage.GetBufferLength()];
>
> gimage.GetBuffer(buffer);
>
> //Let's now convert each 16-bits pixel to a 8-bits pixel, leveled to the
> maximum intensity value of the image
>
> //Because the image is a 16-bits grayscale, we thus have 65536 available
> values to store the gray value.
>
> // BUT, usually, the value doesn't oversize around 2000~2200. So if we
> directly convert the 16-bits to 8-bits (from max. 65536 to max255)
>
> // and try to display it, we will have very dark images. Then, we will level
> all the intensities to the maximum intensity value.
>
> //We are gonna use a RGB888 format for Qt display. In a grayscale image,
> R=G=B = the gray value.
>
> //The buffer length, in char, will thus be: 256*256*3 (if the image is
> 256x256) = 196608 chars.
>
> buffer16 = new unsigned short int[dimX*dimY];
>
> maxValue = 0;
>
> for(unsigned int i = 0; i < gimage.GetBufferLength(); i+=2)
>
> {
>
> //@todo: think about #typedef to avoid these long conversions lines.
>
> //Bitwise left shift and bitwise OR
>
> unsigned short int pixel16bitValue = ((((unsigned short int) ((unsigned
> char) buffer [i+1]))<<8)|(unsigned char) buffer [i]);
>
> buffer16[i/2] = pixel16bitValue;
>
> if(pixel16bitValue > maxValue)
>
> {
>
> maxValue = pixel16bitValue;
>
> }
>
> }
>
> unsigned char *buffer888 = new unsigned char[dimX*dimY*3];
>
> for(int i = 0; i < dimX*dimY; i++)
>
> {
>
> unsigned char pixel8bitValue = (unsigned char)
> (buffer16[i]*255/maxValue);
>
> buffer888[i*3] = pixel8bitValue;
>
> buffer888[i*3+1] = pixel8bitValue;
>
> buffer888[i*3+2] = pixel8bitValue;
>
> }
>
> QImage *imageQt = new QImage(buffer888, dimX, dimY, QImage::Format_RGB888);
>
> //The DICOM image is a 16-bits image
>
> //For a 256x256 image, it's 256x256 = 65536 pixels.
>
> //So, normally in a char* (1 char = 1 byte = 1 bit), it does have a length
> of 65536
>
> //BUT as it's a 16-bits image (2*8-bits), we thus have 131072 chars
>
> ============================================================================
>
> Hope this will help other people to get everything right!
>
> Haven't tried the PGM yet, but I guess that's easily comparable.
>
> By the way, I've tried accessing your wiki page, but nothing displays for
> now?
>
> And should I try to post a new bug for this memory problem I evoked
> yesterday?
>
> Sylvain
>
>
>> Date: Wed, 17 Jun 2009 13:41:14 +0200
>> Subject: Re: [Gdcm2] Buffer problem in Qt environment
>> From: mat...@gm...
>> To: syl...@ho...
>> CC: gdc...@li...
>>
>> Hi Sylvain,
>>
>> congrats ! I realize this was my mistake, I quickly browse through
>> the documentation of the image format:
>>
>> http://doc.trolltech.com/4.5/qimage.html#Format-enum
>>
>> and I thought QImage could handle 16bits monochrome image, while it does
>> not.
>>
>> Please share your conversion code and I'll put it in Examples/Cxx
>> (did you use C++ ?).
>>
>> As a side note, you can use the command line gdcmimg to convert a
>> DICOM image to pgm, and then you could load the QImage using the load
>> API:
>>
>> http://doc.trolltech.com/4.5/qimage.html#reading-and-writing-image-files
>>
>> so that you can check your conversion from 8bits is identical to
>> what 'load()' would do with an input 16bits file (I am surprise there
>> is no such filter already in Qt).
>>
>> Eg:
>>
>> $ gdcmconv --raw gdcmData/012345.002.050.dcm /tmp/dummy.dcm
>> $ gdcmimg /tmp/dummy.dcm /tmp/dummy.pgm
>> $ identify /tmp/dummy.pgm
>>
>>
>> ~
>> /tmp/dummy.pgm PNM 256x256 256x256+0+0 PseudoClass 65536c 16-bit 128.016kb
>>
>> thanks for your contribution !
>>
>>
>>
>> --
>> Mathieu
>
> ________________________________
> Discutez sur Messenger où que vous soyez ! Mettez Messenger sur votre mobile
> !
--
Mathieu
|