-
Notifications
You must be signed in to change notification settings - Fork 667
Expand file tree
/
Copy pathsoftimageinput.cpp
More file actions
549 lines (464 loc) · 18.6 KB
/
softimageinput.cpp
File metadata and controls
549 lines (464 loc) · 18.6 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// Copyright Contributors to the OpenImageIO project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/AcademySoftwareFoundation/OpenImageIO
#include "softimage_pvt.h"
OIIO_PLUGIN_NAMESPACE_BEGIN
using namespace softimage_pvt;
class SoftimageInput final : public ImageInput {
public:
SoftimageInput() { init(); }
~SoftimageInput() override { close(); }
const char* format_name(void) const override { return "softimage"; }
bool open(const std::string& name, ImageSpec& spec) override;
bool close() override;
bool read_native_scanline(int subimage, int miplevel, int y, int z,
void* data) override;
private:
/// Resets the core data members to defaults.
///
void init();
/// Read a scanline from m_fd.
///
bool read_next_scanline(void* data);
/// Read uncompressed pixel data from m_fd.
///
bool read_pixels_uncompressed(const softimage_pvt::ChannelPacket& curPacket,
void* data);
/// Read pure run length encoded pixels.
///
bool
read_pixels_pure_run_length(const softimage_pvt::ChannelPacket& curPacket,
void* data);
/// Read mixed run length encoded pixels.
///
bool
read_pixels_mixed_run_length(const softimage_pvt::ChannelPacket& curPacket,
void* data);
// Name for encoding
const char* encoding_name(int encoding);
FILE* m_fd;
softimage_pvt::PicFileHeader m_pic_header;
std::vector<softimage_pvt::ChannelPacket> m_channel_packets;
std::string m_filename;
std::vector<fpos_t> m_scanline_markers;
// Maps absolute channel index (0=R,1=G,2=B,3=A) to sequential output
// offset within the scanline buffer. Initialized to -1 (unused).
int m_channel_map[4];
};
// symbols required for OpenImageIO plugin
OIIO_PLUGIN_EXPORTS_BEGIN
OIIO_EXPORT ImageInput*
softimage_input_imageio_create()
{
return new SoftimageInput;
}
OIIO_EXPORT const char* softimage_input_extensions[] = { "pic", nullptr };
OIIO_PLUGIN_EXPORTS_END
void
SoftimageInput::init()
{
m_fd = NULL;
m_filename.clear();
m_channel_packets.clear();
m_scanline_markers.clear();
std::fill(m_channel_map, m_channel_map + 4, -1);
}
bool
SoftimageInput::open(const std::string& name, ImageSpec& spec)
{
// Remember the filename
m_filename = name;
m_fd = Filesystem::fopen(m_filename, "rb");
if (!m_fd) {
errorfmt("Could not open file \"{}\"", name);
return false;
}
// Try read the header
if (!m_pic_header.read_header(m_fd)) {
errorfmt("\"{}\": failed to read header", m_filename);
close();
return false;
}
// Check whether it has the pic magic number
if (m_pic_header.magic != 0x5380f634) {
errorfmt(
"\"{}\" is not a Softimage Pic file, magic number of 0x{:x} is not Pic",
m_filename, m_pic_header.magic);
close();
return false;
}
// Get the ChannelPackets
ChannelPacket curPacket;
std::vector<std::string> encodings;
do {
// Read the next packet into curPacket and store it off
if (fread(&curPacket, 1, sizeof(ChannelPacket), m_fd)
!= sizeof(ChannelPacket)) {
errorfmt("Unexpected end of file \"{}\".", m_filename);
close();
return false;
}
// Some validity checking
if (curPacket.size != 8 && curPacket.size != 16) {
errorfmt("Unsupported bits per channel {}", curPacket.size);
close();
return false;
}
if (curPacket.channelCode == 0) {
errorfmt("Channel packet with no channels");
close();
return false;
}
m_channel_packets.push_back(curPacket);
encodings.push_back(encoding_name(m_channel_packets.back().type));
if (m_channel_packets.size() > 4) {
errorfmt("Too many channel packets");
close();
return false;
}
} while (curPacket.chained);
// Build channel map: absolute RGBA index -> sequential output offset
int nchannels = 0;
{
for (auto& cp : m_channel_packets)
for (int ch : cp.channels())
if (m_channel_map[ch] == -1)
m_channel_map[ch] = nchannels++;
}
// Get the depth per pixel per channel
TypeDesc chanType = TypeDesc::UINT8;
if (curPacket.size == 16)
chanType = TypeDesc::UINT16;
// Set the details in the ImageSpec
m_spec = ImageSpec(m_pic_header.width, m_pic_header.height, nchannels,
chanType);
if (!check_open(m_spec, { 0, 65535, 0, 65535, 0, 1, 0, 4 })) {
close();
return false;
}
m_spec.attribute("BitsPerSample", (int)curPacket.size);
m_spec.attribute("softimage:compression", Strutil::join(encodings, ","));
if (m_pic_header.comment[0] != 0) {
char comment[80];
Strutil::safe_strcpy(comment, m_pic_header.comment, 80);
m_spec.attribute("ImageDescription", comment);
}
// Build the scanline index
fpos_t curPos;
fgetpos(m_fd, &curPos);
m_scanline_markers.push_back(curPos);
spec = m_spec;
return true;
}
bool
SoftimageInput::read_native_scanline(int subimage, int miplevel, int y,
int /*z*/, void* data)
{
lock_guard lock(*this);
if (!seek_subimage(subimage, miplevel))
return false;
bool result = false;
if (y == (int)m_scanline_markers.size() - 1) {
// we're up to this scanline
result = read_next_scanline(data);
// save the marker for the next scanline if we haven't got the who images
if (m_scanline_markers.size() < m_pic_header.height) {
fpos_t curPos;
fgetpos(m_fd, &curPos);
m_scanline_markers.push_back(curPos);
}
} else if (y >= (int)m_scanline_markers.size()) {
// we haven't yet read this far
fpos_t curPos;
// Store the ones before this without pulling the pixels
do {
if (!read_next_scanline(NULL))
return false;
fgetpos(m_fd, &curPos);
m_scanline_markers.push_back(curPos);
} while ((int)m_scanline_markers.size() <= y);
result = read_next_scanline(data);
fgetpos(m_fd, &curPos);
m_scanline_markers.push_back(curPos);
} else {
// We've already got the index for this scanline and moved past
// Let's seek to the scanline's data
if (fsetpos(m_fd, &m_scanline_markers[y])) {
errorfmt("Failed to seek to scanline {} in \"{}\"", y, m_filename);
close();
return false;
}
result = read_next_scanline(data);
// If the index isn't complete let's shift the file pointer back to the latest readline
if (m_scanline_markers.size() < m_pic_header.height) {
if (fsetpos(m_fd,
&m_scanline_markers[m_scanline_markers.size() - 1])) {
errorfmt("Failed to restore to scanline {} in \"{}\"",
m_scanline_markers.size() - 1, m_filename);
close();
return false;
}
}
}
return result;
}
bool
SoftimageInput::close()
{
if (m_fd) {
fclose(m_fd);
m_fd = NULL;
}
init();
return true;
}
const char*
SoftimageInput::encoding_name(int encoding)
{
switch (encoding & 0x3) {
case UNCOMPRESSED: return "none";
case PURE_RUN_LENGTH: return "rle";
case MIXED_RUN_LENGTH: return "mixed-rle";
default: return "unknown";
}
}
bool
SoftimageInput::read_next_scanline(void* data)
{
// Each scanline is stored using one or more channel packets.
// We go through each of those to pull the data
for (auto& cp : m_channel_packets) {
bool ok = false;
int type = int(cp.type) & 0x3;
if (type == UNCOMPRESSED) {
ok = read_pixels_uncompressed(cp, data);
} else if (type == PURE_RUN_LENGTH) {
ok = read_pixels_pure_run_length(cp, data);
} else if (type == MIXED_RUN_LENGTH) {
ok = read_pixels_mixed_run_length(cp, data);
}
if (!ok) {
errorfmt("Failed to read channel packed type {:d} from \"{}\"",
int(cp.type), m_filename);
close();
return false;
}
}
return true;
}
bool
SoftimageInput::read_pixels_uncompressed(
const softimage_pvt::ChannelPacket& curPacket, void* data)
{
// We're going to need to use the channels more than once
std::vector<int> channels = curPacket.channels();
// We'll need to use the pixelChannelSize a bit
size_t pixelChannelSize = curPacket.size / 8;
if (data) {
// data pointer is set so we're supposed to write data there
uint8_t* scanlineData = (uint8_t*)data;
for (size_t pixelX = 0; pixelX < m_pic_header.width; pixelX++) {
for (int channel : channels) {
for (size_t byte = 0; byte < pixelChannelSize; byte++) {
// Get which byte we should be placing this in depending on endianness
size_t curByte = byte;
if (littleendian())
curByte = ((pixelChannelSize)-1) - curByte;
//read the data into the correct place
if (fread(&scanlineData[(pixelX * pixelChannelSize
* m_spec.nchannels)
+ (m_channel_map[channel]
* pixelChannelSize)
+ curByte],
1, 1, m_fd)
!= 1)
return false;
}
}
}
} else {
// data pointer is null so we should just seek to the next scanline
// If the seek fails return false
if (fseek(m_fd, m_pic_header.width * pixelChannelSize * channels.size(),
SEEK_CUR))
return false;
}
return true;
}
bool
SoftimageInput::read_pixels_pure_run_length(
const softimage_pvt::ChannelPacket& curPacket, void* data)
{
// How many pixels we've read so far this line
size_t linePixelCount = 0;
// Number of repeats of this value
uint8_t curCount = 0;
// We'll need to use the pixelChannelSize a bit
size_t pixelChannelSize = curPacket.size / 8;
// We're going to need to use the channels more than once
std::vector<int> channels = curPacket.channels();
// Read the pixels until we've read them all
while (linePixelCount < m_pic_header.width) {
// Read the repeats for the run length - return false if read fails
if (fread(&curCount, 1, 1, m_fd) != 1)
return false;
// Clamp to avoid writing past the end of the scanline buffer
if (linePixelCount + curCount > m_pic_header.width)
curCount = m_pic_header.width - linePixelCount;
if (data) {
// data pointer is set so we're supposed to write data there
size_t pixelSize = pixelChannelSize * channels.size();
uint8_t* pixelData = new uint8_t[pixelSize];
if (fread(pixelData, pixelSize, 1, m_fd) != pixelSize)
return false;
// Now we've got the pixel value we need to push it into the data
uint8_t* scanlineData = (uint8_t*)data;
for (size_t pixelX = linePixelCount;
pixelX < linePixelCount + curCount; pixelX++) {
for (size_t curChan = 0; curChan < channels.size(); curChan++) {
for (size_t byte = 0; byte < pixelChannelSize; byte++) {
// Get which byte we should be placing this in depending on endianness
size_t curByte = byte;
if (littleendian())
curByte = ((pixelChannelSize)-1) - curByte;
//put the data into the correct place
scanlineData[(pixelX * pixelChannelSize
* m_spec.nchannels)
+ (m_channel_map[channels[curChan]]
* pixelChannelSize)
+ curByte]
= pixelData[(curChan * pixelChannelSize) + curByte];
}
}
}
delete[] pixelData;
} else {
// data pointer is null so we should just seek to the next scanline
// If the seek fails return false
if (fseek(m_fd, pixelChannelSize * channels.size(), SEEK_CUR))
return false;
}
// Add these pixels to the current pixel count
linePixelCount += curCount;
}
return true;
}
bool
SoftimageInput::read_pixels_mixed_run_length(
const softimage_pvt::ChannelPacket& curPacket, void* data)
{
// How many pixels we've read so far this line
size_t linePixelCount = 0;
// Number of repeats of this value
uint8_t curCount = 0;
// We'll need to use the pixelChannelSize a bit
size_t pixelChannelSize = curPacket.size / 8;
// We're going to need to use the channels more than once
std::vector<int> channels = curPacket.channels();
// Read the pixels until we've read them all
while (linePixelCount < m_pic_header.width) {
// Read the repeats for the run length - return false if read fails
if (fread(&curCount, 1, 1, m_fd) != 1)
return false;
if (curCount < 128) {
// It's a raw packet - so this means the count is 1 less then the actual value
curCount++;
// Just to be safe let's make sure this wouldn't take us
// past the end of this scanline
if (curCount + linePixelCount > m_pic_header.width)
curCount = m_pic_header.width - linePixelCount;
if (data) {
// data pointer is set so we're supposed to write data there
uint8_t* scanlineData = (uint8_t*)data;
for (size_t pixelX = linePixelCount;
pixelX < linePixelCount + curCount; pixelX++) {
for (int channel : channels) {
for (size_t byte = 0; byte < pixelChannelSize; byte++) {
// Get which byte we should be placing this in depending on endianness
size_t curByte = byte;
if (littleendian())
curByte = ((pixelChannelSize)-1) - curByte;
//read the data into the correct place
if (fread(&scanlineData[(pixelX * pixelChannelSize
* m_spec.nchannels)
+ (m_channel_map[channel]
* pixelChannelSize)
+ curByte],
1, 1, m_fd)
!= 1)
return false;
}
}
}
} else {
// data pointer is null so we should just seek to the
// next scanline If the seek fails return false.
if (fseek(m_fd, curCount * pixelChannelSize * channels.size(),
SEEK_CUR))
return false;
}
// Add these pixels to the current pixel count
linePixelCount += curCount;
} else {
// It's a run length encoded packet
uint16_t longCount = 0;
if (curCount == 128) {
// This is a long count so the next 16bits of the file
// are an unsigned int containing the count. If the
// read fails we should return false.
if (fread(&longCount, 1, 2, m_fd) != 2)
return false;
// longCount is in big endian format - if we're not
// let's swap it
if (littleendian())
OIIO::swap_endian(&longCount);
} else {
longCount = curCount - 127;
}
// Clamp to avoid writing past the end of the scanline buffer
if (linePixelCount + longCount > m_pic_header.width)
longCount = m_pic_header.width - linePixelCount;
if (data) {
// data pointer is set so we're supposed to write data there
size_t pixelSize = pixelChannelSize * channels.size();
uint8_t* pixelData = new uint8_t[pixelSize];
if (fread(pixelData, 1, pixelSize, m_fd) != pixelSize)
return false;
// Now we've got the pixel value we need to push it into
// the data.
uint8_t* scanlineData = (uint8_t*)data;
for (size_t pixelX = linePixelCount;
pixelX < linePixelCount + longCount; pixelX++) {
for (size_t curChan = 0; curChan < channels.size();
curChan++) {
for (size_t byte = 0; byte < pixelChannelSize; byte++) {
// Get which byte we should be placing this
// in depending on endianness.
size_t curByte = byte;
if (littleendian())
curByte = ((pixelChannelSize)-1) - curByte;
//put the data into the correct place
scanlineData[(pixelX * pixelChannelSize
* m_spec.nchannels)
+ (m_channel_map[channels[curChan]]
* pixelChannelSize)
+ curByte]
= pixelData[(curChan * pixelChannelSize)
+ curByte];
}
}
}
delete[] pixelData;
} else {
// data pointer is null so we should just seek to the
// next scanline. If the seek fails return false.
if (fseek(m_fd, pixelChannelSize * channels.size(), SEEK_CUR))
return false;
}
// Add these pixels to the current pixel count.
linePixelCount += longCount;
}
}
return true;
}
OIIO_PLUGIN_NAMESPACE_END