1 /*=========================================================================
4 Module: $RCSfile: gdcmRLE.cxx,v $
6 Date: $Date: 2004/10/08 04:52:55 $
7 Version: $Revision: 1.25 $
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 =========================================================================*/
21 #define str2num(str, typeNum) *((typeNum *)(str))
23 //-----------------------------------------------------------------------------
26 * \brief Reads a 'Run Length Encoded' Dicom encapsulated file
27 * @param fp already open File Pointer
28 * @param image_buffer destination Address (in caller's memory space)
29 * at which the pixel data should be copied
32 bool gdcmFile::gdcm_read_RLE_file (FILE* fp,void* image_buffer)
34 char * im = (char *)image_buffer;
35 long uncompressedSegmentSize = Header->GetXSize() * Header->GetYSize();
38 // Loop on the frame[s]
39 for( gdcmRLEFramesInfo::RLEFrameList::iterator
40 it = Header->RLEInfo.Frames.begin();
41 it != Header->RLEInfo.Frames.end();
44 std::cout << "...new frame...\n ";
45 // Loop on the fragments
46 for( unsigned int k = 1; k <= (*it)->NumberFragments; k++ )
48 fseek( fp, (*it)->Offset[k] ,SEEK_SET);
49 gdcm_read_RLE_fragment( &im, (*it)->Length[k],
50 uncompressedSegmentSize, fp );
54 if (Header->GetBitsAllocated()==16)
56 // try to deal with RLE 16 Bits
58 im = (char *)image_buffer;
59 // need to make 16 Bits Pixels from Low Byte and Hight Byte 'Planes'
61 int l = Header->GetXSize()*Header->GetYSize();
62 int nbFrames = Header->GetZSize();
64 char * newDest = new char[l*nbFrames*2];
66 char * a = (char *)image_buffer;
69 for (int i=0;i<nbFrames;i++)
71 for (int j=0;j<l; j++)
77 memmove(image_buffer,newDest,ImageDataSize);
85 // ----------------------------------------------------------------------------
86 // RLE LossLess Fragment
87 int gdcmFile::gdcm_read_RLE_fragment(char** areaToRead, long lengthToDecode,
88 long uncompressedSegmentSize, FILE* fp)
90 (void)lengthToDecode; //FIXME
92 long numberOfOutputBytes=0;
95 while( numberOfOutputBytes < uncompressedSegmentSize )
97 fread(&n,sizeof(char),1,fp);
99 if (count >= 0 && count <= 127)
101 fread(*areaToRead,(count+1)*sizeof(char),1,fp);
102 *areaToRead+=count+1;
103 numberOfOutputBytes+=count+1;
107 if (count <= -1 && count >= -127)
109 fread(&car,sizeof(char),1,fp);
110 for(int i=0; i<-count+1; i++)
112 (*areaToRead)[i]=car;
114 *areaToRead+=(-count+1);
115 numberOfOutputBytes+=(-count+1);
118 // if count = 128 output nothing (See : PS 3.5-2003 Page 86)
123 // ----------------------------------------------------------------------------