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
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmCurve.h"
#include "gdcmDataElement.h"
#include "gdcmDataSet.h"
#include "gdcmAttribute.h"
#include <vector>
namespace gdcm
{
/*
C.10.2.1 Curve Attribute Descriptions
C.10.2.1.1 Type of data
A description of the Type of Data (50xx,0020) in this curve.
Defined Terms:
TAC = time activity curve PROF = image profile
HIST = histogram ROI = polygraphic region of interest
TABL = table of values FILT = filter kernel
POLY = poly line ECG = ecg data
PRESSURE = pressure data FLOW = flow data
PHYSIO = physio data RESP = Respiration trace
*/
class CurveInternal
{
public:
CurveInternal():
Group(0),
Dimensions(0),
NumberOfPoints(0),
TypeOfData(),
CurveDescription(),
DataValueRepresentation(0),
Data() {}
/*
(5004,0000) UL 2316 # 4, 1 CurveGroupLength
(5004,0005) US 1 # 2, 1 CurveDimensions
(5004,0010) US 1126 # 2, 1 NumberOfPoints
(5004,0020) CS [PHYSIO] # 6, 1 TypeOfData
(5004,0022) LO (no value available) # 0, 0 CurveDescription
(5000,0030) SH [DPPS\NONE] # 10, 2 AxisUnits
(5004,0103) US 0 # 2, 1 DataValueRepresentation
(5000,0110) US 0\1 # 4, 2 CurveDataDescriptor
(5000,0112) US 0 # 2, 1 CoordinateStartValue
(5000,0114) US 300 # 2, 1 CoordinateStepValue
(5000,2500) LO [Physio_1] # 8, 1 CurveLabel
(5004,3000) OW 0020\0020\0020\0020\0020\0020\0020\0020\0020\0020\0020\0020\0020... # 2252, 1 CurveData
*/
// Identifier need to be in the [5000,50FF] range (no odd number):
unsigned short Group;
unsigned short Dimensions;
unsigned short NumberOfPoints;
std::string TypeOfData;
std::string CurveDescription;
unsigned short DataValueRepresentation;
std::vector<char> Data;
std::vector<unsigned short> CurveDataDescriptor;
unsigned short CoordinateStartValue;
unsigned short CoordinateStepValue;
void Print(std::ostream &os) const {
os << "Group 0x" << std::hex << Group << std::dec << std::endl;
os << "Dimensions :" << Dimensions << std::endl;
os << "NumberOfPoints :" << NumberOfPoints << std::endl;
os << "TypeOfData :" << TypeOfData << std::endl;
os << "CurveDescription :" << CurveDescription << std::endl;
os << "DataValueRepresentation :" << DataValueRepresentation << std::endl;
unsigned short * p = (unsigned short*)&Data[0];
for(int i = 0; i < NumberOfPoints; i+=2)
{
os << p[i] << "," << p[i+1] << std::endl;
}
}
};
Curve::Curve()
{
Internal = new CurveInternal;
}
Curve::~Curve()
{
delete Internal;
}
Curve::Curve(Curve const &ov):Object(ov)
{
//delete Internal;
Internal = new CurveInternal;
// TODO: copy CurveInternal into other...
*Internal = *ov.Internal;
}
void Curve::Print(std::ostream &os) const
{
Internal->Print( os );
}
unsigned int Curve::GetNumberOfCurves(DataSet const & ds)
{
Tag overlay(0x5000,0x0000); // First possible overlay
bool finished = false;
unsigned int numoverlays = 0;
while( !finished )
{
const DataElement &de = ds.FindNextDataElement( overlay );
if( de.GetTag().GetGroup() > 0x50FF ) // last possible curve
{
finished = true;
}
else if( de.GetTag().IsPrivate() )
{
// Move on to the next public one:
overlay.SetGroup( (uint16_t)(de.GetTag().GetGroup() + 1) );
overlay.SetElement( 0 ); // reset just in case...
}
else
{
// Yeah this is an overlay element
//if( ds.FindDataElement( Tag(overlay.GetGroup(),0x3000 ) ) )
if( ds.FindDataElement( Tag(de.GetTag().GetGroup(),0x3000 ) ) )
{
// ok so far so good...
//const DataElement& overlaydata = ds.GetDataElement(Tag(overlay.GetGroup(),0x3000));
const DataElement& overlaydata = ds.GetDataElement(Tag(de.GetTag().GetGroup(),0x3000));
if( !overlaydata.IsEmpty() )
{
++numoverlays;
}
}
// Store found tag in overlay:
overlay = de.GetTag();
// Move on to the next possible one:
overlay.SetGroup( (uint16_t)(overlay.GetGroup() + 2) );
// reset to element 0x0 just in case...
overlay.SetElement( 0 );
}
}
return numoverlays;
}
void Curve::Update(const DataElement & de)
{
assert( de.GetTag().IsPublic() );
const ByteValue* bv = de.GetByteValue();
if( !bv ) return; // Discard any empty element (will default to another value)
assert( bv->GetPointer() && bv->GetLength() );
std::string s( bv->GetPointer(), bv->GetLength() );
// What if a \0 can be found before the end of string...
//assert( strlen( s.c_str() ) == s.size() );
// First thing check consistency:
if( !GetGroup() )
{
SetGroup( de.GetTag().GetGroup() );
}
else // check consistency
{
assert( GetGroup() == de.GetTag().GetGroup() ); // programmer error
}
//std::cerr << "Tag: " << de.GetTag() << std::endl;
if( de.GetTag().GetElement() == 0x0000 ) // CurveGroupLength
{
// ??
}
else if( de.GetTag().GetElement() == 0x0005 ) // CurveDimensions
{
Attribute<0x5000,0x0005> at;
at.SetFromDataElement( de );
SetDimensions( at.GetValue() );
}
else if( de.GetTag().GetElement() == 0x0010 ) // NumberOfPoints
{
Attribute<0x5000,0x0010> at;
at.SetFromDataElement( de );
SetNumberOfPoints( at.GetValue() );
}
else if( de.GetTag().GetElement() == 0x0020 ) // TypeOfData
{
SetTypeOfData( s.c_str() );
}
else if( de.GetTag().GetElement() == 0x0022 ) // CurveDescription
{
SetCurveDescription( s.c_str() );
}
else if( de.GetTag().GetElement() == 0x0030 ) // AxisUnits
{
gdcmWarningMacro( "TODO" );
}
else if( de.GetTag().GetElement() == 0x0040 ) // Axis Labels
{
gdcmWarningMacro( "TODO" );
}
else if( de.GetTag().GetElement() == 0x0103 ) // DataValueRepresentation
{
Attribute<0x5000,0x0103> at;
at.SetFromDataElement( de );
SetDataValueRepresentation( at.GetValue() );
}
else if( de.GetTag().GetElement() == 0x0104 ) // Minimum Coordinate Value
{
gdcmWarningMacro( "TODO" );
}
else if( de.GetTag().GetElement() == 0x0105 ) // Maximum Coordinate Value
{
gdcmWarningMacro( "TODO" );
}
else if( de.GetTag().GetElement() == 0x0106 ) // Curve Range
{
gdcmWarningMacro( "TODO" );
}
else if( de.GetTag().GetElement() == 0x0110 ) // CurveDataDescriptor
{
Attribute<0x5000,0x0110> at;
at.SetFromDataElement( de );
SetCurveDataDescriptor( at.GetValues(), at.GetNumberOfValues() );
}
else if( de.GetTag().GetElement() == 0x0112 ) // CoordinateStartValue
{
Attribute<0x5000,0x0112> at;
at.SetFromDataElement( de );
SetCoordinateStartValue( at.GetValue() );
}
else if( de.GetTag().GetElement() == 0x0114 ) // CoordinateStepValue
{
Attribute<0x5000,0x0114> at;
at.SetFromDataElement( de );
SetCoordinateStepValue( at.GetValue() );
}
else if( de.GetTag().GetElement() == 0x2500 ) // CurveLabel
{
gdcmWarningMacro( "TODO" );
}
else if( de.GetTag().GetElement() == 0x2600 ) // Referenced Overlay Sequence
{
gdcmWarningMacro( "TODO" );
}
else if( de.GetTag().GetElement() == 0x2610 ) // Referenced Overlay Group
{
gdcmWarningMacro( "TODO" );
}
else if( de.GetTag().GetElement() == 0x3000 ) // CurveData
{
SetCurve(bv->GetPointer(), bv->GetLength());
}
else
{
assert( 0 && "should not happen: Unknown curve tag" );
}
}
void Curve::SetGroup(unsigned short group) { Internal->Group = group; }
unsigned short Curve::GetGroup() const { return Internal->Group; }
void Curve::SetDimensions(unsigned short dimensions) { Internal->Dimensions = dimensions; }
unsigned short Curve::GetDimensions() const { return Internal->Dimensions; }
void Curve::SetNumberOfPoints(unsigned short numberofpoints) { Internal->NumberOfPoints = numberofpoints; }
unsigned short Curve::GetNumberOfPoints() const { return Internal->NumberOfPoints; }
void Curve::SetTypeOfData(const char *typeofdata)
{
if( typeofdata )
Internal->TypeOfData = typeofdata;
}
const char *Curve::GetTypeOfData() const { return Internal->TypeOfData.c_str(); }
static const char * const TypeOfDataDescription[][2] = {
{ "TAC" , "time activity curve" },
{ "PROF" , "image profile" },
{ "HIST" , "histogram" },
{ "ROI" , "polygraphic region of interest" },
{ "TABL" , "table of values" },
{ "FILT" , "filter kernel" },
{ "POLY" , "poly line" },
{ "ECG" , "ecg data" },
{ "PRESSURE" , "pressure data" },
{ "FLOW" , "flow data" },
{ "PHYSIO" , "physio data" },
{ "RESP" , "Respiration trace" },
{ 0 , 0 }
};
const char *Curve::GetTypeOfDataDescription() const
{
typedef const char* const (*TypeOfDataDescriptionType)[2];
TypeOfDataDescriptionType t = TypeOfDataDescription;
int i = 0;
const char *p = t[i][0];
while( p )
{
if( Internal->TypeOfData == p )
{
break;
}
++i;
p = t[i][0];
}
return t[i][1];
}
void Curve::SetCurveDescription(const char *curvedescription)
{
if( curvedescription )
Internal->CurveDescription = curvedescription;
}
void Curve::SetDataValueRepresentation(unsigned short datavaluerepresentation) { Internal->DataValueRepresentation = datavaluerepresentation; }
unsigned short Curve::GetDataValueRepresentation() const { return Internal->DataValueRepresentation; }
void Curve::SetCurveDataDescriptor(const uint16_t * values, size_t num)
{
Internal->CurveDataDescriptor = std::vector<uint16_t>(values, values+num);
}
std::vector<unsigned short> const &Curve::GetCurveDataDescriptor() const
{
return Internal->CurveDataDescriptor;
}
void Curve::SetCoordinateStartValue( unsigned short v )
{
Internal->CoordinateStartValue = v;
}
void Curve::SetCoordinateStepValue( unsigned short v )
{
Internal->CoordinateStepValue = v;
}
bool Curve::IsEmpty() const
{
return Internal->Data.empty();
}
void Curve::SetCurve(const char *array, unsigned int length)
{
if( !array || length == 0 ) return;
Internal->Data.resize( length );
std::copy(array, array+length, Internal->Data.begin());
//assert( 8 * length == (unsigned int)Internal->Rows * Internal->Columns );
//assert( Internal->Data.size() == length );
}
void Curve::Decode(std::istream &is, std::ostream &os)
{
(void)is;
(void)os;
assert(0);
}
/*
PS 3.3 - 2004
C.10.2.1.2 Data value representation
0000H = unsigned short (US)
0001H = signed short (SS)
0002H = floating point single (FL)
0003H = floating point double (FD)
0004H = signed long (SL)
*/
inline size_t getsizeofrep( unsigned short dr )
{
size_t val = 0;
switch( dr )
{
case 0:
val = sizeof( uint16_t );
break;
case 1:
val = sizeof( int16_t );
break;
case 2:
val = sizeof( float );
break;
case 3:
val = sizeof( double );
break;
case 4:
val = sizeof( int32_t );
break;
}
return val;
}
/*
C.10.2.1.5 Curve data descriptor, coordinate start value, coordinate step value
The Curve Data for dimension(s) containing evenly distributed data can be eliminated by using a
method that defines the Coordinate Start Value and Coordinate Step Value (interval). The one
dimensional data list is then calculated rather than being enumerated.
For the Curve Data Descriptor (50xx,0110) an Enumerated Value describing how each
component of the N-tuple curve is described, either by points or interval spacing. One value for
each dimension. Where:
0000H = Dimension component described using interval spacing
0001H = Dimension component described using values
Using interval spacing:
Dimension component(s) described by interval spacing use Attributes of Coordinate Start Value
(50xx,0112), Coordinate Step Value (50xx,0114) and Number of Points (50xx,0010). The 1-
dimensional data list is calculated by using a start point of Coordinate Start Value and adding the
interval (Coordinate Step Value) to obtain each data point until the Number of Points is satisfied.
The data points of this dimension will be absent from Curve Data (50xx,3000).
*/
double Curve::ComputeValueFromStartAndStep(unsigned int idx) const
{
assert( !Internal->CurveDataDescriptor.empty() );
const double res = Internal->CoordinateStartValue +
Internal->CoordinateStepValue * idx;
return res;
}
void Curve::GetAsPoints(float *array) const
{
assert( getsizeofrep(Internal->DataValueRepresentation) );
if( Internal->CurveDataDescriptor.empty() )
{
assert( Internal->Data.size() == (uint32_t)Internal->NumberOfPoints *
Internal->Dimensions * getsizeofrep( Internal->DataValueRepresentation) );
}
else
{
assert( Internal->Data.size() == (uint32_t)Internal->NumberOfPoints *
1 * getsizeofrep( Internal->DataValueRepresentation) );
}
assert( Internal->Dimensions == 1 || Internal->Dimensions == 2 );
const int mult = Internal->Dimensions;
int genidx = -1;
if( !Internal->CurveDataDescriptor.empty() )
{
assert( Internal->CurveDataDescriptor.size() == Internal->Dimensions );
assert( Internal->CurveDataDescriptor.size() == 2 ); // FIXME
if( Internal->CurveDataDescriptor[0] == 0 )
{
assert( Internal->CurveDataDescriptor[1] == 1 );
genidx = 0;
}
else if( Internal->CurveDataDescriptor[1] == 0 )
{
assert( Internal->CurveDataDescriptor[0] == 1 );
genidx = 1;
}
else
{
assert( 0 && "TODO" );
}
}
const char * beg = &Internal->Data[0];
const char * end = beg + Internal->Data.size();
if( genidx == -1 )
{
assert( end == beg + 2 * Internal->NumberOfPoints ); (void)beg;(void)end;
}
else
{
assert( end == beg + mult * Internal->NumberOfPoints ); (void)beg;(void)end;
}
if( Internal->DataValueRepresentation == 0 )
{
// PS 3.3 - 2004
// C.10.2.1.5 Curve data descriptor, coordinate start value, coordinate step value
uint16_t * p = (uint16_t*)&Internal->Data[0];
// X
if( genidx == 0 )
for(int i = 0; i < Internal->NumberOfPoints; i++ )
array[3*i+0] = ComputeValueFromStartAndStep( i );
else
for(int i = 0; i < Internal->NumberOfPoints; i++ )
array[3*i+0] = p[i + 0];
// Y
if( genidx == 1 )
for(int i = 0; i < Internal->NumberOfPoints; i++ )
array[3*i+1] = ComputeValueFromStartAndStep( i );
else
{
if( mult == 2 && genidx == -1 )
{
for(int i = 0; i < Internal->NumberOfPoints; i++ )
array[3*i+1] = p[i + 1];
}
else if( mult == 2 && genidx == 0 )
{
for(int i = 0; i < Internal->NumberOfPoints; i++ )
array[3*i+1] = p[i + 0];
}
else
{
for(int i = 0; i < Internal->NumberOfPoints; i++ )
array[3*i+1] = 0;
}
}
// Z
for(int i = 0; i < Internal->NumberOfPoints; i++ )
array[3*i+2] = 0;
}
else if( Internal->DataValueRepresentation == 1 )
{
int16_t * p = (int16_t*)&Internal->Data[0];
for(int i = 0; i < Internal->NumberOfPoints; i++ )
{
array[3*i+0] = p[mult*i + 0];
if( mult > 1 )
array[3*i+1] = p[mult*i + 1];
else
array[3*i+1] = 0;
array[3*i+2] = 0;
}
}
else if( Internal->DataValueRepresentation == 2 )
{
float * p = (float*)&Internal->Data[0];
for(int i = 0; i < Internal->NumberOfPoints; i++ )
{
array[3*i+0] = p[mult*i + 0];
if( mult > 1 )
array[3*i+1] = p[mult*i + 1];
else
array[3*i+1] = 0;
array[3*i+2] = 0;
}
}
else if( Internal->DataValueRepresentation == 3 )
{
double * p = (double*)&Internal->Data[0];
for(int i = 0; i < Internal->NumberOfPoints; i++ )
{
array[3*i+0] = (float)p[mult*i + 0];
if( mult > 1 )
array[3*i+1] = (float)p[mult*i + 1];
else
array[3*i+1] = 0;
array[3*i+2] = 0;
}
}
else if( Internal->DataValueRepresentation == 4 )
{
int32_t * p = (int32_t*)&Internal->Data[0];
for(int i = 0; i < Internal->NumberOfPoints; i++ )
{
array[3*i+0] = (float)p[mult*i + 0];
if( mult > 1 )
array[3*i+1] = (float)p[mult*i + 1];
else
array[3*i+1] = 0;
array[3*i+2] = 0;
}
}
else
{
assert( 0 );
}
}
}
|