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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkTGAReader.h"
#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkImageFlip.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include <vtksys/FStream.hxx>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkTGAReader);
namespace
{
constexpr int HeaderSize = 18;
enum TGAFormat : unsigned char
{
Uncompressed_RGB = 2,
RLE_RGB = 10
};
}
//----------------------------------------------------------------------------
void vtkTGAReader::ExecuteInformation()
{
char header[::HeaderSize];
if (this->GetMemoryBuffer())
{
const char* memBuffer = static_cast<const char*>(this->GetMemoryBuffer());
std::copy(memBuffer, memBuffer + ::HeaderSize, header);
}
else
{
this->ComputeInternalFileName(0);
vtksys::ifstream file(this->InternalFileName, std::ios::binary);
file.read(header, ::HeaderSize * sizeof(char));
file.close();
}
// tmp char needed to avoid strict anti aliasing warning
char* tmp = &header[8];
this->DataOrigin[0] = static_cast<double>(*reinterpret_cast<short*>(tmp));
tmp = &header[10];
this->DataOrigin[1] = static_cast<double>(*reinterpret_cast<short*>(tmp));
this->DataOrigin[2] = 0.0;
this->DataExtent[0] = 0;
tmp = &header[12];
this->DataExtent[1] = static_cast<int>(*reinterpret_cast<short*>(tmp) - 1);
this->DataExtent[2] = 0;
tmp = &header[14];
this->DataExtent[3] = static_cast<int>(*reinterpret_cast<short*>(tmp) - 1);
bool upperLeft = static_cast<bool>((header[17] >> 5) & 1);
this->SetFileLowerLeft(!upperLeft);
this->SetHeaderSize(::HeaderSize);
this->SetDataScalarTypeToUnsignedChar();
this->SetNumberOfScalarComponents(header[16] / 8);
this->vtkImageReader2::ExecuteInformation();
}
//------------------------------------------------------------------------------
void vtkTGAReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInformation* outInfo)
{
vtkImageData* data = this->AllocateOutputData(output, outInfo);
data->GetPointData()->GetScalars()->SetName("TGAImage");
this->ComputeDataIncrements();
std::vector<unsigned char> content;
if (this->GetMemoryBuffer())
{
const unsigned char* uBuffer = reinterpret_cast<const unsigned char*>(this->GetMemoryBuffer());
content.assign(uBuffer, uBuffer + this->GetMemoryBufferLength());
}
else
{
vtksys::ifstream imageFile(this->InternalFileName, std::ios::binary);
content.assign(std::istreambuf_iterator<char>(imageFile), std::istreambuf_iterator<char>());
}
bool encoded = (content[2] == ::TGAFormat::RLE_RGB);
vtkIdType nComponents = this->GetNumberOfScalarComponents();
// buffer to image data
unsigned char* outPtr = reinterpret_cast<unsigned char*>(data->GetScalarPointer());
unsigned char* endPtr =
outPtr + ((this->DataExtent[3] + 1) * (this->DataExtent[1] + 1) * nComponents);
auto GetColor = [&](size_t& currentIndex, unsigned char*& buffer) {
for (vtkIdType j = 0; j < nComponents; j++)
{
buffer[j] = content[currentIndex + j];
}
if (nComponents >= 3) // swap red and blue
{
std::swap(buffer[0], buffer[2]);
}
currentIndex += nComponents;
buffer += nComponents;
};
size_t skip = ::HeaderSize + static_cast<size_t>(content[0]);
size_t index = skip;
while (outPtr < endPtr && index < content.size())
{
if (encoded)
{
unsigned char packet = content[index++];
if (packet & 0x80) // packet is RLE if highest bit is 1
{
// RLE packet (clear highest bit and add 1)
packet = (packet & 0x7f) + 1;
unsigned char* dupBuffer = outPtr;
GetColor(index, outPtr);
for (unsigned char i = 0; i < packet - 1; i++)
{
for (vtkIdType k = 0; k < nComponents; k++)
{
outPtr[k] = dupBuffer[k];
}
outPtr += nComponents;
}
}
else
{
// raw packet (add 1)
packet += 1;
for (unsigned char i = 0; i < packet; i++)
{
GetColor(index, outPtr);
}
}
}
else
{
GetColor(index, outPtr);
}
}
if (!this->FileLowerLeft)
{
vtkNew<vtkImageFlip> flipY;
flipY->SetFilteredAxis(1);
flipY->SetInputData(data);
flipY->Update();
data->ShallowCopy(flipY->GetOutput());
}
}
//----------------------------------------------------------------------------
int vtkTGAReader::CanReadFile(const char* fname)
{
vtksys::ifstream file(fname, std::ios::binary);
if (!file.is_open())
{
return 0;
}
char header[::HeaderSize];
file.read(header, ::HeaderSize * sizeof(char));
// only uncompressed RGB and RLE encoded RGB formats are supported
if (header[2] != ::TGAFormat::RLE_RGB && header[2] != ::TGAFormat::Uncompressed_RGB)
{
vtkWarningMacro("Only RLE RGB and uncompressed RGB TGA files are supported");
return 0;
}
return 1;
}
//------------------------------------------------------------------------------
void vtkTGAReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
VTK_ABI_NAMESPACE_END
|