1 /*=========================================================================
4 Module: $RCSfile: gdcmRLE.cxx,v $
6 Date: $Date: 2004/10/06 22:31:31 $
7 Version: $Revision: 1.24 $
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 #include <ctype.h> // For isprint()
23 #define str2num(str, typeNum) *((typeNum *)(str))
25 //-----------------------------------------------------------------------------
28 * \brief Reads a 'Run Length Encoded' Dicom encapsulated file
29 * @param fp already open File Pointer
30 * @param image_buffer destination Address (in caller's memory space)
31 * at which the pixel data should be copied
34 bool gdcmFile::gdcm_read_RLE_file (FILE* fp,void* image_buffer) {
35 char * im = (char *)image_buffer;
36 long uncompressedSegmentSize = Header->GetXSize() * Header->GetYSize();
39 // Loop on the frame[s]
40 for( gdcmRLEFramesInfo::RLEFrameList::iterator
41 it = Header->RLEInfo.Frames.begin();
42 it != Header->RLEInfo.Frames.end();
45 std::cout << "...new frame...\n ";
46 // Loop on the fragments
47 for( unsigned int k = 1; k <= (*it)->NumberFragments; k++ )
49 fseek( fp, (*it)->Offset[k] ,SEEK_SET);
50 gdcm_read_RLE_fragment( &im, (*it)->Length[k],
51 uncompressedSegmentSize, fp );
55 if (Header->GetBitsAllocated()==16) { // try to deal with RLE 16 Bits
57 im = (char *)image_buffer;
58 // need to make 16 Bits Pixels from Low Byte and Hight Byte 'Planes'
60 int l = Header->GetXSize()*Header->GetYSize();
61 int nbFrames = Header->GetZSize();
63 char * newDest = new char[l*nbFrames*2];
65 char * a = (char *)image_buffer;
68 for (int i=0;i<nbFrames;i++) {
69 for (int j=0;j<l; j++) {
74 memmove(image_buffer,newDest,ImageDataSize);
82 // ----------------------------------------------------------------------------
83 // RLE LossLess Fragment
84 int gdcmFile::gdcm_read_RLE_fragment(char** areaToRead, long lengthToDecode,
85 long uncompressedSegmentSize, FILE* fp) {
86 (void)lengthToDecode; //FIXME
88 long numberOfOutputBytes=0;
91 while( numberOfOutputBytes < uncompressedSegmentSize )
93 fread(&n,sizeof(char),1,fp);
95 if (count >= 0 && count <= 127) {
96 fread(*areaToRead,(count+1)*sizeof(char),1,fp);
98 numberOfOutputBytes+=count+1;
100 if (count <= -1 && count >= -127) {
101 fread(&car,sizeof(char),1,fp);
102 for(int i=0; i<-count+1; i++) {
103 (*areaToRead)[i]=car;
105 *areaToRead+=(-count+1);
106 numberOfOutputBytes+=(-count+1);
109 // if count = 128 output nothing (See : PS 3.5-2003 Page 86)
114 // ----------------------------------------------------------------------------