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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkJSONImageWriter.h"
#include "vtkCharArray.h"
#include "vtkCommand.h"
#include "vtkDataArray.h"
#include "vtkErrorCode.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationExecutivePortKey.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include <vtksys/FStream.hxx>
#include <vtksys/SystemTools.hxx>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkJSONImageWriter);
//------------------------------------------------------------------------------
vtkJSONImageWriter::vtkJSONImageWriter()
{
this->FileName = nullptr;
this->ArrayName = nullptr;
this->Slice = -1;
this->SetNumberOfOutputPorts(0);
}
//------------------------------------------------------------------------------
vtkJSONImageWriter::~vtkJSONImageWriter()
{
this->SetFileName(nullptr);
this->SetArrayName(nullptr);
}
//------------------------------------------------------------------------------
void vtkJSONImageWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "FileName: " << (this->FileName ? this->FileName : "(none)") << "\n";
}
//------------------------------------------------------------------------------
int vtkJSONImageWriter::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* vtkNotUsed(outputVector))
{
this->SetErrorCode(vtkErrorCode::NoError);
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkImageData* input = vtkImageData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
// Error checking
if (input == nullptr)
{
vtkErrorMacro(<< "Write:Please specify an input!");
return 0;
}
if (!this->FileName)
{
vtkErrorMacro(<< "Write:Please specify either a FileName or a file prefix and pattern");
this->SetErrorCode(vtkErrorCode::NoFileNameError);
return 0;
}
// Write
this->InvokeEvent(vtkCommand::StartEvent);
vtkCharArray* validMask =
vtkArrayDownCast<vtkCharArray>(input->GetPointData()->GetArray("vtkValidPointMask"));
vtksys::ofstream file(this->FileName, ios::out);
if (file.fail())
{
vtkErrorMacro("RecursiveWrite: Could not open file " << this->FileName);
this->SetErrorCode(vtkErrorCode::CannotOpenFileError);
return 0;
}
file << "{"
<< "\"filename\" : \"" << this->FileName << "\""
<< ",\n\"dimensions\": [" << input->GetDimensions()[0] << ", " << input->GetDimensions()[1]
<< ", " << input->GetDimensions()[2] << "]"
<< ",\n\"origin\": [" << input->GetOrigin()[0] << ", " << input->GetOrigin()[1] << ", "
<< input->GetOrigin()[2] << "]"
<< ",\n\"spacing\": [" << input->GetSpacing()[0] << ", " << input->GetSpacing()[1] << ", "
<< input->GetSpacing()[2] << "]";
// Write all arrays
int nbArrays = input->GetPointData()->GetNumberOfArrays();
for (int i = 0; i < nbArrays; ++i)
{
vtkDataArray* array = input->GetPointData()->GetArray(i);
// We only dump scalar values
if (array->GetNumberOfComponents() != 1 || !strcmp(array->GetName(), "vtkValidPointMask"))
{
continue;
}
if (this->ArrayName && strlen(this->ArrayName) > 0 &&
strcmp(array->GetName(), this->ArrayName) != 0)
{
continue;
}
file << ",\n\"" << array->GetName() << "\": [";
vtkIdType startIdx = 0;
vtkIdType endIdx = array->GetNumberOfTuples();
if (this->Slice >= 0)
{
vtkIdType sliceSize = input->GetDimensions()[0] * input->GetDimensions()[1];
startIdx = sliceSize * this->Slice;
endIdx = startIdx + sliceSize;
}
for (vtkIdType idx = startIdx; idx < endIdx; ++idx)
{
if (idx % 50 == 0)
{
file << "\n";
file.flush();
}
if (idx != startIdx)
{
file << ", ";
}
if ((validMask == nullptr) || (validMask && validMask->GetValue(idx)))
{
file << array->GetVariantValue(idx).ToString();
}
else
{
file << "null";
}
}
file << "]";
}
file << "\n}" << endl;
file.close();
file.flush();
this->InvokeEvent(vtkCommand::EndEvent);
return 1;
}
//------------------------------------------------------------------------------
void vtkJSONImageWriter::Write()
{
this->Modified();
this->UpdateWholeExtent();
}
VTK_ABI_NAMESPACE_END
|