-/**
- * \brief Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
- * \warning : to be re-written using the chained list instead of the H table.
- * \warning : DO NOT use (doesn't work any longer because of the multimap)
- * \todo : to be re-written using the chained list instead of the H table
- * @param SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
- * @param type Type of the File (ExplicitVR,ImplicitVR, ACR, ...)
- */
-
- /* Probabely useless, now !
-
-void gdcmDocument::UpdateGroupLength(bool SkipSequence, FileType type) {
- guint16 gr, el;
- std::string vr;
-
- gdcmDocEntry *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;
-
- gdcmDocEntry *elemZ;
-
- // for each Tag in the DCM Header
-
- for (TagDocEntryHT::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)
-
- // there is SEQUENCE in 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); //FIXME
- 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 gdcmDocEntry(tagZ);
- elemZ->SetLength(4);
- AddEntry(elemZ); // create it
- }
- else
- {
- elemZ=GetDocEntryByNumber(gr_bid, 0x0000);
- }
- sprintf(trash ,"%d",g->second);
- str_trash=trash;
- //elemZ->SetValue(str_trash); // to go on compiling
- }
-}
-
-*/
-
-/**
- * \brief Writes in a file (according to the requested format)
- * the group, the element, the value representation and the length
- * of a single gdcmDocEntry passed as argument.
- * @param tag pointer on the gdcmDocEntry to be written
- * @param _fp already open file pointer
- * @param type type of the File to be written
- */
-void gdcmDocument::WriteEntryTagVRLength(gdcmDocEntry *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 == gdcmExplicitVR ) {
-
- // 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 gdcmDocEntry passed as argument.
- * @param tag Pointer on the gdcmDocEntry to be written
- * @param _fp Already open file pointer
- * @param type type of the File to be written
- */
-
- // TODO : to be re -written recursively !
-
-void gdcmDocument::WriteEntryValue(gdcmDocEntry *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();
- // to go on compiling
- 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 gdcmDocEntry passed as argument.
- * \sa WriteEntryValue, WriteEntryTagVRLength.
- * @param tag Pointer on the gdcmDocEntry to be written
- * @param _fp Already open file pointer
- * @param type type of the File to be written
- */
-
-bool gdcmDocument::WriteEntry(gdcmDocEntry *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");
-// to go on compiling
- tag->SetLength(tag->GetReadLength()+1);
- }
-
- WriteEntryTagVRLength(tag, _fp, type);
- WriteEntryValue(tag, _fp, type);
- return true;
-}
-
-/**
- * \brief writes on disc according to the requested format
- * (ACR-NEMA, ExplicitVR, ImplicitVR) the image
- * using the Chained List
- * \warning does NOT add the missing elements in the header :
- * it's up to the user doing it !
- * (function CheckHeaderCoherence to be written)
- * \warning DON'T try, right now, to write a DICOM image
- * from an ACR Header (meta elements will be missing!)
- * \sa WriteEntriesDeprecated (Special temporary method for Theralys)
- * @param type type of the File to be written
- * (ACR-NEMA, ExplicitVR, ImplicitVR)
- * @param _fp already open file pointer
- */
-
-bool gdcmDocument::WriteEntries(FILE *_fp,FileType type)
-{
- /// \todo (?) check write failures (after *each* fwrite)
-
- for (ListTag::iterator tag2=listEntries.begin();
- tag2 != listEntries.end();
- ++tag2)
- {
- if ( type == gdcmACR ){
- if ((*tag2)->GetGroup() < 0x0008)
- // Ignore pure DICOM V3 groups
- continue;
- if ((*tag2)->GetElement() %2)
- // Ignore the "shadow" groups
- continue;
- if ((*tag2)->GetVR() == "SQ" ) // ignore Sequences
- continue;
- if ((*tag2)->GetSQDepthLevel() != 0) // Not only ignore the SQ element
- continue;
- }
- if (! WriteEntry(*tag2,_fp,type) )
- return false;
- }
- return true;
-}
-
-/**
- * \brief writes on disc according to the requested format
- * (ACR-NEMA, ExplicitVR, ImplicitVR) the image,
- * using only the last synonym of each mutimap H Table post.
- * \warning Uses the H Table, instead of the Chained List
- * in order to be compliant with the old way to proceed
- * (added elements taken in to account)
- * Only THERALYS, during a transitory phase is supposed
- * to use this method !!!
- * \warning DON'T try, right now, to write a DICOM image
- * from an ACR Header (meta elements will be missing!)
- * \sa WriteEntries
- * @param _fp already open file pointer
- * @param type type of the File to be written
- * (ACR-NEMA, ExplicitVR, ImplicitVR)
- */
-void gdcmDocument::WriteEntriesDeprecated(FILE *_fp,FileType type) {
-
- // restent a tester les echecs en ecriture (apres chaque fwrite)
-
- for (TagDocEntryHT::iterator tag2=tagHT.begin();
- tag2 != tagHT.end();
- ++tag2){
- if ( type == gdcmACR ){
- if ((*tag2->second).GetGroup() < 0x0008) continue; // ignore pure DICOM V3 groups
- if ((*tag2->second).GetElement() %2) continue; // ignore shadow groups
- if ((*tag2->second).GetVR() == "SQ" ) continue; // ignore Sequences
- if ((*tag2->second).GetSQDepthLevel() != 0) continue; // Not only ignore the SQ element
- }
- if ( ! WriteEntry(tag2->second,_fp,type))
- break;
- }
-}
-