+/**
+ * \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;
+ string vr;
+
+ gdcmElValue *elem;
+ char trash[10];
+ 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
+ * @param type
+ * @param _fp
+ * @return
+ */
+void gdcmElValSet::WriteElements(FileType type, FILE * _fp) {
+ guint16 gr, el;
+ guint32 lgr;
+ const char * val;
+ string vr;
+ guint32 val_uint32;
+ guint16 val_uint16;
+
+ vector<string> tokens;
+
+ void *ptr;
+
+ // Tout ceci ne marche QUE parce qu'on est sur un proc Little Endian
+ // restent à tester les echecs en écriture (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();
+
+ // cout << "Tag "<< hex << gr << " " << el << "\n";
+
+ 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
+ }
+
+ fwrite ( &gr,(size_t)2 ,(size_t)1 ,_fp); //group
+ fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp); //element
+
+ if ( (type == ExplicitVR) && (gr <= 0x0002) ) {
+ // EXPLICIT VR
+ guint16 z=0, shortLgr;
+ 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 {
+ shortLgr=lgr;
+ fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
+ }
+ } else { // IMPLICIT VR
+ fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
+ }
+
+ if (vr == "US" || vr == "SS") {
+ tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
+ Tokenize (tag2->second->GetValue(), tokens, "\\");
+ for (unsigned int i=0; i<tokens.size();i++) {
+ val_uint16 = atoi(tokens[i].c_str());
+ ptr = &val_uint16;
+ fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
+ }
+ tokens.clear();
+ continue;
+ }
+ if (vr == "UL" || vr == "SL") {
+ tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
+ Tokenize (tag2->second->GetValue(), tokens, "\\");
+ for (unsigned int i=0; i<tokens.size();i++) {
+ val_uint32 = atoi(tokens[i].c_str());
+ ptr = &val_uint32;
+ fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
+ }
+ tokens.clear();
+ continue;
+ }
+ // Pixels are never loaded in the element !
+ if ((gr == 0x7fe0) && (el == 0x0010) ) break;
+
+ fwrite ( val,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
+ }
+}
+
+/**
+ * \ingroup gdcmElValSet
+ * \brief
+ * @param _fp
+ * @param type
+ * @return
+ */
+int gdcmElValSet::Write(FILE * _fp, FileType type) {
+
+ if (type == ImplicitVR) {
+ string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
+ SetElValueByNumber(implicitVRTransfertSyntax, 0x0002, 0x0010);
+
+ //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
+ // values with a VR of UI shall be padded with a single trailing null
+ // Dans le cas suivant on doit pader manuellement avec un 0
+
+ SetElValueLengthByNumber(18, 0x0002, 0x0010);
+ }
+ // Question :
+ // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non ,
+ // (FileType est un champ de gdcmHeader ...)
+ // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
+ // no way
+
+ if (type == ExplicitVR) {
+ string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
+ SetElValueByNumber(explicitVRTransfertSyntax, 0x0002, 0x0010);
+ // See above comment
+ SetElValueLengthByNumber(20, 0x0002, 0x0010);
+ }
+
+ if ( (type == ImplicitVR) || (type == ExplicitVR) )
+ UpdateGroupLength(false,type);
+ if ( type == ACR)
+ UpdateGroupLength(true,ACR);
+
+ WriteElements(type, _fp);
+ return(1);
+}