Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

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: 2005/02/05 01:37:09 $
00007   Version:   $Revision: 1.7 $
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
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 
00089    while( numberOfOutputBytes < rawSegmentSize )
00090    {
00091       fp->read( (char*)&count, 1 );
00092       numberOfReadBytes += 1;
00093       if ( count >= 0 )
00094       // Note: count <= 127 comparison is always true due to limited range
00095       //       of data type int8_t [since the maximum of an exact width
00096       //       signed integer of width N is 2^(N-1) - 1, which for int8_t
00097       //       is 127].
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       // if count = 128 output nothing
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 // Protected
00132 
00133 //-----------------------------------------------------------------------------
00134 // Private
00135 
00136 //-----------------------------------------------------------------------------
00137 // Print
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 } // end namespace gdcm
00159 

Generated on Thu Feb 10 22:17:59 2005 for gdcm by doxygen 1.3.6