1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelConvert.cxx,v $
6 Date: $Date: 2004/10/11 03:47:50 $
7 Version: $Revision: 1.7 $
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
23 // grep PIXELCONVERT everywhere and clean up !
26 #define str2num(str, typeNum) *((typeNum *)(str))
28 #include "gdcmDebug.h"
29 #include "gdcmPixelConvert.h"
31 // External JPEG decompression
33 // for JPEGLosslessDecodeImage
34 #include "jpeg/ljpg/jpegless.h"
36 // For JPEG 2000, body in file gdcmJpeg2000.cxx
37 bool gdcm_read_JPEG2000_file (FILE* fp, void* image_buffer);
39 // For JPEG 8 Bits, body in file gdcmJpeg8.cxx
40 bool gdcm_read_JPEG_file (FILE* fp, void* image_buffer);
42 // For JPEG 12 Bits, body in file gdcmJpeg12.cxx
43 bool gdcm_read_JPEG_file12 (FILE* fp, void* image_buffer);
47 //-----------------------------------------------------------------------------
48 // Constructor / Destructor
49 gdcmPixelConvert::gdcmPixelConvert()
57 void gdcmPixelConvert::Squeeze()
63 delete [] Uncompressed;
67 gdcmPixelConvert::~gdcmPixelConvert()
72 void gdcmPixelConvert::AllocateRGB()
77 RGB = new uint8_t[RGBSize];
80 void gdcmPixelConvert::AllocateUncompressed()
83 delete [] Uncompressed;
85 Uncompressed = new uint8_t[ UncompressedSize ];
89 * \brief Read from file a 12 bits per pixel image and uncompress it
90 * into a 16 bits per pixel image.
92 void gdcmPixelConvert::ConvertDecompress12BitsTo16Bits(
97 throw ( gdcmFormatError )
99 int nbPixels = sizeX * sizeY;
100 uint16_t* destination = (uint16_t*)pixelZone;
102 for( int p = 0; p < nbPixels; p += 2 )
107 ItemRead = fread( &b0, 1, 1, filePtr);
110 throw gdcmFormatError( "gdcmFile::ConvertDecompress12BitsTo16Bits()",
111 "Unfound first block" );
114 ItemRead = fread( &b1, 1, 1, filePtr);
117 throw gdcmFormatError( "gdcmFile::ConvertDecompress12BitsTo16Bits()",
118 "Unfound second block" );
121 ItemRead = fread( &b2, 1, 1, filePtr);
124 throw gdcmFormatError( "gdcmFile::ConvertDecompress12BitsTo16Bits()",
125 "Unfound second block" );
128 // Two steps are necessary to please VC++
130 // 2 pixels 12bit = [0xABCDEF]
131 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
133 *destination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
135 *destination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
137 /// \todo JPR Troubles expected on Big-Endian processors ?
142 * \brief Try to deal with RLE 16 Bits.
143 * We assume the RLE has allready been parsed and loaded in
144 * Uncompressed (through \ref ReadAndDecompressJPEGFile ).
145 * We here need to make 16 Bits Pixels from Low Byte and
146 * High Byte 'Planes'...(for what it may mean)
149 bool gdcmPixelConvert::UncompressRLE16BitsFromRLE8Bits(
153 uint8_t* fixMemUncompressed )
155 size_t PixelNumber = XSize * YSize;
156 size_t fixMemUncompressedSize = XSize * YSize * NumberOfFrames;
158 // We assumed Uncompressed contains the decoded RLE pixels but as
159 // 8 bits per pixel. In order to convert those pixels to 16 bits
160 // per pixel we cannot work in place within Uncompressed and hence
161 // we copy Uncompressed in a safe place, say OldUncompressed.
163 uint8_t* OldUncompressed = new uint8_t[ fixMemUncompressedSize * 2 ];
164 memmove( OldUncompressed, fixMemUncompressed, fixMemUncompressedSize * 2);
166 uint8_t* x = fixMemUncompressed;
167 uint8_t* a = OldUncompressed;
168 uint8_t* b = a + PixelNumber;
170 for ( int i = 0; i < NumberOfFrames; i++ )
172 for ( unsigned int j = 0; j < PixelNumber; j++ )
179 delete[] OldUncompressed;
181 /// \todo check that operator new []didn't fail, and sometimes return false
186 * \brief Implementation of the RLE decoding algorithm for uncompressing
187 * a RLE fragment. [refer to PS 3.5-2003, section G.3.2 p 86]
189 bool gdcmPixelConvert::ReadAndUncompressRLEFragment( uint8_t* decodedZone,
191 long uncompressedSegmentSize,
195 long numberOfOutputBytes = 0;
196 long numberOfReadBytes = 0;
198 while( numberOfOutputBytes < uncompressedSegmentSize )
200 fread( &count, 1, 1, fp );
201 numberOfReadBytes += 1;
203 // Note: count <= 127 comparison is always true due to limited range
204 // of data type int8_t [since the maximum of an exact width
205 // signed integer of width N is 2^(N-1) - 1, which for int8_t
208 fread( decodedZone, count + 1, 1, fp);
209 numberOfReadBytes += count + 1;
210 decodedZone += count + 1;
211 numberOfOutputBytes += count + 1;
215 if ( ( count <= -1 ) && ( count >= -127 ) )
218 fread( &newByte, 1, 1, fp);
219 numberOfReadBytes += 1;
220 for( int i = 0; i < -count + 1; i++ )
222 decodedZone[i] = newByte;
224 decodedZone += -count + 1;
225 numberOfOutputBytes += -count + 1;
228 // if count = 128 output nothing
230 if ( numberOfReadBytes > fragmentSize )
232 dbg.Verbose(0, "gdcmFile::gdcm_read_RLE_fragment: we read more "
233 "bytes than the segment size.");
241 * \brief Reads from disk the Pixel Data of 'Run Length Encoded'
242 * Dicom encapsulated file and uncompress it.
243 * @param fp already open File Pointer
244 * @param image_buffer destination Address (in caller's memory space)
245 * at which the pixel data should be copied
248 bool gdcmPixelConvert::ReadAndDecompressRLEFile( void* image_buffer,
253 gdcmRLEFramesInfo* RLEInfo,
256 uint8_t* im = (uint8_t*)image_buffer;
257 long uncompressedSegmentSize = XSize * YSize;
260 // Loop on the frame[s]
261 for( gdcmRLEFramesInfo::RLEFrameList::iterator
262 it = RLEInfo->Frames.begin();
263 it != RLEInfo->Frames.end();
266 // Loop on the fragments
267 for( unsigned int k = 1; k <= (*it)->NumberFragments; k++ )
269 fseek( fp, (*it)->Offset[k] ,SEEK_SET );
270 (void)gdcmPixelConvert::ReadAndUncompressRLEFragment(
271 (uint8_t*) im, (*it)->Length[k],
272 uncompressedSegmentSize, fp );
273 im += uncompressedSegmentSize;
277 if ( BitsAllocated == 16 )
279 // Try to deal with RLE 16 Bits
280 (void)gdcmPixelConvert::UncompressRLE16BitsFromRLE8Bits(
284 (uint8_t*) image_buffer);
291 * \brief Swap the bytes, according to swap code.
292 * \warning not end user intended
293 * @param im area to deal with
294 * @param swap swap code
295 * @param lgr Area Length
296 * @param nb Pixels Bit number
298 void gdcmPixelConvert::SwapZone(void* im, int swap, int lgr, int nb)
304 uint16_t* im16 = (uint16_t*)im;
315 for(i=0; i < lgr/2; i++)
317 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
321 std::cout << "SWAP value (16 bits) not allowed :i" << swap <<
328 uint16_t fort, faible;
329 uint32_t* im32 = (uint32_t*)im;
336 for(i = 0; i < lgr/4; i++)
338 faible = im32[i] & 0x0000ffff; // 4321
339 fort = im32[i] >> 16;
340 fort = ( fort >> 8 ) | ( fort << 8 );
341 faible = ( faible >> 8 ) | ( faible << 8);
343 im32[i] = ( s32 << 16 ) | fort;
347 for(i = 0; i < lgr/4; i++)
349 faible = im32[i] & 0x0000ffff; // 2143
350 fort = im32[i] >> 16;
351 fort = ( fort >> 8 ) | ( fort << 8 );
352 faible = ( faible >> 8) | ( faible << 8);
354 im32[i] = ( s32 << 16 ) | faible;
358 for(i = 0; i < lgr/4; i++)
360 faible = im32[i] & 0x0000ffff; // 3412
361 fort = im32[i] >> 16;
363 im32[i] = ( s32 << 16 ) | fort;
367 std::cout << "SWAP value (32 bits) not allowed : " << swap <<
376 * \brief Deal with endianity i.e. re-arange bytes inside the integer
378 void gdcmPixelConvert::ConvertReorderEndianity( uint8_t* pixelZone,
379 size_t imageDataSize,
380 int numberBitsStored,
381 int numberBitsAllocated,
385 if ( numberBitsAllocated != 8 )
387 SwapZone( pixelZone, swapCode, imageDataSize, numberBitsAllocated );
390 // Special kludge in order to deal with xmedcon broken images:
391 if ( ( numberBitsAllocated == 16 )
392 && ( numberBitsStored < numberBitsAllocated )
393 && ( ! signedPixel ) )
395 int l = (int)(imageDataSize / (numberBitsAllocated/8));
396 uint16_t *deb = (uint16_t *)pixelZone;
397 for(int i = 0; i<l; i++)
409 * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
410 & file and uncompress it.
411 * @param fp already open File Pointer
412 * @param destination Where decompressed fragments should end up
415 bool gdcmPixelConvert::ReadAndDecompressJPEGFile( uint8_t* destination,
424 gdcmJPEGFragmentsInfo* JPEGInfo,
427 // Loop on the fragment[s]
428 for( gdcmJPEGFragmentsInfo::JPEGFragmentsList::iterator
429 it = JPEGInfo->Fragments.begin();
430 it != JPEGInfo->Fragments.end();
433 fseek( fp, (*it)->Offset, SEEK_SET );
437 if ( ! gdcm_read_JPEG2000_file( fp, destination ) )
442 else if ( isJPEGLossless )
444 // JPEG LossLess : call to xmedcom Lossless JPEG
445 JPEGLosslessDecodeImage( fp,
446 (uint16_t*)destination,
447 PixelSize * 8 * SamplesPerPixel,
450 else if ( BitsStored == 8)
452 // JPEG Lossy : call to IJG 6b
453 if ( ! gdcm_read_JPEG_file ( fp, destination ) )
458 else if ( BitsStored == 12)
460 // Reading Fragment pixels
461 if ( ! gdcm_read_JPEG_file12 ( fp, destination ) )
468 // other JPEG lossy not supported
469 dbg.Error(" gdcmFile::ReadPixelData : unknown jpeg lossy "
474 // Advance to next free location in destination
475 // for next fragment decompression (if any)
476 int length = XSize * YSize * SamplesPerPixel;
477 int numberBytes = BitsAllocated / 8;
479 destination = (uint8_t*)destination + length * numberBytes;
486 * \brief Re-arrange the bits within the bytes.
487 * @param fp already open File Pointer
488 * @param destination Where decompressed fragments should end up
491 bool gdcmPixelConvert::ConvertReArrangeBits(
493 size_t imageDataSize,
494 int numberBitsStored,
495 int numberBitsAllocated,
496 int highBitPosition )
497 throw ( gdcmFormatError )
499 if ( numberBitsStored != numberBitsAllocated )
501 int l = (int)(imageDataSize / (numberBitsAllocated/8));
502 if ( numberBitsAllocated == 16 )
504 uint16_t mask = 0xffff;
505 mask = mask >> ( numberBitsAllocated - numberBitsStored );
506 uint16_t* deb = (uint16_t*)pixelZone;
507 for(int i = 0; i<l; i++)
509 *deb = (*deb >> (numberBitsStored - highBitPosition - 1)) & mask;
513 else if ( numberBitsAllocated == 32 )
515 uint32_t mask = 0xffffffff;
516 mask = mask >> ( numberBitsAllocated - numberBitsStored );
517 uint32_t* deb = (uint32_t*)pixelZone;
518 for(int i = 0; i<l; i++)
520 *deb = (*deb >> (numberBitsStored - highBitPosition - 1)) & mask;
526 dbg.Verbose(0, "gdcmPixelConvert::ConvertReArrangeBits: weird image");
527 throw gdcmFormatError( "gdcmFile::ConvertReArrangeBits()",