00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmRLEFrame.h"
00020 #include "gdcmDebug.h"
00021
00022 namespace GDCM_NAME_SPACE
00023 {
00024
00025
00026
00027
00028
00029 void RLEFrame::SetOffset(unsigned int id,long offset)
00030 {
00031 gdcmAssertMacro(id<15);
00032 Offset[id] = offset;
00033 }
00034
00035 long RLEFrame::GetOffset(unsigned int id)
00036 {
00037 gdcmAssertMacro(id<15);
00038 return Offset[id];
00039 }
00040
00041 void RLEFrame::SetLength(unsigned int id,long length)
00042 {
00043 gdcmAssertMacro(id<15);
00044 Length[id] = length;
00045 }
00046
00047 long RLEFrame::GetLength(unsigned int id)
00048 {
00049 gdcmAssertMacro(id<15);
00050 return Length[id];
00051 }
00052
00053 uint8_t *RLEFrame::ReadAndDecompressRLEFrame( uint8_t *subRaw,
00054 long rawSegmentSize,
00055 std::ifstream *fp )
00056 {
00057
00058 for( unsigned int k = 1; k <= NumberOfFragments; k++ )
00059 {
00060
00061 fp->seekg(Offset[k], std::ios::beg);
00062 ReadAndDecompressRLEFragment(subRaw, Length[k],
00063 rawSegmentSize, fp);
00064 subRaw += rawSegmentSize;
00065 }
00066
00067 return subRaw;
00068 }
00069
00079 bool RLEFrame::ReadAndDecompressRLEFragment( uint8_t *subRaw,
00080 long fragmentSize,
00081 long rawSegmentSize,
00082 std::ifstream *fp )
00083 {
00084 int8_t count;
00085 long numberOfOutputBytes = 0;
00086 long numberOfReadBytes = 0;
00087
00088 while( numberOfOutputBytes < rawSegmentSize )
00089 {
00090 fp->read( (char*)&count, 1 );
00091 numberOfReadBytes += 1;
00092 if ( count >= 0 )
00093
00094
00095
00096
00097 {
00098 fp->read( (char*)subRaw, count + 1);
00099 numberOfReadBytes += count + 1;
00100 subRaw += count + 1;
00101 numberOfOutputBytes += count + 1;
00102 }
00103 else
00104 {
00105 if ( count <= -1 && count >= -127 )
00106 {
00107 int8_t newByte;
00108 fp->read( (char*)&newByte, 1);
00109 numberOfReadBytes += 1;
00110 for( int i = 0; i < -count + 1; i++ )
00111 {
00112 subRaw[i] = newByte;
00113 }
00114 subRaw += -count + 1;
00115 numberOfOutputBytes += -count + 1;
00116 }
00117 }
00118
00119
00120 if ( numberOfReadBytes > fragmentSize )
00121 {
00122 gdcmWarningMacro( "Read more bytes (" << numberOfReadBytes
00123 << " ) than the segment size. ("
00124 << fragmentSize << ")" );
00125 return false;
00126 }
00127 }
00128 return true;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00144 void RLEFrame::Print( std::ostream &os, std::string const &indent )
00145 {
00146 os << indent
00147 << "--- fragments"
00148 << std::endl;
00149 for ( unsigned int i = 0; i < NumberOfFragments; i++ )
00150 {
00151 os << indent
00152 << " offset : " << Offset[i]
00153 << " length : " << Length[i]
00154 << std::endl;
00155 }
00156 }
00157
00158
00159 }
00160