1 /*=========================================================================
4 Module: $RCSfile: gdcmRLEFrame.cxx,v $
6 Date: $Date: 2005/11/28 17:24:21 $
7 Version: $Revision: 1.12 $
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 #include "gdcmRLEFrame.h"
20 #include "gdcmDebug.h"
24 //-------------------------------------------------------------------------
25 // Constructor / Destructor
27 //-----------------------------------------------------------------------------
29 void RLEFrame::SetOffset(unsigned int id,long offset)
31 gdcmAssertMacro(id<15);
35 long RLEFrame::GetOffset(unsigned int id)
37 gdcmAssertMacro(id<15);
41 void RLEFrame::SetLength(unsigned int id,long length)
43 gdcmAssertMacro(id<15);
47 long RLEFrame::GetLength(unsigned int id)
49 gdcmAssertMacro(id<15);
53 uint8_t *RLEFrame::ReadAndDecompressRLEFrame( uint8_t *subRaw,
57 // Loop on the fragments
58 for( unsigned int k = 1; k <= NumberOfFragments; k++ )
60 // First thing need to reset file to proper position:
61 fp->seekg(Offset[k], std::ios::beg);
62 ReadAndDecompressRLEFragment(subRaw, Length[k],
64 subRaw += rawSegmentSize;
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 where the decoded fragment should be placed.
74 * @param fragmentSize The length of the binary fragment as found on the disk.
75 * @param rawSegmentSize The expected length of the fragment ONCE Raw.
76 * @param fp File Pointer: on entry the position should be the one of
77 * the fragment to be decoded.
79 bool RLEFrame::ReadAndDecompressRLEFragment( uint8_t *subRaw,
85 long numberOfOutputBytes = 0;
86 long numberOfReadBytes = 0;
88 while( numberOfOutputBytes < rawSegmentSize )
90 fp->read( (char*)&count, 1 );
91 numberOfReadBytes += 1;
93 // Note: count <= 127 comparison is always true due to limited range
94 // of data type int8_t [since the maximum of an exact width
95 // signed integer of width N is 2^(N-1) - 1, which for int8_t
98 fp->read( (char*)subRaw, count + 1);
99 numberOfReadBytes += count + 1;
101 numberOfOutputBytes += count + 1;
105 if ( count <= -1 && count >= -127 )
108 fp->read( (char*)&newByte, 1);
109 numberOfReadBytes += 1;
110 for( int i = 0; i < -count + 1; i++ )
114 subRaw += -count + 1;
115 numberOfOutputBytes += -count + 1;
118 // if count = 128 output nothing
120 if ( numberOfReadBytes > fragmentSize )
122 gdcmWarningMacro( "Read more bytes (" << numberOfReadBytes
123 << " ) than the segment size. ("
124 << fragmentSize << ")" );
131 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
137 //-----------------------------------------------------------------------------
141 * @param indent Indentation string to be prepended during printing.
142 * @param os Stream to print to.
144 void RLEFrame::Print( std::ostream &os, std::string const &indent )
149 for ( unsigned int i = 0; i < NumberOfFragments; i++ )
152 << " offset : " << Offset[i]
153 << " length : " << Length[i]
158 //-----------------------------------------------------------------------------
159 } // end namespace gdcm