1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelConvert.cxx,v $
6 Date: $Date: 2004/10/08 16:27:20 $
7 Version: $Revision: 1.3 $
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 bool gdcmPixelConvert::ReadAndUncompress12Bits( FILE* filePointer,
74 size_t uncompressedSize,
77 SetUncompressedSize( uncompressedSize );
78 AllocateUncompressed();
80 uint16_t* pdestination = (uint16_t*)Uncompressed;
82 for(int p = 0; p < PixelNumber; p += 2 )
84 // 2 pixels 12bit = [0xABCDEF]
85 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
88 ItemRead = fread( &b0, 1, 1, filePointer);
93 ItemRead = fread( &b1, 1, 1, filePointer);
98 ItemRead = fread( &b2, 1, 1, filePointer);
104 //Two steps are necessary to please VC++
105 *pdestination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
107 *pdestination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
110 /// \todo JPR Troubles expected on Big-Endian processors ?
116 * \brief Read from file an uncompressed image.
118 bool gdcmPixelConvert::ReadUncompressed( FILE* filePointer,
119 size_t uncompressedSize,
120 size_t expectedSize )
122 if ( expectedSize > uncompressedSize )
124 dbg.Verbose(0, "gdcmPixelConvert::ReadUncompressed: expectedSize"
125 "is bigger than it should");
128 SetUncompressedSize( uncompressedSize );
129 AllocateUncompressed();
130 size_t ItemRead = fread( (void*)Uncompressed, expectedSize, 1, filePointer);
139 * \brief Convert a Gray plane and ( Lut R, Lut G, Lut B ) into an
141 * @return True on success.
143 bool gdcmPixelConvert::ConvertGrayAndLutToRGB( uint8_t *lutRGBA )
146 /// We assume Uncompressed contains the decompressed gray plane
147 /// and build the RGB image.
148 SetRGBSize( UncompressedSize );
151 //aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
152 //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
153 //COPY HERE THE CODE OF GetImageDataIntoVector
155 /// \todo check that operator new []didn't fail, and sometimes return false
160 * \brief Try to deal with RLE 16 Bits.
161 * We assume the RLE has allready been parsed and loaded in
162 * Uncompressed (through \ref ReadAndUncompressRLE8Bits ).
163 * We here need to make 16 Bits Pixels from Low Byte and
164 * High Byte 'Planes'...(for what it may mean)
167 bool gdcmPixelConvert::UncompressRLE16BitsFromRLE8Bits(
171 uint8_t* fixMemUncompressed )
173 size_t PixelNumber = XSize * YSize;
174 size_t fixMemUncompressedSize = XSize * YSize * NumberOfFrames;
176 // We assumed Uncompressed contains the decoded RLE pixels but as
177 // 8 bits per pixel. In order to convert those pixels to 16 bits
178 // per pixel we cannot work in place within Uncompressed and hence
179 // we copy Uncompressed in a safe place, say OldUncompressed.
181 uint8_t* OldUncompressed = new uint8_t[ fixMemUncompressedSize * 2 ];
182 memmove( OldUncompressed, fixMemUncompressed, fixMemUncompressedSize * 2);
184 uint8_t* x = fixMemUncompressed;
185 uint8_t* a = OldUncompressed;
186 uint8_t* b = a + PixelNumber;
188 for ( int i = 0; i < NumberOfFrames; i++ )
190 for ( int j = 0; j < PixelNumber; j++ )
197 delete[] OldUncompressed;
199 /// \todo check that operator new []didn't fail, and sometimes return false
204 * \brief Implementation of the RLE decoding algorithm for uncompressing
205 * a RLE fragment. [refer to PS 3.5-2003, section G.3.2 p 86]
207 bool gdcmPixelConvert::ReadAndUncompressRLEFragment( uint8_t* decodedZone,
209 long uncompressedSegmentSize,
213 long numberOfOutputBytes = 0;
214 long numberOfReadBytes = 0;
216 while( numberOfOutputBytes < uncompressedSegmentSize )
218 fread( &count, 1, 1, fp );
219 numberOfReadBytes += 1;
221 // Note: count <= 127 comparison is always true due to limited range
222 // of data type int8_t [since the maximum of an exact width
223 // signed integer of width N is 2^(N-1) - 1, which for int8_t
226 fread( decodedZone, count + 1, 1, fp);
227 numberOfReadBytes += count + 1;
228 decodedZone += count + 1;
229 numberOfOutputBytes += count + 1;
233 if ( ( count <= -1 ) && ( count >= -127 ) )
236 fread( &newByte, 1, 1, fp);
237 numberOfReadBytes += 1;
238 for( int i = 0; i < -count + 1; i++ )
240 decodedZone[i] = newByte;
242 decodedZone += -count + 1;
243 numberOfOutputBytes += -count + 1;
246 // if count = 128 output nothing
248 if ( numberOfReadBytes > fragmentSize )
250 dbg.Verbose(0, "gdcmFile::gdcm_read_RLE_fragment: we read more "
251 "bytes than the segment size.");
259 * \brief Reads from disk the Pixel Data of 'Run Length Encoded'
260 * Dicom encapsulated file and uncompress it.
261 * @param fp already open File Pointer
262 * @param image_buffer destination Address (in caller's memory space)
263 * at which the pixel data should be copied
266 bool gdcmPixelConvert::gdcm_read_RLE_file( void* image_buffer,
271 gdcmRLEFramesInfo* RLEInfo,
274 uint8_t* im = (uint8_t*)image_buffer;
275 long uncompressedSegmentSize = XSize * YSize;
278 // Loop on the frame[s]
279 for( gdcmRLEFramesInfo::RLEFrameList::iterator
280 it = RLEInfo->Frames.begin();
281 it != RLEInfo->Frames.end();
284 // Loop on the fragments
285 for( unsigned int k = 1; k <= (*it)->NumberFragments; k++ )
287 fseek( fp, (*it)->Offset[k] ,SEEK_SET);
288 (void)gdcmPixelConvert::ReadAndUncompressRLEFragment(
289 (uint8_t*) im, (*it)->Length[k],
290 uncompressedSegmentSize, fp );
291 im += uncompressedSegmentSize;
295 if ( BitsAllocated == 16 )
297 // Try to deal with RLE 16 Bits
298 (void)gdcmPixelConvert::UncompressRLE16BitsFromRLE8Bits(
302 (uint8_t*) image_buffer);