1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelConvert.cxx,v $
6 Date: $Date: 2004/10/12 04:35:47 $
7 Version: $Revision: 1.8 $
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 !
24 #include "gdcmDebug.h"
25 #include "gdcmPixelConvert.h"
27 // External JPEG decompression
29 // for JPEGLosslessDecodeImage
30 #include "jpeg/ljpg/jpegless.h"
36 #define str2num(str, typeNum) *((typeNum *)(str))
38 // For JPEG 2000, body in file gdcmJpeg2000.cxx
39 bool gdcm_read_JPEG2000_file (FILE* fp, void* image_buffer);
41 // For JPEG 8 Bits, body in file gdcmJpeg8.cxx
42 bool gdcm_read_JPEG_file (FILE* fp, void* image_buffer);
44 // For JPEG 12 Bits, body in file gdcmJpeg12.cxx
45 bool gdcm_read_JPEG_file12 (FILE* fp, void* image_buffer);
49 //-----------------------------------------------------------------------------
50 // Constructor / Destructor
51 PixelConvert::PixelConvert()
59 void PixelConvert::Squeeze()
65 delete [] Uncompressed;
69 PixelConvert::~PixelConvert()
74 void PixelConvert::AllocateRGB()
79 RGB = new uint8_t[RGBSize];
82 void PixelConvert::AllocateUncompressed()
85 delete [] Uncompressed;
87 Uncompressed = new uint8_t[ UncompressedSize ];
91 * \brief Read from file a 12 bits per pixel image and uncompress it
92 * into a 16 bits per pixel image.
94 void PixelConvert::ConvertDecompress12BitsTo16Bits(
101 int nbPixels = sizeX * sizeY;
102 uint16_t* destination = (uint16_t*)pixelZone;
104 for( int p = 0; p < nbPixels; p += 2 )
109 ItemRead = fread( &b0, 1, 1, filePtr);
112 throw FormatError( "File::ConvertDecompress12BitsTo16Bits()",
113 "Unfound first block" );
116 ItemRead = fread( &b1, 1, 1, filePtr);
119 throw FormatError( "File::ConvertDecompress12BitsTo16Bits()",
120 "Unfound second block" );
123 ItemRead = fread( &b2, 1, 1, filePtr);
126 throw FormatError( "File::ConvertDecompress12BitsTo16Bits()",
127 "Unfound second block" );
130 // Two steps are necessary to please VC++
132 // 2 pixels 12bit = [0xABCDEF]
133 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
135 *destination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
137 *destination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
139 /// \todo JPR Troubles expected on Big-Endian processors ?
144 * \brief Try to deal with RLE 16 Bits.
145 * We assume the RLE has allready been parsed and loaded in
146 * Uncompressed (through \ref ReadAndDecompressJPEGFile ).
147 * We here need to make 16 Bits Pixels from Low Byte and
148 * High Byte 'Planes'...(for what it may mean)
151 bool PixelConvert::UncompressRLE16BitsFromRLE8Bits(
155 uint8_t* fixMemUncompressed )
157 size_t PixelNumber = XSize * YSize;
158 size_t fixMemUncompressedSize = XSize * YSize * NumberOfFrames;
160 // We assumed Uncompressed contains the decoded RLE pixels but as
161 // 8 bits per pixel. In order to convert those pixels to 16 bits
162 // per pixel we cannot work in place within Uncompressed and hence
163 // we copy Uncompressed in a safe place, say OldUncompressed.
165 uint8_t* OldUncompressed = new uint8_t[ fixMemUncompressedSize * 2 ];
166 memmove( OldUncompressed, fixMemUncompressed, fixMemUncompressedSize * 2);
168 uint8_t* x = fixMemUncompressed;
169 uint8_t* a = OldUncompressed;
170 uint8_t* b = a + PixelNumber;
172 for ( int i = 0; i < NumberOfFrames; i++ )
174 for ( unsigned int j = 0; j < PixelNumber; j++ )
181 delete[] OldUncompressed;
183 /// \todo check that operator new []didn't fail, and sometimes return false
188 * \brief Implementation of the RLE decoding algorithm for uncompressing
189 * a RLE fragment. [refer to PS 3.5-2003, section G.3.2 p 86]
191 bool PixelConvert::ReadAndUncompressRLEFragment( uint8_t* decodedZone,
193 long uncompressedSegmentSize,
197 long numberOfOutputBytes = 0;
198 long numberOfReadBytes = 0;
200 while( numberOfOutputBytes < uncompressedSegmentSize )
202 fread( &count, 1, 1, fp );
203 numberOfReadBytes += 1;
205 // Note: count <= 127 comparison is always true due to limited range
206 // of data type int8_t [since the maximum of an exact width
207 // signed integer of width N is 2^(N-1) - 1, which for int8_t
210 fread( decodedZone, count + 1, 1, fp);
211 numberOfReadBytes += count + 1;
212 decodedZone += count + 1;
213 numberOfOutputBytes += count + 1;
217 if ( ( count <= -1 ) && ( count >= -127 ) )
220 fread( &newByte, 1, 1, fp);
221 numberOfReadBytes += 1;
222 for( int i = 0; i < -count + 1; i++ )
224 decodedZone[i] = newByte;
226 decodedZone += -count + 1;
227 numberOfOutputBytes += -count + 1;
230 // if count = 128 output nothing
232 if ( numberOfReadBytes > fragmentSize )
234 dbg.Verbose(0, "File::gdcm_read_RLE_fragment: we read more "
235 "bytes than the segment size.");
243 * \brief Reads from disk the Pixel Data of 'Run Length Encoded'
244 * Dicom encapsulated file and uncompress it.
245 * @param fp already open File Pointer
246 * @param image_buffer destination Address (in caller's memory space)
247 * at which the pixel data should be copied
250 bool PixelConvert::ReadAndDecompressRLEFile( void* image_buffer,
255 RLEFramesInfo* RLEInfo,
258 uint8_t* im = (uint8_t*)image_buffer;
259 long uncompressedSegmentSize = XSize * YSize;
262 // Loop on the frame[s]
263 for( RLEFramesInfo::RLEFrameList::iterator
264 it = RLEInfo->Frames.begin();
265 it != RLEInfo->Frames.end();
268 // Loop on the fragments
269 for( unsigned int k = 1; k <= (*it)->NumberFragments; k++ )
271 fseek( fp, (*it)->Offset[k] ,SEEK_SET );
272 (void)PixelConvert::ReadAndUncompressRLEFragment(
273 (uint8_t*) im, (*it)->Length[k],
274 uncompressedSegmentSize, fp );
275 im += uncompressedSegmentSize;
279 if ( BitsAllocated == 16 )
281 // Try to deal with RLE 16 Bits
282 (void)PixelConvert::UncompressRLE16BitsFromRLE8Bits(
286 (uint8_t*) image_buffer);
293 * \brief Swap the bytes, according to swap code.
294 * \warning not end user intended
295 * @param im area to deal with
296 * @param swap swap code
297 * @param lgr Area Length
298 * @param nb Pixels Bit number
300 void PixelConvert::SwapZone(void* im, int swap, int lgr, int nb)
306 uint16_t* im16 = (uint16_t*)im;
317 for(i=0; i < lgr/2; i++)
319 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
323 std::cout << "SWAP value (16 bits) not allowed :i" << swap <<
330 uint16_t fort, faible;
331 uint32_t* im32 = (uint32_t*)im;
338 for(i = 0; i < lgr/4; i++)
340 faible = im32[i] & 0x0000ffff; // 4321
341 fort = im32[i] >> 16;
342 fort = ( fort >> 8 ) | ( fort << 8 );
343 faible = ( faible >> 8 ) | ( faible << 8);
345 im32[i] = ( s32 << 16 ) | fort;
349 for(i = 0; i < lgr/4; i++)
351 faible = im32[i] & 0x0000ffff; // 2143
352 fort = im32[i] >> 16;
353 fort = ( fort >> 8 ) | ( fort << 8 );
354 faible = ( faible >> 8) | ( faible << 8);
356 im32[i] = ( s32 << 16 ) | faible;
360 for(i = 0; i < lgr/4; i++)
362 faible = im32[i] & 0x0000ffff; // 3412
363 fort = im32[i] >> 16;
365 im32[i] = ( s32 << 16 ) | fort;
369 std::cout << "SWAP value (32 bits) not allowed : " << swap <<
378 * \brief Deal with endianity i.e. re-arange bytes inside the integer
380 void PixelConvert::ConvertReorderEndianity( uint8_t* pixelZone,
381 size_t imageDataSize,
382 int numberBitsStored,
383 int numberBitsAllocated,
387 if ( numberBitsAllocated != 8 )
389 SwapZone( pixelZone, swapCode, imageDataSize, numberBitsAllocated );
392 // Special kludge in order to deal with xmedcon broken images:
393 if ( ( numberBitsAllocated == 16 )
394 && ( numberBitsStored < numberBitsAllocated )
395 && ( ! signedPixel ) )
397 int l = (int)(imageDataSize / (numberBitsAllocated/8));
398 uint16_t *deb = (uint16_t *)pixelZone;
399 for(int i = 0; i<l; i++)
411 * \brief Reads from disk the Pixel Data of JPEG Dicom encapsulated
412 & file and uncompress it.
413 * @param fp already open File Pointer
414 * @param destination Where decompressed fragments should end up
417 bool PixelConvert::ReadAndDecompressJPEGFile( uint8_t* destination,
426 JPEGFragmentsInfo* JPEGInfo,
429 // Loop on the fragment[s]
430 for( JPEGFragmentsInfo::JPEGFragmentsList::iterator
431 it = JPEGInfo->Fragments.begin();
432 it != JPEGInfo->Fragments.end();
435 fseek( fp, (*it)->Offset, SEEK_SET );
439 if ( ! gdcm_read_JPEG2000_file( fp, destination ) )
444 else if ( isJPEGLossless )
446 // JPEG LossLess : call to xmedcom Lossless JPEG
447 JPEGLosslessDecodeImage( fp,
448 (uint16_t*)destination,
449 PixelSize * 8 * SamplesPerPixel,
452 else if ( BitsStored == 8)
454 // JPEG Lossy : call to IJG 6b
455 if ( ! gdcm_read_JPEG_file ( fp, destination ) )
460 else if ( BitsStored == 12)
462 // Reading Fragment pixels
463 if ( ! gdcm_read_JPEG_file12 ( fp, destination ) )
470 // other JPEG lossy not supported
471 dbg.Error(" File::ReadPixelData : unknown jpeg lossy "
476 // Advance to next free location in destination
477 // for next fragment decompression (if any)
478 int length = XSize * YSize * SamplesPerPixel;
479 int numberBytes = BitsAllocated / 8;
481 destination = (uint8_t*)destination + length * numberBytes;
488 * \brief Re-arrange the bits within the bytes.
489 * @param fp already open File Pointer
490 * @param destination Where decompressed fragments should end up
493 bool PixelConvert::ConvertReArrangeBits(
495 size_t imageDataSize,
496 int numberBitsStored,
497 int numberBitsAllocated,
498 int highBitPosition )
499 throw ( FormatError )
501 if ( numberBitsStored != numberBitsAllocated )
503 int l = (int)(imageDataSize / (numberBitsAllocated/8));
504 if ( numberBitsAllocated == 16 )
506 uint16_t mask = 0xffff;
507 mask = mask >> ( numberBitsAllocated - numberBitsStored );
508 uint16_t* deb = (uint16_t*)pixelZone;
509 for(int i = 0; i<l; i++)
511 *deb = (*deb >> (numberBitsStored - highBitPosition - 1)) & mask;
515 else if ( numberBitsAllocated == 32 )
517 uint32_t mask = 0xffffffff;
518 mask = mask >> ( numberBitsAllocated - numberBitsStored );
519 uint32_t* deb = (uint32_t*)pixelZone;
520 for(int i = 0; i<l; i++)
522 *deb = (*deb >> (numberBitsStored - highBitPosition - 1)) & mask;
528 dbg.Verbose(0, "PixelConvert::ConvertReArrangeBits: weird image");
529 throw FormatError( "File::ConvertReArrangeBits()",
535 } // end namespace gdcm