vtkMINCImageAttributes.cxx 41.1 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>
Sean McBride's avatar
Sean McBride committed
68 69
#include <cctype>
#include <cfloat>
70 71
#include <string>
#include <map>
72
#include <sstream>
73 74 75 76 77 78

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

  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; };
115
    return nullptr; };
116 117 118

private:
  vtkMINCImageAttributeMap() : Map() {};
119
  ~vtkMINCImageAttributeMap() = default;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

  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();
139 140 141 142 143 144
  // Add global attribute name array
  vtkStringArray *tmparray = vtkStringArray::New();
  tmparray->SetName("");
  this->AttributeNames->AddArray(tmparray);
  tmparray->Delete();

145
  this->AttributeValues = vtkMINCImageAttributeMap::New();
146
  this->StringStore = nullptr;
147 148

  this->NumberOfImageMinMaxDimensions = 0;
149 150
  this->ImageMin = nullptr;
  this->ImageMax = nullptr;
151

152
  this->Name = nullptr;
153
  this->DataType = VTK_VOID;
154 155

  this->ValidateAttributes = 1;
156 157 158 159 160
}

//-------------------------------------------------------------------------
vtkMINCImageAttributes::~vtkMINCImageAttributes()
{
161
  this->SetName(nullptr);
162

163
  if (this->DimensionNames)
164
  {
165
    this->DimensionNames->Delete();
166
    this->DimensionNames = nullptr;
167
  }
168
  if (this->DimensionLengths)
169
  {
170
    this->DimensionLengths->Delete();
171
    this->DimensionLengths = nullptr;
172
  }
173
  if (this->VariableNames)
174
  {
175
    this->VariableNames->Delete();
176
    this->VariableNames = nullptr;
177
  }
178
  if (this->AttributeNames)
179
  {
180
    this->AttributeNames->Delete();
181
    this->AttributeNames = nullptr;
182
  }
183
  if (this->AttributeValues)
184
  {
185
    this->AttributeValues->Delete();
186
    this->AttributeValues = nullptr;
187
  }
188
  if (this->ImageMin)
189
  {
190
    this->ImageMin->Delete();
191
    this->ImageMin = nullptr;
192
  }
193
  if (this->ImageMax)
194
  {
195
    this->ImageMax->Delete();
196
    this->ImageMax = nullptr;
197
  }
198
  if (this->StringStore)
199
  {
200
    this->StringStore->Delete();
201
    this->StringStore = nullptr;
202
  }
203 204 205 206 207 208 209
}

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

210
  os << indent << "Name: "
211 212 213 214 215 216 217
     << (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";
218
  os << indent << "NumberOfImageMinMaxDimensions: "
219
     << this->NumberOfImageMinMaxDimensions << "\n";
220 221
  os << indent << "ValidateAttributes: "
     << (this->ValidateAttributes ? "On\n" : "Off\n");
222 223 224 225 226
}

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

  // Add global attribute name array
  vtkStringArray *tmparray = vtkStringArray::New();
  tmparray->SetName("");
  this->AttributeNames->AddArray(tmparray);
  tmparray->Delete();
247 248 249 250 251 252 253
}

//-------------------------------------------------------------------------
// Allowed dimension variable names
static const char *vtkMINCDimVarNames[] = {
  MIxspace, MIyspace, MIzspace, MItime,
  MIxfrequency, MIyfrequency, MIzfrequency, MItfrequency,
254
  nullptr
255 256 257 258 259 260 261
};

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::AddDimension(const char *dimension,
                                          vtkIdType length)
{
  // Check for duplicates
262 263
  vtkIdType n = this->DimensionNames->GetNumberOfValues();
  for (vtkIdType i = 0; i < n; i++)
264
  {
265
    if (strcmp(dimension, this->DimensionNames->GetValue(i)) == 0)
266
    {
267 268 269 270
      vtkErrorMacro("The dimension " << dimension <<
                    " has already been created.");
      return;
    }
271
  }
272 273

  // Ensure the dimension name is valid
274 275
  const char **tryname = nullptr;
  for (tryname = vtkMINCDimVarNames; *tryname != nullptr; tryname++)
276
  {
277
    if (strcmp(dimension, *tryname) == 0)
278
    {
279 280
      break;
    }
281
  }
282
  if (*tryname == nullptr && strcmp(dimension, MIvector_dimension) != 0)
283
  {
284 285
    vtkWarningMacro("The dimension name " << dimension <<
                    " is not recognized.");
286
  }
287 288 289 290 291 292 293 294 295 296

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

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

304 305
  int dataType = array->GetDataType();
  if (dataType == VTK_CHAR)
306
  {
307
    vtkCharArray *charArray = vtkArrayDownCast<vtkCharArray>(array);
308
    if (charArray)
309
    {
310 311 312 313 314
      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'))
315
      {
316
        return result;
317
      }
318
    }
319
  }
320

321
  std::ostringstream os;
322

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

364 365
  // Store the string
  std::string str = os.str();
366

367
  if (this->StringStore == nullptr)
368
  {
369
    this->StringStore = vtkStringArray::New();
370
  }
371

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

390
  return result;
391 392 393
}

//-------------------------------------------------------------------------
394
void vtkMINCImageAttributes::PrintFileHeader()
395
{
396
  this->PrintFileHeader(cout);
397 398 399
}

//-------------------------------------------------------------------------
400
void vtkMINCImageAttributes::PrintFileHeader(ostream &os)
401 402 403
{
  const char *name = "unknown";
  if (this->Name)
404
  {
405
    name = this->Name;
406
  }
407 408 409 410

  // Get the data type
  const char *imageDataType;
  switch (this->DataType)
411
  {
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    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";
433
  }
434 435 436

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

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

  os << "variables:\n";

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

        if (nvardim > 0)
488
        {
489 490
          os << "(";
          for (int ivardim = 0; ivardim < nvardim; ivardim++)
491
          {
492 493
            os << this->DimensionNames->GetValue(ivardim);
            if (ivardim < nvardim - 1)
494
            {
495 496 497
              os << ", ";
            }
          }
498
          os << ")";
499
        }
500 501
        os << " ;\n";
      }
502
      else
503
      {
504 505
        os << "\t" << "int " << varname << " ;\n";
      }
506
    }
507 508 509
    vtkStringArray *attArray =
      this->AttributeNames->GetStringArray(varname);
    if (attArray)
510
    {
511 512
      vtkIdType natt = attArray->GetNumberOfValues();
      for (vtkIdType iatt = 0; iatt < natt; iatt++)
513
      {
514 515 516 517 518
        const char *attname = attArray->GetValue(iatt);
        vtkDataArray *array =
          this->GetAttributeValueAsArray(varname, attname);
        os << "\t\t" << varname << ":" << attname << " = ";
        if (array->GetDataType() == VTK_CHAR)
519
        {
520
          os << "\"";
521 522
          const char *cp = this->ConvertDataArrayToString(array);
          const char *endcp = cp + strlen(cp);
523 524 525
          char text[512];
          text[0] = '\0';
          while (cp < endcp)
526
          {
527 528 529
            int c = 0;
            int j;
            for (j = 0; j < 508 && cp < endcp; j++, cp++)
530
            {
531 532
              c = *cp;
              if (c == '\0' && (cp + 1) == endcp)
533
              {
534 535 536
                // break if at terminal null
                cp++;
                break;
537
              }
538
              if (isprint(c) && c != '\\')
539
              {
540
                text[j] = c;
541
              }
542
              else
543
              {
544 545
                // quote the non-printing characters
                switch (c)
546
                {
547 548 549 550 551 552 553
                  case '\\':
                    text[j++] = '\\';
                    text[j] = '\\';
                    break;
                  case '\b':
                    text[j++] = '\\';
                    text[j] = '\b';
554
                    break;
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
                  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;
                }
586
              }
587
              if (c == '\n')
588
              {
589 590 591 592
                j++;
                cp++;
                break;
              }
593
            }
594 595 596 597 598 599 600 601

            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)
602
            {
603 604 605
              os << "\",\n\t\t\t\"";
            }
          }
606 607
          os << "\" ;\n";
        }
608
        else
609
        {
610 611 612 613 614
          // Use handy conversion method
          os << this->ConvertDataArrayToString(array) << " ;\n";
        }
      }
    }
615
  }
616 617 618 619

  os << "data:\n";

  if (this->VariableNames)
620
  {
621
    nvar = this->VariableNames->GetNumberOfValues();
622
  }
623
  for (ivar = 0; ivar < nvar; ivar++)
624
  {
625 626 627
    const char *varname = this->VariableNames->GetValue(ivar);

    if (strcmp(varname, MIimage) == 0)
628
    {
629
      continue;
630
    }
631 632 633 634

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

    if (strcmp(varname, MIimagemin) == 0)
635
    {
636
      if (this->ImageMin)
637
      {
638
        os << this->ConvertDataArrayToString(this->ImageMin) << " ;\n";
639
      }
640
      else
641
      {
642 643
        os << "0. ;\n";
      }
644
    }
645
    else if (strcmp(varname, MIimagemax) == 0)
646
    {
647
      if (this->ImageMax)
648
      {
649
        os << this->ConvertDataArrayToString(this->ImageMax) << " ;\n";
650
      }
651
      else
652
      {
653 654
        os << "1. ;\n";
      }
655
    }
656
    else
657
    {
658
      os << "_ ;\n";
659
    }
660
  }
661 662 663 664 665 666 667 668 669

  os << "}\n";
}

//-------------------------------------------------------------------------
vtkStringArray *vtkMINCImageAttributes::GetAttributeNames(
  const char *variable)
{
  // If variable is null, use empty string to get global attributes
670
  if (variable == nullptr)
671
  {
672
    variable = MI_EMPTY_STRING;
673
  }
674 675 676 677 678 679 680 681 682

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

//-------------------------------------------------------------------------
int vtkMINCImageAttributes::HasAttribute(
  const char *variable,
  const char *attribute)
{
683
  return (this->GetAttributeValueAsArray(variable, attribute) != nullptr);
684 685 686 687 688 689 690
}

//-------------------------------------------------------------------------
vtkDataArray *vtkMINCImageAttributes::GetAttributeValueAsArray(
  const char *variable,
  const char *attribute)
{
691
  std::string path = MI_GRPNAME;
692
  if (variable && variable[0] != '\0')
693
  {
694 695
    path += MI_GRP_SEP;
    path += variable;
696
  }
697 698 699 700 701 702 703 704 705 706 707 708 709 710
  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);

711 712
  // Return nullptr if not found
  if (array == nullptr)
713
  {
714
    return nullptr;
715
  }
716

717
  // Convert any other array to a string.
718 719 720 721 722 723 724 725 726 727
  return this->ConvertDataArrayToString(array);
}

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

728
  if (array == nullptr)
729
  {
730 731 732
    vtkErrorMacro("The attribute " << variable << ":"
                  << attribute << " was not found.");
    return 0;
733
  }
734 735

  if (array->GetDataType() == VTK_CHAR)
736
  {
737 738 739 740 741
    const char *text = this->ConvertDataArrayToString(array);
    char *endp = const_cast<char *>(text);
    long result = strtol(text, &endp, 10);
    // Check for complete conversion
    if (*endp == '\0' && *text != '\0')
742
    {
743
      return static_cast<int>(result);
744
    }
745
  }
746
  else if (array->GetNumberOfTuples() == 1)
747
  {
748
    switch(array->GetDataType())
749
    {
750 751 752 753 754 755 756 757
      case VTK_SIGNED_CHAR:
      case VTK_UNSIGNED_CHAR:
      case VTK_SHORT:
      case VTK_INT:
        return static_cast<int>(array->GetComponent(0,0));
      default:
        break;
    }
758
  }
759 760 761 762 763 764 765 766 767 768 769

  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)
{
770
  if (variable == nullptr)
771
  {
772
    variable = MI_EMPTY_STRING;
773
  }
774 775 776

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

777
  if (array == nullptr)
778
  {
779 780 781
    vtkErrorMacro("The attribute " << variable << ":"
                  << attribute << " was not found.");
    return 0;
782
  }
783 784

  if (array->GetDataType() == VTK_CHAR)
785
  {
786 787 788 789 790
    const char *text = this->ConvertDataArrayToString(array);
    char *endp = const_cast<char *>(text);
    double result = strtod(text, &endp);
    // Check for complete conversion
    if (*endp == '\0' && *text != '\0')
791
    {
792
      return result;
793
    }
794
  }
795
  else if (array->GetNumberOfTuples() == 1)
796
  {
797
    switch(array->GetDataType())
798
    {
799 800 801 802 803 804 805 806 807 808
      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;
    }
809
  }
810 811 812 813 814 815 816 817 818 819 820 821

  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)
{
822
  std::string path = MI_GRPNAME;
823
  if (variable && variable[0] != '\0')
824
  {
825 826
    path += MI_GRP_SEP;
    path += variable;
827
  }
828 829 830 831 832 833 834
  path += MI_ATT_SEP;
  path += attribute;

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

  // Add to variable to VariableNames
835 836
  vtkIdType n = this->VariableNames->GetNumberOfValues();
  vtkIdType i = 0;
837
  for (i = 0; i < n; i++)
838
  {
839
    if (strcmp(this->VariableNames->GetValue(i), variable) == 0)
840
    {
841 842
      break;
    }
843
  }
844
  if (i == n && variable[0] != '\0')
845
  {
846
    this->VariableNames->InsertNextValue(variable);
847
  }
848 849 850 851

  // Add to attribute to AttributeNames
  vtkStringArray *attribs = this->AttributeNames->GetStringArray(variable);
  // Create a new array if necessary
852
  if (attribs == nullptr)
853
  {
854 855 856 857
    attribs = vtkStringArray::New();
    attribs->SetName(variable);
    this->AttributeNames->AddArray(attribs);
    attribs->Delete();
858
  }
859 860 861

  n = attribs->GetNumberOfValues();
  for (i = 0; i < n; i++)
862
  {
863
    if (strcmp(attribs->GetValue(i), attribute) == 0)
864
    {
865 866
      break;
    }
867
  }
868
  if (i == n)
869
  {
870
    attribs->InsertNextValue(attribute);
871
  }
872

873
  if (this->ValidateAttributes)
874
  {
875 876 877 878
    // Print warning if there is something wrong with the attribute
    int result = this->ValidateAttribute(variable, attribute, array);

    if (result > 1)
879
    {
880 881 882
      vtkWarningMacro("Attribute " << variable << ":" << attribute
                      << " is not a valid attribute.");
    }
883
  }
884 885 886 887 888 889 890 891
}

//-------------------------------------------------------------------------
void vtkMINCImageAttributes::SetAttributeValueAsString(
  const char *variable,
  const char *attribute,
  const char *value)
{
892
  size_t length = strlen(value);
893 894

  vtkCharArray *array = vtkCharArray::New();