29
29
30
30
// this is defined so that we get static linking.
31
31
#include " openjpeg.h"
32
- #include " event.h"
33
- #include " cio.h"
34
32
#include " owning_ptr.h"
35
33
#include < string>
36
34
35
+
36
+ #define WANT_VERBOSE_OPJ_SPAM LL_DEBUG
37
+
37
38
#define MAX_ENCODED_DISCARD_LEVELS 5
38
39
39
40
// Factory function: see declaration in llimagej2c.cpp
@@ -52,6 +53,7 @@ std::string LLImageJ2COJ::getEngineInfo() const
52
53
#endif
53
54
}
54
55
56
+ #if WANT_VERBOSE_OPJ_SPAM
55
57
// Return string from message, eliminating final \n if present
56
58
static std::string chomp (const char * msg)
57
59
{
@@ -67,27 +69,34 @@ static std::string chomp(const char* msg)
67
69
}
68
70
return message;
69
71
}
72
+ #endif
70
73
71
74
/* *
72
75
sample error callback expecting a LLFILE* client object
73
76
*/
74
77
void error_callback (const char * msg, void *)
75
78
{
76
- LL_DEBUGS () << " LLImageJ2COJ: " << chomp (msg) << LL_ENDL;
79
+ #if WANT_VERBOSE_OPJ_SPAM
80
+ LL_WARNS () << " LLImageJ2COJ: " << chomp (msg) << LL_ENDL;
81
+ #endif
77
82
}
78
83
/* *
79
84
sample warning callback expecting a LLFILE* client object
80
85
*/
81
86
void warning_callback (const char * msg, void *)
82
87
{
83
- LL_DEBUGS () << " LLImageJ2COJ: " << chomp (msg) << LL_ENDL;
88
+ #if WANT_VERBOSE_OPJ_SPAM
89
+ LL_WARNS () << " LLImageJ2COJ: " << chomp (msg) << LL_ENDL;
90
+ #endif
84
91
}
85
92
/* *
86
93
sample debug callback expecting no client object
87
94
*/
88
95
void info_callback (const char * msg, void *)
89
96
{
90
- LL_DEBUGS () << " LLImageJ2COJ: " << chomp (msg) << LL_ENDL;
97
+ #if WANT_VERBOSE_OPJ_SPAM
98
+ LL_INFOS () << " LLImageJ2COJ: " << chomp (msg) << LL_ENDL;
99
+ #endif
91
100
}
92
101
93
102
// Divide a by 2 to the power of b and round upwards
@@ -99,39 +108,13 @@ int ceildivpow2(int a, int b)
99
108
class JPEG2KBase
100
109
{
101
110
public:
102
- JPEG2KBase () {}
111
+ JPEG2KBase () = default ;
103
112
104
113
U8* buffer = nullptr ;
105
114
OPJ_SIZE_T size = 0 ;
106
115
OPJ_OFF_T offset = 0 ;
107
116
};
108
117
109
- #define WANT_VERBOSE_OPJ_SPAM LL_DEBUG
110
-
111
- static void opj_info (const char * msg, void * user_data)
112
- {
113
- llassert (user_data);
114
- #if WANT_VERBOSE_OPJ_SPAM
115
- LL_INFOS (" OpenJPEG" ) << msg << LL_ENDL;
116
- #endif
117
- }
118
-
119
- static void opj_warn (const char * msg, void * user_data)
120
- {
121
- llassert (user_data);
122
- #if WANT_VERBOSE_OPJ_SPAM
123
- LL_WARNS (" OpenJPEG" ) << msg << LL_ENDL;
124
- #endif
125
- }
126
-
127
- static void opj_error (const char * msg, void * user_data)
128
- {
129
- llassert (user_data);
130
- #if WANT_VERBOSE_OPJ_SPAM
131
- LL_WARNS (" OpenJPEG" ) << msg << LL_ENDL;
132
- #endif
133
- }
134
-
135
118
static OPJ_SIZE_T opj_read (void * buffer, OPJ_SIZE_T bytes, void * user_data)
136
119
{
137
120
llassert (user_data);
@@ -224,11 +207,7 @@ class JPEG2KDecode : public JPEG2KBase
224
207
225
208
JPEG2KDecode (S8 discardLevel)
226
209
{
227
- memset (&event_mgr, 0 , sizeof (opj_event_mgr_t ));
228
210
memset (¶meters, 0 , sizeof (opj_dparameters_t ));
229
- event_mgr.error_handler = error_callback;
230
- event_mgr.warning_handler = warning_callback;
231
- event_mgr.info_handler = info_callback;
232
211
opj_set_default_decoder_parameters (¶meters);
233
212
parameters.cp_reduce = discardLevel;
234
213
}
@@ -245,6 +224,11 @@ class JPEG2KDecode : public JPEG2KBase
245
224
246
225
decoder = opj_create_decompress (OPJ_CODEC_J2K);
247
226
227
+ /* catch events using our callbacks and give a local context */
228
+ opj_set_error_handler (decoder, error_callback, nullptr );
229
+ opj_set_warning_handler (decoder, warning_callback, nullptr );
230
+ opj_set_info_handler (decoder, info_callback, nullptr );
231
+
248
232
if (!opj_setup_decoder (decoder, ¶meters))
249
233
{
250
234
return false ;
@@ -311,9 +295,9 @@ class JPEG2KDecode : public JPEG2KBase
311
295
decoder = opj_create_decompress (OPJ_CODEC_J2K);
312
296
opj_setup_decoder (decoder, ¶meters);
313
297
314
- opj_set_info_handler (decoder, opj_info , this );
315
- opj_set_warning_handler (decoder, opj_warn , this );
316
- opj_set_error_handler (decoder, opj_error , this );
298
+ opj_set_info_handler (decoder, info_callback , this );
299
+ opj_set_warning_handler (decoder, warning_callback , this );
300
+ opj_set_error_handler (decoder, error_callback , this );
317
301
318
302
stream = opj_stream_create (dataSize, true );
319
303
if (!stream)
@@ -366,7 +350,6 @@ class JPEG2KDecode : public JPEG2KBase
366
350
367
351
private:
368
352
opj_dparameters_t parameters;
369
- opj_event_mgr_t event_mgr;
370
353
owning_ptr<opj_codestream_info_v2_t > codestream_info{
371
354
nullptr ,
372
355
// opj_destroy_cstr_info(opj_codestream_info_v2_t**) requires a
@@ -389,10 +372,6 @@ class JPEG2KEncode : public JPEG2KBase
389
372
JPEG2KEncode (const char * comment_text_in, bool reversible)
390
373
{
391
374
memset (¶meters, 0 , sizeof (opj_cparameters_t ));
392
- memset (&event_mgr, 0 , sizeof (opj_event_mgr_t ));
393
- event_mgr.error_handler = error_callback;
394
- event_mgr.warning_handler = warning_callback;
395
- event_mgr.info_handler = info_callback;
396
375
397
376
opj_set_default_encoder_parameters (¶meters);
398
377
parameters.cod_format = OPJ_CODEC_J2K;
@@ -435,6 +414,11 @@ class JPEG2KEncode : public JPEG2KBase
435
414
436
415
encoder = opj_create_compress (OPJ_CODEC_J2K);
437
416
417
+ /* catch events using our callbacks and give a local context */
418
+ opj_set_error_handler (encoder, error_callback, nullptr );
419
+ opj_set_warning_handler (encoder, warning_callback, nullptr );
420
+ opj_set_info_handler (encoder, info_callback, nullptr );
421
+
438
422
parameters.tcp_mct = (image->numcomps >= 3 ) ? 1 : 0 ;
439
423
parameters.cod_format = OPJ_CODEC_J2K;
440
424
parameters.prog_order = OPJ_RLCP;
@@ -489,9 +473,9 @@ class JPEG2KEncode : public JPEG2KBase
489
473
return false ;
490
474
}
491
475
492
- opj_set_info_handler (encoder, opj_info , this );
493
- opj_set_warning_handler (encoder, opj_warn , this );
494
- opj_set_error_handler (encoder, opj_error , this );
476
+ opj_set_info_handler (encoder, info_callback , this );
477
+ opj_set_warning_handler (encoder, warning_callback , this );
478
+ opj_set_error_handler (encoder, error_callback , this );
495
479
496
480
U32 tile_count = (rawImageIn.getWidth () >> 6 ) * (rawImageIn.getHeight () >> 6 );
497
481
U32 data_size_guess = tile_count * TILE_SIZE;
@@ -681,7 +665,6 @@ class JPEG2KEncode : public JPEG2KBase
681
665
private:
682
666
std::string comment_text;
683
667
opj_cparameters_t parameters;
684
- opj_event_mgr_t event_mgr;
685
668
owning_ptr<opj_stream_t > stream{ nullptr , opj_stream_destroy };
686
669
owning_ptr<opj_image_t > image{ nullptr , opj_image_destroy };
687
670
owning_ptr<opj_codec_t > encoder{ nullptr , opj_destroy_codec };
0 commit comments