Enable accepting file names with `-ยด symbol .Modification getopt.c
[openjpeg.git] / libopenjpeg / pi.c
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 bool 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 bool 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 bool 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 bool 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 bool pi_next_cprl(opj_pi_iterator_t * pi);
71
72 /*@}*/
73
74 /*@}*/
75
76 /* 
77 ==========================================================
78    local functions
79 ==========================================================
80 */
81
82 static bool pi_next_lrcp(opj_pi_iterator_t * pi) {
83         opj_pi_comp_t *comp = NULL;
84         opj_pi_resolution_t *res = NULL;
85         long index = 0;
86         
87         if (!pi->first) {
88                 comp = &pi->comps[pi->compno];
89                 res = &comp->resolutions[pi->resno];
90                 goto LABEL_SKIP;
91         } else {
92                 pi->first = 0;
93         }
94
95         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
96                 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
97                 pi->resno++) {
98                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
99                                 comp = &pi->comps[pi->compno];
100                                 if (pi->resno >= comp->numresolutions) {
101                                         continue;
102                                 }
103                                 res = &comp->resolutions[pi->resno];
104                                 if (!pi->tp_on){
105                                         pi->poc.precno1 = res->pw * res->ph;
106                                 }
107                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
108                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
109                                         if (!pi->include[index]) {
110                                                 pi->include[index] = 1;
111                                                 return true;
112                                         }
113 LABEL_SKIP:;
114                                 }
115                         }
116                 }
117         }
118         
119         return false;
120 }
121
122 static bool pi_next_rlcp(opj_pi_iterator_t * pi) {
123         opj_pi_comp_t *comp = NULL;
124         opj_pi_resolution_t *res = NULL;
125         long index = 0;
126
127         if (!pi->first) {
128                 comp = &pi->comps[pi->compno];
129                 res = &comp->resolutions[pi->resno];
130                 goto LABEL_SKIP;
131         } else {
132                 pi->first = 0;
133         }
134
135         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
136                 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
137                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
138                                 comp = &pi->comps[pi->compno];
139                                 if (pi->resno >= comp->numresolutions) {
140                                         continue;
141                                 }
142                                 res = &comp->resolutions[pi->resno];
143                                 if(!pi->tp_on){
144                                         pi->poc.precno1 = res->pw * res->ph;
145                                 }
146                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
147                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
148                                         if (!pi->include[index]) {
149                                                 pi->include[index] = 1;
150                                                 return true;
151                                         }
152 LABEL_SKIP:;
153                                 }
154                         }
155                 }
156         }
157         
158         return false;
159 }
160
161 static bool pi_next_rpcl(opj_pi_iterator_t * pi) {
162         opj_pi_comp_t *comp = NULL;
163         opj_pi_resolution_t *res = NULL;
164         long index = 0;
165
166         if (!pi->first) {
167                 goto LABEL_SKIP;
168         } else {
169                 int compno, resno;
170                 pi->first = 0;
171                 pi->dx = 0;
172                 pi->dy = 0;
173                 for (compno = 0; compno < pi->numcomps; compno++) {
174                         comp = &pi->comps[compno];
175                         for (resno = 0; resno < comp->numresolutions; resno++) {
176                                 int dx, dy;
177                                 res = &comp->resolutions[resno];
178                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
179                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
180                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
181                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
182                         }
183                 }
184         }
185 if (!pi->tp_on){
186                         pi->poc.ty0 = pi->ty0;
187                         pi->poc.tx0 = pi->tx0;
188                         pi->poc.ty1 = pi->ty1;
189                         pi->poc.tx1 = pi->tx1;
190                 }
191         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
192                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
193                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
194                                 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
195                                         int levelno;
196                                         int trx0, try0;
197                                         int trx1, try1;
198                                         int rpx, rpy;
199                                         int prci, prcj;
200                                         comp = &pi->comps[pi->compno];
201                                         if (pi->resno >= comp->numresolutions) {
202                                                 continue;
203                                         }
204                                         res = &comp->resolutions[pi->resno];
205                                         levelno = comp->numresolutions - 1 - pi->resno;
206                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
207                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
208                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
209                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
210                                         rpx = res->pdx + levelno;
211                                         rpy = res->pdy + levelno;
212                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){
213                                                 continue;       
214                                         }
215                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
216                                                 continue; 
217                                         }
218                                         
219                                         if ((res->pw==0)||(res->pw==0)) continue;
220                                         
221                                         if ((trx0==trx1)||(try0==try1)) continue;
222                                         
223                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
224                                                  - int_floordivpow2(trx0, res->pdx);
225                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
226                                                  - int_floordivpow2(try0, res->pdy);
227                                         pi->precno = prci + prcj * res->pw;
228                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
229                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
230                                                 if (!pi->include[index]) {
231                                                         pi->include[index] = 1;
232                                                         return true;
233                                                 }
234 LABEL_SKIP:;
235                                         }
236                                 }
237                         }
238                 }
239         }
240         
241         return false;
242 }
243
244 static bool pi_next_pcrl(opj_pi_iterator_t * pi) {
245         opj_pi_comp_t *comp = NULL;
246         opj_pi_resolution_t *res = NULL;
247         long index = 0;
248
249         if (!pi->first) {
250                 comp = &pi->comps[pi->compno];
251                 goto LABEL_SKIP;
252         } else {
253                 int compno, resno;
254                 pi->first = 0;
255                 pi->dx = 0;
256                 pi->dy = 0;
257                 for (compno = 0; compno < pi->numcomps; compno++) {
258                         comp = &pi->comps[compno];
259                         for (resno = 0; resno < comp->numresolutions; resno++) {
260                                 int dx, dy;
261                                 res = &comp->resolutions[resno];
262                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
263                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
264                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
265                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
266                         }
267                 }
268         }
269         if (!pi->tp_on){
270                         pi->poc.ty0 = pi->ty0;
271                         pi->poc.tx0 = pi->tx0;
272                         pi->poc.ty1 = pi->ty1;
273                         pi->poc.tx1 = pi->tx1;
274                 }
275         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
276                 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
277                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
278                                 comp = &pi->comps[pi->compno];
279                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
280                                         int levelno;
281                                         int trx0, try0;
282                                         int trx1, try1;
283                                         int rpx, rpy;
284                                         int prci, prcj;
285                                         res = &comp->resolutions[pi->resno];
286                                         levelno = comp->numresolutions - 1 - pi->resno;
287                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
288                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
289                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
290                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
291                                         rpx = res->pdx + levelno;
292                                         rpy = res->pdy + levelno;
293                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){
294                                                 continue;       
295                                         }
296                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
297                                                 continue; 
298                                         }
299                                         
300                                         if ((res->pw==0)||(res->pw==0)) continue;
301                                         
302                                         if ((trx0==trx1)||(try0==try1)) continue;
303                                         
304                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
305                                                  - int_floordivpow2(trx0, res->pdx);
306                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
307                                                  - int_floordivpow2(try0, res->pdy);
308                                         pi->precno = prci + prcj * res->pw;
309                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
310                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
311                                                 if (!pi->include[index]) {
312                                                         pi->include[index] = 1;
313                                                         return true;
314                                                 }       
315 LABEL_SKIP:;
316                                         }
317                                 }
318                         }
319                 }
320         }
321         
322         return false;
323 }
324
325 static bool pi_next_cprl(opj_pi_iterator_t * pi) {
326         opj_pi_comp_t *comp = NULL;
327         opj_pi_resolution_t *res = NULL;
328         long index = 0;
329
330         if (!pi->first) {
331                 comp = &pi->comps[pi->compno];
332                 goto LABEL_SKIP;
333         } else {
334                 pi->first = 0;
335         }
336
337         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
338                 int resno;
339                 comp = &pi->comps[pi->compno];
340                 pi->dx = 0;
341                 pi->dy = 0;
342                 for (resno = 0; resno < comp->numresolutions; resno++) {
343                         int dx, dy;
344                         res = &comp->resolutions[resno];
345                         dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
346                         dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
347                         pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
348                         pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
349                 }
350                 if (!pi->tp_on){
351                         pi->poc.ty0 = pi->ty0;
352                         pi->poc.tx0 = pi->tx0;
353                         pi->poc.ty1 = pi->ty1;
354                         pi->poc.tx1 = pi->tx1;
355                 }
356                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
357                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
358                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
359                                         int levelno;
360                                         int trx0, try0;
361                                         int trx1, try1;
362                                         int rpx, rpy;
363                                         int prci, prcj;
364                                         res = &comp->resolutions[pi->resno];
365                                         levelno = comp->numresolutions - 1 - pi->resno;
366                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
367                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
368                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
369                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
370                                         rpx = res->pdx + levelno;
371                                         rpy = res->pdy + levelno;
372                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){
373                                                 continue;       
374                                         }
375                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
376                                                 continue; 
377                                         }
378                                         
379                                         if ((res->pw==0)||(res->pw==0)) continue;
380                                         
381                                         if ((trx0==trx1)||(try0==try1)) continue;
382                                         
383                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
384                                                  - int_floordivpow2(trx0, res->pdx);
385                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
386                                                  - int_floordivpow2(try0, res->pdy);
387                                         pi->precno = prci + prcj * res->pw;
388                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
389                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
390                                                 if (!pi->include[index]) {
391                                                         pi->include[index] = 1;
392                                                         return true;
393                                                 }
394 LABEL_SKIP:;
395                                         }
396                                 }
397                         }
398                 }
399         }
400         
401         return false;
402 }
403
404 /* 
405 ==========================================================
406    Packet iterator interface
407 ==========================================================
408 */
409
410 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
411         int p, q;
412         int compno, resno, pino;
413         opj_pi_iterator_t *pi = NULL;
414         opj_tcp_t *tcp = NULL;
415         opj_tccp_t *tccp = NULL;
416         size_t array_size;
417         
418         tcp = &cp->tcps[tileno];
419
420         array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
421         pi = (opj_pi_iterator_t *) opj_malloc(array_size);
422         if(!pi) {
423                 /* TODO: throw an error */
424                 return NULL;
425         }
426         
427         for (pino = 0; pino < tcp->numpocs + 1; pino++) {       /* change */
428                 int maxres = 0;
429                 int maxprec = 0;
430                 p = tileno % cp->tw;
431                 q = tileno / cp->tw;
432
433                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
434                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
435                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
436                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
437                 pi[pino].numcomps = image->numcomps;
438
439                 array_size = image->numcomps * sizeof(opj_pi_comp_t);
440                 pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
441                 if(!pi[pino].comps) {
442                         /* TODO: throw an error */
443                         pi_destroy(pi, cp, tileno);
444                         return NULL;
445                 }
446                 memset(pi[pino].comps, 0, array_size);
447                 
448                 for (compno = 0; compno < pi->numcomps; compno++) {
449                         int tcx0, tcy0, tcx1, tcy1;
450                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
451                         tccp = &tcp->tccps[compno];
452                         comp->dx = image->comps[compno].dx;
453                         comp->dy = image->comps[compno].dy;
454                         comp->numresolutions = tccp->numresolutions;
455
456                         array_size = comp->numresolutions * sizeof(opj_pi_resolution_t);
457                         comp->resolutions =     (opj_pi_resolution_t *) opj_malloc(array_size);
458                         if(!comp->resolutions) {
459                                 /* TODO: throw an error */
460                                 pi_destroy(pi, cp, tileno);
461                                 return NULL;
462                         }
463
464                         tcx0 = int_ceildiv(pi->tx0, comp->dx);
465                         tcy0 = int_ceildiv(pi->ty0, comp->dy);
466                         tcx1 = int_ceildiv(pi->tx1, comp->dx);
467                         tcy1 = int_ceildiv(pi->ty1, comp->dy);
468                         if (comp->numresolutions > maxres) {
469                                 maxres = comp->numresolutions;
470                         }
471
472                         for (resno = 0; resno < comp->numresolutions; resno++) {
473                                 int levelno;
474                                 int rx0, ry0, rx1, ry1;
475                                 int px0, py0, px1, py1;
476                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
477                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
478                                         res->pdx = tccp->prcw[resno];
479                                         res->pdy = tccp->prch[resno];
480                                 } else {
481                                         res->pdx = 15;
482                                         res->pdy = 15;
483                                 }
484                                 levelno = comp->numresolutions - 1 - resno;
485                                 rx0 = int_ceildivpow2(tcx0, levelno);
486                                 ry0 = int_ceildivpow2(tcy0, levelno);
487                                 rx1 = int_ceildivpow2(tcx1, levelno);
488                                 ry1 = int_ceildivpow2(tcy1, levelno);
489                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
490                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
491                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
492                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
493                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
494                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
495                                 
496                                 if (res->pw*res->ph > maxprec) {
497                                         maxprec = res->pw*res->ph;
498                                 }
499                                 
500                         }
501                 }
502                 
503                 tccp = &tcp->tccps[0];
504                 pi[pino].step_p = 1;
505                 pi[pino].step_c = maxprec * pi[pino].step_p;
506                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
507                 pi[pino].step_l = maxres * pi[pino].step_r;
508                 
509                 if (pino == 0) {
510                         array_size = image->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int);
511                         pi[pino].include = (short int *) opj_malloc(array_size);
512                         if(!pi[pino].include) {
513                                 /* TODO: throw an error */
514                                 pi_destroy(pi, cp, tileno);
515                                 return NULL;
516                         }
517                 }
518                 else {
519                         pi[pino].include = pi[pino - 1].include;
520                 }
521                 
522                 if (tcp->POC == 0) {
523                         pi[pino].first = 1;
524                         pi[pino].poc.resno0 = 0;
525                         pi[pino].poc.compno0 = 0;
526                         pi[pino].poc.layno1 = tcp->numlayers;
527                         pi[pino].poc.resno1 = maxres;
528                         pi[pino].poc.compno1 = image->numcomps;
529                         pi[pino].poc.prg = tcp->prg;
530                 } else {
531                         pi[pino].first = 1;
532                         pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
533                         pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
534                         pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
535                         pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
536                         pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
537                         pi[pino].poc.prg = tcp->pocs[pino].prg;
538                 }
539                 pi[pino].poc.layno0  = 0;
540                 pi[pino].poc.precno0 = 0; 
541                 pi[pino].poc.precno1 = maxprec;
542                         
543         }
544         
545         return pi;
546 }
547
548
549 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, int pino){ 
550         int p, q;
551         int compno, resno;
552         int maxres = 0;
553         int maxprec = 0;
554         opj_pi_iterator_t *pi = NULL;
555         opj_tcp_t *tcp = NULL;
556         opj_tccp_t *tccp = NULL;
557         size_t array_size;
558         
559         tcp = &cp->tcps[tileno];
560
561         array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
562         pi = (opj_pi_iterator_t *) opj_malloc(array_size);
563         pi->tp_on = cp->tp_on;
564         if(!pi) {       return NULL;}
565         for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
566                 p = tileno % cp->tw;
567                 q = tileno / cp->tw;
568
569                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
570                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
571                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
572                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
573                 pi[pino].numcomps = image->numcomps;
574                 
575                 array_size = image->numcomps * sizeof(opj_pi_comp_t);
576                 pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
577                 if(!pi[pino].comps) {
578                         pi_destroy(pi, cp, tileno);
579                         return NULL;
580                 }
581                 memset(pi[pino].comps, 0, array_size);
582                 
583                 for (compno = 0; compno < pi->numcomps; compno++) {
584                         int tcx0, tcy0, tcx1, tcy1;
585                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
586                         tccp = &tcp->tccps[compno];
587                         comp->dx = image->comps[compno].dx;
588                         comp->dy = image->comps[compno].dy;
589                         comp->numresolutions = tccp->numresolutions;
590
591                         array_size = comp->numresolutions * sizeof(opj_pi_resolution_t);
592                         comp->resolutions =     (opj_pi_resolution_t *) opj_malloc(array_size);
593                         if(!comp->resolutions) {
594                                 pi_destroy(pi, cp, tileno);
595                                 return NULL;
596                         }
597
598                         tcx0 = int_ceildiv(pi->tx0, comp->dx);
599                         tcy0 = int_ceildiv(pi->ty0, comp->dy);
600                         tcx1 = int_ceildiv(pi->tx1, comp->dx);
601                         tcy1 = int_ceildiv(pi->ty1, comp->dy);
602                         if (comp->numresolutions > maxres) {
603                                 maxres = comp->numresolutions;
604                         }
605
606                         for (resno = 0; resno < comp->numresolutions; resno++) {
607                                 int levelno;
608                                 int rx0, ry0, rx1, ry1;
609                                 int px0, py0, px1, py1;
610                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
611                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
612                                         res->pdx = tccp->prcw[resno];
613                                         res->pdy = tccp->prch[resno];
614                                 } else {
615                                         res->pdx = 15;
616                                         res->pdy = 15;
617                                 }
618                                 levelno = comp->numresolutions - 1 - resno;
619                                 rx0 = int_ceildivpow2(tcx0, levelno);
620                                 ry0 = int_ceildivpow2(tcy0, levelno);
621                                 rx1 = int_ceildivpow2(tcx1, levelno);
622                                 ry1 = int_ceildivpow2(tcy1, levelno);
623                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
624                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
625                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
626                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
627                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
628                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
629
630                                 if (res->pw*res->ph > maxprec) {
631                                         maxprec = res->pw * res->ph;
632                                 }
633                         }
634                 }
635                 
636                 tccp = &tcp->tccps[0];
637                 pi[pino].step_p = 1;
638                 pi[pino].step_c = maxprec * pi[pino].step_p;
639                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
640                 pi[pino].step_l = maxres * pi[pino].step_r;
641                 
642                 for (compno = 0; compno < pi->numcomps; compno++) {
643                         opj_pi_comp_t *comp = &pi->comps[compno];
644                         for (resno = 0; resno < comp->numresolutions; resno++) {
645                                 int dx, dy;
646                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
647                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
648                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
649                                 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
650                                 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
651                         }
652                 }
653
654                 if (pino == 0) {
655                         array_size = (tcp->numpocs+1) * tcp->numlayers * pi[pino].step_l * sizeof(short int);
656                         pi[pino].include = (short int *) opj_malloc(array_size);
657                         if(!pi[pino].include) {
658                                 pi_destroy(pi, cp, tileno);
659                                 return NULL;
660                         }
661                 }
662                 else {
663                         pi[pino].include = pi[pino - 1].include;
664                 }
665                 
666                 /* Generation of boundaries for each prog flag*/
667                         if (tcp->POC == 1){
668                                 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
669                                 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
670                                 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
671                                 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
672         tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
673                                 tcp->pocs[pino].prg  = tcp->pocs[pino].prg;
674                         }else {
675                                 tcp->pocs[pino].compS= 0;
676                                 tcp->pocs[pino].compE= image->numcomps;
677                                 tcp->pocs[pino].resS = 0;
678                                 tcp->pocs[pino].resE = maxres;
679                                 tcp->pocs[pino].layE = tcp->numlayers;
680                                 tcp->pocs[pino].prg  = tcp->prg;
681                         }
682                         tcp->pocs[pino].layS = 0;                               
683                         tcp->pocs[pino].prcS = 0;
684                         tcp->pocs[pino].prcE = maxprec;;
685                         tcp->pocs[pino].txS = pi[pino].tx0;
686                         tcp->pocs[pino].txE = pi[pino].tx1;
687                         tcp->pocs[pino].tyS = pi[pino].ty0;
688                         tcp->pocs[pino].tyE = pi[pino].ty1;
689                         tcp->pocs[pino].dx = pi[pino].dx;
690                         tcp->pocs[pino].dy = pi[pino].dy;
691                 }
692                         return pi;
693         }
694
695
696
697 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
698         int compno, pino;
699         opj_tcp_t *tcp = &cp->tcps[tileno];
700         if(pi) {
701                 for (pino = 0; pino < tcp->numpocs + 1; pino++) {       
702                         if(pi[pino].comps) {
703                                 for (compno = 0; compno < pi->numcomps; compno++) {
704                                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
705                                         if(comp->resolutions) {
706                                                 opj_free(comp->resolutions);
707                                         }
708                                 }
709                                 opj_free(pi[pino].comps);
710                         }
711                 }
712                 if(pi->include) {
713                         opj_free(pi->include);
714                 }
715                 opj_free(pi);
716         }
717 }
718
719 bool pi_next(opj_pi_iterator_t * pi) {
720         switch (pi->poc.prg) {
721                 case LRCP:
722                         return pi_next_lrcp(pi);
723                 case RLCP:
724                         return pi_next_rlcp(pi);
725                 case RPCL:
726                         return pi_next_rpcl(pi);
727                 case PCRL:
728                         return pi_next_pcrl(pi);
729                 case CPRL:
730                         return pi_next_cprl(pi);
731                 case PROG_UNKNOWN:
732                         return false;
733         }
734         
735         return false;
736 }
737
738 int pi_check_next_level(int pos,opj_cp_t *cp,int tileno, int pino, char *prog){
739         int i,l;
740         opj_tcp_t *tcps =&cp->tcps[tileno];
741         opj_poc_t *tcp = &tcps->pocs[pino];
742         if(pos>=0){
743                 for(i=pos;pos>=0;i--){
744                         switch(prog[i]){
745                 case 'R':
746                         if(tcp->res_t==tcp->resE){
747                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
748                                 if(l==1){
749                                         return 1;
750                                 }else{
751                                         return 0;
752                                 }
753                         }else{
754                                 return 1;
755                         }
756                         break;
757                 case 'C':
758                         if(tcp->comp_t==tcp->compE){
759                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
760                                 if(l==1){
761                                         return 1;
762                                 }else{
763                                         return 0;
764                                 }
765                         }else{
766                                 return 1;
767                         }
768                         break;
769                 case 'L':
770                         if(tcp->lay_t==tcp->layE){
771                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
772                                 if(l==1){
773                                         return 1;
774                                 }else{
775                                         return 0;
776                                 }
777                         }else{
778                                 return 1;
779                         }
780                         break;
781                 case 'P':
782                         switch(tcp->prg){
783                                 case LRCP||RLCP:
784                                         if(tcp->prc_t == tcp->prcE){
785                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
786                                                 if(l==1){
787                                                         return 1;
788                                                 }else{
789                                                         return 0;
790                                                 }
791                                         }else{
792                                                 return 1;
793                                         }
794                                         break;
795                         default:
796                                 if(tcp->tx0_t == tcp->txE){
797                                         //TY 
798                                         if(tcp->ty0_t == tcp->tyE){
799                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
800                                                 if(l==1){
801                                                         return 1;
802                                                 }else{
803                                                         return 0;
804                                                 }
805                                         }else{
806                                                 return 1;
807                                         }//TY
808                                 }else{
809                                         return 1;
810                                 }
811                                 break;
812                         }//end case P
813                 }//end switch
814                 }//end for
815         }//end if
816         return 0;
817 }
818
819
820 void pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos,char final_encoding) {
821         char *prog;
822         int i,l;
823         int incr_top=1,resetX=0;
824         opj_tcp_t *tcps =&cp->tcps[tileno];
825         opj_poc_t *tcp= &tcps->pocs[pino];
826         prog = j2k_convert_progression_order(tcp->prg);
827
828         pi[pino].first = 1;
829         pi[pino].poc.prg = tcp->prg;    
830         
831         if(!(cp->tp_on & final_encoding)){
832                 pi[pino].poc.resno0 = tcp->resS;
833                 pi[pino].poc.resno1 = tcp->resE;
834                 pi[pino].poc.compno0 = tcp->compS;
835                 pi[pino].poc.compno1 = tcp->compE;
836                 pi[pino].poc.layno0 = tcp->layS;
837                 pi[pino].poc.layno1 = tcp->layE;
838                 pi[pino].poc.precno0 = tcp->prcS;
839                 pi[pino].poc.precno1 = tcp->prcE;
840                 pi[pino].poc.tx0 = tcp->txS;
841                 pi[pino].poc.ty0 = tcp->tyS;
842                 pi[pino].poc.tx1 = tcp->txE;
843                 pi[pino].poc.ty1 = tcp->tyE;
844         }else {
845                 for(i=tppos+1;i<4;i++){
846                         switch(prog[i]){
847                         case 'R':
848                                 pi[pino].poc.resno0 = tcp->resS;
849                                 pi[pino].poc.resno1 = tcp->resE;
850                                 break;
851                         case 'C':
852                                 pi[pino].poc.compno0 = tcp->compS;
853                                 pi[pino].poc.compno1 = tcp->compE;
854                                 break;
855                         case 'L':
856                                 pi[pino].poc.layno0 = tcp->layS;
857                                 pi[pino].poc.layno1 = tcp->layE;
858                                 break;
859                         case 'P':
860                                 switch(tcp->prg){
861                                         case LRCP:
862                                         case RLCP:
863                                                 pi[pino].poc.precno0 = tcp->prcS;
864                                                 pi[pino].poc.precno1 = tcp->prcE;
865                                                 break;
866                                         default:
867                                                 pi[pino].poc.tx0 = tcp->txS;
868                                                 pi[pino].poc.ty0 = tcp->tyS;
869                                                 pi[pino].poc.tx1 = tcp->txE;
870                                                 pi[pino].poc.ty1 = tcp->tyE;
871                                                 break;
872                                 }
873                                 break;
874                         }
875                 }
876
877                 if(tpnum==0){
878                         for(i=tppos;i>=0;i--){
879                                 switch(prog[i]){
880                                                 case 'C':
881                                                         tcp->comp_t = tcp->compS;
882                                                         pi[pino].poc.compno0 = tcp->comp_t;
883                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
884                                                         tcp->comp_t+=1;
885                                                         break;
886                                                 case 'R':
887                                                         tcp->res_t = tcp->resS;
888                                                         pi[pino].poc.resno0 = tcp->res_t;
889                                                         pi[pino].poc.resno1 = tcp->res_t+1;
890                                                         tcp->res_t+=1;
891                                                         break;
892                                                 case 'L':
893                                                         tcp->lay_t = tcp->layS;
894                                                         pi[pino].poc.layno0 = tcp->lay_t;
895                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
896                                                         tcp->lay_t+=1;
897                                                         break;
898                                                 case 'P':
899                                                         switch(tcp->prg){
900                                                                 case LRCP:
901                                                                 case RLCP:
902                                                                         tcp->prc_t = tcp->prcS;
903                                                                         pi[pino].poc.precno0 = tcp->prc_t;
904                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
905                                                                         tcp->prc_t+=1; 
906                                                                         break;
907                                                                 default:
908                                                                         tcp->tx0_t = tcp->txS;
909                                                                         tcp->ty0_t = tcp->tyS;
910                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
911                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
912                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
913                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
914                                                                         tcp->tx0_t = pi[pino].poc.tx1;
915                                                                         tcp->ty0_t = pi[pino].poc.ty1;
916                                                                         break;
917                                                         }
918                                                         break;
919                                 }
920                         }
921                         incr_top=1;
922                 }else{
923                         for(i=tppos;i>=0;i--){
924                                 switch(prog[i]){
925                                                 case 'C':
926                                                         pi[pino].poc.compno0 = tcp->comp_t-1;
927                                                         pi[pino].poc.compno1 = tcp->comp_t;
928                                                         break;
929                                                 case 'R':
930                                                         pi[pino].poc.resno0 = tcp->res_t-1;
931                                                         pi[pino].poc.resno1 = tcp->res_t;
932                                                         break;
933                                                 case 'L':
934                                                         pi[pino].poc.layno0 = tcp->lay_t-1;
935                                                         pi[pino].poc.layno1 = tcp->lay_t;
936                                                         break;
937                                                 case 'P':
938                                                         switch(tcp->prg){
939                                                                 case LRCP:
940                                                                 case RLCP:
941                                                                         pi[pino].poc.precno0 = tcp->prc_t-1;
942                                                                         pi[pino].poc.precno1 = tcp->prc_t;
943                                                                         break;
944                                                                 default:
945                                                                         pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
946                                                                         pi[pino].poc.tx1 = tcp->tx0_t ;
947                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
948                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
949                                                                         break;
950                                                         }
951                                                         break;
952                                 }
953                                 if(incr_top==1){
954                                         switch(prog[i]){
955                                                         case 'R':
956                                                                 if(tcp->res_t==tcp->resE){
957                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
958                                                                         if(l==1){
959                                                                                 tcp->res_t = tcp->resS;
960                                                                                 pi[pino].poc.resno0 = tcp->res_t;
961                                                                                 pi[pino].poc.resno1 = tcp->res_t+1;
962                                                                                 tcp->res_t+=1;
963                                                                                 incr_top=1;
964                                                                         }else{
965                                                                                 incr_top=0;
966                                                                         }
967                                                                 }else{
968                                                                         pi[pino].poc.resno0 = tcp->res_t;
969                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
970                                                                         tcp->res_t+=1;
971                                                                         incr_top=0;
972                                                                 }
973                                                                 break;
974                                                         case 'C':
975                                                                 if(tcp->comp_t ==tcp->compE){
976                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
977                                                                         if(l==1){
978                                                                                 tcp->comp_t = tcp->compS;
979                                                                                 pi[pino].poc.compno0 = tcp->comp_t;
980                                                                                 pi[pino].poc.compno1 = tcp->comp_t+1;
981                                                                                 tcp->comp_t+=1;
982                                                                                 incr_top=1;
983                                                                         }else{
984                                                                                 incr_top=0;
985                                                                         }
986                                                                 }else{
987                                                                         pi[pino].poc.compno0 = tcp->comp_t;
988                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
989                                                                         tcp->comp_t+=1;
990                                                                         incr_top=0;
991                                                                 }
992                                                                 break;  
993                                                         case 'L':
994                                                                 if(tcp->lay_t == tcp->layE){
995                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
996                                                                         if(l==1){
997                                                                                 tcp->lay_t = tcp->layS;
998                                                                                 pi[pino].poc.layno0 = tcp->lay_t;
999                                                                                 pi[pino].poc.layno1 = tcp->lay_t+1;
1000                                                                                 tcp->lay_t+=1;
1001                                                                                 incr_top=1;
1002                                                                         }else{
1003                                                                                 incr_top=0;
1004                                                                         }
1005                                                                 }else{
1006                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1007                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1008                                                                         tcp->lay_t+=1;
1009                                                                         incr_top=0;
1010                                                                 }
1011                                                                 break;
1012                                                         case 'P':
1013                                                                 switch(tcp->prg){
1014                                                                         case LRCP:
1015                                                                         case RLCP:
1016                                                                                 if(tcp->prc_t == tcp->prcE){
1017                                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
1018                                                                                         if(l==1){
1019                                                                                                 tcp->prc_t = tcp->prcS;
1020                                                                                                 pi[pino].poc.precno0 = tcp->prc_t;
1021                                                                                                 pi[pino].poc.precno1 = tcp->prc_t+1;
1022                                                                                                 tcp->prc_t+=1;
1023                                                                                                 incr_top=1;
1024                                                                                         }else{
1025                                                                                                 incr_top=0;
1026                                                                                         }
1027                                                                                 }else{
1028                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1029                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1030                                                                                         tcp->prc_t+=1;
1031                                                                                         incr_top=0;
1032                                                                                 }
1033                                                                                 break;
1034                                                                         default:
1035                                                                                 if(tcp->tx0_t >= tcp->txE){
1036                                                                                         if(tcp->ty0_t >= tcp->tyE){
1037                                                                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
1038                                                                                                 if(l==1){
1039                                                                                                         tcp->ty0_t = tcp->tyS;
1040                                                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
1041                                                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1042                                                                                                         tcp->ty0_t = pi[pino].poc.ty1;
1043                                                                                                         incr_top=1;resetX=1;
1044                                                                                                 }else{
1045                                                                                                         incr_top=0;resetX=0;
1046                                                                                                 }
1047                                                                                         }else{
1048                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1049                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1050                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1051                                                                                                 incr_top=0;resetX=1;
1052                                                                                         }
1053                                                                                         if(resetX==1){
1054                                                                                                 tcp->tx0_t = tcp->txS;
1055                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
1056                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1057                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
1058                                                                                         }
1059                                                                                 }else{
1060                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1061                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1062                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1063                                                                                         incr_top=0;
1064                                                                                 }
1065                                                                                 break;
1066                                                                 }
1067                                                                 break;
1068                                         }
1069                                 }
1070                         }
1071                 }
1072         }
1073 }
1074
1075