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 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
|
/*
*
* 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: Implementation of class DcmElement
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/ofstd/ofdefine.h"
#include "dcmtk/ofstd/ofstd.h"
#include "dcmtk/ofstd/ofstream.h"
#include "dcmtk/dcmdata/dcjson.h"
#include "dcmtk/dcmdata/dcelem.h"
#include "dcmtk/dcmdata/dcobject.h"
#include "dcmtk/dcmdata/dcswap.h"
#include "dcmtk/dcmdata/dcistrma.h" /* for class DcmInputStream */
#include "dcmtk/dcmdata/dcostrma.h" /* for class DcmOutputStream */
#include "dcmtk/dcmdata/dcfcache.h" /* for class DcmFileCache */
#include "dcmtk/dcmdata/dcwcache.h" /* for class DcmWriteCache */
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/vrscan.h"
#include "dcmtk/dcmdata/dcpath.h"
#include <cstring> /* for memset() */
#define SWAPBUFFER_SIZE 16 /* sufficient for all DICOM VRs as per the 2007 edition */
//
// CLASS DcmElement
//
DcmElement::DcmElement(const DcmTag &tag,
const Uint32 len)
: DcmObject(tag, len),
fByteOrder(gLocalByteOrder),
fLoadValue(NULL),
fValue(NULL)
{
}
DcmElement::DcmElement(const DcmElement &elem)
: DcmObject(elem),
fByteOrder(elem.fByteOrder),
fLoadValue(NULL),
fValue(NULL)
{
if (elem.fValue)
{
DcmVR vr(elem.getVR());
const unsigned short pad = (vr.isaString()) ? OFstatic_cast(unsigned short, 1) : OFstatic_cast(unsigned short, 0);
// The next lines are a special version of newValueField().
// newValueField() cannot be used because it is virtual and it does
// not allocate enough bytes for strings. The number of pad bytes
// is added to the Length for this purpose.
if (getLengthField() & 1)
{
#ifdef HAVE_STD__NOTHROW
// we want to use a non-throwing new here if available
// If the allocation fails, we report an EC_MemoryExhausted error
// back to the caller.
fValue = new (std::nothrow) Uint8[getLengthField() + 1 + pad]; // protocol error: odd value length
#else
/* make sure that the pointer is set to NULL in case of error */
try
{
fValue = new Uint8[getLengthField() + 1 + pad]; // protocol error: odd value length
}
catch (STD_NAMESPACE bad_alloc const &)
{
fValue = NULL;
}
#endif
if (fValue)
fValue[getLengthField()] = 0;
setLengthField(getLengthField() + 1); // make Length even
}
else
{
#ifdef HAVE_STD__NOTHROW
// we want to use a non-throwing new here if available.
// If the allocation fails, we report an EC_MemoryExhausted error
// back to the caller.
fValue = new (std::nothrow) Uint8[getLengthField() + pad];
#else
/* make sure that the pointer is set to NULL in case of error */
try
{
fValue = new Uint8[getLengthField() + pad];
}
catch (STD_NAMESPACE bad_alloc const &)
{
fValue = NULL;
}
#endif
}
if (!fValue)
errorFlag = EC_MemoryExhausted;
if (pad && fValue)
fValue[getLengthField()] = 0;
if (fValue)
memcpy(fValue, elem.fValue, size_t(getLengthField() + pad));
}
if (elem.fLoadValue)
fLoadValue = elem.fLoadValue->clone();
}
DcmElement &DcmElement::operator=(const DcmElement &obj)
{
if (this != &obj)
{
#if defined(HAVE_STD__NOTHROW) && defined(HAVE_NOTHROW_DELETE)
// if created with the nothrow version it must also be deleted with
// the nothrow version else memory error.
operator delete[] (fValue, std::nothrow);
#else
delete[] fValue;
#endif
delete fLoadValue;
fLoadValue = NULL;
fValue = NULL;
DcmObject::operator=(obj);
fByteOrder = obj.fByteOrder;
if (obj.fValue)
{
DcmVR vr(obj.getVR());
const unsigned short pad = (vr.isaString()) ? OFstatic_cast(unsigned short, 1) : OFstatic_cast(unsigned short, 0);
// The next lines are a special version of newValueField().
// newValueField() cannot be used because it is virtual and it does
// not allocate enough bytes for strings. The number of pad bytes
// is added to the Length for this purpose.
if (getLengthField() & 1)
{
#ifdef HAVE_STD__NOTHROW
// we want to use a non-throwing new here if available.
// If the allocation fails, we report an EC_MemoryExhausted error
// back to the caller.
fValue = new (std::nothrow) Uint8[getLengthField() + 1 + pad]; // protocol error: odd value length
#else
/* make sure that the pointer is set to NULL in case of error */
try
{
fValue = new Uint8[getLengthField() + 1 + pad]; // protocol error: odd value length
}
catch (STD_NAMESPACE bad_alloc const &)
{
fValue = NULL;
}
#endif
if (fValue)
fValue[getLengthField()] = 0;
setLengthField(getLengthField() + 1); // make Length even
}
else
{
#ifdef HAVE_STD__NOTHROW
// we want to use a non-throwing new here if available.
// If the allocation fails, we report an EC_MemoryExhausted error
// back to the caller.
fValue = new (std::nothrow) Uint8[getLengthField() + pad];
#else
/* make sure that the pointer is set to NULL in case of error */
try
{
fValue = new Uint8[getLengthField() + pad];
}
catch (STD_NAMESPACE bad_alloc const &)
{
fValue = NULL;
}
#endif
}
if (!fValue)
errorFlag = EC_MemoryExhausted;
if (pad && fValue)
fValue[getLengthField()] = 0;
if (fValue)
memcpy(fValue, obj.fValue, size_t(getLengthField()+pad));
}
if (obj.fLoadValue)
fLoadValue = obj.fLoadValue->clone();
}
return *this;
}
int DcmElement::compare(const DcmElement& rhs) const
{
if (this == &rhs)
return 0;
DcmElement* myThis = OFconst_cast(DcmElement*, this);
DcmElement* myRhs = OFconst_cast(DcmElement*, &rhs);
DcmTagKey thisKey = (*myThis).getTag();
DcmTagKey rhsKey = (*myRhs).getTag();
if ( thisKey > rhsKey )
{
return 1;
}
else if (thisKey < rhsKey)
{
return -1;
}
if ( this->ident() != rhs.ident() )
return -1;
return 0;
}
OFCondition DcmElement::copyFrom(const DcmObject& rhs)
{
if (this != &rhs)
{
if (rhs.ident() != ident()) return EC_IllegalCall;
*this = OFstatic_cast(const DcmElement &, rhs);
}
return EC_Normal;
}
DcmElement::~DcmElement()
{
#if defined(HAVE_STD__NOTHROW) && defined(HAVE_NOTHROW_DELETE)
// if created with the nothrow version it must also be deleted with
// the nothrow version else memory error.
operator delete[] (fValue, std::nothrow);
#else
delete[] fValue;
#endif
delete fLoadValue;
}
// ********************************
OFCondition DcmElement::clear()
{
errorFlag = EC_Normal;
#if defined(HAVE_STD__NOTHROW) && defined(HAVE_NOTHROW_DELETE)
// if created with the nothrow version it must also be deleted with
// the nothrow version else memory error.
operator delete[] (fValue, std::nothrow);
#else
delete[] fValue;
#endif
fValue = NULL;
delete fLoadValue;
fLoadValue = NULL;
setLengthField(0);
return errorFlag;
}
OFCondition DcmElement::checkValue(const OFString & /*vm*/,
const OFBool /*oldFormat*/)
{
return EC_IllegalCall;
}
// ********************************
Uint32 DcmElement::calcElementLength(const E_TransferSyntax xfer,
const E_EncodingType enctype)
{
DcmXfer xferSyn(xfer);
DcmEVR vr = getVR();
/* These VRs don't use extended length encoding, but when writing, they are
* converted to EVR_UN, which DOES use extended length encoding.
* (EVR_na should never happen here, it's just handled for completeness)
*/
if (vr == EVR_UNKNOWN2B || vr == EVR_na)
vr = EVR_UN;
/* compute length of element value */
const Uint32 elemLength = getLength(xfer, enctype);
/* Create an object that represents this object's "valid" data type */
DcmVR myvalidvr(vr);
if ((elemLength) > 0xffff && (! myvalidvr.usesExtendedLengthEncoding()) && xferSyn.isExplicitVR())
{
/* special case: we are writing in explicit VR, the VR of this
* element uses a 2-byte length encoding, but the element length is
* too large for a 2-byte length field. We need to write this element
* as VR=UN (or VR=OB) and adjust the length calculation accordingly.
* Since UN and OB always have the same header length, we can simply
* assume that we are using UN.
*/
vr = EVR_UN;
}
/* now compute length of header */
const Uint32 headerLength = xferSyn.sizeofTagHeader(vr);
if (OFStandard::check32BitAddOverflow(headerLength, elemLength))
return DCM_UndefinedLength;
else
return headerLength + elemLength;
}
OFBool DcmElement::canWriteXfer(const E_TransferSyntax newXfer,
const E_TransferSyntax /*oldXfer*/)
{
return (newXfer != EXS_Unknown);
}
OFCondition DcmElement::detachValueField(OFBool copy)
{
OFCondition l_error = EC_Normal;
if (getLengthField() != 0)
{
if (copy)
{
if (!fValue)
l_error = loadValue();
if (l_error.good())
{
Uint8 * newValue;
#ifdef HAVE_STD__NOTHROW
// we want to use a non-throwing new here if available
newValue = new (std::nothrow) Uint8[getLengthField()];
#else
/* make sure that the pointer is set to NULL in case of error */
try
{
newValue = new Uint8[getLengthField()];
}
catch (STD_NAMESPACE bad_alloc const &)
{
newValue = NULL;
}
#endif
if (newValue)
{
memcpy(newValue, fValue, size_t(getLengthField()));
fValue = newValue;
} else {
/* the copy could not be created, so return an error */
l_error = EC_MemoryExhausted;
}
}
} else {
fValue = NULL;
setLengthField(0);
}
}
return l_error;
}
// ********************************
OFCondition DcmElement::getUint8(Uint8 & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getSint16(Sint16 & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getUint16(Uint16 & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getSint32(Sint32 & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getUint32(Uint32 & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getFloat32(Float32 & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getSint64(Sint64 & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getUint64(Uint64 & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getFloat64(Float64 & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getTagVal(DcmTagKey & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getOFString(OFString & /*val*/,
const unsigned long /*pos*/,
OFBool /*normalize*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getString(char * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getString(char * & /*val*/,
Uint32 & /*len*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getOFStringArray(OFString &value,
OFBool normalize)
{
/* this is a general implementation which is only used when the derived
VR class does not reimplement it
*/
errorFlag = EC_Normal;
const unsigned long count = getVM();
/* initialize result string */
value.clear();
if (count > 0)
{
OFString string;
unsigned long i = 0;
/* reserve number of bytes expected (heuristic) */
value.reserve(OFstatic_cast(unsigned int, getLength()));
/* iterate over all values and convert them to a character string */
while ((i < count) && (errorFlag = getOFString(string, i, normalize)).good())
{
if (i > 0)
value += '\\';
/* append current value to the result string */
value += string;
i++;
}
}
return errorFlag;
}
OFCondition DcmElement::getUint8Array(Uint8 * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getSint16Array(Sint16 * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getUint16Array(Uint16 * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getSint32Array(Sint32 * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getUint32Array(Uint32 * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getFloat32Array(Float32 * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getSint64Array(Sint64 * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getUint64Array(Uint64 * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::getFloat64Array(Float64 * & /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
void *DcmElement::getValue(const E_ByteOrder newByteOrder)
{
/* initialize return value */
Uint8 * value = NULL;
/* if the byte ordering is unknown, this is an illegal call */
if (newByteOrder == EBO_unknown)
errorFlag = EC_IllegalCall;
else
{
/* in case this call is not illegal, we need to do something. First of all, set the error flag to ok */
errorFlag = EC_Normal;
/* do something only if the length of this element's value does not equal (i.e. is greater than) 0 */
if (getLengthField() != 0)
{
/* if the value has not yet been loaded, do so now */
if (!fValue)
errorFlag = loadValue();
/* if everything is ok */
if (errorFlag.good())
{
/* if this element's value's byte ordering does not correspond to the */
/* desired byte ordering, we need to rearrange this value's bytes and */
/* set its byte order indicator variable correspondingly */
if (newByteOrder != fByteOrder)
{
swapIfNecessary(newByteOrder, fByteOrder, fValue,
getLengthField(), getTag().getVR().getValueWidth());
fByteOrder = newByteOrder;
}
/* if everything is ok, assign the current value to the result variable */
if (errorFlag.good())
value = fValue;
}
}
}
/* return result */
return value;
}
// ********************************
OFCondition DcmElement::loadAllDataIntoMemory()
{
errorFlag = EC_Normal;
if (!fValue && (getLengthField() != 0))
errorFlag = loadValue();
return errorFlag;
}
OFCondition DcmElement::loadValue(DcmInputStream *inStream)
{
/* initialize return value */
errorFlag = EC_Normal;
/* only if the length of this element does not equal 0, read information */
if (getLengthField() != 0)
{
DcmInputStream *readStream = inStream;
OFBool isStreamNew = OFFalse;
// if the NULL pointer was passed (i.e. we're not in the middle of
// a read() cycle, and fValue is NULL (i.e. the attribute value still
// remains in file and fLoadValue is not NULL (i.e. we know how to
// load the value from that file, then let's do it..
if (!readStream && fLoadValue && !fValue)
{
/* we need to read information from the stream which is */
/* accessible through fLoadValue. Hence, reassign readStream */
readStream = fLoadValue->create();
isStreamNew = OFTrue;
/* reset number of transferred bytes to zero */
setTransferredBytes(0);
}
/* if we have a stream from which we can read */
if (readStream)
{
/* check if the stream reported an error */
errorFlag = readStream->status();
/* if we encountered the end of the stream, set the error flag correspondingly */
if (errorFlag.good() && readStream->eos())
errorFlag = EC_EndOfStream;
/* if we did not encounter the end of the stream and no error occurred so far, go ahead */
else if (errorFlag.good())
{
/* if the object which holds this element's value does not yet exist, create it */
if (!fValue)
fValue = newValueField(); /* also set errorFlag in case of error */
/* if object could be created (i.e. we have an object which can be used to capture this element's */
/* value) we need to read a certain amount of bytes from the stream */
if (fValue)
{
/* determine how many bytes shall be read from the stream */
const Uint32 readLength = getLengthField() - getTransferredBytes();
/* read a corresponding amount of bytes from the stream, store the information in fValue */
/* increase the counter that counts how many bytes were actually read */
incTransferredBytes(OFstatic_cast(Uint32, readStream->read(&fValue[getTransferredBytes()], readLength)));
/* if we have read all the bytes which make up this element's value */
if (getLengthField() == getTransferredBytes())
{
/* call a function which performs certain operations on the information which was read */
postLoadValue();
errorFlag = EC_Normal;
}
/* else set the return value correspondingly */
else if (readStream->eos())
{
errorFlag = EC_InvalidStream; // premature end of stream
DCMDATA_ERROR("DcmElement: " << getTagName() << " " << getTag()
<< " larger (" << getLengthField() << ") than remaining bytes ("
<< getTransferredBytes() << ") in file, premature end of stream");
}
else
errorFlag = EC_StreamNotifyClient;
}
}
/* if we created the stream from which information was read in this */
/* function, we need to we need to delete this object here as well */
if (isStreamNew)
delete readStream;
}
else
{
errorFlag = EC_InvalidStream; // incomplete dataset read from stream
DCMDATA_ERROR("DcmElement: " << getTagName() << " " << getTag()
<< " larger (" << getLengthField() << ") than remaining bytes ("
<< getTransferredBytes() << ") in file, premature end of stream");
}
}
/* return result value */
return errorFlag;
}
// ********************************
Uint8 *DcmElement::newValueField()
{
Uint8 * value;
/* if this element's length is odd */
Uint32 lengthField = getLengthField();
if (lengthField & 1)
{
if (lengthField == DCM_UndefinedLength)
{
/* Print an error message when private attribute states to have an odd length
* equal to the maximum length, because we are not able then to make this value even (+1)
* which would an overflow on some systems as well as being illegal in DICOM
*/
DCMDATA_ERROR("DcmElement: " << getTagName() << " " << getTag()
<< " has odd maximum length (" << DCM_UndefinedLength << ") and therefore is not loaded");
errorFlag = EC_CorruptedData;
return NULL;
}
/* create an array of Length+1 bytes */
#ifdef HAVE_STD__NOTHROW
// we want to use a non-throwing new here if available.
// If the allocation fails, we report an EC_MemoryExhausted error
// back to the caller.
value = new (std::nothrow) Uint8[lengthField + 1]; // protocol error: odd value length
#else
/* make sure that the pointer is set to NULL in case of error */
try
{
value = new Uint8[lengthField + 1]; // protocol error: odd value length
}
catch (STD_NAMESPACE bad_alloc const &)
{
value = NULL;
}
#endif
/* if creation was successful, set last byte to 0 (in order to initialize this byte) */
/* (no value will be assigned to this byte later, since Length was odd) */
if (value)
value[lengthField] = 0;
/* enforce old (pre DCMTK 3.5.2) behaviour ? */
if (!dcmAcceptOddAttributeLength.get())
{
lengthField++;
setLengthField(lengthField); // make Length even
}
}
/* if this element's length is even, create a corresponding array of Length bytes */
else
#ifdef HAVE_STD__NOTHROW
// we want to use a non-throwing new here if available.
// If the allocation fails, we report an EC_MemoryExhausted error
// back to the caller.
value = new (std::nothrow) Uint8[lengthField];
#else
/* make sure that the pointer is set to NULL in case of error */
try
{
value = new Uint8[lengthField];
}
catch (STD_NAMESPACE bad_alloc const &)
{
value = NULL;
}
#endif
/* if creation was not successful set member error flag correspondingly */
if (!value)
errorFlag = EC_MemoryExhausted;
/* return byte array */
return value;
}
// ********************************
void DcmElement::postLoadValue()
{
if (dcmEnableAutomaticInputDataCorrection.get())
{
// newValueField always allocates an even number of bytes
// and sets the pad byte to zero, so we can safely increase Length here
if (getLengthField() & 1)
setLengthField(getLengthField() + 1); // make Length even
}
}
// ********************************
OFCondition DcmElement::changeValue(const void *value,
const Uint32 position,
const Uint32 num)
{
errorFlag = EC_Normal;
// check for invalid parameter values
if (position % num != 0 || getLengthField() % num != 0 || position > getLengthField())
errorFlag = EC_IllegalCall;
else if (position == getLengthField())
{
// append value
if (getLengthField() == 0)
{
errorFlag = putValue(value, num);
} else {
// load value (if not loaded yet)
if (!fValue)
errorFlag = loadValue();
if (errorFlag.good())
{
Uint8 * newValue;
// allocate new memory for value
#ifdef HAVE_STD__NOTHROW
// we want to use a non-throwing new here if available.
// If the allocation fails, we report an EC_MemoryExhausted error
// back to the caller.
newValue = new (std::nothrow) Uint8[getLengthField() + num];
#else
/* make sure that the pointer is set to NULL in case of error */
try
{
newValue = new Uint8[getLengthField() + num];
}
catch (STD_NAMESPACE bad_alloc const &)
{
newValue = NULL;
}
#endif
if (!newValue)
errorFlag = EC_MemoryExhausted;
if (errorFlag.good())
{
// swap to local byte order
swapIfNecessary(gLocalByteOrder, fByteOrder, fValue,
getLengthField(), getTag().getVR().getValueWidth());
fByteOrder = gLocalByteOrder;
// copy old value in the beginning of new value
memcpy(newValue, fValue, size_t(getLengthField()));
// copy value passed as a parameter to the end
memcpy(&newValue[getLengthField()], OFstatic_cast(const Uint8 *, value), size_t(num));
#if defined(HAVE_STD__NOTHROW) && defined(HAVE_NOTHROW_DELETE)
// if created with the nothrow version it must also be deleted with
// the nothrow version else memory error.
operator delete[] (fValue, std::nothrow);
#else
delete[] fValue;
#endif
fValue = newValue;
setLengthField(getLengthField() + num);
} else
errorFlag = EC_MemoryExhausted;
}
}
} else {
// load value (if not loaded yet)
if (!fValue)
errorFlag = loadValue();
if (errorFlag.good())
{
// swap to local byte order
swapIfNecessary(gLocalByteOrder, fByteOrder, fValue,
getLengthField(), getTag().getVR().getValueWidth());
// copy value at given position
memcpy(&fValue[position], OFstatic_cast(const Uint8 *, value), size_t(num));
fByteOrder = gLocalByteOrder;
}
}
return errorFlag;
}
// ********************************
OFCondition DcmElement::putOFStringArray(const OFString &val)
{
/* sets the value of a complete (possibly multi-valued) string attribute */
return putString(val.c_str(), OFstatic_cast(Uint32, val.length()));
}
OFCondition DcmElement::putString(const char * /*val*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putString(const char * /*val*/,
const Uint32 /*len*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putSint16(const Sint16 /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putUint16(const Uint16 /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putSint32(const Sint32 /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putUint32(const Uint32 /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putFloat32(const Float32 /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putFloat64(const Float64 /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putTagVal(const DcmTagKey & /*val*/,
const unsigned long /*pos*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putUint8Array(const Uint8 * /*val*/,
const unsigned long /*num*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putSint16Array(const Sint16 * /*val*/,
const unsigned long /*num*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putUint16Array(const Uint16 * /*val*/,
const unsigned long /*num*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putSint32Array(const Sint32 * /*val*/,
const unsigned long /*num*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putUint32Array(const Uint32 * /*val*/,
const unsigned long /*num*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putFloat32Array(const Float32 * /*val*/,
const unsigned long /*num*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putFloat64Array(const Float64 * /*val*/,
const unsigned long /*num*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::putValue(const void * newValue,
const Uint32 length)
{
errorFlag = EC_Normal;
if (fValue)
{
#if defined(HAVE_STD__NOTHROW) && defined(HAVE_NOTHROW_DELETE)
// if created with the nothrow version it must also be deleted with
// the nothrow version else memory error.
operator delete[] (fValue, std::nothrow);
#else
delete[] fValue;
#endif
}
fValue = NULL;
if (fLoadValue)
delete fLoadValue;
fLoadValue = NULL;
setLengthField(length);
if (length != 0)
{
fValue = newValueField();
// newValueField always allocates an even number of bytes
// and sets the pad byte to zero, so we can safely increase Length here
if (getLengthField() & 1)
setLengthField(getLengthField() + 1); // make Length even
// copy length (which may be odd), not Length (which is always even)
if (fValue)
memcpy(fValue, newValue, size_t(length));
else
errorFlag = EC_MemoryExhausted;
}
fByteOrder = gLocalByteOrder;
return errorFlag;
}
// ********************************
OFCondition DcmElement::createUint8Array(const Uint32 /*numBytes*/,
Uint8 *& /*bytes*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
OFCondition DcmElement::createUint16Array(const Uint32 /*numWords*/,
Uint16 *& /*bytes*/)
{
errorFlag = EC_IllegalCall;
return errorFlag;
}
// ********************************
OFCondition DcmElement::createEmptyValue(const Uint32 length)
{
errorFlag = EC_Normal;
if (fValue)
{
#if defined(HAVE_STD__NOTHROW) && defined(HAVE_NOTHROW_DELETE)
// if created with the nothrow version it must also be deleted with
// the nothrow version else memory error.
operator delete[] (fValue, std::nothrow);
#else
delete[] fValue;
#endif
}
fValue = NULL;
if (fLoadValue)
delete fLoadValue;
fLoadValue = NULL;
setLengthField(length);
if (length != 0)
{
fValue = newValueField();
// newValueField always allocates an even number of bytes
// and sets the pad byte to zero, so we can safely increase Length here
if (getLengthField() & 1)
setLengthField(getLengthField() + 1); // make Length even
// initialize <length> bytes (which may be odd), not Length (which is always even)
if (fValue)
memset(fValue, 0, size_t(length));
else
errorFlag = EC_MemoryExhausted;
}
fByteOrder = gLocalByteOrder;
return errorFlag;
}
// ********************************
OFCondition DcmElement::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 and create a DcmXfer */
/* object based on the transfer syntax which was passed */
DcmXfer inXfer(ixfer);
/* determine the transfer syntax's byte ordering */
if (getTag() == DCM_PixelData)
fByteOrder = inXfer.getPixelDataByteOrder();
else fByteOrder = inXfer.getByteOrder();
/* check if the stream reported an error */
errorFlag = inStream.status();
/* if we encountered the end of the stream, set the error flag correspondingly */
if (errorFlag.good() && inStream.eos())
{
errorFlag = EC_EndOfStream; // not handled as parsing error by caller
/* Network can never report EOS at this point, so we are in a file
* and no bytes are left for reading. This is only valid if
* the announced length of the current value is 0 and we are in the
* last element of the dataset (e.g. Pixel Data) otherwise it must be an error */
if (getLengthField() > 0)
{
/* Return error code if we are are not ignoring parsing errors */
if (!dcmIgnoreParsingErrors.get())
errorFlag = EC_StreamNotifyClient; // should we rather return EC_InvalidStream?
/* In any case, make sure that calling the load value routine on this
* element will fail later. For that, create the stream factory that
* the load routine will use. Otherwise it would not realize
* that there is a problem */
delete fLoadValue;
fLoadValue = inStream.newFactory();
/* Print an error message when too few bytes are available in the file in order to
* distinguish this problem from any other generic "InvalidStream" problem. */
DCMDATA_ERROR("DcmElement: " << getTagName() << " " << getTag()
<< " larger (" << getLengthField() << ") than remaining bytes in file");
}
}
/* if we did not encounter the end of the stream and no error occurred so far, go ahead */
else if (errorFlag.good())
{
/* if the transfer state is ERW_init, we need to prepare */
/* the reading of this element's value from the stream */
if (getTransferState() == ERW_init)
{
/* if the Length of this element's value is greater than the amount of bytes we */
/* can read from the stream and if the stream has random access, we want to create */
/* a DcmInputStreamFactory object that enables us to read this element's value later. */
/* This new object will be stored (together with the position where we have to start */
/* reading the value) in the member variable fLoadValue. */
if (getLengthField() > maxReadLength)
{
/* try to create a stream factory to read the value later */
delete fLoadValue;
fLoadValue = inStream.newFactory();
if (fLoadValue)
{
offile_off_t skipped = inStream.skip(getLengthField());
if (skipped < OFstatic_cast(offile_off_t, getLengthField()))
{
/* If desired, specific parser errors will be ignored */
if (dcmIgnoreParsingErrors.get())
errorFlag = EC_Normal;
else
errorFlag = EC_StreamNotifyClient;
/* Print an error message when too few bytes are available in the file in order to
* distinguish this problem from any other generic "InvalidStream" problem. */
DCMDATA_ERROR("DcmElement: " << getTagName() << " " << getTag()
<< " larger (" << getLengthField() << ") than remaining bytes in file");
}
}
}
/* if there is already a value for this element, delete this value */
#if defined(HAVE_STD__NOTHROW) && defined(HAVE_NOTHROW_DELETE)
// if created with the nothrow version it must also be deleted with
// the nothrow version else memory error.
operator delete[] (fValue, std::nothrow);
#else
delete[] fValue;
#endif
/* set the transfer state to ERW_inWork */
setTransferState(ERW_inWork);
}
/* if the transfer state is ERW_inWork and we are not supposed */
/* to read this element's value later, read the value now */
if (getTransferState() == ERW_inWork && !fLoadValue)
errorFlag = loadValue(&inStream);
/* if the amount of transferred bytes equals the Length of this element */
/* or the object which contains information to read the value of this */
/* element later is existent, set the transfer state to ERW_ready */
if (getTransferredBytes() == getLengthField() || fLoadValue)
setTransferState(ERW_ready);
}
}
/* return result value */
return errorFlag;
}
// ********************************
void DcmElement::swapValueField(size_t valueWidth)
{
if (getLengthField() != 0)
{
if (!fValue)
errorFlag = loadValue();
if (errorFlag.good())
swapBytes(fValue, getLengthField(), valueWidth);
}
}
// ********************************
void DcmElement::transferInit()
{
DcmObject::transferInit();
setTransferredBytes(0);
}
// ********************************
OFCondition DcmElement::write(DcmOutputStream &outStream,
const E_TransferSyntax oxfer,
const E_EncodingType enctype,
DcmWriteCache *wcache)
{
DcmWriteCache wcache2;
/* Create an object that represents this object's data type */
DcmVR myvr(getVR());
/* create an object that represents the transfer syntax */
DcmXfer outXfer(oxfer);
/* getValidEVR() will convert post 1993 VRs to "OB" if these are disabled */
DcmEVR vr = myvr.getValidEVR();
/* compute length of element value */
const Uint32 elemLength = getLength(oxfer, enctype);
/* Create an object that represents this object's "valid" data type */
DcmVR myvalidvr(vr);
if ((elemLength) > 0xffff && (! myvalidvr.usesExtendedLengthEncoding()) && outXfer.isExplicitVR())
{
/* special case: we are writing in explicit VR, the VR of this
* element uses a 2-byte length encoding, but the element length is
* too large for a 2-byte length field. We need to write this element
* as VR=UN (or VR=OB if the generation of UN is disabled).
* In this method, the variable "vr" is only used to determine the
* output byte order, which is always the same for OB and UN.
* Therefore, we do not need to distinguish between these two.
*/
vr = EVR_UN;
}
/* In case the transfer state is not initialized, this is an illegal call */
if (getTransferState() == ERW_notInitialized)
errorFlag = EC_IllegalCall;
else
{
/* if this is not an illegal call, we need to do something. First */
/* of all, check the error state of the stream that was passed */
/* only do something if the error state of the stream is ok */
errorFlag = outStream.status();
if (errorFlag.good())
{
E_ByteOrder outByteOrder;
/* determine the transfer syntax's byte ordering for this element */
if (getTag() == DCM_PixelData)
outByteOrder = outXfer.getPixelDataByteOrder();
else outByteOrder = outXfer.getByteOrder();
/* UN and OB element content always needs to be written in little endian.
We need to set this manually for the case that we are converting
elements to UN or OB while writing because some post-1993 VRs are disabled */
if ((vr == EVR_OB) || (vr == EVR_UN)) outByteOrder = EBO_LittleEndian;
/* pointer to element value if value resides in memory or old-style
* write behaviour is active (i.e. everything loaded into memory upon write)
*/
Uint8 *value = NULL;
OFBool accessPossible = OFFalse;
/* check that we actually do have access to the element's value.
* If the element is unaccessible (which would mean that the value resides
* in file and access to the file fails), write an empty element with
* zero length.
*/
if (getLengthField() > 0)
{
if (valueLoaded())
{
/* get this element's value. Mind the byte ordering (little */
/* or big endian) of the transfer syntax which shall be used */
value = OFstatic_cast(Uint8 *, getValue(outByteOrder));
if (value) accessPossible = OFTrue;
}
else
{
/* Use local cache object if needed. This may cause those bytes
* that are read but not written because the output stream stalls to
* be read again, and the file from being re-opened next time.
* Therefore, this case should be avoided.
*/
if (!wcache) wcache = &wcache2;
/* initialize cache object. This is safe even if the object was already initialized */
wcache->init(this, getLengthField(), getTransferredBytes(), outByteOrder);
/* access first block of element content */
errorFlag = wcache->fillBuffer(*this);
/* check that everything worked and the buffer is non-empty now */
accessPossible = errorFlag.good() && (! wcache->bufferIsEmpty());
}
}
/* if this element's transfer state is ERW_init (i.e. it has not yet been written to */
/* the stream) and if the outstream provides enough space for tag and length information */
/* write tag and length information to it, do something */
if (getTransferState() == ERW_init)
{
// Force a compression filter (if any) to process the input buffer, by calling outStream.write().
// This ensures that we cannot get stuck if there are just a few bytes available in the buffer
outStream.write(NULL, 0);
/* first compare with DCM_TagInfoLength (12). If there is not enough space
* in the buffer, check if the buffer is still sufficient for the requirements
* of this element, which may need only 8 instead of 12 bytes.
*/
if ((outStream.avail() >= OFstatic_cast(offile_off_t, DCM_TagInfoLength)) ||
(outStream.avail() >= OFstatic_cast(offile_off_t, getTagAndLengthSize(oxfer))))
{
/* if there is no value, Length (member variable) shall be set to 0 */
if (! accessPossible) setLengthField(0);
/* remember how many bytes have been written to the stream, currently none so far */
Uint32 writtenBytes = 0;
/* write tag and length information (and possibly also data type information) to the stream, */
/* mind the transfer syntax and remember the amount of bytes that have been written */
errorFlag = writeTagAndLength(outStream, oxfer, writtenBytes);
/* if the writing was successful, set this element's transfer */
/* state to ERW_inWork and the amount of transferred bytes to 0 */
if (errorFlag.good())
{
setTransferState(ERW_inWork);
setTransferredBytes(0);
}
} else errorFlag = EC_StreamNotifyClient;
}
/* if there is a value that has to be written to the stream */
/* and if this element's transfer state is ERW_inWork */
if (accessPossible && getTransferState() == ERW_inWork)
{
Uint32 len = 0;
if (valueLoaded())
{
/* write as many bytes as possible to the stream starting at value[getTransferredBytes()] */
/* (note that the bytes value[0] to value[getTransferredBytes()-1] have already been */
/* written to the stream) */
len = OFstatic_cast(Uint32, outStream.write(&value[getTransferredBytes()], getLengthField() - getTransferredBytes()));
/* increase the amount of bytes which have been transferred correspondingly */
incTransferredBytes(len);
/* see if there is something fishy with the stream */
errorFlag = outStream.status();
}
else
{
Uint32 buflen = 0;
OFBool done = getTransferredBytes() == getLengthField();
while (! done)
{
// re-fill buffer from file if empty
errorFlag = wcache->fillBuffer(*this);
buflen = wcache->contentLength();
if (errorFlag.good())
{
// write as many bytes from cache buffer to stream as possible
len = wcache->writeBuffer(outStream);
/* increase the amount of bytes which have been transferred correspondingly */
incTransferredBytes(len);
/* see if there is something fishy with the stream */
errorFlag = outStream.status();
}
// stop writing if something went wrong, we were unable to send all of the buffer content
// (which indicates that the output stream needs to be flushed, or everything was sent out.
done = errorFlag.bad() || (len < buflen) || (getTransferredBytes() == getLengthField());
}
}
/* if the amount of transferred bytes equals the length of the element's value, the */
/* entire value has been written to the stream. Thus, this element's transfer state */
/* has to be set to ERW_ready. If this is not the case but the error flag still shows */
/* an ok value, there was no more space in the stream and a corresponding return value */
/* has to be set. (Isn't the "else if" part superfluous?!?) */
if (getLengthField() == getTransferredBytes()) setTransferState(ERW_ready);
else if (errorFlag.good()) errorFlag = EC_StreamNotifyClient;
}
}
}
/* return result value */
return errorFlag;
}
OFCondition DcmElement::writeSignatureFormat(DcmOutputStream &outStream,
const E_TransferSyntax oxfer,
const E_EncodingType enctype,
DcmWriteCache *wcache)
{
// for normal DICOM elements (everything except sequences), the data
// stream used for digital signature creation or verification is
// identical to the stream used for network communication or media
// storage.
return write(outStream, oxfer, enctype, wcache);
}
// ********************************
void DcmElement::writeXMLStartTag(STD_NAMESPACE ostream &out,
const size_t flags,
const char *attrText)
{
OFString xmlString;
DcmVR vr(getTag().getVR());
DcmTag tag = getTag();
const OFBool isPrivate = tag.isPrivate();
/* write XML start tag for attribute */
if (flags & DCMTypes::XF_useNativeModel)
{
out << "<DicomAttribute";
out << STD_NAMESPACE uppercase;
} else
out << "<element";
/* write attribute tag */
out << " tag=\"";
out << STD_NAMESPACE hex << STD_NAMESPACE setfill('0')
<< STD_NAMESPACE setw(4) << tag.getGTag();
/* in Native DICOM Model, write "ggggeeee" (no comma, upper case!) */
if (flags & DCMTypes::XF_useNativeModel)
{
/* for private element numbers, zero out 2 first element digits */
if (isPrivate)
{
out << STD_NAMESPACE setw(4) << (tag.getETag() & 0x00FF) << "\""
<< STD_NAMESPACE dec << STD_NAMESPACE setfill(' ');
}
else /* output full element number "eeee" */
{
out << STD_NAMESPACE setw(4) << tag.getETag() << "\""
<< STD_NAMESPACE dec << STD_NAMESPACE setfill(' ');
}
out << STD_NAMESPACE nouppercase;
}
else /* in DCMTK-specific format, write "gggg,eeee" */
{
out << "," << STD_NAMESPACE setw(4) << tag.getETag() << "\""
<< STD_NAMESPACE dec << STD_NAMESPACE setfill(' ');
}
/* value representation = VR */
out << " vr=\"" << vr.getValidVRName() << "\"";
if (flags & DCMTypes::XF_useNativeModel)
{
if (isPrivate)
{
/* output the creator of this private tag (but not for the creator tag itself) */
if (!tag.isPrivateReservation())
{
const char *creator = tag.getPrivateCreator();
if (creator != NULL)
{
out << " privateCreator=\"";
out << creator << "\"";
} else {
DCMDATA_WARN("Cannot write private creator for group 0x"
<< STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
<< tag.getGTag() << STD_NAMESPACE dec << STD_NAMESPACE setfill(' ')
<< " to XML output: Not present in data set");
}
}
} else {
/* write attribute keyword if known (and the official name is used in the data dictionary) */
const OFString tagName = getTagName();
if ((tagName != DcmTag_ERROR_TagName) &&
/* check for DCMTK-specific name prefixes used for old ACR NEMA and retired tags */
(tagName.substr(0, 8) != "RETIRED_") && (tagName.substr(0, 9) != "ACR_NEMA_"))
{
out << " keyword=\"" << OFStandard::convertToMarkupString(tagName, xmlString) << "\"";
}
}
/* close XML start tag */
out << ">" << OFendl;
} else {
/* value multiplicity = 1..n */
out << " vm=\"" << getVM() << "\"";
/* value length in bytes = 0..max */
out << " len=\"" << getLengthField() << "\"";
/* tag name (if known and not suppressed) */
if (!(flags & DCMTypes::XF_omitDataElementName))
out << " name=\"" << OFStandard::convertToMarkupString(getTagName(), xmlString) << "\"";
/* value loaded = no (or absent)*/
if (!valueLoaded())
out << " loaded=\"no\"";
/* write additional attributes (if any) */
if ((attrText != NULL) && (attrText[0] != '\0'))
out << " " << attrText;
/* close XML start tag */
out << ">";
}
}
void DcmElement::writeXMLEndTag(STD_NAMESPACE ostream &out,
const size_t flags)
{
/* write standardized XML end tag for all element types */
if (flags & DCMTypes::XF_useNativeModel)
out << "</DicomAttribute>" << OFendl;
else
out << "</element>" << OFendl;
}
OFCondition DcmElement::writeXML(STD_NAMESPACE ostream &out,
const size_t flags)
{
/* do not output group length elements in Native DICOM Model
* (as per PS 3.19 section A.1.1, introduced with Supplement 166) */
if (!(flags & DCMTypes::XF_useNativeModel) || !getTag().isGroupLength())
{
/* write XML start tag */
writeXMLStartTag(out, flags);
OFString value;
const OFBool convertNonASCII = (flags & DCMTypes::XF_convertNonASCII) > 0;
if (flags & DCMTypes::XF_useNativeModel)
{
/* write element value (if non-empty) */
if (!isEmpty())
{
const unsigned long vm = getVM();
for (unsigned long valNo = 0; valNo < vm; valNo++)
{
if (getOFString(value, valNo).good())
{
out << "<Value number=\"" << (valNo + 1) << "\">";
/* check whether conversion to XML markup string is required */
if (OFStandard::checkForMarkupConversion(value, convertNonASCII))
OFStandard::convertToMarkupStream(out, value, convertNonASCII);
else
out << value;
out << "</Value>" << OFendl;
}
}
}
} else {
/* write element value (only if loaded) */
if (valueLoaded())
{
if (getOFStringArray(value).good())
{
/* check whether conversion to XML markup string is required */
if (OFStandard::checkForMarkupConversion(value, convertNonASCII))
OFStandard::convertToMarkupStream(out, value, convertNonASCII);
else
out << value;
}
}
}
/* write XML end tag */
writeXMLEndTag(out, flags);
}
/* always report success */
return EC_Normal;
}
// ********************************
void DcmElement::writeJsonOpener(STD_NAMESPACE ostream &out,
DcmJsonFormat &format)
{
DcmVR vr(getTag().getVR());
DcmTag tag = getTag();
/* increase indentation level */
/* write attribute tag */
out << ++format.indent() << "\""
<< STD_NAMESPACE hex << STD_NAMESPACE setfill('0')
<< STD_NAMESPACE setw(4) << STD_NAMESPACE uppercase << tag.getGTag();
/* write "ggggeeee" (no comma, upper case!) */
/* for private element numbers, zero out 2 first element digits */
/* or output full element number "eeee" */
out << STD_NAMESPACE setw(4) << STD_NAMESPACE uppercase << tag.getETag() << "\":"
<< format.space() << "{" << STD_NAMESPACE dec << STD_NAMESPACE setfill(' ');
out << STD_NAMESPACE nouppercase;
/* increase indentation level */
/* value representation = VR */
out << format.newline() << ++format.indent() << "\"vr\":" << format.space() << "\""
<< vr.getValidVRName() << "\"";
}
void DcmElement::writeJsonCloser(STD_NAMESPACE ostream &out,
DcmJsonFormat &format)
{
/* output JSON ending and decrease indentation level */
out << format.newline() << --format.indent() << "}";
--format.indent();
}
OFCondition DcmElement::writeJson(STD_NAMESPACE ostream &out,
DcmJsonFormat &format)
{
/* always write JSON Opener */
writeJsonOpener(out, format);
/* write element value (if non-empty) */
if (!isEmpty())
{
OFString value;
if (format.asBulkDataURI(getTag(), value))
{
format.printBulkDataURIPrefix(out);
DcmJsonFormat::printString(out, value);
}
else
{
OFCondition status = getOFString(value, 0L);
if (status.bad())
return status;
format.printValuePrefix(out);
DcmJsonFormat::printNumberDecimal(out, value);
const unsigned long vm = getVM();
for (unsigned long valNo = 1; valNo < vm; ++valNo)
{
status = getOFString(value, valNo);
if (status.bad())
return status;
format.printNextArrayElementPrefix(out);
DcmJsonFormat::printNumberDecimal(out, value);
}
format.printValueSuffix(out);
}
}
/* write JSON Closer */
writeJsonCloser(out, format);
/* always report success */
return EC_Normal;
}
// ********************************
OFCondition DcmElement::getPartialValue(void *targetBuffer,
const Uint32 offset,
Uint32 numBytes,
DcmFileCache *cache,
E_ByteOrder byteOrder)
{
// check integrity of parameters passed to this method
if (targetBuffer == NULL) return EC_IllegalCall;
// if the user has only requested zero bytes, we immediately return
if (numBytes == 0) return EC_Normal;
// offset must always be less than attribute length (unless offset,
// attribute length and numBytes are all zero, a case that was
// handled above).
if (offset >= getLengthField()) return EC_InvalidOffset;
// check if the caller is trying to read past the end of the value field
if (numBytes > (getLengthField() - offset)) return EC_TooManyBytesRequested;
// check if the value is already in memory
if (valueLoaded())
{
// the attribute value is already in memory.
// change internal byte order of the attribute value to the desired byte order.
// This should only happen once for multiple calls to this method since the
// caller will hopefully always request the same byte order.
char *value = OFstatic_cast(char *, getValue(byteOrder));
if (value)
{
memcpy(targetBuffer, value + offset, numBytes);
}
else
{
// this should never happen because valueLoaded() returned true, but
// we don't want to dereference a NULL pointer anyway
return EC_IllegalCall;
}
}
else
{
// the value is not in memory. We should directly read from file and
// also consider byte order.
// since the value is not in memory, fLoadValue should exist. Check anyway.
if (! fLoadValue) return EC_IllegalCall;
// make sure we have a file cache object
DcmFileCache defaultcache; // automatic object, creation is cheap.
if (cache == NULL) cache = &defaultcache;
// the stream from which we will read the attribute value
DcmInputStream *readStream = NULL;
// check if we need to seek to a position in file earlier than
// the one specified by the user in order to correctly swap according
// to the VR.
size_t valueWidth = getTag().getVR().getValueWidth();
// we need to cast the target buffer to something we can increment bytewise
char *targetBufferChar = OFreinterpret_cast(char *, targetBuffer);
// the swap buffer should be large enough to keep one value of the current VR
unsigned char swapBuffer[SWAPBUFFER_SIZE];
if (valueWidth > SWAPBUFFER_SIZE) return EC_IllegalCall;
// seekoffset is the number of bytes we need to skip from the beginning of the
// value field to the point where we will start reading. This is always at the
// start of a new value of a multi-valued attribute.
Uint32 partialvalue = 0;
const Uint32 partialoffset = OFstatic_cast(Uint32, offset % valueWidth);
const offile_off_t seekoffset = offset - partialoffset;
// check if cache already contains the stream we're looking for
if (cache->isUser(this))
{
readStream = cache->getStream();
// since we cannot seek back in the stream (only forward), check if the stream
// is already past our needed start position
if (readStream->tell() - cache->getOffset() > seekoffset)
{
readStream = NULL;
}
}
// initialize the cache with new stream
if (!readStream)
{
// create input stream object
readStream = fLoadValue->create();
// check that read stream is non-NULL
if (readStream == NULL) return EC_InvalidStream;
// check that stream status is OK
if (readStream->status().bad())
{
OFCondition result = readStream->status();
delete readStream;
return result;
}
// readStream will be deleted when the cache is deleted
cache->init(readStream, this);
}
// now skip bytes from our current position in file to where we
// want to start reading.
offile_off_t remainingBytesToSkip = seekoffset - (readStream->tell() - cache->getOffset());
offile_off_t skipResult;
while (remainingBytesToSkip)
{
skipResult = readStream->skip(remainingBytesToSkip);
if (skipResult == 0) return EC_InvalidStream; // error while skipping
remainingBytesToSkip -= skipResult;
}
// check if the first few bytes we want to read are "in the middle" of one value
// of a multi-valued attribute. In that case we need to read the complete value,
// swap it and then copy only the last bytes in desired byte order.
if (partialoffset > 0)
{
// we possibly want to reset the stream to this point later
readStream->mark();
// compute the number of bytes we need to copy from the first attributes
partialvalue = OFstatic_cast(Uint32, valueWidth - partialoffset);
// we need to read a single data element into the swap buffer
if (valueWidth != OFstatic_cast(size_t, readStream->read(swapBuffer, OFstatic_cast(offile_off_t, valueWidth))))
return EC_InvalidStream;
// swap to desired byte order. fByteOrder contains the byte order in file.
swapIfNecessary(byteOrder, fByteOrder, swapBuffer, OFstatic_cast(Uint32, valueWidth), valueWidth);
// copy to target buffer and adjust values
if (partialvalue > numBytes)
{
memcpy(targetBufferChar, &swapBuffer[partialoffset], numBytes);
targetBufferChar += numBytes;
numBytes = 0;
// Reset stream to position marked before, since we have not copied the complete value
readStream->putback();
}
else
{
memcpy(targetBufferChar, &swapBuffer[partialoffset], partialvalue);
targetBufferChar += partialvalue;
numBytes -= partialvalue;
}
}
// now read the main block of data directly into the target buffer
partialvalue = OFstatic_cast(Uint32, numBytes % valueWidth);
const Uint32 bytesToRead = numBytes - partialvalue;
if (bytesToRead > 0)
{
// here we read the main block of data
if (OFstatic_cast(offile_off_t, bytesToRead) != readStream->read(targetBufferChar, bytesToRead))
return EC_InvalidStream;
// swap to desired byte order. fByteOrder contains the byte order in file.
swapIfNecessary(byteOrder, fByteOrder, targetBufferChar, bytesToRead, valueWidth);
// adjust pointer to target buffer
targetBufferChar += bytesToRead;
}
// check if the last few bytes we want to read are only a partial value.
// In that case we need to read the complete value, swap it and then copy
// only the first few bytes in desired byte order.
if (partialvalue > 0)
{
OFBool appendDuplicateByte = OFFalse;
size_t partialBytesToRead = valueWidth;
// we want to reset the stream to this point later
readStream->mark();
if (readStream->tell() + valueWidth > getLengthField()) {
// We are trying to read past the end of the value. We already made sure
// above that the requested range fits completely into the element's
// size, so this must mean that the length is not a multiple of the VR's
// value width.
// We allow this for OW and error out on all other VRs.
if (getTag().getVR().getValidEVR() == EVR_OW)
{
DCMDATA_WARN("DcmElement: Trying to read past end of value, duplicating last byte");
appendDuplicateByte = OFTrue;
// This is 2 for OW, but we know that only 1 byte of data is available
partialBytesToRead--;
}
else
{
// This would read the beginning of the next element from the stream,
// possibly hitting the end of stream.
DCMDATA_ERROR("DcmElement: Trying to read past end of value");
return EC_InvalidStream;
}
}
// we need to read a single data element into the swap buffer
if (partialBytesToRead != OFstatic_cast(size_t, readStream->read(swapBuffer, OFstatic_cast(offile_off_t, partialBytesToRead))))
return EC_InvalidStream;
if (appendDuplicateByte)
swapBuffer[partialBytesToRead] = swapBuffer[partialBytesToRead - 1];
// swap to desired byte order. fByteOrder contains the byte order in file.
swapIfNecessary(byteOrder, fByteOrder, swapBuffer, OFstatic_cast(Uint32, valueWidth), valueWidth);
// copy to target buffer and adjust values
memcpy(targetBufferChar, swapBuffer, partialvalue);
// finally reset stream to position marked before
readStream->putback();
}
}
// done.
return EC_Normal;
}
void DcmElement::compact()
{
if (fLoadValue && fValue)
{
DCMDATA_DEBUG("DcmElement::compact() removed element value of " << getTag()
<< " with " << getTransferredBytes() << " bytes");
delete[] fValue;
fValue = NULL;
setTransferredBytes(0);
}
}
OFCondition DcmElement::createValueFromTempFile(DcmInputStreamFactory *factory,
const Uint32 length,
const E_ByteOrder byteOrder)
{
if (factory && !(length & 1))
{
#if defined(HAVE_STD__NOTHROW) && defined(HAVE_NOTHROW_DELETE)
// if created with the nothrow version it must also be deleted with
// the nothrow version else memory error.
operator delete[] (fValue, std::nothrow);
#else
delete[] fValue;
#endif
fValue = 0;
delete fLoadValue;
fLoadValue = factory;
fByteOrder = byteOrder;
setLengthField(length);
return EC_Normal;
}
else return EC_IllegalCall;
}
// the following macro makes the source code more readable and easier to maintain
#define GET_AND_CHECK_UINT16_VALUE(tag, variable) \
result = dataset->findAndGetUint16(tag, variable); \
if (result == EC_TagNotFound) \
{ \
DCMDATA_WARN("DcmElement: Mandatory element " << DcmTag(tag).getTagName() << " " << tag << " is missing"); \
result = EC_MissingAttribute; \
} \
else if ((result == EC_IllegalCall) || (result == EC_IllegalParameter)) \
{ \
DCMDATA_WARN("DcmElement: No value for mandatory element " << DcmTag(tag).getTagName() << " " << tag); \
result = EC_MissingValue; \
} \
else if (result.bad()) \
DCMDATA_WARN("DcmElement: Cannot retrieve value of element " << DcmTag(tag).getTagName() << " " << tag << ": " << result.text());
OFCondition DcmElement::getUncompressedFrameSize(DcmItem *dataset,
Uint32 &frameSize,
OFBool pixelDataIsUncompressed) const
{
OFCondition result = EC_IllegalParameter;
if (dataset != NULL)
{
Uint16 rows = 0;
Uint16 cols = 0;
Uint16 samplesPerPixel = 0;
Uint16 bitsAllocated = 0;
Sint32 numberOfFrames = 1;
OFString photometricInterpretation;
/* retrieve number of frames from dataset (may be absent) */
(void) dataset->findAndGetSint32(DCM_NumberOfFrames, numberOfFrames);
if (numberOfFrames < 1) numberOfFrames = 1;
/* retrieve further values from dataset (and check them for validity and plausibility) */
GET_AND_CHECK_UINT16_VALUE(DCM_Columns, cols)
else if (cols == 0)
DCMDATA_WARN("DcmElement: Dubious value (" << cols << ") for element Columns " << DCM_Columns);
if (result.good())
{
GET_AND_CHECK_UINT16_VALUE(DCM_Rows, rows)
else if (rows == 0)
DCMDATA_WARN("DcmElement: Dubious value (" << rows << ") for element Rows " << DCM_Rows);
}
if (result.good())
{
GET_AND_CHECK_UINT16_VALUE(DCM_SamplesPerPixel, samplesPerPixel)
else /* result.good() */
{
/* also need to check value of PhotometricInterpretation */
if (dataset->findAndGetOFStringArray(DCM_PhotometricInterpretation, photometricInterpretation).good())
{
if (photometricInterpretation.empty())
DCMDATA_WARN("DcmElement: No value for mandatory element PhotometricInterpretation " << DCM_PhotometricInterpretation);
else {
const OFBool isMono = (photometricInterpretation == "MONOCHROME1") ||
(photometricInterpretation == "MONOCHROME2");
const OFBool isColor1 = (photometricInterpretation == "PALETTE COLOR");
const OFBool isColor3 = (photometricInterpretation == "RGB") ||
(photometricInterpretation == "HSV" /* retired */) ||
(photometricInterpretation == "YBR_FULL") ||
(photometricInterpretation == "YBR_FULL_422") ||
(photometricInterpretation == "YBR_PARTIAL_422" /* retired */) ||
(photometricInterpretation == "YBR_PARTIAL_420") ||
(photometricInterpretation == "YBR_ICT") ||
(photometricInterpretation == "YBR_RCT");
const OFBool isColor4 = (photometricInterpretation == "ARGB" /* retired */) ||
(photometricInterpretation == "CMYK" /* retired */);
if (((isMono || isColor1) && (samplesPerPixel != 1)) || (isColor3 && (samplesPerPixel != 3)) || (isColor4 && (samplesPerPixel != 4)))
{
DCMDATA_WARN("DcmElement: Invalid value (" << samplesPerPixel << ") for element SamplesPerPixel " << DCM_SamplesPerPixel
<< " when PhotometricInterpretation " << DCM_PhotometricInterpretation << " is " << photometricInterpretation);
result = EC_InvalidValue;
}
else if (!isMono && !isColor1 && !isColor3 && !isColor4)
DCMDATA_WARN("DcmElement: Unsupported value (" << photometricInterpretation << ") for element PhotometricInterpretation " << DCM_PhotometricInterpretation);
}
}
if (result.good() && (samplesPerPixel != 1) && (samplesPerPixel != 3))
DCMDATA_WARN("DcmElement: Dubious value (" << samplesPerPixel << ") for element SamplesPerPixel " << DCM_SamplesPerPixel);
}
}
if (result.good())
{
GET_AND_CHECK_UINT16_VALUE(DCM_BitsAllocated, bitsAllocated)
/* see PS3.3 Table C.7-11c: "Bits Allocated (0028,0100) shall be either 1, or a multiple of 8." */
else if ((bitsAllocated == 0) || ((bitsAllocated > 1) && (bitsAllocated % 8 != 0)))
DCMDATA_WARN("DcmElement: Dubious value (" << bitsAllocated << ") for element BitsAllocated " << DCM_BitsAllocated);
}
/* if all checks were passed... */
if (result.good())
{
if (pixelDataIsUncompressed && (photometricInterpretation == "YBR_FULL_422"))
{
/* YBR_FULL_422 can exist in uncompressed format, but in many cases
* images claiming to be YBR_FULL_422 are in fact formerly compressed
* images in YBR_FULL color model where the decoder has failed to update
* the photometric interpretation. We can keep these apart by checking
* the size of the pixel data and the number of frames.
*/
Uint32 pixelLen = 0;
DcmElement *pixData = NULL;
result = dataset->findAndGetElement(DCM_PixelData, pixData);
if (result.good() && pixData && ((pixelLen = pixData->getLength()) > 0))
{
const Uint32 v1 = rows * cols * 3;
const Uint32 v2 = (bitsAllocated / 8) * v1;
const Uint32 v3 = ((bitsAllocated % 8) * v1 + 7) / 8;
if (pixelLen >= (v2 + v3) * numberOfFrames)
{
/* the size of the pixel data indicates that no subsampling is present. We assume YBR_FULL. */
DCMDATA_WARN("DcmElement: PhotometricInterpretation probably incorrect, assuming YBR_FULL instead of YBR_FULL_422");
}
else
{
/* the size of the pixel data indicates subsampling is present. We assume YBR_FULL_422,
* which means that the frame size can be computed by setting samplesPerPixel to 2.
*/
samplesPerPixel = 2;
}
}
else
DCMDATA_WARN("DcmElement: failed to compute size of PixelData element");
}
/* compute frame size (TODO: check for 32-bit integer overflow?) */
if ((bitsAllocated % 8) == 0)
{
const Uint16 bytesAllocated = bitsAllocated / 8;
frameSize = bytesAllocated * rows * cols * samplesPerPixel;
}
else
{
/* need to split calculation in order to avoid integer overflow for large pixel data */
const Uint32 v1 = rows * cols * samplesPerPixel;
const Uint32 v2 = (bitsAllocated / 8) * v1;
const Uint32 v3 = ((bitsAllocated % 8) * v1 + 7) / 8;
// # old code: frameSize = (bitsAllocated * rows * cols * samplesPerPixel + 7) / 8;
frameSize = v2 + v3;
}
} else {
/* in case of error, return a frame size of 0 */
frameSize = 0;
}
}
return result;
}
OFCondition DcmElement::getUncompressedFrame(DcmItem * /* dataset */ ,
Uint32 /* frameNo */ ,
Uint32& /* startFragment */ ,
void * /* buffer */ ,
Uint32 /* bufSize */ ,
OFString& /* decompressedColorModel */ ,
DcmFileCache * /* cache */ )
{
return EC_IllegalCall;
}
OFCondition DcmElement::getDecompressedColorModel(DcmItem * /* dataset */,
OFString & /* decompressedColorModel */)
{
return EC_IllegalCall;
}
// ********************************
int DcmElement::scanValue(const OFString &value,
const OFString &vr,
const size_t pos,
const size_t num)
{
return scanValue(vr, value.data() + pos, num != OFString_npos ? num : value.size() - pos);
}
int DcmElement::scanValue(const OFString& vr,
const char* const value,
const size_t size)
{
return vrscan::scan(vr, value, size);
}
unsigned long DcmElement::determineVM(const char *str,
const size_t len)
{
unsigned long vm = 0;
// check for non-empty string
if ((str != NULL) && (len > 0))
{
// count number of delimiters (plus 1)
vm = 1;
const char *p = str;
for (size_t i = 0; i < len; i++)
{
if (*p++ == '\\')
vm++;
}
}
return vm;
}
size_t DcmElement::getValueFromString(const char *str,
const size_t pos,
const size_t len,
OFString &val)
{
size_t newPos = pos;
// check for non-empty string or invalid start position
if ((str != NULL) && (len > 0) && (pos < len))
{
// start at given position
const char *p = str + pos;
// search for next backslash (if any)
while ((newPos++ < len) && (*p != '\\'))
p++;
// extract selected value from string
val.assign(str + pos, newPos - pos - 1);
} else
val.clear();
return newPos;
}
OFCondition DcmElement::checkVM(const unsigned long vmNum,
const OFString &vmStr)
{
OFCondition result = EC_Normal;
// only check non-empty values
if (vmNum > 0)
{
if (vmStr == "1")
{
if (vmNum != 1) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "1-2")
{
if (vmNum > 2) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "1-3")
{
if (vmNum > 3) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "1-8")
{
if (vmNum > 8) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "1-99")
{
if (vmNum > 99) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "2")
{
if (vmNum != 2) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "2-4")
{
if ((vmNum < 2) || (vmNum > 4)) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "2-n")
{
if (vmNum < 2) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "2-2n")
{
if ((vmNum % 2) != 0) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "3")
{
if (vmNum != 3) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "3-n")
{
if (vmNum < 3) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "3-3n")
{
if ((vmNum % 3) != 0) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "4")
{
if (vmNum != 4) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "4-5")
{
if ((vmNum < 4) || (vmNum > 5)) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "4-4n")
{
if ((vmNum % 4) != 0) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "5")
{
if (vmNum != 5) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "5-n")
{
if (vmNum < 5) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "6")
{
if (vmNum != 6) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "6-n")
{
if (vmNum < 6) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "7")
{
if (vmNum != 7) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "7-7n")
{
if ((vmNum % 7) != 0) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "8")
{
if (vmNum != 8) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "9")
{
if (vmNum != 9) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "11")
{
if (vmNum != 11) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "16")
{
if (vmNum != 16) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "24")
{
if (vmNum != 24) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "32")
{
if (vmNum != 32) result = EC_ValueMultiplicityViolated;
}
else if (vmStr == "256")
{
if (vmNum != 256) result = EC_ValueMultiplicityViolated;
}
else if ((vmStr != "1-n") && (vmStr != "0-n"))
{
// given value of 'vmStr' not (yet) supported
result = EC_IllegalParameter;
}
}
return result;
}
OFBool DcmElement::isUniversalMatch(const OFBool normalize,
const OFBool enableWildCardMatching)
{
OFstatic_cast(void,enableWildCardMatching);
return isEmpty(normalize);
}
OFBool DcmElement::matches(const DcmElement& candidate,
const OFBool enableWildCardMatching) const
{
OFstatic_cast(void,candidate);
OFstatic_cast(void,enableWildCardMatching);
return OFFalse;
}
OFBool DcmElement::combinationMatches(const DcmElement& keySecond,
const DcmElement& candidateFirst,
const DcmElement& candidateSecond) const
{
OFstatic_cast(void,keySecond);
OFstatic_cast(void,candidateFirst);
OFstatic_cast(void,candidateSecond);
return OFFalse;
}
|