1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelConvert.cxx,v $
6 Date: $Date: 2004/10/10 03:03:10 $
7 Version: $Revision: 1.5 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 ////////////////// TEMPORARY NOT
20 // look for "fixMem" and convert that to a member of this class
21 // Removing the prefix fixMem and dealing with allocations should do the trick
22 #define str2num(str, typeNum) *((typeNum *)(str))
24 #include "gdcmDebug.h"
25 #include "gdcmPixelConvert.h"
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
30 gdcmPixelConvert::gdcmPixelConvert()
38 void gdcmPixelConvert::Squeeze()
44 delete [] Uncompressed;
48 gdcmPixelConvert::~gdcmPixelConvert()
53 void gdcmPixelConvert::AllocateRGB()
58 RGB = new uint8_t[RGBSize];
61 void gdcmPixelConvert::AllocateUncompressed()
64 delete [] Uncompressed;
66 Uncompressed = new uint8_t[ UncompressedSize ];
70 * \brief Read from file a 12 bits per pixel image and uncompress it
71 * into a 16 bits per pixel image.
73 void gdcmPixelConvert::ConvertDecompress12BitsTo16Bits(
78 throw ( gdcmFormatError )
80 int nbPixels = sizeX * sizeY;
81 uint16_t* destination = (uint16_t*)pixelZone;
83 for( int p = 0; p < nbPixels; p += 2 )
88 ItemRead = fread( &b0, 1, 1, filePtr);
91 throw gdcmFormatError( "gdcmFile::ConvertDecompress12BitsTo16Bits()",
92 "Unfound first block" );
95 ItemRead = fread( &b1, 1, 1, filePtr);
98 throw gdcmFormatError( "gdcmFile::ConvertDecompress12BitsTo16Bits()",
99 "Unfound second block" );
102 ItemRead = fread( &b2, 1, 1, filePtr);
105 throw gdcmFormatError( "gdcmFile::ConvertDecompress12BitsTo16Bits()",
106 "Unfound second block" );
109 // Two steps are necessary to please VC++
111 // 2 pixels 12bit = [0xABCDEF]
112 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
114 *destination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
116 *destination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
118 /// \todo JPR Troubles expected on Big-Endian processors ?
123 * \brief Read from file an uncompressed image.
125 bool gdcmPixelConvert::ReadUncompressed( FILE* filePointer,
126 size_t uncompressedSize,
127 size_t expectedSize )
129 if ( expectedSize > uncompressedSize )
131 dbg.Verbose(0, "gdcmPixelConvert::ReadUncompressed: expectedSize"
132 "is bigger than it should");
135 SetUncompressedSize( uncompressedSize );
136 AllocateUncompressed();
137 size_t ItemRead = fread( (void*)Uncompressed, expectedSize, 1, filePointer);
146 * \brief Convert a Gray plane and ( Lut R, Lut G, Lut B ) into an
148 * @return True on success.
150 bool gdcmPixelConvert::ConvertGrayAndLutToRGB( uint8_t *lutRGBA )
154 /// We assume Uncompressed contains the decompressed gray plane
155 /// and build the RGB image.
156 SetRGBSize( UncompressedSize );
159 //aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
160 //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
161 //COPY HERE THE CODE OF GetImageDataIntoVector
163 /// \todo check that operator new []didn't fail, and sometimes return false
168 * \brief Try to deal with RLE 16 Bits.
169 * We assume the RLE has allready been parsed and loaded in
170 * Uncompressed (through \ref ReadAndUncompressRLE8Bits ).
171 * We here need to make 16 Bits Pixels from Low Byte and
172 * High Byte 'Planes'...(for what it may mean)
175 bool gdcmPixelConvert::UncompressRLE16BitsFromRLE8Bits(
179 uint8_t* fixMemUncompressed )
181 size_t PixelNumber = XSize * YSize;
182 size_t fixMemUncompressedSize = XSize * YSize * NumberOfFrames;
184 // We assumed Uncompressed contains the decoded RLE pixels but as
185 // 8 bits per pixel. In order to convert those pixels to 16 bits
186 // per pixel we cannot work in place within Uncompressed and hence
187 // we copy Uncompressed in a safe place, say OldUncompressed.
189 uint8_t* OldUncompressed = new uint8_t[ fixMemUncompressedSize * 2 ];
190 memmove( OldUncompressed, fixMemUncompressed, fixMemUncompressedSize * 2);
192 uint8_t* x = fixMemUncompressed;
193 uint8_t* a = OldUncompressed;
194 uint8_t* b = a + PixelNumber;
196 for ( int i = 0; i < NumberOfFrames; i++ )
198 for ( unsigned int j = 0; j < PixelNumber; j++ )
205 delete[] OldUncompressed;
207 /// \todo check that operator new []didn't fail, and sometimes return false
212 * \brief Implementation of the RLE decoding algorithm for uncompressing
213 * a RLE fragment. [refer to PS 3.5-2003, section G.3.2 p 86]
215 bool gdcmPixelConvert::ReadAndUncompressRLEFragment( uint8_t* decodedZone,
217 long uncompressedSegmentSize,
221 long numberOfOutputBytes = 0;
222 long numberOfReadBytes = 0;
224 while( numberOfOutputBytes < uncompressedSegmentSize )
226 fread( &count, 1, 1, fp );
227 numberOfReadBytes += 1;
229 // Note: count <= 127 comparison is always true due to limited range
230 // of data type int8_t [since the maximum of an exact width
231 // signed integer of width N is 2^(N-1) - 1, which for int8_t
234 fread( decodedZone, count + 1, 1, fp);
235 numberOfReadBytes += count + 1;
236 decodedZone += count + 1;
237 numberOfOutputBytes += count + 1;
241 if ( ( count <= -1 ) && ( count >= -127 ) )
244 fread( &newByte, 1, 1, fp);
245 numberOfReadBytes += 1;
246 for( int i = 0; i < -count + 1; i++ )
248 decodedZone[i] = newByte;
250 decodedZone += -count + 1;
251 numberOfOutputBytes += -count + 1;
254 // if count = 128 output nothing
256 if ( numberOfReadBytes > fragmentSize )
258 dbg.Verbose(0, "gdcmFile::gdcm_read_RLE_fragment: we read more "
259 "bytes than the segment size.");
267 * \brief Reads from disk the Pixel Data of 'Run Length Encoded'
268 * Dicom encapsulated file and uncompress it.
269 * @param fp already open File Pointer
270 * @param image_buffer destination Address (in caller's memory space)
271 * at which the pixel data should be copied
274 bool gdcmPixelConvert::gdcm_read_RLE_file( void* image_buffer,
279 gdcmRLEFramesInfo* RLEInfo,
282 uint8_t* im = (uint8_t*)image_buffer;
283 long uncompressedSegmentSize = XSize * YSize;
286 // Loop on the frame[s]
287 for( gdcmRLEFramesInfo::RLEFrameList::iterator
288 it = RLEInfo->Frames.begin();
289 it != RLEInfo->Frames.end();
292 // Loop on the fragments
293 for( int k = 1; k <= (*it)->NumberFragments; k++ )
295 fseek( fp, (*it)->Offset[k] ,SEEK_SET);
296 (void)gdcmPixelConvert::ReadAndUncompressRLEFragment(
297 (uint8_t*) im, (*it)->Length[k],
298 uncompressedSegmentSize, fp );
299 im += uncompressedSegmentSize;
303 if ( BitsAllocated == 16 )
305 // Try to deal with RLE 16 Bits
306 (void)gdcmPixelConvert::UncompressRLE16BitsFromRLE8Bits(
310 (uint8_t*) image_buffer);
317 * \brief Swap the bytes, according to swap code.
318 * \warning not end user intended
319 * @param im area to deal with
320 * @param swap swap code
321 * @param lgr Area Length
322 * @param nb Pixels Bit number
324 void gdcmPixelConvert::SwapZone(void* im, int swap, int lgr, int nb)
330 uint16_t* im16 = (uint16_t*)im;
341 for(i=0; i < lgr/2; i++)
343 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
347 std::cout << "SWAP value (16 bits) not allowed :i" << swap <<
354 uint16_t fort, faible;
355 uint32_t* im32 = (uint32_t*)im;
362 for(i = 0; i < lgr/4; i++)
364 faible = im32[i] & 0x0000ffff; // 4321
365 fort = im32[i] >> 16;
366 fort = ( fort >> 8 ) | ( fort << 8 );
367 faible = ( faible >> 8 ) | ( faible << 8);
369 im32[i] = ( s32 << 16 ) | fort;
373 for(i = 0; i < lgr/4; i++)
375 faible = im32[i] & 0x0000ffff; // 2143
376 fort = im32[i] >> 16;
377 fort = ( fort >> 8 ) | ( fort << 8 );
378 faible = ( faible >> 8) | ( faible << 8);
380 im32[i] = ( s32 << 16 ) | faible;
384 for(i = 0; i < lgr/4; i++)
386 faible = im32[i] & 0x0000ffff; // 3412
387 fort = im32[i] >> 16;
389 im32[i] = ( s32 << 16 ) | fort;
393 std::cout << "SWAP value (32 bits) not allowed : " << swap <<
402 * \brief Deal with endianity i.e. re-arange bytes inside the integer
404 void gdcmPixelConvert::ConvertReorderEndianity( uint8_t* pixelZone,
405 size_t imageDataSize,
406 int numberBitsStored,
407 int numberBitsAllocated,
411 if ( numberBitsAllocated != 8 )
413 SwapZone( pixelZone, swapCode, imageDataSize, numberBitsAllocated );
416 // Special kludge in order to deal with xmedcon broken images:
417 if ( ( numberBitsAllocated == 16 )
418 && ( numberBitsStored < numberBitsAllocated )
419 && ( ! signedPixel ) )
421 int l = (int)(imageDataSize / (numberBitsAllocated/8));
422 uint16_t *deb = (uint16_t *)pixelZone;
423 for(int i = 0; i<l; i++)