forked from DraconPern/fmjpeg2koj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcmdjp2k.cc
333 lines (273 loc) · 12.9 KB
/
dcmdjp2k.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
*
* Copyright (C) 2007-2014, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: dcmdjpls
*
* Author: Martin Willkomm
*
* Purpose: Decompress DICOM file with JPEG-LS transfer syntax
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#define INCLUDE_CSTDLIB
#define INCLUDE_CSTDIO
#define INCLUDE_CSTRING
#include "dcmtk/ofstd/ofstdinc.h"
#ifdef HAVE_GUSI_H
#include <GUSI.h>
#endif
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmdata/cmdlnarg.h"
#include "dcmtk/ofstd/ofconapp.h"
#include "dcmtk/dcmdata/dcuid.h" /* for dcmtk version name */
#include "dcmtk/dcmimage/diregist.h" /* include to support color images */
#include "fmjpeg2k/djlsutil.h" /* for dcmjpgls typedefs */
#include "fmjpeg2k/djdecode.h" /* for JPEG-LS decoder */
#ifdef USE_LICENSE_FILE
#include "oflice.h"
#endif
#ifndef OFFIS_CONSOLE_APPLICATION
#define OFFIS_CONSOLE_APPLICATION "dcmdjp2k"
#endif
static OFLogger dcmdjplsLogger = OFLog::getLogger("dcmtk.apps." OFFIS_CONSOLE_APPLICATION);
static char rcsid[] = "$dcmtk: " OFFIS_CONSOLE_APPLICATION " v"
OFFIS_DCMTK_VERSION " " OFFIS_DCMTK_RELEASEDATE " $";
// ********************************************
#define SHORTCOL 3
#define LONGCOL 21
int main(int argc, char *argv[])
{
#ifdef HAVE_GUSI_H
GUSISetup(GUSIwithSIOUXSockets);
GUSISetup(GUSIwithInternetSockets);
#endif
const char *opt_ifname = NULL;
const char *opt_ofname = NULL;
E_TransferSyntax opt_oxfer = EXS_LittleEndianExplicit;
E_GrpLenEncoding opt_oglenc = EGL_recalcGL;
E_EncodingType opt_oenctype = EET_ExplicitLength;
E_PaddingEncoding opt_opadenc = EPD_noChange;
OFCmdUnsignedInt opt_filepad = 0;
OFCmdUnsignedInt opt_itempad = 0;
E_FileReadMode opt_readMode = ERM_autoDetect;
E_FileWriteMode opt_writeMode = EWM_fileformat;
E_TransferSyntax opt_ixfer = EXS_Unknown;
// parameter
J2K_UIDCreation opt_uidcreation = EJ2KUC_default;
J2K_PlanarConfiguration opt_planarconfig = EJ2KPC_restore;
OFBool opt_ignoreOffsetTable = OFFalse;
#ifdef USE_LICENSE_FILE
LICENSE_FILE_DECLARATIONS
#endif
OFConsoleApplication app(OFFIS_CONSOLE_APPLICATION, "Decode JPEG-LS compressed DICOM file", rcsid);
OFCommandLine cmd;
cmd.setOptionColumns(LONGCOL, SHORTCOL);
cmd.setParamColumn(LONGCOL + SHORTCOL + 4);
cmd.addParam("dcmfile-in", "DICOM input filename to be converted");
cmd.addParam("dcmfile-out", "DICOM output filename");
cmd.addGroup("general options:", LONGCOL, SHORTCOL + 2);
cmd.addOption("--help", "-h", "print this help text and exit", OFCommandLine::AF_Exclusive);
cmd.addOption("--version", "print version information and exit", OFCommandLine::AF_Exclusive);
OFLog::addOptions(cmd);
#ifdef USE_LICENSE_FILE
LICENSE_FILE_DECLARE_COMMAND_LINE_OPTIONS
#endif
cmd.addGroup("input options:");
cmd.addSubGroup("input file format:");
cmd.addOption("--read-file", "+f", "read file format or data set (default)");
cmd.addOption("--read-file-only", "+fo", "read file format only");
cmd.addOption("--read-dataset", "-f", "read data set without file meta information");
cmd.addGroup("processing options:");
cmd.addSubGroup("planar configuration:");
cmd.addOption("--planar-restore", "+pr", "restore original planar configuration (default)");
cmd.addOption("--planar-auto", "+pa", "automatically determine planar configuration\nfrom SOP class and color space");
cmd.addOption("--color-by-pixel", "+px", "always store color-by-pixel");
cmd.addOption("--color-by-plane", "+pl", "always store color-by-plane");
cmd.addSubGroup("SOP Instance UID:");
cmd.addOption("--uid-default", "+ud", "keep same SOP Instance UID (default)");
cmd.addOption("--uid-always", "+ua", "always assign new UID");
cmd.addSubGroup("other processing options:");
cmd.addOption("--ignore-offsettable", "+io", "ignore offset table when decompressing");
cmd.addGroup("output options:");
cmd.addSubGroup("output file format:");
cmd.addOption("--write-file", "+F", "write file format (default)");
cmd.addOption("--write-dataset", "-F", "write data set without file meta information");
cmd.addSubGroup("output transfer syntax:");
cmd.addOption("--write-xfer-little", "+te", "write with explicit VR little endian (default)");
cmd.addOption("--write-xfer-big", "+tb", "write with explicit VR big endian TS");
cmd.addOption("--write-xfer-implicit", "+ti", "write with implicit VR little endian TS");
cmd.addSubGroup("post-1993 value representations:");
cmd.addOption("--enable-new-vr", "+u", "enable support for new VRs (UN/UT) (default)");
cmd.addOption("--disable-new-vr", "-u", "disable support for new VRs, convert to OB");
cmd.addSubGroup("group length encoding:");
cmd.addOption("--group-length-recalc", "+g=", "recalculate group lengths if present (default)");
cmd.addOption("--group-length-create", "+g", "always write with group length elements");
cmd.addOption("--group-length-remove", "-g", "always write without group length elements");
cmd.addSubGroup("length encoding in sequences and items:");
cmd.addOption("--length-explicit", "+e", "write with explicit lengths (default)");
cmd.addOption("--length-undefined", "-e", "write with undefined lengths");
cmd.addSubGroup("data set trailing padding (not with --write-dataset):");
cmd.addOption("--padding-retain", "-p=", "do not change padding (default)");
cmd.addOption("--padding-off", "-p", "no padding (implicit if --write-dataset)");
cmd.addOption("--padding-create", "+p", 2, "[f]ile-pad [i]tem-pad: integer",
"align file on multiple of f bytes\nand items on multiple of i bytes");
/* evaluate command line */
prepareCmdLineArgs(argc, argv, OFFIS_CONSOLE_APPLICATION);
if (app.parseCommandLine(cmd, argc, argv))
{
/* check exclusive options first */
if (cmd.hasExclusiveOption())
{
if (cmd.findOption("--version"))
{
app.printHeader(OFTrue /*print host identifier*/);
COUT << OFendl << "External libraries used:" << OFendl;
COUT << "- " << FMJPEG2KDecoderRegistration::getLibraryVersionString() << OFendl;
return 0;
}
}
/* command line parameters */
cmd.getParam(1, opt_ifname);
cmd.getParam(2, opt_ofname);
/* options */
OFLog::configureFromCommandLine(cmd, app);
#ifdef USE_LICENSE_FILE
LICENSE_FILE_EVALUATE_COMMAND_LINE_OPTIONS
#endif
cmd.beginOptionBlock();
if (cmd.findOption("--planar-restore")) opt_planarconfig = EJ2KPC_restore;
if (cmd.findOption("--planar-auto")) opt_planarconfig = EJ2KPC_auto;
if (cmd.findOption("--color-by-pixel")) opt_planarconfig = EJ2KPC_colorByPixel;
if (cmd.findOption("--color-by-plane")) opt_planarconfig = EJ2KPC_colorByPlane;
cmd.endOptionBlock();
cmd.beginOptionBlock();
if (cmd.findOption("--uid-default")) opt_uidcreation = EJ2KUC_default;
if (cmd.findOption("--uid-always")) opt_uidcreation = EJ2KUC_always;
cmd.endOptionBlock();
if (cmd.findOption("--ignore-offsettable")) opt_ignoreOffsetTable = OFTrue;
cmd.beginOptionBlock();
if (cmd.findOption("--read-file"))
{
opt_readMode = ERM_autoDetect;
opt_ixfer = EXS_Unknown;
}
if (cmd.findOption("--read-file-only"))
{
opt_readMode = ERM_fileOnly;
opt_ixfer = EXS_Unknown;
}
if (cmd.findOption("--read-dataset"))
{
opt_readMode = ERM_dataset;
// This transfer syntax works as long as the content of encapsulated pixel
// sequences is some kind of JPEG-LS bitstream. hmm?
opt_ixfer = EXS_JPEGLSLossless;
}
cmd.endOptionBlock();
cmd.beginOptionBlock();
if (cmd.findOption("--write-file")) opt_writeMode = EWM_fileformat;
if (cmd.findOption("--write-dataset")) opt_writeMode = EWM_dataset;
cmd.endOptionBlock();
cmd.beginOptionBlock();
if (cmd.findOption("--write-xfer-little")) opt_oxfer = EXS_LittleEndianExplicit;
if (cmd.findOption("--write-xfer-big")) opt_oxfer = EXS_BigEndianExplicit;
if (cmd.findOption("--write-xfer-implicit")) opt_oxfer = EXS_LittleEndianImplicit;
cmd.endOptionBlock();
cmd.beginOptionBlock();
if (cmd.findOption("--enable-new-vr")) dcmEnableGenerationOfNewVRs();
if (cmd.findOption("--disable-new-vr")) dcmDisableGenerationOfNewVRs();
cmd.endOptionBlock();
cmd.beginOptionBlock();
if (cmd.findOption("--group-length-recalc")) opt_oglenc = EGL_recalcGL;
if (cmd.findOption("--group-length-create")) opt_oglenc = EGL_withGL;
if (cmd.findOption("--group-length-remove")) opt_oglenc = EGL_withoutGL;
cmd.endOptionBlock();
cmd.beginOptionBlock();
if (cmd.findOption("--length-explicit")) opt_oenctype = EET_ExplicitLength;
if (cmd.findOption("--length-undefined")) opt_oenctype = EET_UndefinedLength;
cmd.endOptionBlock();
cmd.beginOptionBlock();
if (cmd.findOption("--padding-retain"))
{
app.checkConflict("--padding-retain", "--write-dataset", opt_writeMode == EWM_dataset);
opt_opadenc = EPD_noChange;
}
if (cmd.findOption("--padding-off")) opt_opadenc = EPD_withoutPadding;
if (cmd.findOption("--padding-create"))
{
app.checkConflict("--padding-create", "--write-dataset", opt_writeMode == EWM_dataset);
app.checkValue(cmd.getValueAndCheckMin(opt_filepad, 0));
app.checkValue(cmd.getValueAndCheckMin(opt_itempad, 0));
opt_opadenc = EPD_withPadding;
}
cmd.endOptionBlock();
}
/* print resource identifier */
OFLOG_DEBUG(dcmdjplsLogger, rcsid << OFendl);
// register global decompression codecs
FMJPEG2KDecoderRegistration::registerCodecs(opt_uidcreation, opt_planarconfig, opt_ignoreOffsetTable);
/* make sure data dictionary is loaded */
if (!dcmDataDict.isDictionaryLoaded())
{
OFLOG_WARN(dcmdjplsLogger, "no data dictionary loaded, "
<< "check environment variable: "
<< DCM_DICT_ENVIRONMENT_VARIABLE);
}
// open input file
if ((opt_ifname == NULL) || (strlen(opt_ifname) == 0))
{
OFLOG_FATAL(dcmdjplsLogger, "invalid filename: <empty string>");
return 1;
}
DcmFileFormat fileformat;
OFLOG_INFO(dcmdjplsLogger, "reading input file " << opt_ifname);
OFCondition error = fileformat.loadFile(opt_ifname, opt_ixfer, EGL_noChange, DCM_MaxReadLength, opt_readMode);
if (error.bad())
{
OFLOG_FATAL(dcmdjplsLogger, error.text() << ": reading file: " << opt_ifname);
return 1;
}
DcmDataset *dataset = fileformat.getDataset();
OFLOG_INFO(dcmdjplsLogger, "decompressing file");
DcmXfer opt_oxferSyn(opt_oxfer);
DcmXfer original_xfer(dataset->getOriginalXfer());
error = dataset->chooseRepresentation(opt_oxfer, NULL);
if (error.bad())
{
OFLOG_FATAL(dcmdjplsLogger, error.text() << ": decompressing file: " << opt_ifname);
if (error == EC_CannotChangeRepresentation)
OFLOG_FATAL(dcmdjplsLogger, "Input transfer syntax " << original_xfer.getXferName() << " not supported");
return 1;
}
if (! dataset->canWriteXfer(opt_oxfer))
{
OFLOG_FATAL(dcmdjplsLogger, "no conversion to transfer syntax " << opt_oxferSyn.getXferName() << " possible");
return 1;
}
OFLOG_INFO(dcmdjplsLogger, "creating output file " << opt_ofname);
// update file meta information with new SOP Instance UID
if (opt_uidcreation && (opt_writeMode == EWM_fileformat))
opt_writeMode = EWM_updateMeta;
fileformat.loadAllDataIntoMemory();
error = fileformat.saveFile(opt_ofname, opt_oxfer, opt_oenctype, opt_oglenc, opt_opadenc,
OFstatic_cast(Uint32, opt_filepad), OFstatic_cast(Uint32, opt_itempad), opt_writeMode);
if (error != EC_Normal)
{
OFLOG_FATAL(dcmdjplsLogger, error.text() << ": writing file: " << opt_ofname);
return 1;
}
OFLOG_INFO(dcmdjplsLogger, "conversion successful");
// deregister global decompression codecs
FMJPEG2KDecoderRegistration::cleanup();
return 0;
}