-
Notifications
You must be signed in to change notification settings - Fork 667
Expand file tree
/
Copy pathiffinput.cpp
More file actions
773 lines (633 loc) · 25.9 KB
/
iffinput.cpp
File metadata and controls
773 lines (633 loc) · 25.9 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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// Copyright Contributors to the OpenImageIO project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/OpenImageIO/oiio
#include "iff_pvt.h"
#include <cmath>
OIIO_PLUGIN_NAMESPACE_BEGIN
using namespace iff_pvt;
class IffInput final : public ImageInput {
public:
IffInput() { init(); }
~IffInput() override { close(); }
const char* format_name(void) const override { return "iff"; }
int supports(string_view feature) const override
{
return feature == "ioproxy";
}
bool open(const std::string& name, ImageSpec& spec) override;
bool open(const std::string& name, ImageSpec& newspec,
const ImageSpec& config) override;
bool close(void) override;
bool read_native_scanline(int subimage, int miplevel, int y, int z,
void* data) override;
bool read_native_tile(int subimage, int miplevel, int x, int y, int z,
void* data) override;
private:
std::string m_filename;
iff_pvt::IffFileHeader m_iff_header;
std::vector<uint8_t> m_buf;
uint32_t m_tbmp_start;
// init to initialize state
void init(void)
{
ioproxy_clear();
m_filename.clear();
m_buf.clear();
}
// Reads information about IFF file. If errors are encountereed,
// read_header wil. issue error messages and return false.
bool read_header();
// helper to read an image
bool readimg(void);
// helper to uncompress a rle channel
size_t uncompress_rle_channel(const uint8_t* in, uint8_t* out, int size);
/// Helper: read buf[0..nitems-1], swap endianness if necessary
template<typename T> bool read(T* buf, size_t nitems = 1)
{
if (!ioread(buf, sizeof(T), nitems))
return false;
if (littleendian()
&& (is_same<T, uint16_t>::value || is_same<T, int16_t>::value
|| is_same<T, uint32_t>::value || is_same<T, int32_t>::value)) {
swap_endian(buf, nitems);
}
return true;
}
bool read_str(std::string& val, uint32_t len, uint32_t round = 4)
{
const uint32_t big = 1024;
char strbuf[big];
len = std::min(len, big);
bool ok = ioread(strbuf, len);
val.assign(strbuf, len);
ok &= ioseek(len % round, SEEK_CUR);
return ok;
}
bool read_type_len(std::string& type, uint32_t& len)
{
return read_str(type, 4) && read(&len);
}
bool read_meta_string(std::string& name, std::string& val)
{
uint32_t len = 0;
return read_type_len(name, len) && read_str(val, len);
}
// Read a 4-byte type code (no endian swap), and if that succeeds (beware
// of EOF or other errors), then also read a 32 bit size (subject to
// endian swap).
bool read_typesize(uint8_t type[4], uint32_t& size)
{
return ioread(type, 1, 4) && read(&size);
}
};
// Obligatory material to make this a recognizable imageio plugin
OIIO_PLUGIN_EXPORTS_BEGIN
OIIO_EXPORT int iff_imageio_version = OIIO_PLUGIN_VERSION;
OIIO_EXPORT const char*
iff_imageio_library_version()
{
return nullptr;
}
OIIO_EXPORT ImageInput*
iff_input_imageio_create()
{
return new IffInput;
}
OIIO_EXPORT const char* iff_input_extensions[] = { "iff", "z", nullptr };
OIIO_PLUGIN_EXPORTS_END
bool
IffInput::open(const std::string& name, ImageSpec& newspec,
const ImageSpec& config)
{
// Check 'config' for any special requests
ioproxy_retrieve_from_config(config);
return open(name, newspec);
}
bool
IffInput::open(const std::string& name, ImageSpec& spec)
{
// Autodesk Maya documentation:
// "Maya Image File Format - IFF
//
// Maya supports images in the Interchange File Format (IFF).
// IFF is a generic structured file access mechanism, and is not only
// limited to images.
//
// The openimageio IFF implementation deals specifically with Maya IFF
// images with it's data blocks structured as follows:
//
// Header:
// FOR4 <size> CIMG
// TBHD <size> flags, width, height, compression ...
// AUTH <size> attribute ...
// DATE <size> attribute ...
// FOR4 <size> TBMP
// Tiles:
// RGBA <size> tile pixels
// RGBA <size> tile pixels
// RGBA <size> tile pixels
// ...
// saving 'name' for later use
m_filename = name;
if (!ioproxy_use_or_open(name))
return false;
ioseek(0);
// we read header of the file that we think is IFF file
if (!read_header()) {
close();
return false;
}
// image specification
m_spec = ImageSpec(m_iff_header.width, m_iff_header.height,
m_iff_header.pixel_channels,
m_iff_header.pixel_bits == 8 ? TypeDesc::UINT8
: TypeDesc::UINT16);
// set x, y
m_spec.x = m_iff_header.x;
m_spec.y = m_iff_header.y;
// set full width, height
m_spec.full_width = m_iff_header.width;
m_spec.full_height = m_iff_header.height;
// tiles
if (m_iff_header.tile_width > 0 && m_iff_header.tile_height > 0) {
m_spec.tile_width = m_iff_header.tile_width;
m_spec.tile_height = m_iff_header.tile_height;
// only 1 subimage for IFF
m_spec.tile_depth = 1;
} else {
errorfmt("\"{}\": wrong tile size", m_filename);
close();
return false;
}
// attributes
// compression
if (m_iff_header.compression == iff_pvt::RLE) {
m_spec.attribute("compression", "rle");
}
// author
if (m_iff_header.author.size()) {
m_spec.attribute("Artist", m_iff_header.author);
}
// date
if (m_iff_header.date.size()) {
m_spec.attribute("DateTime", m_iff_header.date);
}
// file pointer is set to the beginning of tbmp data
// we save this position - it will be helpful in read_native_tile
m_tbmp_start = m_iff_header.tbmp_start;
spec = m_spec;
return true;
}
bool
IffInput::read_header()
{
uint8_t type[4];
uint32_t size;
uint32_t chunksize;
uint32_t tbhdsize;
uint32_t flags;
uint16_t bytes;
uint16_t prnum;
uint16_t prden;
// read FOR4 <size> CIMG.
for (;;) {
// get type and length
if (!read_typesize(type, size))
return false;
chunksize = align_size(size, 4);
if (type[0] == 'F' && type[1] == 'O' && type[2] == 'R'
&& type[3] == '4') {
// get type
if (!ioread(&type, 1, sizeof(type)))
return false;
// check if CIMG
if (type[0] == 'C' && type[1] == 'I' && type[2] == 'M'
&& type[3] == 'G') {
// read TBHD.
for (;;) {
if (!read_typesize(type, size))
return false;
chunksize = align_size(size, 4);
if (type[0] == 'T' && type[1] == 'B' && type[2] == 'H'
&& type[3] == 'D') {
tbhdsize = size;
// test if table header size is correct
if (tbhdsize != 24 && tbhdsize != 32) {
errorfmt("Bad table ehader size {}", tbhdsize);
return false; // bad table header
}
// get width and height
if (!read(&m_iff_header.width)
|| !read(&m_iff_header.height) || !read(&prnum)
|| !read(&prden) || !read(&flags) || !read(&bytes)
|| !read(&m_iff_header.tiles)
|| !read(&m_iff_header.compression)) {
return false;
}
// get xy
if (tbhdsize == 32) {
if (!read(&m_iff_header.x)
|| !read(&m_iff_header.y)) {
return false;
}
} else {
m_iff_header.x = 0;
m_iff_header.y = 0;
}
// tiles
if (m_iff_header.tiles == 0) {
errorfmt("non-tiles are not supported");
return false;
}
// 0 no compression
// 1 RLE compression
// 2 QRL (not supported)
// 3 QR4 (not supported)
if (m_iff_header.compression > 1) {
errorfmt("only RLE compression is supported");
return false;
}
// test format.
if (flags & RGBA) {
// test if black is set
OIIO_DASSERT(!(flags & BLACK));
// test for RGB channels.
if (flags & RGB)
m_iff_header.pixel_channels = 3;
// test for alpha channel
if (flags & ALPHA)
m_iff_header.pixel_channels++;
// test pixel bits
m_iff_header.pixel_bits = bytes ? 16 : 8;
}
// Z format.
else if (flags & ZBUFFER) {
m_iff_header.pixel_channels = 1;
m_iff_header.pixel_bits = 32; // 32bit
// NOTE: Z_F32 support - not supported
OIIO_DASSERT(bytes == 0);
}
// read AUTH, DATE or FOR4
for (;;) {
// get type
if (!read_typesize(type, size))
return false;
chunksize = align_size(size, 4);
if (type[0] == 'A' && type[1] == 'U'
&& type[2] == 'T' && type[3] == 'H') {
std::vector<char> str(chunksize);
if (!ioread(&str[0], 1, chunksize))
return false;
m_iff_header.author = std::string(&str[0],
size);
} else if (type[0] == 'D' && type[1] == 'A'
&& type[2] == 'T' && type[3] == 'E') {
std::vector<char> str(chunksize);
if (!ioread(&str[0], 1, chunksize))
return false;
m_iff_header.date = std::string(&str[0], size);
} else if (type[0] == 'F' && type[1] == 'O'
&& type[2] == 'R' && type[3] == '4') {
if (!ioread(&type, 1, sizeof(type)))
return false;
// check if CIMG
if (type[0] == 'T' && type[1] == 'B'
&& type[2] == 'M' && type[3] == 'P') {
// tbmp position for later user in in
// read_native_tile
m_iff_header.tbmp_start = (uint32_t)iotell();
// read first RGBA block to detect tile size.
for (unsigned int t = 0;
t < m_iff_header.tiles; t++) {
if (!read_typesize(type, size))
return false;
chunksize = align_size(size, 4);
// check if RGBA
if (type[0] == 'R' && type[1] == 'G'
&& type[2] == 'B'
&& type[3] == 'A') {
// get tile coordinates.
uint16_t xmin, xmax, ymin, ymax;
if (!read(&xmin) || !read(&ymin)
|| !read(&xmax) || !read(&ymax))
return false;
// check tile
if (xmin > xmax || ymin > ymax
|| xmax >= m_iff_header.width
|| ymax >= m_iff_header.height)
return false;
// set tile width and height
m_iff_header.tile_width
= xmax - xmin + 1;
m_iff_header.tile_height
= ymax - ymin + 1;
// done, return
return true;
}
// skip to the next block.
if (!ioseek(chunksize, SEEK_CUR))
return false;
}
} else {
// skip to the next block.
if (!ioseek(chunksize, SEEK_CUR))
return false;
}
} else {
// skip to the next block.
if (!ioseek(chunksize, SEEK_CUR))
return false;
}
}
// TBHD done, break
break;
}
// skip to the next block.
if (!ioseek(chunksize, SEEK_CUR))
return false;
}
}
}
// skip to the next block.
if (!ioseek(chunksize, SEEK_CUR))
return false;
}
errorfmt("unknown error reading header");
return false;
}
bool
IffInput::read_native_scanline(int /*subimage*/, int /*miplevel*/, int /*y*/,
int /*z*/, void* /*data*/)
{
// scanline not used for Maya IFF, uses tiles instead.
return false;
}
bool
IffInput::read_native_tile(int subimage, int miplevel, int x, int y, int /*z*/,
void* data)
{
lock_guard lock(*this);
if (!seek_subimage(subimage, miplevel))
return false;
if (m_buf.empty())
readimg();
// tile size
int w = m_spec.width;
int tw = std::min(x + m_spec.tile_width, m_spec.width) - x;
int th = std::min(y + m_spec.tile_height, m_spec.height) - y;
// tile data
int oy = 0;
for (int iy = y; iy < y + th; iy++) {
// in
uint8_t* in_p = &m_buf[0] + (iy * w + x) * m_spec.pixel_bytes();
// out
uint8_t* out_p = (uint8_t*)data
+ (oy * m_spec.tile_width) * m_spec.pixel_bytes();
// copy
memcpy(out_p, in_p, tw * m_spec.pixel_bytes());
oy++;
}
return true;
}
bool inline IffInput::close(void)
{
init();
return true;
}
bool
IffInput::readimg()
{
uint8_t type[4];
uint32_t size;
uint32_t chunksize;
// seek pos
// set position tile may be called randomly
ioseek(m_tbmp_start);
// resize buffer
m_buf.resize(m_spec.image_bytes());
for (unsigned int t = 0; t < m_iff_header.tiles;) {
// get type and length
if (!ioread(&type, 1, sizeof(type)) || !read(&size))
return false;
chunksize = align_size(size, 4);
// check if RGBA
if (type[0] == 'R' && type[1] == 'G' && type[2] == 'B'
&& type[3] == 'A') {
// get tile coordinates.
uint16_t xmin, xmax, ymin, ymax;
if (!read(&xmin) || !read(&ymin) || !read(&xmax) || !read(&ymax))
return false;
// get tile width/height
uint32_t tw = xmax - xmin + 1;
uint32_t th = ymax - ymin + 1;
// get image size
// skip coordinates, uint16_t (2) * 4 = 8
uint32_t image_size = chunksize - 8;
// check tile
if (xmin > xmax || ymin > ymax || xmax >= m_spec.width
|| ymax >= m_spec.height || !tw || !th) {
return false;
}
// tile compress
bool tile_compress = false;
// if tile compression fails to be less than image data stored
// uncompressed the tile is written uncompressed
// set channels
uint8_t channels = m_iff_header.pixel_channels;
// set tile size
size_t tile_size = tw * th * channels * m_spec.channel_bytes()
+ 8;
// test if compressed
// we use the non aligned size
if (tile_size > size) {
tile_compress = true;
}
// handle 8-bit data.
if (m_iff_header.pixel_bits == 8) {
std::vector<uint8_t> scratch;
// set bytes.
scratch.resize(image_size);
if (!ioread(scratch.data(), 1, scratch.size()))
return false;
// set tile data
uint8_t* p = static_cast<uint8_t*>(&scratch[0]);
// tile compress.
if (tile_compress) {
// map BGR(A) to RGB(A)
for (int c = (int)(channels * m_spec.channel_bytes()) - 1;
c >= 0; --c) {
std::vector<uint8_t> in(tw * th);
uint8_t* in_p = &in[0];
// uncompress and increment
p += uncompress_rle_channel(p, in_p, tw * th);
// set tile
for (uint16_t py = ymin; py <= ymax; py++) {
uint8_t* out_dy = static_cast<uint8_t*>(&m_buf[0])
+ (py * m_spec.width)
* m_spec.pixel_bytes();
for (uint16_t px = xmin; px <= xmax; px++) {
uint8_t* out_p
= out_dy + px * m_spec.pixel_bytes() + c;
*out_p++ = *in_p++;
}
}
}
} else {
int sy = 0;
for (uint16_t py = ymin; py <= ymax; py++) {
uint8_t* out_dy = static_cast<uint8_t*>(&m_buf[0])
+ (py * m_spec.width + xmin)
* m_spec.pixel_bytes();
// set tile
int sx = 0;
for (uint16_t px = xmin; px <= xmax; px++) {
uint8_t* in_p
= p + (sy * tw + sx) * m_spec.pixel_bytes();
// map BGR(A) to RGB(A)
for (int c = channels - 1; c >= 0; --c) {
uint8_t* out_p = in_p
+ (c * m_spec.channel_bytes());
*out_dy++ = *out_p;
}
sx++;
}
sy++;
}
}
}
// handle 16-bit data.
else if (m_iff_header.pixel_bits == 16) {
std::vector<uint8_t> scratch;
// set bytes.
scratch.resize(image_size);
if (!ioread(scratch.data(), 1, scratch.size()))
return false;
// set tile data
uint8_t* p = static_cast<uint8_t*>(&scratch[0]);
if (tile_compress) {
// set map
std::vector<uint8_t> map;
if (littleendian()) {
int rgb16[] = { 0, 2, 4, 1, 3, 5 };
int rgba16[] = { 0, 2, 4, 6, 1, 3, 5, 7 };
if (m_iff_header.pixel_channels == 3) {
map = std::vector<uint8_t>(rgb16, &rgb16[6]);
} else {
map = std::vector<uint8_t>(rgba16, &rgba16[8]);
}
} else {
int rgb16[] = { 1, 3, 5, 0, 2, 4 };
int rgba16[] = { 1, 3, 5, 7, 0, 2, 4, 6 };
if (m_iff_header.pixel_channels == 3) {
map = std::vector<uint8_t>(rgb16, &rgb16[6]);
} else {
map = std::vector<uint8_t>(rgba16, &rgba16[8]);
}
}
// map BGR(A)BGR(A) to RRGGBB(AA)
for (size_t c = (channels * m_spec.channel_bytes()) - 1;
c >= 0; --c) {
int mc = map[c];
std::vector<uint8_t> in(tw * th);
uint8_t* in_p = &in[0];
// uncompress and increment
p += uncompress_rle_channel(p, in_p, tw * th);
// set tile
for (uint16_t py = ymin; py <= ymax; py++) {
uint8_t* out_dy = static_cast<uint8_t*>(&m_buf[0])
+ (py * m_spec.width)
* m_spec.pixel_bytes();
for (uint16_t px = xmin; px <= xmax; px++) {
uint8_t* out_p
= out_dy + px * m_spec.pixel_bytes() + mc;
*out_p++ = *in_p++;
}
}
}
} else {
int sy = 0;
for (uint16_t py = ymin; py <= ymax; py++) {
uint8_t* out_dy = static_cast<uint8_t*>(&m_buf[0])
+ (py * m_spec.width + xmin)
* m_spec.pixel_bytes();
// set scanline, make copy easier
std::vector<uint16_t> scanline(tw
* m_spec.pixel_bytes());
uint16_t* sl_p = &scanline[0];
// set tile
int sx = 0;
for (uint16_t px = xmin; px <= xmax; px++) {
uint8_t* in_p
= p + (sy * tw + sx) * m_spec.pixel_bytes();
// map BGR(A) to RGB(A)
for (int c = channels - 1; c >= 0; --c) {
uint16_t pixel;
uint8_t* out_p = in_p
+ (c * m_spec.channel_bytes());
memcpy(&pixel, out_p, 2);
// swap endianness
if (littleendian()) {
swap_endian(&pixel);
}
*sl_p++ = pixel;
}
sx++;
}
// copy data
memcpy(out_dy, &scanline[0], tw * m_spec.pixel_bytes());
sy++;
}
}
} else {
errorfmt("\"{}\": unsupported number of bits per pixel for tile",
m_filename);
return false;
}
// tile
t++;
} else {
// skip to the next block
if (!ioseek(chunksize))
return false;
}
}
// flip buffer to make read_native_tile easier,
// from tga.imageio:
size_t bytespp = m_spec.pixel_bytes();
std::vector<unsigned char> flip(m_spec.width * bytespp);
unsigned char *src, *dst, *tmp = &flip[0];
for (int y = 0; y < m_spec.height / 2; y++) {
src = &m_buf[(m_spec.height - y - 1) * m_spec.width * bytespp];
dst = &m_buf[y * m_spec.width * bytespp];
memcpy(tmp, src, m_spec.width * bytespp);
memcpy(src, dst, m_spec.width * bytespp);
memcpy(dst, tmp, m_spec.width * bytespp);
}
return true;
}
size_t
IffInput::uncompress_rle_channel(const uint8_t* in, uint8_t* out, int size)
{
const uint8_t* const _in = in;
const uint8_t* const end = out + size;
while (out < end) {
// information.
const uint8_t count = (*in & 0x7f) + 1;
const bool run = (*in & 0x80) ? true : false;
++in;
// find runs
if (!run) {
// verbatim
for (int i = 0; i < count; i++)
*out++ = *in++;
} else {
// duplicate
const uint8_t p = *in++;
for (int i = 0; i < count; i++)
*out++ = p;
}
}
const size_t r = in - _in;
return r;
}
OIIO_PLUGIN_NAMESPACE_END