Skip to content

Commit bd7c78f

Browse files
committed
Introduce a fallback mechanism for scrub
1 parent 0875db9 commit bd7c78f

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

Source/MediaStorageAndFileFormat/gdcmCleaner.cxx

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,12 @@ struct Cleaner::impl {
538538
bool AllMissingPrivateCreator;
539539
bool AllGroupLength;
540540
bool AllIllegal;
541+
bool WhenScrubFails;
541542
impl()
542543
: AllMissingPrivateCreator(true),
543544
AllGroupLength(true),
544-
AllIllegal(true) {}
545+
AllIllegal(true),
546+
WhenScrubFails(false) {}
545547

546548
enum ACTION { NONE, EMPTY, REMOVE, SCRUB };
547549
enum ACTION ComputeAction(File const &file, DataSet &ds,
@@ -668,6 +670,10 @@ struct Cleaner::impl {
668670
bool RemoveMissingPrivateCreator(Tag const & /*t*/) { return false; }
669671
void RemoveAllGroupLength(bool remove) { AllGroupLength = remove; }
670672
void RemoveAllIllegal(bool remove) { AllIllegal = remove; }
673+
void EmptyWhenScrubFails(bool empty) { WhenScrubFails = empty; }
674+
675+
bool CleanCSAImage(DataSet &ds, const DataElement &de);
676+
bool CleanCSASeries(DataSet &ds, const DataElement &de);
671677
};
672678

673679
static VR ComputeDictVR(File &file, DataSet &ds, DataElement const &de) {
@@ -840,7 +846,7 @@ static inline bool bs_is_signature(const ByteValue *bv, const char *str) {
840846
return false;
841847
}
842848

843-
static bool CleanCSAImage(DataSet &ds, const DataElement &de) {
849+
bool Cleaner::impl::CleanCSAImage(DataSet &ds, const DataElement &de) {
844850
const ByteValue *bv = de.GetByteValue();
845851
// fast path:
846852
if (!bv) return true;
@@ -888,6 +894,13 @@ static bool CleanCSAImage(DataSet &ds, const DataElement &de) {
888894
gdcmDebugMacro("Zero-out CSA header");
889895
return true;
890896
}
897+
// fallback logic:
898+
if (WhenScrubFails && is_signature(bv, sv10)) {
899+
// so SV10 header has been identified, but we failed to 'scrub', let's
900+
// empty it:
901+
ds.Replace(clean);
902+
return true;
903+
}
891904
gdcmErrorMacro("Failure to call CleanCSAImage");
892905
return false;
893906
}
@@ -900,7 +913,7 @@ static bool CleanCSAImage(DataSet &ds, const DataElement &de) {
900913
return true;
901914
}
902915

903-
static bool CleanCSASeries(DataSet &ds, const DataElement &de) {
916+
bool Cleaner::impl::CleanCSASeries(DataSet &ds, const DataElement &de) {
904917
const ByteValue *bv = de.GetByteValue();
905918
// fast path:
906919
if (!bv) return true;
@@ -944,6 +957,13 @@ static bool CleanCSASeries(DataSet &ds, const DataElement &de) {
944957
gdcmDebugMacro("Zero-out CSA header");
945958
return true;
946959
}
960+
// fallback logic:
961+
if (WhenScrubFails && is_signature(bv, sv10)) {
962+
// so SV10 header has been identified, but we failed to 'scrub', let's
963+
// empty it:
964+
ds.Replace(clean);
965+
return true;
966+
}
947967
gdcmErrorMacro("Failure to call CleanCSASeries");
948968
return false;
949969
}
@@ -1065,8 +1085,8 @@ static bool IsDPathInSet(std::set<DPath> const &aset, DPath const dpath) {
10651085
}
10661086

10671087
Cleaner::impl::ACTION Cleaner::impl::ComputeAction(
1068-
File const & /*file*/, DataSet &ds, const DataElement &de, VR const &ref_dict_vr,
1069-
const std::string &tag_path) {
1088+
File const & /*file*/, DataSet &ds, const DataElement &de,
1089+
VR const &ref_dict_vr, const std::string &tag_path) {
10701090
const Tag &tag = de.GetTag();
10711091
// Group Length & Illegal cannot be preserved so it is safe to do them now:
10721092
if (tag.IsGroupLength()) {
@@ -1302,6 +1322,9 @@ void Cleaner::RemoveAllGroupLength(bool remove) {
13021322
pimpl->RemoveAllGroupLength(remove);
13031323
}
13041324
void Cleaner::RemoveAllIllegal(bool remove) { pimpl->RemoveAllIllegal(remove); }
1325+
void Cleaner::EmptyWhenScrubFails(bool empty) {
1326+
pimpl->EmptyWhenScrubFails(empty);
1327+
}
13051328

13061329
bool Cleaner::Clean() {
13071330
DataSet &ds = F->GetDataSet();

Source/MediaStorageAndFileFormat/gdcmCleaner.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class GDCM_EXPORT Cleaner : public Subject {
6565
/// Should I remove all illegal attribute. Default: true
6666
void RemoveAllIllegal(bool remove);
6767

68+
/// Should I empty instead of scrub upon failure
69+
void EmptyWhenScrubFails(bool empty);
70+
6871
/// main loop
6972
bool Clean();
7073

0 commit comments

Comments
 (0)