File: vtkPNMWriter.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (101 lines) | stat: -rw-r--r-- 2,834 bytes parent folder | download | duplicates (3)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkPNMWriter.h"

#include "vtkErrorCode.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"

VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkPNMWriter);

void vtkPNMWriter::WriteFileHeader(ostream* file, vtkImageData* cache, int wExt[6])
{
  int min1 = wExt[0], max1 = wExt[1], min2 = wExt[2], max2 = wExt[3];
  int bpp;

  // Find the length of the rows to write.
  bpp = cache->GetNumberOfScalarComponents();

  // spit out the pnm header
  if (bpp == 1)
  {
    *file << "P5\n";
    *file << "# pgm file written by the visualization toolkit\n";
    *file << (max1 - min1 + 1) << " " << (max2 - min2 + 1) << "\n255\n";
  }
  else
  {
    *file << "P6\n";
    *file << "# ppm file written by the visualization toolkit\n";
    *file << (max1 - min1 + 1) << " " << (max2 - min2 + 1) << "\n255\n";
  }
}

void vtkPNMWriter::WriteFile(ostream* file, vtkImageData* data, int extent[6], int wExtent[6])
{
  int idx0, idx1, idx2;
  int rowLength; // in bytes
  void* ptr;
  unsigned long count = 0;
  unsigned long target;
  float progress = this->Progress;
  float area;

  // Make sure we actually have data.
  if (!data->GetPointData()->GetScalars())
  {
    vtkErrorMacro(<< "Could not get data from input.");
    return;
  }

  // take into consideration the scalar type
  switch (data->GetScalarType())
  {
    case VTK_UNSIGNED_CHAR:
      rowLength = sizeof(unsigned char);
      break;
    default:
      vtkErrorMacro("PNMWriter only accepts unsigned char scalars!");
      return;
  }
  rowLength *= data->GetNumberOfScalarComponents();

  area = static_cast<float>(((extent[5] - extent[4] + 1) * (extent[3] - extent[2] + 1) *
           (extent[1] - extent[0] + 1))) /
    static_cast<float>(((wExtent[5] - wExtent[4] + 1) * (wExtent[3] - wExtent[2] + 1) *
      (wExtent[1] - wExtent[0] + 1)));

  target =
    (unsigned long)((extent[5] - extent[4] + 1) * (extent[3] - extent[2] + 1) / (50.0 * area));
  target++;

  for (idx2 = extent[4]; idx2 <= extent[5]; ++idx2)
  {
    for (idx1 = extent[3]; idx1 >= extent[2]; idx1--)
    {
      if (!(count % target))
      {
        this->UpdateProgress(progress + count / (50.0 * target));
      }
      count++;
      for (idx0 = extent[0]; idx0 <= extent[1]; idx0++)
      {
        ptr = data->GetScalarPointer(idx0, idx1, idx2);
        if (!file->write((char*)ptr, rowLength))
        {
          this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
          return;
        }
      }
    }
  }
}

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