+/**
+ * \ingroup gdcmElValSet
+ * \brief
+ * @param length
+ * @param TagName
+ * @return
+ */
+int gdcmElValSet::SetElValueLengthByName(guint32 length, std::string TagName) {
+ if ( ! NameHt.count(TagName))
+ return 0;
+ if (length%2) length++; // length must be even
+ NameHt.find(TagName)->second->SetLength(length);
+ return 1 ;
+}
+
+/**
+ * \ingroup gdcmElValSet
+ * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
+ * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
+ * @param type
+ */
+void gdcmElValSet::UpdateGroupLength(bool SkipSequence, FileType type) {
+ guint16 gr, el;
+ std::string vr;
+
+ gdcmElValue *elem;
+ char trash[10];
+ std::string str_trash;
+
+ GroupKey key;
+ GroupHT groupHt; // to hold the length of each group
+ TagKey tk;
+ // remember :
+ // typedef std::map<GroupKey, int> GroupHT;
+
+ gdcmElValue *elemZ;
+
+ // for each Tag in the DCM Header
+
+ for (TagElValueHT::iterator tag2 = tagHt.begin();
+ tag2 != tagHt.end();
+ ++tag2){
+
+ elem = tag2->second;
+ gr = elem->GetGroup();
+ el = elem->GetElement();
+ vr = elem->GetVR();
+
+ sprintf(trash, "%04x", gr);
+ key = trash; // generate 'group tag'
+
+ // if the caller decided not to take SEQUENCEs into account
+ // e.g : he wants to write an ACR-NEMA File
+
+ if (SkipSequence && vr == "SQ") continue;
+
+ // Still unsolved problem :
+ // we cannot find the 'Sequence Delimitation Item'
+ // since it's at the end of the Hash Table
+ // (fffe,e0dd)
+
+ // pas SEQUENCE en ACR-NEMA
+ // WARNING :
+ // --> la descente a l'interieur' des SQ
+ // devrait etre faite avec une liste chainee, pas avec une HTable...
+
+ if ( groupHt.count(key) == 0) { // we just read the first elem of a given group
+ if (el == 0x0000) { // the first elem is 0x0000
+ groupHt[key] = 0; // initialize group length
+ } else {
+ groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
+ }
+ } else { // any elem but the first
+ if (type == ExplicitVR) {
+ if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
+ groupHt[key] += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
+ }
+ }
+ groupHt[key] += 2 + 2 + 4 + elem->GetLength();
+ }
+ }
+
+ unsigned short int gr_bid;
+
+ for (GroupHT::iterator g = groupHt.begin(); // for each group we found
+ g != groupHt.end();
+ ++g){
+ // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
+ // warning: unsigned int format, different type arg
+ sscanf(g->first.c_str(),"%x",&gr_bid);
+ tk = g->first + "|0000"; // generate the element full tag
+
+ if ( tagHt.count(tk) == 0) { // if element 0x0000 not found
+ gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");
+ elemZ = new gdcmElValue(tagZ);
+ elemZ->SetLength(4);
+ Add(elemZ); // create it
+ } else {
+ elemZ=GetElementByNumber(gr_bid, 0x0000);
+ }
+ sprintf(trash ,"%d",g->second);
+ str_trash=trash;
+ elemZ->SetValue(str_trash);
+ }
+}
+
+/**
+ * \ingroup gdcmElValSet
+ * \brief writes on disc according to the requested format
+ * \ (ACR-NEMA, DICOM, RAW) the image
+ * \ warning does NOT add the missing elements in the header :
+ * \ it's up to the user doing it !
+ * \ (function CheckHeaderCoherence to be written)
+ * @param type type of the File to be written
+ * (ACR-NEMA, DICOM, RAW)
+ * @param _fp already open file pointer
+ * @return
+ */
+void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
+ guint16 gr, el;
+ guint32 lgr;
+ const char * val;
+ std::string vr;
+ guint32 val_uint32;
+ guint16 val_uint16;
+
+ std::vector<std::string> tokens;
+
+ // TODO : use listElem to iterate, not TagHt!
+ // pb : gdcmElValSet.Add does NOT update listElem
+ // find a trick in STL to do it, at low cost !
+
+ void *ptr;
+
+ // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
+ // restent a tester les echecs en ecriture (apres chaque fwrite)
+
+ for (TagElValueHT::iterator tag2=tagHt.begin();
+ tag2 != tagHt.end();
+ ++tag2){
+
+ gr = tag2->second->GetGroup();
+ el = tag2->second->GetElement();
+ lgr = tag2->second->GetLength();
+ val = tag2->second->GetValue().c_str();
+ vr = tag2->second->GetVR();
+
+ // std::cout << "Tag "<< std::hex << gr << " " << el << std::endl;
+
+ if ( type == ACR ) {
+ if (gr < 0x0008) continue; // ignore pure DICOM V3 groups
+ if (gr %2) continue; // ignore shadow groups
+ if (vr == "SQ" ) continue; // ignore Sequences
+ if (gr == 0xfffe ) continue; // ignore delimiters
+ }