]> Creatis software - gdcm.git/blob - src/gdcmRLE.cxx
Typos on refs to docs.
[gdcm.git] / src / gdcmRLE.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmRLE.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/08 04:52:55 $
7   Version:   $Revision: 1.25 $
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmFile.h"
20
21 #define str2num(str, typeNum) *((typeNum *)(str))
22
23 //-----------------------------------------------------------------------------
24 /**
25  * \ingroup   gdcmFile
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 
30  * @return    Boolean 
31  */
32 bool gdcmFile::gdcm_read_RLE_file (FILE* fp,void* image_buffer)
33 {
34    char * im = (char *)image_buffer;
35    long uncompressedSegmentSize = Header->GetXSize() * Header->GetYSize();
36    
37
38    // Loop on the frame[s]
39    for( gdcmRLEFramesInfo::RLEFrameList::iterator
40         it  = Header->RLEInfo.Frames.begin();
41         it != Header->RLEInfo.Frames.end();
42       ++it )
43    {
44        std::cout << "...new frame...\n ";
45       // Loop on the fragments
46       for( unsigned int k = 1; k <= (*it)->NumberFragments; k++ )
47       {
48          fseek( fp, (*it)->Offset[k] ,SEEK_SET);  
49          gdcm_read_RLE_fragment( &im, (*it)->Length[k],
50                                  uncompressedSegmentSize, fp );
51       }
52    }
53
54    if (Header->GetBitsAllocated()==16)
55    {
56       // try to deal with RLE 16 Bits
57    
58       im = (char *)image_buffer;
59          //  need to make 16 Bits Pixels from Low Byte and Hight Byte 'Planes'
60
61       int l = Header->GetXSize()*Header->GetYSize();
62       int nbFrames = Header->GetZSize();
63
64       char * newDest = new char[l*nbFrames*2];
65       char *x  = newDest;
66       char * a = (char *)image_buffer;
67       char * b = a + l;
68
69       for (int i=0;i<nbFrames;i++)
70       {
71          for (int j=0;j<l; j++)
72          {
73             *(x++) = *(a++);
74             *(x++) = *(b++);
75          }
76       }
77       memmove(image_buffer,newDest,ImageDataSize);
78       delete[] newDest;
79    }
80       
81    return true;
82 }
83
84
85 // ----------------------------------------------------------------------------
86 // RLE LossLess Fragment
87 int gdcmFile::gdcm_read_RLE_fragment(char** areaToRead, long lengthToDecode, 
88                                      long uncompressedSegmentSize, FILE* fp)
89 {
90    (void)lengthToDecode; //FIXME
91    int count;
92    long numberOfOutputBytes=0;
93    char n, car;
94
95    while( numberOfOutputBytes < uncompressedSegmentSize )
96    {
97       fread(&n,sizeof(char),1,fp);
98       count=n;
99       if (count >= 0 && count <= 127)
100       {
101          fread(*areaToRead,(count+1)*sizeof(char),1,fp);
102          *areaToRead+=count+1;
103          numberOfOutputBytes+=count+1;
104       }
105       else
106       {
107          if (count <= -1 && count >= -127)
108          {
109             fread(&car,sizeof(char),1,fp);
110             for(int i=0; i<-count+1; i++)
111             {
112                (*areaToRead)[i]=car;  
113             }
114             *areaToRead+=(-count+1);
115             numberOfOutputBytes+=(-count+1); 
116          }
117       } 
118       // if count = 128 output nothing (See : PS 3.5-2003 Page 86)
119    } 
120    return 1;
121 }
122
123 // ----------------------------------------------------------------------------