1 /*
2  * Copyright (c) 2005, Herv� Drolon, FreeImage Team
3  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifdef WIN32
29 #include <windows.h>
30 #endif /* WIN32 */
31 
32 #include "openjpeg.h"
33 #include "opj_malloc.h"
34 #include "j2k.h"
35 #include "jp2.h"
36 #include "event.h"
37 #include "cio.h"
38 
39 typedef struct opj_decompression
40 {
41         bool (* opj_read_header) (
42                 void *p_codec,
43                 opj_image_t **,
44                 OPJ_INT32 * p_tile_x0,
45                 OPJ_INT32 * p_tile_y0,
46                 OPJ_UINT32 * p_tile_width,
47                 OPJ_UINT32 * p_tile_height,
48                 OPJ_UINT32 * p_nb_tiles_x,
49                 OPJ_UINT32 * p_nb_tiles_y,
50                 struct opj_stream_private *cio,
51                 struct opj_event_mgr * p_manager);
52         opj_image_t* (* opj_decode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
53         bool (*opj_read_tile_header)(
54                 void * p_codec,
55                 OPJ_UINT32 * p_tile_index,
56                 OPJ_UINT32* p_data_size,
57                 OPJ_INT32 * p_tile_x0,
58                 OPJ_INT32 * p_tile_y0,
59                 OPJ_INT32 * p_tile_x1,
60                 OPJ_INT32 * p_tile_y1,
61                 OPJ_UINT32 * p_nb_comps,
62                 bool * p_should_go_on,
63                 struct opj_stream_private *p_cio,
64                 struct opj_event_mgr * p_manager);
65                 bool (*opj_decode_tile_data)(void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private *p_cio,struct opj_event_mgr * p_manager);
66         bool (* opj_end_decompress) (void *p_codec,struct opj_stream_private *cio,struct opj_event_mgr * p_manager);
67         void (* opj_destroy) (void * p_codec);
68         void (*opj_setup_decoder) (void * p_codec,opj_dparameters_t * p_param);
69         bool (*opj_set_decode_area) (void * p_codec,OPJ_INT32 p_start_x,OPJ_INT32 p_end_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_y,struct opj_event_mgr * p_manager);
70 
71 
72 }opj_decompression_t;
73 
74 typedef struct opj_compression
75 {
76         bool (* opj_start_compress) (void *p_codec,struct opj_stream_private *cio,struct opj_image * p_image,        struct opj_event_mgr * p_manager);
77         bool (* opj_encode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
78         bool (* opj_write_tile) (void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private * p_cio,struct opj_event_mgr * p_manager);
79         bool (* opj_end_compress) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
80         void (* opj_destroy) (void * p_codec);
81         void (*opj_setup_encoder) (void * p_codec,opj_cparameters_t * p_param,struct opj_image * p_image, struct opj_event_mgr * p_manager);
82 
83 }opj_compression_t;
84 
85 
86 
87 typedef struct opj_codec_private
88 {
89         union
90         {                /* code-blocks informations */
91           opj_decompression_t m_decompression;
92           opj_compression_t m_compression;
93     } m_codec_data;
94         void * m_codec;
95         opj_event_mgr_t m_event_mgr;
96         unsigned is_decompressor : 1;
97 }
98 opj_codec_private_t;
99 
100 
101 
102 /**
103  * Default callback function.
104  * Do nothing.
105  */
opj_default_callback(const char * msg,void * client_data)106 void opj_default_callback (const char *msg, void *client_data)
107 {
108 }
109 
set_default_event_handler(opj_event_mgr_t * p_manager)110 void set_default_event_handler(opj_event_mgr_t * p_manager)
111 {
112         p_manager->m_error_data = 00;
113         p_manager->m_warning_data = 00;
114         p_manager->m_info_data = 00;
115         p_manager->error_handler = opj_default_callback;
116         p_manager->info_handler = opj_default_callback;
117         p_manager->warning_handler = opj_default_callback;
118 }
119 
opj_read_from_file(void * p_buffer,OPJ_UINT32 p_nb_bytes,FILE * p_file)120 OPJ_UINT32 opj_read_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
121 {
122         OPJ_UINT32 l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
123         return l_nb_read ? l_nb_read : -1;
124 }
125 
opj_write_from_file(void * p_buffer,OPJ_UINT32 p_nb_bytes,FILE * p_file)126 OPJ_UINT32 opj_write_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
127 {
128         return fwrite(p_buffer,1,p_nb_bytes,p_file);
129 }
130 
opj_skip_from_file(OPJ_SIZE_T p_nb_bytes,FILE * p_user_data)131 OPJ_SIZE_T opj_skip_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
132 {
133         if
134                 (fseek(p_user_data,p_nb_bytes,SEEK_CUR))
135         {
136                 return -1;
137         }
138         return p_nb_bytes;
139 }
140 
opj_seek_from_file(OPJ_SIZE_T p_nb_bytes,FILE * p_user_data)141 bool opj_seek_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
142 {
143         if
144                 (fseek(p_user_data,p_nb_bytes,SEEK_SET))
145         {
146                 return false;
147         }
148         return true;
149 }
150 
151 /* ---------------------------------------------------------------------- */
152 #ifdef WIN32
153 #ifndef OPJ_STATIC
154 BOOL APIENTRY
DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)155 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
156         switch (ul_reason_for_call) {
157                 case DLL_PROCESS_ATTACH :
158                         break;
159                 case DLL_PROCESS_DETACH :
160                         break;
161                 case DLL_THREAD_ATTACH :
162                 case DLL_THREAD_DETACH :
163                         break;
164     }
165 
166     return TRUE;
167 }
168 #endif /* OPJ_STATIC */
169 #endif /* WIN32 */
170 
171 /* ---------------------------------------------------------------------- */
172 
173 
opj_version(void)174 const char* OPJ_CALLCONV opj_version(void) {
175     return OPENJPEG_VERSION;
176 }
177 
opj_create_decompress(OPJ_CODEC_FORMAT p_format)178 opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
179 {
180         opj_codec_private_t *l_info = 00;
181 
182         l_info = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
183         if
184                 (!l_info)
185         {
186                 return 00;
187         }
188         memset(l_info, 0, sizeof(opj_codec_private_t));
189         l_info->is_decompressor = 1;
190         switch
191                 (p_format)
192         {
193                 case CODEC_J2K:
194                         l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))j2k_decode;
195                         l_info->m_codec_data.m_decompression.opj_end_decompress =  (bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *))j2k_end_decompress;
196                         l_info->m_codec_data.m_decompression.opj_read_header =  (bool (*) (
197                                 void *,
198                                 opj_image_t **,
199                                 OPJ_INT32 * ,
200                                 OPJ_INT32 * ,
201                                 OPJ_UINT32 * ,
202                                 OPJ_UINT32 * ,
203                                 OPJ_UINT32 * ,
204                                 OPJ_UINT32 * ,
205                                 struct opj_stream_private *,
206                                 struct opj_event_mgr * )) j2k_read_header;
207                         l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))j2k_destroy;
208                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) j2k_setup_decoder;
209                         l_info->m_codec_data.m_decompression.opj_read_tile_header = (bool (*) (
210                                 void *,
211                                 OPJ_UINT32*,
212                                 OPJ_UINT32*,
213                                 OPJ_INT32 * ,
214                                 OPJ_INT32 * ,
215                                 OPJ_INT32 * ,
216                                 OPJ_INT32 * ,
217                                 OPJ_UINT32 * ,
218                                 bool *,
219                                 struct opj_stream_private *,
220                                 struct opj_event_mgr * )) j2k_read_tile_header;
221                                 l_info->m_codec_data.m_decompression.opj_decode_tile_data = (bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *,        struct opj_event_mgr * )) j2k_decode_tile;
222                         l_info->m_codec_data.m_decompression.opj_set_decode_area = (bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) j2k_set_decode_area;
223                         l_info->m_codec = j2k_create_decompress();
224                         if
225                                 (! l_info->m_codec)
226                         {
227                                 opj_free(l_info);
228                                 return 00;
229                         }
230                         break;
231 
232                 case CODEC_JP2:
233                         /* get a JP2 decoder handle */
234                         l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))jp2_decode;
235                         l_info->m_codec_data.m_decompression.opj_end_decompress =  (bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
236                         l_info->m_codec_data.m_decompression.opj_read_header =  (bool (*) (
237                                 void *,
238                                 opj_image_t **,
239 
240                                 OPJ_INT32 * ,
241                                 OPJ_INT32 * ,
242                                 OPJ_UINT32 * ,
243                                 OPJ_UINT32 * ,
244                                 OPJ_UINT32 * ,
245                                 OPJ_UINT32 * ,
246                                 struct opj_stream_private *,
247                                 struct opj_event_mgr * )) jp2_read_header;
248 
249                         l_info->m_codec_data.m_decompression.opj_read_tile_header = (
250                                 bool (*) (
251                                         void *,
252                                         OPJ_UINT32*,
253                                         OPJ_UINT32*,
254                                         OPJ_INT32*,
255                                         OPJ_INT32*,
256                                         OPJ_INT32 * ,
257                                         OPJ_INT32 * ,
258                                         OPJ_UINT32 * ,
259                                         bool *,
260                                         struct opj_stream_private *,
261                                         struct opj_event_mgr * )) jp2_read_tile_header;
262 
263                         l_info->m_codec_data.m_decompression.opj_decode_tile_data = (bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *,        struct opj_event_mgr * )) jp2_decode_tile;
264 
265                         l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
266                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder;
267                         l_info->m_codec_data.m_decompression.opj_set_decode_area = (bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area;
268 
269 
270                         l_info->m_codec = jp2_create(true);
271                         if
272                                 (! l_info->m_codec)
273                         {
274                                 opj_free(l_info);
275                                 return 00;
276                         }
277                         break;
278                 case CODEC_UNKNOWN:
279                 case CODEC_JPT:
280                 default:
281                         opj_free(l_info);
282                         return 00;
283         }
284         set_default_event_handler(&(l_info->m_event_mgr));
285         return (opj_codec_t*) l_info;
286 }
287 
opj_destroy_codec(opj_codec_t * p_info)288 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
289 {
290         if
291                 (p_info)
292         {
293                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
294                 if
295                         (l_info->is_decompressor)
296                 {
297                         l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
298                 }
299                 else
300                 {
301                         l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
302                 }
303                 l_info->m_codec = 00;
304                 opj_free(l_info);
305         }
306 }
307 
opj_set_default_decoder_parameters(opj_dparameters_t * parameters)308 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
309         if(parameters) {
310                 memset(parameters, 0, sizeof(opj_dparameters_t));
311                 /* default decoding parameters */
312                 parameters->cp_layer = 0;
313                 parameters->cp_reduce = 0;
314 
315                 parameters->decod_format = -1;
316                 parameters->cod_format = -1;
317 /* UniPG>> */
318 #ifdef USE_JPWL
319                 parameters->jpwl_correct = false;
320                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
321                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
322 #endif /* USE_JPWL */
323 /* <<UniPG */
324         }
325 }
326 
opj_setup_decoder(opj_codec_t * p_info,opj_dparameters_t * parameters)327 bool OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_info, opj_dparameters_t *parameters) {
328         if
329                 (p_info && parameters)
330         {
331                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
332                 if
333                         (! l_info->is_decompressor)
334                 {
335                         return false;
336                 }
337                 l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec,parameters);
338                 return true;
339         }
340         return false;
341 }
342 
opj_decode(opj_codec_t * p_info,opj_stream_t * cio)343 opj_image_t* OPJ_CALLCONV opj_decode(opj_codec_t *p_info, opj_stream_t *cio)
344 {
345         if
346                 (p_info && cio)
347         {
348                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
349                 opj_stream_private_t * l_cio = (opj_stream_private_t *) cio;
350                 if
351                         (! l_info->is_decompressor)
352                 {
353                         return 00;
354                 }
355                 return l_info->m_codec_data.m_decompression.opj_decode(l_info->m_codec,l_cio,&(l_info->m_event_mgr));
356         }
357         return 00;
358 }
359 
360 /**
361  * Writes a tile with the given data.
362  *
363  * @param        p_compressor                the jpeg2000 codec.
364  * @param        p_tile_index                the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence.
365  * @param        p_data                                pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set.
366  * @param        p_data_size                        this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes,
367  *                                                                depending on the precision of the given component.
368  * @param        p_stream                        the stream to write data to.
369  */
opj_write_tile(opj_codec_t * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_t * p_stream)370 bool OPJ_CALLCONV opj_write_tile (
371                                          opj_codec_t *p_codec,
372                                          OPJ_UINT32 p_tile_index,
373                                          OPJ_BYTE * p_data,
374                                          OPJ_UINT32 p_data_size,
375                                          opj_stream_t *p_stream
376                                         )
377 {
378         if
379                 (p_codec && p_stream && p_data)
380         {
381                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
382                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
383                 if
384                         (l_info->is_decompressor)
385                 {
386                         return false;
387                 }
388                 return l_info->m_codec_data.m_compression.opj_write_tile(l_info->m_codec,p_tile_index,p_data,p_data_size,l_cio,&(l_info->m_event_mgr));
389         }
390         return false;
391 }
392 
393 /**
394  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
395  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
396  *
397  * @param        p_codec                        the jpeg2000 codec.
398  * @param        p_tile_index        pointer to a value that will hold the index of the tile being decoded, in case of success.
399  * @param        p_data_size                pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
400  *                                                        of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
401  *                                                        as depicted in opj_write_tile.
402  * @param        p_tile_x0                pointer to a value that will hold the x0 pos of the tile (in the image).
403  * @param        p_tile_y0                pointer to a value that will hold the y0 pos of the tile (in the image).
404  * @param        p_tile_x1                pointer to a value that will hold the x1 pos of the tile (in the image).
405  * @param        p_tile_y1                pointer to a value that will hold the y1 pos of the tile (in the image).
406  * @param        p_nb_comps                pointer to a value that will hold the number of components in the tile.
407  * @param        p_should_go_on        pointer to a boolean that will hold the fact that the decoding should go on. In case the
408  *                                                        codestream is over at the time of the call, the value will be set to false. The user should then stop
409  *                                                        the decoding.
410  * @param        p_stream                the stream to decode.
411  * @return        true                        if the tile header could be decoded. In case the decoding should end, the returned value is still true.
412  *                                                        returning false may be the result of a shortage of memory or an internal error.
413  */
opj_read_tile_header(opj_codec_t * p_codec,OPJ_UINT32 * p_tile_index,OPJ_UINT32 * p_data_size,OPJ_INT32 * p_tile_x0,OPJ_INT32 * p_tile_y0,OPJ_INT32 * p_tile_x1,OPJ_INT32 * p_tile_y1,OPJ_UINT32 * p_nb_comps,bool * p_should_go_on,opj_stream_t * p_stream)414 bool OPJ_CALLCONV opj_read_tile_header(
415                                         opj_codec_t *p_codec,
416                                         OPJ_UINT32 * p_tile_index,
417                                         OPJ_UINT32 * p_data_size,
418                                         OPJ_INT32 * p_tile_x0,
419                                         OPJ_INT32 * p_tile_y0,
420                                         OPJ_INT32 * p_tile_x1,
421                                         OPJ_INT32 * p_tile_y1,
422                                         OPJ_UINT32 * p_nb_comps,
423                                         bool * p_should_go_on,
424                                         opj_stream_t * p_stream)
425 {
426         if
427                 (p_codec && p_stream && p_data_size && p_tile_index)
428         {
429                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
430                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
431                 if
432                         (! l_info->is_decompressor)
433                 {
434                         return false;
435                 }
436                 return l_info->m_codec_data.m_decompression.opj_read_tile_header(
437                         l_info->m_codec,
438                         p_tile_index,
439                         p_data_size,
440                         p_tile_x0,
441                         p_tile_y0,
442                         p_tile_x1,
443                         p_tile_y1,
444                         p_nb_comps,
445                         p_should_go_on,
446                         l_cio,&(l_info->m_event_mgr));
447         }
448         return false;
449 }
450 
451 /**
452  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
453  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
454  *
455  * @param        p_codec                        the jpeg2000 codec.
456  * @param        p_tile_index        the index of the tile being decoded, this should be the value set by opj_read_tile_header.
457  * @param        p_data                        pointer to a memory block that will hold the decoded data.
458  * @param        p_data_size                size of p_data. p_data_size should be bigger or equal to the value set by opj_read_tile_header.
459  * @param        p_stream                the stream to decode.
460  *
461  * @return        true                        if the data could be decoded.
462  */
opj_decode_tile_data(opj_codec_t * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_t * p_stream)463 bool OPJ_CALLCONV opj_decode_tile_data(
464                                         opj_codec_t *p_codec,
465                                         OPJ_UINT32 p_tile_index,
466                                         OPJ_BYTE * p_data,
467                                         OPJ_UINT32 p_data_size,
468                                         opj_stream_t *p_stream
469                                         )
470 {
471         if
472                 (p_codec && p_data && p_stream)
473         {
474                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
475                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
476                 if
477                         (! l_info->is_decompressor)
478                 {
479                         return false;
480                 }
481                 return l_info->m_codec_data.m_decompression.opj_decode_tile_data(l_info->m_codec,p_tile_index,p_data,p_data_size,l_cio,&(l_info->m_event_mgr));
482         }
483         return false;
484 }
485 
opj_read_header(opj_codec_t * p_codec,opj_image_t ** p_image,OPJ_INT32 * p_tile_x0,OPJ_INT32 * p_tile_y0,OPJ_UINT32 * p_tile_width,OPJ_UINT32 * p_tile_height,OPJ_UINT32 * p_nb_tiles_x,OPJ_UINT32 * p_nb_tiles_y,opj_stream_t * p_cio)486 bool OPJ_CALLCONV opj_read_header (
487                                                                    opj_codec_t *p_codec,
488                                                                    opj_image_t ** p_image,
489                                                                    OPJ_INT32 * p_tile_x0,
490                                                                    OPJ_INT32 * p_tile_y0,
491                                                                    OPJ_UINT32 * p_tile_width,
492                                                                    OPJ_UINT32 * p_tile_height,
493                                                                    OPJ_UINT32 * p_nb_tiles_x,
494                                                                    OPJ_UINT32 * p_nb_tiles_y,
495                                                                    opj_stream_t *p_cio)
496 {
497         if
498                 (p_codec && p_cio)
499         {
500                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
501                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
502                 if
503                         (! l_info->is_decompressor)
504                 {
505                         return false;
506                 }
507                 return l_info->m_codec_data.m_decompression.opj_read_header(
508                         l_info->m_codec,
509                         p_image,
510                         p_tile_x0,
511                         p_tile_y0,
512                         p_tile_width,
513                         p_tile_height,
514                         p_nb_tiles_x,
515                         p_nb_tiles_y,
516                         l_cio,
517                         &(l_info->m_event_mgr));
518         }
519         return false;
520 }
521 
522 /**
523  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
524  *
525  * @param        p_codec                        the jpeg2000 codec.
526  * @param        p_start_x                the left position of the rectangle to decode (in image coordinates).
527  * @param        p_end_x                        the right position of the rectangle to decode (in image coordinates).
528  * @param        p_start_y                the up position of the rectangle to decode (in image coordinates).
529  * @param        p_end_y                        the bottom position of the rectangle to decode (in image coordinates).
530  *
531  * @return        true                        if the area could be set.
532  */
opj_set_decode_area(opj_codec_t * p_codec,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y)533 bool OPJ_CALLCONV opj_set_decode_area(
534                                         opj_codec_t *p_codec,
535                                         OPJ_INT32 p_start_x,
536                                         OPJ_INT32 p_start_y,
537                                         OPJ_INT32 p_end_x,
538                                         OPJ_INT32 p_end_y
539                                         )
540 {
541         if
542                 (p_codec)
543         {
544                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
545                 if
546                         (! l_info->is_decompressor)
547                 {
548                         return false;
549                 }
550                 return  l_info->m_codec_data.m_decompression.opj_set_decode_area(
551                                 l_info->m_codec,
552                                 p_start_x,
553                                 p_start_y,
554                                 p_end_x,
555                                 p_end_y,
556                                 &(l_info->m_event_mgr));
557 
558         }
559         return false;
560 
561 }
562 
opj_end_decompress(opj_codec_t * p_codec,opj_stream_t * p_cio)563 bool OPJ_CALLCONV opj_end_decompress (opj_codec_t *p_codec,opj_stream_t *p_cio)
564 {
565         if
566                 (p_codec && p_cio)
567         {
568                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
569                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
570                 if
571                         (! l_info->is_decompressor)
572                 {
573                         return false;
574                 }
575                 return l_info->m_codec_data.m_decompression.opj_end_decompress(l_info->m_codec,l_cio,&(l_info->m_event_mgr));
576         }
577         return false;
578 }
579 
580 
opj_create_compress(OPJ_CODEC_FORMAT p_format)581 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
582 {
583         opj_codec_private_t *l_info = 00;
584 
585         l_info = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
586         if
587                 (!l_info)
588         {
589                 return 00;
590         }
591         memset(l_info, 0, sizeof(opj_codec_private_t));
592         l_info->is_decompressor = 0;
593         switch
594                 (p_format)
595         {
596                 case CODEC_J2K:
597                         l_info->m_codec_data.m_compression.opj_encode = (bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr * )) j2k_encode;
598                         l_info->m_codec_data.m_compression.opj_end_compress = (bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr *)) j2k_end_compress;
599                         l_info->m_codec_data.m_compression.opj_start_compress = (bool (*) (void *,struct opj_stream_private *,struct opj_image * ,        struct opj_event_mgr *)) j2k_start_compress;
600                         l_info->m_codec_data.m_compression.opj_write_tile = (bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *,        struct opj_event_mgr *)) j2k_write_tile;
601                         l_info->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
602                         l_info->m_codec_data.m_compression.opj_setup_encoder = (void (*) (void *,opj_cparameters_t *,struct opj_image *, struct opj_event_mgr * )) j2k_setup_encoder;
603 
604                         l_info->m_codec = j2k_create_compress();
605                         if
606                                 (! l_info->m_codec)
607                         {
608                                 opj_free(l_info);
609                                 return 00;
610                         }
611                         break;
612 
613                 case CODEC_JP2:
614                         /* get a JP2 decoder handle */
615                         l_info->m_codec_data.m_compression.opj_encode = (bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr * )) jp2_encode;
616                         l_info->m_codec_data.m_compression.opj_end_compress = (bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr *)) jp2_end_compress;
617                         l_info->m_codec_data.m_compression.opj_start_compress = (bool (*) (void *,struct opj_stream_private *,struct opj_image * ,        struct opj_event_mgr *))  jp2_start_compress;
618                         l_info->m_codec_data.m_compression.opj_write_tile = (bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *,        struct opj_event_mgr *)) jp2_write_tile;
619                         l_info->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
620                         l_info->m_codec_data.m_compression.opj_setup_encoder = (void (*) (void *,opj_cparameters_t *,struct opj_image *, struct opj_event_mgr * )) jp2_setup_encoder;
621 
622                         l_info->m_codec = jp2_create(false);
623                         if
624                                 (! l_info->m_codec)
625                         {
626                                 opj_free(l_info);
627                                 return 00;
628                         }
629                         break;
630                 case CODEC_UNKNOWN:
631                 case CODEC_JPT:
632                 default:
633                         opj_free(l_info);
634                         return 00;
635         }
636         set_default_event_handler(&(l_info->m_event_mgr));
637         return (opj_codec_t*) l_info;
638 }
639 
opj_set_default_encoder_parameters(opj_cparameters_t * parameters)640 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
641         if(parameters) {
642                 memset(parameters, 0, sizeof(opj_cparameters_t));
643                 /* default coding parameters */
644                 parameters->cp_cinema = OFF;
645                 parameters->max_comp_size = 0;
646                 parameters->numresolution = 6;
647                 parameters->cp_rsiz = STD_RSIZ;
648                 parameters->cblockw_init = 64;
649                 parameters->cblockh_init = 64;
650                 parameters->prog_order = LRCP;
651                 parameters->roi_compno = -1;                /* no ROI */
652                 parameters->subsampling_dx = 1;
653                 parameters->subsampling_dy = 1;
654                 parameters->tp_on = 0;
655                 parameters->decod_format = -1;
656                 parameters->cod_format = -1;
657                 parameters->tcp_rates[0] = 0;
658                 parameters->tcp_numlayers = 0;
659     parameters->cp_disto_alloc = 0;
660                 parameters->cp_fixed_alloc = 0;
661                 parameters->cp_fixed_quality = 0;
662 /* UniPG>> */
663 #ifdef USE_JPWL
664                 parameters->jpwl_epc_on = false;
665                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
666                 {
667                         int i;
668                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
669                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
670                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
671                         }
672                 };
673                 {
674                         int i;
675                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
676                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
677                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
678                                 parameters->jpwl_pprot[i] = 0; /* absent */
679                         }
680                 };
681                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
682                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
683                 parameters->jpwl_sens_range = 0; /* 0 means packet */
684                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
685                 {
686                         int i;
687                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
688                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
689                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
690                         }
691                 };
692 #endif /* USE_JPWL */
693 /* <<UniPG */
694         }
695 }
696 
697 /**
698  * Helper function.
699  * Sets the stream to be a file stream. The FILE must have been open previously.
700  * @param                p_stream        the stream to modify
701  * @param                p_file                handler to an already open file.
702 */
opj_stream_create_default_file_stream(FILE * p_file,bool p_is_read_stream)703 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file,bool p_is_read_stream)
704 {
705         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
706 }
707 
opj_stream_create_file_stream(FILE * p_file,OPJ_UINT32 p_size,bool p_is_read_stream)708 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file,OPJ_UINT32 p_size,bool p_is_read_stream)
709 {
710         opj_stream_t* l_stream = 00;
711         if
712                 (! p_file)
713         {
714                 return 00;
715         }
716         l_stream = opj_stream_create(p_size,p_is_read_stream);
717         if
718                 (! l_stream)
719         {
720                 return 00;
721         }
722         opj_stream_set_user_data(l_stream,p_file);
723         opj_stream_set_read_function(l_stream,(opj_stream_read_fn) opj_read_from_file);
724         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
725         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
726         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
727         return l_stream;
728 }
729 
730 
opj_setup_encoder(opj_codec_t * p_info,opj_cparameters_t * parameters,opj_image_t * image)731 bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_info, opj_cparameters_t *parameters, opj_image_t *image)
732 {
733         if
734                 (p_info && parameters && image)
735         {
736                 opj_codec_private_t * l_codec = ((opj_codec_private_t *) p_info);
737                 if
738                         (! l_codec->is_decompressor)
739                 {
740                         l_codec->m_codec_data.m_compression.opj_setup_encoder(l_codec->m_codec,parameters,image,&(l_codec->m_event_mgr));
741                         return true;
742                 }
743         }
744         return false;
745 }
746 
opj_encode(opj_codec_t * p_info,opj_stream_t * cio)747 bool OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *cio)
748 {
749         if
750                 (p_info && cio)
751         {
752                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
753                 opj_stream_private_t * l_cio = (opj_stream_private_t *) cio;
754                 if
755                         (! l_codec->is_decompressor)
756                 {
757                         l_codec->m_codec_data.m_compression.opj_encode(l_codec->m_codec,l_cio,&(l_codec->m_event_mgr));
758                         return true;
759                 }
760         }
761         return false;
762 
763 }
764 
opj_start_compress(opj_codec_t * p_codec,opj_image_t * p_image,opj_stream_t * p_cio)765 bool OPJ_CALLCONV opj_start_compress (opj_codec_t *p_codec,opj_image_t * p_image,opj_stream_t *p_cio)
766 {
767         if
768                 (p_codec && p_cio)
769         {
770                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
771                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
772                 if
773                         (! l_codec->is_decompressor)
774                 {
775                         return l_codec->m_codec_data.m_compression.opj_start_compress(l_codec->m_codec,l_cio,p_image,&(l_codec->m_event_mgr));
776                 }
777         }
778         return false;
779 }
780 
opj_end_compress(opj_codec_t * p_codec,opj_stream_t * p_cio)781 bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,opj_stream_t *p_cio)
782 {
783         if
784                 (p_codec && p_cio)
785         {
786                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
787                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
788                 if
789                         (! l_codec->is_decompressor)
790                 {
791                         return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,l_cio,&(l_codec->m_event_mgr));
792                 }
793         }
794         return false;
795 
796 }
797 
opj_set_info_handler(opj_codec_t * p_codec,opj_msg_callback p_callback,void * p_user_data)798 bool OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec, opj_msg_callback p_callback,void * p_user_data)
799 {
800         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
801         if
802                 (! l_codec)
803         {
804                 return false;
805         }
806         l_codec->m_event_mgr.info_handler = p_callback;
807         l_codec->m_event_mgr.m_info_data = p_user_data;
808         return true;
809 }
810 
opj_set_warning_handler(opj_codec_t * p_codec,opj_msg_callback p_callback,void * p_user_data)811 bool OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec, opj_msg_callback p_callback,void * p_user_data)
812 {
813         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
814         if
815                 (! l_codec)
816         {
817                 return false;
818         }
819         l_codec->m_event_mgr.warning_handler = p_callback;
820         l_codec->m_event_mgr.m_warning_data = p_user_data;
821         return true;
822 }
823 
opj_set_error_handler(opj_codec_t * p_codec,opj_msg_callback p_callback,void * p_user_data)824 bool OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, opj_msg_callback p_callback,void * p_user_data)
825 {
826         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
827         if
828                 (! l_codec)
829         {
830                 return false;
831         }
832         l_codec->m_event_mgr.error_handler = p_callback;
833         l_codec->m_event_mgr.m_error_data = p_user_data;
834         return true;
835 }
836 
837 /*bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_stream_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
838         if(cinfo && cio && image) {
839                 switch(cinfo->codec_format) {
840                         case CODEC_J2K:
841                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, (opj_stream_private_t *) cio, image, cstr_info);
842                         case CODEC_JP2:
843                                 return jp2_encode((opj_jp2_t*)cinfo->jp2_handle, (opj_stream_private_t *) cio, image, cstr_info);
844                         case CODEC_JPT:
845                         case CODEC_UNKNOWN:
846                         default:
847                                 break;
848                 }
849         }
850         return false;
851 }*/
852 
opj_destroy_cstr_info(opj_codestream_info_t * cstr_info)853 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
854         if
855                 (cstr_info)
856         {
857                 int tileno;
858                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
859                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
860                         opj_free(tile_info->thresh);
861                         opj_free(tile_info->packet);
862                         opj_free(tile_info->tp);
863                 }
864                 opj_free(cstr_info->tile);
865                 opj_free(cstr_info->marker);
866         }
867 }
868 
opj_set_MCT(opj_cparameters_t * parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)869 bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
870 {
871         OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
872         OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
873         OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
874         // add MCT capability
875         int rsiz = (int)parameters->cp_rsiz | (int)MCT;
876         parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
877         parameters->irreversible = 1;
878         // use array based MCT
879         parameters->tcp_mct = 2;
880         parameters->mct_data = opj_malloc(l_mct_total_size);
881         if
882                 (! parameters->mct_data)
883         {
884                 return false;
885         }
886         memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
887         memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
888         return true;
889 }
890 
891 /**
892  * Restricts the decoding to the given image area.
893  *
894  * @param        parameters                the parameters to update.
895  * @param        p_start_x                the starting x position of the area to decode.
896  * @param        p_start_y                the starting y position of the area to decode.
897  * @param        p_end_x                        the x end position of the area to decode.
898  * @param        p_end_x                        the y end position of the area to decode.
899  */
opj_restrict_decoding(opj_dparameters_t * parameters,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y)900 OPJ_API bool OPJ_CALLCONV opj_restrict_decoding (opj_dparameters_t *parameters,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y)
901 {
902         parameters->m_use_restrict_decode = 1;
903         parameters->m_decode_start_x = p_start_x;
904         parameters->m_decode_start_y = p_start_y;
905         parameters->m_decode_end_x = p_end_x;
906         parameters->m_decode_end_y = p_end_y;
907         return true;
908 }
909