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

gdcmBinEntry.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmBinEntry.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2005/02/10 14:23:18 $
00007   Version:   $Revision: 1.68 $
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 "gdcmBinEntry.h"
00020 #include "gdcmContentEntry.h"
00021 
00022 #include "gdcmDebug.h"
00023 
00024 #include <fstream>
00025 #include <sstream>
00026 #include <iostream> // for std::ios_base, since <ios> does not exist on gcc/Solaris
00027 
00028 namespace gdcm 
00029 {
00030 //-----------------------------------------------------------------------------
00031 // Constructor / Destructor
00035 BinEntry::BinEntry(DictEntry *e) 
00036          :ContentEntry(e)
00037 {
00038    BinArea = 0;
00039    SelfArea = true;
00040 }
00041 
00046 BinEntry::BinEntry(DocEntry *e) 
00047         : ContentEntry(e->GetDictEntry())
00048 {
00049    Copy(e);
00050 
00051    BinArea = 0;
00052    SelfArea = true;
00053 }
00054 
00058 BinEntry::~BinEntry()
00059 {
00060    if (BinArea && SelfArea)
00061    {
00062       delete[] BinArea;
00063       BinArea = 0; // let's be carefull !
00064    }
00065 }
00066 
00067 //-----------------------------------------------------------------------------
00068 // Public
00074 void BinEntry::WriteContent(std::ofstream *fp, FileType filetype)
00075 { 
00076    DocEntry::WriteContent(fp, filetype);
00077    void* binArea = GetBinArea();
00078    int lgr = GetLength();
00079    if (binArea) // the binArea was *actually* loaded
00080    {
00081 
00082    // TODO FIME
00083    // Probabely, the same operation will have to be done when we want 
00084    // to write image with Big Endian Transfert Syntax, 
00085    //   and we are working on Little Endian Processor
00086 
00087 #ifdef GDCM_WORDS_BIGENDIAN
00088       const int BUFFER_SIZE = 4096;
00089       // TODO FIXME Right now, we only care of Pixels element
00090 
00091       // 8 Bits Pixels *are* OB, 16 Bits Pixels *are* OW
00092       // -value forced while Reading process-
00093       if (GetGroup() == 0x7fe0 && GetVR() == "OW")
00094       {     
00095          uint16_t *buffer = new uint16_t[BUFFER_SIZE/2];
00096 
00097          // how many BUFFER_SIZE long pieces in binArea ?
00098          int nbPieces = lgr/BUFFER_SIZE; //(16 bits = 2 Bytes)
00099          int remainingSize = lgr%BUFFER_SIZE;
00100 
00101          uint16_t *binArea16 = (uint16_t*)binArea;
00102          for (int j=0;j<nbPieces;j++)
00103          {
00104             for (int i = 0; i < BUFFER_SIZE/2; i++)
00105             {
00106                //buffer[i] =  (binArea16[i] >> 8) | (binArea16[i] << 8);
00107                uint16_t val = binArea16[i];
00108                buffer[i] = ((( val << 8 ) & 0xff00 ) | (( val >> 8 ) & 0x00ff ) );
00109             }
00110             fp->write ( (char*)buffer, BUFFER_SIZE );
00111             binArea16 += BUFFER_SIZE/2;
00112          }
00113          if ( remainingSize > 0)
00114          {
00115             for (int i = 0; i < remainingSize/2; i++)
00116             {
00117                //buffer[i] =  (binArea16[i] >> 8) | (binArea16[i] << 8);
00118                uint16_t val = binArea16[i];
00119                buffer[i] = ((( val << 8 ) & 0xff00 ) | (( val >> 8 ) & 0x00ff ) );
00120             }
00121             fp->write ( (char*)buffer, remainingSize );
00122          } 
00123          delete[] buffer;
00124       }
00125       else
00126       { 
00127          // For any other VR, BinEntry is re-written as-is
00128          fp->write ( (char*)binArea, lgr );
00129       }
00130 #else
00131       fp->write ( (char*)binArea, lgr ); // Elem value
00132 #endif //GDCM_WORDS_BIGENDIAN
00133 
00134    }
00135    else
00136    {
00137       // nothing was loaded, but we need to skip space on disc
00138       fp->seekp(lgr, std::ios::cur);
00139    }
00140 }
00141 
00145 void BinEntry::SetBinArea( uint8_t *area, bool self )  
00146 { 
00147    if (BinArea && SelfArea)
00148       delete[] BinArea;
00149 
00150    BinArea = area;
00151    SelfArea=self;
00152 }
00153 
00154 //-----------------------------------------------------------------------------
00155 // Protected
00156 
00157 //-----------------------------------------------------------------------------
00158 // Private
00159    
00160 //-----------------------------------------------------------------------------
00161 // Print
00167 void BinEntry::Print(std::ostream &os, std::string const & )
00168 {
00169    os << "B ";
00170    DocEntry::Print(os);
00171    std::ostringstream s;
00172    void* binArea = GetBinArea();
00173    if (binArea)
00174    {
00175       s << " [" << GetValue()
00176         << "; length = " << GetLength() << "]";
00177    }
00178    else
00179    {
00180       if ( GetLength() == 0 )
00181       {
00182          s << " []";
00183       }
00184       else 
00185       {
00186          s << " [" <<GetValue() << "]";
00187       }         
00188    }
00189    os << s.str();
00190 }
00191 
00192 //-----------------------------------------------------------------------------
00193 } // end namespace gdcm

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