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
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
00089 while( numberOfOutputBytes < rawSegmentSize )
00090 {
00091 fp->read( (char*)&count, 1 );
00092 numberOfReadBytes += 1;
00093 if ( count >= 0 )
00094
00095
00096
00097
00098 {
00099 fp->read( (char*)subRaw, count + 1);
00100 numberOfReadBytes += count + 1;
00101 subRaw += count + 1;
00102 numberOfOutputBytes += count + 1;
00103 }
00104 else
00105 {
00106 if ( count <= -1 && count >= -127 )
00107 {
00108 int8_t newByte;
00109 fp->read( (char*)&newByte, 1);
00110 numberOfReadBytes += 1;
00111 for( int i = 0; i < -count + 1; i++ )
00112 {
00113 subRaw[i] = newByte;
00114 }
00115 subRaw += -count + 1;
00116 numberOfOutputBytes += -count + 1;
00117 }
00118 }
00119
00120
00121 if ( numberOfReadBytes > fragmentSize )
00122 {
00123 gdcmWarningMacro( "Read more bytes than the segment size.");
00124 return false;
00125 }
00126 }
00127 return true;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00143 void RLEFrame::Print( std::ostream &os, std::string indent )
00144 {
00145 os << indent
00146 << "--- fragments"
00147 << std::endl;
00148 for ( unsigned int i = 0; i < NumberOfFragments; i++ )
00149 {
00150 os << indent
00151 << " offset : " << Offset[i]
00152 << " length : " << Length[i]
00153 << std::endl;
00154 }
00155 }
00156
00157
00158 }
00159