Skip to content

Commit

Permalink
bump version 2.4.0
Browse files Browse the repository at this point in the history
+ disable recomression of compressed images (new option)
  • Loading branch information
knopkem committed Jun 23, 2022
1 parent c488264 commit 0ac1ceb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/recompress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const options: recompressOptions =
storagePath: p.join(__dirname, "output"), // existing directory only
writeTransfer: "1.2.840.10008.1.2.4.51", // see supported ts
lossyQuality: 40, // only supported for JPEG Baseline (Processes 1, 2, 4) 0..100
enableRecompression: true, // change compression of already compressed images
verbose: true
};

Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface recompressOptions {
storagePath: string;
writeTransfer?: string;
lossyQuality?: number;
enableRecompression?: boolean;
verbose?: boolean;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dicom-dimse-native",
"version": "2.3.4",
"version": "2.4.0",
"description": "native addon using DCMTK dicom toolkit",
"main": "index.js",
"scripts": {
Expand Down
31 changes: 26 additions & 5 deletions src/CompressAsyncWorker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void CompressAsyncWorker::Execute(const ExecutionProgress &progress)
bool validFileFound = false;
while ((iter != enditer))
{
if (recompress(*iter, OFString(in.storagePath.c_str()), writeTrans.getXfer(), in.lossyQuality))
if (recompress(*iter, OFString(in.storagePath.c_str()), writeTrans.getXfer(), in.lossyQuality, in.enableRecompression))
{
validFileFound = true;
}
Expand All @@ -196,7 +196,7 @@ OFBool CompressAsyncWorker::isDicomFile(const OFFilename &fname)
return OFTrue;
}

OFBool CompressAsyncWorker::recompress(const OFFilename &infile, const OFString &storePath, E_TransferSyntax prefXfer, int quality)
OFBool CompressAsyncWorker::recompress(const OFFilename &infile, const OFString &storePath, E_TransferSyntax _prefXfer, int quality, bool enableRecompression)
{
DcmFileFormat dfile;
OFCondition status = dfile.loadFile(infile, EXS_Unknown, EGL_noChange, DCM_MaxReadLength, ERM_autoDetect);
Expand All @@ -223,11 +223,27 @@ OFBool CompressAsyncWorker::recompress(const OFFilename &infile, const OFString

// check if input is same as output
bool isSameFile = false;
E_TransferSyntax originalXfer = lookForXfer(dfile.getMetaInfo());
E_TransferSyntax prefXfer = _prefXfer;

// check if already compressed
if (!enableRecompression) {
if (originalXfer == EXS_Unknown ||
originalXfer == EXS_LittleEndianImplicit ||
originalXfer == EXS_BigEndianImplicit ||
originalXfer == EXS_LittleEndianExplicit ||
originalXfer == EXS_BigEndianExplicit ||
originalXfer == EXS_DeflatedLittleEndianExplicit) {
DCMNET_INFO("Not recompressing file as this is disabled...");
prefXfer = originalXfer;
}
}

if (inpath == outpath)
{
isSameFile = true;
// skip writing if input and output is the same and TS match already
if (lookForXfer(dfile.getMetaInfo()) == prefXfer)
if (originalXfer == prefXfer)
{
DCMNET_INFO("file has correct Xfer already skipping...");
return OFTrue;
Expand Down Expand Up @@ -259,8 +275,13 @@ OFBool CompressAsyncWorker::recompress(const OFFilename &infile, const OFString
OFCondition cond = dfile.chooseRepresentation(prefXfer, rp);
if (cond.bad() || !dfile.canWriteXfer(prefXfer))
{
DCMNET_WARN("Failed compressing file: " << infile.getCharPointer());
return OFFalse;
DCMNET_WARN("Failed compressing file: " << infile.getCharPointer() << " keeping original");
cond = dfile.chooseRepresentation(originalXfer, NULL);
}

if (cond.bad()) {
DCMNET_WARN("Something went wrong");
return OFFalse;
}

// just save the file if output is different
Expand Down
2 changes: 1 addition & 1 deletion src/CompressAsyncWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class CompressAsyncWorker : public BaseAsyncWorker

protected:
OFBool isDicomFile( const OFFilename &fname );
OFBool recompress(const OFFilename& infile, const OFString& storePath, E_TransferSyntax prefXfer, int quality);
OFBool recompress(const OFFilename& infile, const OFString& storePath, E_TransferSyntax prefXfer, int quality, bool enableRecompression);
};
7 changes: 6 additions & 1 deletion src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace ns {
};

struct sInput {
sInput() : verbose(false), permissive(false), storeOnly(false), writeFile(true), lossyQuality(80) {}
sInput() : verbose(false), permissive(false), storeOnly(false), writeFile(true), lossyQuality(80), enableRecompression(false) {}
sIdent source;
sIdent target;
std::string storagePath;
Expand All @@ -89,6 +89,7 @@ namespace ns {
bool permissive;
bool storeOnly;
bool writeFile;
bool enableRecompression;
inline bool valid() {
return source.valid() && target.valid();
}
Expand Down Expand Up @@ -203,6 +204,10 @@ namespace ns {
in.writeFile = j.at("writeFile");
}
catch (...) {}
try {
in.enableRecompression = j.at("enableRecompression");
}
catch (...) {}
try {
in.lossyQuality = toInt(j, "lossyQuality");
}
Expand Down

0 comments on commit 0ac1ceb

Please sign in to comment.