+ * \brief Writes in a file (according to the requested format)
+ * the group, the element, the value representation and the length
+ * of a single gdcmHeaderEntry passed as argument.
+ * @param tag pointer on the gdcmHeaderEntry to be written
+ * @param _fp already open file pointer
+ * @param type type of the File to be written
+ */
+void gdcmParser::WriteEntryTagVRLength(gdcmHeaderEntry *tag,
+ FILE *_fp,
+ FileType type)
+{
+ guint16 group = tag->GetGroup();
+ std::string vr = tag->GetVR();
+ guint16 el = tag->GetElement();
+ guint32 lgr = tag->GetReadLength();
+
+ if ( (group == 0xfffe) && (el == 0x0000) )
+ // Fix in order to make some MR PHILIPS images e-film readable
+ // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
+ // we just *always* ignore spurious fffe|0000 tag !
+ return;
+
+ fwrite ( &group,(size_t)2 ,(size_t)1 ,_fp); //group
+ fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
+
+ if ( type == ExplicitVR ) {
+
+ // Special case of delimiters:
+ if (group == 0xfffe) {
+ // Delimiters have NO Value Representation and have NO length.
+ // Hence we skip writing the VR and length and we pad by writing
+ // 0xffffffff
+
+ int ff=0xffffffff;
+ fwrite (&ff,(size_t)4 ,(size_t)1 ,_fp);
+ return;
+ }
+
+ guint16 z=0;
+ guint16 shortLgr = lgr;
+ if (vr == "unkn") { // Unknown was 'written'
+ // deal with Little Endian
+ fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
+ fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
+ } else {
+ fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);
+ if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
+ {
+ fwrite ( &z, (size_t)2 ,(size_t)1 ,_fp);
+ fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
+ } else {
+ fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
+ }
+ }
+ }
+ else // IMPLICIT VR
+ {
+ fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
+ }
+}
+
+/**
+ * \brief Writes in a file (according to the requested format)
+ * the value of a single gdcmHeaderEntry passed as argument.
+ * @param tag Pointer on the gdcmHeaderEntry to be written
+ * @param _fp Already open file pointer
+ * @param type type of the File to be written
+ */
+void gdcmParser::WriteEntryValue(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
+{
+ (void)type;
+ guint16 group = tag->GetGroup();
+ std::string vr = tag->GetVR();
+ guint32 lgr = tag->GetReadLength();
+
+ if (vr == "SQ")
+ // SeQuences have no value:
+ return;
+ if (group == 0xfffe)
+ // Delimiters have no associated value:
+ return;
+
+ void *voidArea;
+ voidArea = tag->GetVoidArea();
+ if (voidArea != NULL)
+ { // there is a 'non string' LUT, overlay, etc
+ fwrite ( voidArea,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
+ return;
+ }
+
+ if (vr == "US" || vr == "SS")
+ {
+ // some 'Short integer' fields may be mulivaluated
+ // each single value is separated from the next one by '\'
+ // we split the string and write each value as a short int
+ std::vector<std::string> tokens;
+ tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
+ Tokenize (tag->GetValue(), tokens, "\\");
+ for (unsigned int i=0; i<tokens.size();i++)
+ {
+ guint16 val_uint16 = atoi(tokens[i].c_str());
+ void *ptr = &val_uint16;
+ fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
+ }
+ tokens.clear();
+ return;
+ }
+ // some 'Integer' fields may be mulivaluated
+ // each single value is separated from the next one by '\'
+ // we split the string and write each value as an int
+ if (vr == "UL" || vr == "SL")
+ {
+ std::vector<std::string> tokens;
+ tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
+ Tokenize (tag->GetValue(), tokens, "\\");
+ for (unsigned int i=0; i<tokens.size();i++)
+ {
+ guint32 val_uint32 = atoi(tokens[i].c_str());
+ void *ptr = &val_uint32;
+ fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
+ }
+ tokens.clear();
+ return;
+ }
+ fwrite (tag->GetValue().c_str(), (size_t)lgr ,(size_t)1, _fp); // Elem value
+}
+
+/**
+ * \brief Writes in a file (according to the requested format)
+ * a single gdcmHeaderEntry passed as argument.
+ * \sa WriteEntryValue, WriteEntryTagVRLength.
+ * @param tag Pointer on the gdcmHeaderEntry to be written
+ * @param _fp Already open file pointer
+ * @param type type of the File to be written
+ */
+
+bool gdcmParser::WriteEntry(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
+{
+ guint32 length = tag->GetLength();
+
+ // The value of a tag MUST (see the DICOM norm) be an odd number of
+ // bytes. When this is not the case, pad with an additional byte:
+ if(length%2==1)
+ {
+ tag->SetValue(tag->GetValue()+"\0");
+ tag->SetLength(tag->GetReadLength()+1);
+ }
+
+ WriteEntryTagVRLength(tag, _fp, type);
+ WriteEntryValue(tag, _fp, type);
+ return true;
+}
+
+/**