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