]> Creatis software - gdcm.git/blob - src/gdcmBinEntry.cxx
* src/gdcmFile.cxx, gdcmDocument.cxx : fix the bug on the group|element
[gdcm.git] / src / gdcmBinEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmBinEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/25 15:21:20 $
7   Version:   $Revision: 1.57 $
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 "gdcmContentEntry.h"
21
22 #include "gdcmDebug.h"
23
24 #include <fstream>
25 #include <sstream>
26 #include <iostream> // for std::ios_base, since <ios> does not exist on gcc/Solaris
27
28 namespace gdcm 
29 {
30
31 //-----------------------------------------------------------------------------
32 // Constructor / Destructor
33
34 /**
35  * \brief   Constructor from a given BinEntry
36  */
37 BinEntry::BinEntry(DictEntry *e) : ContentEntry(e)
38 {
39    BinArea = 0;
40    SelfArea = true;
41 }
42
43 /**
44  * \brief   Constructor from a given BinEntry
45  * @param   e Pointer to existing Doc entry
46  */
47 BinEntry::BinEntry(DocEntry *e) : ContentEntry(e->GetDictEntry())
48 {
49    Copy(e);
50
51    BinArea = 0; // let's be carefull !
52    SelfArea = true;
53 }
54
55 /**
56  * \brief   Canonical destructor.
57  */
58 BinEntry::~BinEntry()
59 {
60    if (BinArea && SelfArea)
61    {
62       delete[] BinArea;
63       BinArea = 0; // let's be carefull !
64    }
65 }
66
67
68 //-----------------------------------------------------------------------------
69 // Print
70 /**
71  * \brief   Prints a BinEntry (Dicom entry)
72  * @param   os ostream we want to print in
73  * @param indent Indentation string to be prepended during printing
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    os << s.str();
100 }
101
102 /*
103  * \brief   canonical Writer
104  * @param fp already open file pointer
105  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
106 */
107 void BinEntry::WriteContent(std::ofstream *fp, FileType filetype)
108
109 #define BUFFER_SIZE 4096
110    DocEntry::WriteContent(fp, filetype);
111    void* binArea = GetBinArea();
112    int lgr = GetLength();
113    if (binArea) // the binArea was *actually* loaded
114    {
115
116    // TODO FIME
117    // Probabely, the same operation will have to be done when we want 
118    // to write image with Big Endian Transfert Syntax, 
119    //   and we are working on Little Endian Processor
120
121 #ifdef GDCM_WORDS_BIGENDIAN
122       // Be carefull with *any* 16 bits words 'binEntries !'
123       // if ( GetVR() == "OW") // to be used later
124
125       // TODO FIXME Right now, we only care of Pixels element
126
127       // 8 Bits Pixels *are* OB, 16 Bits Pixels *are* OW
128       // -value forced while Reading process-
129       if (GetGroup() == 0x7fe0 && GetVR() == "OW")
130       {     
131          uint16_t *currPosition = (uint16_t *)binArea;
132
133          // TODO FIXME : Maybe we should allocate somewhere a static buffer,
134          // in order not to have to alloc/delete for almost every BinEntry ...
135          uint16_t *buffer = new uint16_t[BUFFER_SIZE];
136
137          // how many BUFFER_SIZE long pieces in binArea ?
138          int nbPieces = lgr/BUFFER_SIZE/2; //(16 bits = 2 Bytes)
139          uint16_t *binArea16 = (uint16_t*)binArea;
140          for (int j=0;j<nbPieces;j++)
141          {
142             for (int i = 0; i < BUFFER_SIZE/2; i++)
143             {
144                buffer[i] =  (binArea16[i] >> 8) | (binArea16[i] << 8);
145             }
146             fp->write ( (char*)currPosition, BUFFER_SIZE );
147             currPosition += BUFFER_SIZE/2;
148          }
149          int remainingSize = lgr%BUFFER_SIZE;
150          if ( remainingSize != 0)
151          {
152             fp->write ( (char*)currPosition, remainingSize );   
153          } 
154          delete[] buffer; 
155       }
156       else
157       { 
158          // For any other VR, BinEntry is re-written as-is
159          fp->write ( (char*)binArea, lgr );
160       }
161 #else
162       fp->write ( (char*)binArea, lgr ); // Elem value
163 #endif //GDCM_WORDS_BIGENDIAN
164
165    }
166    else
167    {
168       // nothing was loaded, but we need to skip space on disc
169       fp->seekp(lgr, std::ios::cur);
170    }
171 }
172 //-----------------------------------------------------------------------------
173 // Public
174
175
176 /// \brief Sets the value (non string) of the current Dicom Header Entry
177 void BinEntry::SetBinArea( uint8_t *area, bool self )  
178
179    if (BinArea && SelfArea)
180       delete[] BinArea;
181
182    BinArea = area;
183    SelfArea=self;
184 }
185
186 //-----------------------------------------------------------------------------
187 // Protected
188
189 //-----------------------------------------------------------------------------
190 // Private
191    
192 //-----------------------------------------------------------------------------
193 } // end namespace gdcm