vtkMINCImageAttributes.cxx 41.5 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkMINCImageAttributes.cxx

6 7 8 9 10 11 12 13 14 15 16
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm 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.

=========================================================================*/
/*=========================================================================

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
Copyright (c) 2006 Atamai, Inc.

Use, modification and redistribution of the software, in source or
binary forms, are permitted provided that the following terms and
conditions are met:

1) Redistribution of the source code, in verbatim or modified
   form, must retain the above copyright notice, this license,
   the following disclaimer, and any notices that refer to this
   license and/or the following disclaimer.

2) Redistribution in binary form must include the above copyright
   notice, a copy of this license and the following disclaimer
   in the documentation or with other materials provided with the
   distribution.

3) Modified copies of the source code must be clearly marked as such,
   and must not be misrepresented as verbatim copies of the source code.

THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS"
WITHOUT EXPRESSED OR IMPLIED WARRANTY INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  IN NO EVENT SHALL ANY COPYRIGHT HOLDER OR OTHER PARTY WHO MAY
MODIFY AND/OR REDISTRIBUTE THE SOFTWARE UNDER THE TERMS OF THIS LICENSE
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR DATA BECOMING INACCURATE
OR LOSS OF PROFIT OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF
THE USE OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

=========================================================================*/

#include "vtkMINCImageAttributes.h"

#include "vtkObjectFactory.h"

#include "vtkStringArray.h"
#include "vtkCharArray.h"
#include "vtkSignedCharArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkShortArray.h"
#include "vtkIntArray.h"
#include "vtkFloatArray.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkSmartPointer.h"

#include "vtkMINC.h"
65
#include "vtk_netcdf.h"
66

67
#include <cstdlib>
68 69
#include <cctype>
#include <cfloat>
70 71
#include <string>
#include <map>
72
#include <sstream>
73

74 75 76 77
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif

78 79 80 81 82
//-------------------------------------------------------------------------
// A container for mapping attribute names to arrays
class vtkMINCImageAttributeMap
{
public:
83
  typedef std::map<std::string, vtkSmartPointer<vtkObject> > MapType;
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

  static vtkMINCImageAttributeMap *New() {
    return new vtkMINCImageAttributeMap; };

  void Delete() {
    delete this; };

  void Clear() {
    this->Map.clear(); };

  void AddArray(vtkDataArray *array) {
    this->AddObject(array->GetName(), array); };

  void AddArray(vtkStringArray *array) {
    this->AddObject(array->GetName(), array); };

  vtkDataArray *GetDataArray(const char *name) const {
    return vtkDataArray::SafeDownCast(this->GetObject(name)); };

  vtkCharArray *GetCharArray(const char *name) const {
    return vtkCharArray::SafeDownCast(this->GetObject(name)); };

  vtkDoubleArray *GetDoubleArray(const char *name) const {
    return vtkDoubleArray::SafeDownCast(this->GetObject(name)); };

  vtkStringArray *GetStringArray(const char *name) const {
    return vtkStringArray::SafeDownCast(this->GetObject(name)); };

protected:
  void AddObject(const char *name, vtkObject *object) {
    this->Map[name] = object; };

  vtkObject *GetObject(const char *name) const {
    MapType::const_iterator iter = this->Map.find(name);
    if (iter != this->Map.end()) { return iter->second; };
    return 0; };

private:
  vtkMINCImageAttributeMap() : Map() {};
  ~vtkMINCImageAttributeMap() {};

  MapType Map;
};

//--------------------------------------------------------------------------
vtkStandardNewMacro(vtkMINCImageAttributes);

vtkCxxSetObjectMacro(vtkMINCImageAttributes,ImageMin,vtkDoubleArray);
vtkCxxSetObjectMacro(vtkMINCImageAttributes,ImageMax,vtkDoubleArray);

//-------------------------------------------------------------------------
vtkMINCImageAttributes::vtkMINCImageAttributes()
{
  this->DimensionNames = vtkStringArray::New();
  this->DimensionLengths = vtkIdTypeArray::New();

  this->VariableNames = vtkStringArray::New();

  this->AttributeNames = vtkMINCImageAttributeMap::New();
143 144 145 146 147 148
  // Add global attribute name array
  vtkStringArray *tmparray = vtkStringArray::New();
  tmparray->SetName("");
  this->AttributeNames->AddArray(tmparray);
  tmparray->Delete();

149 150 151 152 153 154 155 156
  this->AttributeValues = vtkMINCImageAttributeMap::New();
  this->StringStore = 0;

  this->NumberOfImageMinMaxDimensions = 0;
  this->ImageMin = 0;
  this->ImageMax = 0;

  this->Name = 0;
157
  this->DataType = VTK_VOID;
158 159

  this->ValidateAttributes = 1;
160 161 162 163 164 165
}

//-------------------------------------------------------------------------
vtkMINCImageAttributes::~vtkMINCImageAttributes()
{
  this->SetName(0);
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
  if (this->DimensionNames)
    {
    this->DimensionNames->Delete();
    this->DimensionNames = 0;
    }
  if (this->DimensionLengths)
    {
    this->DimensionLengths->Delete();
    this->DimensionLengths = 0;
    }
  if (this->VariableNames)
    {
    this->VariableNames->Delete();
    this->VariableNames = 0;
    }
  if (this->AttributeNames)
    {
    this->AttributeNames->Delete();
    this->AttributeNames = 0;
    }
  if (this->AttributeValues)
    {
    this->AttributeValues->Delete();
    this->AttributeValues = 0;
    }
  if (this->ImageMin)
    {
    this->ImageMin->Delete();
    this->ImageMin = 0;
    }
  if (this->ImageMax)
    {
    this->ImageMax->Delete();
    this->ImageMax = 0;
    }
  if (this->StringStore)
    {
    this->StringStore->Delete();
    this->StringStore = 0;
    }
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

214
  os << indent << "Name: "
215 216 217 218 219 220 221
     << (this->Name ? this->Name : "(none)") << "\n";
  os << indent << "DataType: " << this->DataType << "\n";
  os << indent << "DimensionNames: " << this->DimensionNames << "\n";
  os << indent << "DimensionLengths: " << this->DimensionLengths << "\n";
  os << indent << "VariableNames: " << this->VariableNames << "\n";
  os << indent << "ImageMin: " << this->ImageMin << "\n";
  os << indent << "ImageMax: " << this->ImageMax << "\n";
222
  os << indent << "NumberOfImageMinMaxDimensions: "
223
     << this->NumberOfImageMinMaxDimensions << "\n";
224 225
  os << indent << "ValidateAttributes: "
     << (this->ValidateAttributes ? "On\n" : "Off\n");
226 227 228 229 230 231
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::Reset()
{
  this->SetName(0);
232
  this->SetDataType(VTK_VOID);
233 234 235 236 237 238 239 240 241 242 243 244
  this->SetImageMin(0);
  this->SetImageMax(0);
  this->AttributeValues->Clear();
  this->AttributeNames->Clear();
  this->VariableNames->Reset();
  this->DimensionNames->Reset();
  this->DimensionLengths->Reset();
  if (this->StringStore)
    {
    this->StringStore->Reset();
    }
  this->NumberOfImageMinMaxDimensions = 0;
245 246 247 248 249 250

  // Add global attribute name array
  vtkStringArray *tmparray = vtkStringArray::New();
  tmparray->SetName("");
  this->AttributeNames->AddArray(tmparray);
  tmparray->Delete();
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
}

//-------------------------------------------------------------------------
// Allowed dimension variable names
static const char *vtkMINCDimVarNames[] = {
  MIxspace, MIyspace, MIzspace, MItime,
  MIxfrequency, MIyfrequency, MIzfrequency, MItfrequency,
  0
};

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::AddDimension(const char *dimension,
                                          vtkIdType length)
{
  // Check for duplicates
266 267
  vtkIdType n = this->DimensionNames->GetNumberOfValues();
  for (vtkIdType i = 0; i < n; i++)
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
    {
    if (strcmp(dimension, this->DimensionNames->GetValue(i)) == 0)
      {
      vtkErrorMacro("The dimension " << dimension <<
                    " has already been created.");
      return;
      }
    }

  // Ensure the dimension name is valid
  const char **tryname = 0;
  for (tryname = vtkMINCDimVarNames; *tryname != 0; tryname++)
    {
    if (strcmp(dimension, *tryname) == 0)
      {
      break;
      }
    }
  if (*tryname == 0 && strcmp(dimension, MIvector_dimension) != 0)
    {
    vtkWarningMacro("The dimension name " << dimension <<
                    " is not recognized.");
    }

  this->DimensionNames->InsertNextValue(dimension);
  this->DimensionLengths->InsertNextTuple1(length);
}

//-------------------------------------------------------------------------
// This method also has to store the resulting string internally.
const char *vtkMINCImageAttributes::ConvertDataArrayToString(
  vtkDataArray *array)
{
301 302 303 304 305 306
  const char *result = "";
  vtkIdType n = array->GetNumberOfTuples();
  if (n == 0)
    {
    return result;
    }
307

308 309 310 311
  int dataType = array->GetDataType();
  if (dataType == VTK_CHAR)
    {
    vtkCharArray *charArray = vtkCharArray::SafeDownCast(array);
312
    if (charArray)
313
      {
314 315 316 317 318 319 320 321
      result = charArray->GetPointer(0);
      // Check to see if string has a terminal null (the null might be
      // part of the attribute, or stored in the following byte)
      if ((n > 0 && result[n-1] == '\0') ||
          (charArray->GetSize() > n && result[n] == '\0'))
        {
        return result;
        }
322
      }
323 324
    }

325
  std::ostringstream os;
326

327
  for (vtkIdType i = 0; i < n; i++)
328 329 330 331 332 333 334 335
    {
    double val = array->GetComponent(i, 0);
    if (dataType == VTK_DOUBLE || dataType == VTK_FLOAT)
      {
      // Use NetCDF's usual precision for printing the values
      char storage[128];
      if (dataType == VTK_DOUBLE)
        {
336
        snprintf(storage, 128, "%0.15g", val);
337 338 339
        }
      else
        {
340
        snprintf(storage, 128, "%0.7g", val);
341 342 343 344 345 346 347 348 349
        }
      // Add a decimal if there isn't one, to distinguish from int
      for (char *cp = storage; *cp != '.'; cp++)
        {
        if (*cp == '\0')
          {
          *cp++ = '.';
          *cp = '\0';
          break;
350
          }
351 352 353
        }
      os << storage;
      }
354 355 356 357
    else if (dataType == VTK_CHAR)
      {
      os.put(static_cast<char>(val));
      }
358 359 360 361
    else
      {
      os << val;
      }
362
    if (i < n-1 && dataType != VTK_CHAR)
363 364 365 366 367
      {
      os << ", ";
      }
    }

368 369
  // Store the string
  std::string str = os.str();
370

371 372 373 374
  if (this->StringStore == 0)
    {
    this->StringStore = vtkStringArray::New();
    }
375

376 377 378 379 380 381 382
  // See if the string is already stored
  vtkIdType m = this->StringStore->GetNumberOfValues();
  vtkIdType j;
  for (j = 0; j < m; j++)
    {
    result = this->StringStore->GetValue(j);
    if (strcmp(str.c_str(), result) == 0)
383
      {
384
      break;
385
      }
386 387 388 389 390 391 392
    }
  // If not, add it to the array.
  if (j == m)
    {
    j = this->StringStore->InsertNextValue(str.c_str());
    result = this->StringStore->GetValue(j);
    }
393

394
  return result;
395 396 397
}

//-------------------------------------------------------------------------
398
void vtkMINCImageAttributes::PrintFileHeader()
399
{
400
  this->PrintFileHeader(cout);
401 402 403
}

//-------------------------------------------------------------------------
404
void vtkMINCImageAttributes::PrintFileHeader(ostream &os)
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
{
  const char *name = "unknown";
  if (this->Name)
    {
    name = this->Name;
    }

  // Get the data type
  const char *imageDataType;
  switch (this->DataType)
    {
    case VTK_CHAR:
    case VTK_SIGNED_CHAR:
    case VTK_UNSIGNED_CHAR:
      imageDataType = "byte";
      break;
    case VTK_SHORT:
    case VTK_UNSIGNED_SHORT:
      imageDataType = "short";
      break;
    case VTK_INT:
    case VTK_UNSIGNED_INT:
      imageDataType = "int";
      break;
    case VTK_FLOAT:
      imageDataType = "float";
      break;
    case VTK_DOUBLE:
      imageDataType = "double";
      break;
    default:
      imageDataType = "void";
    }

  os << "netcdf " << name << " {\n";
  os << "dimensions:\n";
441

442
  vtkIdType ndim = 0;
443 444 445 446
  if (this->DimensionNames)
    {
    ndim = this->DimensionNames->GetNumberOfValues();
    }
447
  for (vtkIdType idim = 0; idim < ndim; idim++)
448 449 450 451 452 453 454
    {
    os << "\t" << this->DimensionNames->GetValue(idim) << " = "
       << this->DimensionLengths->GetValue(idim) << " ;\n";
    }

  os << "variables:\n";

455 456
  vtkIdType nvar = 0;
  vtkIdType ivar = 0;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
  if (this->VariableNames)
    {
    nvar = this->VariableNames->GetNumberOfValues();
    }
  for (ivar = 0; ivar < nvar+1; ivar++)
    {
    const char *varname = MI_EMPTY_STRING;
    if (ivar == nvar)
      {
      os << "\n// global attributes:\n";
      }
    else
      {
      varname = this->VariableNames->GetValue(ivar);
      if (strcmp(varname, MIimage) == 0 ||
          strcmp(varname, MIimagemax) == 0 ||
          strcmp(varname, MIimagemin) == 0)
        {
        os << "\t" << imageDataType << " " << varname;
476
        vtkIdType nvardim = this->DimensionNames->GetNumberOfValues();
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
        // If this is image-min or image-max, only print the
        // dimensions for these variables
        if (varname[5] == '-')
          {
          if (this->NumberOfImageMinMaxDimensions < nvardim)
            {
            nvardim = this->NumberOfImageMinMaxDimensions;
            }
          }

        if (nvardim > 0)
          {
          os << "(";
          for (int ivardim = 0; ivardim < nvardim; ivardim++)
            {
            os << this->DimensionNames->GetValue(ivardim);
            if (ivardim < nvardim - 1)
              {
              os << ", ";
              }
            }
          os << ")";
          }
        os << " ;\n";
        }
      else
        {
        os << "\t" << "int " << varname << " ;\n";
        }
      }
    vtkStringArray *attArray =
      this->AttributeNames->GetStringArray(varname);
    if (attArray)
      {
511 512
      vtkIdType natt = attArray->GetNumberOfValues();
      for (vtkIdType iatt = 0; iatt < natt; iatt++)
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
        {
        const char *attname = attArray->GetValue(iatt);
        vtkDataArray *array =
          this->GetAttributeValueAsArray(varname, attname);
        os << "\t\t" << varname << ":" << attname << " = ";
        if (array->GetDataType() == VTK_CHAR)
          {
          vtkCharArray *charArray =
            vtkCharArray::SafeDownCast(array);
          os << "\"";
          const char *cp = charArray->GetPointer(0);
          const char *endcp = cp + charArray->GetNumberOfTuples();
          char text[512];
          text[0] = '\0';
          while (cp < endcp)
            {
            int c = 0;
            int j;
            for (j = 0; j < 508 && cp < endcp; j++, cp++)
              {
              c = *cp;
              if (c == '\0' && (cp + 1) == endcp)
                {
                // break if at terminal null
                cp++;
                break;
                }
              if (isprint(c) && c != '\\')
                {
                text[j] = c;
                }
              else
                {
                // quote the non-printing characters
                switch (c)
                  {
                  case '\\':
                    text[j++] = '\\';
                    text[j] = '\\';
                    break;
                  case '\b':
                    text[j++] = '\\';
                    text[j] = '\b';
556
                    break;
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
                  case '\n':
                    text[j++] = '\\';
                    text[j] = 'n';
                    break;
                  case '\r':
                    text[j++] = '\\';
                    text[j] = 'r';
                    break;
                  case '\f':
                    text[j++] = '\\';
                    text[j] = 'f';
                    break;
                  case '\v':
                    text[j++] = '\\';
                    text[j] = 'v';
                    break;
                  case '\'':
                    text[j++] = '\\';
                    text[j] = '\'';
                    break;
                  case '\"':
                    text[j++] = '\\';
                    text[j] = '\"';
                    break;
                  default:
                    text[j++] = '\\';
                    text[j++] = '0' + ((c & 0xc0) >> 6);
                    text[j++] = '0' + ((c & 0x38) >> 3);
                    text[j] = '0' + (c & 0x7);
                    break;
                  }
                }
              if (c == '\n')
                {
                j++;
                cp++;
                break;
                }
              }

            text[j] = '\0';
            os << text;
            text[0] = '\0';

            // Start a new string after each newline, unless this
            // newline is the final character.
            if (c == '\n' && cp < endcp)
              {
              os << "\",\n\t\t\t\"";
              }
            }
          os << "\" ;\n";
          }
        else
          {
          // Use handy conversion method
          os << this->ConvertDataArrayToString(array) << " ;\n";
          }
        }
      }
    }

  os << "data:\n";

  if (this->VariableNames)
    {
    nvar = this->VariableNames->GetNumberOfValues();
    }
  for (ivar = 0; ivar < nvar; ivar++)
    {
    const char *varname = this->VariableNames->GetValue(ivar);

    if (strcmp(varname, MIimage) == 0)
      {
      continue;
      }

    os << "\n " << varname << " = ";

    if (strcmp(varname, MIimagemin) == 0)
      {
      if (this->ImageMin)
        {
        os << this->ConvertDataArrayToString(this->ImageMin) << " ;\n";
        }
      else
        {
        os << "0. ;\n";
        }
      }
    else if (strcmp(varname, MIimagemax) == 0)
      {
      if (this->ImageMax)
        {
        os << this->ConvertDataArrayToString(this->ImageMax) << " ;\n";
        }
      else
        {
        os << "1. ;\n";
        }
      }
    else
      {
660
      os << "_ ;\n";
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
      }
    }

  os << "}\n";
}

//-------------------------------------------------------------------------
vtkStringArray *vtkMINCImageAttributes::GetAttributeNames(
  const char *variable)
{
  // If variable is null, use empty string to get global attributes
  if (variable == 0)
    {
    variable = MI_EMPTY_STRING;
    }

  return this->AttributeNames->GetStringArray(variable);
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::HasAttribute(
  const char *variable,
  const char *attribute)
{
  return (this->GetAttributeValueAsArray(variable, attribute) != 0);
}

//-------------------------------------------------------------------------
vtkDataArray *vtkMINCImageAttributes::GetAttributeValueAsArray(
  const char *variable,
  const char *attribute)
{
693
  std::string path = MI_GRPNAME;
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
  if (variable && variable[0] != '\0')
    {
    path += MI_GRP_SEP;
    path += variable;
    }
  path += MI_ATT_SEP;
  path += attribute;

  return this->AttributeValues->GetDataArray(path.c_str());
}

//-------------------------------------------------------------------------
const char *vtkMINCImageAttributes::GetAttributeValueAsString(
  const char *variable,
  const char *attribute)
{
  vtkDataArray *array =
    this->GetAttributeValueAsArray(variable, attribute);

  // Return NULL if not found
  if (array == 0)
    {
    return 0;
    }

719
  // Convert any other array to a string.
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
  return this->ConvertDataArrayToString(array);
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::GetAttributeValueAsInt(
  const char *variable,
  const char *attribute)
{
  vtkDataArray *array = this->GetAttributeValueAsArray(variable, attribute);

  if (array == 0)
    {
    vtkErrorMacro("The attribute " << variable << ":"
                  << attribute << " was not found.");
    return 0;
    }

  if (array->GetDataType() == VTK_CHAR)
    {
    char *text = vtkCharArray::SafeDownCast(array)->GetPointer(0);
740
    if (text)
741
      {
742 743 744 745 746 747 748
      char *endp = text;
      long result = strtol(text, &endp, 10);
      // Check for complete conversion
      if (*endp == '\0' && *text != '\0')
        {
        return static_cast<int>(result);
        }
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
      }
    }
  else if (array->GetNumberOfTuples() == 1)
    {
    switch(array->GetDataType())
      {
      case VTK_SIGNED_CHAR:
      case VTK_UNSIGNED_CHAR:
      case VTK_SHORT:
      case VTK_INT:
        return static_cast<int>(array->GetComponent(0,0));
      default:
        break;
      }
    }

  vtkErrorMacro("GetAttributeValueAsInt() used on non-integer attribute "
                << variable << ":" << attribute <<".");
  return static_cast<int>(array->GetComponent(0,0));
}

//-------------------------------------------------------------------------
double vtkMINCImageAttributes::GetAttributeValueAsDouble(
  const char *variable,
  const char *attribute)
{
  if (variable == 0)
    {
    variable = MI_EMPTY_STRING;
    }

  vtkDataArray *array = this->GetAttributeValueAsArray(variable, attribute);

  if (array == 0)
    {
    vtkErrorMacro("The attribute " << variable << ":"
                  << attribute << " was not found.");
    return 0;
    }

  if (array->GetDataType() == VTK_CHAR)
    {
    char *text = vtkCharArray::SafeDownCast(array)->GetPointer(0);
792
    if (text)
793
      {
794 795 796 797 798 799 800
      char *endp = text;
      double result = strtod(text, &endp);
      // Check for complete conversion
      if (*endp == '\0' && *text != '\0')
        {
        return result;
        }
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
      }
    }
  else if (array->GetNumberOfTuples() == 1)
    {
    switch(array->GetDataType())
      {
      case VTK_SIGNED_CHAR:
      case VTK_UNSIGNED_CHAR:
      case VTK_SHORT:
      case VTK_INT:
      case VTK_FLOAT:
      case VTK_DOUBLE:
        return array->GetComponent(0,0);
      default:
        break;
      }
    }

  vtkErrorMacro("GetAttributeValueAsDouble() used on non-real attribute "
                << variable << ":" << attribute <<".");
  return array->GetComponent(0,0);
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::SetAttributeValueAsArray(
  const char *variable,
  const char *attribute,
  vtkDataArray *array)
{
830
  std::string path = MI_GRPNAME;
831 832 833 834 835 836 837 838 839 840 841 842
  if (variable && variable[0] != '\0')
    {
    path += MI_GRP_SEP;
    path += variable;
    }
  path += MI_ATT_SEP;
  path += attribute;

  array->SetName(path.c_str());
  this->AttributeValues->AddArray(array);

  // Add to variable to VariableNames
843 844
  vtkIdType n = this->VariableNames->GetNumberOfValues();
  vtkIdType i = 0;
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
  for (i = 0; i < n; i++)
    {
    if (strcmp(this->VariableNames->GetValue(i), variable) == 0)
      {
      break;
      }
    }
  if (i == n && variable[0] != '\0')
    {
    this->VariableNames->InsertNextValue(variable);
    }

  // Add to attribute to AttributeNames
  vtkStringArray *attribs = this->AttributeNames->GetStringArray(variable);
  // Create a new array if necessary
  if (attribs == 0)
    {
    attribs = vtkStringArray::New();
    attribs->SetName(variable);
    this->AttributeNames->AddArray(attribs);
    attribs->Delete();
    }

  n = attribs->GetNumberOfValues();
  for (i = 0; i < n; i++)
    {
    if (strcmp(attribs->GetValue(i), attribute) == 0)
      {
      break;
      }
    }
  if (i == n)
    {
    attribs->InsertNextValue(attribute);
    }

881 882 883 884 885 886 887 888 889 890 891
  if (this->ValidateAttributes)
    {
    // Print warning if there is something wrong with the attribute
    int result = this->ValidateAttribute(variable, attribute, array);

    if (result > 1)
      {
      vtkWarningMacro("Attribute " << variable << ":" << attribute
                      << " is not a valid attribute.");
      }
    }
892 893 894 895 896 897 898 899
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::SetAttributeValueAsString(
  const char *variable,
  const char *attribute,
  const char *value)
{
900
  size_t length = strlen(value);
901 902

  vtkCharArray *array = vtkCharArray::New();
903 904 905
  // Allocate an extra byte to store a null terminator.
  array->Resize(length + 1);
  char *dest = array->WritePointer(0, length);
906 907
  strncpy(dest, value, length);
  dest[length] = '\0';
908
  this->SetAttributeValueAsArray(variable, attribute, array);
David Gobbi's avatar
David Gobbi committed
909 910

  array->Delete();
911 912 913 914 915 916 917 918 919 920 921 922 923
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::SetAttributeValueAsInt(
  const char *variable,
  const char *attribute,
  int value)
{
  vtkIntArray *array = vtkIntArray::New();
  array->SetNumberOfValues(1);
  array->SetValue(0, value);

  this->SetAttributeValueAsArray(variable, attribute, array);
David Gobbi's avatar
David Gobbi committed
924 925

  array->Delete();
926 927 928 929 930 931 932 933 934 935 936 937 938
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::SetAttributeValueAsDouble(
  const char *variable,
  const char *attribute,
  double value)
{
  vtkDoubleArray *array = vtkDoubleArray::New();
  array->SetNumberOfValues(1);
  array->SetValue(0, value);

  this->SetAttributeValueAsArray(variable, attribute, array);
David Gobbi's avatar
David Gobbi committed
939 940

  array->Delete();
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
}

//-------------------------------------------------------------------------
// These validation methods have three return values:
// 0 means that the attribute should be skipped
// 1 means that the attribute should be set
// 2 means that the attribute wasn't recognized

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::ValidateGlobalAttribute(
  const char *attname, vtkDataArray *vtkNotUsed(array))
{
  // Global attributes
  static const char *globalAttributes[] = {
    MIident,
    MIhistory,
    MItitle,
    0
  };
  const int autoGlobalAttributes = 2;

  int itry = 0;
  for (itry = 0; globalAttributes[itry] != 0; itry++)
    {
    if (strcmp(attname, globalAttributes[itry]) == 0)
      {
      break;
      }
    }
  if (itry < autoGlobalAttributes)
    {
    // Skip to the next attribute
    return 0;
    }
  else if (globalAttributes[itry] == 0)
    {
    return 2;
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::ValidateGeneralAttribute(
  const char *varname, const char *attname, vtkDataArray *array)
{
  // Attributes that all MINC variables have
  static const char *generalAttributes[] = {
    MIvartype,      // MI_GROUP, MI_DIMENSION, MI_VARATT
    MIvarid,        // MI_STDVAR
    MIversion,      // MI_VERSION_1_0
    MIparent,       // parent variable for this variable
    MIchildren,     // newline-separated list of child variables
    MIcomments,     // each variable has specific comments to go with it
    0
  };
  const int autoGeneralAttributes = 5;

  int dataType = array->GetDataType();

  // Check to see if the attribute is one that we automatically generate.
  int itry = 0;
  for (itry = 0; generalAttributes[itry] != 0; itry++)
    {
    if (strcmp(attname, generalAttributes[itry]) == 0)
      {
      break;
      }
    }
  if (itry < autoGeneralAttributes)
    {
    // Skip to the next attribute
    return 0;
    }
  else if (generalAttributes[itry] != 0)
    {
    if (dataType != VTK_CHAR)
      {
      vtkWarningMacro("The attribute " << varname << ":"
                      << attname << " has the wrong type ("
                      << dataType << ").");
      return 0;
      }
    }
  else
    {
    return 2;
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::ValidateDimensionAttribute(
  const char *varname, const char *attname, vtkDataArray *array)
{
  // Attributes for dimension variables (vartype = MI_DIMENSION)
  static const char *dimensionAttributes[] = {
    MIstep,
    MIstart,
    MIspacing,      // MI_REGULAR ("irregular" not supported)
    MIspacetype,    // "native____", "talairach_", "calossal__"
    MIalignment,    // MI_CENTRE ("start_", "end___" not supported)
    MIunits,        // "mm"
    MIdirection_cosines,  // three doubles
    0
  };
  const int autoDimensionAttributes = 3;

  vtkIdType size = (array->GetNumberOfTuples()*
                    array->GetNumberOfComponents());
  int dataType = array->GetDataType();

  int itry = 0;
  for (itry = 0; dimensionAttributes[itry] != 0; itry++)
    {
    if (strcmp(attname, dimensionAttributes[itry]) == 0)
      {
      break;
      }
    }
  if (itry < autoDimensionAttributes)
    {
    // Skip to the next attribute
    return 0;
    }
  else if (strcmp(attname, MIdirection_cosines) == 0)
    {
    if (varname[0] == 'x' || varname[0] == 'y' || varname[0] == 'z')
      {
      if (dataType != VTK_DOUBLE || size != 3)
        {
        vtkWarningMacro("The attribute " << varname << ":"
                        << attname << " has the wrong type ("
                        << dataType << ") or size ("
                        << size << ").");
        return 0;
        }
      }
    else
      {
      vtkWarningMacro("Dimension " << varname << " cannot have"
                      " a direction_cosines attribute");
      return 0;
      }
    }
  else if (dimensionAttributes[itry] != 0)
    {
    if (dataType != VTK_CHAR)
      {
      vtkWarningMacro("The attribute " << varname << ":"
                      << attname << " has the wrong type ("
                      << dataType << ").");
      return 0;
      }
    }
  else
    {
    return 2;
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::ValidateImageAttribute(
  const char *vtkNotUsed(varname), const char *attname,
  vtkDataArray *vtkNotUsed(array))
{
  // Attributes for the MIimage variable (vartype = MI_GROUP)
  static const char *imageAttributes[] = {
    MIcomplete,    // MI_TRUE (MI_FALSE means not yet all written)
    MIimagemin     // "--->image-min" variable attribute pointer
    MIimagemax     // "--->image-max" variable attribute pointer
    MIsigntype,     // MI_SIGNED or MI_UNSIGNED
    MIvalid_range,  // min and max scalar values as doubles
    0
  };
  const int autoImageAttributes = 5;

  int itry = 0;
  for (itry = 0; imageAttributes[itry] != 0; itry++)
    {
    if (strcmp(attname, imageAttributes[itry]) == 0)
      {
      break;
      }
    }
  if (itry < autoImageAttributes)
    {
    // Skip to the next attribute
    return 0;
    }
  else if (imageAttributes[itry] == 0)
    {
    return 2;
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::ValidateImageMinMaxAttribute(
  const char *varname, const char *attname, vtkDataArray *array)
{
  // Attributes for MIimagemin, MIimagemax (vartype = MI_VARATT)
  static const char *imageMinMaxAttributes[] = {
     MI_FillValue,  // 0.0 for image-min, 1.0 for image-max
     MIunits,       // "normalized", "Hounsfields", etc.
     0
  };
  const int autoImageMinMaxAttributes = 1;

  int itry = 0;
  for (itry = 0; imageMinMaxAttributes[itry] != 0; itry++)
    {
    if (strcmp(attname, imageMinMaxAttributes[itry]) == 0)
      {
      break;
      }
    }
  if (itry < autoImageMinMaxAttributes)
    {
    // Skip to the next attribute
    return 0;
    }
  else if (imageMinMaxAttributes[itry] != 0)
    {
    int dataType = array->GetDataType();
    if (dataType != VTK_CHAR)
      {
      vtkWarningMacro("The attribute " << varname << ":"
                      << attname << " has the wrong type ("
                      << dataType << ").");
      return 0;
      }
    }
  else
    {
    return 2;
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::ValidatePatientAttribute(
  const char *vtkNotUsed(varname), const char *attname,
  vtkDataArray *vtkNotUsed(array))
{
  // Attributes for MIpatient variable (vartype = MI_GROUP)
  static const char *patientAttributes[] = {
    MIfull_name,     // "LASTNAME^FIRSTNAME SECONDNAME"
    MIother_names,   // newline-separated string
    MIidentification,
    MIother_ids,
    MIbirthdate,     // "YYYYMMDD"
    MIsex,           // "male__", "female", "other_"
    MIage,           // "XXXD", "XXXM", or "XXXY" (days, months, years)
    MIweight,        // "XXkg", "X.Xkg" (assume kg if no units given)
    MIsize,          // "XXXcm" (assume metres if no units given)
    MIaddress,       // newline-separated string
    MIinsurance_id,
    0
  };

  int itry = 0;
  for (itry = 0; patientAttributes[itry] != 0; itry++)
    {
    if (strcmp(attname, patientAttributes[itry]) == 0)
      {
      break;
      }
    }
  if (patientAttributes[itry] != 0)
    {
    // Add checks for correct data type?
    }
  else
    {
    return 2;
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::ValidateStudyAttribute(
  const char *vtkNotUsed(varname), const char *attname,
  vtkDataArray *vtkNotUsed(array))
{
  // Attributes for MIstudy variable (vartype = MI_GROUP)
  static const char *studyAttributes[] = {
    MIstudy_id,
    MIstart_time,    // "YYYYMMDDHHMMSS.SS"
    MIstart_year,    // as int (use start_time instead)
    MIstart_month,   // as int (use start_time instead)
    MIstart_day,     // as int (use start_time instead)
    MIstart_hour,    // as int (use start_time instead)
    MIstart_minute,  // as int (use start_time instead)
    MIstart_seconds, // as double or int (use start_time instead)
    MImodality,      // "PET__", "SPECT", "GAMMA", "MRI__", "MRS__",
                     // "MRA__", "CT___", "DSA__", "DR___", "label"
    MImanufacturer,
    MIdevice_model,
    MIinstitution,
    MIdepartment,
    MIstation_id,
    MIreferring_physician,
    MIattending_physician,
    MIradiologist,
    MIoperator,
    MIadmitting_diagnosis,
    MIprocedure,
    0
  };

  int itry = 0;
  for (itry = 0; studyAttributes[itry] != 0; itry++)
    {
    if (strcmp(attname, studyAttributes[itry]) == 0)
      {
      break;
      }
    }
  if (studyAttributes[itry] != 0)
    {
    // Add checks for correct data type?
    }
  else
    {
    return 2;
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::ValidateAcquisitionAttribute(
  const char *vtkNotUsed(varname), const char *attname,
  vtkDataArray *vtkNotUsed(array))
{
  // Attributes for MIacquisition variable (vartype = MI_GROUP)
  static const char *acquisitionAttributes[] = {
    MIprotocol,
    MIscanning_sequence, // "GR", "SPGR", etc.
    MIrepetition_time,   // as double, milliseconds
    MIecho_time,         // as double, milliseconds
    MIinversion_time,    // as double, milliseconds
    MInum_averages,      // as int
    MIimaging_frequency, // in Hz, as double
    MIimaged_nucleus,    // "H1", "C13", etc. for MRI
    MIradionuclide,      // for PET and SPECT
    MIradionuclide_halflife,
    MIcontrast_agent,
    MItracer,
    MIinjection_time,
    MIinjection_year,
    MIinjection_month,
    MIinjection_day,
    MIinjection_hour,
    MIinjection_minute,
    MIinjection_seconds,
    MIinjection_length,
    MIinjection_dose,
    MIdose_units,
    MIinjection_volume,
    MIinjection_route,
    0
  };

  int itry = 0;
  for (itry = 0; acquisitionAttributes[itry] != 0; itry++)
    {
    if (strcmp(attname, acquisitionAttributes[itry]) == 0)
      {
      break;
      }
    }
  if (acquisitionAttributes[itry] != 0)
    {
    // Add checks for correct data type?
    }
  else
    {
    return 2;
    }

  return 1;
}

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::ValidateAttribute(
  const char *varname, const char *attname, vtkDataArray *array)
{
  // Standard variable names
  static const char *stdVarNames[] = {
    MIrootvariable, MIimage, MIimagemin, MIimagemax,
    MIpatient, MIstudy, MIacquisition,
    0
  };

  int result = 1;
  int vartype = 0;

  const char **tryname = 0;
  for (tryname = stdVarNames; *tryname != 0; tryname++)
    {
    if (strcmp(varname, *tryname) == 0)
      {
      vartype = 1;
      break;
      }
    }
  for (tryname = vtkMINCDimVarNames; *tryname != 0; tryname++)
    {
    if (strcmp(varname, *tryname) == 0)
      {
      vartype = 2;
      break;
      }
    }

  if (strcmp(varname, MI_EMPTY_STRING) == 0)
    {
    // Check global attributes
    result = this->ValidateGlobalAttribute(attname, array);
    // Allow users to create their own global attributes
    if (result == 2)
      {
      result = 1;
      }
    }
  else if (vartype != 0)
    {
    // Check general attributes
    result = this->ValidateGeneralAttribute(varname, attname, array);
    }
  if (result == 2)
    {
    if (vartype == 2)
      {
      result = this->ValidateDimensionAttribute(varname, attname, array);
      }
    else if (strcmp(varname, MIimage) == 0)
      {
      result = this->ValidateImageAttribute(varname, attname, array);
      }
    else if (strcmp(varname, MIimagemin) == 0 ||
             strcmp(varname, MIimagemax) == 0)
      {
      result = this->ValidateImageMinMaxAttribute(varname, attname, array);
      }
    else if (strcmp(varname, MIpatient) == 0)
      {
      result = this->ValidatePatientAttribute(varname, attname, array);
      }
    else if (strcmp(varname, MIstudy) == 0)
      {
      result = this->ValidateStudyAttribute(varname, attname, array);
      }
    else if (strcmp(varname, MIacquisition) == 0)
      {
      result = this->ValidateAcquisitionAttribute(varname, attname, array);
      }
    }
1407

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
  return result;
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::FindValidRange(double range[2])
{
  // Find the valid range. Start with the default.
  range[0] = 0.0;
  range[1] = 1.0;

1418 1419
  // Look for the valid_range attribute of the data.
  vtkDoubleArray *rangearray =
1420 1421 1422 1423 1424 1425
    vtkDoubleArray::SafeDownCast(this->GetAttributeValueAsArray(
                                   MIimage, MIvalid_range));
  if (rangearray)
    {
    range[0] = rangearray->GetValue(0);
    range[1] = rangearray->GetValue(1);
1426 1427 1428 1429 1430 1431
    if (range[0] > range[1])
      {
      double tmpval = range[0];
      range[0] = range[1];
      range[1] = tmpval;
      }
1432 1433 1434

    if (this->DataType == VTK_FLOAT)
      {
1435
      // use float precision if VTK_FLOAT
1436 1437
      range[0] = static_cast<float>(range[0]);
      range[1] = static_cast<float>(range[1]);
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
      }
    }
  else
    {
    // If there is no valid_range attribute, use maximum range.
    switch (this->DataType)
      {
      case VTK_CHAR:
      case VTK_SIGNED_CHAR:
        range[0] = VTK_SIGNED_CHAR_MIN;
        range[1] = VTK_SIGNED_CHAR_MAX;
        break;
      case VTK_UNSIGNED_CHAR:
        range[0] = VTK_UNSIGNED_CHAR_MIN;
        range[1] = VTK_UNSIGNED_CHAR_MAX;
        break;
      case VTK_SHORT:
        range[0] = VTK_SHORT_MIN;
        range[1] = VTK_SHORT_MAX;
        break;
      case VTK_UNSIGNED_SHORT:
        range[0] = VTK_UNSIGNED_SHORT_MIN;
        range[1] = VTK_UNSIGNED_SHORT_MAX;
        break;
      case VTK_INT:
        range[0] = VTK_INT_MIN;
        range[1] = VTK_INT_MAX;
        break;
      case VTK_UNSIGNED_INT:
        range[0] = VTK_UNSIGNED_INT_MIN;
        range[1] = VTK_UNSIGNED_INT_MAX;
        break;
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
      case VTK_FLOAT:
        range[0] = -FLT_MAX;
        range[1] = FLT_MAX;
        break;
      case VTK_DOUBLE:
        range[0] = -DBL_MAX;
        range[1] = DBL_MAX;
        break;
      }
    }

  // If the valid_range is set to the full float range, replace
  // with the image range.
  if ((this->DataType == VTK_FLOAT && range[1] == FLT_MAX) ||
      (this->DataType == VTK_DOUBLE && range[1] == DBL_MAX))
    {
    if (this->ImageMin && this->ImageMax &&
        this->ImageMin->GetNumberOfTuples() > 0 &&
        this->ImageMax->GetNumberOfTuples() > 0)
      {
      range[0] = this->ImageMin->GetRange()[0];
      range[1] = this->ImageMax->GetRange()[1];
      }
    else
      {
      range[0] = 0.0;
      range[1] = 1.0;
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
      }
    }
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::FindImageRange(double range[2])
{
  // Initialize to the default values
  range[0] = 0.0;
  range[1] = 1.0;

  // If image-min and image-max variables exist, use them.
  // Otherwise, use the valid_range of the data instead.
  if (this->ImageMin && this->ImageMax &&
      this->ImageMin->GetNumberOfTuples() > 0 &&
      this->ImageMax->GetNumberOfTuples() > 0)
    {
    range[0] = this->ImageMin->GetRange()[0];
    range[1] = this->ImageMax->GetRange()[1];
    }
  else
    {
    this->FindValidRange(range);
    }
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::ShallowCopy(vtkMINCImageAttributes *source)
{
  this->SetName(source->GetName());
  this->SetDataType(source->GetDataType());

  this->SetImageMin(source->GetImageMin());
  this->SetImageMax(source->GetImageMax());
1531
  this->SetNumberOfImageMinMaxDimensions(
1532 1533 1534 1535
    source->GetNumberOfImageMinMaxDimensions());

  this->DimensionNames->DeepCopy(source->GetDimensionNames());
  this->DimensionLengths->DeepCopy(source->GetDimensionLengths());
1536

1537 1538 1539 1540 1541
  this->VariableNames->Reset();
  this->AttributeValues->Clear();
  this->AttributeNames->Clear();

  vtkStringArray *varnames = source->GetVariableNames();
1542 1543
  vtkIdType nvar = varnames->GetNumberOfValues();
  for (vtkIdType ivar = 0; ivar <= nvar; ivar++)
1544 1545 1546
    {
    // set varname to emtpy last time around to get global attributes
    const char *varname = MI_EMPTY_STRING;
1547
    if (ivar < nvar)
1548
      {
1549
      varname = varnames->GetValue(ivar);
1550 1551
      }
    vtkStringArray *attnames = source->GetAttributeNames(varname);
1552 1553
    vtkIdType natt = attnames->GetNumberOfValues();
    for (vtkIdType iatt = 0; iatt < natt; iatt++)
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
      {
      const char *attname = attnames->GetValue(iatt);
      this->SetAttributeValueAsArray(
        varname, attname, source->GetAttributeValueAsArray(varname, attname));
      }
    }

  if (this->StringStore)
    {
    this->StringStore->Reset();
    }
}