gdcmRLEFrame.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmRLEFrame.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2007/05/23 14:18:11 $
00007   Version:   $Revision: 1.13 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
00016                                                                                 
00017 =========================================================================*/
00018                                                                                 
00019 #include "gdcmRLEFrame.h"
00020 #include "gdcmDebug.h"
00021                                                                                 
00022 namespace GDCM_NAME_SPACE
00023 {
00024 //-------------------------------------------------------------------------
00025 // Constructor / Destructor
00026 
00027 //-----------------------------------------------------------------------------
00028 // Public
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    // Loop on the fragments
00058    for( unsigned int k = 1; k <= NumberOfFragments; k++ )
00059    {
00060       // First thing need to reset file to proper position:
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       // Note: count <= 127 comparison is always true due to limited range
00094       //       of data type int8_t [since the maximum of an exact width
00095       //       signed integer of width N is 2^(N-1) - 1, which for int8_t
00096       //       is 127].
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       // if count = 128 output nothing
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 // Protected
00133 
00134 //-----------------------------------------------------------------------------
00135 // Private
00136 
00137 //-----------------------------------------------------------------------------
00138 // Print
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 } // end namespace gdcm
00160 

Generated on Fri Aug 24 12:53:18 2007 for gdcm by  doxygen 1.4.6