Coverage Report

Created: 2023-12-08 06:53

/src/freeimage-svn/FreeImage/trunk/Source/LibOpenJPEG/pi.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3
 * Copyright (c) 2002-2007, Professor Benoit Macq
4
 * Copyright (c) 2001-2003, David Janssens
5
 * Copyright (c) 2002-2003, Yannick Verschueren
6
 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8
 * Copyright (c) 2006-2007, Parvatha Elangovan
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
 * POSSIBILITY OF SUCH DAMAGE.
31
 */
32
33
#include "opj_includes.h"
34
35
/** @defgroup PI PI - Implementation of a packet iterator */
36
/*@{*/
37
38
/** @name Local static functions */
39
/*@{*/
40
41
/**
42
Get next packet in layer-resolution-component-precinct order.
43
@param pi packet iterator to modify
44
@return returns false if pi pointed to the last packet or else returns true
45
*/
46
static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
47
/**
48
Get next packet in resolution-layer-component-precinct order.
49
@param pi packet iterator to modify
50
@return returns false if pi pointed to the last packet or else returns true
51
*/
52
static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
53
/**
54
Get next packet in resolution-precinct-component-layer order.
55
@param pi packet iterator to modify
56
@return returns false if pi pointed to the last packet or else returns true
57
*/
58
static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
59
/**
60
Get next packet in precinct-component-resolution-layer order.
61
@param pi packet iterator to modify
62
@return returns false if pi pointed to the last packet or else returns true
63
*/
64
static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
65
/**
66
Get next packet in component-precinct-resolution-layer order.
67
@param pi packet iterator to modify
68
@return returns false if pi pointed to the last packet or else returns true
69
*/
70
static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
71
72
/**
73
 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
74
 *
75
 * @param p_cp    the coding parameters to modify
76
 * @param p_tileno  the tile index being concerned.
77
 * @param p_tx0   X0 parameter for the tile
78
 * @param p_tx1   X1 parameter for the tile
79
 * @param p_ty0   Y0 parameter for the tile
80
 * @param p_ty1   Y1 parameter for the tile
81
 * @param p_max_prec  the maximum precision for all the bands of the tile
82
 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
83
 * @param p_dx_min    the minimum dx of all the components of all the resolutions for the tile.
84
 * @param p_dy_min    the minimum dy of all the components of all the resolutions for the tile.
85
 */
86
static void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
87
                                                 OPJ_UINT32 p_tileno,
88
                                                 OPJ_INT32 p_tx0,
89
                                                 OPJ_INT32 p_tx1,
90
                                                 OPJ_INT32 p_ty0,
91
                                                 OPJ_INT32 p_ty1,
92
                                                 OPJ_UINT32 p_max_prec,
93
                                                 OPJ_UINT32 p_max_res,
94
                                                 OPJ_UINT32 p_dx_min,
95
                                                 OPJ_UINT32 p_dy_min);
96
97
/**
98
 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
99
 *
100
 * @param p_cp    the coding parameters to modify
101
 * @param p_num_comps   the number of components
102
 * @param p_tileno  the tile index being concerned.
103
 * @param p_tx0   X0 parameter for the tile
104
 * @param p_tx1   X1 parameter for the tile
105
 * @param p_ty0   Y0 parameter for the tile
106
 * @param p_ty1   Y1 parameter for the tile
107
 * @param p_max_prec  the maximum precision for all the bands of the tile
108
 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
109
 * @param p_dx_min    the minimum dx of all the components of all the resolutions for the tile.
110
 * @param p_dy_min    the minimum dy of all the components of all the resolutions for the tile.
111
 */
112
static void opj_pi_update_encode_not_poc (  opj_cp_t *p_cp,
113
                                            OPJ_UINT32 p_num_comps,
114
                                            OPJ_UINT32 p_tileno,
115
                                            OPJ_INT32 p_tx0,
116
                                            OPJ_INT32 p_tx1,
117
                                            OPJ_INT32 p_ty0,
118
                                            OPJ_INT32 p_ty1,
119
                                            OPJ_UINT32 p_max_prec,
120
                                            OPJ_UINT32 p_max_res,
121
                                            OPJ_UINT32 p_dx_min,
122
                                            OPJ_UINT32 p_dy_min);
123
/**
124
 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
125
 * 
126
 * @param p_image     the image being encoded.
127
 * @param p_cp      the coding parameters.
128
 * @param tileno      the tile index of the tile being encoded.
129
 * @param p_tx0     pointer that will hold the X0 parameter for the tile
130
 * @param p_tx1     pointer that will hold the X1 parameter for the tile
131
 * @param p_ty0     pointer that will hold the Y0 parameter for the tile
132
 * @param p_ty1     pointer that will hold the Y1 parameter for the tile
133
 * @param p_max_prec    pointer that will hold the the maximum precision for all the bands of the tile
134
 * @param p_max_res   pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
135
 * @param p_dx_min      pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
136
 * @param p_dy_min      pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
137
 */
138
static void opj_get_encoding_parameters(const opj_image_t *p_image,
139
                                        const opj_cp_t *p_cp,
140
                                        OPJ_UINT32  tileno,
141
                                        OPJ_INT32  * p_tx0,
142
                                        OPJ_INT32 * p_tx1,
143
                                        OPJ_INT32 * p_ty0,
144
                                        OPJ_INT32 * p_ty1,
145
                                        OPJ_UINT32 * p_dx_min,
146
                                        OPJ_UINT32 * p_dy_min,
147
                                        OPJ_UINT32 * p_max_prec,
148
                                        OPJ_UINT32 * p_max_res );
149
150
/**
151
 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
152
 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
153
 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
154
 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
155
 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
156
 *
157
 * @param p_image     the image being encoded.
158
 * @param p_cp      the coding parameters.
159
 * @param tileno      the tile index of the tile being encoded.
160
 * @param p_tx0     pointer that will hold the X0 parameter for the tile
161
 * @param p_tx1     pointer that will hold the X1 parameter for the tile
162
 * @param p_ty0     pointer that will hold the Y0 parameter for the tile
163
 * @param p_ty1     pointer that will hold the Y1 parameter for the tile
164
 * @param p_max_prec    pointer that will hold the the maximum precision for all the bands of the tile
165
 * @param p_max_res   pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
166
 * @param p_dx_min    pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
167
 * @param p_dy_min    pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
168
 * @param p_resolutions pointer to an area corresponding to the one described above.
169
 */
170
static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
171
                                            const opj_cp_t *p_cp,
172
                                            OPJ_UINT32 tileno,
173
                                            OPJ_INT32 * p_tx0,
174
                                            OPJ_INT32 * p_tx1,
175
                                            OPJ_INT32 * p_ty0,
176
                                            OPJ_INT32 * p_ty1,
177
                                            OPJ_UINT32 * p_dx_min,
178
                                            OPJ_UINT32 * p_dy_min,
179
                                            OPJ_UINT32 * p_max_prec,
180
                                            OPJ_UINT32 * p_max_res,
181
                                            OPJ_UINT32 ** p_resolutions );
182
/**
183
 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
184
 * No other data is set. The include section of the packet  iterator is not allocated.
185
 * 
186
 * @param p_image   the image used to initialize the packet iterator (in fact only the number of components is relevant.
187
 * @param p_cp    the coding parameters.
188
 * @param tileno  the index of the tile from which creating the packet iterator.
189
 */
190
static opj_pi_iterator_t * opj_pi_create( const opj_image_t *p_image,
191
                                            const opj_cp_t *p_cp,
192
                                            OPJ_UINT32 tileno );
193
/**
194
 * FIXME DOC
195
 */
196
static void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
197
                                          opj_tcp_t * p_tcp,
198
                                          OPJ_UINT32 p_max_precision,
199
                                          OPJ_UINT32 p_max_res);
200
/**
201
 * FIXME DOC
202
 */
203
static void opj_pi_update_decode_poc (  opj_pi_iterator_t * p_pi,
204
                                        opj_tcp_t * p_tcp,
205
                                        OPJ_UINT32 p_max_precision,
206
                                        OPJ_UINT32 p_max_res);
207
208
/**
209
 * FIXME DOC
210
 */
211
OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos,
212
                opj_cp_t *cp,
213
                OPJ_UINT32 tileno,
214
                OPJ_UINT32 pino,
215
                const OPJ_CHAR *prog);
216
217
/*@}*/
218
219
/*@}*/
220
221
/*
222
==========================================================
223
   local functions
224
==========================================================
225
*/
226
227
0
OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi) {
228
0
  opj_pi_comp_t *comp = NULL;
229
0
  opj_pi_resolution_t *res = NULL;
230
0
  OPJ_UINT32 index = 0;
231
  
232
0
  if (!pi->first) {
233
0
    comp = &pi->comps[pi->compno];
234
0
    res = &comp->resolutions[pi->resno];
235
0
    goto LABEL_SKIP;
236
0
  } else {
237
0
    pi->first = 0;
238
0
  }
239
240
0
  for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
241
0
    for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
242
0
    pi->resno++) {
243
0
      for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
244
0
        comp = &pi->comps[pi->compno];
245
0
        if (pi->resno >= comp->numresolutions) {
246
0
          continue;
247
0
        }
248
0
        res = &comp->resolutions[pi->resno];
249
0
        if (!pi->tp_on){
250
0
          pi->poc.precno1 = res->pw * res->ph;
251
0
        }
252
0
        for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
253
0
          index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
254
0
          if (!pi->include[index]) {
255
0
            pi->include[index] = 1;
256
0
            return OPJ_TRUE;
257
0
          }
258
0
LABEL_SKIP:;
259
0
        }
260
0
      }
261
0
    }
262
0
  }
263
  
264
0
  return OPJ_FALSE;
265
0
}
266
267
0
OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi) {
268
0
  opj_pi_comp_t *comp = NULL;
269
0
  opj_pi_resolution_t *res = NULL;
270
0
  OPJ_UINT32 index = 0;
271
272
0
  if (!pi->first) {
273
0
    comp = &pi->comps[pi->compno];
274
0
    res = &comp->resolutions[pi->resno];
275
0
    goto LABEL_SKIP;
276
0
  } else {
277
0
    pi->first = 0;
278
0
  }
279
280
0
  for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
281
0
    for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
282
0
      for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
283
0
        comp = &pi->comps[pi->compno];
284
0
        if (pi->resno >= comp->numresolutions) {
285
0
          continue;
286
0
        }
287
0
        res = &comp->resolutions[pi->resno];
288
0
        if(!pi->tp_on){
289
0
          pi->poc.precno1 = res->pw * res->ph;
290
0
        }
291
0
        for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
292
0
          index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
293
0
          if (!pi->include[index]) {
294
0
            pi->include[index] = 1;
295
0
            return OPJ_TRUE;
296
0
          }
297
0
LABEL_SKIP:;
298
0
        }
299
0
      }
300
0
    }
301
0
  }
302
  
303
0
  return OPJ_FALSE;
304
0
}
305
306
0
OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
307
0
  opj_pi_comp_t *comp = NULL;
308
0
  opj_pi_resolution_t *res = NULL;
309
0
  OPJ_UINT32 index = 0;
310
311
0
  if (!pi->first) {
312
0
    goto LABEL_SKIP;
313
0
  } else {
314
0
    OPJ_UINT32 compno, resno;
315
0
    pi->first = 0;
316
0
    pi->dx = 0;
317
0
    pi->dy = 0;
318
0
    for (compno = 0; compno < pi->numcomps; compno++) {
319
0
      comp = &pi->comps[compno];
320
0
      for (resno = 0; resno < comp->numresolutions; resno++) {
321
0
        OPJ_UINT32 dx, dy;
322
0
        res = &comp->resolutions[resno];
323
0
        dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
324
0
        dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
325
0
        pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
326
0
        pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
327
0
      }
328
0
    }
329
0
  }
330
0
if (!pi->tp_on){
331
0
      pi->poc.ty0 = pi->ty0;
332
0
      pi->poc.tx0 = pi->tx0;
333
0
      pi->poc.ty1 = pi->ty1;
334
0
      pi->poc.tx1 = pi->tx1;
335
0
    }
336
0
  for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
337
0
    for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
338
0
      for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
339
0
        for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
340
0
          OPJ_UINT32 levelno;
341
0
          OPJ_INT32 trx0, try0;
342
0
          OPJ_INT32  trx1, try1;
343
0
          OPJ_UINT32  rpx, rpy;
344
0
          OPJ_INT32  prci, prcj;
345
0
          comp = &pi->comps[pi->compno];
346
0
          if (pi->resno >= comp->numresolutions) {
347
0
            continue;
348
0
          }
349
0
          res = &comp->resolutions[pi->resno];
350
0
          levelno = comp->numresolutions - 1 - pi->resno;
351
0
          trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
352
0
          try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
353
0
          trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
354
0
          try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
355
0
          rpx = res->pdx + levelno;
356
0
          rpy = res->pdy + levelno;
357
0
          if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
358
0
            continue; 
359
0
          }
360
0
          if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
361
0
            continue;
362
0
          }
363
          
364
0
          if ((res->pw==0)||(res->ph==0)) continue;
365
          
366
0
          if ((trx0==trx1)||(try0==try1)) continue;
367
          
368
0
          prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
369
0
             - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
370
0
          prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
371
0
             - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
372
0
          pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
373
0
          for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
374
0
            index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
375
0
            if (!pi->include[index]) {
376
0
              pi->include[index] = 1;
377
0
              return OPJ_TRUE;
378
0
            }
379
0
LABEL_SKIP:;
380
0
          }
381
0
        }
382
0
      }
383
0
    }
384
0
  }
385
  
386
0
  return OPJ_FALSE;
387
0
}
388
389
0
OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
390
0
  opj_pi_comp_t *comp = NULL;
391
0
  opj_pi_resolution_t *res = NULL;
392
0
  OPJ_UINT32 index = 0;
393
394
0
  if (!pi->first) {
395
0
    comp = &pi->comps[pi->compno];
396
0
    goto LABEL_SKIP;
397
0
  } else {
398
0
    OPJ_UINT32 compno, resno;
399
0
    pi->first = 0;
400
0
    pi->dx = 0;
401
0
    pi->dy = 0;
402
0
    for (compno = 0; compno < pi->numcomps; compno++) {
403
0
      comp = &pi->comps[compno];
404
0
      for (resno = 0; resno < comp->numresolutions; resno++) {
405
0
        OPJ_UINT32 dx, dy;
406
0
        res = &comp->resolutions[resno];
407
0
        dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
408
0
        dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
409
0
        pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
410
0
        pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
411
0
      }
412
0
    }
413
0
  }
414
0
  if (!pi->tp_on){
415
0
      pi->poc.ty0 = pi->ty0;
416
0
      pi->poc.tx0 = pi->tx0;
417
0
      pi->poc.ty1 = pi->ty1;
418
0
      pi->poc.tx1 = pi->tx1;
419
0
    }
420
0
  for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
421
0
    for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
422
0
      for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
423
0
        comp = &pi->comps[pi->compno];
424
0
        for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
425
0
          OPJ_UINT32 levelno;
426
0
          OPJ_INT32 trx0, try0;
427
0
          OPJ_INT32 trx1, try1;
428
0
          OPJ_UINT32 rpx, rpy;
429
0
          OPJ_INT32 prci, prcj;
430
0
          res = &comp->resolutions[pi->resno];
431
0
          levelno = comp->numresolutions - 1 - pi->resno;
432
0
          trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
433
0
          try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
434
0
          trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
435
0
          try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
436
0
          rpx = res->pdx + levelno;
437
0
          rpy = res->pdy + levelno;
438
0
          if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
439
0
            continue; 
440
0
          }
441
0
          if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
442
0
            continue;
443
0
          }
444
          
445
0
          if ((res->pw==0)||(res->ph==0)) continue;
446
          
447
0
          if ((trx0==trx1)||(try0==try1)) continue;
448
          
449
0
          prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
450
0
             - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
451
0
          prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
452
0
             - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
453
0
          pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
454
0
          for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
455
0
            index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
456
0
            if (!pi->include[index]) {
457
0
              pi->include[index] = 1;
458
0
              return OPJ_TRUE;
459
0
            } 
460
0
LABEL_SKIP:;
461
0
          }
462
0
        }
463
0
      }
464
0
    }
465
0
  }
466
  
467
0
  return OPJ_FALSE;
468
0
}
469
470
0
OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) {
471
0
  opj_pi_comp_t *comp = NULL;
472
0
  opj_pi_resolution_t *res = NULL;
473
0
  OPJ_UINT32 index = 0;
474
475
0
  if (!pi->first) {
476
0
    comp = &pi->comps[pi->compno];
477
0
    goto LABEL_SKIP;
478
0
  } else {
479
0
    pi->first = 0;
480
0
  }
481
482
0
  for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
483
0
    OPJ_UINT32 resno;
484
0
    comp = &pi->comps[pi->compno];
485
0
    pi->dx = 0;
486
0
    pi->dy = 0;
487
0
    for (resno = 0; resno < comp->numresolutions; resno++) {
488
0
      OPJ_UINT32 dx, dy;
489
0
      res = &comp->resolutions[resno];
490
0
      dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
491
0
      dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
492
0
      pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
493
0
      pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
494
0
    }
495
0
    if (!pi->tp_on){
496
0
      pi->poc.ty0 = pi->ty0;
497
0
      pi->poc.tx0 = pi->tx0;
498
0
      pi->poc.ty1 = pi->ty1;
499
0
      pi->poc.tx1 = pi->tx1;
500
0
    }
501
0
    for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
502
0
      for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
503
0
        for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
504
0
          OPJ_UINT32 levelno;
505
0
          OPJ_INT32 trx0, try0;
506
0
          OPJ_INT32 trx1, try1;
507
0
          OPJ_UINT32 rpx, rpy;
508
0
          OPJ_INT32 prci, prcj;
509
0
          res = &comp->resolutions[pi->resno];
510
0
          levelno = comp->numresolutions - 1 - pi->resno;
511
0
          trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
512
0
          try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
513
0
          trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
514
0
          try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
515
0
          rpx = res->pdx + levelno;
516
0
          rpy = res->pdy + levelno;
517
0
          if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
518
0
            continue; 
519
0
          }
520
0
          if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
521
0
            continue;
522
0
          }
523
          
524
0
          if ((res->pw==0)||(res->ph==0)) continue;
525
          
526
0
          if ((trx0==trx1)||(try0==try1)) continue;
527
          
528
0
          prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
529
0
             - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
530
0
          prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
531
0
             - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
532
0
          pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
533
0
          for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
534
0
            index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
535
0
            if (!pi->include[index]) {
536
0
              pi->include[index] = 1;
537
0
              return OPJ_TRUE;
538
0
            }
539
0
LABEL_SKIP:;
540
0
          }
541
0
        }
542
0
      }
543
0
    }
544
0
  }
545
  
546
0
  return OPJ_FALSE;
547
0
}
548
549
void opj_get_encoding_parameters( const opj_image_t *p_image,
550
                                    const opj_cp_t *p_cp,
551
                                    OPJ_UINT32 p_tileno,
552
                                    OPJ_INT32 * p_tx0,
553
                                    OPJ_INT32  * p_tx1,
554
                                    OPJ_INT32  * p_ty0,
555
                                    OPJ_INT32  * p_ty1,
556
                                    OPJ_UINT32 * p_dx_min,
557
                                    OPJ_UINT32 * p_dy_min,
558
                                    OPJ_UINT32 * p_max_prec,
559
                                    OPJ_UINT32 * p_max_res )
560
0
{
561
  /* loop */
562
0
  OPJ_UINT32  compno, resno;
563
  /* pointers */
564
0
  const opj_tcp_t *l_tcp = 00;
565
0
  const opj_tccp_t * l_tccp = 00;
566
0
  const opj_image_comp_t * l_img_comp = 00;
567
568
  /* position in x and y of tile */
569
0
  OPJ_UINT32 p, q;
570
571
  /* preconditions */
572
0
  assert(p_cp != 00);
573
0
  assert(p_image != 00);
574
0
  assert(p_tileno < p_cp->tw * p_cp->th);
575
576
  /* initializations */
577
0
  l_tcp = &p_cp->tcps [p_tileno];
578
0
  l_img_comp = p_image->comps;
579
0
  l_tccp = l_tcp->tccps;
580
581
  /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
582
0
  p = p_tileno % p_cp->tw;
583
0
  q = p_tileno / p_cp->tw;
584
585
  /* find extent of tile */
586
0
  *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
587
0
  *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
588
0
  *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
589
0
  *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
590
591
  /* max precision is 0 (can only grow) */
592
0
  *p_max_prec = 0;
593
0
  *p_max_res = 0;
594
595
  /* take the largest value for dx_min and dy_min */
596
0
  *p_dx_min = 0x7fffffff;
597
0
  *p_dy_min  = 0x7fffffff;
598
599
0
  for (compno = 0; compno < p_image->numcomps; ++compno) {
600
    /* arithmetic variables to calculate */
601
0
    OPJ_UINT32 l_level_no;
602
0
    OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
603
0
    OPJ_INT32 l_px0, l_py0, l_px1, py1;
604
0
    OPJ_UINT32 l_pdx, l_pdy;
605
0
    OPJ_UINT32 l_pw, l_ph;
606
0
    OPJ_UINT32 l_product;
607
0
    OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
608
609
0
    l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
610
0
    l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
611
0
    l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
612
0
    l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
613
614
0
    if (l_tccp->numresolutions > *p_max_res) {
615
0
      *p_max_res = l_tccp->numresolutions;
616
0
    }
617
618
    /* use custom size for precincts */
619
0
    for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
620
0
      OPJ_UINT32 l_dx, l_dy;
621
622
      /* precinct width and height */
623
0
      l_pdx = l_tccp->prcw[resno];
624
0
      l_pdy = l_tccp->prch[resno];
625
626
0
      l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
627
0
      l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
628
629
      /* take the minimum size for dx for each comp and resolution */
630
0
      *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
631
0
      *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
632
633
      /* various calculations of extents */
634
0
      l_level_no = l_tccp->numresolutions - 1 - resno;
635
636
0
      l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
637
0
      l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
638
0
      l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
639
0
      l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
640
641
0
      l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
642
0
      l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
643
0
      l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
644
645
0
      py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
646
647
0
      l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
648
0
      l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
649
650
0
      l_product = l_pw * l_ph;
651
652
      /* update precision */
653
0
      if (l_product > *p_max_prec) {
654
0
        *p_max_prec = l_product;
655
0
      }
656
0
    }
657
0
    ++l_img_comp;
658
0
    ++l_tccp;
659
0
  }
660
0
}
661
662
663
void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
664
                                        const opj_cp_t *p_cp,
665
                                        OPJ_UINT32 tileno,
666
                                        OPJ_INT32 * p_tx0,
667
                                        OPJ_INT32 * p_tx1,
668
                                        OPJ_INT32 * p_ty0,
669
                                        OPJ_INT32 * p_ty1,
670
                                        OPJ_UINT32 * p_dx_min,
671
                                        OPJ_UINT32 * p_dy_min,
672
                                        OPJ_UINT32 * p_max_prec,
673
                                        OPJ_UINT32 * p_max_res,
674
                                        OPJ_UINT32 ** p_resolutions )
675
0
{
676
  /* loop*/
677
0
  OPJ_UINT32 compno, resno;
678
679
  /* pointers*/
680
0
  const opj_tcp_t *tcp = 00;
681
0
  const opj_tccp_t * l_tccp = 00;
682
0
  const opj_image_comp_t * l_img_comp = 00;
683
684
  /* to store l_dx, l_dy, w and h for each resolution and component.*/
685
0
  OPJ_UINT32 * lResolutionPtr;
686
687
  /* position in x and y of tile*/
688
0
  OPJ_UINT32 p, q;
689
690
  /* preconditions in debug*/
691
0
  assert(p_cp != 00);
692
0
  assert(p_image != 00);
693
0
  assert(tileno < p_cp->tw * p_cp->th);
694
695
  /* initializations*/
696
0
  tcp = &p_cp->tcps [tileno];
697
0
  l_tccp = tcp->tccps;
698
0
  l_img_comp = p_image->comps;
699
700
  /* position in x and y of tile*/
701
0
  p = tileno % p_cp->tw;
702
0
  q = tileno / p_cp->tw;
703
704
  /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
705
0
  *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
706
0
  *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
707
0
  *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
708
0
  *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
709
710
  /* max precision and resolution is 0 (can only grow)*/
711
0
  *p_max_prec = 0;
712
0
  *p_max_res = 0;
713
714
  /* take the largest value for dx_min and dy_min*/
715
0
  *p_dx_min = 0x7fffffff;
716
0
  *p_dy_min = 0x7fffffff;
717
718
0
  for (compno = 0; compno < p_image->numcomps; ++compno) {
719
    /* aritmetic variables to calculate*/
720
0
    OPJ_UINT32 l_level_no;
721
0
    OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
722
0
    OPJ_INT32 l_px0, l_py0, l_px1, py1;
723
0
    OPJ_UINT32 l_product;
724
0
    OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
725
0
    OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
726
727
0
    lResolutionPtr = p_resolutions[compno];
728
729
0
    l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
730
0
    l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
731
0
    l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
732
0
    l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
733
734
0
    if (l_tccp->numresolutions > *p_max_res) {
735
0
      *p_max_res = l_tccp->numresolutions;
736
0
    }
737
738
    /* use custom size for precincts*/
739
0
    l_level_no = l_tccp->numresolutions - 1;
740
0
    for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
741
0
      OPJ_UINT32 l_dx, l_dy;
742
743
      /* precinct width and height*/
744
0
      l_pdx = l_tccp->prcw[resno];
745
0
      l_pdy = l_tccp->prch[resno];
746
0
      *lResolutionPtr++ = l_pdx;
747
0
      *lResolutionPtr++ = l_pdy;
748
0
      l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
749
0
      l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
750
      /* take the minimum size for l_dx for each comp and resolution*/
751
0
      *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dx_min, (OPJ_INT32)l_dx);
752
0
      *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dy_min, (OPJ_INT32)l_dy);
753
754
      /* various calculations of extents*/
755
0
      l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
756
0
      l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
757
0
      l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
758
0
      l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
759
0
      l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
760
0
      l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
761
0
      l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
762
0
      py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
763
0
      l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
764
0
      l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
765
0
      *lResolutionPtr++ = l_pw;
766
0
      *lResolutionPtr++ = l_ph;
767
0
      l_product = l_pw * l_ph;
768
      
769
            /* update precision*/
770
0
      if (l_product > *p_max_prec) {
771
0
        *p_max_prec = l_product;
772
0
      }
773
774
0
      --l_level_no;
775
0
    }
776
0
    ++l_tccp;
777
0
    ++l_img_comp;
778
0
  }
779
0
}
780
781
opj_pi_iterator_t * opj_pi_create(  const opj_image_t *image,
782
                                    const opj_cp_t *cp,
783
                                    OPJ_UINT32 tileno )
784
0
{
785
  /* loop*/
786
0
  OPJ_UINT32 pino, compno;
787
  /* number of poc in the p_pi*/
788
0
  OPJ_UINT32 l_poc_bound;
789
790
  /* pointers to tile coding parameters and components.*/
791
0
  opj_pi_iterator_t *l_pi = 00;
792
0
  opj_tcp_t *tcp = 00;
793
0
  const opj_tccp_t *tccp = 00;
794
795
  /* current packet iterator being allocated*/
796
0
  opj_pi_iterator_t *l_current_pi = 00;
797
798
  /* preconditions in debug*/
799
0
  assert(cp != 00);
800
0
  assert(image != 00);
801
0
  assert(tileno < cp->tw * cp->th);
802
803
  /* initializations*/
804
0
  tcp = &cp->tcps[tileno];
805
0
  l_poc_bound = tcp->numpocs+1;
806
807
  /* memory allocations*/
808
0
  l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
809
0
  if (!l_pi) {
810
0
    return NULL;
811
0
  }
812
0
  memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
813
814
0
  l_current_pi = l_pi;
815
0
  for (pino = 0; pino < l_poc_bound ; ++pino) {
816
817
0
    l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
818
0
    if (! l_current_pi->comps) {
819
0
      opj_pi_destroy(l_pi, l_poc_bound);
820
0
      return NULL;
821
0
    }
822
823
0
    l_current_pi->numcomps = image->numcomps;
824
0
    memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
825
826
0
    for (compno = 0; compno < image->numcomps; ++compno) {
827
0
      opj_pi_comp_t *comp = &l_current_pi->comps[compno];
828
829
0
      tccp = &tcp->tccps[compno];
830
831
0
      comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
832
0
      if (!comp->resolutions) {
833
0
        opj_pi_destroy(l_pi, l_poc_bound);
834
0
        return 00;
835
0
      }
836
837
0
      comp->numresolutions = tccp->numresolutions;
838
0
      memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
839
0
    }
840
0
    ++l_current_pi;
841
0
  }
842
0
  return l_pi;
843
0
}
844
845
void opj_pi_update_encode_poc_and_final (   opj_cp_t *p_cp,
846
                                            OPJ_UINT32 p_tileno,
847
                                            OPJ_INT32 p_tx0,
848
                                            OPJ_INT32 p_tx1,
849
                                            OPJ_INT32 p_ty0,
850
                                            OPJ_INT32 p_ty1,
851
                                            OPJ_UINT32 p_max_prec,
852
                                            OPJ_UINT32 p_max_res,
853
                                            OPJ_UINT32 p_dx_min,
854
                                            OPJ_UINT32 p_dy_min)
855
0
{
856
  /* loop*/
857
0
  OPJ_UINT32 pino;
858
  /* tile coding parameter*/
859
0
  opj_tcp_t *l_tcp = 00;
860
  /* current poc being updated*/
861
0
  opj_poc_t * l_current_poc = 00;
862
863
  /* number of pocs*/
864
0
  OPJ_UINT32 l_poc_bound;
865
866
0
    OPJ_ARG_NOT_USED(p_max_res);
867
868
  /* preconditions in debug*/
869
0
  assert(p_cp != 00);
870
0
  assert(p_tileno < p_cp->tw * p_cp->th);
871
872
  /* initializations*/
873
0
  l_tcp = &p_cp->tcps [p_tileno];
874
  /* number of iterations in the loop */
875
0
  l_poc_bound = l_tcp->numpocs+1;
876
877
  /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
878
     store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
879
0
  l_current_poc = l_tcp->pocs;
880
881
0
  l_current_poc->compS = l_current_poc->compno0;
882
0
  l_current_poc->compE = l_current_poc->compno1;
883
0
  l_current_poc->resS = l_current_poc->resno0;
884
0
  l_current_poc->resE = l_current_poc->resno1;
885
0
  l_current_poc->layE = l_current_poc->layno1;
886
887
  /* special treatment for the first element*/
888
0
  l_current_poc->layS = 0;
889
0
  l_current_poc->prg  = l_current_poc->prg1;
890
0
  l_current_poc->prcS = 0;
891
892
0
  l_current_poc->prcE = p_max_prec;
893
0
  l_current_poc->txS = (OPJ_UINT32)p_tx0;
894
0
  l_current_poc->txE = (OPJ_UINT32)p_tx1;
895
0
  l_current_poc->tyS = (OPJ_UINT32)p_ty0;
896
0
  l_current_poc->tyE = (OPJ_UINT32)p_ty1;
897
0
  l_current_poc->dx = p_dx_min;
898
0
  l_current_poc->dy = p_dy_min;
899
900
0
  ++ l_current_poc;
901
0
  for (pino = 1;pino < l_poc_bound ; ++pino) {
902
0
    l_current_poc->compS = l_current_poc->compno0;
903
0
    l_current_poc->compE= l_current_poc->compno1;
904
0
    l_current_poc->resS = l_current_poc->resno0;
905
0
    l_current_poc->resE = l_current_poc->resno1;
906
0
    l_current_poc->layE = l_current_poc->layno1;
907
0
    l_current_poc->prg  = l_current_poc->prg1;
908
0
    l_current_poc->prcS = 0;
909
    /* special treatment here different from the first element*/
910
0
    l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
911
912
0
    l_current_poc->prcE = p_max_prec;
913
0
    l_current_poc->txS = (OPJ_UINT32)p_tx0;
914
0
    l_current_poc->txE = (OPJ_UINT32)p_tx1;
915
0
    l_current_poc->tyS = (OPJ_UINT32)p_ty0;
916
0
    l_current_poc->tyE = (OPJ_UINT32)p_ty1;
917
0
    l_current_poc->dx = p_dx_min;
918
0
    l_current_poc->dy = p_dy_min;
919
0
    ++ l_current_poc;
920
0
  }
921
0
}
922
923
void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
924
                                    OPJ_UINT32 p_num_comps,
925
                                    OPJ_UINT32 p_tileno,
926
                                    OPJ_INT32 p_tx0,
927
                                    OPJ_INT32 p_tx1,
928
                                    OPJ_INT32 p_ty0,
929
                                    OPJ_INT32 p_ty1,
930
                                    OPJ_UINT32 p_max_prec,
931
                                    OPJ_UINT32 p_max_res,
932
                                    OPJ_UINT32 p_dx_min,
933
                                    OPJ_UINT32 p_dy_min)
934
0
{
935
  /* loop*/
936
0
  OPJ_UINT32 pino;
937
  /* tile coding parameter*/
938
0
  opj_tcp_t *l_tcp = 00;
939
  /* current poc being updated*/
940
0
  opj_poc_t * l_current_poc = 00;
941
  /* number of pocs*/
942
0
  OPJ_UINT32 l_poc_bound;
943
944
  /* preconditions in debug*/
945
0
  assert(p_cp != 00);
946
0
  assert(p_tileno < p_cp->tw * p_cp->th);
947
948
  /* initializations*/
949
0
  l_tcp = &p_cp->tcps [p_tileno];
950
951
  /* number of iterations in the loop */
952
0
  l_poc_bound = l_tcp->numpocs+1;
953
954
  /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
955
     store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
956
0
  l_current_poc = l_tcp->pocs;
957
958
0
  for (pino = 0; pino < l_poc_bound ; ++pino) {
959
0
    l_current_poc->compS = 0;
960
0
    l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
961
0
    l_current_poc->resS = 0;
962
0
    l_current_poc->resE = p_max_res;
963
0
    l_current_poc->layS = 0;
964
0
    l_current_poc->layE = l_tcp->numlayers;
965
0
    l_current_poc->prg  = l_tcp->prg;
966
0
    l_current_poc->prcS = 0;
967
0
    l_current_poc->prcE = p_max_prec;
968
0
    l_current_poc->txS = (OPJ_UINT32)p_tx0;
969
0
    l_current_poc->txE = (OPJ_UINT32)p_tx1;
970
0
    l_current_poc->tyS = (OPJ_UINT32)p_ty0;
971
0
    l_current_poc->tyE = (OPJ_UINT32)p_ty1;
972
0
    l_current_poc->dx = p_dx_min;
973
0
    l_current_poc->dy = p_dy_min;
974
0
    ++ l_current_poc;
975
0
  }
976
0
}
977
978
void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
979
                               opj_tcp_t * p_tcp,
980
                               OPJ_UINT32 p_max_precision,
981
                               OPJ_UINT32 p_max_res)
982
0
{
983
  /* loop*/
984
0
  OPJ_UINT32 pino;
985
986
  /* encoding prameters to set*/
987
0
  OPJ_UINT32 l_bound;
988
989
0
  opj_pi_iterator_t * l_current_pi = 00;
990
0
  opj_poc_t* l_current_poc = 0;
991
992
0
    OPJ_ARG_NOT_USED(p_max_res);
993
994
  /* preconditions in debug*/
995
0
  assert(p_pi != 00);
996
0
  assert(p_tcp != 00);
997
998
  /* initializations*/
999
0
  l_bound = p_tcp->numpocs+1;
1000
0
  l_current_pi = p_pi;
1001
0
  l_current_poc = p_tcp->pocs;
1002
1003
0
  for (pino = 0;pino<l_bound;++pino) {
1004
0
    l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1005
0
    l_current_pi->first = 1;
1006
1007
0
    l_current_pi->poc.resno0 = l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1008
0
    l_current_pi->poc.compno0 = l_current_poc->compno0; /* Component Index #0 (Start) */
1009
0
    l_current_pi->poc.layno0 = 0;
1010
0
    l_current_pi->poc.precno0 = 0;
1011
0
    l_current_pi->poc.resno1 = l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1012
0
    l_current_pi->poc.compno1 = l_current_poc->compno1; /* Component Index #0 (End) */
1013
0
    l_current_pi->poc.layno1 = l_current_poc->layno1; /* Layer Index #0 (End) */
1014
0
    l_current_pi->poc.precno1 = p_max_precision;
1015
0
    ++l_current_pi;
1016
0
    ++l_current_poc;
1017
0
  }
1018
0
}
1019
1020
void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
1021
                                   opj_tcp_t * p_tcp,
1022
                                   OPJ_UINT32 p_max_precision,
1023
                                   OPJ_UINT32 p_max_res)
1024
0
{
1025
  /* loop*/
1026
0
  OPJ_UINT32 pino;
1027
1028
  /* encoding prameters to set*/
1029
0
  OPJ_UINT32 l_bound;
1030
1031
0
  opj_pi_iterator_t * l_current_pi = 00;
1032
  /* preconditions in debug*/
1033
0
  assert(p_tcp != 00);
1034
0
  assert(p_pi != 00);
1035
1036
  /* initializations*/
1037
0
  l_bound = p_tcp->numpocs+1;
1038
0
  l_current_pi = p_pi;
1039
1040
0
  for (pino = 0;pino<l_bound;++pino) {
1041
0
    l_current_pi->poc.prg = p_tcp->prg;
1042
0
    l_current_pi->first = 1;
1043
0
    l_current_pi->poc.resno0 = 0;
1044
0
    l_current_pi->poc.compno0 = 0;
1045
0
    l_current_pi->poc.layno0 = 0;
1046
0
    l_current_pi->poc.precno0 = 0;
1047
0
    l_current_pi->poc.resno1 = p_max_res;
1048
0
    l_current_pi->poc.compno1 = l_current_pi->numcomps;
1049
0
    l_current_pi->poc.layno1 = p_tcp->numlayers;
1050
0
    l_current_pi->poc.precno1 = p_max_precision;
1051
0
    ++l_current_pi;
1052
0
  }
1053
0
}
1054
1055
1056
1057
OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos,
1058
                opj_cp_t *cp,
1059
                OPJ_UINT32 tileno,
1060
                OPJ_UINT32 pino,
1061
                const OPJ_CHAR *prog)
1062
0
{
1063
0
  OPJ_INT32 i;
1064
0
  opj_tcp_t *tcps =&cp->tcps[tileno];
1065
0
  opj_poc_t *tcp = &tcps->pocs[pino];
1066
1067
0
  if(pos>=0){
1068
0
    for(i=pos;pos>=0;i--){
1069
0
      switch(prog[i]){
1070
0
        case 'R':
1071
0
          if(tcp->res_t==tcp->resE){
1072
0
            if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1073
0
              return OPJ_TRUE;
1074
0
            }else{
1075
0
              return OPJ_FALSE;
1076
0
            }
1077
0
          }else{
1078
0
            return OPJ_TRUE;
1079
0
          }
1080
0
          break;
1081
0
        case 'C':
1082
0
          if(tcp->comp_t==tcp->compE){
1083
0
            if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1084
0
              return OPJ_TRUE;
1085
0
            }else{
1086
0
              return OPJ_FALSE;
1087
0
            }
1088
0
          }else{
1089
0
            return OPJ_TRUE;
1090
0
          }
1091
0
          break;
1092
0
        case 'L':
1093
0
          if(tcp->lay_t==tcp->layE){
1094
0
            if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1095
0
              return OPJ_TRUE;
1096
0
            }else{
1097
0
              return OPJ_FALSE;
1098
0
            }
1099
0
          }else{
1100
0
            return OPJ_TRUE;
1101
0
          }
1102
0
          break;
1103
0
        case 'P':
1104
0
          switch(tcp->prg){
1105
0
            case OPJ_LRCP||OPJ_RLCP:
1106
0
              if(tcp->prc_t == tcp->prcE){
1107
0
                if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1108
0
                  return OPJ_TRUE;
1109
0
                }else{
1110
0
                  return OPJ_FALSE;
1111
0
                }
1112
0
              }else{
1113
0
                return OPJ_TRUE;
1114
0
              }
1115
0
              break;
1116
0
          default:
1117
0
            if(tcp->tx0_t == tcp->txE){
1118
              /*TY*/
1119
0
              if(tcp->ty0_t == tcp->tyE){
1120
0
                if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1121
0
                  return OPJ_TRUE;
1122
0
                }else{
1123
0
                  return OPJ_FALSE;
1124
0
                }
1125
0
              }else{
1126
0
                return OPJ_TRUE;
1127
0
              }/*TY*/
1128
0
            }else{
1129
0
              return OPJ_TRUE;
1130
0
            }
1131
0
            break;
1132
0
          }/*end case P*/
1133
0
        }/*end switch*/
1134
0
    }/*end for*/
1135
0
  }/*end if*/
1136
0
  return OPJ_FALSE;
1137
0
}
1138
1139
1140
/*
1141
==========================================================
1142
   Packet iterator interface
1143
==========================================================
1144
*/
1145
opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1146
                    opj_cp_t *p_cp,
1147
                    OPJ_UINT32 p_tile_no)
1148
0
{
1149
  /* loop */
1150
0
  OPJ_UINT32 pino;
1151
0
  OPJ_UINT32 compno, resno;
1152
1153
  /* to store w, h, dx and dy fro all components and resolutions */
1154
0
  OPJ_UINT32 * l_tmp_data;
1155
0
  OPJ_UINT32 ** l_tmp_ptr;
1156
1157
  /* encoding prameters to set */
1158
0
  OPJ_UINT32 l_max_res;
1159
0
  OPJ_UINT32 l_max_prec;
1160
0
  OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1161
0
  OPJ_UINT32 l_dx_min,l_dy_min;
1162
0
  OPJ_UINT32 l_bound;
1163
0
  OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1164
0
  OPJ_UINT32 l_data_stride;
1165
1166
  /* pointers */
1167
0
  opj_pi_iterator_t *l_pi = 00;
1168
0
  opj_tcp_t *l_tcp = 00;
1169
0
  const opj_tccp_t *l_tccp = 00;
1170
0
  opj_pi_comp_t *l_current_comp = 00;
1171
0
  opj_image_comp_t * l_img_comp = 00;
1172
0
  opj_pi_iterator_t * l_current_pi = 00;
1173
0
  OPJ_UINT32 * l_encoding_value_ptr = 00;
1174
1175
  /* preconditions in debug */
1176
0
  assert(p_cp != 00);
1177
0
  assert(p_image != 00);
1178
0
  assert(p_tile_no < p_cp->tw * p_cp->th);
1179
1180
  /* initializations */
1181
0
  l_tcp = &p_cp->tcps[p_tile_no];
1182
0
  l_bound = l_tcp->numpocs+1;
1183
1184
0
  l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1185
0
  l_tmp_data = (OPJ_UINT32*)opj_malloc(
1186
0
    l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1187
0
  if
1188
0
    (! l_tmp_data)
1189
0
  {
1190
0
    return 00;
1191
0
  }
1192
0
  l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1193
0
    p_image->numcomps * sizeof(OPJ_UINT32 *));
1194
0
  if
1195
0
    (! l_tmp_ptr)
1196
0
  {
1197
0
    opj_free(l_tmp_data);
1198
0
    return 00;
1199
0
  }
1200
1201
  /* memory allocation for pi */
1202
0
  l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1203
0
  if (!l_pi) {
1204
0
    opj_free(l_tmp_data);
1205
0
    opj_free(l_tmp_ptr);
1206
0
    return 00;
1207
0
  }
1208
1209
0
  l_encoding_value_ptr = l_tmp_data;
1210
  /* update pointer array */
1211
0
  for
1212
0
    (compno = 0; compno < p_image->numcomps; ++compno)
1213
0
  {
1214
0
    l_tmp_ptr[compno] = l_encoding_value_ptr;
1215
0
    l_encoding_value_ptr += l_data_stride;
1216
0
  }
1217
  /* get encoding parameters */
1218
0
  opj_get_all_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res,l_tmp_ptr);
1219
1220
  /* step calculations */
1221
0
  l_step_p = 1;
1222
0
  l_step_c = l_max_prec * l_step_p;
1223
0
  l_step_r = p_image->numcomps * l_step_c;
1224
0
  l_step_l = l_max_res * l_step_r;
1225
1226
  /* set values for first packet iterator */
1227
0
  l_current_pi = l_pi;
1228
1229
  /* memory allocation for include */
1230
0
  l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
1231
0
  if
1232
0
    (!l_current_pi->include)
1233
0
  {
1234
0
    opj_free(l_tmp_data);
1235
0
    opj_free(l_tmp_ptr);
1236
0
    opj_pi_destroy(l_pi, l_bound);
1237
0
    return 00;
1238
0
  }
1239
0
  memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
1240
1241
  /* special treatment for the first packet iterator */
1242
0
  l_current_comp = l_current_pi->comps;
1243
0
  l_img_comp = p_image->comps;
1244
0
  l_tccp = l_tcp->tccps;
1245
1246
0
  l_current_pi->tx0 = l_tx0;
1247
0
  l_current_pi->ty0 = l_ty0;
1248
0
  l_current_pi->tx1 = l_tx1;
1249
0
  l_current_pi->ty1 = l_ty1;
1250
1251
  /*l_current_pi->dx = l_img_comp->dx;*/
1252
  /*l_current_pi->dy = l_img_comp->dy;*/
1253
1254
0
  l_current_pi->step_p = l_step_p;
1255
0
  l_current_pi->step_c = l_step_c;
1256
0
  l_current_pi->step_r = l_step_r;
1257
0
  l_current_pi->step_l = l_step_l;
1258
1259
  /* allocation for components and number of components has already been calculated by opj_pi_create */
1260
0
  for
1261
0
    (compno = 0; compno < l_current_pi->numcomps; ++compno)
1262
0
  {
1263
0
    opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1264
0
    l_encoding_value_ptr = l_tmp_ptr[compno];
1265
1266
0
    l_current_comp->dx = l_img_comp->dx;
1267
0
    l_current_comp->dy = l_img_comp->dy;
1268
    /* resolutions have already been initialized */
1269
0
    for
1270
0
      (resno = 0; resno < l_current_comp->numresolutions; resno++)
1271
0
    {
1272
0
      l_res->pdx = *(l_encoding_value_ptr++);
1273
0
      l_res->pdy = *(l_encoding_value_ptr++);
1274
0
      l_res->pw =  *(l_encoding_value_ptr++);
1275
0
      l_res->ph =  *(l_encoding_value_ptr++);
1276
0
      ++l_res;
1277
0
    }
1278
0
    ++l_current_comp;
1279
0
    ++l_img_comp;
1280
0
    ++l_tccp;
1281
0
  }
1282
0
  ++l_current_pi;
1283
1284
0
  for (pino = 1 ; pino<l_bound ; ++pino )
1285
0
  {
1286
0
    l_current_comp = l_current_pi->comps;
1287
0
    l_img_comp = p_image->comps;
1288
0
    l_tccp = l_tcp->tccps;
1289
1290
0
    l_current_pi->tx0 = l_tx0;
1291
0
    l_current_pi->ty0 = l_ty0;
1292
0
    l_current_pi->tx1 = l_tx1;
1293
0
    l_current_pi->ty1 = l_ty1;
1294
    /*l_current_pi->dx = l_dx_min;*/
1295
    /*l_current_pi->dy = l_dy_min;*/
1296
0
    l_current_pi->step_p = l_step_p;
1297
0
    l_current_pi->step_c = l_step_c;
1298
0
    l_current_pi->step_r = l_step_r;
1299
0
    l_current_pi->step_l = l_step_l;
1300
1301
    /* allocation for components and number of components has already been calculated by opj_pi_create */
1302
0
    for
1303
0
      (compno = 0; compno < l_current_pi->numcomps; ++compno)
1304
0
    {
1305
0
      opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1306
0
      l_encoding_value_ptr = l_tmp_ptr[compno];
1307
1308
0
      l_current_comp->dx = l_img_comp->dx;
1309
0
      l_current_comp->dy = l_img_comp->dy;
1310
      /* resolutions have already been initialized */
1311
0
      for
1312
0
        (resno = 0; resno < l_current_comp->numresolutions; resno++)
1313
0
      {
1314
0
        l_res->pdx = *(l_encoding_value_ptr++);
1315
0
        l_res->pdy = *(l_encoding_value_ptr++);
1316
0
        l_res->pw =  *(l_encoding_value_ptr++);
1317
0
        l_res->ph =  *(l_encoding_value_ptr++);
1318
0
        ++l_res;
1319
0
      }
1320
0
      ++l_current_comp;
1321
0
      ++l_img_comp;
1322
0
      ++l_tccp;
1323
0
    }
1324
    /* special treatment*/
1325
0
    l_current_pi->include = (l_current_pi-1)->include;
1326
0
    ++l_current_pi;
1327
0
  }
1328
0
  opj_free(l_tmp_data);
1329
0
  l_tmp_data = 00;
1330
0
  opj_free(l_tmp_ptr);
1331
0
  l_tmp_ptr = 00;
1332
0
  if
1333
0
    (l_tcp->POC)
1334
0
  {
1335
0
    opj_pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
1336
0
  }
1337
0
  else
1338
0
  {
1339
0
    opj_pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
1340
0
  }
1341
0
  return l_pi;
1342
0
}
1343
1344
1345
1346
opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1347
                                            opj_cp_t *p_cp,
1348
                                            OPJ_UINT32 p_tile_no,
1349
                                            J2K_T2_MODE p_t2_mode )
1350
0
{
1351
  /* loop*/
1352
0
  OPJ_UINT32 pino;
1353
0
  OPJ_UINT32 compno, resno;
1354
1355
  /* to store w, h, dx and dy fro all components and resolutions*/
1356
0
  OPJ_UINT32 * l_tmp_data;
1357
0
  OPJ_UINT32 ** l_tmp_ptr;
1358
1359
  /* encoding prameters to set*/
1360
0
  OPJ_UINT32 l_max_res;
1361
0
  OPJ_UINT32 l_max_prec;
1362
0
  OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1363
0
  OPJ_UINT32 l_dx_min,l_dy_min;
1364
0
  OPJ_UINT32 l_bound;
1365
0
  OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1366
0
  OPJ_UINT32 l_data_stride;
1367
1368
  /* pointers*/
1369
0
  opj_pi_iterator_t *l_pi = 00;
1370
0
  opj_tcp_t *l_tcp = 00;
1371
0
  const opj_tccp_t *l_tccp = 00;
1372
0
  opj_pi_comp_t *l_current_comp = 00;
1373
0
  opj_image_comp_t * l_img_comp = 00;
1374
0
  opj_pi_iterator_t * l_current_pi = 00;
1375
0
  OPJ_UINT32 * l_encoding_value_ptr = 00;
1376
1377
  /* preconditions in debug*/
1378
0
  assert(p_cp != 00);
1379
0
  assert(p_image != 00);
1380
0
  assert(p_tile_no < p_cp->tw * p_cp->th);
1381
1382
  /* initializations*/
1383
0
  l_tcp = &p_cp->tcps[p_tile_no];
1384
0
  l_bound = l_tcp->numpocs+1;
1385
1386
0
  l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1387
0
  l_tmp_data = (OPJ_UINT32*)opj_malloc(
1388
0
    l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1389
0
  if (! l_tmp_data) {
1390
0
    return 00;
1391
0
  }
1392
1393
0
  l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1394
0
    p_image->numcomps * sizeof(OPJ_UINT32 *));
1395
0
  if (! l_tmp_ptr) {
1396
0
    opj_free(l_tmp_data);
1397
0
    return 00;
1398
0
  }
1399
1400
  /* memory allocation for pi*/
1401
0
  l_pi = opj_pi_create(p_image,p_cp,p_tile_no);
1402
0
  if (!l_pi) {
1403
0
    opj_free(l_tmp_data);
1404
0
    opj_free(l_tmp_ptr);
1405
0
    return 00;
1406
0
  }
1407
1408
0
  l_encoding_value_ptr = l_tmp_data;
1409
  /* update pointer array*/
1410
0
  for (compno = 0; compno < p_image->numcomps; ++compno) {
1411
0
    l_tmp_ptr[compno] = l_encoding_value_ptr;
1412
0
    l_encoding_value_ptr += l_data_stride;
1413
0
  }
1414
1415
  /* get encoding parameters*/
1416
0
  opj_get_all_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res,l_tmp_ptr);
1417
1418
  /* step calculations*/
1419
0
  l_step_p = 1;
1420
0
  l_step_c = l_max_prec * l_step_p;
1421
0
  l_step_r = p_image->numcomps * l_step_c;
1422
0
  l_step_l = l_max_res * l_step_r;
1423
1424
  /* set values for first packet iterator*/
1425
0
  l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
1426
0
  l_current_pi = l_pi;
1427
1428
  /* memory allocation for include*/
1429
0
  l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
1430
0
  if (!l_current_pi->include) {
1431
0
    opj_free(l_tmp_data);
1432
0
    opj_free(l_tmp_ptr);
1433
0
    opj_pi_destroy(l_pi, l_bound);
1434
0
    return 00;
1435
0
  }
1436
0
  memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
1437
1438
  /* special treatment for the first packet iterator*/
1439
0
  l_current_comp = l_current_pi->comps;
1440
0
  l_img_comp = p_image->comps;
1441
0
  l_tccp = l_tcp->tccps;
1442
0
  l_current_pi->tx0 = l_tx0;
1443
0
  l_current_pi->ty0 = l_ty0;
1444
0
  l_current_pi->tx1 = l_tx1;
1445
0
  l_current_pi->ty1 = l_ty1;
1446
0
  l_current_pi->dx = l_dx_min;
1447
0
  l_current_pi->dy = l_dy_min;
1448
0
  l_current_pi->step_p = l_step_p;
1449
0
  l_current_pi->step_c = l_step_c;
1450
0
  l_current_pi->step_r = l_step_r;
1451
0
  l_current_pi->step_l = l_step_l;
1452
1453
  /* allocation for components and number of components has already been calculated by opj_pi_create */
1454
0
  for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1455
0
    opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1456
0
    l_encoding_value_ptr = l_tmp_ptr[compno];
1457
1458
0
    l_current_comp->dx = l_img_comp->dx;
1459
0
    l_current_comp->dy = l_img_comp->dy;
1460
1461
    /* resolutions have already been initialized */
1462
0
    for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1463
0
      l_res->pdx = *(l_encoding_value_ptr++);
1464
0
      l_res->pdy = *(l_encoding_value_ptr++);
1465
0
      l_res->pw =  *(l_encoding_value_ptr++);
1466
0
      l_res->ph =  *(l_encoding_value_ptr++);
1467
0
      ++l_res;
1468
0
    }
1469
1470
0
    ++l_current_comp;
1471
0
    ++l_img_comp;
1472
0
    ++l_tccp;
1473
0
  }
1474
0
  ++l_current_pi;
1475
1476
0
  for (pino = 1 ; pino<l_bound ; ++pino ) {
1477
0
    l_current_comp = l_current_pi->comps;
1478
0
    l_img_comp = p_image->comps;
1479
0
    l_tccp = l_tcp->tccps;
1480
1481
0
    l_current_pi->tx0 = l_tx0;
1482
0
    l_current_pi->ty0 = l_ty0;
1483
0
    l_current_pi->tx1 = l_tx1;
1484
0
    l_current_pi->ty1 = l_ty1;
1485
0
    l_current_pi->dx = l_dx_min;
1486
0
    l_current_pi->dy = l_dy_min;
1487
0
    l_current_pi->step_p = l_step_p;
1488
0
    l_current_pi->step_c = l_step_c;
1489
0
    l_current_pi->step_r = l_step_r;
1490
0
    l_current_pi->step_l = l_step_l;
1491
1492
    /* allocation for components and number of components has already been calculated by opj_pi_create */
1493
0
    for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1494
0
      opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1495
0
      l_encoding_value_ptr = l_tmp_ptr[compno];
1496
1497
0
      l_current_comp->dx = l_img_comp->dx;
1498
0
      l_current_comp->dy = l_img_comp->dy;
1499
      /* resolutions have already been initialized */
1500
0
      for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1501
0
        l_res->pdx = *(l_encoding_value_ptr++);
1502
0
        l_res->pdy = *(l_encoding_value_ptr++);
1503
0
        l_res->pw =  *(l_encoding_value_ptr++);
1504
0
        l_res->ph =  *(l_encoding_value_ptr++);
1505
0
        ++l_res;
1506
0
      }
1507
0
      ++l_current_comp;
1508
0
      ++l_img_comp;
1509
0
      ++l_tccp;
1510
0
    }
1511
1512
    /* special treatment*/
1513
0
    l_current_pi->include = (l_current_pi-1)->include;
1514
0
    ++l_current_pi;
1515
0
  }
1516
1517
0
  opj_free(l_tmp_data);
1518
0
  l_tmp_data = 00;
1519
0
  opj_free(l_tmp_ptr);
1520
0
  l_tmp_ptr = 00;
1521
1522
0
  if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
1523
0
    opj_pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1524
0
  }
1525
0
  else {
1526
0
    opj_pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1527
0
  }
1528
1529
0
  return l_pi;
1530
0
}
1531
1532
void opj_pi_create_encode(  opj_pi_iterator_t *pi,
1533
              opj_cp_t *cp,
1534
              OPJ_UINT32 tileno,
1535
              OPJ_UINT32 pino,
1536
              OPJ_UINT32 tpnum,
1537
              OPJ_INT32 tppos,
1538
              J2K_T2_MODE t2_mode)
1539
0
{
1540
0
  const OPJ_CHAR *prog;
1541
0
  OPJ_INT32 i;
1542
0
  OPJ_UINT32 incr_top=1,resetX=0;
1543
0
  opj_tcp_t *tcps =&cp->tcps[tileno];
1544
0
  opj_poc_t *tcp= &tcps->pocs[pino];
1545
1546
0
  prog = opj_j2k_convert_progression_order(tcp->prg);
1547
1548
0
  pi[pino].first = 1;
1549
0
  pi[pino].poc.prg = tcp->prg;
1550
1551
0
  if(!(cp->m_specific_param.m_enc.m_tp_on && ((!cp->m_specific_param.m_enc.m_cinema && (t2_mode == FINAL_PASS)) || cp->m_specific_param.m_enc.m_cinema))){
1552
0
    pi[pino].poc.resno0 = tcp->resS;
1553
0
    pi[pino].poc.resno1 = tcp->resE;
1554
0
    pi[pino].poc.compno0 = tcp->compS;
1555
0
    pi[pino].poc.compno1 = tcp->compE;
1556
0
    pi[pino].poc.layno0 = tcp->layS;
1557
0
    pi[pino].poc.layno1 = tcp->layE;
1558
0
    pi[pino].poc.precno0 = tcp->prcS;
1559
0
    pi[pino].poc.precno1 = tcp->prcE;
1560
0
    pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1561
0
    pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1562
0
    pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1563
0
    pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1564
0
  }else {
1565
0
    for(i=tppos+1;i<4;i++){
1566
0
      switch(prog[i]){
1567
0
      case 'R':
1568
0
        pi[pino].poc.resno0 = tcp->resS;
1569
0
        pi[pino].poc.resno1 = tcp->resE;
1570
0
        break;
1571
0
      case 'C':
1572
0
        pi[pino].poc.compno0 = tcp->compS;
1573
0
        pi[pino].poc.compno1 = tcp->compE;
1574
0
        break;
1575
0
      case 'L':
1576
0
        pi[pino].poc.layno0 = tcp->layS;
1577
0
        pi[pino].poc.layno1 = tcp->layE;
1578
0
        break;
1579
0
      case 'P':
1580
0
        switch(tcp->prg){
1581
0
        case OPJ_LRCP:
1582
0
        case OPJ_RLCP:
1583
0
          pi[pino].poc.precno0 = tcp->prcS;
1584
0
          pi[pino].poc.precno1 = tcp->prcE;
1585
0
          break;
1586
0
        default:
1587
0
          pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1588
0
          pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1589
0
          pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1590
0
          pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1591
0
          break;
1592
0
        }
1593
0
        break;
1594
0
      }
1595
0
    }
1596
1597
0
    if(tpnum==0){
1598
0
      for(i=tppos;i>=0;i--){
1599
0
        switch(prog[i]){
1600
0
        case 'C':
1601
0
          tcp->comp_t = tcp->compS;
1602
0
          pi[pino].poc.compno0 = tcp->comp_t;
1603
0
          pi[pino].poc.compno1 = tcp->comp_t+1;
1604
0
          tcp->comp_t+=1;
1605
0
          break;
1606
0
        case 'R':
1607
0
          tcp->res_t = tcp->resS;
1608
0
          pi[pino].poc.resno0 = tcp->res_t;
1609
0
          pi[pino].poc.resno1 = tcp->res_t+1;
1610
0
          tcp->res_t+=1;
1611
0
          break;
1612
0
        case 'L':
1613
0
          tcp->lay_t = tcp->layS;
1614
0
          pi[pino].poc.layno0 = tcp->lay_t;
1615
0
          pi[pino].poc.layno1 = tcp->lay_t+1;
1616
0
          tcp->lay_t+=1;
1617
0
          break;
1618
0
        case 'P':
1619
0
          switch(tcp->prg){
1620
0
          case OPJ_LRCP:
1621
0
          case OPJ_RLCP:
1622
0
            tcp->prc_t = tcp->prcS;
1623
0
            pi[pino].poc.precno0 = tcp->prc_t;
1624
0
            pi[pino].poc.precno1 = tcp->prc_t+1;
1625
0
            tcp->prc_t+=1;
1626
0
            break;
1627
0
          default:
1628
0
            tcp->tx0_t = tcp->txS;
1629
0
            tcp->ty0_t = tcp->tyS;
1630
0
            pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1631
0
            pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1632
0
            pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1633
0
            pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1634
0
            tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1635
0
            tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1636
0
            break;
1637
0
          }
1638
0
          break;
1639
0
        }
1640
0
      }
1641
0
      incr_top=1;
1642
0
    }else{
1643
0
      for(i=tppos;i>=0;i--){
1644
0
        switch(prog[i]){
1645
0
        case 'C':
1646
0
          pi[pino].poc.compno0 = tcp->comp_t-1;
1647
0
          pi[pino].poc.compno1 = tcp->comp_t;
1648
0
          break;
1649
0
        case 'R':
1650
0
          pi[pino].poc.resno0 = tcp->res_t-1;
1651
0
          pi[pino].poc.resno1 = tcp->res_t;
1652
0
          break;
1653
0
        case 'L':
1654
0
          pi[pino].poc.layno0 = tcp->lay_t-1;
1655
0
          pi[pino].poc.layno1 = tcp->lay_t;
1656
0
          break;
1657
0
        case 'P':
1658
0
          switch(tcp->prg){
1659
0
          case OPJ_LRCP:
1660
0
          case OPJ_RLCP:
1661
0
            pi[pino].poc.precno0 = tcp->prc_t-1;
1662
0
            pi[pino].poc.precno1 = tcp->prc_t;
1663
0
            break;
1664
0
          default:
1665
0
            pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
1666
0
            pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
1667
0
            pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
1668
0
            pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
1669
0
            break;
1670
0
          }
1671
0
          break;
1672
0
        }
1673
0
        if(incr_top==1){
1674
0
          switch(prog[i]){
1675
0
          case 'R':
1676
0
            if(tcp->res_t==tcp->resE){
1677
0
              if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1678
0
                tcp->res_t = tcp->resS;
1679
0
                pi[pino].poc.resno0 = tcp->res_t;
1680
0
                pi[pino].poc.resno1 = tcp->res_t+1;
1681
0
                tcp->res_t+=1;
1682
0
                incr_top=1;
1683
0
              }else{
1684
0
                incr_top=0;
1685
0
              }
1686
0
            }else{
1687
0
              pi[pino].poc.resno0 = tcp->res_t;
1688
0
              pi[pino].poc.resno1 = tcp->res_t+1;
1689
0
              tcp->res_t+=1;
1690
0
              incr_top=0;
1691
0
            }
1692
0
            break;
1693
0
          case 'C':
1694
0
            if(tcp->comp_t ==tcp->compE){
1695
0
              if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1696
0
                tcp->comp_t = tcp->compS;
1697
0
                pi[pino].poc.compno0 = tcp->comp_t;
1698
0
                pi[pino].poc.compno1 = tcp->comp_t+1;
1699
0
                tcp->comp_t+=1;
1700
0
                incr_top=1;
1701
0
              }else{
1702
0
                incr_top=0;
1703
0
              }
1704
0
            }else{
1705
0
              pi[pino].poc.compno0 = tcp->comp_t;
1706
0
              pi[pino].poc.compno1 = tcp->comp_t+1;
1707
0
              tcp->comp_t+=1;
1708
0
              incr_top=0;
1709
0
            }
1710
0
            break;
1711
0
          case 'L':
1712
0
            if(tcp->lay_t == tcp->layE){
1713
0
              if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1714
0
                tcp->lay_t = tcp->layS;
1715
0
                pi[pino].poc.layno0 = tcp->lay_t;
1716
0
                pi[pino].poc.layno1 = tcp->lay_t+1;
1717
0
                tcp->lay_t+=1;
1718
0
                incr_top=1;
1719
0
              }else{
1720
0
                incr_top=0;
1721
0
              }
1722
0
            }else{
1723
0
              pi[pino].poc.layno0 = tcp->lay_t;
1724
0
              pi[pino].poc.layno1 = tcp->lay_t+1;
1725
0
              tcp->lay_t+=1;
1726
0
              incr_top=0;
1727
0
            }
1728
0
            break;
1729
0
          case 'P':
1730
0
            switch(tcp->prg){
1731
0
            case OPJ_LRCP:
1732
0
            case OPJ_RLCP:
1733
0
              if(tcp->prc_t == tcp->prcE){
1734
0
                if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1735
0
                  tcp->prc_t = tcp->prcS;
1736
0
                  pi[pino].poc.precno0 = tcp->prc_t;
1737
0
                  pi[pino].poc.precno1 = tcp->prc_t+1;
1738
0
                  tcp->prc_t+=1;
1739
0
                  incr_top=1;
1740
0
                }else{
1741
0
                  incr_top=0;
1742
0
                }
1743
0
              }else{
1744
0
                pi[pino].poc.precno0 = tcp->prc_t;
1745
0
                pi[pino].poc.precno1 = tcp->prc_t+1;
1746
0
                tcp->prc_t+=1;
1747
0
                incr_top=0;
1748
0
              }
1749
0
              break;
1750
0
            default:
1751
0
              if(tcp->tx0_t >= tcp->txE){
1752
0
                if(tcp->ty0_t >= tcp->tyE){
1753
0
                  if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1754
0
                    tcp->ty0_t = tcp->tyS;
1755
0
                    pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1756
0
                    pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1757
0
                    tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1758
0
                    incr_top=1;resetX=1;
1759
0
                  }else{
1760
0
                    incr_top=0;resetX=0;
1761
0
                  }
1762
0
                }else{
1763
0
                  pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1764
0
                  pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1765
0
                  tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1766
0
                  incr_top=0;resetX=1;
1767
0
                }
1768
0
                if(resetX==1){
1769
0
                  tcp->tx0_t = tcp->txS;
1770
0
                  pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1771
0
                  pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
1772
0
                  tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1773
0
                }
1774
0
              }else{
1775
0
                pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1776
0
                pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
1777
0
                tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1778
0
                incr_top=0;
1779
0
              }
1780
0
              break;
1781
0
            }
1782
0
            break;
1783
0
          }
1784
0
        }
1785
0
      }
1786
0
    }
1787
0
  }
1788
0
}
1789
1790
void opj_pi_destroy(opj_pi_iterator_t *p_pi,
1791
                    OPJ_UINT32 p_nb_elements)
1792
0
{
1793
0
  OPJ_UINT32 compno, pino;
1794
0
  opj_pi_iterator_t *l_current_pi = p_pi;
1795
0
    if (p_pi) {
1796
0
    if (p_pi->include) {
1797
0
      opj_free(p_pi->include);
1798
0
      p_pi->include = 00;
1799
0
    }
1800
0
    for (pino = 0; pino < p_nb_elements; ++pino){
1801
0
      if(l_current_pi->comps) {
1802
0
        opj_pi_comp_t *l_current_component = l_current_pi->comps;
1803
0
                for (compno = 0; compno < l_current_pi->numcomps; compno++){
1804
0
                    if(l_current_component->resolutions) {
1805
0
            opj_free(l_current_component->resolutions);
1806
0
            l_current_component->resolutions = 00;
1807
0
          }
1808
1809
0
          ++l_current_component;
1810
0
        }
1811
0
        opj_free(l_current_pi->comps);
1812
0
        l_current_pi->comps = 0;
1813
0
      }
1814
0
      ++l_current_pi;
1815
0
    }
1816
0
    opj_free(p_pi);
1817
0
  }
1818
0
}
1819
1820
1821
1822
void opj_pi_update_encoding_parameters( const opj_image_t *p_image,
1823
                                        opj_cp_t *p_cp,
1824
                                        OPJ_UINT32 p_tile_no )
1825
0
{
1826
  /* encoding parameters to set */
1827
0
  OPJ_UINT32 l_max_res;
1828
0
  OPJ_UINT32 l_max_prec;
1829
0
  OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1830
0
  OPJ_UINT32 l_dx_min,l_dy_min;
1831
1832
  /* pointers */
1833
0
  opj_tcp_t *l_tcp = 00;
1834
1835
  /* preconditions */
1836
0
  assert(p_cp != 00);
1837
0
  assert(p_image != 00);
1838
0
  assert(p_tile_no < p_cp->tw * p_cp->th);
1839
1840
0
  l_tcp = &(p_cp->tcps[p_tile_no]);
1841
1842
  /* get encoding parameters */
1843
0
  opj_get_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res);
1844
1845
0
  if (l_tcp->POC) {
1846
0
    opj_pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1847
0
  }
1848
0
  else {
1849
0
    opj_pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1850
0
  }
1851
0
}
1852
1853
0
OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi) {
1854
0
  switch (pi->poc.prg) {
1855
0
    case OPJ_LRCP:
1856
0
      return opj_pi_next_lrcp(pi);
1857
0
    case OPJ_RLCP:
1858
0
      return opj_pi_next_rlcp(pi);
1859
0
    case OPJ_RPCL:
1860
0
      return opj_pi_next_rpcl(pi);
1861
0
    case OPJ_PCRL:
1862
0
      return opj_pi_next_pcrl(pi);
1863
0
    case OPJ_CPRL:
1864
0
      return opj_pi_next_cprl(pi);
1865
0
    case OPJ_PROG_UNKNOWN:
1866
0
      return OPJ_FALSE;
1867
0
  }
1868
  
1869
0
  return OPJ_FALSE;
1870
0
}