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 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
|
/*
*
* Copyright (C) 1997-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: Andreas Barth
*
* Purpose: class DcmPixelData
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/dcmdata/dcpixel.h"
#include "dcmtk/dcmdata/dccodec.h"
#include "dcmtk/dcmdata/dcpixseq.h"
#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmdata/dcpxitem.h"
#include "dcmtk/dcmdata/dcjson.h"
//
// class DcmRepresentationEntry
//
DcmRepresentationEntry::DcmRepresentationEntry(
const E_TransferSyntax rt,
const DcmRepresentationParameter *rp,
DcmPixelSequence * ps)
: repType(rt),
repParam(NULL),
pixSeq(ps)
{
if (rp)
repParam = rp->clone();
}
DcmRepresentationEntry::DcmRepresentationEntry(
const DcmRepresentationEntry & oldEntry)
: repType(oldEntry.repType),
repParam(NULL),
pixSeq(NULL)
{
if (oldEntry.repParam)
repParam = oldEntry.repParam->clone();
pixSeq = new DcmPixelSequence(*(oldEntry.pixSeq));
}
DcmRepresentationEntry::~DcmRepresentationEntry()
{
delete repParam;
delete pixSeq;
}
OFBool
DcmRepresentationEntry::operator==(const DcmRepresentationEntry & x) const
{
return (repType == x.repType) &&
((x.repParam == NULL && repParam == NULL) ||
((x.repParam != NULL) && (repParam != NULL) && (*(x.repParam) == *repParam)));
}
//
// class DcmPixelData
//
// Constructors / Deconstructors
DcmPixelData::DcmPixelData(
const DcmTag & tag,
const Uint32 len)
: DcmPolymorphOBOW(tag, len),
repList(),
repListEnd(),
original(),
current(),
existUnencapsulated(OFFalse),
alwaysUnencapsulated(OFFalse),
unencapsulatedVR(EVR_UNKNOWN),
pixelSeqForWrite(NULL)
{
repListEnd = repList.end();
current = original = repListEnd;
if ((getTag().getEVR() == EVR_ox) || (getTag().getEVR() == EVR_px))
setTagVR(EVR_OW);
unencapsulatedVR = getTag().getEVR();
recalcVR();
}
DcmPixelData::DcmPixelData(
const DcmPixelData & oldPixelData)
: DcmPolymorphOBOW(oldPixelData),
repList(),
repListEnd(),
original(),
current(),
existUnencapsulated(oldPixelData.existUnencapsulated),
alwaysUnencapsulated(oldPixelData.alwaysUnencapsulated),
unencapsulatedVR(oldPixelData.unencapsulatedVR),
pixelSeqForWrite(NULL)
{
repListEnd = repList.end();
original = repListEnd;
current = original;
recalcVR();
DcmRepresentationListConstIterator oldEnd(oldPixelData.repList.end());
for (DcmRepresentationListConstIterator it(oldPixelData.repList.begin());
it != oldEnd;
++it)
{
DcmRepresentationEntry * repEnt = new DcmRepresentationEntry(**it);
repList.push_back(repEnt);
if (it == oldPixelData.original)
original = --repList.end();
if (it == oldPixelData.current)
{
current = --repList.end();
recalcVR();
}
}
}
DcmPixelData::~DcmPixelData()
{
for (DcmRepresentationListIterator it(repList.begin());
it != repListEnd;
++it)
{
delete *it;
*it = NULL;
}
}
DcmPixelData &DcmPixelData::operator=(const DcmPixelData &obj)
{
if (this != &obj)
{
DcmPolymorphOBOW::operator=(obj);
existUnencapsulated = obj.existUnencapsulated;
alwaysUnencapsulated = obj.alwaysUnencapsulated;
unencapsulatedVR = obj.unencapsulatedVR;
pixelSeqForWrite = NULL;
repList.clear();
repListEnd = repList.end();
original = repListEnd;
current = original;
recalcVR();
DcmRepresentationListConstIterator oldEnd(obj.repList.end());
DcmRepresentationListConstIterator it(obj.repList.begin());
while (it != oldEnd)
{
DcmRepresentationEntry *repEnt = new DcmRepresentationEntry(**it);
repList.push_back(repEnt);
if (it == obj.original) original = --repList.end();
if (it == current)
{
current = --repList.end();
recalcVR();
}
++it;
}
}
return *this;
}
// methods in alphabetical order
Uint32
DcmPixelData::calcElementLength(
const E_TransferSyntax xfer,
const E_EncodingType enctype)
{
DcmXfer xferSyn(xfer);
errorFlag = EC_Normal;
Uint32 elementLength = 0;
if (xferSyn.usesEncapsulatedFormat() && (! writeUnencapsulated(xfer)))
{
DcmRepresentationListIterator found;
errorFlag = findConformingEncapsulatedRepresentation(xfer, NULL, found);
if (errorFlag == EC_Normal)
elementLength = (*found)->pixSeq->calcElementLength(xfer, enctype);
}
else if (existUnencapsulated)
elementLength = DcmPolymorphOBOW::calcElementLength(xfer, enctype);
else
errorFlag = EC_RepresentationNotFound;
return elementLength;
}
OFBool
DcmPixelData::canChooseRepresentation(
const E_TransferSyntax repType,
const DcmRepresentationParameter * repParam)
{
OFBool result = OFFalse;
DcmXfer toType(repType);
const DcmRepresentationEntry findEntry(repType, repParam, NULL);
DcmRepresentationListIterator resultIt(repListEnd);
// find out whether we have the desired target representation available. Three possibilities:
// 1. we have uncompressed data, and target is uncompressed (conversion between uncompressed always possible)
// 2. we have uncompressed and want compressed, but we are forced to write uncompressed anyway
// 3. we want to go to compressed, and already have the desired representation available
if ((toType.usesNativeFormat() && existUnencapsulated) ||
(toType.usesEncapsulatedFormat() && writeUnencapsulated(repType) && existUnencapsulated) ||
(toType.usesEncapsulatedFormat() && findRepresentationEntry(findEntry, resultIt) == EC_Normal))
{
// representation found
result = OFTrue;
}
// otherwise let's see whether we know how to convert to the target representation
else
{
// representation not found, check if we have a codec that can create the
// desired representation.
if (original == repListEnd)
{
// we have uncompressed data, check whether we know how to go from uncompressed to desired compression
result = DcmCodecList::canChangeCoding(EXS_LittleEndianExplicit, toType.getXfer());
}
else if (toType.usesEncapsulatedFormat())
{
// we have encapsulated data, check whether we know how to transcode
result = DcmCodecList::canChangeCoding((*original)->repType, toType.getXfer());
if (!result)
{
// direct transcoding is not possible. Check if we can decode and then encode.
result = canChooseRepresentation(EXS_LittleEndianExplicit, NULL);
if (result) result = DcmCodecList::canChangeCoding(EXS_LittleEndianExplicit, toType.getXfer());
}
}
else
{
// target transfer syntax is uncompressed, look whether decompression is possible
result = DcmCodecList::canChangeCoding((*original)->repType, EXS_LittleEndianExplicit);
}
}
return result;
}
OFBool
DcmPixelData::canWriteXfer(
const E_TransferSyntax newXfer,
const E_TransferSyntax /*oldXfer*/)
{
DcmXfer newXferSyn(newXfer);
DcmRepresentationListIterator found;
OFBool result = existUnencapsulated && (newXferSyn.usesNativeFormat() || writeUnencapsulated(newXfer));
if (!result && newXferSyn.usesEncapsulatedFormat())
result = (findConformingEncapsulatedRepresentation(newXferSyn, NULL, found) == EC_Normal);
return result;
}
OFCondition
DcmPixelData::chooseRepresentation(
const E_TransferSyntax repType,
const DcmRepresentationParameter * repParam,
DcmStack & pixelStack)
{
OFCondition l_error = EC_CannotChangeRepresentation;
DcmXfer toType(repType);
const DcmRepresentationEntry findEntry(repType, repParam, NULL);
DcmRepresentationListIterator result(repListEnd);
if ((toType.usesNativeFormat() && existUnencapsulated) ||
(toType.usesEncapsulatedFormat() && findRepresentationEntry(findEntry, result) == EC_Normal))
{
// representation found
current = result;
recalcVR();
l_error = EC_Normal;
}
else
{
if (original == repListEnd)
l_error = encode(EXS_LittleEndianExplicit, NULL, NULL,
toType, repParam, pixelStack);
else if (toType.usesEncapsulatedFormat())
l_error = encode((*original)->repType, (*original)->repParam,
(*original)->pixSeq, toType, repParam, pixelStack);
else
l_error = decode((*original)->repType, (*original)->repParam,
(*original)->pixSeq, pixelStack);
}
if (l_error.bad() && toType.usesEncapsulatedFormat() && existUnencapsulated && writeUnencapsulated(repType))
// Encoding failed so this will be written out unencapsulated
l_error = EC_Normal;
return l_error;
}
int DcmPixelData::compare(const DcmElement& rhs) const
{
// check tag and VR
int result = DcmElement::compare(rhs);
if (result != 0)
{
return result;
}
// cast away constness (dcmdata is not const correct...)
DcmPixelData* myThis = NULL;
DcmPixelData* myRhs = NULL;
myThis = OFconst_cast(DcmPixelData*, this);
myRhs = OFstatic_cast(DcmPixelData*, OFconst_cast(DcmElement*, &rhs));
if (myThis->existUnencapsulated && myRhs->existUnencapsulated)
{
// we have uncompressed representations, which can be compared using DcmPolymorphOBOW::compare
return DcmPolymorphOBOW::compare(rhs);
}
// both do not have uncompressed data, we must compare compressed ones.
// check both have a current representation at all.
if ((myThis->current == myThis->repList.end()) && (myRhs->current != myRhs->repList.end())) return -1;
if ((myThis->current != myThis->repList.end()) && (myRhs->current == myRhs->repList.end())) return 1;
if ((myThis->current == myThis->repList.end()) && (myRhs->current == myRhs->repList.end()))
{
// if one of both have uncompressed data at least, that one is considered "bigger"
if (myThis->existUnencapsulated) return 1;
if (myRhs->existUnencapsulated) return -1;
else return 0;
}
// both have compressed data: compare current representation (only)
if ((myThis->current != myThis->repList.end()) && (myRhs->current != myRhs->repList.end()) )
{
E_TransferSyntax myRep = (*(myThis->current))->repType;
E_TransferSyntax rhsRep = (*(myRhs->current))->repType;
DcmXfer myXfer(myRep);
DcmXfer rhsXfer(rhsRep);
// if both transfer syntaxes are different, we have to perform more checks to
// find out whether the related pixel data is comparable; this is the case
// for all uncompressed transfer syntaxes, except Big Endian with OW data
// since it uses a different memory layout, and we do not want to byte-swap
// the values for the comparison.
if (myRep != rhsRep)
{
return 1;
}
else
{
// For compressed, compare pixel items bytewise
DcmPixelSequence* myPix = (*(myThis->current))->pixSeq;
DcmPixelSequence* rhsPix = (*(myRhs->current))->pixSeq;
if (!myPix && rhsPix) return -1;
if (myPix && !rhsPix) return 1;
if (!myPix && !rhsPix) return 0;
// Check number of pixel items
long unsigned int myNumPix = myPix->card();
long unsigned int rhsNumPix = rhsPix->card();
if (myNumPix < rhsNumPix) return -1;
if (myNumPix > rhsNumPix) return 1;
// loop over pixel items, both have the same number of pixel items
for (unsigned long n = 0; n < myNumPix; n++)
{
DcmPixelItem* myPixItem = NULL;
DcmPixelItem* rhsPixItem = NULL;
if (myPix->getItem(myPixItem, n).good() && rhsPix->getItem(rhsPixItem, n).good())
{
// compare them value by value, using DcmOtherByteOtherWord::compare() method
result = myPixItem->compare(*rhsPixItem);
if (result != 0)
{
return result;
}
}
else
{
DCMDATA_ERROR("Internal error: Could not get pixel item #" << n << " from Pixel Sequence");
return 1;
}
}
return 0;
}
}
// if one of both have a current representation; consider that one "bigger".
// if none has a current one, consider both equal (neither uncompressed or compressed data present).
else
{
if (myThis->current != myThis->repList.end()) return 1;
if (myRhs->current != myRhs->repList.end()) return -1;
else return 0;
}
}
OFCondition DcmPixelData::copyFrom(const DcmObject& rhs)
{
if (this != &rhs)
{
if (rhs.ident() != ident()) return EC_IllegalCall;
*this = OFstatic_cast(const DcmPixelData &, rhs);
}
return EC_Normal;
}
void DcmPixelData::clearRepresentationList(
DcmRepresentationListIterator leaveInList)
{
/* define iterators to go through all representations in the list */
DcmRepresentationListIterator it(repList.begin());
DcmRepresentationListIterator del;
/* as long as we have not encountered the end of the */
/* representation list, go through all representations */
while (it != repListEnd)
{
/* if this representation shall not be left in the list */
if (it != leaveInList)
{
/* delete representation and move it to the next representation */
delete *it;
del = it++;
repList.erase(del);
}
/* else leave this representation in the list and just go to the next */
else
++it;
}
}
OFCondition
DcmPixelData::decode(
const DcmXfer & fromType,
const DcmRepresentationParameter * fromParam,
DcmPixelSequence * fromPixSeq,
DcmStack & pixelStack)
{
if (existUnencapsulated) return EC_Normal;
OFBool removeOldPixelRepresentation = OFFalse;
OFCondition l_error = DcmCodecList::decode(fromType, fromParam, fromPixSeq, *this, pixelStack, removeOldPixelRepresentation);
if (l_error.good())
{
existUnencapsulated = OFTrue;
current = repListEnd;
setVR(EVR_OW);
recalcVR();
// the codec has indicated that the image pixel module has been modified
// in a way that may affect the validity of the old representation of pixel data.
// Thus, we cannot just switch back to the old representation.
// Thus, remove old representation(s).
if (removeOldPixelRepresentation) removeAllButCurrentRepresentations();
}
else
{
DcmPolymorphOBOW::putUint16Array(NULL,0);
existUnencapsulated = OFFalse;
}
return l_error;
}
OFCondition
DcmPixelData::encode(
const DcmXfer & fromType,
const DcmRepresentationParameter * fromParam,
DcmPixelSequence * fromPixSeq,
const DcmXfer & toType,
const DcmRepresentationParameter *toParam,
DcmStack & pixelStack)
{
OFCondition l_error = EC_CannotChangeRepresentation;
if (toType.usesEncapsulatedFormat())
{
DcmPixelSequence * toPixSeq = NULL;
OFBool removeOldPixelRepresentation = OFFalse;
if (fromType.usesEncapsulatedFormat())
{
l_error = DcmCodecList::encode(fromType.getXfer(), fromParam, fromPixSeq,
toType.getXfer(), toParam, toPixSeq, pixelStack, removeOldPixelRepresentation);
}
else
{
Uint16 * pixelData;
l_error = DcmPolymorphOBOW::getUint16Array(pixelData);
Uint32 length = DcmPolymorphOBOW::getLength();
if (l_error == EC_Normal)
{
l_error = DcmCodecList::encode(fromType.getXfer(), pixelData, length,
toType.getXfer(), toParam, toPixSeq, pixelStack, removeOldPixelRepresentation);
}
}
if (l_error.good())
{
current = insertRepresentationEntry(
new DcmRepresentationEntry(toType.getXfer(), toParam, toPixSeq));
recalcVR();
// the codec has indicated that the image pixel module has been modified
// in a way that may affect the validity of the old representation of pixel data.
// Thus, we cannot just switch back to the old representation, but have
// to actually decode in this case. Thus, remove old representation(s).
if (removeOldPixelRepresentation) removeAllButCurrentRepresentations();
} else delete toPixSeq;
// if it was possible to convert one encapsulated syntax into
// another directly try it using decoding and encoding!
if (l_error.bad() && fromType.usesEncapsulatedFormat())
{
l_error = decode(fromType, fromParam, fromPixSeq, pixelStack);
if (l_error.good()) l_error = encode(EXS_LittleEndianExplicit, NULL, NULL, toType, toParam, pixelStack);
}
}
return l_error;
}
OFCondition
DcmPixelData::findRepresentationEntry(
const DcmRepresentationEntry & findEntry,
DcmRepresentationListIterator & result)
{
result = repList.begin();
while(result != repListEnd &&
(*result)->repType < findEntry.repType)
++result;
DcmRepresentationListIterator it(result);
while(it != repListEnd && **it != findEntry)
++it;
if (it == repListEnd || **it != findEntry)
return EC_RepresentationNotFound;
else
{
result = it;
return EC_Normal;
}
}
OFCondition
DcmPixelData::findConformingEncapsulatedRepresentation(
const DcmXfer & repTypeSyn,
const DcmRepresentationParameter * repParam,
DcmRepresentationListIterator & result)
{
E_TransferSyntax repType = repTypeSyn.getXfer();
result = repListEnd;
OFCondition l_error = EC_RepresentationNotFound;
// we are looking for an encapsulated representation
// of this pixel data element which meets both
// the transfer syntax and (if given) the representation
// parameter (i.e. quality factor for lossy JPEG).
if (repTypeSyn.usesEncapsulatedFormat())
{
// first we check the current (active) representation if any.
if ((current != repListEnd) && ((*current)->repType == repType) &&
((repParam==NULL) || (((*current)->repParam != NULL)&&(*(*current)->repParam == *repParam))))
{
result = current;
l_error = EC_Normal;
}
else
{
// now we check all representations
DcmRepresentationListIterator it(repList.begin());
OFBool found = OFFalse;
while (!found && (it != repListEnd))
{
if ((*it)->repType == repType)
{
if ((repParam == NULL) || (((*it)->repParam != NULL)&&(*(*it)->repParam == *repParam)))
{
// repParam is NULL or matches the one we are comparing with
found = OFTrue;
result = it;
l_error = EC_Normal;
} else ++it;
} else ++it;
}
}
}
return l_error;
}
OFCondition
DcmPixelData::getEncapsulatedRepresentation(
const E_TransferSyntax repType,
const DcmRepresentationParameter * repParam,
DcmPixelSequence * & pixSeq)
{
DcmRepresentationListIterator found;
DcmRepresentationEntry findEntry(repType, repParam, NULL);
if (findRepresentationEntry(findEntry, found) == EC_Normal)
{
pixSeq = (*found)->pixSeq;
return EC_Normal;
}
return EC_RepresentationNotFound;
}
OFBool
DcmPixelData::hasRepresentation(
const E_TransferSyntax repType,
const DcmRepresentationParameter * repParam)
{
DcmXfer repTypeSyn(repType);
DcmRepresentationListIterator found;
if (repTypeSyn.usesNativeFormat() && existUnencapsulated)
return OFTrue;
else if (repTypeSyn.usesEncapsulatedFormat())
return findConformingEncapsulatedRepresentation(repTypeSyn, repParam, found).good();
return OFFalse;
}
Uint32
DcmPixelData::getLength(const E_TransferSyntax xfer,
const E_EncodingType enctype)
{
DcmXfer xferSyn(xfer);
errorFlag = EC_Normal;
Uint32 valueLength = 0;
if (xferSyn.usesEncapsulatedFormat() && !writeUnencapsulated(xfer))
{
DcmRepresentationListIterator foundEntry;
errorFlag = findConformingEncapsulatedRepresentation(
xferSyn, NULL, foundEntry);
if (errorFlag == EC_Normal)
valueLength = (*foundEntry)->pixSeq->getLength(xfer, enctype);
}
else if (existUnencapsulated)
valueLength = DcmPolymorphOBOW::getLength(xfer, enctype);
else
errorFlag = EC_RepresentationNotFound;
return valueLength;
}
void
DcmPixelData::getCurrentRepresentationKey(
E_TransferSyntax & repType,
const DcmRepresentationParameter * & repParam)
{
if (current != repListEnd)
{
repType = (*current)->repType;
repParam = (*current)->repParam;
}
else
{
repType = EXS_LittleEndianExplicit;
repParam = NULL;
}
}
void
DcmPixelData::getOriginalRepresentationKey(
E_TransferSyntax & repType,
const DcmRepresentationParameter * & repParam)
{
if (original != repListEnd)
{
repType = (*original)->repType;
repParam = (*original)->repParam;
}
else
{
repType = EXS_LittleEndianExplicit;
repParam = NULL;
}
}
DcmRepresentationListIterator
DcmPixelData::insertRepresentationEntry(
DcmRepresentationEntry * repEntry)
{
DcmRepresentationListIterator insertedEntry;
DcmRepresentationListIterator result;
if (findRepresentationEntry(*repEntry, result).good())
{
// this type of representation entry was already present in the list
if (repEntry != *result)
{
insertedEntry = repList.insert(result, repEntry);
// delete old entry from representation list
delete *result;
repList.erase(result);
}
}
else
insertedEntry = repList.insert(result,repEntry);
return insertedEntry;
}
void
DcmPixelData::print(
STD_NAMESPACE ostream &out,
const size_t flags,
const int level,
const char *pixelFileName,
size_t *pixelCounter)
{
if (current == repListEnd)
printPixel(out, flags, level, pixelFileName, pixelCounter);
else
(*current)->pixSeq->print(out, flags, level, pixelFileName, pixelCounter);
}
OFCondition
DcmPixelData::putUint8Array(
const Uint8 * byteValue,
const unsigned long length)
{
// clear RepresentationList
clearRepresentationList(repListEnd);
OFCondition l_error = DcmPolymorphOBOW::putUint8Array(byteValue, length);
original = current = repListEnd;
recalcVR();
existUnencapsulated = OFTrue;
return l_error;
}
OFCondition
DcmPixelData::putUint16Array(
const Uint16 * wordValue,
const unsigned long length)
{
// clear RepresentationList
clearRepresentationList(repListEnd);
OFCondition l_error = DcmPolymorphOBOW::putUint16Array(wordValue, length);
original = current = repListEnd;
recalcVR();
existUnencapsulated = OFTrue;
return l_error;
}
OFCondition
DcmPixelData::createUint8Array(
const Uint32 numBytes,
Uint8 * & bytes)
{
OFCondition l_error = DcmPolymorphOBOW::createUint8Array(numBytes, bytes);
existUnencapsulated = OFTrue;
return l_error;
}
OFCondition
DcmPixelData::createUint16Array(
const Uint32 numWords,
Uint16 * & words)
{
OFCondition l_error = DcmPolymorphOBOW::createUint16Array(numWords, words);
existUnencapsulated = OFTrue;
return l_error;
}
OFCondition
DcmPixelData::createValueFromTempFile(
DcmInputStreamFactory *factory,
const Uint32 length,
const E_ByteOrder byteOrder)
{
OFCondition l_error = DcmPolymorphOBOW::createValueFromTempFile(factory, length, byteOrder);
existUnencapsulated = OFTrue;
return l_error;
}
void
DcmPixelData::putOriginalRepresentation(
const E_TransferSyntax repType,
const DcmRepresentationParameter * repParam,
DcmPixelSequence * pixSeq)
{
// delete RepresentationList
clearRepresentationList(repListEnd);
// delete unencapsulated representation
DcmPolymorphOBOW::putUint16Array(NULL,0);
existUnencapsulated = OFFalse;
// insert new Representation
current = original = insertRepresentationEntry(
new DcmRepresentationEntry(repType, repParam, pixSeq));
recalcVR();
}
OFCondition
DcmPixelData::read(
DcmInputStream & inStream,
const E_TransferSyntax ixfer,
const E_GrpLenEncoding glenc,
const Uint32 maxReadLength)
{
/* if this element's transfer state shows ERW_notInitialized, this is an illegal call */
if (getTransferState() == ERW_notInitialized)
errorFlag = EC_IllegalCall;
else
{
/* if this is not an illegal call, go ahead */
/* if the transfer state is ERW_init, we need to prepare the reading of the pixel */
/* data from the stream: remove all representations from the representation list. */
if (getTransferState() == ERW_init)
clearRepresentationList(repListEnd);
/* create a DcmXfer object based on the transfer syntax which was passed */
DcmXfer ixferSyn(ixfer);
/* determine if the pixel data is captured in native or encapsulated format.
* We only derive this information from the length field which is set to
* undefined length for encapsulated data because even in compressed transfer
* syntaxes the Icon Image Sequence may contain an uncompressed image.
*/
if (getLengthField() == DCM_UndefinedLength)
{
/* the pixel data is captured in encapsulated (e.g. compressed) format */
/* if the transfer state is ERW_init, we need to prepare */
/* the reading of the pixel data from the stream. */
if (getTransferState() == ERW_init)
{
current = insertRepresentationEntry(
new DcmRepresentationEntry(
ixfer, NULL, new DcmPixelSequence(getTag(), getLengthField())));
recalcVR();
original = current;
existUnencapsulated = OFFalse;
setTransferState(ERW_inWork);
if (! ixferSyn.usesEncapsulatedFormat())
{
/* Special case: we have encountered encapsulated format for
* the pixel data (undefined element length) although we're
* decoding a non-encapsulated transfer syntax. This could e.g.
* be a compressed image stored without meta-header. For now,
* we just accept the data element; however, any attempt to
* write the dataset will fail because no suitable decoder is
* known.
*/
}
}
/* conduct the reading process */
errorFlag =
(*current)->pixSeq->read(inStream, ixfer, glenc, maxReadLength);
/* if the errorFlag equals EC_Normal, all pixel data has been */
/* read; hence, the transfer state has to be set to ERW_ready */
if (errorFlag == EC_Normal)
setTransferState(ERW_ready);
}
else
{
/* the pixel data is captured in native (uncompressed) format */
/* if the transfer state is ERW_init, we need to prepare */
/* the reading of the pixel data from the stream. */
if (getTransferState() == ERW_init)
{
current = original = repListEnd;
unencapsulatedVR = getTag().getEVR();
recalcVR();
existUnencapsulated = OFTrue;
if (ixferSyn.usesEncapsulatedFormat())
{
/* Special case: we have encountered native format for the pixel
* data (explicit element length) although we're decoding an
* encapsulated transfer syntax. This is probably an icon image.
*/
alwaysUnencapsulated = OFTrue;
}
}
/* conduct the reading process */
errorFlag =
DcmPolymorphOBOW::read(inStream, ixfer, glenc, maxReadLength);
}
}
/* return result value */
return errorFlag;
}
void
DcmPixelData::removeAllButCurrentRepresentations()
{
clearRepresentationList(current);
if (current != repListEnd && existUnencapsulated)
{
DcmPolymorphOBOW::putUint16Array(NULL,0);
existUnencapsulated = OFFalse;
}
original = current;
}
void
DcmPixelData::removeAllButOriginalRepresentations()
{
clearRepresentationList(original);
if (original != repListEnd && existUnencapsulated)
{
DcmPolymorphOBOW::putUint16Array(NULL,0);
existUnencapsulated = OFFalse;
}
current = original;
recalcVR();
}
OFCondition
DcmPixelData::removeOriginalRepresentation(
const E_TransferSyntax repType,
const DcmRepresentationParameter * repParam)
{
OFCondition l_error = EC_Normal;
DcmXfer repTypeSyn(repType);
/* native format or referenced pixel data */
if (!repTypeSyn.usesEncapsulatedFormat())
{
if (original != repListEnd)
{
if (current == original)
{
current = repListEnd;
recalcVR();
}
repList.erase(original);
original = repListEnd;
}
else
l_error = EC_IllegalCall;
}
else
{
/* encapsulated format */
DcmRepresentationListIterator result;
DcmRepresentationEntry findEntry(repType, repParam, NULL);
if (findRepresentationEntry(findEntry, result) == EC_Normal)
{
if (result != original)
{
if (current == original)
{
current = result;
recalcVR();
}
if (original == repListEnd)
{
DcmPolymorphOBOW::putUint16Array(NULL, 0);
existUnencapsulated = OFFalse;
}
else
repList.erase(original);
original = result;
}
else
l_error = EC_IllegalCall;
}
else
l_error = EC_RepresentationNotFound;
}
return l_error;
}
OFCondition
DcmPixelData::removeRepresentation(
const E_TransferSyntax repType,
const DcmRepresentationParameter * repParam)
{
OFCondition l_error = EC_Normal;
DcmXfer repTypeSyn(repType);
/* native format or referenced pixel data */
if (!repTypeSyn.usesEncapsulatedFormat())
{
if (original != repListEnd && existUnencapsulated)
{
DcmPolymorphOBOW::putUint16Array(NULL, 0);
existUnencapsulated = OFFalse;
}
else
l_error = EC_CannotChangeRepresentation;
}
else
{
/* encapsulated format */
DcmRepresentationListIterator result;
DcmRepresentationEntry findEntry(repType, repParam, NULL);
if (findRepresentationEntry(findEntry, result) == EC_Normal)
{
if (original != result)
repList.erase(result);
else l_error = EC_CannotChangeRepresentation;
}
else
l_error = EC_RepresentationNotFound;
}
return l_error;
}
OFCondition
DcmPixelData::setCurrentRepresentationParameter(
const DcmRepresentationParameter * repParam)
{
if (current != repListEnd)
{
if (repParam == NULL)
(*current)->repParam = NULL;
else
(*current)->repParam = repParam->clone();
return EC_Normal;
}
return EC_RepresentationNotFound;
}
OFCondition
DcmPixelData::setVR(DcmEVR vr)
{
unencapsulatedVR = vr;
return DcmPolymorphOBOW::setVR(vr);
}
void
DcmPixelData::transferEnd()
{
DcmPolymorphOBOW::transferEnd();
for (DcmRepresentationListIterator it(repList.begin());
it != repListEnd;
++it)
(*it)->pixSeq->transferEnd();
}
void
DcmPixelData::transferInit()
{
DcmPolymorphOBOW::transferInit();
for (DcmRepresentationListIterator it(repList.begin());
it != repListEnd;
++it)
(*it)->pixSeq->transferInit();
}
OFCondition DcmPixelData::write(
DcmOutputStream &outStream,
const E_TransferSyntax oxfer,
const E_EncodingType enctype,
DcmWriteCache *wcache)
{
errorFlag = EC_Normal;
if (getTransferState() == ERW_notInitialized) errorFlag = EC_IllegalCall;
else
{
// check if the output transfer syntax is encapsulated and
// we are not requested to write an uncompressed dataset,
// for example because this is within an Icon Image Sequence
DcmXfer xferSyn(oxfer);
if (xferSyn.usesEncapsulatedFormat() && (! writeUnencapsulated(oxfer)))
{
// write encapsulated representation (i.e., compressed image)
if (getTransferState() == ERW_init)
{
DcmRepresentationListIterator found;
// find a compressed image matching the output transfer syntax
errorFlag = findConformingEncapsulatedRepresentation(xferSyn, NULL, found);
if (errorFlag == EC_Normal)
{
current = found;
recalcVR();
pixelSeqForWrite = (*found)->pixSeq;
setTransferState(ERW_inWork);
}
}
// write compressed image
if (errorFlag == EC_Normal && pixelSeqForWrite) errorFlag = pixelSeqForWrite->write(outStream, oxfer, enctype, wcache);
if (errorFlag == EC_Normal) setTransferState(ERW_ready);
}
else if (existUnencapsulated)
{
// we're supposed to write an uncompressed image, and we happen to have one available.
current = repListEnd;
recalcVR();
// write uncompressed image
errorFlag = DcmPolymorphOBOW::write(outStream, oxfer, enctype, wcache);
}
else if ((getValue() == NULL) && (current == repListEnd))
{
// the PixelData is empty. Write an empty element.
errorFlag = DcmPolymorphOBOW::write(outStream, oxfer, enctype, wcache);
} else errorFlag = EC_RepresentationNotFound;
}
return errorFlag;
}
OFCondition DcmPixelData::writeXML(
STD_NAMESPACE ostream &out,
const size_t flags)
{
if (current == repListEnd)
{
errorFlag = DcmPolymorphOBOW::writeXML(out, flags);
} else {
/* pixel sequence (encapsulated data) */
errorFlag = (*current)->pixSeq->writeXML(out, flags);
}
return errorFlag;
}
OFCondition DcmPixelData::writeSignatureFormat(
DcmOutputStream &outStream,
const E_TransferSyntax oxfer,
const E_EncodingType enctype,
DcmWriteCache *wcache)
{
errorFlag = EC_Normal;
if (getTransferState() == ERW_notInitialized) errorFlag = EC_IllegalCall;
else if (getTag().isSignable())
{
DcmXfer xferSyn(oxfer);
if (xferSyn.usesEncapsulatedFormat() && (! writeUnencapsulated(oxfer)))
{
if (getTransferState() == ERW_init)
{
DcmRepresentationListIterator found;
errorFlag = findConformingEncapsulatedRepresentation(xferSyn, NULL, found);
if (errorFlag == EC_Normal)
{
current = found;
recalcVR();
pixelSeqForWrite = (*found)->pixSeq;
setTransferState(ERW_inWork);
}
}
if (errorFlag == EC_Normal && pixelSeqForWrite) errorFlag = pixelSeqForWrite->writeSignatureFormat(outStream, oxfer, enctype, wcache);
if (errorFlag == EC_Normal) setTransferState(ERW_ready);
}
else if (existUnencapsulated)
{
current = repListEnd;
recalcVR();
errorFlag = DcmPolymorphOBOW::writeSignatureFormat(outStream, oxfer, enctype, wcache);
}
else if (getValue() == NULL)
{
errorFlag = DcmPolymorphOBOW::writeSignatureFormat(outStream, oxfer, enctype, wcache);
} else errorFlag = EC_RepresentationNotFound;
} else errorFlag = EC_Normal;
return errorFlag;
}
OFCondition DcmPixelData::loadAllDataIntoMemory(void)
{
if (current == repListEnd)
return DcmElement::loadAllDataIntoMemory();
else
return (*current)->pixSeq->loadAllDataIntoMemory();
}
void DcmPixelData::setNonEncapsulationFlag(OFBool flag)
{
alwaysUnencapsulated = flag;
}
OFCondition DcmPixelData::getUncompressedFrame(
DcmItem *dataset,
Uint32 frameNo,
Uint32& startFragment,
void *buffer,
Uint32 bufSize,
OFString& decompressedColorModel,
DcmFileCache *cache)
{
if ((dataset == NULL) || (buffer == NULL)) return EC_IllegalCall;
Sint32 numberOfFrames = 1;
dataset->findAndGetSint32(DCM_NumberOfFrames, numberOfFrames); // don't fail if absent
if (numberOfFrames < 1) numberOfFrames = 1;
Uint32 frameSize;
OFCondition result = getUncompressedFrameSize(dataset, frameSize, existUnencapsulated);
if (result.bad()) return result;
// determine the minimum buffer size, which may be frame size plus one pad byte if frame size is odd.
// We need this extra byte, because the image might be in a different
// endianness than our host cpu. In this case the decoder will swap
// the data to the host byte order which could overflow the buffer.
Uint32 minBufSize = frameSize;
if (minBufSize & 1) ++minBufSize;
if (bufSize < minBufSize) return EC_IllegalCall;
// check frame number
if (frameNo >= OFstatic_cast(Uint32, numberOfFrames)) return EC_IllegalCall;
if (existUnencapsulated)
{
// we already have an uncompressed version of the pixel data
// either in memory or in file. We can directly access this using
// DcmElement::getPartialValue.
result = getPartialValue(buffer, frameNo * frameSize, frameSize, cache);
if (result.good()) result = dataset->findAndGetOFString(DCM_PhotometricInterpretation, decompressedColorModel);
}
else
{
// we only have a compressed version of the pixel data.
// Identify a codec for decompressing the frame.
result = DcmCodecList::decodeFrame(
(*original)->repType, (*original)->repParam, (*original)->pixSeq,
dataset, frameNo, startFragment, buffer, bufSize, decompressedColorModel);
}
return result;
}
OFCondition DcmPixelData::getDecompressedColorModel(
DcmItem *dataset,
OFString &decompressedColorModel)
{
OFCondition result = EC_IllegalParameter;
if (dataset != NULL)
{
if (existUnencapsulated)
{
// we already have an uncompressed version of the pixel data either in memory or in file,
// so just retrieve the color model from the given dataset
result = dataset->findAndGetOFString(DCM_PhotometricInterpretation, decompressedColorModel);
if (result == EC_TagNotFound)
{
DCMDATA_WARN("DcmPixelData: Mandatory element PhotometricInterpretation " << DCM_PhotometricInterpretation << " is missing");
result = EC_MissingAttribute;
}
else if (result.bad())
{
DCMDATA_WARN("DcmPixelData: Cannot retrieve value of element PhotometricInterpretation " << DCM_PhotometricInterpretation << ": " << result.text());
}
else if (decompressedColorModel.empty())
{
DCMDATA_WARN("DcmPixelData: No value for mandatory element PhotometricInterpretation " << DCM_PhotometricInterpretation);
result = EC_MissingValue;
}
} else {
// we only have a compressed version of the pixel data.
// Identify a codec for determining the color model.
result = DcmCodecList::determineDecompressedColorModel(
(*original)->repType, (*original)->repParam, (*original)->pixSeq,
dataset, decompressedColorModel);
}
}
return result;
}
OFBool DcmPixelData::writeUnencapsulated(const E_TransferSyntax xfer)
{
// There are three cases under which a dataset is written out
// unencapsulated:
//
// - It was already read unencapsulated (handled via alwaysUnencapsulated)
// - We were told to do so (handled via alwaysUnencapsulated)
// - This is not the pixel data element on the main level and it exists
// unencapsulated.
if (alwaysUnencapsulated)
return OFTrue;
if (DcmXfer(xfer).usesEncapsulatedFormat()) {
DcmRepresentationListIterator found;
OFCondition cond = findConformingEncapsulatedRepresentation(xfer, NULL, found);
if (cond.good()) {
// We found a suitable encapsulated representation, so encapsulate
// this element in the output.
return OFFalse;
}
}
return existUnencapsulated && isNested();
}
OFCondition DcmPixelData::writeJson(STD_NAMESPACE ostream &out,
DcmJsonFormat &format)
{
// check if we have an empty uncompressed value field.
// We never encode that as BulkDataURI.
OFBool emptyValue = OFFalse;
if ((current == repListEnd) && existUnencapsulated && (getLengthField() == 0))
{
emptyValue = OFTrue;
}
// now check if the pixel data will be written as
// BulkDataURI, which is possible for both uncompressed
// and encapsulated pixel data.
OFString value;
if ((! emptyValue) && format.asBulkDataURI(getTag(), value))
{
/* write JSON Opener */
writeJsonOpener(out, format);
/* return defined BulkDataURI */
format.printBulkDataURIPrefix(out);
DcmJsonFormat::printString(out, value);
/* write JSON Closer */
writeJsonCloser(out, format);
return EC_Normal;
}
// No bulk data URI, we're supposed to write as InlineBinary.
// This is only defined for uncompressed data, not for any of the
// encapsulated encodings.
// check the current pixel data representation
if ((current == repListEnd) && existUnencapsulated)
{
// current pixel data representation is uncompressed (and available).
/* write JSON Opener */
writeJsonOpener(out, format);
/* for an empty value field, we do not need to do anything */
if (getLengthField() > 0)
{
/* encode binary data as Base64 */
format.printInlineBinaryPrefix(out);
out << "\"";
/* adjust byte order to little endian */
Uint8 *byteValues = OFstatic_cast(Uint8 *, getValue(EBO_LittleEndian));
OFStandard::encodeBase64(out, byteValues, OFstatic_cast(size_t, getLengthField()));
out << "\"";
}
/* write JSON Closer */
writeJsonCloser(out, format);
return EC_Normal;
}
/* write JSON Opener and Closer, because otherwise the output is not valid JSON */
writeJsonOpener(out, format);
writeJsonCloser(out, format);
// pixel data is encapsulated, return error
return EC_CannotWriteJsonInlineBinary;
}
|