Skip to content

Commit af0c7e9

Browse files
committed
Add OpenJPH fallback
1 parent 2ee9f7a commit af0c7e9

File tree

3 files changed

+123
-77
lines changed

3 files changed

+123
-77
lines changed

cmake/LibraryDefine.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ function(OPENEXR_DEFINE_LIBRARY libname)
4646
if(OPENEXR_CURLIB_CURBINDIR)
4747
target_include_directories(${objlib} PRIVATE $<BUILD_INTERFACE:${OPENEXR_CURLIB_CURBINDIR}>)
4848
endif()
49+
50+
if(KDU_LIBRARY)
51+
target_compile_definitions(${objlib} PRIVATE KDU_AVAILABLE=1)
52+
endif()
53+
4954
target_link_libraries(${objlib} PUBLIC ${PROJECT_NAME}::Config ${OPENEXR_CURLIB_DEPENDENCIES} ${KDU_LIBRARY} ${CMAKE_DL_LIBS} openjph)
5055
if(OPENEXR_CURLIB_PRIVATE_DEPS)
5156
target_link_libraries(${objlib} PRIVATE ${OPENEXR_CURLIB_PRIVATE_DEPS})

src/lib/OpenEXRCore/internal_ht.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ internal_exr_apply_ht (exr_encode_pipeline_t* encode)
107107
for (ojph::ui32 c = 0; c < encode->channel_count; c++)
108108
siz.set_component (c, ojph::point (1, 1), 16, true);
109109

110-
/* TODO: extend to multiple tiles and non-coincident data and display windows */
111110
siz.set_image_offset (ojph::point (0, 0));
112111
siz.set_tile_offset (ojph::point (0, 0));
113112
siz.set_image_extent (ojph::point (width, height));

src/lib/OpenEXRCore/internal_htk.cpp

Lines changed: 118 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,59 @@
1717
#include <vector>
1818
#include <string>
1919

20-
#include "kdu_elementary.h"
21-
#include "kdu_params.h"
22-
#include "kdu_stripe_compressor.h"
23-
#include "kdu_compressed.h"
24-
#include "kdu_file_io.h"
25-
#include "kdu_messaging.h"
26-
#include "kdu_sample_processing.h"
27-
#include "kdu_stripe_decompressor.h"
28-
29-
#include "openexr_compression.h"
30-
20+
#ifdef KDU_AVAILABLE
21+
22+
# include "openexr_compression.h"
23+
# include "kdu_elementary.h"
24+
# include "kdu_params.h"
25+
# include "kdu_stripe_compressor.h"
26+
# include "kdu_compressed.h"
27+
# include "kdu_file_io.h"
28+
# include "kdu_messaging.h"
29+
# include "kdu_sample_processing.h"
30+
# include "kdu_stripe_decompressor.h"
3131
using namespace kdu_supp;
3232

33-
class mem_compressed_target : public kdu_compressed_target {
34-
public:
35-
mem_compressed_target() {}
33+
class mem_compressed_target : public kdu_compressed_target
34+
{
35+
public:
36+
mem_compressed_target () {}
3637

37-
bool close() {
38-
this->buf.clear();
39-
return true;
40-
}
38+
bool close ()
39+
{
40+
this->buf.clear ();
41+
return true;
42+
}
4143

42-
bool write(const kdu_byte* buf, int num_bytes) {
43-
std::copy(buf, buf + num_bytes, std::back_inserter(this->buf));
44-
return true;
45-
}
44+
bool write (const kdu_byte* buf, int num_bytes)
45+
{
46+
std::copy (buf, buf + num_bytes, std::back_inserter (this->buf));
47+
return true;
48+
}
4649

47-
void set_target_size(kdu_long num_bytes) { this->buf.reserve(num_bytes); }
50+
void set_target_size (kdu_long num_bytes) { this->buf.reserve (num_bytes); }
4851

49-
bool prefer_large_writes() const { return false; }
52+
bool prefer_large_writes () const { return false; }
5053

51-
std::vector<uint8_t>& get_buffer() { return this->buf; }
54+
std::vector<uint8_t>& get_buffer () { return this->buf; }
5255

53-
private:
54-
std::vector<uint8_t> buf;
56+
private:
57+
std::vector<uint8_t> buf;
5558
};
5659

57-
class error_message_handler : public kdu_core::kdu_message {
58-
public:
59-
60-
void put_text(const char* msg) {
61-
std::cout << msg;
62-
}
60+
class error_message_handler : public kdu_core::kdu_message
61+
{
62+
public:
63+
void put_text (const char* msg) { std::cout << msg; }
6364

64-
virtual void flush(bool end_of_message = false) {
65-
if (end_of_message) {
66-
std::cout << std::endl;
65+
virtual void flush (bool end_of_message = false)
66+
{
67+
if (end_of_message) { std::cout << std::endl; }
6768
}
68-
}
6969
};
7070

7171
static error_message_handler error_handler;
7272

73-
74-
7573
extern "C" exr_result_t
7674
internal_exr_undo_htk (
7775
exr_decode_pipeline_t* decode,
@@ -82,69 +80,77 @@ internal_exr_undo_htk (
8280
{
8381
exr_result_t rv = EXR_ERR_SUCCESS;
8482

85-
std::vector<int> cs_to_file_ch(decode->channel_count);
86-
bool isRGB = make_channel_map (
83+
std::vector<int> cs_to_file_ch (decode->channel_count);
84+
bool isRGB = make_channel_map (
8785
decode->channel_count, decode->channels, cs_to_file_ch);
8886

89-
std::vector<int> heights(decode->channel_count);
90-
std::vector<int> sample_offsets(decode->channel_count);
87+
std::vector<int> heights (decode->channel_count);
88+
std::vector<int> sample_offsets (decode->channel_count);
9189

92-
int32_t width = decode->chunk.width;
90+
int32_t width = decode->chunk.width;
9391
int32_t height = decode->chunk.height;
9492

95-
for(int i = 0; i < sample_offsets.size(); i++) {
93+
for (int i = 0; i < sample_offsets.size (); i++)
94+
{
9695
sample_offsets[i] = cs_to_file_ch[i] * width;
9796
}
9897

99-
std::vector<int> row_gaps(decode->channel_count);
100-
std::fill(row_gaps.begin(), row_gaps.end(), width * decode->channel_count);
98+
std::vector<int> row_gaps (decode->channel_count);
99+
std::fill (
100+
row_gaps.begin (), row_gaps.end (), width * decode->channel_count);
101101

102-
kdu_core::kdu_customize_errors(&error_handler);
102+
kdu_core::kdu_customize_errors (&error_handler);
103103

104-
kdu_compressed_source_buffered infile((kdu_byte*) (compressed_data), comp_buf_size);
104+
kdu_compressed_source_buffered infile (
105+
(kdu_byte*) (compressed_data), comp_buf_size);
105106

106-
kdu_codestream cs;
107-
cs.create(&infile);
107+
kdu_codestream cs;
108+
cs.create (&infile);
108109

109110
kdu_dims dims;
110-
cs.get_dims(0, dims, false);
111+
cs.get_dims (0, dims, false);
111112

112113
assert (width == dims.size.x);
113114
assert (height == dims.size.y);
114-
assert (decode->channel_count == cs.get_num_components());
115+
assert (decode->channel_count == cs.get_num_components ());
115116
assert (sizeof (int16_t) == 2);
116117

117118
kdu_stripe_decompressor d;
118119

119-
d.start(cs);
120+
d.start (cs);
120121

121-
std::fill(heights.begin(), heights.end(), height);
122-
d.pull_stripe((kdu_int16 *) uncompressed_data, heights.data(), sample_offsets.data(), NULL, row_gaps.data());
122+
std::fill (heights.begin (), heights.end (), height);
123+
d.pull_stripe (
124+
(kdu_int16*) uncompressed_data,
125+
heights.data (),
126+
sample_offsets.data (),
127+
NULL,
128+
row_gaps.data ());
123129

124-
d.finish();
130+
d.finish ();
125131

126-
cs.destroy();
132+
cs.destroy ();
127133

128134
return rv;
129135
}
130136

131-
extern "C"
132-
exr_result_t
137+
extern "C" exr_result_t
133138
internal_exr_apply_htk (exr_encode_pipeline_t* encode)
134139
{
135140
exr_result_t rv = EXR_ERR_SUCCESS;
136141

137-
std::vector<int> cs_to_file_ch(encode->channel_count);
138-
bool isRGB = make_channel_map (
142+
std::vector<int> cs_to_file_ch (encode->channel_count);
143+
bool isRGB = make_channel_map (
139144
encode->channel_count, encode->channels, cs_to_file_ch);
140145

141146
int height = encode->chunk.height;
142147
int width = encode->chunk.width;
143-
144-
std::vector<int> heights(encode->channel_count);
145-
std::vector<int> sample_offsets(encode->channel_count);
146-
std::vector<int> row_gaps(encode->channel_count);
147-
std::fill(row_gaps.begin(), row_gaps.end(), width * encode->channel_count);
148+
149+
std::vector<int> heights (encode->channel_count);
150+
std::vector<int> sample_offsets (encode->channel_count);
151+
std::vector<int> row_gaps (encode->channel_count);
152+
std::fill (
153+
row_gaps.begin (), row_gaps.end (), width * encode->channel_count);
148154

149155
assert (
150156
encode->packed_bytes == (encode->channel_count * 2 * height * width));
@@ -157,7 +163,7 @@ internal_exr_apply_htk (exr_encode_pipeline_t* encode)
157163
siz.set (Nsigned, 0, 0, true);
158164
static_cast<kdu_params&> (siz).finalize ();
159165

160-
kdu_codestream codestream;
166+
kdu_codestream codestream;
161167
mem_compressed_target output;
162168

163169
// kdu_simple_file_target target("/tmp/out.j2c");
@@ -177,30 +183,66 @@ internal_exr_apply_htk (exr_encode_pipeline_t* encode)
177183
cod->set (Clevels, 0, 0, 5);
178184
cod->set (Cycc, 0, 0, isRGB);
179185

180-
kdu_params *nlt = codestream.access_siz ()->access_cluster(NLT_params);
186+
kdu_params* nlt = codestream.access_siz ()->access_cluster (NLT_params);
181187

182-
nlt->set(NLType, 0, 0, NLType_SMAG);
188+
nlt->set (NLType, 0, 0, NLType_SMAG);
183189

184190
codestream.access_siz ()->finalize_all ();
185191

186192
kdu_stripe_compressor compressor;
187193
compressor.start (codestream);
188194

189-
std::fill(heights.begin(), heights.end(), height);
190-
compressor.push_stripe((kdu_int16 *) encode->packed_buffer, heights.data(), sample_offsets.data(), NULL, row_gaps.data());
195+
std::fill (heights.begin (), heights.end (), height);
196+
compressor.push_stripe (
197+
(kdu_int16*) encode->packed_buffer,
198+
heights.data (),
199+
sample_offsets.data (),
200+
NULL,
201+
row_gaps.data ());
191202

192-
compressor.finish();
203+
compressor.finish ();
193204

194-
codestream.destroy();
205+
codestream.destroy ();
195206

196-
int compressed_sz = static_cast<size_t> (output.get_buffer().size());
207+
int compressed_sz = static_cast<size_t> (output.get_buffer ().size ());
197208

198209
if (compressed_sz < encode->packed_bytes)
199210
{
200-
memcpy (encode->compressed_buffer, output.get_buffer().data(), compressed_sz);
211+
memcpy (
212+
encode->compressed_buffer,
213+
output.get_buffer ().data (),
214+
compressed_sz);
201215
encode->compressed_bytes = compressed_sz;
202216
}
203217
else { encode->compressed_bytes = encode->packed_bytes; }
204218

205219
return rv;
206220
}
221+
222+
#else
223+
224+
#error
225+
226+
extern "C" exr_result_t
227+
internal_exr_undo_htk (
228+
exr_decode_pipeline_t* decode,
229+
const void* compressed_data,
230+
uint64_t comp_buf_size,
231+
void* uncompressed_data,
232+
uint64_t uncompressed_size)
233+
{
234+
return internal_exr_undo_ht (
235+
decode,
236+
compressed_data,
237+
comp_buf_size,
238+
uncompressed_data,
239+
uncompressed_size);
240+
}
241+
242+
extern "C" exr_result_t
243+
internal_exr_apply_htk (exr_encode_pipeline_t* encode)
244+
{
245+
return internal_exr_apply_ht(encode);
246+
}
247+
248+
#endif

0 commit comments

Comments
 (0)