[a32309]: v3p / dcmtk / dcmdata / libsrc / dcmetinf.cxx Maximize Restore History

Download this file

dcmetinf.cxx    515 lines (447 with data), 18.7 kB

  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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
*
* Copyright (C) 1994-2002, OFFIS
*
* This software and supporting documentation were developed by
*
* Kuratorium OFFIS e.V.
* Healthcare Information and Communication Systems
* Escherweg 2
* D-26121 Oldenburg, Germany
*
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND OFFIS MAKES NO WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
* Module: dcmdata
*
* Author: Gerd Ehlers, Andreas Barth
*
* Purpose: Implementation of class DcmMetaInfo
*
*/
#include "osconfig.h" /* make sure OS specific configuration is included first */
#define INCLUDE_CSTDLIB
#define INCLUDE_CSTRING
#include "ofstdinc.h"
#include "ofstream.h"
#include "ofstd.h"
#include "dcdefine.h" /* for memzero() */
#include "dcmetinf.h"
#include "dcitem.h"
#include "dcxfer.h"
#include "dcvrul.h"
#include "dcdebug.h"
#include "dcdeftag.h"
#include "dcdefine.h"
#include "dcistrma.h" /* for class DcmInputStream */
#include "dcostrma.h" /* for class DcmOutputStream */
const Uint32 DCM_GroupLengthElementLength = 12;
// ********************************
DcmMetaInfo::DcmMetaInfo()
: DcmItem(ItemTag),
preambleUsed(OFFalse),
fPreambleTransferState(ERW_init),
Xfer(META_HEADER_DEFAULT_TRANSFERSYNTAX)
{
setPreamble();
}
DcmMetaInfo::DcmMetaInfo(const DcmMetaInfo &old)
: DcmItem(old),
preambleUsed(OFFalse),
fPreambleTransferState(ERW_init),
Xfer(META_HEADER_DEFAULT_TRANSFERSYNTAX)
{
if (old.ident() == EVR_metainfo)
{
Xfer = old.Xfer;
preambleUsed = old.preambleUsed;
memcpy(filePreamble, old.filePreamble, 128);
} else {
// wrong use of copy constructor
setPreamble();
}
}
DcmMetaInfo::~DcmMetaInfo()
{
}
// ********************************
DcmEVR DcmMetaInfo::ident() const
{
return EVR_metainfo;
}
E_TransferSyntax DcmMetaInfo::getOriginalXfer() const
{
return Xfer;
}
// ********************************
void DcmMetaInfo::print(ostream &out,
const size_t flags,
const int level,
const char *pixelFileName,
size_t *pixelCounter)
{
out << endl;
printNestingLevel(out, flags, level);
out << "# Dicom-Meta-Information-Header" << endl;
printNestingLevel(out, flags, level);
out << "# Used TransferSyntax: " << DcmXfer(Xfer).getXferName();
out << endl;
if (!elementList->empty())
{
DcmObject *dO;
elementList->seek(ELP_first);
do {
dO = elementList->get();
dO->print(out, flags, level + 1, pixelFileName, pixelCounter);
} while (elementList->seek(ELP_next));
}
}
// ********************************
OFCondition DcmMetaInfo::writeXML(ostream &out,
const size_t flags)
{
OFString xmlString;
DcmXfer xfer(Xfer);
/* XML start tag for "meta-header" */
out << "<meta-header xfer=\"" << xfer.getXferID() << "\"";
out << " name=\"" << OFStandard::convertToMarkupString(xfer.getXferName(), xmlString) << "\">" << endl;
if (!elementList->empty())
{
/* write content of all children */
DcmObject *dO;
elementList->seek(ELP_first);
do
{
dO = elementList->get();
dO->writeXML(out, flags);
} while (elementList->seek(ELP_next));
}
/* XML end tag for "meta-header" */
out << "</meta-header>" << endl;
/* always report success */
return EC_Normal;
}
// ********************************
void DcmMetaInfo::setPreamble()
{
memzero(filePreamble, sizeof(filePreamble));
preambleUsed = OFFalse;
}
// ********************************
OFBool DcmMetaInfo::checkAndReadPreamble(DcmInputStream &inStream,
E_TransferSyntax &newxfer)
{
if (fPreambleTransferState == ERW_init)
{
inStream.mark();
fPreambleTransferState = ERW_inWork;
}
OFBool retval = OFFalse;
if (fPreambleTransferState == ERW_inWork)
{
const Uint32 preambleLen = DCM_PreambleLen + DCM_MagicLen;
const Uint32 readLen = preambleLen-fTransferredBytes;
if (readLen > 0)
fTransferredBytes += inStream.read(&filePreamble[fTransferredBytes], readLen);
if (inStream.eos() && fTransferredBytes != preambleLen)
{ // file too short, no preamble
inStream.putback();
debug(4, ("DcmMetaInfo::checkAndReadPreamble() No Preamble available: File too short (%d) < %d bytes",
preambleLen, DCM_PreambleLen + DCM_MagicLen));
retval = OFFalse;
this -> setPreamble();
fPreambleTransferState = ERW_ready;
}
else if (fTransferredBytes == preambleLen) // check Preamble and Dicom Prefix
{ // set prefix to appropriate position
char *prefix = filePreamble + DCM_PreambleLen;
if (memcmp(prefix, DCM_Magic, DCM_MagicLen) == 0)
{
retval = OFTrue; // Preamble present
// inStream.UnsetPutbackMark(); // not needed anymore with new stream architecture
} else { // no Preamble
retval = OFFalse;
this -> setPreamble();
inStream.putback();
}
fPreambleTransferState = ERW_ready;
} else
errorFlag = EC_StreamNotifyClient;
}
if (fPreambleTransferState == ERW_ready)
{
E_TransferSyntax tmpxfer = checkTransferSyntax(inStream);
DcmXfer tmpxferSyn(tmpxfer);
DcmXfer xferSyn(newxfer);
if ((tmpxferSyn.isExplicitVR() && xferSyn.isImplicitVR()) ||
(tmpxferSyn.isImplicitVR() && xferSyn.isExplicitVR()) ||
xferSyn.getXfer() == EXS_Unknown)
{
newxfer = tmpxferSyn.getXfer(); // use determined xfer
if (xferSyn.getXfer() != EXS_Unknown)
{
ofConsole.lockCerr() << "Info: DcmMetaInfo::checkAndReadPreamble(): "
"TransferSyntax of MetaInfo is different from "
"passed parameter newxfer: ignoring newxfer!" << endl;
ofConsole.unlockCerr();
}
} else
newxfer = xferSyn.getXfer();
}
Cdebug(4, retval==OFTrue, ("DcmMetaInfo::checkAndReadPreamble() found Preamble=[0x%8.8x]", (Uint32)(*filePreamble)));
Cdebug(4, retval==OFFalse, ("DcmMetaInfo::checkAndReadPreamble() No Preambel found!"));
debug(4, ("DcmMetaInfo::checkAndReadPreamble() TransferSyntax = %s", DcmXfer(newxfer).getXferName()));
return retval;
} // DcmMetaInfo::checkAndReadPreamble
// ********************************
OFBool DcmMetaInfo::nextTagIsMeta(DcmInputStream &inStream)
{
char testbytes[2];
inStream.mark();
inStream.read(testbytes, 2);
inStream.putback();
// check for group 0x0002 only
return (testbytes[0] == 0x02 && testbytes[1] == 0x00) || (testbytes[0] == 0x00 && testbytes[1] == 0x02);
}
// ********************************
Uint32 DcmMetaInfo::calcElementLength(const E_TransferSyntax /*xfer*/,
const E_EncodingType enctype)
{
Uint32 metaLength = DcmItem::getLength(META_HEADER_DEFAULT_TRANSFERSYNTAX, enctype);
metaLength += DCM_PreambleLen + DCM_MagicLen;
return metaLength;
}
// ********************************
OFCondition DcmMetaInfo::readGroupLength(DcmInputStream &inStream,
const E_TransferSyntax xfer,
const DcmTagKey &xtag,
const E_GrpLenEncoding glenc,
Uint32 &headerLen,
Uint32 &bytesRead,
const Uint32 maxReadLength)
{
OFCondition l_error = EC_TagNotFound;
E_TransferSyntax newxfer = xfer;
bytesRead = 0;
headerLen = 0;
if (nextTagIsMeta(inStream))
{
DcmTag newTag;
Uint32 newValueLength = 0;
Uint32 bytes_tagAndLen = 0;
l_error = DcmItem::readTagAndLength(inStream, newxfer, newTag, newValueLength, bytes_tagAndLen);
bytesRead += bytes_tagAndLen;
if (l_error.good() && !inStream.good())
l_error = inStream.status();
if (l_error.good())
{
l_error = DcmItem::readSubElement(inStream, newTag, newValueLength, newxfer, glenc, maxReadLength);
bytesRead += newValueLength;
if (l_error.good() && newTag.getXTag() == xtag && elementList->get() != NULL && newValueLength > 0)
{
l_error = ((DcmUnsignedLong*)(elementList->get()))->getUint32(headerLen);
debug(4, ("DcmMetaInfo::readGroupLength() Group Length of File Meta Header=%d", headerLen+bytesRead));
} else {
l_error = EC_CorruptedData;
ofConsole.lockCerr() << "Warning: DcmMetaInfo::readGroupLength(): No Group"
" Length available in Meta Information Header" << endl;
ofConsole.unlockCerr();
}
}
}
debug(4, ("DcmMetaInfo::readGroupLength() returns error = %s", l_error.text()));
return l_error;
}
// ********************************
OFCondition DcmMetaInfo::read(DcmInputStream &inStream,
const E_TransferSyntax xfer,
const E_GrpLenEncoding glenc,
const Uint32 maxReadLength)
{
if (fPreambleTransferState == ERW_notInitialized || fTransferState == ERW_notInitialized)
errorFlag = EC_IllegalCall;
else
{
Xfer = xfer;
E_TransferSyntax newxfer = xfer;
/* figure out if the stream reported an error */
errorFlag = inStream.status();
if (errorFlag.good() && inStream.eos())
errorFlag = EC_EndOfStream;
else if (errorFlag.good() && fTransferState != ERW_ready)
{
if (fTransferState == ERW_init && fPreambleTransferState != ERW_ready)
{
if (xfer == EXS_Unknown)
preambleUsed = checkAndReadPreamble(inStream, newxfer);
else
newxfer = xfer;
if (fPreambleTransferState == ERW_ready)
{
Xfer = newxfer; // store parameter transfer syntax
fTransferState = ERW_inWork;
fTransferredBytes = 0;
fStartPosition = inStream.tell();
Length = 0;
}
}
if (fTransferState == ERW_inWork && Length == 0)
{
if (inStream.avail() < DCM_GroupLengthElementLength)
errorFlag = EC_StreamNotifyClient;
else
{
Uint32 headerLength = 0;
errorFlag = readGroupLength(inStream, newxfer, DCM_MetaElementGroupLength, glenc,
headerLength, fTransferredBytes, maxReadLength);
if (errorFlag.good())
Length = headerLength + fTransferredBytes;
else
Length = DCM_UndefinedLength;
}
}
if (fTransferState == ERW_inWork && Length != 0 && errorFlag.good())
{
while (inStream.good() && !inStream.eos() &&
((Length < DCM_UndefinedLength && fTransferredBytes < Length) ||
(Length == DCM_UndefinedLength && nextTagIsMeta(inStream)) ||
!lastElementComplete))
{
DcmTag newTag;
Uint32 newValueLength = 0;
Uint32 bytes_tagAndLen = 0;
if (lastElementComplete)
{
errorFlag = DcmItem::readTagAndLength(inStream, newxfer, newTag, newValueLength, bytes_tagAndLen);
fTransferredBytes += bytes_tagAndLen;
if (errorFlag != EC_Normal)
break; // terminate while loop
lastElementComplete = OFFalse;
errorFlag = DcmItem::readSubElement(inStream, newTag, newValueLength, newxfer, glenc, maxReadLength);
if (errorFlag.good())
lastElementComplete = OFTrue;
} else {
errorFlag = elementList->get()->read(inStream, xfer, glenc, maxReadLength);
if (errorFlag.good())
lastElementComplete = OFTrue;
}
fTransferredBytes = inStream.tell() - fStartPosition;
if (errorFlag.bad())
break; // terminate while loop
} //while
}
if (errorFlag == EC_TagNotFound || errorFlag == EC_EndOfStream)
{
errorFlag = EC_Normal; // there is no meta header
Xfer = EXS_Unknown;
} else if (errorFlag == EC_ItemEnd)
errorFlag = EC_Normal;
if (errorFlag.good())
{
if (Length != DCM_UndefinedLength && fTransferredBytes != Length)
{
ofConsole.lockCerr() << "Warning: DcmMetaInfo::read(): "
"Group Length of Meta"
" Information Header has incorrect Value!" << endl;
ofConsole.unlockCerr();
}
fTransferState = ERW_ready; // MetaInfo ist komplett
}
}
}
return errorFlag;
} // DcmMetaInfo::read()
// ********************************
void DcmMetaInfo::transferInit()
{
DcmItem::transferInit();
fPreambleTransferState = ERW_init;
}
// ********************************
void DcmMetaInfo::transferEnd()
{
DcmItem::transferEnd();
fPreambleTransferState = ERW_notInitialized;
}
// ********************************
OFCondition DcmMetaInfo::write(DcmOutputStream &outStream,
const E_TransferSyntax /*oxfer*/,
const E_EncodingType enctype)
/*
* This function writes all data elements which make up the meta header to the stream.
* For a specification of the elements that make up the meta header see DICOM standard
* (year 2000) part 10, section 7.1)) or the corresponding section in a later version of
* the standard).
*
* Parameters:
* outStream - [inout] The stream that the information will be written to.
* oxfer - [in] The transfer syntax which shall be used. (is not necessary since the meta header
* shall always be encoded in the explicit VR little endian transfer syntax)
* enctype - [in] Encoding type for sequences. Specifies how sequences will be handled.
*/
{
/* if the transfer state of this is not initialized, this is an illegal call */
if (fTransferState == ERW_notInitialized)
errorFlag = EC_IllegalCall;
else
{
/* if this is not an illegal call, do something */
/* determine the (default) transfer syntax which shall be used (always explicit VR little endian) */
E_TransferSyntax outxfer = META_HEADER_DEFAULT_TRANSFERSYNTAX;
/* check if the stream reported an error so far */
errorFlag = outStream.status();
/* if the stream did not report any error and the transfer state is ERW_ready, */
/* go ahead and write the meta header information to the out stream */
if (errorFlag.good() && fTransferState != ERW_ready)
{
/* if some particular conditions are met we need to write the file preamble (128 byte wide) and */
/* the DICOM prefix "DICM" to the stream. Always check if there is enough space in the stream and */
/* set the transfer state of certain elements to indicate that they have already been written. */
if (fTransferState == ERW_init)
{
if (preambleUsed || !elementList->empty())
{
if (fPreambleTransferState == ERW_init)
{
fTransferredBytes += outStream.write(&filePreamble[fTransferredBytes],
DCM_PreambleLen - fTransferredBytes);
if (fTransferredBytes != DCM_PreambleLen)
errorFlag = EC_StreamNotifyClient;
else
fPreambleTransferState = ERW_inWork;
}
if (fPreambleTransferState == ERW_inWork && outStream.avail() >= 4)
{
outStream.write(DCM_Magic, 4);
fPreambleTransferState = ERW_ready;
fTransferState = ERW_inWork;
elementList->seek(ELP_first);
} else
errorFlag = EC_StreamNotifyClient;
}
}
/* if the file premable and the DICOM prefix have been written, go */
/* ahead and write the meta header's data elements to the stream. */
/* (note that at this point elementList->get() should never be NULL, */
/* but lets play the game safe here...) */
if (!elementList->empty() && (fTransferState == ERW_inWork) && (elementList->get() != NULL))
{
DcmObject *dO;
/* iterate over the list of data elements and write them to the stream */
do {
dO = elementList->get();
errorFlag = dO->write(outStream, outxfer, enctype);
} while (errorFlag.good() && elementList->seek(ELP_next));
}
/* if the error flag equals ok and the transfer state equals ERW_inWork, all data elements of the meta */
/* header have been written to the stream. Indicate this by setting the transfer state to ERW_ready */
if (errorFlag.good() && fTransferState == ERW_inWork)
fTransferState = ERW_ready;
}
}
/* return result value */
return errorFlag;
}