]> Creatis software - gdcm.git/blob - src/gdcmBinEntry.cxx
* ENH - Comment out Mathieu's (dangerous) Super Hack
[gdcm.git] / src / gdcmBinEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmBinEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/14 15:06:37 $
7   Version:   $Revision: 1.47 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmBinEntry.h"
20 #include "gdcmDebug.h"
21
22 #include <fstream>
23 #include <sstream>
24 #include <iostream> // for std::ios_base, since <ios> does not exist on gcc/Solaris
25
26 namespace gdcm 
27 {
28
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31
32 /**
33  * \brief   Constructor from a given BinEntry
34  */
35 BinEntry::BinEntry(DictEntry *e) : ValEntry(e)
36 {
37    BinArea = 0;
38    SelfArea = true;
39 }
40
41 /**
42  * \brief   Constructor from a given BinEntry
43  * @param   e Pointer to existing Doc entry
44  */
45 BinEntry::BinEntry(DocEntry *e) : ValEntry(e->GetDictEntry())
46 {
47    Copy(e);
48 /*   Length     = e->GetLength();
49    ReadLength = e->GetReadLength();
50    ImplicitVR = e->IsImplicitVR();
51    Offset     = e->GetOffset();*/
52
53    //FIXME
54    //SQDepthLevel = e->GetDepthLevel();
55
56    BinArea = 0; // let's be carefull !
57    SelfArea = true;
58 }
59
60 /**
61  * \brief   Canonical destructor.
62  */
63 BinEntry::~BinEntry()
64 {
65    if (BinArea && SelfArea)
66    {
67       delete[] BinArea;
68       BinArea = 0; // let's be carefull !
69    }
70 }
71
72
73 //-----------------------------------------------------------------------------
74 // Print
75 /*
76  * \brief   canonical Printer
77  */
78  
79 void BinEntry::Print(std::ostream &os)
80 {
81    os << "B ";
82    DocEntry::Print(os);
83    std::ostringstream s;
84    void* binArea = GetBinArea();
85    if (binArea)
86    {
87       //s << " [" << GDCM_BINLOADED 
88       s << " [" << GetValue()
89         << "; length = " << GetLength() << "]";
90    }
91    else
92    {
93       if ( GetLength() == 0 )
94       {
95          s << " []";
96       }
97       else 
98       {
99          //s << " [gdcm::Binary data NOT loaded]";
100          s << " [" <<GetValue() << "]";
101       }
102          
103    }
104    os << s.str();
105 }
106
107 /*
108  * \brief   canonical Writer
109  * @param fp already open file pointer
110  * @param filetype type of the file to be written
111 */
112 void BinEntry::WriteContent(std::ofstream *fp, FileType filetype)
113
114 #define BUFFER_SIZE 4096
115    DocEntry::WriteContent(fp, filetype);
116    void* binArea = GetBinArea();
117    int lgr = GetLength();
118    if (binArea) // the binArea was *actually* loaded
119    {
120
121    // TODO FIME
122    // Probabely, the same operation will have to be done when we want 
123    // to write image with Big Endian Transfert Syntax, 
124    //   and we are working onj Little Endian Processor
125
126 #ifdef GDCM_WORDS_BIGENDIAN
127       // Be carefull with *any* 16 bits words 'binEntries !'
128       // if ( GetVR() == "OW") // to be used later
129
130       // TODO FIXME Right now, we only care of Pixels element
131
132       // 8 Bits Pixels *are* OB, 16 Bits Pixels *are* OW
133       // -value forced while Reading process-
134       if (GetGroup == 0x7fe0 && GetVR() == "OW")
135       {     
136          unit16_t *currPosition = (uint16_t *)binArea;
137
138          // TODO FIXME : Maybe we should allocate somewhere a static buffer,
139          // in order not to have to alloc/delete for almost every BinEntry ...
140          unit16_t *buffer = new uint16[BUFFER_SIZE];
141
142          // how many BUFFER_SIZE long pieces in binArea ?
143          int nbPieces = lgr/BUFFER_SIZE/2; //(16 bits = 2 Bytes)
144          for (int j=0;j<nbPieces;j++)
145          {
146             for (int i = 0; i < BUFFER_SIZE/2; i++)
147             {
148                buffer[i] =   (uint16_t *)binArea[i] >> 8 
149                            | (uint16_t *)binArea[i] << 8;  
150             }
151             fp->write ( (char*)currPosition, BUFFER_SIZE );
152             currPosition += BUFFER_SIZE/2;
153          }
154          int remainingSize = lgr%BUFFER_SIZE;
155          if ( remainingSize != 0)
156          {
157             fp->write ( (char*)currPosition, remainingSize );   
158          } 
159          delete buffer; 
160       }
161       else
162       { 
163          // For any other VR, BinEntry is re-written as-is
164          fp->write ( (char*)binArea, lgr );
165       }
166 #else
167       fp->write ( (char*)binArea, lgr ); // Elem value
168 #endif //GDCM_WORDS_BIGENDIAN
169
170    }
171    else
172    {
173       // nothing was loaded, but we need to skip space on disc
174       fp->seekp(lgr, std::ios::cur);
175    }
176 }
177 //-----------------------------------------------------------------------------
178 // Public
179
180
181 /// \brief Sets the value (non string) of the current Dicom Header Entry
182 void BinEntry::SetBinArea( uint8_t *area, bool self )  
183
184    if (BinArea && SelfArea)
185       delete[] BinArea;
186
187    BinArea = area;
188    SelfArea=self;
189 }
190
191 //-----------------------------------------------------------------------------
192 // Protected
193
194 //-----------------------------------------------------------------------------
195 // Private
196    
197 //-----------------------------------------------------------------------------
198 } // end namespace gdcm