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
|
/*
*
* 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 DcmPixelSequence
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/ofstd/ofstream.h"
#include "dcmtk/ofstd/ofuuid.h"
#include "dcmtk/dcmdata/dcpixseq.h"
#include "dcmtk/dcmdata/dcpxitem.h"
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmdata/dcvr.h"
#include "dcmtk/dcmdata/dcdeftag.h"
// ********************************
DcmPixelSequence::DcmPixelSequence(const DcmTag &tag)
: DcmSequenceOfItems(tag, 0),
Xfer(EXS_Unknown)
{
setTagVR(EVR_OB);
setLengthField(DCM_UndefinedLength); // pixel sequences always use undefined length
}
DcmPixelSequence::DcmPixelSequence(const DcmTag &tag,
const Uint32 len)
: DcmSequenceOfItems(tag, len),
Xfer(EXS_Unknown)
{
setTagVR(EVR_OB);
setLengthField(DCM_UndefinedLength); // pixel sequences always use undefined length
}
DcmPixelSequence::DcmPixelSequence(const DcmPixelSequence &old)
: DcmSequenceOfItems(old),
Xfer(old.Xfer)
{
/* everything gets handled in DcmSequenceOfItems constructor */
}
DcmPixelSequence::~DcmPixelSequence()
{
}
DcmPixelSequence &DcmPixelSequence::operator=(const DcmPixelSequence &obj)
{
if (this != &obj)
{
DcmSequenceOfItems::operator=(obj);
Xfer = obj.Xfer;
}
return *this;
}
OFCondition DcmPixelSequence::copyFrom(const DcmObject& rhs)
{
if (this != &rhs)
{
if (rhs.ident() != ident()) return EC_IllegalCall;
*this = OFstatic_cast(const DcmPixelSequence &, rhs);
}
return EC_Normal;
}
// ********************************
void DcmPixelSequence::print(STD_NAMESPACE ostream &out,
const size_t flags,
const int level,
const char *pixelFileName,
size_t *pixelCounter)
{
/* print pixel sequence start line */
if (flags & DCMTypes::PF_showTreeStructure)
{
/* empty text */
printInfoLine(out, flags, level);
/* print pixel sequence content */
if (!itemList->empty())
{
/* print pixel items */
DcmObject *dO;
itemList->seek(ELP_first);
do {
dO = itemList->get();
dO->print(out, flags, level + 1, pixelFileName, pixelCounter);
} while (itemList->seek(ELP_next));
}
} else {
OFOStringStream oss;
oss << "(PixelSequence ";
if (getLengthField() != DCM_UndefinedLength)
oss << "with explicit length ";
oss << "#=" << card() << ")" << OFStringStream_ends;
OFSTRINGSTREAM_GETSTR(oss, tmpString)
printInfoLine(out, flags, level, tmpString);
OFSTRINGSTREAM_FREESTR(tmpString)
/* print pixel sequence content */
if (!itemList->empty())
{
DcmObject *dO;
itemList->seek(ELP_first);
do {
dO = itemList->get();
dO->print(out, flags, level + 1, pixelFileName, pixelCounter);
} while (itemList->seek(ELP_next));
}
/* print pixel sequence end line */
DcmTag delimItemTag(DCM_SequenceDelimitationItemTag);
if (getLengthField() == DCM_UndefinedLength)
printInfoLine(out, flags, level, "(SequenceDelimitationItem)", &delimItemTag);
else
printInfoLine(out, flags, level, "(SequenceDelimitationItem for re-encod.)", &delimItemTag);
}
}
// ********************************
OFCondition DcmPixelSequence::writeXML(STD_NAMESPACE ostream &out,
const size_t flags)
{
OFCondition l_error = EC_Normal;
if (flags & DCMTypes::XF_useNativeModel)
{
/* write XML start tag */
writeXMLStartTag(out, flags);
/* for an empty value field, we do not need to do anything */
if (getLengthField() > 0)
{
/* encode binary data as Base64 */
if (flags & DCMTypes::XF_encodeBase64)
{
out << "<InlineBinary>";
Uint8 *byteValues = OFstatic_cast(Uint8 *, getValue());
OFStandard::encodeBase64(out, byteValues, OFstatic_cast(size_t, getLengthField()));
out << "</InlineBinary>" << OFendl;
} else {
/* generate a new UID but the binary data is not (yet) written. */
OFUUID uuid;
out << "<BulkData uuid=\"";
uuid.print(out, OFUUID::ER_RepresentationHex);
out << "\"/>" << OFendl;
}
}
/* write XML end tag */
writeXMLEndTag(out, flags);
} else {
/* the DCMTK-specific XML format requires no special handling */
l_error = DcmSequenceOfItems::writeXML(out, flags);
}
return l_error;
}
// ********************************
Uint32 DcmPixelSequence::calcElementLength(const E_TransferSyntax xfer,
const E_EncodingType enctype)
{
// add 8 bytes for Sequence Delimitation Tag which always exists for Pixel Sequences
return DcmElement::calcElementLength(xfer, enctype) + 8;
}
// ********************************
OFCondition DcmPixelSequence::makeSubObject(DcmObject *&subObject,
const DcmTag &newTag,
const Uint32 newLength)
{
OFCondition l_error = EC_Normal;
DcmObject *newObject = NULL;
switch (newTag.getEVR())
{
case EVR_na:
if (newTag == DCM_Item)
newObject = new DcmPixelItem(newTag, newLength);
else if (newTag == DCM_SequenceDelimitationItem)
l_error = EC_SequEnd;
else if (newTag == DCM_ItemDelimitationItem)
l_error = EC_ItemEnd;
else
l_error = EC_InvalidTag;
break;
default:
newObject = new DcmPixelItem(newTag, newLength);
l_error = EC_CorruptedData;
break;
}
subObject = newObject;
return l_error;
}
// ********************************
OFCondition DcmPixelSequence::insert(DcmPixelItem *item,
unsigned long where)
{
errorFlag = EC_Normal;
if (item != NULL)
{
// special case: last position
if (where == DCM_EndOfListIndex)
{
// insert at end of list (avoid seeking)
itemList->append(item);
DCMDATA_TRACE("DcmPixelSequence::insert() Item at last position inserted");
} else {
// insert after "where"
itemList->seek_to(where);
itemList->insert(item);
DCMDATA_TRACE("DcmPixelSequence::insert() Item at position " << where << " inserted");
}
// check whether the new item already has a parent
if (item->getParent() != NULL)
{
DCMDATA_DEBUG("DcmPixelSequence::insert() PixelItem already has a parent: "
<< item->getParent()->getTag() << " VR=" << DcmVR(item->getParent()->getVR()).getVRName());
}
// remember the parent (i.e. the surrounding sequence)
item->setParent(this);
} else
errorFlag = EC_IllegalCall;
return errorFlag;
}
// ********************************
OFCondition DcmPixelSequence::getItem(DcmPixelItem *&item,
const unsigned long num)
{
errorFlag = EC_Normal;
item = OFstatic_cast(DcmPixelItem*, itemList->seek_to(num)); // read item from list
if (item == NULL)
errorFlag = EC_IllegalCall;
return errorFlag;
}
// ********************************
OFCondition DcmPixelSequence::remove(DcmPixelItem *&item,
const unsigned long num)
{
errorFlag = EC_Normal;
item = OFstatic_cast(DcmPixelItem*, itemList->seek_to(num)); // read item from list
if (item != NULL)
{
itemList->remove();
item->setParent(NULL); // forget about the parent
} else
errorFlag = EC_IllegalCall;
return errorFlag;
}
// ********************************
OFCondition DcmPixelSequence::remove(DcmPixelItem *item)
{
errorFlag = EC_IllegalCall;
if (!itemList->empty() && item != NULL)
{
DcmObject *dO;
itemList->seek(ELP_first);
do {
dO = itemList->get();
if (dO == item)
{
itemList->remove(); // remove element from list, but do no delete it
item->setParent(NULL); // forget about the parent
errorFlag = EC_Normal;
break;
}
} while (itemList->seek(ELP_next));
}
return errorFlag;
}
// ********************************
OFCondition DcmPixelSequence::changeXfer(const E_TransferSyntax newXfer)
{
if (Xfer == EXS_Unknown || canWriteXfer(newXfer, Xfer))
{
Xfer = newXfer;
return EC_Normal;
} else
return EC_IllegalCall;
}
// ********************************
OFBool DcmPixelSequence::canWriteXfer(const E_TransferSyntax newXfer,
const E_TransferSyntax oldXfer)
{
DcmXfer newXferSyn(newXfer);
return newXferSyn.usesEncapsulatedFormat() && newXfer == oldXfer && oldXfer == Xfer;
}
// ********************************
OFCondition DcmPixelSequence::read(DcmInputStream &inStream,
const E_TransferSyntax ixfer,
const E_GrpLenEncoding glenc,
const Uint32 maxReadLength)
{
OFCondition l_error = changeXfer(ixfer);
if (l_error.good())
return DcmSequenceOfItems::read(inStream, ixfer, glenc, maxReadLength);
return l_error;
}
// ********************************
OFCondition DcmPixelSequence::write(DcmOutputStream &outStream,
const E_TransferSyntax oxfer,
const E_EncodingType /*enctype*/,
DcmWriteCache *wcache)
{
OFCondition l_error = changeXfer(oxfer);
if (l_error.good())
return DcmSequenceOfItems::write(outStream, oxfer, EET_UndefinedLength, wcache);
return l_error;
}
// ********************************
OFCondition DcmPixelSequence::writeSignatureFormat(DcmOutputStream &outStream,
const E_TransferSyntax oxfer,
const E_EncodingType /*enctype*/,
DcmWriteCache *wcache)
{
OFCondition l_error = changeXfer(oxfer);
if (l_error.good())
return DcmSequenceOfItems::writeSignatureFormat(outStream, oxfer, EET_UndefinedLength, wcache);
return l_error;
}
OFCondition DcmPixelSequence::storeCompressedFrame(DcmOffsetList &offsetList,
Uint8 *compressedData,
Uint32 compressedLen,
Uint32 fragmentSize)
{
if (compressedData == NULL)
return EC_IllegalCall;
OFCondition result = EC_Normal;
if (fragmentSize >= 0x400000)
fragmentSize = 0; // prevent overflow
else
fragmentSize <<= 10; // unit is kbytes
if (fragmentSize == 0)
fragmentSize = compressedLen;
Uint32 offset = 0;
Uint32 currentSize = 0;
Uint32 numFragments = 0;
DcmPixelItem *fragment = NULL;
while ((offset < compressedLen) && (result.good()))
{
fragment = new DcmPixelItem(DCM_PixelItemTag);
if (fragment == NULL)
result = EC_MemoryExhausted;
else
{
insert(fragment);
numFragments++;
currentSize = fragmentSize;
if (offset + currentSize > compressedLen)
currentSize = compressedLen - offset;
// if currentSize is odd this will be fixed during DcmOtherByteOtherWord::write()
result = fragment->putUint8Array(compressedData + offset, currentSize);
if (result.good())
offset += currentSize;
}
}
currentSize = offset + (numFragments << 3); // 8 bytes extra for each item header
// odd frame size requires padding, i.e. last fragment uses odd length pixel item
if (currentSize & 1)
currentSize++;
offsetList.push_back(currentSize);
return result;
}
|