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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkGESignaReader.h"
#include "vtkByteSwap.h"
#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkMedicalImageProperties.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include <vtksys/SystemTools.hxx>
#include <cassert>
#include <vector>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkGESignaReader);
int vtkGESignaReader::CanReadFile(const char* fname)
{
FILE* fp = vtksys::SystemTools::Fopen(fname, "rb");
if (!fp)
{
return 0;
}
int magic;
if (fread(&magic, 4, 1, fp) != 1)
{
fclose(fp);
return 0;
}
vtkByteSwap::Swap4BE(&magic);
if (magic != 0x494d4746) // "IMGF"
{
fclose(fp);
return 0;
}
fclose(fp);
return 3;
}
void vtkGESignaReader::ExecuteInformation()
{
this->ComputeInternalFileName(this->DataExtent[4]);
if (this->InternalFileName == nullptr)
{
return;
}
FILE* fp = vtksys::SystemTools::Fopen(this->InternalFileName, "rb");
if (!fp)
{
vtkErrorMacro("Unable to open file " << this->InternalFileName);
return;
}
int magic;
if (fread(&magic, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading magic.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&magic);
if (magic != 0x494d4746)
{
vtkErrorMacro(<< "Unknown file type! Not a GE ximg file!");
fclose(fp);
return;
}
// read in the pixel offset from the header
int offset;
if (fread(&offset, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading pixel offset.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&offset);
this->SetHeaderSize(offset);
int width, height, depth;
if (fread(&width, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading width.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&width);
if (fread(&height, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading height.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&height);
// depth in bits
if (fread(&depth, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading depth.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&depth);
int compression;
if (fread(&compression, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading compression.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&compression);
// seek to the exam series and image header offsets
fseek(fp, 132, SEEK_SET);
int examHdrOffset;
if (fread(&examHdrOffset, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading exam header offset.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&examHdrOffset);
fseek(fp, 140, SEEK_SET);
int seriesHdrOffset;
if (fread(&seriesHdrOffset, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while series header offset.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&seriesHdrOffset);
fseek(fp, 148, SEEK_SET);
int imgHdrOffset;
if (fread(&imgHdrOffset, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while image header offset.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&imgHdrOffset);
// seek to the exam and read some info
char tmpStr[1024];
// suite ID
fseek(fp, examHdrOffset + 0, SEEK_SET);
if (fread(tmpStr, 4, 1, fp) != 1)
{
vtkErrorMacro(
"GESignaReader error reading file: " << this->FileName << " Premature EOF while suite ID.");
fclose(fp);
return;
}
tmpStr[4] = 0;
this->GetMedicalImageProperties()->SetStudyDescription(tmpStr); // StudyID would be more suited...
// exam number
fseek(fp, examHdrOffset + 8, SEEK_SET);
unsigned short examnumber;
if (fread(&examnumber, 2, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while exam number.");
fclose(fp);
return;
}
vtkByteSwap::Swap2BE(&examnumber);
snprintf(tmpStr, sizeof(tmpStr), "%d", examnumber);
// this->SetStudyNumber(tmpStr);
// Patient ID
fseek(fp, examHdrOffset + 84, SEEK_SET);
if (fread(tmpStr, 13, 1, fp) != 1)
{
vtkErrorMacro(
"GESignaReader error reading file: " << this->FileName << " Premature EOF while patient ID.");
fclose(fp);
return;
}
tmpStr[13] = 0;
this->SetPatientID(tmpStr);
// Patient Name
if (fread(tmpStr, 25, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading patient name.");
fclose(fp);
return;
}
tmpStr[25] = 0;
this->SetPatientName(tmpStr);
// Patient Age
fseek(fp, examHdrOffset + 122, SEEK_SET);
short patientage;
if (fread(&patientage, 2, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading patient age.");
fclose(fp);
return;
}
vtkByteSwap::Swap2BE(&patientage);
snprintf(tmpStr, sizeof(tmpStr), "%d", patientage);
this->GetMedicalImageProperties()->SetPatientAge(tmpStr);
// Patient Sex
fseek(fp, examHdrOffset + 126, SEEK_SET);
short patientsex;
if (fread(&patientsex, 2, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading patient sex.");
fclose(fp);
return;
}
vtkByteSwap::Swap2BE(&patientsex);
snprintf(tmpStr, sizeof(tmpStr), "%d", patientsex);
this->GetMedicalImageProperties()->SetPatientSex(tmpStr);
// Modality
fseek(fp, examHdrOffset + 305, SEEK_SET);
if (fread(tmpStr, 3, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading modality.");
fclose(fp);
return;
}
tmpStr[3] = 0;
this->SetModality(tmpStr);
// seek to the series and read some info
// series number
fseek(fp, seriesHdrOffset + 10, SEEK_SET);
short series;
if (fread(&series, 2, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading series.");
fclose(fp);
return;
}
vtkByteSwap::Swap2BE(&series);
snprintf(tmpStr, sizeof(tmpStr), "%d", series);
this->SetSeries(tmpStr);
// scan protocol name
fseek(fp, seriesHdrOffset + 92, SEEK_SET);
if (fread(tmpStr, 25, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading scan protocol.");
fclose(fp);
return;
}
tmpStr[25] = 0;
this->SetStudy(tmpStr); // ??
// now seek to the image header and read some values
float tmpX, tmpY, tmpZ;
float spacingX, spacingY, spacingZ;
fseek(fp, imgHdrOffset + 50, SEEK_SET);
if (fread(&spacingX, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading spacing x.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&spacingX);
if (fread(&spacingY, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading spacing y.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&spacingY);
fseek(fp, imgHdrOffset + 116, SEEK_SET);
if (fread(&spacingZ, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading spacing z.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&spacingZ);
// Slice Thickness
fseek(fp, imgHdrOffset + 26, SEEK_SET);
if (fread(&tmpZ, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: "
<< this->FileName << " Premature EOF while reading slice thickness.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&tmpZ);
spacingZ = spacingZ + tmpZ;
float origX, origY, origZ;
fseek(fp, imgHdrOffset + 154, SEEK_SET);
// read TLHC
if (fread(&origX, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading origX.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&origX);
if (fread(&origY, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading origY.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&origY);
if (fread(&origZ, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading origZ.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&origZ);
// read TRHC
if (fread(&tmpX, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading TRHC x.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&tmpX);
if (fread(&tmpY, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading TRCH y.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&tmpY);
if (fread(&tmpZ, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading TRCH z.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&tmpZ);
// compute BLHC = TLHC - TRHC + BRHC
origX = origX - tmpX;
origY = origY - tmpY;
origZ = origZ - tmpZ;
// read BRHC
if (fread(&tmpX, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading BRHC x.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&tmpX);
if (fread(&tmpY, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading BRHC y.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&tmpY);
if (fread(&tmpZ, 4, 1, fp) != 1)
{
vtkErrorMacro("GESignaReader error reading file: " << this->FileName
<< " Premature EOF while reading BRCH z.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&tmpZ);
// compute BLHC = TLHC - TRHC + BRHC
origX = origX + tmpX;
origY = origY + tmpY;
origZ = origZ + tmpZ;
/*
http://www.dclunie.com/medical-image-faq/html/part4.html#Signa5X
image header - for MR (1022 bytes long):
194 - int - repetition time(usec)
198 - int - inversion time(usec)
202 - int - echo time(usec)
210 - short - number of echoes
212 - short - echo number
218 - float - NEX
308 - char[33] - pulse sequence name
362 - char[17] - coil name
640 - short - ETL for FSE
*/
this->SetDataOrigin(origX, origY, origZ);
this->DataExtent[0] = 0;
this->DataExtent[1] = width - 1;
this->DataExtent[2] = 0;
this->DataExtent[3] = height - 1;
this->SetDataScalarTypeToUnsignedShort();
this->SetNumberOfScalarComponents(1);
this->SetDataSpacing(spacingX, spacingY, spacingZ);
this->vtkImageReader2::ExecuteInformation();
// close the file
fclose(fp);
}
static void vtkcopygenesisimage(FILE* infp, int width, int height, int compress, short* map_left,
short* map_wide, unsigned short* output)
{
unsigned short last_pixel = 0;
for (int row = 0; row < height; ++row)
{
unsigned short start;
unsigned short end;
if (compress == 2 || compress == 4)
{ // packed/compacked
assert(map_left);
assert(map_wide);
start = map_left[row];
end = start + map_wide[row];
}
else
{
start = 0;
end = width;
}
// Pad the first "empty" part of the line ...
for (unsigned short k = 0; k < start; k++)
{
(*output) = 0;
++output;
}
if (compress == 3 || compress == 4)
{ // compressed/compacked
while (start < end)
{
unsigned char byte;
if (!fread(&byte, 1, 1, infp))
{
return;
}
if (byte & 0x80)
{
unsigned char byte2;
if (!fread(&byte2, 1, 1, infp))
{
return;
}
if (byte & 0x40)
{ // next word
if (!fread(&byte, 1, 1, infp))
{
return;
}
last_pixel = (((unsigned short)byte2 << 8) + byte);
}
else
{ // 14 bit delta
if (byte & 0x20)
{
byte |= 0xe0;
}
else
{
byte &= 0x1f;
}
last_pixel += (((short)byte << 8) + byte2);
}
}
else
{ // 7 bit delta
if (byte & 0x40)
{
byte |= 0xc0;
}
last_pixel += (signed char)byte;
}
(*output) = last_pixel;
++output;
++start;
}
}
else
{
while (start < end)
{
unsigned short u;
if (!fread(&u, 2, 1, infp))
{
return;
}
vtkByteSwap::Swap2BE(&u);
(*output) = u;
++output;
++start;
}
}
// Pad the last "empty" part of the line ...
for (int j = end; j < width; j++)
{
(*output) = 0;
++output;
}
}
}
static void vtkGESignaReaderUpdate2(
vtkGESignaReader* self, unsigned short* outPtr, int* outExt, vtkIdType*)
{
FILE* fp = vtksys::SystemTools::Fopen(self->GetInternalFileName(), "rb");
if (!fp)
{
return;
}
int magic;
if (fread(&magic, 4, 1, fp) != 1)
{
vtkGenericWarningMacro("GESignaReader error reading file: "
<< self->GetInternalFileName() << " Premature EOF while reading magic.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&magic);
if (magic != 0x494d4746)
{
vtkGenericWarningMacro(<< "Unknown file type! Not a GE ximg file!");
fclose(fp);
return;
}
// read in the pixel offset from the header
int offset;
if (fread(&offset, 4, 1, fp) != 1)
{
vtkGenericWarningMacro("GESignaReader error reading file: "
<< self->GetInternalFileName() << " Premature EOF while reading origY.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&offset);
int width, height, depth;
if (fread(&width, 4, 1, fp) != 1)
{
vtkGenericWarningMacro("GESignaReader error reading file: "
<< self->GetInternalFileName() << " Premature EOF while reading width.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&width);
if (fread(&height, 4, 1, fp) != 1)
{
vtkGenericWarningMacro("GESignaReader error reading file: "
<< self->GetInternalFileName() << " Premature EOF while reading height.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&height);
// depth in bits
if (fread(&depth, 4, 1, fp) != 1)
{
vtkGenericWarningMacro("GESignaReader error reading file: "
<< self->GetInternalFileName() << " Premature EOF while reading depth.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&depth);
int compression;
if (fread(&compression, 4, 1, fp) != 1)
{
vtkGenericWarningMacro("GESignaReader error reading file: "
<< self->GetInternalFileName() << " Premature EOF while reading compression.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&compression);
std::vector<short> leftMap;
std::vector<short> widthMap;
if (compression == 2 || compression == 4)
{ // packed/compacked
leftMap.resize(height);
widthMap.resize(height);
fseek(fp, 64, SEEK_SET);
int packHdrOffset;
if (fread(&packHdrOffset, 4, 1, fp) != 1)
{
vtkGenericWarningMacro("GESignaReader error reading file: "
<< self->GetInternalFileName() << " Premature EOF while reading packHdrOffset.");
fclose(fp);
return;
}
vtkByteSwap::Swap4BE(&packHdrOffset);
// now seek to the pack header and read some values
fseek(fp, packHdrOffset, SEEK_SET);
// read in the maps
int i;
for (i = 0; i < height; i++)
{
if (fread(leftMap.data() + i, 2, 1, fp) != 1)
{
vtkGenericWarningMacro("GESignaReader error reading file: "
<< self->GetInternalFileName() << " Premature EOF while reading maps.");
fclose(fp);
return;
}
vtkByteSwap::Swap2BE(leftMap.data() + i);
if (fread(widthMap.data(), 2, 1, fp) != 1)
{
vtkGenericWarningMacro("GESignaReader error reading file: "
<< self->GetInternalFileName() << " Premature EOF while reading maps.");
fclose(fp);
return;
}
vtkByteSwap::Swap2BE(widthMap.data() + i);
}
}
// seek to pixel data
fseek(fp, offset, SEEK_SET);
// read in the pixels
std::vector<unsigned short> tmp(width * height);
int* dext = self->GetDataExtent();
vtkcopygenesisimage(
fp, dext[1] + 1, dext[3] + 1, compression, leftMap.data(), widthMap.data(), tmp.data());
// now copy into desired extent
int yp;
for (yp = outExt[2]; yp <= outExt[3]; ++yp)
{
int ymod = height - yp - 1;
memcpy(outPtr, tmp.data() + ymod * width + outExt[0], 2 * width);
outPtr = outPtr + width;
}
fclose(fp);
}
//------------------------------------------------------------------------------
// This function reads in one data of data.
// templated to handle different data types.
static void vtkGESignaReaderUpdate(
vtkGESignaReader* self, vtkImageData* data, unsigned short* outPtr)
{
vtkIdType outIncr[3];
int outExtent[6];
unsigned short* outPtr2;
data->GetExtent(outExtent);
data->GetIncrements(outIncr);
outPtr2 = outPtr;
int idx2;
for (idx2 = outExtent[4]; idx2 <= outExtent[5]; ++idx2)
{
self->ComputeInternalFileName(idx2);
// read in a PNG file
vtkGESignaReaderUpdate2(self, outPtr2, outExtent, outIncr);
self->UpdateProgress((idx2 - outExtent[4]) / (outExtent[5] - outExtent[4] + 1.0));
outPtr2 += outIncr[2];
}
}
//------------------------------------------------------------------------------
// This function reads a data from a file. The datas extent/axes
// are assumed to be the same as the file extent/order.
void vtkGESignaReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInformation* outInfo)
{
vtkImageData* data = this->AllocateOutputData(output, outInfo);
if (this->InternalFileName == nullptr)
{
vtkErrorMacro(<< "Either a FileName or FilePrefix must be specified.");
return;
}
data->GetPointData()->GetScalars()->SetName("GESignalImage");
this->ComputeDataIncrements();
// Call the correct templated function for the output
void* outPtr;
// Call the correct templated function for the input
outPtr = data->GetScalarPointer();
vtkGESignaReaderUpdate(this, data, (unsigned short*)(outPtr));
}
//------------------------------------------------------------------------------
void vtkGESignaReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
VTK_ABI_NAMESPACE_END
|