-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathSGILoader.cpp
More file actions
243 lines (208 loc) · 8.96 KB
/
SGILoader.cpp
File metadata and controls
243 lines (208 loc) · 8.96 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright 2019-2024 Diligent Graphics LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/
#include "SGILoader.h"
#include <cstring>
#include "DataBlob.h"
#include "PlatformMisc.hpp"
#include "Errors.hpp"
namespace Diligent
{
namespace
{
struct SGIHeader
{
Uint16 Magic; // SGI magic number
uint8_t Compression; // Whether to use RLE compression or not
uint8_t BytePerPixelChannel; // 1 for 8-bit channels, or 2 for 16-bit channels
Uint16 DimensionBE; // Image dimension, equals 3 for RGBA image
Uint16 WidthBE; // Image width
Uint16 HeightBE; // Image height
Uint16 ChannelsBE; // Number of channels, equals 4 for RGBA image
Uint32 MinPixelValueBE; // Smallest pixel value in the image
Uint32 MaxPixelValueBE; // Largest pixel value in the image
Uint32 Reserved1BE; // Unused
char Name[80]; // C string null-terminated name
Uint32 ColorMapIDBE; // Only for color map image
char Reserved2[404]; // To make header 512 bytes long. Ignore
};
static_assert(sizeof(SGIHeader) == 512, "must be 512 bytes");
} // namespace
// http://paulbourke.net/dataformats/sgirgb/sgiversion.html
bool LoadSGI(const void* pSGIData,
size_t DataSize,
IDataBlob* pDstPixels,
ImageDesc* pDstImgDesc)
{
VERIFY_EXPR(pSGIData != nullptr && pDstImgDesc != nullptr);
const Uint8* pDataStart = static_cast<const Uint8*>(pSGIData);
const Uint8* pDataEnd = pDataStart + DataSize;
const Uint8* pSrcPtr = pDataStart;
if (DataSize < sizeof(SGIHeader))
{
LOG_ERROR_MESSAGE("The SGI data size (", DataSize, ") is smaller than the size of required SGI header (", sizeof(SGIHeader), ").");
return false;
}
const SGIHeader& Header = reinterpret_cast<const SGIHeader&>(*pSrcPtr);
pSrcPtr += sizeof(SGIHeader);
constexpr Uint16 SGIMagic = 0xda01u;
if (Header.Magic != 0xda01)
{
LOG_ERROR_MESSAGE("0x", std::hex, Header.Magic, " is not a valid SGI magic number; 0x", std::hex, SGIMagic, " is expected.");
return false;
}
const Uint32 Width = PlatformMisc::SwapBytes(Header.WidthBE);
const Uint32 Height = PlatformMisc::SwapBytes(Header.HeightBE);
const Uint32 NumChannels = PlatformMisc::SwapBytes(Header.ChannelsBE);
const Uint32 BytesPerChannel = Header.BytePerPixelChannel;
pDstImgDesc->Width = Width;
pDstImgDesc->Height = Height;
pDstImgDesc->NumComponents = NumChannels;
switch (BytesPerChannel)
{
case 1:
pDstImgDesc->ComponentType = VT_UINT8;
break;
case 2:
pDstImgDesc->ComponentType = VT_UINT16;
break;
case 4:
pDstImgDesc->ComponentType = VT_UINT32;
break;
default:
UNEXPECTED(BytesPerChannel, " is not a supported byte count");
return false;
}
if (pDstPixels == nullptr)
return true;
pDstImgDesc->RowStride = Width * NumChannels * BytesPerChannel;
pDstPixels->Resize(size_t{Height} * pDstImgDesc->RowStride);
Uint8* pDstPtr = pDstPixels->GetDataPtr<Uint8>();
if (Header.Compression == 0)
{
// Uncompressed SGI image
const size_t UncompressedSize = size_t{Width} * Height * NumChannels * BytesPerChannel;
if (pSrcPtr + UncompressedSize > pDataEnd)
{
LOG_ERROR_MESSAGE("Not enough data for uncompressed image.");
return false;
}
const size_t PlaneSize = size_t{Width} * Height * BytesPerChannel;
for (size_t y = 0; y < Height; ++y)
{
for (size_t x = 0; x < Width; ++x)
{
// Copy each channel from the respective plane
for (size_t c = 0; c < NumChannels; ++c)
{
const Uint8* pSrcPlane = pSrcPtr + PlaneSize * c;
const size_t PixelIdx = y * Width + x;
// Copy channel
std::memcpy(
pDstPtr + (PixelIdx * NumChannels + c) * BytesPerChannel,
pSrcPlane + PixelIdx * BytesPerChannel,
BytesPerChannel);
}
}
}
}
else if (Header.Compression == 1)
{
// RLE-compressed SGI image
// Offsets table starts at byte 512 and is Height * NumChannels * 4 bytes long.
const size_t TableSize = sizeof(Uint32) * Height * NumChannels;
const Uint32* OffsetTableBE = reinterpret_cast<const Uint32*>(pSrcPtr);
pSrcPtr += TableSize;
if (pSrcPtr > pDataEnd)
return false;
// Length table follows the offsets table and is the same size.
const Uint32* LengthTableBE = reinterpret_cast<const Uint32*>(pSrcPtr);
pSrcPtr += TableSize;
if (pSrcPtr > pDataEnd)
return false;
//VERIFY(Header.Compression, "Only RLE compressed files are currently supported");
VERIFY(BytesPerChannel == 1, "Only 8-bit images are currently supported");
const auto ReadLine = [Width, BytesPerChannel, NumChannels](auto* pDst, const auto* pLineDataStart, const auto* pLineDataEnd) //
{
VERIFY_EXPR(sizeof(*pDst) == BytesPerChannel);
VERIFY_EXPR(sizeof(*pLineDataStart) == BytesPerChannel);
(void)BytesPerChannel;
const auto* pSrc = pLineDataStart;
Uint32 x = 0;
while (x < Width && pSrc < pLineDataEnd)
{
// The lowest 7 bits is the counter
int Count = *pSrc & 0x7F;
// If the high order bit of the first byte is 1, then the count is used to specify how many values to copy
// from the RLE data buffer.
// If the high order bit of the first byte is 0, then the count is used to specify how many times to repeat
// the value following the counter.
auto DistinctValues = (*pSrc & 0x80) != 0;
++pSrc;
while (Count > 0 && pSrc < pLineDataEnd)
{
*pDst = *pSrc;
if (DistinctValues || Count == 1) // Always move the pointer for the last value
++pSrc;
pDst += NumChannels;
--Count;
++x;
}
}
return x == Width;
};
for (Uint32 c = 0; c < NumChannels; ++c)
{
for (Uint32 y = 0; y < Height; ++y)
{
// Each unsigned int in the offset table is the offset (from the file start) to the
// start of the compressed data of each scanline for each channel.
const Uint32 RleOff = PlatformMisc::SwapBytes(OffsetTableBE[y + c * Height]);
// The size table tells the size of the compressed data (unsigned int) of each scanline.
const Uint32 RleLen = PlatformMisc::SwapBytes(LengthTableBE[y + c * Height]);
Uint8* DstLine = pDstPtr + y * size_t{pDstImgDesc->RowStride} + c;
if (!ReadLine(DstLine, pDataStart + RleOff, pSrcPtr + RleOff + RleLen))
return false;
}
}
}
else
{
LOG_ERROR_MESSAGE("Unsupported compression value.");
return false;
}
return true;
}
} // namespace Diligent
extern "C"
{
void Diligent_LoadSGI(const void* pSGIData,
size_t DataSize,
Diligent::IDataBlob* pDstPixels,
Diligent::ImageDesc* pDstImgDesc)
{
Diligent::LoadSGI(pSGIData, DataSize, pDstPixels, pDstImgDesc);
}
}