]> Creatis software - gdcm.git/blob - src/gdcmBinEntry.cxx
* src/gdcmDocEntry.h : DocEntry is now secure. User can't change the
[gdcm.git] / src / gdcmBinEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmBinEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/19 15:58:00 $
7   Version:   $Revision: 1.54 $
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
49    //FIXME
50    //SQDepthLevel = e->GetDepthLevel();
51
52    BinArea = 0; // let's be carefull !
53    SelfArea = true;
54 }
55
56 /**
57  * \brief   Canonical destructor.
58  */
59 BinEntry::~BinEntry()
60 {
61    if (BinArea && SelfArea)
62    {
63       delete[] BinArea;
64       BinArea = 0; // let's be carefull !
65    }
66 }
67
68
69 //-----------------------------------------------------------------------------
70 // Print
71 /*
72  * \brief   canonical Printer
73  */
74  
75 void BinEntry::Print(std::ostream &os, std::string const & )
76 {
77    os << "B ";
78    DocEntry::Print(os);
79    std::ostringstream s;
80    void* binArea = GetBinArea();
81    if (binArea)
82    {
83       //s << " [" << GDCM_BINLOADED 
84       s << " [" << GetValue()
85         << "; length = " << GetLength() << "]";
86    }
87    else
88    {
89       if ( GetLength() == 0 )
90       {
91          s << " []";
92       }
93       else 
94       {
95          //s << " [gdcm::Binary data NOT loaded]";
96          s << " [" <<GetValue() << "]";
97       }
98          
99    }
100    os << s.str();
101 }
102
103 /*
104  * \brief   canonical Writer
105  * @param fp already open file pointer
106  * @param filetype type of the file to be written
107 */
108 void BinEntry::WriteContent(std::ofstream *fp, FileType filetype)
109
110 #define BUFFER_SIZE 4096
111    DocEntry::WriteContent(fp, filetype);
112    void* binArea = GetBinArea();
113    int lgr = GetLength();
114    if (binArea) // the binArea was *actually* loaded
115    {
116
117    // TODO FIME
118    // Probabely, the same operation will have to be done when we want 
119    // to write image with Big Endian Transfert Syntax, 
120    //   and we are working onj Little Endian Processor
121
122 #ifdef GDCM_WORDS_BIGENDIAN
123       // Be carefull with *any* 16 bits words 'binEntries !'
124       // if ( GetVR() == "OW") // to be used later
125
126       // TODO FIXME Right now, we only care of Pixels element
127
128       // 8 Bits Pixels *are* OB, 16 Bits Pixels *are* OW
129       // -value forced while Reading process-
130       if (GetGroup() == 0x7fe0 && GetVR() == "OW")
131       {     
132          uint16_t *currPosition = (uint16_t *)binArea;
133
134          // TODO FIXME : Maybe we should allocate somewhere a static buffer,
135          // in order not to have to alloc/delete for almost every BinEntry ...
136          uint16_t *buffer = new uint16_t[BUFFER_SIZE];
137
138          // how many BUFFER_SIZE long pieces in binArea ?
139          int nbPieces = lgr/BUFFER_SIZE/2; //(16 bits = 2 Bytes)
140          uint16_t *binArea16 = (uint16_t*)binArea;
141          for (int j=0;j<nbPieces;j++)
142          {
143             for (int i = 0; i < BUFFER_SIZE/2; i++)
144             {
145                buffer[i] =  (binArea16[i] >> 8) | (binArea16[i] << 8);
146             }
147             fp->write ( (char*)currPosition, BUFFER_SIZE );
148             currPosition += BUFFER_SIZE/2;
149          }
150          int remainingSize = lgr%BUFFER_SIZE;
151          if ( remainingSize != 0)
152          {
153             fp->write ( (char*)currPosition, remainingSize );   
154          } 
155          delete[] buffer; 
156       }
157       else
158       { 
159          // For any other VR, BinEntry is re-written as-is
160          fp->write ( (char*)binArea, lgr );
161       }
162 #else
163       fp->write ( (char*)binArea, lgr ); // Elem value
164 #endif //GDCM_WORDS_BIGENDIAN
165
166    }
167    else
168    {
169       // nothing was loaded, but we need to skip space on disc
170       fp->seekp(lgr, std::ios::cur);
171    }
172 }
173 //-----------------------------------------------------------------------------
174 // Public
175
176
177 /// \brief Sets the value (non string) of the current Dicom Header Entry
178 void BinEntry::SetBinArea( uint8_t *area, bool self )  
179
180    if (BinArea && SelfArea)
181       delete[] BinArea;
182
183    BinArea = area;
184    SelfArea=self;
185 }
186
187 //-----------------------------------------------------------------------------
188 // Protected
189
190 //-----------------------------------------------------------------------------
191 // Private
192    
193 //-----------------------------------------------------------------------------
194 } // end namespace gdcm