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
|
/**
* @file llimagej2coj.cpp
* @brief This is an implementation of JPEG2000 encode/decode using OpenJPEG.
*
* $LicenseInfo:firstyear=2006&license=viewergpl$
*
* Copyright (c) 2006-2009, Linden Research, Inc.
*
* Second Life Viewer Source Code
* The source code in this file ("Source Code") is provided by Linden Lab
* to you under the terms of the GNU General Public License, version 2.0
* ("GPL"), unless you have obtained a separate licensing agreement
* ("Other License"), formally executed by you and Linden Lab. Terms of
* the GPL can be found in doc/GPL-license.txt in this distribution, or
* online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at
* http://secondlifegrid.net/programs/open_source/licensing/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llimagej2coj.h"
// this is defined so that we get static linking.
#include "openjpeg.h"
#include "lltimer.h"
#include "llmemory.h"
const char* fallbackEngineInfoLLImageJ2CImpl()
{
static std::string version_string =
std::string("OpenJPEG: " OPENJPEG_VERSION ", Runtime: ")
+ opj_version();
return version_string.c_str();
}
LLImageJ2CImpl* fallbackCreateLLImageJ2CImpl()
{
return new LLImageJ2COJ();
}
void fallbackDestroyLLImageJ2CImpl(LLImageJ2CImpl* impl)
{
delete impl;
impl = NULL;
}
// Return string from message, eliminating final \n if present
static std::string chomp(const char* msg)
{
// stomp trailing \n
std::string message = msg;
if (!message.empty())
{
size_t last = message.size() - 1;
if (message[last] == '\n')
{
message.resize( last );
}
}
return message;
}
/**
sample error callback expecting a LLFILE* client object
*/
void error_callback(const char* msg, void*)
{
lldebugs << "LLImageJ2COJ: " << chomp(msg) << llendl;
}
/**
sample warning callback expecting a LLFILE* client object
*/
void warning_callback(const char* msg, void*)
{
lldebugs << "LLImageJ2COJ: " << chomp(msg) << llendl;
}
/**
sample debug callback expecting no client object
*/
void info_callback(const char* msg, void*)
{
lldebugs << "LLImageJ2COJ: " << chomp(msg) << llendl;
}
LLImageJ2COJ::LLImageJ2COJ() : LLImageJ2CImpl()
{
mRawImagep=NULL;
}
LLImageJ2COJ::~LLImageJ2COJ()
{
}
BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decode_time, S32 first_channel, S32 max_channel_count)
{
//
// FIXME: Get the comment field out of the texture
//
LLTimer decode_timer;
opj_dparameters_t parameters; /* decompression parameters */
opj_event_mgr_t event_mgr; /* event manager */
opj_image_t *image = NULL;
opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */
opj_cio_t *cio = NULL;
/* configure the event callbacks (not required) */
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
event_mgr.error_handler = error_callback;
event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = info_callback;
/* set decoding parameters to default values */
opj_set_default_decoder_parameters(¶meters);
parameters.cp_reduce = base.getRawDiscardLevel();
/* decode the code-stream */
/* ---------------------- */
/* JPEG-2000 codestream */
/* get a decoder handle */
dinfo = opj_create_decompress(CODEC_J2K);
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
/* setup the decoder decoding parameters using user parameters */
opj_setup_decoder(dinfo, ¶meters);
/* open a byte stream */
cio = opj_cio_open((opj_common_ptr)dinfo, base.getData(), base.getDataSize());
/* decode the stream and fill the image structure, also fill in an additional
structure to get the decoding result. This structure is a bit unusual in that
it is not received through opj, but still has some dynamically allocated fields
that need to be cleared up at the end by calling a destroy function. */
opj_codestream_info_t cinfo;
memset(&cinfo, 0, sizeof(opj_codestream_info_t));
image = opj_decode_with_info(dinfo, cio, &cinfo);
/* close the byte stream */
opj_cio_close(cio);
/* free remaining structures */
if(dinfo)
{
opj_destroy_decompress(dinfo);
}
// The image decode failed if the return was NULL or the component
// count was zero. The latter is just a sanity check before we
// dereference the array.
if(!image)
{
LL_DEBUGS("Openjpeg") << "ERROR -> decodeImpl: failed to decode image - no image" << LL_ENDL;
return TRUE; // done
}
S32 img_components = image->numcomps;
if( !img_components ) // < 1 ||img_components > 4 )
{
LL_DEBUGS("Openjpeg") << "ERROR -> decodeImpl: failed to decode image wrong number of components: " << img_components << LL_ENDL;
if (image)
{
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
}
return TRUE; // done
}
// sometimes we get bad data out of the cache - check to see if the decode succeeded
int decompdifference = 0;
if (cinfo.numdecompos) // sanity
{
for (int comp = 0; comp < image->numcomps; comp++)
{ /* get maximum decomposition level difference, first field is from the COD header and the second
is what is actually met in the codestream, NB: if everything was ok, this calculation will
return what was set in the cp_reduce value! */
decompdifference = llmax(decompdifference, cinfo.numdecompos[comp] - image->comps[comp].resno_decoded);
}
if (decompdifference < 0) // sanity
{
decompdifference = 0;
}
}
/* if OpenJPEG failed to decode all requested decomposition levels
the difference will be greater than this level */
if (decompdifference > base.getRawDiscardLevel())
{
llwarns << "not enough data for requested discard level, setting mDecoding to FALSE, difference: " << (decompdifference - base.getRawDiscardLevel()) << llendl;
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
base.mDecoding = FALSE;
return TRUE;
}
if(img_components <= first_channel)
{
// sanity
LL_DEBUGS("Openjpeg") << "trying to decode more channels than are present in image: numcomps: " << img_components << " first_channel: " << first_channel << LL_ENDL;
if (image)
{
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
}
return TRUE;
}
// Copy image data into our raw image format (instead of the separate channel format
S32 channels = img_components - first_channel;
if( channels > max_channel_count )
channels = max_channel_count;
// Component buffers are allocated in an image width by height buffer.
// The image placed in that buffer is ceil(width/2^factor) by
// ceil(height/2^factor) and if the factor isn't zero it will be at the
// top left of the buffer with black filled in the rest of the pixels.
// It is integer math so the formula is written in ceildivpo2.
// (Assuming all the components have the same width, height and
// factor.)
S32 comp_width = image->comps[0].w;
S32 f=image->comps[0].factor;
S32 width = ceildivpow2(image->x1 - image->x0, f);
S32 height = ceildivpow2(image->y1 - image->y0, f);
raw_image.resize(width, height, channels);
U8 *rawp = raw_image.getData();
// first_channel is what channel to start copying from
// dest is what channel to copy to. first_channel comes from the
// argument, dest always starts writing at channel zero.
for (S32 comp = first_channel, dest=0; comp < first_channel + channels;
comp++, dest++)
{
if (image->comps[comp].data)
{
S32 offset = dest;
for (S32 y = (height - 1); y >= 0; y--)
{
for (S32 x = 0; x < width; x++)
{
rawp[offset] = image->comps[comp].data[y*comp_width + x];
offset += channels;
}
}
}
else // Some rare OpenJPEG versions have this bug.
{
llwarns << "ERROR -> decodeImpl: failed to decode image! (NULL comp data - OpenJPEG bug)" << llendl;
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
return TRUE; // done
}
}
/* free opj data structures */
if (image)
{
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
}
return TRUE; // done
}
BOOL LLImageJ2COJ::encodeImpl(LLImageJ2C &base, const LLImageRaw &raw_image, const char* comment_text, F32 encode_time, BOOL reversible)
{
const S32 MAX_COMPS = 5;
opj_cparameters_t parameters; /* compression parameters */
opj_event_mgr_t event_mgr; /* event manager */
/*
configure the event callbacks (not required)
setting of each callback is optional
*/
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
event_mgr.error_handler = error_callback;
event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = info_callback;
/* set encoding parameters to default values */
opj_set_default_encoder_parameters(¶meters);
parameters.cod_format = 0;
parameters.cp_disto_alloc = 1;
if (reversible)
{
parameters.tcp_numlayers = 1;
parameters.tcp_rates[0] = 0.0f;
}
else
{
parameters.tcp_numlayers = 5;
parameters.tcp_rates[0] = 1920.0f;
parameters.tcp_rates[1] = 480.0f;
parameters.tcp_rates[2] = 120.0f;
parameters.tcp_rates[3] = 30.0f;
parameters.tcp_rates[4] = 10.0f;
parameters.irreversible = 1;
if (raw_image.getComponents() >= 3)
{
parameters.tcp_mct = 1;
}
}
if (!comment_text)
{
parameters.cp_comment = (char *) "";
}
else
{
// Awful hacky cast, too lazy to copy right now.
parameters.cp_comment = (char *) comment_text;
}
//
// Fill in the source image from our raw image
//
OPJ_COLOR_SPACE color_space = CLRSPC_SRGB;
opj_image_cmptparm_t cmptparm[MAX_COMPS];
opj_image_t * image = NULL;
S32 numcomps = raw_image.getComponents();
S32 width = raw_image.getWidth();
S32 height = raw_image.getHeight();
memset(&cmptparm[0], 0, MAX_COMPS * sizeof(opj_image_cmptparm_t));
for(S32 c = 0; c < numcomps; c++) {
cmptparm[c].prec = 8;
cmptparm[c].bpp = 8;
cmptparm[c].sgnd = 0;
cmptparm[c].dx = parameters.subsampling_dx;
cmptparm[c].dy = parameters.subsampling_dy;
cmptparm[c].w = width;
cmptparm[c].h = height;
}
/* create the image */
image = opj_image_create(numcomps, &cmptparm[0], color_space);
image->x1 = width;
image->y1 = height;
S32 i = 0;
const U8 *src_datap = raw_image.getData();
for (S32 y = height - 1; y >= 0; y--)
{
for (S32 x = 0; x < width; x++)
{
const U8 *pixel = src_datap + (y*width + x) * numcomps;
for (S32 c = 0; c < numcomps; c++)
{
image->comps[c].data[i] = *pixel;
pixel++;
}
i++;
}
}
/* encode the destination image */
/* ---------------------------- */
int codestream_length;
opj_cio_t *cio = NULL;
/* get a J2K compressor handle */
opj_cinfo_t* cinfo = opj_create_compress(CODEC_J2K);
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
/* setup the encoder parameters using the current image and using user parameters */
opj_setup_encoder(cinfo, ¶meters, image);
/* open a byte stream for writing */
/* allocate memory for all tiles */
cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
/* encode the image */
bool bSuccess = opj_encode(cinfo, cio, image, NULL);
if (!bSuccess)
{
opj_cio_close(cio);
llinfos << "Failed to encode image." << llendl;
return FALSE;
}
codestream_length = cio_tell(cio);
base.copyData(cio->buffer, codestream_length);
base.updateData(); // set width, height
/* close and free the byte stream */
opj_cio_close(cio);
/* free remaining compression structures */
opj_destroy_compress(cinfo);
/* free user parameters structure */
if(parameters.cp_matrice) free(parameters.cp_matrice);
/* free image data */
opj_image_destroy(image);
return TRUE;
}
BOOL LLImageJ2COJ::getMetadata(LLImageJ2C &base)
{
//
// FIXME: We get metadata by decoding the ENTIRE image.
//
// Update the raw discard level
base.updateRawDiscardLevel();
opj_dparameters_t parameters; /* decompression parameters */
opj_event_mgr_t event_mgr; /* event manager */
opj_image_t *image = NULL;
opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */
opj_cio_t *cio = NULL;
/* configure the event callbacks (not required) */
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
event_mgr.error_handler = error_callback;
event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = info_callback;
/* set decoding parameters to default values */
opj_set_default_decoder_parameters(¶meters);
// Only decode what's required to get the size data.
parameters.cp_limit_decoding=LIMIT_TO_MAIN_HEADER;
//parameters.cp_reduce = mRawDiscardLevel;
/* decode the code-stream */
/* ---------------------- */
/* JPEG-2000 codestream */
/* get a decoder handle */
dinfo = opj_create_decompress(CODEC_J2K);
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
/* setup the decoder decoding parameters using user parameters */
opj_setup_decoder(dinfo, ¶meters);
/* open a byte stream */
cio = opj_cio_open((opj_common_ptr)dinfo, base.getData(), base.getDataSize());
/* decode the stream and fill the image structure */
image = opj_decode(dinfo, cio);
/* close the byte stream */
opj_cio_close(cio);
/* free remaining structures */
if(dinfo)
{
opj_destroy_decompress(dinfo);
}
if(!image)
{
llwarns << "ERROR -> getMetadata: failed to decode image!" << llendl;
return FALSE;
}
// Copy image data into our raw image format (instead of the separate channel format
S32 width = 0;
S32 height = 0;
S32 img_components = image->numcomps;
width = image->x1 - image->x0;
height = image->y1 - image->y0;
base.setSize(width, height, img_components);
/* free image data structure */
opj_image_destroy(image);
return TRUE;
}
|