forked from AcademySoftwareFoundation/OpenImageIO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjpeg2000input.cpp
More file actions
808 lines (686 loc) · 24.9 KB
/
jpeg2000input.cpp
File metadata and controls
808 lines (686 loc) · 24.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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// Copyright Contributors to the OpenImageIO project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/AcademySoftwareFoundation/OpenImageIO
#include <cstdarg>
#include <cstdio>
#include <vector>
#include <openjpeg.h>
#include <opj_config.h>
#include <OpenImageIO/filesystem.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/sysutil.h>
#include <OpenImageIO/tiffutils.h>
#ifdef USE_OPENJPH
# include <openjph/ojph_codestream.h>
# include <openjph/ojph_file.h>
OIIO_PRAGMA_WARNING_PUSH
OIIO_GCC_PRAGMA(GCC diagnostic ignored "-Wdelete-incomplete")
# include <openjph/ojph_mem.h>
OIIO_PRAGMA_WARNING_POP
# include <openjph/ojph_message.h>
# include <openjph/ojph_params.h>
#endif
#ifndef OIIO_OPJ_VERSION
# if defined(OPJ_VERSION_MAJOR)
// OpenJPEG >= 2.1 defines these symbols
# define OIIO_OPJ_VERSION \
(OPJ_VERSION_MAJOR * 10000 + OPJ_VERSION_MINOR * 100 \
+ OPJ_VERSION_BUILD)
# else
// Older, assume it's the minimum of 2.0
# define OIIO_OPJ_VERSION 20000
# endif
#endif
OIIO_PLUGIN_NAMESPACE_BEGIN
namespace {
// TODO(sergey): This actually a straight duplication from the png reader,
// consider de-duplicating the code somehow?
template<class T>
void
j2k_associateAlpha(T* data, int size, int channels, int alpha_channel,
float gamma)
{
T max = std::numeric_limits<T>::max();
if (gamma == 1) {
for (int x = 0; x < size; ++x, data += channels)
for (int c = 0; c < channels; c++)
if (c != alpha_channel) {
unsigned int f = data[c];
data[c] = (f * data[alpha_channel]) / max;
}
} else { //With gamma correction
float inv_max = 1.0 / max;
for (int x = 0; x < size; ++x, data += channels) {
float alpha_associate = pow(data[alpha_channel] * inv_max, gamma);
// We need to transform to linear space, associate the alpha, and
// then transform back. That is, if D = data[c], we want
//
// D' = max * ( (D/max)^(1/gamma) * (alpha/max) ) ^ gamma
//
// This happens to simplify to something which looks like
// multiplying by a nonlinear alpha:
//
// D' = D * (alpha/max)^gamma
for (int c = 0; c < channels; c++)
if (c != alpha_channel)
data[c] = static_cast<T>(data[c] * alpha_associate);
}
}
}
} // namespace
#ifdef USE_OPENJPH
// A wrapper for ojph::infile_base to use OIIO's IOProxy
class jph_infile : public ojph::infile_base {
private:
Filesystem::IOProxy* ioproxy;
public:
jph_infile(Filesystem::IOProxy* iop) { ioproxy = iop; }
~jph_infile()
{
// if (ioproxy != NULL)
// ioproxy->close();
}
//read reads size bytes, returns the number of bytes read
size_t read(void* ptr, size_t size) { return ioproxy->read(ptr, size); }
//seek returns 0 on success
int seek(ojph::si64 offset, enum infile_base::seek origin)
{
return ioproxy->seek(offset, origin);
}
ojph::si64 tell() { return ioproxy->tell(); };
bool eof()
{
int64_t pos = ioproxy->tell();
if (pos < 0)
return false; // Error condition, not EOF
return pos == static_cast<int64_t>(ioproxy->size());
}
void close()
{
ioproxy->close();
ioproxy = NULL;
};
};
#endif
class Jpeg2000Input final : public ImageInput {
public:
Jpeg2000Input() { init(); }
~Jpeg2000Input() override { close(); }
const char* format_name(void) const override { return "jpeg2000"; }
int supports(string_view feature) const override
{
return feature == "ioproxy";
// FIXME: we should support Exif/IPTC, but currently don't.
}
bool valid_file(Filesystem::IOProxy* ioproxy) const override;
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;
private:
std::string m_filename;
std::vector<int> m_bpp; // per channel bpp
opj_image_t* m_image;
opj_codec_t* m_codec;
opj_stream_t* m_stream;
bool m_keep_unassociated_alpha; // Do not convert unassociated alpha
void init(void);
static bool is_jp2_header(const uint8_t header[12]);
static bool is_j2k_header(const uint8_t header[5]);
opj_codec_t* create_decompressor();
void destroy_decompressor();
void destroy_stream()
{
if (m_stream) {
opj_stream_destroy(m_stream);
m_stream = NULL;
}
}
template<typename T> void copy_scanline(int y, int z, void* data);
uint16_t baseTypeConvertU10ToU16(int src)
{
return (uint16_t)((src << 6) | (src >> 4));
}
uint16_t baseTypeConvertU12ToU16(int src)
{
return (uint16_t)((src << 4) | (src >> 8));
}
template<typename T> void yuv_to_rgb(T* p_scanline)
{
for (int x = 0, i = 0; x < m_spec.width; ++x, i += m_spec.nchannels) {
float y = convert_type<T, float>(p_scanline[i + 0]);
float u = convert_type<T, float>(p_scanline[i + 1]) - 0.5f;
float v = convert_type<T, float>(p_scanline[i + 2]) - 0.5f;
float r = y + 1.402 * v;
float g = y - 0.344 * u - 0.714 * v;
float b = y + 1.772 * u;
p_scanline[i + 0] = convert_type<float, T>(r);
p_scanline[i + 1] = convert_type<float, T>(g);
p_scanline[i + 2] = convert_type<float, T>(b);
}
}
void setup_event_mgr(opj_codec_t* codec)
{
opj_set_error_handler(codec, openjpeg_error_callback, this);
opj_set_warning_handler(codec, openjpeg_dummy_callback, NULL);
opj_set_info_handler(codec, openjpeg_dummy_callback, NULL);
}
static OPJ_SIZE_T StreamRead(void* p_buffer, OPJ_SIZE_T p_nb_bytes,
void* p_user_data)
{
auto in = static_cast<Jpeg2000Input*>(p_user_data);
auto r = in->ioproxy()->read(p_buffer, p_nb_bytes);
return r ? OPJ_SIZE_T(r) : OPJ_SIZE_T(-1);
}
static OPJ_BOOL StreamSeek(OPJ_OFF_T p_nb_bytes, void* p_user_data)
{
auto in = static_cast<Jpeg2000Input*>(p_user_data);
return in->ioseek(p_nb_bytes, SEEK_SET);
}
static OPJ_OFF_T StreamSkip(OPJ_OFF_T p_nb_bytes, void* p_user_data)
{
auto in = static_cast<Jpeg2000Input*>(p_user_data);
return in->ioseek(p_nb_bytes, SEEK_CUR) ? p_nb_bytes : OPJ_SIZE_T(-1);
}
static void StreamFree(void* p_user_data) {}
static void openjpeg_error_callback(const char* msg, void* data)
{
if (ImageInput* input = (ImageInput*)data) {
input->errorfmt("{}",
msg && msg[0] ? msg : "Unknown OpenJpeg error");
}
}
static void openjpeg_dummy_callback(const char* /*msg*/, void* /*data*/) {}
#ifdef USE_OPENJPH
private: // OJPH code
bool ojph_read_header(); // Read the header and set up the spec
bool ojph_read_image(); // We need to read the image once.
bool ojph_image_read = false; // Have we read the image yet?
bool ojph_reader = false; // Are we using the ojph reader?
std::vector<unsigned char> m_buf; // Buffer the image pixels
int buffer_bpp; // Bytes per pixel in the buffer
ojph::codestream codestream; // The HTJ2K codestream
std::unique_ptr<jph_infile> m_jphinfile;
#endif
};
// Obligatory material to make this a recognizable imageio plugin
OIIO_PLUGIN_EXPORTS_BEGIN
OIIO_EXPORT int jpeg2000_imageio_version = OIIO_PLUGIN_VERSION;
OIIO_EXPORT const char*
jpeg2000_imageio_library_version()
{
return ustring::fmtformat("OpenJpeg {}", opj_version()).c_str();
}
OIIO_EXPORT ImageInput*
jpeg2000_input_imageio_create()
{
return new Jpeg2000Input;
}
OIIO_EXPORT const char* jpeg2000_input_extensions[] = { "jp2", "j2k",
#ifdef USE_OPENJPH
"j2c",
#endif
nullptr };
OIIO_PLUGIN_EXPORTS_END
void
Jpeg2000Input::init(void)
{
m_image = NULL;
m_codec = NULL;
m_stream = NULL;
m_keep_unassociated_alpha = false;
#ifdef USE_OPENJPH
m_jphinfile.reset();
#endif
ioproxy_clear();
}
bool
Jpeg2000Input::valid_file(Filesystem::IOProxy* ioproxy) const
{
if (!ioproxy || ioproxy->mode() != Filesystem::IOProxy::Mode::Read)
return false;
uint8_t header[12];
auto r = ioproxy->pread(header, sizeof(header), 0);
if (r != sizeof(header)) {
return false;
}
return is_jp2_header(header) || is_j2k_header(header);
}
#ifdef USE_OPENJPH
// Convert a 32-bit signed integer to a 16-bit signed integer, with special
// handling for special numbers (NaN, Infinity, etc.) if requested.
ojph::si16
convert_si32_to_si16(const ojph::si32 si32_value,
bool convert_special_numbers_to_finite_numbers = false)
{
if (si32_value > INT16_MAX)
return INT16_MAX;
else if (si32_value < INT16_MIN)
return INT16_MIN;
else if (true == convert_special_numbers_to_finite_numbers) {
const ojph::si16 si16_value = (ojph::si16)si32_value;
half half_value;
half_value.setBits(si16_value);
if (half_value.isFinite())
return si16_value;
// handle non-real number to real-number mapping
if (half_value.isNan())
half_value = 0.0f;
else if (half_value.isInfinity() && !half_value.isNegative())
half_value = HALF_MAX;
else if (half_value.isInfinity() && half_value.isNegative())
half_value = -1.0f * HALF_MAX;
return half_value.bits();
} else
return (ojph::si16)si32_value;
}
bool
Jpeg2000Input::ojph_read_header()
{
ojph::param_siz siz = codestream.access_siz();
int ch = siz.get_num_components();
const int w = siz.get_recon_width(0);
const int h = siz.get_recon_height(0);
TypeDesc dtype;
if (ch > 4)
ch = 4; // Only do the first 4 channels.
m_bpp.resize(ch);
for (int c = 0; c < ch; c++) {
switch (siz.get_bit_depth(c)) {
case 8:
dtype = TypeDesc::UCHAR;
m_bpp[c] = 1;
break;
case 10:
case 12:
case 16:
m_bpp[c] = 2;
dtype = TypeDesc::USHORT;
break;
case 32:
m_bpp[c] = 4;
dtype = TypeDesc::UINT;
break;
default:
errorfmt("Unsupported bit depth {} for channel {}",
siz.get_bit_depth(c), c);
close();
return false;
}
if (m_bpp[c] != m_bpp[0]) {
errorfmt("All channels need to be the same bitdepth");
close();
return false;
}
}
m_spec = ImageSpec(w, h, ch, dtype);
m_spec.default_channel_names();
m_spec.attribute("oiio:BitsPerSample", siz.get_bit_depth(0));
m_spec.set_colorspace("srgb_rec709_scene");
return true;
}
bool
Jpeg2000Input::ojph_read_image()
{
buffer_bpp = m_bpp[0];
int w = m_spec.width;
int h = m_spec.height;
int ch = m_spec.nchannels;
ojph::param_siz siz = codestream.access_siz();
const size_t bufsize
= clamped_mult64(clamped_mult64(uint64_t(w), uint64_t(h)),
clamped_mult64(uint64_t(ch), uint64_t(buffer_bpp)));
m_buf.resize(bufsize);
codestream.create();
int file_bit_depth = siz.get_bit_depth(0); // Assuming RGBA are the same.
// We are going to read the whole image into the buffer, since with openjph
// its hard to easily grab part of the image.
if (codestream.is_planar()) {
for (int c = 0; c < ch; ++c)
for (int i = 0; i < h; ++i) {
ojph::ui32 comp_num;
ojph::line_buf* line = codestream.pull(comp_num);
const ojph::si32* sp = line->i32;
OIIO_DASSERT(int(comp_num) == c);
if (m_spec.format == TypeDesc::UCHAR) {
unsigned char* dout = &m_buf[i * w * ch];
dout += c;
for (int j = w; j > 0; j--, dout += ch) {
*dout = *sp++;
}
}
if (m_spec.format == TypeDesc::USHORT) {
unsigned short* dout
= (unsigned short*)&m_buf[buffer_bpp * (i * w * ch)];
dout += c;
for (int j = w; j > 0; j--, dout += ch) {
*dout = bit_range_convert(*sp++, file_bit_depth,
buffer_bpp * 8);
}
}
}
} else {
for (int i = 0; i < h; ++i) {
for (int c = 0; c < ch; ++c) {
ojph::ui32 comp_num;
ojph::line_buf* line = codestream.pull(comp_num);
const ojph::si32* sp = line->i32;
OIIO_DASSERT(int(comp_num) == c);
if (m_spec.format == TypeDesc::UCHAR) {
unsigned char* dout = &m_buf[i * w * ch];
dout += c;
for (int j = w; j > 0; j--, dout += ch) {
*dout = *sp++;
}
}
if (m_spec.format == TypeDesc::USHORT) {
unsigned short* dout
= (unsigned short*)&m_buf[buffer_bpp * (i * w * ch)];
dout += c;
for (int j = w; j > 0; j--, dout += ch) {
*dout = bit_range_convert(*sp++, file_bit_depth,
buffer_bpp * 8);
}
}
}
}
}
ojph_image_read = true;
return true;
}
class Oiio_Reader_Error_handler : public ojph::message_error {
// This is a special error handler, since in this case, if we get the error-code for not a J2K file, we dont
// want to print out anything. If not, we fall through to the regular error handler.
ojph::message_error* default_error;
public:
Oiio_Reader_Error_handler(ojph::message_error* error)
{
default_error = error;
}
virtual void operator()(int error_code, const char* file_name, int line_num,
const char* fmt, ...)
{
if (error_code == 0x00050044) {
throw std::runtime_error("ojph error: not HTJ2K file");
}
va_list args;
va_start(args, fmt);
default_error[0](error_code, file_name, line_num, fmt, args);
va_end(args);
}
};
#endif // USE_OPENJPH
bool
Jpeg2000Input::open(const std::string& name, ImageSpec& p_spec)
{
m_filename = name;
if (!ioproxy_use_or_open(name))
return false;
#ifdef USE_OPENJPH
m_jphinfile.reset(new jph_infile(ioproxy()));
ojph_reader = true;
ojph::message_error* default_error = ojph::get_error();
// Disable the default OpenJPH error stream to prevent unwanted error output.
// Errors will be handled by the custom error handler (Oiio_Reader_Error_handler) configured below.
ojph::set_error_stream(nullptr);
try {
Oiio_Reader_Error_handler error_handler(default_error);
ojph::configure_error(&error_handler);
codestream.read_headers(m_jphinfile.get());
return ojph_read_header();
} catch (const std::runtime_error& e) {
ojph::configure_error(default_error);
ojph_reader = false;
m_jphinfile.reset();
}
#endif // USE_OPENJPH
ioseek(0);
m_codec = create_decompressor();
if (!m_codec) {
errorfmt("Could not create Jpeg2000 stream decompressor");
close();
return false;
}
setup_event_mgr(m_codec);
opj_dparameters_t parameters;
opj_set_default_decoder_parameters(¶meters);
opj_setup_decoder(m_codec, ¶meters);
#if OIIO_OPJ_VERSION >= 20200
// Set up multithread in OpenJPEG library -- added in OpenJPEG 2.2,
// but it doesn't seem reliably safe until 2.4.
int nthreads = threads();
if (!nthreads)
nthreads = OIIO::get_int_attribute("threads");
opj_codec_set_threads(m_codec, nthreads);
#endif
m_stream = opj_stream_default_create(true /* is_input */);
if (!m_stream) {
errorfmt("Could not create Jpeg2000 stream");
close();
return false;
}
opj_stream_set_user_data(m_stream, this, StreamFree);
opj_stream_set_read_function(m_stream, StreamRead);
opj_stream_set_seek_function(m_stream, StreamSeek);
opj_stream_set_skip_function(m_stream, StreamSkip);
opj_stream_set_user_data_length(m_stream, ioproxy()->size());
// opj_stream_set_write_function(m_stream, StreamWrite);
OIIO_ASSERT(m_image == nullptr);
if (!opj_read_header(m_stream, m_codec, &m_image) || !m_image
|| has_error()) {
if (!has_error())
errorfmt("Could not read Jpeg2000 header");
}
if (!has_error()) {
if (!opj_decode(m_codec, m_stream, m_image)) {
if (!has_error())
errorfmt("Could not decode Jpeg2000 data");
}
}
destroy_decompressor();
destroy_stream();
if (has_error()) {
close();
return false;
}
OIIO_ASSERT(m_image != nullptr);
// we support only one, three or four components in image
const int channelCount = m_image->numcomps;
if (channelCount != 1 && channelCount != 3 && channelCount != 4) {
errorfmt(
"Only images with one, three or four components are supported");
close();
return false;
}
for (int c = 0; c < channelCount; ++c) {
const opj_image_comp_t& comp(m_image->comps[c]);
if (!comp.data) {
errorfmt("Could not read Jpeg2000 component, no channel data {}",
c);
close();
return false;
}
}
unsigned int maxPrecision = 0;
ROI datawindow;
m_bpp.clear();
m_bpp.reserve(channelCount);
std::vector<TypeDesc> chantypes(channelCount, TypeDesc::UINT8);
for (int i = 0; i < channelCount; i++) {
const opj_image_comp_t& comp(m_image->comps[i]);
m_bpp.push_back(comp.prec);
maxPrecision = std::max(comp.prec, maxPrecision);
ROI roichan(comp.x0, comp.x0 + comp.w * comp.dx, comp.y0,
comp.y0 + comp.h * comp.dy);
datawindow = roi_union(datawindow, roichan);
// std::cout << " chan " << i << "\n";
// std::cout << " dx=" << comp.dx << " dy=" << comp.dy
// << " x0=" << comp.x0 << " y0=" << comp.y0
// << " w=" << comp.w << " h=" << comp.h
// << " prec=" << comp.prec << " bpp=" << comp.bpp << "\n";
// std::cout << " sgnd=" << comp.sgnd << " resno_decoded=" << comp.resno_decoded << " factor=" << comp.factor << "\n";
// std::cout << " roichan=" << roichan << "\n";
}
// std::cout << "overall x0=" << m_image->x0 << " y0=" << m_image->y0
// << " x1=" << m_image->x1 << " y1=" << m_image->y1 << "\n";
// std::cout << "color_space=" << m_image->color_space << "\n";
TypeDesc format = (maxPrecision <= 8) ? TypeDesc::UINT8 : TypeDesc::UINT16;
m_spec = ImageSpec(datawindow.width(), datawindow.height(), channelCount,
format);
m_spec.x = datawindow.xbegin;
m_spec.y = datawindow.ybegin;
m_spec.full_x = m_image->x0;
m_spec.full_y = m_image->y0;
m_spec.full_width = m_image->x1;
m_spec.full_height = m_image->y1;
// Validation of resolution
if (!check_open(m_spec,
{ 0, std::numeric_limits<int>::max(), 0,
std::numeric_limits<int>::max(), 0, 1, 0, 16384 })) {
close();
return false;
}
m_spec.attribute("oiio:BitsPerSample", maxPrecision);
m_spec.set_colorspace("srgb_rec709_scene");
if (m_image->icc_profile_len && m_image->icc_profile_buf) {
m_spec.attribute("ICCProfile",
TypeDesc(TypeDesc::UINT8, m_image->icc_profile_len),
m_image->icc_profile_buf);
std::string errormsg;
bool ok = decode_icc_profile(
cspan<uint8_t>((const uint8_t*)m_image->icc_profile_buf,
m_image->icc_profile_len),
m_spec, errormsg);
if (!ok && OIIO::get_int_attribute("imageinput:strict")) {
errorfmt("Possible corrupt file, could not decode ICC profile: {}\n",
errormsg);
return false;
}
}
p_spec = m_spec;
return true;
}
bool
Jpeg2000Input::open(const std::string& name, ImageSpec& newspec,
const ImageSpec& config)
{
// Check 'config' for any special requests
if (config.get_int_attribute("oiio:UnassociatedAlpha", 0) == 1)
m_keep_unassociated_alpha = true;
ioproxy_retrieve_from_config(config);
return open(name, newspec);
}
bool
Jpeg2000Input::read_native_scanline(int subimage, int miplevel, int y, int z,
void* data)
{
lock_guard lock(*this);
if (!seek_subimage(subimage, miplevel))
return false;
#ifdef USE_OPENJPH
if (ojph_reader) {
if (!ojph_image_read)
ojph_read_image();
unsigned char* start
= &m_buf[buffer_bpp * (y * m_spec.width * m_spec.nchannels)];
memcpy(data, start, buffer_bpp * m_spec.width * m_spec.nchannels);
} else {
#endif // USE_OPENJPH
if (m_spec.format == TypeDesc::UINT8)
copy_scanline<uint8_t>(y, z, data);
else
copy_scanline<uint16_t>(y, z, data);
#ifdef USE_OPENJPH
}
#endif
// JPEG2000 specifically dictates unassociated (un-"premultiplied") alpha.
// Convert to associated unless we were requested not to do so.
if (m_spec.alpha_channel != -1 && !m_keep_unassociated_alpha) {
float gamma = m_spec.get_float_attribute("oiio:Gamma", 2.2f);
if (m_spec.format == TypeDesc::UINT16)
j2k_associateAlpha((unsigned short*)data, m_spec.width,
m_spec.nchannels, m_spec.alpha_channel, gamma);
else
j2k_associateAlpha((unsigned char*)data, m_spec.width,
m_spec.nchannels, m_spec.alpha_channel, gamma);
}
return true;
}
inline bool
Jpeg2000Input::close(void)
{
if (m_image) {
opj_image_destroy(m_image);
m_image = NULL;
}
destroy_decompressor();
destroy_stream();
init();
return true;
}
bool
Jpeg2000Input::is_jp2_header(const uint8_t header[12])
{
const uint8_t jp2_header[] = { 0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50,
0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A };
return memcmp(header, jp2_header, sizeof(jp2_header)) == 0;
}
bool
Jpeg2000Input::is_j2k_header(const uint8_t header[5])
{
const uint8_t j2k_header[] = { 0xFF, 0x4F, 0xFF, 0x51, 0x00 };
return memcmp(header, j2k_header, sizeof(j2k_header)) == 0;
}
opj_codec_t*
Jpeg2000Input::create_decompressor()
{
uint8_t header[12];
auto r = ioproxy()->pread(header, sizeof(header), 0);
if (r != sizeof(header)) {
errorfmt("Empty file \"{}\"", m_filename);
return nullptr;
}
return opj_create_decompress(is_jp2_header(header) ? OPJ_CODEC_JP2
: OPJ_CODEC_J2K);
}
void
Jpeg2000Input::destroy_decompressor()
{
if (m_codec) {
opj_destroy_codec(m_codec);
m_codec = NULL;
}
}
template<typename T>
void
Jpeg2000Input::copy_scanline(int y, int /*z*/, void* data)
{
T* scanline = static_cast<T*>(data);
int nc = m_spec.nchannels;
// It's easier to loop over channels
int bits = sizeof(T) * 8;
for (int c = 0; c < nc; ++c) {
const opj_image_comp_t& comp(m_image->comps[c]);
int chan_ybegin = comp.y0, chan_yend = comp.y0 + comp.h * comp.dy;
int chan_xend = comp.w * comp.dx;
int yoff = (y - comp.y0) / comp.dy;
for (int x = 0; x < m_spec.width; ++x) {
if (yoff < chan_ybegin || yoff >= chan_yend || x > chan_xend) {
// Outside the window of this channel
scanline[x * nc + c] = T(0);
} else {
unsigned int val = comp.data[yoff * comp.w + x / comp.dx];
if (comp.sgnd)
val += (1 << (bits / 2 - 1));
scanline[x * nc + c] = (T)bit_range_convert(val, comp.prec,
bits);
}
}
}
if (m_image->color_space == OPJ_CLRSPC_SYCC)
yuv_to_rgb(scanline);
}
OIIO_PLUGIN_NAMESPACE_END