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