]> Creatis software - gdcm.git/blob - src/gdcmRLEFrame.cxx
* src/*.cxx : first parss to normalize file organisation
[gdcm.git] / src / gdcmRLEFrame.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmRLEFrame.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/01 10:29:56 $
7   Version:   $Revision: 1.5 $
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 "gdcmRLEFrame.h"
20 #include "gdcmDebug.h"
21                                                                                 
22 namespace gdcm
23 {
24 //-------------------------------------------------------------------------
25 // Constructor / Destructor
26
27 //-----------------------------------------------------------------------------
28 // Public
29 void RLEFrame::SetOffset(unsigned int id,long offset)
30 {
31    gdcmAssertMacro(id<15);
32    Offset[id] = offset;
33 }
34
35 long RLEFrame::GetOffset(unsigned int id)
36 {
37    gdcmAssertMacro(id<15);
38    return Offset[id];
39 }
40
41 void RLEFrame::SetLength(unsigned int id,long length)
42 {
43    gdcmAssertMacro(id<15);
44    Length[id] = length;
45 }
46
47 long RLEFrame::GetLength(unsigned int id)
48 {
49    gdcmAssertMacro(id<15);
50    return Length[id];
51 }
52
53 uint8_t *RLEFrame::ReadAndDecompressRLEFrame( uint8_t *subRaw,
54                                           long rawSegmentSize,
55                                           std::ifstream *fp )
56 {
57    // Loop on the fragments
58    for( unsigned int k = 1; k <= NumberOfFragments; k++ )
59    {
60       // First thing need to reset file to proper position:
61       fp->seekg(Offset[k], std::ios::beg);
62       ReadAndDecompressRLEFragment(subRaw, Length[k],
63                                    rawSegmentSize, fp);
64       subRaw += rawSegmentSize;
65    }
66
67    return subRaw;
68 }
69
70 /**
71  * \brief Implementation of the RLE decoding algorithm for decompressing
72  *        a RLE fragment. [refer to PS 3.5-2003, section G.3.2 p 86]
73  * @param subRaw Sub region of \ref Raw where the decoded fragment
74  *        should be placed.
75  * @param fragmentSize The length of the binary fragment as found on the disk.
76  * @param RawSegmentSize The expected length of the fragment ONCE
77  *        Raw.
78  * @param fp File Pointer: on entry the position should be the one of
79  *        the fragment to be decoded.
80  */
81 bool RLEFrame::ReadAndDecompressRLEFragment( uint8_t *subRaw,
82                                              long fragmentSize,
83                                              long rawSegmentSize,
84                                              std::ifstream *fp )
85 {
86    int8_t count;
87    long numberOfOutputBytes = 0;
88    long numberOfReadBytes = 0;
89
90
91    while( numberOfOutputBytes < rawSegmentSize )
92    {
93       fp->read( (char*)&count, 1 );
94       numberOfReadBytes += 1;
95       if ( count >= 0 )
96       // Note: count <= 127 comparison is always true due to limited range
97       //       of data type int8_t [since the maximum of an exact width
98       //       signed integer of width N is 2^(N-1) - 1, which for int8_t
99       //       is 127].
100       {
101          fp->read( (char*)subRaw, count + 1);
102          numberOfReadBytes   += count + 1;
103          subRaw     += count + 1;
104          numberOfOutputBytes += count + 1;
105       }
106       else
107       {
108          if ( count <= -1 && count >= -127 )
109          {
110             int8_t newByte;
111             fp->read( (char*)&newByte, 1);
112             numberOfReadBytes += 1;
113             for( int i = 0; i < -count + 1; i++ )
114             {
115                subRaw[i] = newByte;
116             }
117             subRaw     += -count + 1;
118             numberOfOutputBytes += -count + 1;
119          }
120       }
121       // if count = 128 output nothing
122                                                                                 
123       if ( numberOfReadBytes > fragmentSize )
124       {
125          gdcmVerboseMacro( "Read more bytes than the segment size.");
126          return false;
127       }
128    }
129    return true;
130 }
131
132 //-----------------------------------------------------------------------------
133 // Protected
134
135 //-----------------------------------------------------------------------------
136 // Private
137
138 //-----------------------------------------------------------------------------
139 // Print
140 /**
141  * \brief        Print self.
142  * @param indent Indentation string to be prepended during printing.
143  * @param os     Stream to print to.
144  */
145 void RLEFrame::Print( std::ostream &os, std::string indent )
146 {
147    os << indent
148       << "--- fragments"
149       << std::endl;
150    for ( unsigned int i = 0; i < NumberOfFragments; i++ )
151    {
152       os << indent
153          << "   offset : " <<  Offset[i]
154          << "   length : " <<  Length[i]
155          << std::endl;
156    }
157 }
158
159 //-----------------------------------------------------------------------------
160 } // end namespace gdcm
161