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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
|
/*
*
* Copyright (C) 1994-2024, 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: dcmdata
*
* Author: Gerd Ehlers, Andreas Barth
*
* Purpose: class DcmFileFormat
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/ofstd/ofstream.h"
#include "dcmtk/dcmdata/dcfilefo.h"
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmdata/dcxfer.h"
#include "dcmtk/dcmdata/dcvrobow.h"
#include "dcmtk/dcmdata/dcvrui.h"
#include "dcmtk/dcmdata/dcvrul.h"
#include "dcmtk/dcmdata/dcvrus.h"
#include "dcmtk/dcmdata/dcvrae.h"
#include "dcmtk/dcmdata/dcvrsh.h"
#include "dcmtk/dcmdata/dcvrur.h"
#include "dcmtk/dcmdata/dcmetinf.h"
#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcuid.h"
#include "dcmtk/dcmdata/dcistrma.h" /* for class DcmInputStream */
#include "dcmtk/dcmdata/dcistrmf.h" /* for class DcmInputFileStream */
#include "dcmtk/dcmdata/dcistrms.h" /* for class DcmStdinStream */
#include "dcmtk/dcmdata/dcostrma.h" /* for class DcmOutputStream */
#include "dcmtk/dcmdata/dcostrmf.h" /* for class DcmOutputFileStream */
#include "dcmtk/dcmdata/dcostrms.h" /* for class DcmStdoutStream */
#include "dcmtk/dcmdata/dcwcache.h" /* for class DcmWriteCache */
#include "dcmtk/dcmdata/dcjson.h"
// ********************************
DcmFileFormat::DcmFileFormat()
: DcmSequenceOfItems(DCM_InternalUseTag),
FileReadMode(ERM_autoDetect)
{
DcmMetaInfo *MetaInfo = new DcmMetaInfo();
DcmSequenceOfItems::itemList->insert(MetaInfo);
MetaInfo->setParent(this);
DcmDataset *Dataset = new DcmDataset();
DcmSequenceOfItems::itemList->insert(Dataset);
Dataset->setParent(this);
}
DcmFileFormat::DcmFileFormat(DcmDataset *dataset,
OFBool deepCopy)
: DcmSequenceOfItems(DCM_InternalUseTag),
FileReadMode(ERM_autoDetect)
{
DcmMetaInfo *MetaInfo = new DcmMetaInfo();
DcmSequenceOfItems::itemList->insert(MetaInfo);
MetaInfo->setParent(this);
DcmDataset* insertion;
if (dataset == NULL)
{
insertion = new DcmDataset();
}
else if (deepCopy)
{
insertion = new DcmDataset(*dataset);
}
else // shallow copy
{
insertion = dataset;
}
insertion->setParent(this);
DcmSequenceOfItems::itemList->insert(insertion);
}
DcmFileFormat::DcmFileFormat(const DcmFileFormat &old)
: DcmSequenceOfItems(old),
FileReadMode(old.FileReadMode)
{
}
OFCondition DcmFileFormat::copyFrom(const DcmObject& rhs)
{
if (this != &rhs)
{
if (rhs.ident() != ident()) return EC_IllegalCall;
*this = OFstatic_cast(const DcmFileFormat &, rhs);
}
return EC_Normal;
}
DcmFileFormat::~DcmFileFormat()
{
}
DcmFileFormat &DcmFileFormat::operator=(const DcmFileFormat &obj)
{
if (this != &obj)
{
DcmSequenceOfItems::operator=(obj);
FileReadMode = obj.FileReadMode;
}
return *this;
}
// ********************************
DcmEVR DcmFileFormat::ident() const
{
return EVR_fileFormat;
}
// ********************************
void DcmFileFormat::print(STD_NAMESPACE ostream &out,
const size_t flags,
const int level,
const char *pixelFileName,
size_t *pixelCounter)
{
out << OFendl;
if (flags & DCMTypes::PF_useANSIEscapeCodes)
out << DCMDATA_ANSI_ESCAPE_CODE_COMMENT;
printNestingLevel(out, flags, level);
out << "# Dicom-File-Format";
if (flags & DCMTypes::PF_useANSIEscapeCodes)
out << DCMDATA_ANSI_ESCAPE_CODE_RESET;
out << OFendl;
if (!itemList->empty())
{
DcmObject *dO;
itemList->seek(ELP_first);
do {
dO = itemList->get();
dO->print(out, flags, level, pixelFileName, pixelCounter);
} while (itemList->seek(ELP_next));
} else {
if (flags & DCMTypes::PF_useANSIEscapeCodes)
out << DCMDATA_ANSI_ESCAPE_CODE_COMMENT;
printNestingLevel(out, flags, level);
out << "# Dicom-File-Format has been erased";
if (flags & DCMTypes::PF_useANSIEscapeCodes)
out << DCMDATA_ANSI_ESCAPE_CODE_RESET;
out << OFendl;
}
}
// ********************************
OFCondition DcmFileFormat::writeXML(STD_NAMESPACE ostream &out,
const size_t flags)
{
OFCondition l_error = EC_Normal;
if (flags & DCMTypes::XF_useNativeModel)
{
/* in Native DICOM Model, there is no concept of a "file format" */
DcmDataset *dset = getDataset();
if (dset != NULL)
{
/* write content of dataset */
l_error = dset->writeXML(out, flags);
} else {
l_error = makeOFCondition(OFM_dcmdata, EC_CODE_CannotConvertToXML, OF_error,
"Cannot convert to Native DICOM Model: No data set present");
}
} else {
/* XML start tag for "file-format" */
out << "<file-format";
if (flags & DCMTypes::XF_useXMLNamespace)
out << " xmlns=\"" << DCMTK_XML_NAMESPACE_URI << "\"";
out << ">" << OFendl;
/* write content of file meta information and dataset */
if (!itemList->empty())
{
/* write content of all children */
DcmObject *dO;
itemList->seek(ELP_first);
do {
dO = itemList->get();
l_error = dO->writeXML(out, flags & ~DCMTypes::XF_useXMLNamespace);
} while (l_error.good() && itemList->seek(ELP_next));
} else {
/* a file format should never be empty */
l_error = EC_CorruptedData;
}
if (l_error.good())
{
/* XML end tag for "file-format" */
out << "</file-format>" << OFendl;
}
}
return l_error;
}
// ********************************
OFCondition DcmFileFormat::writeJson(STD_NAMESPACE ostream &out,
DcmJsonFormat &format)
{
OFBool meta = format.printMetaheaderInformation;
DcmDataset *dset = getDataset();
OFCondition status = EC_Normal;
if (meta)
{
// print out meta-header elements and dataset (non-standard)
DcmMetaInfo *metinf = getMetaInfo();
out << format.indent() << "{" << format.newline();
if (metinf)
{
status = metinf->writeJsonExt(out, format, OFFalse, OFFalse);
}
if (dset && status.good())
{
if (metinf && (metinf->card() > 0) && (dset->card() > 0)) out << "," << format.newline();
status = dset->writeJsonExt(out, format, OFFalse, OFFalse);
}
out << format.newline() << format.indent() << "}" << format.newline();
}
else
{
// standard case: only print dataset
if (dset)
{
status = dset->writeJsonExt(out, format, OFTrue, OFTrue);
}
else
{
out << format.indent() << "{}" << format.newline();
}
}
return status;
}
// ********************************
OFCondition DcmFileFormat::checkMetaHeaderValue(DcmMetaInfo *metainfo,
DcmDataset *dataset,
const DcmTagKey &atagkey,
DcmObject *obj,
const E_TransferSyntax oxfer,
const E_FileWriteMode writeMode)
/*
* This function checks if a particular data element of the file meta information header is
* existent. If the element is not existent, it will be inserted. Additionally, this function
* makes sure that the corresponding data element will contain a correct value.
*
* Parameters:
* metainfo - [in] The meta header information.
* dataset - [in] The data set information.
* atagkey - [in] Tag of the data element which shall be checked.
* obj - [in] Data object from metainfo which represents the data element that shall be checked.
* Equals NULL, if this data element is not existent in the meta header information.
* oxfer - [in] The transfer syntax which shall be used.
* writeMode - [in] Flag indicating whether to update the file meta information or not.
*/
{
/* initialize result value */
OFCondition l_error = EC_Normal;
/* if there is meta header information and also data set information, do something */
if ((metainfo != NULL) && (dataset != NULL))
{
/* initialize variables */
DcmStack stack;
DcmTag tag(atagkey);
if (obj != NULL)
tag = obj->getTag();
DcmElement *elem = OFstatic_cast(DcmElement *, obj);
/* go ahead and scrutinize one particular data element (depending on tag) */
if (tag == DCM_FileMetaInformationGroupLength) // (0002,0000)
{
if (elem == NULL)
{
elem = new DcmUnsignedLong(tag);
metainfo->insert(elem, OFTrue);
}
Uint32 temp = 0;
if ((elem->getLength() == 0) && (elem->ident() == EVR_UL))
OFstatic_cast(DcmUnsignedLong *, elem)->putUint32Array(&temp, 1);
// the calculation of actual group length value is contained in validateMetaInfo()
}
else if (tag == DCM_FileMetaInformationVersion) // (0002,0001)
{
if (elem == NULL)
{
elem = new DcmOtherByteOtherWord(tag);
metainfo->insert(elem, OFTrue);
}
// supported version of this implementation: 00\01
Uint8 version[2] = {0, 1};
if ((elem->getLength() == 0) && (elem->ident() == EVR_OB))
OFstatic_cast(DcmOtherByteOtherWord *, elem)->putUint8Array(version, 2);
// check version of meta header
Uint8 *currVers = NULL;
l_error = OFstatic_cast(DcmOtherByteOtherWord *, elem)->getUint8Array(currVers);
if (l_error.good() && (currVers != NULL))
{
// the version information is stored in a bit field
if (((currVers[0] & version[0] & 0xff) == version[0]) &&
((currVers[1] & version[1] & 0xff) == version[1]))
{
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() Version of MetaHeader is ok: 0x"
<< STD_NAMESPACE hex << STD_NAMESPACE setfill('0')
<< STD_NAMESPACE setw(2) << OFstatic_cast(int, currVers[0])
<< STD_NAMESPACE setw(2) << OFstatic_cast(int, currVers[1]));
} else {
DCMDATA_WARN ("DcmFileFormat: Unknown Version of MetaHeader detected: 0x"
<< STD_NAMESPACE hex << STD_NAMESPACE setfill('0')
<< STD_NAMESPACE setw(2) << OFstatic_cast(int, currVers[0])
<< STD_NAMESPACE setw(2) << OFstatic_cast(int, currVers[1])
<< ", supported: 0x"
<< STD_NAMESPACE setw(2) << OFstatic_cast(int, version[0])
<< STD_NAMESPACE setw(2) << OFstatic_cast(int, version[1]));
}
} else {
DCMDATA_ERROR("DcmFileFormat: Cannot determine Version of MetaHeader");
}
}
else if (tag == DCM_MediaStorageSOPClassUID) // (0002,0002)
{
if (elem == NULL)
{
elem = new DcmUniqueIdentifier(tag);
metainfo->insert(elem, OFTrue);
}
if (elem->ident() == EVR_UI)
{
if ((writeMode == EWM_updateMeta) || (elem->getLength() == 0))
{
if (dataset->search(DCM_SOPClassUID, stack).good() && (stack.top()->ident() == EVR_UI))
{
char *uid = NULL;
l_error = OFstatic_cast(DcmUniqueIdentifier *, stack.top())->getString(uid);
OFstatic_cast(DcmUniqueIdentifier *, elem)->putString(uid);
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() use SOPClassUID [" << uid << "] from Dataset");
}
else if (elem->getLength() == 0)
{
OFstatic_cast(DcmUniqueIdentifier *, elem)->putString(UID_PrivateGenericFileSOPClass);
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() no SOPClassUID in Dataset, using PrivateGenericFileSOPClass");
}
}
else if (DCM_dcmdataLogger.isEnabledFor(OFLogger::WARN_LOG_LEVEL))
{
// check whether UID in meta-header is identical to the one in the dataset
if (dataset->search(DCM_SOPClassUID, stack).good() && (stack.top()->ident() == EVR_UI))
{
OFString uidDataset, uidMetaHeader;
OFstatic_cast(DcmUniqueIdentifier *, stack.top())->getOFStringArray(uidDataset);
OFstatic_cast(DcmUniqueIdentifier *, elem)->getOFStringArray(uidMetaHeader);
if (uidDataset != uidMetaHeader)
{
DCMDATA_WARN("DcmFileFormat: Value of SOPClassUID in MetaHeader and Dataset is different");
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() value of MediaStorageSOPClassUID (MetaHeader) [" << uidMetaHeader << "]");
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() value of SOPClassUID (Dataset) [" << uidDataset << "]");
}
}
}
}
}
else if (tag == DCM_MediaStorageSOPInstanceUID) // (0002,0003)
{
if (elem == NULL)
{
elem = new DcmUniqueIdentifier(tag);
metainfo->insert(elem, OFTrue);
}
if (elem->ident() == EVR_UI)
{
if ((writeMode == EWM_updateMeta) || (elem->getLength() == 0))
{
if (dataset->search(DCM_SOPInstanceUID, stack).good() && (stack.top()->ident() == EVR_UI))
{
char* uid = NULL;
l_error = OFstatic_cast(DcmUniqueIdentifier *, stack.top())->getString(uid);
OFstatic_cast(DcmUniqueIdentifier *, elem)->putString(uid);
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() use SOPInstanceUID [" << uid << "] from Dataset");
}
else if (elem->getLength() == 0)
{
char uid[128];
dcmGenerateUniqueIdentifier(uid); // from dcuid.h
OFstatic_cast(DcmUniqueIdentifier *, elem)->putString(uid);
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() use new generated SOPInstanceUID [" << uid << "]");
}
}
else if (DCM_dcmdataLogger.isEnabledFor(OFLogger::WARN_LOG_LEVEL))
{
// check whether UID in meta-header is identical to the one in the dataset
if (dataset->search(DCM_SOPInstanceUID, stack).good() && (stack.top()->ident() == EVR_UI))
{
OFString uidDataset, uidMetaHeader;
OFstatic_cast(DcmUniqueIdentifier *, stack.top())->getOFStringArray(uidDataset);
OFstatic_cast(DcmUniqueIdentifier *, elem)->getOFStringArray(uidMetaHeader);
if (uidDataset != uidMetaHeader)
{
DCMDATA_WARN("DcmFileFormat: Value of SOPInstanceUID in MetaHeader and Dataset is different");
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() value of MediaStorageSOPInstanceUID (MetaHeader) [" << uidMetaHeader << "]");
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() value of SOPInstanceUID (Dataset) [" << uidDataset << "]");
}
}
}
}
}
else if (tag == DCM_TransferSyntaxUID) // (0002,0010)
{
if (elem == NULL)
{
elem = new DcmUniqueIdentifier(tag);
metainfo->insert(elem, OFTrue);
}
if (elem->ident() == EVR_UI)
{
#ifdef DEBUG
char * uidtmp = NULL;
OFstatic_cast(DcmUniqueIdentifier *, elem)->getString(uidtmp);
if (uidtmp != NULL)
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() found old TransferSyntaxUID [" << uidtmp << "]");
#endif
DcmXfer dcXfer(oxfer);
const char *uid = dcXfer.getXferID();
elem->putString(uid);
DCMDATA_DEBUG("DcmFileFormat::checkMetaHeaderValue() use new TransferSyntaxUID ["
<< dcXfer.getXferName() << "] on writing following Dataset");
}
}
else if (tag == DCM_ImplementationClassUID) // (0002,0012)
{
if (elem == NULL)
{
elem = new DcmUniqueIdentifier(tag);
metainfo->insert(elem, OFTrue);
}
if (elem->ident() == EVR_UI)
{
const char *uid = OFFIS_IMPLEMENTATION_CLASS_UID;
OFstatic_cast(DcmUniqueIdentifier *, elem)->putString(uid);
}
}
else if (tag == DCM_ImplementationVersionName) // (0002,0013)
{
if (elem == NULL)
{
elem = new DcmShortString(tag);
metainfo->insert(elem, OFTrue);
}
if (elem->ident() == EVR_SH)
{
const char *uid = OFFIS_DTK_IMPLEMENTATION_VERSION_NAME;
OFstatic_cast(DcmShortString *, elem)->putString(uid);
}
}
else if ((tag == DCM_SourceApplicationEntityTitle) || // (0002,0016)
(tag == DCM_SendingApplicationEntityTitle) || // (0002,0017)
(tag == DCM_ReceivingApplicationEntityTitle)) // (0002,0018)
{
if (elem == NULL)
{
elem = new DcmApplicationEntity(tag);
metainfo->insert(elem, OFTrue);
}
DCMDATA_WARN("DcmFileFormat: Don't know how to handle " << tag.getTagName());
}
else if ((tag == DCM_SourcePresentationAddress) || // (0002,0026)
(tag == DCM_SendingPresentationAddress) || // (0002,0027)
(tag == DCM_ReceivingPresentationAddress)) // (0002,0028)
{
if (elem == NULL)
{
elem = new DcmUniversalResourceIdentifierOrLocator(tag);
metainfo->insert(elem, OFTrue);
}
DCMDATA_WARN("DcmFileFormat: Don't know how to handle " << tag.getTagName());
}
else if (tag == DCM_PrivateInformationCreatorUID) // (0002,0100)
{
if (elem == NULL)
{
elem = new DcmUniqueIdentifier(tag);
metainfo->insert(elem, OFTrue);
}
DCMDATA_WARN("DcmFileFormat: Don't know how to handle PrivateInformationCreatorUID");
}
else if (tag == DCM_PrivateInformation) // (0002,0102)
{
if (elem == NULL)
{
elem = new DcmOtherByteOtherWord(tag);
metainfo->insert(elem, OFTrue);
}
DCMDATA_WARN("DcmFileFormat: Don't know how to handle PrivateInformation");
} else
DCMDATA_ERROR("DcmFileFormat: Don't know how to handle " << tag.getTagName());
/* if at this point elem still equals NULL, something is fishy */
if (elem == NULL)
l_error = EC_InvalidVR;
} else {
/* (i.e. there is either no meta header information or no data set information */
l_error = EC_IllegalCall;
}
/* return result value */
return l_error;
}
// ********************************
OFCondition DcmFileFormat::validateMetaInfo(const E_TransferSyntax oxfer,
const E_FileWriteMode writeMode)
/*
* This function makes sure that all data elements of the meta header information are existent
* in metainfo and contain correct values.
*
* Parameters:
* oxfer - [in] The transfer syntax which shall be used.
* writeMode - [in] Flag indicating whether to update the file meta information or not.
*/
{
/* initialize some variables */
OFCondition l_error = EC_Normal;
DcmMetaInfo *metinf = getMetaInfo();
DcmDataset *datset = getDataset();
/* if there is meta header information and data set information, do something */
if (metinf != NULL && datset != NULL)
{
if (writeMode == EWM_dontUpdateMeta)
{
DCMDATA_WARN("DcmFileFormat: Meta Information Header is not updated!");
} else {
/* start with empty file meta information */
if (writeMode == EWM_createNewMeta)
metinf->clear();
/* in the following, we want to make sure all elements of the meta header */
/* are existent in metinf and contain correct values */
DcmStack stack;
/* DCM_FileMetaInformationGroupLength */
metinf->search(DCM_FileMetaInformationGroupLength, stack, ESM_fromHere, OFFalse);
checkMetaHeaderValue(metinf, datset, DCM_FileMetaInformationGroupLength, stack.top(), oxfer, writeMode);
/* DCM_FileMetaInformationVersion */
metinf->search(DCM_FileMetaInformationVersion, stack, ESM_fromHere, OFFalse);
checkMetaHeaderValue(metinf, datset, DCM_FileMetaInformationVersion, stack.top(), oxfer, writeMode);
/* DCM_MediaStorageSOPClassUID */
metinf->search(DCM_MediaStorageSOPClassUID, stack, ESM_fromHere, OFFalse);
checkMetaHeaderValue(metinf, datset, DCM_MediaStorageSOPClassUID, stack.top(), oxfer, writeMode);
/* DCM_MediaStorageSOPInstanceUID */
metinf->search(DCM_MediaStorageSOPInstanceUID, stack, ESM_fromHere, OFFalse);
checkMetaHeaderValue(metinf, datset, DCM_MediaStorageSOPInstanceUID, stack.top(), oxfer, writeMode);
/* DCM_TransferSyntaxUID */
metinf->search(DCM_TransferSyntaxUID, stack, ESM_fromHere, OFFalse);
checkMetaHeaderValue(metinf, datset, DCM_TransferSyntaxUID, stack.top(), oxfer, writeMode);
/* DCM_ImplementationClassUID */
metinf->search(DCM_ImplementationClassUID, stack, ESM_fromHere, OFFalse);
checkMetaHeaderValue(metinf, datset, DCM_ImplementationClassUID, stack.top(), oxfer, writeMode);
/* DCM_ImplementationVersionName */
metinf->search(DCM_ImplementationVersionName, stack, ESM_fromHere, OFFalse);
checkMetaHeaderValue(metinf, datset, DCM_ImplementationVersionName, stack.top(), oxfer, writeMode);
/* dump some information if required */
DCMDATA_DEBUG("DcmFileFormat::validateMetaInfo() found " << metinf->card() << " Elements in DcmMetaInfo 'metinf'");
/* calculate new GroupLength for meta header */
if (metinf->computeGroupLengthAndPadding(EGL_withGL, EPD_noChange,
META_HEADER_DEFAULT_TRANSFERSYNTAX, EET_UndefinedLength).bad())
{
DCMDATA_ERROR("DcmFileFormat: Group length of Meta Information Header not adapted");
}
}
} else {
/* (i.e. there is either no meta header information or no data set information, or both are missing) */
l_error = EC_CorruptedData;
}
/* return result value */
return l_error;
}
// ********************************
E_TransferSyntax DcmFileFormat::lookForXfer(DcmMetaInfo *metainfo)
{
E_TransferSyntax newxfer = EXS_Unknown;
DcmStack stack;
/* check whether meta header is present (and non-empty, i.e. contains elements) */
if (metainfo && !metainfo->isEmpty())
{
if (metainfo->search(DCM_TransferSyntaxUID, stack).good() && (stack.top()->ident() == EVR_UI))
{
DcmUniqueIdentifier *xferUI = OFstatic_cast(DcmUniqueIdentifier *, stack.top());
if (xferUI->getTag() == DCM_TransferSyntaxUID)
{
char *xferid = NULL;
xferUI->getString(xferid);
DcmXfer localXfer(xferid); // decode to E_TransferSyntax
newxfer = localXfer.getXfer();
DCMDATA_TRACE("DcmFileFormat::lookForXfer() TransferSyntax=\""
<< localXfer.getXferName() << "\" in MetaInfo");
}
} else {
/* there is no transfer syntax UID element in the meta header */
DCMDATA_DEBUG("DcmFileFormat::lookForXfer() no TransferSyntax in MetaInfo");
}
} else {
/* no meta header present at all (or it is empty, i.e. contains no elements) */
DCMDATA_DEBUG("DcmFileFormat::lookForXfer() no MetaInfo found");
}
return newxfer;
}
// ********************************
Uint32 DcmFileFormat::calcElementLength(const E_TransferSyntax xfer,
const E_EncodingType enctype)
{
return getMetaInfo()->calcElementLength(xfer, enctype) +
getDataset()->calcElementLength(xfer, enctype);
}
// ********************************
OFBool DcmFileFormat::canWriteXfer(const E_TransferSyntax newXfer,
const E_TransferSyntax oldXfer)
{
DcmDataset *dataset = getDataset();
if (dataset)
return dataset->canWriteXfer(newXfer, oldXfer);
else
return OFFalse;
}
// ********************************
OFCondition DcmFileFormat::read(DcmInputStream &inStream,
const E_TransferSyntax xfer,
const E_GrpLenEncoding glenc,
const Uint32 maxReadLength)
{
return DcmFileFormat::readUntilTag(inStream,xfer,glenc,maxReadLength,DCM_UndefinedTagKey);
}
OFCondition DcmFileFormat::readUntilTag(DcmInputStream &inStream,
const E_TransferSyntax xfer,
const E_GrpLenEncoding glenc,
const Uint32 maxReadLength,
const DcmTagKey &stopParsingAtElement)
{
if (getTransferState() == ERW_notInitialized)
errorFlag = EC_IllegalCall;
else
{
errorFlag = inStream.status();
E_TransferSyntax newxfer = xfer;
DcmDataset *dataset = NULL;
if (errorFlag.good() && inStream.eos())
errorFlag = EC_EndOfStream;
else if (errorFlag.good() && getTransferState() != ERW_ready)
{
// the new data is added to the end
itemList->seek(ELP_last);
DcmMetaInfo *metaInfo = getMetaInfo();
if (metaInfo == NULL && getTransferState() == ERW_init)
{
metaInfo = new DcmMetaInfo();
itemList->insert(metaInfo, ELP_first);
// remember the parent
metaInfo->setParent(this);
}
if (metaInfo && metaInfo->transferState() != ERW_ready)
{
// do read meta header not in given transfer syntax (always Little Endian Explicit)
errorFlag = metaInfo->read(inStream, EXS_Unknown, glenc, maxReadLength);
}
// bail out if the meta-header is still incomplete or an error occurred
if (errorFlag.bad()) return errorFlag;
// determine xfer from tag (0002,0010) in the meta header
newxfer = lookForXfer(metaInfo);
if ((FileReadMode == ERM_fileOnly) || (FileReadMode == ERM_metaOnly))
{
// reject file if no meta header present
if (errorFlag.good() && (newxfer == EXS_Unknown))
errorFlag = EC_FileMetaInfoHeaderMissing;
}
if (errorFlag.good() && (!metaInfo || metaInfo->transferState() == ERW_ready))
{
dataset = getDataset();
if (dataset == NULL && getTransferState() == ERW_init)
{
dataset = new DcmDataset();
itemList->seek (ELP_first);
itemList->insert(dataset, ELP_next);
// remember the parent
dataset->setParent(this);
}
// check whether to read the dataset at all
if (FileReadMode != ERM_metaOnly)
{
if (dataset && dataset->transferState() != ERW_ready)
{
errorFlag = dataset->readUntilTag(inStream, newxfer, glenc, maxReadLength, stopParsingAtElement);
}
}
}
}
if (getTransferState() == ERW_init)
setTransferState(ERW_inWork);
if (dataset && dataset->transferState() == ERW_ready)
setTransferState(ERW_ready);
}
return errorFlag;
} // DcmFileFormat::readUntilTag()
// ********************************
OFCondition DcmFileFormat::write(DcmOutputStream &outStream,
const E_TransferSyntax oxfer,
const E_EncodingType enctype,
DcmWriteCache *wcache)
{
return write(outStream, oxfer, enctype, wcache, EGL_recalcGL, EPD_noChange);
}
OFCondition DcmFileFormat::write(DcmOutputStream &outStream,
const E_TransferSyntax oxfer,
const E_EncodingType enctype,
DcmWriteCache *wcache,
const E_GrpLenEncoding glenc,
const E_PaddingEncoding padenc,
const Uint32 padlen,
const Uint32 subPadlen,
Uint32 instanceLength,
const E_FileWriteMode writeMode)
/*
* This function writes data values which are contained in this to the stream which is
* passed as first argument. With regard to the writing of information, the other parameters
* which are passed are accounted for. The function will return EC_Normal, if the information
* from all elements of this data set has been written to the buffer, and it will return some
* other (error) value if there was an error.
*
* Parameters:
* outStream - [inout] The stream that the information will be written to.
* oxfer - [in] The transfer syntax which shall be used.
* enctype - [in] Encoding type for sequences. Specifies how sequences will be handled.
* glenc - [in] Encoding type for group length. Specifies what will be done with group length tags.
* padenc - [in] Encoding type for padding. Specifies what will be done with padding tags.
* padlen - [in] The length up to which the dataset shall be padded, if padding is desired.
* subPadlen - [in] For sequences (i.e. sub elements), the length up to which item shall be padded,
* if padding is desired.
* instanceLength - [in] Number of extra bytes added to the item/dataset length used when computing the
* padding. This parameter is for instance used to pass the length of the file meta
* header from the DcmFileFormat to the DcmDataset object.
* writeMode - [in] Write file with or without meta header. Also allows for updating the information
* in the file meta information header.
*/
{
/* if the transfer state of this is not initialized, this is an illegal call */
if (getTransferState() == ERW_notInitialized)
errorFlag = EC_IllegalCall;
else
{
/* if this is not an illegal call, do something */
/* assign data set and the meta information header to local variables */
DcmDataset *dataset = getDataset();
DcmMetaInfo *metainfo = getMetaInfo();
/* Determine the transfer syntax which shall be used. Either we use the one which was passed, */
/* or (if it's an unknown transfer syntax) we use the data set's original transfer syntax. */
E_TransferSyntax outxfer = oxfer;
if (outxfer == EXS_Unknown && dataset)
outxfer = dataset->getOriginalXfer();
/* check if the stream reported an error so far */
errorFlag = outStream.status();
/* check if we can actually write data to the stream; in certain cases we cannot. */
if (outxfer == EXS_Unknown || outxfer == EXS_BigEndianImplicit)
errorFlag = EC_IllegalCall;
else if (itemList->empty())
errorFlag = EC_CorruptedData;
else if (errorFlag.good() && getTransferState() != ERW_ready)
{
/* in this case we can write data to the stream */
/* if this function was called for the first time for the dataset object, the transferState is */
/* still set to ERW_init. In this case, we need to validate the meta header information, set the */
/* item list pointer to the fist element and we need to set the transfer state to ERW_inWork. */
if (getTransferState() == ERW_init)
{
validateMetaInfo(outxfer, writeMode);
itemList->seek(ELP_first);
setTransferState(ERW_inWork);
}
/* if the transfer state is set to ERW_inWork, we need to write the */
/* information which is included in this to the buffer which was passed. */
if (getTransferState() == ERW_inWork)
{
/* write meta header information */
errorFlag = metainfo->write(outStream, outxfer, enctype, wcache);
/* recalculate the instance length */
instanceLength += metainfo->calcElementLength(outxfer, enctype);
/* if everything is ok, write the data set */
if (errorFlag.good())
errorFlag = dataset->write(outStream, outxfer, enctype, wcache, glenc, padenc, padlen,
subPadlen, instanceLength);
/* if everything is ok, set the transfer state to ERW_ready */
if (errorFlag.good())
setTransferState(ERW_ready);
}
}
/* in case the transfer syntax which shall be used is indeed the */
/* BigEndianImplicit transfer syntax dump some error information */
if (outxfer == EXS_BigEndianImplicit)
DCMDATA_ERROR("DcmFileFormat: Illegal TransferSyntax (BigEndianImplicit) used in write method");
}
/* return result value */
return errorFlag;
}
// ********************************
OFCondition DcmFileFormat::loadFile(const OFFilename &fileName,
const E_TransferSyntax readXfer,
const E_GrpLenEncoding groupLength,
const Uint32 maxReadLength,
const E_FileReadMode readMode)
{
return DcmFileFormat::loadFileUntilTag(fileName, readXfer, groupLength, maxReadLength, readMode, DCM_UndefinedTagKey);
}
OFCondition DcmFileFormat::loadFileUntilTag(
const OFFilename &fileName,
const E_TransferSyntax readXfer,
const E_GrpLenEncoding groupLength,
const Uint32 maxReadLength,
const E_FileReadMode readMode,
const DcmTagKey &stopParsingAtElement)
{
if (readMode == ERM_dataset)
return getDataset()->loadFileUntilTag(fileName, readXfer, groupLength, maxReadLength, stopParsingAtElement);
OFCondition l_error = EC_InvalidFilename;
/* check parameters first */
if (!fileName.isEmpty())
{
if (fileName.isStandardStream())
{
/* use stdin stream */
DcmStdinStream inStream;
/* clear this object */
l_error = clear();
if (l_error.good())
{
/* save old value */
const E_FileReadMode oldMode = FileReadMode;
FileReadMode = readMode;
/* initialize transfer */
transferInit();
do
{
/* fill the buffer from stdin */
inStream.fillBuffer();
/* and read the buffer content into the DICOM dataset */
l_error = readUntilTag(inStream, readXfer, groupLength, maxReadLength, stopParsingAtElement);
} while (l_error == EC_StreamNotifyClient); /* repeat until we're at the end of the stream, or an error occurs */
/* end transfer */
transferEnd();
/* restore old value */
FileReadMode = oldMode;
}
} else {
/* open file for output */
DcmInputFileStream fileStream(fileName);
/* check stream status */
l_error = fileStream.status();
if (l_error.good())
{
/* clear this object */
l_error = clear();
if (l_error.good())
{
/* save old value */
const E_FileReadMode oldMode = FileReadMode;
FileReadMode = readMode;
/* read data from file */
transferInit();
l_error = readUntilTag(fileStream, readXfer, groupLength, maxReadLength, stopParsingAtElement);
transferEnd();
/* restore old value */
FileReadMode = oldMode;
}
}
}
}
return l_error;
}
OFCondition DcmFileFormat::saveFile(const OFFilename &fileName,
const E_TransferSyntax writeXfer,
const E_EncodingType encodingType,
const E_GrpLenEncoding groupLength,
const E_PaddingEncoding padEncoding,
const Uint32 padLength,
const Uint32 subPadLength,
const E_FileWriteMode writeMode)
{
if (writeMode == EWM_dataset)
{
return getDataset()->saveFile(fileName, writeXfer, encodingType, groupLength,
padEncoding, padLength, subPadLength);
}
OFCondition l_error = EC_InvalidFilename;
/* check parameters first */
if (!fileName.isEmpty())
{
DcmWriteCache wcache;
DcmOutputStream *outStream = NULL;
DcmOutputFileStream *fileStream = NULL;
if (fileName.isStandardStream())
{
/* use stdout stream */
outStream = new DcmStdoutStream(fileName);
} else {
/* open file for output */
fileStream = new DcmOutputFileStream(fileName);
outStream = fileStream;
}
/* check stream status */
l_error = outStream->status();
if (l_error.good())
{
/* write data to file */
transferInit();
l_error = write(*outStream, writeXfer, encodingType, &wcache, groupLength,
padEncoding, padLength, subPadLength, 0 /*instanceLength*/, writeMode);
transferEnd();
}
if (l_error.good() && fileStream) l_error = fileStream->fclose();
delete outStream;
}
return l_error;
}
// ********************************
OFCondition DcmFileFormat::insertItem(DcmItem * /*item*/,
const unsigned long /*where*/)
{
DCMDATA_WARN("Illegal call of DcmFileFormat::insert(DcmItem *, unsigned long)");
errorFlag = EC_IllegalCall;
return errorFlag;
}
// ********************************
void DcmFileFormat::removeInvalidGroups()
{
getMetaInfo()->removeInvalidGroups();
getDataset()->removeInvalidGroups();
}
// ********************************
DcmItem *DcmFileFormat::remove(const unsigned long /*num*/)
{
DCMDATA_WARN("Illegal call of DcmFileFormat::remove(unsigned long)");
errorFlag = EC_IllegalCall;
return NULL;
}
// ********************************
DcmItem *DcmFileFormat::remove(DcmItem* /*item*/)
{
DCMDATA_WARN("Illegal call of DcmFileFormat::remove(DcmItem *)");
errorFlag = EC_IllegalCall;
return NULL;
}
// ********************************
OFCondition DcmFileFormat::clear()
{
getMetaInfo()->clear();
return getDataset()->clear();
}
// ********************************
DcmMetaInfo *DcmFileFormat::getMetaInfo()
{
errorFlag = EC_Normal;
DcmMetaInfo *meta = NULL;
if (itemList->seek_to(0) != NULL && itemList->get()->ident() == EVR_metainfo)
meta = OFstatic_cast(DcmMetaInfo *, itemList->get());
else
errorFlag = EC_IllegalCall;
return meta;
}
// ********************************
DcmDataset *DcmFileFormat::getDataset()
{
errorFlag = EC_Normal;
DcmDataset *data = NULL;
if (itemList->seek_to(1) != NULL && itemList->get()->ident() == EVR_dataset)
data = OFstatic_cast(DcmDataset *, itemList->get());
else
errorFlag = EC_IllegalCall;
return data;
}
// ********************************
DcmDataset *DcmFileFormat::getAndRemoveDataset()
{
errorFlag = EC_Normal;
DcmDataset *data = NULL;
if (itemList->seek_to(1) != NULL && itemList->get()->ident() == EVR_dataset)
{
data = OFstatic_cast(DcmDataset *, itemList->remove());
// forget about the parent
data->setParent(NULL);
DcmDataset *Dataset = new DcmDataset();
DcmSequenceOfItems::itemList->insert(Dataset, ELP_last);
// remember the parent
Dataset->setParent(this);
}
else
errorFlag = EC_IllegalCall;
return data;
}
// ********************************
OFCondition DcmFileFormat::convertCharacterSet(const OFString &fromCharset,
const OFString &toCharset,
const size_t flags)
{
// convert the dataset associated with this object
return getDataset()->convertCharacterSet(fromCharset, toCharset, flags);
}
OFCondition DcmFileFormat::convertCharacterSet(const OFString &toCharset,
const size_t flags)
{
OFString sopClass;
OFBool ignoreCharset = OFFalse;
// check whether this dataset belongs to a DICOMDIR,
// because the Basic Directory IOD has no SOP Common Module
if (getMetaInfo()->findAndGetOFString(DCM_MediaStorageSOPClassUID, sopClass).good() &&
(sopClass == UID_MediaStorageDirectoryStorage))
{
DCMDATA_DEBUG("DcmFileFormat::convertCharacterSet() according to the value of MediaStorageSOPClassUID "
<< DCM_MediaStorageSOPClassUID << " this is a DICOMDIR, which has no SOP Common Module");
ignoreCharset = OFTrue;
}
// usually, we check for Specific Character Set (0008,0005) element in the dataset
return getDataset()->convertCharacterSet(toCharset, flags, ignoreCharset);
}
OFCondition DcmFileFormat::convertCharacterSet(DcmSpecificCharacterSet &converter)
{
// convert the dataset associated with this object
return getDataset()->convertCharacterSet(converter);
}
OFCondition DcmFileFormat::convertToUTF8()
{
// the DICOM defined term "ISO_IR 192" is used for "UTF-8"
return convertCharacterSet("ISO_IR 192", 0 /*flags*/);
}
|