Coverage Report

Created: 2023-12-08 06:53

/src/freeimage-svn/FreeImage/trunk/Source/LibOpenJPEG/openjpeg.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
3
 * Copyright (c) 2008;2011-2012, Centre National d'Etudes Spatiales (CNES), France 
4
 * Copyright (c) 2012, CS Systemes d'Information, France
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
 * POSSIBILITY OF SUCH DAMAGE.
27
 */
28
29
#ifdef _WIN32
30
#include <windows.h>
31
#endif /* _WIN32 */
32
33
#include "opj_includes.h"
34
35
36
/* ---------------------------------------------------------------------- */
37
/* Functions to set the message handlers */
38
39
OPJ_BOOL OPJ_CALLCONV opj_set_info_handler( opj_codec_t * p_codec, 
40
                      opj_msg_callback p_callback,
41
                      void * p_user_data)
42
0
{
43
0
  opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
44
0
  if(! l_codec){
45
0
    return OPJ_FALSE;
46
0
  }
47
  
48
0
  l_codec->m_event_mgr.info_handler = p_callback;
49
0
  l_codec->m_event_mgr.m_info_data = p_user_data;
50
  
51
0
  return OPJ_TRUE;
52
0
}
53
54
OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(  opj_codec_t * p_codec, 
55
                        opj_msg_callback p_callback,
56
                        void * p_user_data)
57
0
{
58
0
  opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
59
0
  if (! l_codec) {
60
0
    return OPJ_FALSE;
61
0
  }
62
  
63
0
  l_codec->m_event_mgr.warning_handler = p_callback;
64
0
  l_codec->m_event_mgr.m_warning_data = p_user_data;
65
  
66
0
  return OPJ_TRUE;
67
0
}
68
69
OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, 
70
                      opj_msg_callback p_callback,
71
                      void * p_user_data)
72
0
{
73
0
  opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
74
0
  if (! l_codec) {
75
0
    return OPJ_FALSE;
76
0
  }
77
  
78
0
  l_codec->m_event_mgr.error_handler = p_callback;
79
0
  l_codec->m_event_mgr.m_error_data = p_user_data;
80
  
81
0
  return OPJ_TRUE;
82
0
}
83
84
/* ---------------------------------------------------------------------- */
85
86
static OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
87
0
{
88
0
  OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
89
0
  return l_nb_read ? l_nb_read : (OPJ_SIZE_T)-1;
90
0
}
91
92
static OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
93
0
{
94
0
  OPJ_OFF_T file_length = 0;
95
96
0
  OPJ_FSEEK(p_file, 0, SEEK_END);
97
0
  file_length = (OPJ_OFF_T)OPJ_FTELL(p_file);
98
0
  OPJ_FSEEK(p_file, 0, SEEK_SET);
99
100
0
  return (OPJ_UINT64)file_length;
101
0
}
102
103
static OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
104
0
{
105
0
  return fwrite(p_buffer,1,p_nb_bytes,p_file);
106
0
}
107
108
static OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
109
0
{
110
0
  if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
111
0
    return -1;
112
0
  }
113
114
0
  return p_nb_bytes;
115
0
}
116
117
static OPJ_BOOL opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
118
0
{
119
0
  if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
120
0
    return OPJ_FALSE;
121
0
  }
122
123
0
  return OPJ_TRUE;
124
0
}
125
126
/* ---------------------------------------------------------------------- */
127
#ifdef _WIN32
128
#ifndef OPJ_STATIC
129
BOOL APIENTRY
130
DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
131
132
  OPJ_ARG_NOT_USED(lpReserved);
133
  OPJ_ARG_NOT_USED(hModule);
134
135
  switch (ul_reason_for_call) {
136
    case DLL_PROCESS_ATTACH :
137
      break;
138
    case DLL_PROCESS_DETACH :
139
      break;
140
    case DLL_THREAD_ATTACH :
141
    case DLL_THREAD_DETACH :
142
      break;
143
    }
144
145
    return TRUE;
146
}
147
#endif /* OPJ_STATIC */
148
#endif /* _WIN32 */
149
150
/* ---------------------------------------------------------------------- */
151
152
0
const char* OPJ_CALLCONV opj_version(void) {
153
0
    return OPJ_PACKAGE_VERSION;
154
0
}
155
156
/* ---------------------------------------------------------------------- */
157
/* DECOMPRESSION FUNCTIONS*/
158
159
opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
160
0
{
161
0
  opj_codec_private_t *l_codec = 00;
162
163
0
  l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
164
0
  if (!l_codec){
165
0
    return 00;
166
0
  }
167
0
  memset(l_codec, 0, sizeof(opj_codec_private_t));
168
169
0
  l_codec->is_decompressor = 1;
170
171
0
  switch (p_format) {
172
0
    case OPJ_CODEC_J2K:
173
0
      l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
174
175
0
      l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
176
177
0
      l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
178
179
0
      l_codec->m_codec_data.m_decompression.opj_decode =
180
0
          (OPJ_BOOL (*) ( void *,
181
0
                  struct opj_stream_private *,
182
0
                  opj_image_t*, struct opj_event_mgr * )) opj_j2k_decode;
183
184
0
      l_codec->m_codec_data.m_decompression.opj_end_decompress =
185
0
          (OPJ_BOOL (*) ( void *,
186
0
                  struct opj_stream_private *,
187
0
                  struct opj_event_mgr *)) opj_j2k_end_decompress;
188
189
0
      l_codec->m_codec_data.m_decompression.opj_read_header =
190
0
          (OPJ_BOOL (*) ( struct opj_stream_private *,
191
0
                  void *,
192
0
                  opj_image_t **,
193
0
                  struct opj_event_mgr * )) opj_j2k_read_header;
194
195
0
      l_codec->m_codec_data.m_decompression.opj_destroy =
196
0
          (void (*) (void *))opj_j2k_destroy;
197
198
0
      l_codec->m_codec_data.m_decompression.opj_setup_decoder =
199
0
          (void (*) (void * , opj_dparameters_t * )) opj_j2k_setup_decoder;
200
201
0
      l_codec->m_codec_data.m_decompression.opj_read_tile_header =
202
0
          (OPJ_BOOL (*) ( void *,
203
0
                  OPJ_UINT32*,
204
0
                  OPJ_UINT32*,
205
0
                  OPJ_INT32*, OPJ_INT32*,
206
0
                  OPJ_INT32*, OPJ_INT32*,
207
0
                  OPJ_UINT32*,
208
0
                  OPJ_BOOL*,
209
0
                  struct opj_stream_private *,
210
0
                  struct opj_event_mgr * )) opj_j2k_read_tile_header;
211
212
0
      l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
213
0
          (OPJ_BOOL (*) ( void *, 
214
0
                                    OPJ_UINT32, 
215
0
                                    OPJ_BYTE*, 
216
0
                                    OPJ_UINT32, 
217
0
                                    struct opj_stream_private *,
218
0
                                    struct opj_event_mgr *)) opj_j2k_decode_tile;
219
220
0
      l_codec->m_codec_data.m_decompression.opj_set_decode_area =
221
0
          (OPJ_BOOL (*) ( void *, 
222
0
                                    opj_image_t*, 
223
0
                                    OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, 
224
0
                                    struct opj_event_mgr *)) opj_j2k_set_decode_area;
225
226
0
      l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = 
227
0
                    (OPJ_BOOL (*) ( void *p_codec,
228
0
                    opj_stream_private_t *p_cio,
229
0
                    opj_image_t *p_image,
230
0
                    struct opj_event_mgr * p_manager,
231
0
                    OPJ_UINT32 tile_index)) opj_j2k_get_tile;
232
233
0
      l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = 
234
0
                    (OPJ_BOOL (*) ( void * p_codec,
235
0
                  OPJ_UINT32 res_factor,
236
0
                  struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor;
237
238
0
      l_codec->m_codec = opj_j2k_create_decompress();
239
240
0
      if (! l_codec->m_codec) {
241
0
        opj_free(l_codec);
242
0
        return NULL;
243
0
      }
244
245
0
      break;
246
247
0
    case OPJ_CODEC_JP2:
248
      /* get a JP2 decoder handle */
249
0
      l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
250
251
0
      l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
252
253
0
      l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
254
255
0
      l_codec->m_codec_data.m_decompression.opj_decode =
256
0
          (OPJ_BOOL (*) ( void *,
257
0
                  struct opj_stream_private *,
258
0
                  opj_image_t*,
259
0
                  struct opj_event_mgr * )) opj_jp2_decode;
260
261
0
      l_codec->m_codec_data.m_decompression.opj_end_decompress =  
262
0
                    (OPJ_BOOL (*) ( void *,
263
0
                                    struct opj_stream_private *,
264
0
                                    struct opj_event_mgr *)) opj_jp2_end_decompress;
265
266
0
      l_codec->m_codec_data.m_decompression.opj_read_header =  
267
0
                    (OPJ_BOOL (*) ( struct opj_stream_private *,
268
0
                          void *,
269
0
                          opj_image_t **,
270
0
                          struct opj_event_mgr * )) opj_jp2_read_header;
271
272
0
      l_codec->m_codec_data.m_decompression.opj_read_tile_header = 
273
0
                    (OPJ_BOOL (*) ( void *,
274
0
                          OPJ_UINT32*,
275
0
                          OPJ_UINT32*,
276
0
                          OPJ_INT32*,
277
0
                          OPJ_INT32*,
278
0
                          OPJ_INT32 * ,
279
0
                          OPJ_INT32 * ,
280
0
                          OPJ_UINT32 * ,
281
0
                          OPJ_BOOL *,
282
0
                          struct opj_stream_private *,
283
0
                          struct opj_event_mgr * )) opj_jp2_read_tile_header;
284
285
0
      l_codec->m_codec_data.m_decompression.opj_decode_tile_data = 
286
0
                    (OPJ_BOOL (*) ( void *,
287
0
                                    OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,
288
0
                                    struct opj_stream_private *,
289
0
                                    struct opj_event_mgr * )) opj_jp2_decode_tile;
290
291
0
      l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))opj_jp2_destroy;
292
293
0
      l_codec->m_codec_data.m_decompression.opj_setup_decoder = 
294
0
                    (void (*) (void * ,opj_dparameters_t * )) opj_jp2_setup_decoder;
295
296
0
      l_codec->m_codec_data.m_decompression.opj_set_decode_area = 
297
0
                    (OPJ_BOOL (*) ( void *,
298
0
                                    opj_image_t*, 
299
0
                                    OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32,
300
0
                                    struct opj_event_mgr * )) opj_jp2_set_decode_area;
301
302
0
      l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = 
303
0
                    (OPJ_BOOL (*) ( void *p_codec,
304
0
                  opj_stream_private_t *p_cio,
305
0
                  opj_image_t *p_image,
306
0
                  struct opj_event_mgr * p_manager,
307
0
                  OPJ_UINT32 tile_index)) opj_jp2_get_tile;
308
309
0
      l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = 
310
0
                    (OPJ_BOOL (*) ( void * p_codec,
311
0
                    OPJ_UINT32 res_factor,
312
0
                    opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor;
313
314
0
      l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
315
316
0
      if (! l_codec->m_codec) {
317
0
        opj_free(l_codec);
318
0
        return 00;
319
0
      }
320
321
0
      break;
322
0
    case OPJ_CODEC_UNKNOWN:
323
0
    case OPJ_CODEC_JPT:
324
0
    default:
325
0
      opj_free(l_codec);
326
0
      return 00;
327
0
  }
328
329
0
  opj_set_default_event_handler(&(l_codec->m_event_mgr));
330
0
  return (opj_codec_t*) l_codec;
331
0
}
332
333
0
void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
334
0
  if(parameters) {
335
0
    memset(parameters, 0, sizeof(opj_dparameters_t));
336
    /* default decoding parameters */
337
0
    parameters->cp_layer = 0;
338
0
    parameters->cp_reduce = 0;
339
340
0
    parameters->decod_format = -1;
341
0
    parameters->cod_format = -1;
342
0
    parameters->flags = 0;    
343
/* UniPG>> */
344
#ifdef USE_JPWL
345
    parameters->jpwl_correct = OPJ_FALSE;
346
    parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
347
    parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
348
#endif /* USE_JPWL */
349
/* <<UniPG */
350
0
  }
351
0
}
352
353
OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
354
                                        opj_dparameters_t *parameters 
355
                    )
356
0
{
357
0
  if (p_codec && parameters) { 
358
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
359
360
0
    if (! l_codec->is_decompressor) {
361
0
      opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, 
362
0
                "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
363
0
      return OPJ_FALSE;
364
0
    }
365
366
0
    l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
367
0
                                parameters);
368
0
    return OPJ_TRUE;
369
0
  }
370
0
  return OPJ_FALSE;
371
0
}
372
373
OPJ_BOOL OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
374
                    opj_codec_t *p_codec,
375
                    opj_image_t **p_image )
376
0
{
377
0
  if (p_codec && p_stream) {
378
0
    opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
379
0
    opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
380
381
0
    if(! l_codec->is_decompressor) {
382
0
      opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, 
383
0
                "Codec provided to the opj_read_header function is not a decompressor handler.\n");
384
0
      return OPJ_FALSE;
385
0
    }
386
387
0
    return l_codec->m_codec_data.m_decompression.opj_read_header( l_stream,
388
0
                                    l_codec->m_codec,
389
0
                                    p_image,
390
0
                                    &(l_codec->m_event_mgr) );
391
0
  }
392
393
0
  return OPJ_FALSE;
394
0
}
395
396
OPJ_BOOL OPJ_CALLCONV opj_decode(   opj_codec_t *p_codec,
397
                                    opj_stream_t *p_stream,
398
                                    opj_image_t* p_image)
399
0
{
400
0
  if (p_codec && p_stream) {
401
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
402
0
    opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
403
404
0
    if (! l_codec->is_decompressor) {
405
0
      return OPJ_FALSE;
406
0
    }
407
408
0
    return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
409
0
                                l_stream,
410
0
                                p_image,
411
0
                                &(l_codec->m_event_mgr) );
412
0
  }
413
414
0
  return OPJ_FALSE;
415
0
}
416
417
OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(  opj_codec_t *p_codec,
418
                      opj_image_t* p_image,
419
                      OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
420
                      OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
421
                      )
422
0
{
423
0
  if (p_codec) {
424
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
425
    
426
0
    if (! l_codec->is_decompressor) {
427
0
      return OPJ_FALSE;
428
0
    }
429
430
0
    return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(  l_codec->m_codec,
431
0
                                      p_image,
432
0
                                      p_start_x, p_start_y,
433
0
                                      p_end_x, p_end_y,
434
0
                                      &(l_codec->m_event_mgr) );
435
0
  }
436
0
  return OPJ_FALSE;
437
0
}
438
439
OPJ_BOOL OPJ_CALLCONV opj_read_tile_header( opj_codec_t *p_codec,
440
                      opj_stream_t * p_stream,
441
                      OPJ_UINT32 * p_tile_index,
442
                      OPJ_UINT32 * p_data_size,
443
                      OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
444
                      OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
445
                      OPJ_UINT32 * p_nb_comps,
446
                      OPJ_BOOL * p_should_go_on)
447
0
{
448
0
  if (p_codec && p_stream && p_data_size && p_tile_index) {
449
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
450
0
    opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
451
452
0
    if (! l_codec->is_decompressor) {
453
0
      return OPJ_FALSE;
454
0
    }
455
456
0
    return l_codec->m_codec_data.m_decompression.opj_read_tile_header(  l_codec->m_codec,
457
0
                                      p_tile_index,
458
0
                                      p_data_size,
459
0
                                      p_tile_x0, p_tile_y0,
460
0
                                      p_tile_x1, p_tile_y1,
461
0
                                      p_nb_comps,
462
0
                                      p_should_go_on,
463
0
                                      l_stream,
464
0
                                      &(l_codec->m_event_mgr));
465
0
  }
466
0
  return OPJ_FALSE;
467
0
}
468
469
OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data( opj_codec_t *p_codec,
470
                      OPJ_UINT32 p_tile_index,
471
                      OPJ_BYTE * p_data,
472
                      OPJ_UINT32 p_data_size,
473
                      opj_stream_t *p_stream
474
                      )
475
0
{
476
0
  if (p_codec && p_data && p_stream) {
477
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
478
0
    opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
479
480
0
    if (! l_codec->is_decompressor) {
481
0
      return OPJ_FALSE;
482
0
    }
483
484
0
    return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(  l_codec->m_codec,
485
0
                                      p_tile_index,
486
0
                                      p_data,
487
0
                                      p_data_size,
488
0
                                      l_stream,
489
0
                                      &(l_codec->m_event_mgr) );
490
0
  }
491
0
  return OPJ_FALSE;
492
0
}
493
494
OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile( opj_codec_t *p_codec,
495
                      opj_stream_t *p_stream,
496
                      opj_image_t *p_image,
497
                      OPJ_UINT32 tile_index)
498
0
{
499
0
  if (p_codec && p_stream) {
500
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
501
0
    opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
502
503
0
    if (! l_codec->is_decompressor) {
504
0
      return OPJ_FALSE;
505
0
    }
506
    
507
0
    return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(  l_codec->m_codec,
508
0
                                      l_stream,
509
0
                                      p_image,
510
0
                                      &(l_codec->m_event_mgr),
511
0
                                      tile_index);
512
0
  }
513
514
0
  return OPJ_FALSE;
515
0
}
516
517
OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, 
518
                            OPJ_UINT32 res_factor )
519
0
{
520
0
  opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
521
522
0
  if ( !l_codec ){
523
0
    fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
524
0
    return OPJ_FALSE;
525
0
  }
526
527
0
  l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, 
528
0
                                      res_factor,
529
0
                                      &(l_codec->m_event_mgr) );
530
0
  return OPJ_TRUE;
531
0
}
532
533
/* ---------------------------------------------------------------------- */
534
/* COMPRESSION FUNCTIONS*/
535
536
opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
537
0
{
538
0
  opj_codec_private_t *l_codec = 00;
539
540
0
  l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
541
0
  if (!l_codec) {
542
0
    return 00;
543
0
  }
544
0
  memset(l_codec, 0, sizeof(opj_codec_private_t));
545
  
546
0
  l_codec->is_decompressor = 0;
547
548
0
  switch(p_format) {
549
0
    case OPJ_CODEC_J2K:
550
0
      l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
551
0
                                      struct opj_stream_private *,
552
0
                                      struct opj_event_mgr * )) opj_j2k_encode;
553
554
0
      l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (  void *,
555
0
                                          struct opj_stream_private *,
556
0
                                          struct opj_event_mgr *)) opj_j2k_end_compress;
557
558
0
      l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
559
0
                                          struct opj_stream_private *,
560
0
                                          struct opj_image * ,
561
0
                                          struct opj_event_mgr *)) opj_j2k_start_compress;
562
563
0
      l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
564
0
                                        OPJ_UINT32,
565
0
                                        OPJ_BYTE*,
566
0
                                        OPJ_UINT32,
567
0
                                        struct opj_stream_private *,
568
0
                                        struct opj_event_mgr *) ) opj_j2k_write_tile;
569
570
0
      l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_j2k_destroy;
571
572
0
      l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
573
0
                                        opj_cparameters_t *,
574
0
                                        struct opj_image *,
575
0
                                        struct opj_event_mgr * )) opj_j2k_setup_encoder;
576
577
0
      l_codec->m_codec = opj_j2k_create_compress();
578
0
      if (! l_codec->m_codec) {
579
0
        opj_free(l_codec);
580
0
        return 00;
581
0
      }
582
583
0
      break;
584
585
0
    case OPJ_CODEC_JP2:
586
      /* get a JP2 decoder handle */
587
0
      l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
588
0
                                      struct opj_stream_private *,
589
0
                                      struct opj_event_mgr * )) opj_jp2_encode;
590
591
0
      l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (  void *,
592
0
                                          struct opj_stream_private *,
593
0
                                          struct opj_event_mgr *)) opj_jp2_end_compress;
594
595
0
      l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
596
0
                                          struct opj_stream_private *,
597
0
                                          struct opj_image * ,
598
0
                                          struct opj_event_mgr *))  opj_jp2_start_compress;
599
600
0
      l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
601
0
                                        OPJ_UINT32,
602
0
                                        OPJ_BYTE*,
603
0
                                        OPJ_UINT32,
604
0
                                        struct opj_stream_private *,
605
0
                                        struct opj_event_mgr *)) opj_jp2_write_tile;
606
607
0
      l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_jp2_destroy;
608
609
0
      l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) ( void *,
610
0
                                        opj_cparameters_t *,
611
0
                                        struct opj_image *,
612
0
                                        struct opj_event_mgr * )) opj_jp2_setup_encoder;
613
614
0
      l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
615
0
      if (! l_codec->m_codec) {
616
0
        opj_free(l_codec);
617
0
        return 00;
618
0
      }
619
620
0
      break;
621
622
0
    case OPJ_CODEC_UNKNOWN:
623
0
    case OPJ_CODEC_JPT:
624
0
    default:
625
0
      opj_free(l_codec);
626
0
      return 00;
627
0
  }
628
629
0
  opj_set_default_event_handler(&(l_codec->m_event_mgr));
630
0
  return (opj_codec_t*) l_codec;
631
0
}
632
633
0
void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
634
0
  if(parameters) {
635
0
    memset(parameters, 0, sizeof(opj_cparameters_t));
636
    /* default coding parameters */
637
0
    parameters->cp_cinema = OPJ_OFF; 
638
0
    parameters->max_comp_size = 0;
639
0
    parameters->numresolution = 6;
640
0
    parameters->cp_rsiz = OPJ_STD_RSIZ;
641
0
    parameters->cblockw_init = 64;
642
0
    parameters->cblockh_init = 64;
643
0
    parameters->prog_order = OPJ_LRCP;
644
0
    parameters->roi_compno = -1;    /* no ROI */
645
0
    parameters->subsampling_dx = 1;
646
0
    parameters->subsampling_dy = 1;
647
0
    parameters->tp_on = 0;
648
0
    parameters->decod_format = -1;
649
0
    parameters->cod_format = -1;
650
0
    parameters->tcp_rates[0] = 0;   
651
0
    parameters->tcp_numlayers = 0;
652
0
    parameters->cp_disto_alloc = 0;
653
0
    parameters->cp_fixed_alloc = 0;
654
0
    parameters->cp_fixed_quality = 0;
655
0
    parameters->jpip_on = OPJ_FALSE;
656
/* UniPG>> */
657
#ifdef USE_JPWL
658
    parameters->jpwl_epc_on = OPJ_FALSE;
659
    parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
660
    {
661
      int i;
662
      for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
663
        parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
664
        parameters->jpwl_hprot_TPH[i] = 0; /* absent */
665
      }
666
    };
667
    {
668
      int i;
669
      for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
670
        parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
671
        parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
672
        parameters->jpwl_pprot[i] = 0; /* absent */
673
      }
674
    };
675
    parameters->jpwl_sens_size = 0; /* 0 means no ESD */
676
    parameters->jpwl_sens_addr = 0; /* 0 means auto */
677
    parameters->jpwl_sens_range = 0; /* 0 means packet */
678
    parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
679
    {
680
      int i;
681
      for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
682
        parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
683
        parameters->jpwl_sens_TPH[i] = -1; /* absent */
684
      }
685
    };
686
#endif /* USE_JPWL */
687
/* <<UniPG */
688
0
  }
689
0
}
690
691
OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, 
692
                    opj_cparameters_t *parameters, 
693
                    opj_image_t *p_image)
694
0
{
695
0
  if (p_codec && parameters && p_image) {
696
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
697
698
0
    if (! l_codec->is_decompressor) {
699
0
      l_codec->m_codec_data.m_compression.opj_setup_encoder(  l_codec->m_codec,
700
0
                                  parameters,
701
0
                                  p_image,
702
0
                                  &(l_codec->m_event_mgr) );
703
0
      return OPJ_TRUE;
704
0
    }
705
0
  }
706
707
0
  return OPJ_FALSE;
708
0
}
709
710
OPJ_BOOL OPJ_CALLCONV opj_start_compress (  opj_codec_t *p_codec,
711
                      opj_image_t * p_image,
712
                      opj_stream_t *p_stream)
713
0
{
714
0
  if (p_codec && p_stream) {
715
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
716
0
    opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
717
718
0
    if (! l_codec->is_decompressor) {
719
0
      return l_codec->m_codec_data.m_compression.opj_start_compress(  l_codec->m_codec,
720
0
                                      l_stream,
721
0
                                      p_image,
722
0
                                      &(l_codec->m_event_mgr));
723
0
    }
724
0
  }
725
726
0
  return OPJ_FALSE;
727
0
}
728
729
OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
730
0
{
731
0
  if (p_info && p_stream) {
732
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
733
0
    opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
734
735
0
    if (! l_codec->is_decompressor) {
736
0
      return l_codec->m_codec_data.m_compression.opj_encode(  l_codec->m_codec,
737
0
                              l_stream,
738
0
                              &(l_codec->m_event_mgr));
739
0
    }
740
0
  }
741
742
0
  return OPJ_FALSE;
743
744
0
}
745
746
OPJ_BOOL OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
747
                    opj_stream_t *p_stream)
748
0
{
749
0
  if (p_codec && p_stream) {
750
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
751
0
    opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
752
753
0
    if (! l_codec->is_decompressor) {
754
0
      return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
755
0
                                    l_stream,
756
0
                                    &(l_codec->m_event_mgr));
757
0
    }
758
0
  }
759
0
  return OPJ_FALSE;
760
761
0
}
762
763
OPJ_BOOL OPJ_CALLCONV opj_end_decompress (  opj_codec_t *p_codec,
764
                      opj_stream_t *p_stream)
765
0
{
766
0
  if (p_codec && p_stream) {
767
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
768
0
    opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
769
770
0
    if (! l_codec->is_decompressor) {
771
0
      return OPJ_FALSE;
772
0
    }
773
    
774
0
    return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
775
0
                                    l_stream,
776
0
                                    &(l_codec->m_event_mgr) );
777
0
  }
778
779
0
  return OPJ_FALSE;
780
0
}
781
782
OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
783
                                  OPJ_FLOAT32 * pEncodingMatrix,
784
                                  OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
785
0
{
786
0
  OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
787
0
  OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
788
0
  OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
789
790
  /* add MCT capability */
791
0
  OPJ_INT32 rsiz = (OPJ_INT32)parameters->cp_rsiz | (OPJ_INT32)OPJ_MCT;
792
0
  parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
793
0
  parameters->irreversible = 1;
794
795
  /* use array based MCT */
796
0
  parameters->tcp_mct = 2;
797
0
  parameters->mct_data = opj_malloc(l_mct_total_size);
798
0
  if (! parameters->mct_data) {
799
0
    return OPJ_FALSE;
800
0
  }
801
802
0
  memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
803
0
  memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
804
805
0
  return OPJ_TRUE;
806
0
}
807
808
OPJ_BOOL OPJ_CALLCONV opj_write_tile (  opj_codec_t *p_codec,
809
                    OPJ_UINT32 p_tile_index,
810
                    OPJ_BYTE * p_data,
811
                    OPJ_UINT32 p_data_size,
812
                    opj_stream_t *p_stream )
813
0
{
814
0
  if (p_codec && p_stream && p_data) {
815
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
816
0
    opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
817
818
0
    if (l_codec->is_decompressor) {
819
0
      return OPJ_FALSE;
820
0
    }
821
822
0
    return l_codec->m_codec_data.m_compression.opj_write_tile(  l_codec->m_codec,
823
0
                                  p_tile_index,
824
0
                                  p_data,
825
0
                                  p_data_size,
826
0
                                  l_stream,
827
0
                                  &(l_codec->m_event_mgr) );
828
0
  }
829
830
0
  return OPJ_FALSE;
831
0
}
832
833
/* ---------------------------------------------------------------------- */
834
835
void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
836
0
{
837
0
  if (p_codec) {
838
0
    opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
839
840
0
    if (l_codec->is_decompressor) {
841
0
      l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
842
0
    }
843
0
    else {
844
0
      l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
845
0
    }
846
847
0
    l_codec->m_codec = 00;
848
0
    opj_free(l_codec);
849
0
  }
850
0
}
851
852
/* ---------------------------------------------------------------------- */
853
854
void OPJ_CALLCONV opj_dump_codec( opj_codec_t *p_codec,
855
                  OPJ_INT32 info_flag,
856
                  FILE* output_stream)
857
0
{
858
0
  if (p_codec) {
859
0
    opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
860
861
0
    l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
862
0
    return;
863
0
  }
864
865
0
  fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
866
0
  return;
867
0
}
868
869
opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
870
0
{
871
0
  if (p_codec) {
872
0
    opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
873
874
0
    return l_codec->opj_get_codec_info(l_codec->m_codec);
875
0
  }
876
877
0
  return NULL;
878
0
}
879
880
0
void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) {
881
0
  if (cstr_info) {
882
883
0
    if ((*cstr_info)->m_default_tile_info.tccp_info){
884
0
      opj_free((*cstr_info)->m_default_tile_info.tccp_info);
885
0
    }
886
887
0
    if ((*cstr_info)->tile_info){
888
      /* FIXME not used for the moment*/
889
0
    }
890
891
0
    opj_free((*cstr_info));
892
0
    (*cstr_info) = NULL;
893
0
  }
894
0
}
895
896
opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
897
0
{
898
0
  if (p_codec) {
899
0
    opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
900
901
0
    return l_codec->opj_get_codec_index(l_codec->m_codec);
902
0
  }
903
904
0
  return NULL;
905
0
}
906
907
void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
908
0
{
909
0
  if (*p_cstr_index){
910
0
    j2k_destroy_cstr_index(*p_cstr_index);
911
0
    (*p_cstr_index) = NULL;
912
0
  }
913
0
}
914
915
opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream_v3 (const char *fname, OPJ_BOOL p_is_read_stream)
916
0
{
917
0
    return opj_stream_create_file_stream_v3(fname, OPJ_J2K_STREAM_CHUNK_SIZE, p_is_read_stream);
918
0
}
919
920
opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream_v3 (
921
        const char *fname, 
922
    OPJ_SIZE_T p_size, 
923
        OPJ_BOOL p_is_read_stream)
924
0
{
925
0
    opj_stream_t* l_stream = 00;
926
0
    FILE *p_file;
927
0
    const char *mode;
928
929
0
    if (! fname) {
930
0
        return NULL;
931
0
    }
932
    
933
0
    if(p_is_read_stream) mode = "rb"; else mode = "wb";
934
935
0
    p_file = fopen(fname, mode);
936
937
0
    if (! p_file) {
938
0
      return NULL;
939
0
    }
940
941
0
    l_stream = opj_stream_create(p_size,p_is_read_stream);
942
0
    if (! l_stream) {
943
0
        fclose(p_file);
944
0
        return NULL;
945
0
    }
946
947
0
    opj_stream_set_user_data(l_stream, p_file, (opj_stream_free_user_data_fn) fclose);
948
0
    opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
949
0
    opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
950
0
    opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
951
0
    opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
952
0
    opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
953
954
0
    return l_stream;
955
0
}